Skip to content

Commit 6c05ac5

Browse files
authored
feat: tokens endpoint (#129)
1 parent 18b848d commit 6c05ac5

6 files changed

Lines changed: 255 additions & 3 deletions

File tree

src/API/Tokens.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArkEcosystem\Client\API;
6+
7+
class Tokens extends AbstractAPI
8+
{
9+
/**
10+
* Get all tokens.
11+
*
12+
* @param array $query
13+
*
14+
* @return array
15+
*/
16+
public function all(array $query = []): ?array
17+
{
18+
if (isset($query['whitelist'])) {
19+
return $this->requestPost('tokens', $query);
20+
}
21+
22+
return $this->requestGet('tokens', $query);
23+
}
24+
25+
/**
26+
* Get a token by contract address.
27+
*
28+
* @param string $address
29+
*
30+
* @return array
31+
*/
32+
public function get(string $address): ?array
33+
{
34+
return $this->requestGet("tokens/{$address}");
35+
}
36+
37+
/**
38+
* Get token holders for a given token.
39+
*
40+
* @param string $address
41+
* @param array $query
42+
*
43+
* @return array
44+
*/
45+
public function holders(string $address, array $query = []): ?array
46+
{
47+
return $this->requestGet("tokens/{$address}/holders", $query);
48+
}
49+
50+
/**
51+
* Get token transfers for a given token.
52+
*
53+
* @param string $address
54+
* @param array $query
55+
*
56+
* @return array
57+
*/
58+
public function transfersByToken(string $address, array $query = []): ?array
59+
{
60+
return $this->requestGet("tokens/{$address}/transfers", $query);
61+
}
62+
63+
/**
64+
* Get all token transfers.
65+
*
66+
* @param array $query
67+
*
68+
* @return array
69+
*/
70+
public function transfers(array $query = []): ?array
71+
{
72+
return $this->requestGet('tokens/transfers', $query);
73+
}
74+
75+
/**
76+
* Get the token whitelist.
77+
*
78+
* @param array $query
79+
*
80+
* @return array
81+
*/
82+
public function whitelist(array $query = []): ?array
83+
{
84+
return $this->requestGet('tokens/whitelist', $query);
85+
}
86+
}

src/API/Wallets.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,37 @@ public function top(): ?array
9191
{
9292
return $this->requestGet('wallets/top');
9393
}
94+
95+
/**
96+
* Get all tokens held by the given wallet.
97+
*
98+
* @param string $id
99+
* @param array $query
100+
*
101+
* @return array
102+
*/
103+
public function tokensFor(string $id, array $query = []): ?array
104+
{
105+
if (isset($query['whitelist'])) {
106+
return $this->requestPost("wallets/{$id}/tokens", $query);
107+
}
108+
109+
return $this->requestGet("wallets/{$id}/tokens", $query);
110+
}
111+
112+
/**
113+
* Get all tokens held by wallets.
114+
*
115+
* @param array $query
116+
*
117+
* @return array
118+
*/
119+
public function tokens(array $query = []): ?array
120+
{
121+
if (isset($query['whitelist'])) {
122+
return $this->requestPost('wallets/tokens', $query);
123+
}
124+
125+
return $this->requestGet('wallets/tokens', $query);
126+
}
94127
}

src/ArkClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ArkEcosystem\Client\API\Peers;
1515
use ArkEcosystem\Client\API\Receipts;
1616
use ArkEcosystem\Client\API\Rounds;
17+
use ArkEcosystem\Client\API\Tokens;
1718
use ArkEcosystem\Client\API\Transactions;
1819
use ArkEcosystem\Client\API\Validators;
1920
use ArkEcosystem\Client\API\Votes;
@@ -79,6 +80,11 @@ public function rounds(): Rounds
7980
return new Rounds($this->connection);
8081
}
8182

83+
public function tokens(): Tokens
84+
{
85+
return new Tokens($this->connection);
86+
}
87+
8288
public function transactions(): Transactions
8389
{
8490
return new Transactions($this->connection);

tests/API/TokensTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ArkEcosystem\Tests\Client\API;
6+
7+
use ArkEcosystem\Client\ArkClient;
8+
9+
it('calls correct url for all', function () {
10+
$this->assertResponse('GET', 'tokens', function (ArkClient $client) {
11+
return $client->tokens()->all();
12+
});
13+
});
14+
15+
it('calls correct url for all with whitelist', function () {
16+
$this->assertResponse(
17+
'POST',
18+
'tokens',
19+
function (ArkClient $client) {
20+
return $client->tokens()->all([
21+
'whitelist' => ['0x1234567890abcdef1234567890abcdef12345678'],
22+
]);
23+
},
24+
expectedRequestBody: [
25+
'whitelist' => ['0x1234567890abcdef1234567890abcdef12345678'],
26+
]
27+
);
28+
});
29+
30+
it('sends all whitelist values in the request body', function () {
31+
$this->assertResponse(
32+
'POST',
33+
'tokens',
34+
function (ArkClient $client) {
35+
return $client->tokens()->all([
36+
'whitelist' => [
37+
'0x1234567890abcdef1234567890abcdef12345678',
38+
'0xabcdef1234567890abcdef1234567890abcdef12',
39+
],
40+
]);
41+
},
42+
expectedRequestBody: [
43+
'whitelist' => [
44+
'0x1234567890abcdef1234567890abcdef12345678',
45+
'0xabcdef1234567890abcdef1234567890abcdef12',
46+
],
47+
]
48+
);
49+
});
50+
51+
it('calls correct url for whitelist with query', function () {
52+
$this->assertResponse('GET', 'tokens/whitelist?page=2&limit=10', function (ArkClient $client) {
53+
return $client->tokens()->whitelist([
54+
'page' => 2,
55+
'limit' => 10,
56+
]);
57+
});
58+
});
59+
60+
it('calls correct url for get', function () {
61+
$this->assertResponse('GET', 'tokens/dummy', function (ArkClient $client) {
62+
return $client->tokens()->get('dummy');
63+
});
64+
});
65+
66+
it('calls correct url for holders', function () {
67+
$this->assertResponse('GET', 'tokens/dummy/holders', function (ArkClient $client) {
68+
return $client->tokens()->holders('dummy');
69+
});
70+
});
71+
72+
it('calls correct url for transfers by token', function () {
73+
$this->assertResponse('GET', 'tokens/dummy/transfers', function (ArkClient $client) {
74+
return $client->tokens()->transfersByToken('dummy');
75+
});
76+
});
77+
78+
it('calls correct url for all transfers', function () {
79+
$this->assertResponse('GET', 'tokens/transfers', function (ArkClient $client) {
80+
return $client->tokens()->transfers();
81+
});
82+
});
83+
84+
it('calls correct url for whitelist', function () {
85+
$this->assertResponse('GET', 'tokens/whitelist', function (ArkClient $client) {
86+
return $client->tokens()->whitelist();
87+
});
88+
});

tests/API/WalletsTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,31 @@
4747
return $client->wallets()->votes('dummy');
4848
});
4949
});
50+
51+
it('calls correct url for all wallet tokens', function () {
52+
$this->assertResponse('GET', 'wallets/tokens', function (ArkClient $client) {
53+
return $client->wallets()->tokens();
54+
});
55+
});
56+
57+
it('calls correct url for all wallet tokens with query', function () {
58+
$this->assertResponse('GET', 'wallets/tokens?limit=10', function (ArkClient $client) {
59+
return $client->wallets()->tokens([
60+
'limit' => 10,
61+
]);
62+
});
63+
});
64+
65+
it('calls correct url for a wallet tokens', function () {
66+
$this->assertResponse('GET', 'wallets/dummy/tokens', function (ArkClient $client) {
67+
return $client->wallets()->tokensFor('dummy');
68+
});
69+
});
70+
71+
it('calls correct url for a wallet tokens with query', function () {
72+
$this->assertResponse('GET', 'wallets/dummy/tokens?limit=10', function (ArkClient $client) {
73+
return $client->wallets()->tokensFor('dummy', [
74+
'limit' => 10,
75+
]);
76+
});
77+
});

tests/TestCase.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,30 @@ abstract class TestCase extends BaseTestCase
2828
* @param callable $callback
2929
* @param array|null $expectedBody
3030
*/
31-
protected function assertResponse(string $method, string $path, callable $callback, array $expectedBody = [], string $expectedApi = 'api', array $response = []): void
32-
{
31+
protected function assertResponse(
32+
string $method,
33+
string $path,
34+
callable $callback,
35+
array $expectedBody = [],
36+
string $expectedApi = 'api',
37+
array $response = [],
38+
?array $expectedRequestBody = null
39+
): void {
3340
$hosts = [
3441
'api' => 'https://dwallets-evm.mainsailhq.com/api',
3542
'transactions' => 'https://dwallets-evm.mainsailhq.com/tx/api',
3643
'evm' => 'https://dwallets-evm.mainsailhq.com/evm',
3744
];
3845

3946
$mockHandler = new MockHandler([
40-
function (Request $request) use ($method, $path, $response, $hosts, $expectedApi) {
47+
function (Request $request) use ($method, $path, $response, $hosts, $expectedApi, $expectedRequestBody) {
4148
$this->assertSame($method, $request->getMethod());
4249
$this->assertSame($hosts[$expectedApi].'/'.$path, $request->getUri()->__toString());
4350

51+
if ($expectedRequestBody !== null) {
52+
$this->assertSame($expectedRequestBody, json_decode($request->getBody()->getContents(), true));
53+
}
54+
4455
return new Response(200, [], json_encode($response));
4556
},
4657
]);

0 commit comments

Comments
 (0)