Skip to content

Commit 0b45ed2

Browse files
authored
Merge pull request #7 from seregazhuk/bugfix/use-biginter-for-account-balances
bugfix: use BigInteger for balances
2 parents d96ebd0 + 8e8805b commit 0b45ed2

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

src/Module/Accounts/Accounts.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(private readonly EtherscanClient $client) {}
2525
/**
2626
* @see https://docs.etherscan.io/api-endpoints/accounts#get-ether-balance-for-a-single-address
2727
*/
28-
public function getBalance(string $address, AccountBalanceTag $tag = AccountBalanceTag::LATEST): string
28+
public function getBalance(string $address, AccountBalanceTag $tag = AccountBalanceTag::LATEST): BigInteger
2929
{
3030
$params = [
3131
'tag' => $tag->value,
@@ -36,7 +36,7 @@ public function getBalance(string $address, AccountBalanceTag $tag = AccountBala
3636
/** @var array{result: string} $json */
3737
$json = json_decode($response->getBody()->getContents(), true);
3838

39-
return $json['result'];
39+
return new BigInteger($json['result'], 16);
4040
}
4141

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

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

6464
/**

src/Module/Accounts/Model/Balance.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
namespace seregazhuk\EtherscanApi\Module\Accounts\Model;
66

7+
use phpseclib3\Math\BigInteger;
8+
79
final class Balance
810
{
911
public function __construct(
1012
public readonly string $account,
11-
public readonly string $balance,
13+
public readonly BigInteger $balance,
1214
) {}
1315
}

tests/Module/AccountsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function it_retrieves_account_balance(): void
5858
->willReturn(new Response(200, [], $json));
5959

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

6464
#[Test]
@@ -129,9 +129,9 @@ public function it_retrieves_accounts_balances(): void
129129
$balances = $this->accounts->getBalances(['0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', '0x63a9975ba31b0b9626b34300f7f627147df1f526']);
130130
$this->assertCount(2, $balances);
131131
$this->assertSame('0xddbd2b932c763ba5b1b7ae3b362eac3e8d40121a', $balances[0]->account);
132-
$this->assertSame('27000616846559600000999', $balances[0]->balance);
132+
$this->assertTrue((new BigInteger('27000616846559600000999', 16))->equals($balances[0]->balance));
133133
$this->assertSame('0x63a9975ba31b0b9626b34300f7f627147df1f526', $balances[1]->account);
134-
$this->assertSame('2039670355000', $balances[1]->balance);
134+
$this->assertTrue((new BigInteger('2039670355000', 16))->equals($balances[1]->balance));
135135
}
136136

137137
#[Test]

0 commit comments

Comments
 (0)