Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions src/API/Tokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Client\API;

class Tokens extends AbstractAPI
{
/**
* Get all tokens.
*
* @param array $query
*
* @return array
*/
public function all(array $query = []): ?array
{
if (isset($query['whitelist'])) {
return $this->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);
}
}
33 changes: 33 additions & 0 deletions src/API/Wallets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions src/ArkClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
88 changes: 88 additions & 0 deletions tests/API/TokensTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace ArkEcosystem\Tests\Client\API;

use ArkEcosystem\Client\ArkClient;

it('calls correct url for all', function () {
$this->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();
});
});
28 changes: 28 additions & 0 deletions tests/API/WalletsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
});
});
17 changes: 14 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,30 @@ 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',
'evm' => 'https://dwallets-evm.mainsailhq.com/evm',
];

$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));
},
]);
Expand Down