Skip to content
Merged
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
6 changes: 3 additions & 3 deletions src/Module/Accounts/Accounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(private readonly EtherscanClient $client) {}
/**
* @see https://docs.etherscan.io/api-endpoints/accounts#get-ether-balance-for-a-single-address
*/
public function getBalance(string $address, AccountBalanceTag $tag = AccountBalanceTag::LATEST): string
public function getBalance(string $address, AccountBalanceTag $tag = AccountBalanceTag::LATEST): BigInteger
{
$params = [
'tag' => $tag->value,
Expand All @@ -36,7 +36,7 @@ public function getBalance(string $address, AccountBalanceTag $tag = AccountBala
/** @var array{result: string} $json */
$json = json_decode($response->getBody()->getContents(), true);

return $json['result'];
return new BigInteger($json['result'], 16);
}

/**
Expand All @@ -58,7 +58,7 @@ public function getBalances(array $addresses, AccountBalanceTag $tag = AccountBa
* }>} $json */
$json = json_decode($response->getBody()->getContents(), true);

return array_map(fn(array $balance): Balance => new Balance($balance['account'], $balance['balance']), $json['result']);
return array_map(fn(array $balance): Balance => new Balance($balance['account'], new BigInteger($balance['balance'], 16)), $json['result']);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Module/Accounts/Model/Balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace seregazhuk\EtherscanApi\Module\Accounts\Model;

use phpseclib3\Math\BigInteger;

final class Balance
{
public function __construct(
public readonly string $account,
public readonly string $balance,
public readonly BigInteger $balance,
) {}
}
6 changes: 3 additions & 3 deletions tests/Module/AccountsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function it_retrieves_account_balance(): void
->willReturn(new Response(200, [], $json));

$balance = $this->accounts->getBalance('0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae');
$this->assertSame('40891626854930000000000', $balance);
$this->assertTrue((new BigInteger('40891626854930000000000', 16))->equals($balance));
}

#[Test]
Expand Down Expand Up @@ -129,9 +129,9 @@ public function it_retrieves_accounts_balances(): void
$balances = $this->accounts->getBalances(['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0x63a9975ba31b0b9626b34300f7f627147df1f526']);
$this->assertCount(2, $balances);
$this->assertSame('0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', $balances[0]->account);
$this->assertSame('27000616846559600000999', $balances[0]->balance);
$this->assertTrue((new BigInteger('27000616846559600000999', 16))->equals($balances[0]->balance));
$this->assertSame('0x63a9975ba31b0b9626b34300f7f627147df1f526', $balances[1]->account);
$this->assertSame('2039670355000', $balances[1]->balance);
$this->assertTrue((new BigInteger('2039670355000', 16))->equals($balances[1]->balance));
}

#[Test]
Expand Down