From a39a2756d329c409ae1b70cb6a31a0e6c1b9a1d5 Mon Sep 17 00:00:00 2001 From: foxytanuki Date: Fri, 17 May 2024 21:20:33 +0900 Subject: [PATCH] Add balance() to EthJsonRPC.sol --- src/protocols/EthJsonRPC.sol | 15 +++++++++++++++ test/protocols/EthJsonRPC.t.sol | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/src/protocols/EthJsonRPC.sol b/src/protocols/EthJsonRPC.sol index ba60b3a..910f65e 100644 --- a/src/protocols/EthJsonRPC.sol +++ b/src/protocols/EthJsonRPC.sol @@ -32,6 +32,21 @@ contract EthJsonRPC { return val; } + /// @notice get the balance of an address. + /// @param addr the address to get the balance. + /// @return val the balance of the address. + function balance(address addr) public returns (uint256) { + bytes memory body = abi.encodePacked( + '{"jsonrpc":"2.0","method":"eth_getBalance","params":["', + LibString.toHexStringChecksummed(addr), + '","latest"],"id":1}' + ); + + JSONParserLib.Item memory item = doRequest(string(body)); + uint256 val = JSONParserLib.parseUintFromHex(trimQuotes(item.value())); + return val; + } + /// @notice call a contract function. /// @param to the address of the contract. /// @param data the data of the function. diff --git a/test/protocols/EthJsonRPC.t.sol b/test/protocols/EthJsonRPC.t.sol index 3a855df..96bbc03 100644 --- a/test/protocols/EthJsonRPC.t.sol +++ b/test/protocols/EthJsonRPC.t.sol @@ -14,6 +14,13 @@ contract EthJsonRPCTest is Test, SuaveEnabled { assertEq(nonce, 0); } + function testEthJsonRPCGetBalance() public { + EthJsonRPC ethjsonrpc = getEthJsonRPC(); + + uint256 balance = ethjsonrpc.balance(address(this)); + assertEq(balance, 0); + } + function testEthJsonRPCCall() public { EthJsonRPC ethjsonrpc = getEthJsonRPC();