DEV Community

Ayo Solomon
Ayo Solomon

Posted on

Understanding Ethereum JSONRPC Calls

log

How to get the balance of a token in a smart contract? It is easier than you think. Let’s see how to do it.

To begin with here are a list of hexcode of all major rpc methods, thought you moght need them, thank me with a clap.

  1. eth_sendTransaction: 0x61P62
  2. eth_call: 0x70a08231
  3. eth_getLogs: 0x38404ec1
  4. eth_getBalance: 0x70a08231
  5. eth_getCode: 0x3f2fde38
  6. eth_getStorageAt: 0x6e9d7a86
  7. eth_getTransactionCount: 0x068a23cb
  8. eth_getTransactionReceipt: 0x2ef17100
  9. eth_estimateGas: 0x27a46b71
  10. eth_getBlockByHash: 0x9e33679d
  11. eth_getBlockByNumber: 0x5b86e593
  12. eth_getTransactionByHash: 0x03f123db
  13. eth_getTransactionByBlockHashAndIndex: 0x6b1ee0e2
  14. eth_getTransactionByBlockNumberAndIndex: 0x61b26506
  15. eth_getUncleByBlockHashAndIndex: 0x0ff83c09
  16. eth_getUncleByBlockNumberAndIndex: 0x04b88d73
  17. eth_getCompilers: 0x29eeaa28
  18. eth_compileSolidity: 0x43c251a0
  19. eth_compileLLL: 0x38cfae69
  20. eth_compileSerpent: 0x4db3890e

Let's explain what these are and how they work?

When a web3 developer interacts with node through any rpc provider you send a request through JSON RPC either over HTTP or WebSocket; as a single request or as a batched array. Requests as usually in this format:

{"id":1234,"jsonrpc": "2.0", "method": "eth_foo", "params":[...] }
Enter fullscreen mode Exit fullscreen mode

when you make a call to through the rpc(Remote procedure call) you have to specify the method which tells the node what evm function you want to execute
that is what the "method" is for, the id is just a field to tell the node what request you are making and to ensure that the returned response is the same request sent, "params" we will get into that shortly

Therefore to get the balance of a wallet is as simple as

{
    "jsonrpc": "2.0",
    "method": "eth_getBalance",
    "params": [
        "0xbe991e317fbb4e82f7f99c62096f36b7a24278de",
        "latest"
    ],
    "id": 123
}
Enter fullscreen mode Exit fullscreen mode

Now send this as a post request to any EVM RPC but ensure that the address is in the chain, for example you need to get the balance of a polygon address then you need to make a post request to a polygon RPC like this

RPC post request

we get a return value of

{
    "jsonrpc": "2.0",
    "id": 123,
    "result": "0x4ced0c13df6aba72"
}
Enter fullscreen mode Exit fullscreen mode

when you convert the result from hex to integer, you should get the matic balance of that address(In this example)

So let's do a smart contract call?, yeah...

Smart contract call

Interesting right!!

let's back this up and do some explanations

{
    "jsonrpc": "2.0",
    "method": "eth_call",
    "params": [
        {
            "to": "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063",
            "from": "0xbe991e317fbb4e82f7f99c62096f36b7a24278de",
            "data": "0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de"
        },
        "latest"
    ],
    "id": 1
}
Enter fullscreen mode Exit fullscreen mode

we have our

ID - we already looked at what this is
Method- we have looked at this, this call is used to make a stateless query to the EVM, this means we are making a call that is not changing the state of any data and this won't cost gas at all
JSONRPC- this is the generally accepted rpc version for all node clients and it is version 2.0
Params- this field takes in the arguements we are taking to excute, so let's inspect this

  • we have

    • "to" is always a smart contract, so we enter the contract address here.
    • "from" is the user's address just the same way we call the balanceOf function in any solidity ERC20 contract
   function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

Enter fullscreen mode Exit fullscreen mode

, here we pass the user's address which we want to get it's token balance in the contract.

  • "latest" tells the node that we are making this request to the head of the block, so we want the most recent block result.

  • "data" is where all the magic happen.
    so how did we arrive at this "0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de", remember that list i posted above, the item number 2 "eth_call: 0x70a08231", so this hex code 0x70a08231 is actually a sha3 hash of the conventional ERC20 balanceOf function, what is done is that we get the function name and argument "balanceOf(address)" and then use sha3 encryption to hash, next the first four bytes of this hash which comprise it’s 4-byte signature is then padded with zeros, it is padded so that it can measure up to the required length of the ethereum ABI specification making it 32 characters if you remove the first 0x characters, now what about the the other characters after the zero, well that is your address or the user's address without the 0x.

So it is :
balanceOf=0x70a08231
padded zeros=000000000000000000000000
your address=be991e317fbb4e82f7f99c62096(with the first 2 characters 0x removed)

Join all that and we have the the data :0x70a08231000000000000000000000000be991e317fbb4e82f7f99c62096f36b7a24278de

In the same way you call balance of in a contract function

   function balanceOf("0xbe991e317fbb4e82f7f99c62096") 

Enter fullscreen mode Exit fullscreen mode

If you have learned something from this article, please give me a love or drop a comment.

Top comments (0)