diff --git a/src/API/Tokens.php b/src/API/Tokens.php new file mode 100644 index 0000000..6355385 --- /dev/null +++ b/src/API/Tokens.php @@ -0,0 +1,86 @@ +requestPost('tokens', $query); + } + + return $this->requestGet('tokens', $query); + } + + /** + * Get a token by contract address. + * + * @param string $address + * + * @return array + */ + public function get(string $address): ?array + { + return $this->requestGet("tokens/{$address}"); + } + + /** + * Get token holders for a given token. + * + * @param string $address + * @param array $query + * + * @return array + */ + public function holders(string $address, array $query = []): ?array + { + return $this->requestGet("tokens/{$address}/holders", $query); + } + + /** + * Get token transfers for a given token. + * + * @param string $address + * @param array $query + * + * @return array + */ + public function transfersByToken(string $address, array $query = []): ?array + { + return $this->requestGet("tokens/{$address}/transfers", $query); + } + + /** + * Get all token transfers. + * + * @param array $query + * + * @return array + */ + public function transfers(array $query = []): ?array + { + return $this->requestGet('tokens/transfers', $query); + } + + /** + * Get the token whitelist. + * + * @param array $query + * + * @return array + */ + public function whitelist(array $query = []): ?array + { + return $this->requestGet('tokens/whitelist', $query); + } +} diff --git a/src/API/Wallets.php b/src/API/Wallets.php index f473a8c..ab2d3dd 100644 --- a/src/API/Wallets.php +++ b/src/API/Wallets.php @@ -91,4 +91,37 @@ public function top(): ?array { return $this->requestGet('wallets/top'); } + + /** + * Get all tokens held by the given wallet. + * + * @param string $id + * @param array $query + * + * @return array + */ + public function tokensFor(string $id, array $query = []): ?array + { + if (isset($query['whitelist'])) { + return $this->requestPost("wallets/{$id}/tokens", $query); + } + + return $this->requestGet("wallets/{$id}/tokens", $query); + } + + /** + * Get all tokens held by wallets. + * + * @param array $query + * + * @return array + */ + public function tokens(array $query = []): ?array + { + if (isset($query['whitelist'])) { + return $this->requestPost('wallets/tokens', $query); + } + + return $this->requestGet('wallets/tokens', $query); + } } diff --git a/src/ArkClient.php b/src/ArkClient.php index 8b97082..043fee9 100644 --- a/src/ArkClient.php +++ b/src/ArkClient.php @@ -14,6 +14,7 @@ use ArkEcosystem\Client\API\Peers; use ArkEcosystem\Client\API\Receipts; use ArkEcosystem\Client\API\Rounds; +use ArkEcosystem\Client\API\Tokens; use ArkEcosystem\Client\API\Transactions; use ArkEcosystem\Client\API\Validators; use ArkEcosystem\Client\API\Votes; @@ -79,6 +80,11 @@ public function rounds(): Rounds return new Rounds($this->connection); } + public function tokens(): Tokens + { + return new Tokens($this->connection); + } + public function transactions(): Transactions { return new Transactions($this->connection); diff --git a/tests/API/TokensTest.php b/tests/API/TokensTest.php new file mode 100644 index 0000000..d9b7b40 --- /dev/null +++ b/tests/API/TokensTest.php @@ -0,0 +1,88 @@ +assertResponse('GET', 'tokens', function (ArkClient $client) { + return $client->tokens()->all(); + }); +}); + +it('calls correct url for all with whitelist', function () { + $this->assertResponse( + 'POST', + 'tokens', + function (ArkClient $client) { + return $client->tokens()->all([ + 'whitelist' => ['0x1234567890abcdef1234567890abcdef12345678'], + ]); + }, + expectedRequestBody: [ + 'whitelist' => ['0x1234567890abcdef1234567890abcdef12345678'], + ] + ); +}); + +it('sends all whitelist values in the request body', function () { + $this->assertResponse( + 'POST', + 'tokens', + function (ArkClient $client) { + return $client->tokens()->all([ + 'whitelist' => [ + '0x1234567890abcdef1234567890abcdef12345678', + '0xabcdef1234567890abcdef1234567890abcdef12', + ], + ]); + }, + expectedRequestBody: [ + 'whitelist' => [ + '0x1234567890abcdef1234567890abcdef12345678', + '0xabcdef1234567890abcdef1234567890abcdef12', + ], + ] + ); +}); + +it('calls correct url for whitelist with query', function () { + $this->assertResponse('GET', 'tokens/whitelist?page=2&limit=10', function (ArkClient $client) { + return $client->tokens()->whitelist([ + 'page' => 2, + 'limit' => 10, + ]); + }); +}); + +it('calls correct url for get', function () { + $this->assertResponse('GET', 'tokens/dummy', function (ArkClient $client) { + return $client->tokens()->get('dummy'); + }); +}); + +it('calls correct url for holders', function () { + $this->assertResponse('GET', 'tokens/dummy/holders', function (ArkClient $client) { + return $client->tokens()->holders('dummy'); + }); +}); + +it('calls correct url for transfers by token', function () { + $this->assertResponse('GET', 'tokens/dummy/transfers', function (ArkClient $client) { + return $client->tokens()->transfersByToken('dummy'); + }); +}); + +it('calls correct url for all transfers', function () { + $this->assertResponse('GET', 'tokens/transfers', function (ArkClient $client) { + return $client->tokens()->transfers(); + }); +}); + +it('calls correct url for whitelist', function () { + $this->assertResponse('GET', 'tokens/whitelist', function (ArkClient $client) { + return $client->tokens()->whitelist(); + }); +}); diff --git a/tests/API/WalletsTest.php b/tests/API/WalletsTest.php index f2c04cd..bbd8849 100644 --- a/tests/API/WalletsTest.php +++ b/tests/API/WalletsTest.php @@ -47,3 +47,31 @@ return $client->wallets()->votes('dummy'); }); }); + +it('calls correct url for all wallet tokens', function () { + $this->assertResponse('GET', 'wallets/tokens', function (ArkClient $client) { + return $client->wallets()->tokens(); + }); +}); + +it('calls correct url for all wallet tokens with query', function () { + $this->assertResponse('GET', 'wallets/tokens?limit=10', function (ArkClient $client) { + return $client->wallets()->tokens([ + 'limit' => 10, + ]); + }); +}); + +it('calls correct url for a wallet tokens', function () { + $this->assertResponse('GET', 'wallets/dummy/tokens', function (ArkClient $client) { + return $client->wallets()->tokensFor('dummy'); + }); +}); + +it('calls correct url for a wallet tokens with query', function () { + $this->assertResponse('GET', 'wallets/dummy/tokens?limit=10', function (ArkClient $client) { + return $client->wallets()->tokensFor('dummy', [ + 'limit' => 10, + ]); + }); +}); diff --git a/tests/TestCase.php b/tests/TestCase.php index 5d38ae8..64cc507 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,8 +28,15 @@ abstract class TestCase extends BaseTestCase * @param callable $callback * @param array|null $expectedBody */ - protected function assertResponse(string $method, string $path, callable $callback, array $expectedBody = [], string $expectedApi = 'api', array $response = []): void - { + protected function assertResponse( + string $method, + string $path, + callable $callback, + array $expectedBody = [], + string $expectedApi = 'api', + array $response = [], + ?array $expectedRequestBody = null + ): void { $hosts = [ 'api' => 'https://dwallets-evm.mainsailhq.com/api', 'transactions' => 'https://dwallets-evm.mainsailhq.com/tx/api', @@ -37,10 +44,14 @@ protected function assertResponse(string $method, string $path, callable $callba ]; $mockHandler = new MockHandler([ - function (Request $request) use ($method, $path, $response, $hosts, $expectedApi) { + function (Request $request) use ($method, $path, $response, $hosts, $expectedApi, $expectedRequestBody) { $this->assertSame($method, $request->getMethod()); $this->assertSame($hosts[$expectedApi].'/'.$path, $request->getUri()->__toString()); + if ($expectedRequestBody !== null) { + $this->assertSame($expectedRequestBody, json_decode($request->getBody()->getContents(), true)); + } + return new Response(200, [], json_encode($response)); }, ]);