From 8e8805b686e0fbacbda9f913e150111c68a148d0 Mon Sep 17 00:00:00 2001 From: sergeyzhuk Date: Tue, 21 Oct 2025 20:34:44 +0300 Subject: [PATCH] bugfix: use BigInteger for balances --- src/Module/Accounts/Accounts.php | 6 +++--- src/Module/Accounts/Model/Balance.php | 4 +++- tests/Module/AccountsTest.php | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Module/Accounts/Accounts.php b/src/Module/Accounts/Accounts.php index e9d0f5e..e5cfffc 100644 --- a/src/Module/Accounts/Accounts.php +++ b/src/Module/Accounts/Accounts.php @@ -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, @@ -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); } /** @@ -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']); } /** diff --git a/src/Module/Accounts/Model/Balance.php b/src/Module/Accounts/Model/Balance.php index 6ad2634..e11aad3 100644 --- a/src/Module/Accounts/Model/Balance.php +++ b/src/Module/Accounts/Model/Balance.php @@ -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, ) {} } diff --git a/tests/Module/AccountsTest.php b/tests/Module/AccountsTest.php index 425b78e..85c3096 100644 --- a/tests/Module/AccountsTest.php +++ b/tests/Module/AccountsTest.php @@ -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] @@ -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]