Get the Token Balance of an address - RPC Call (ERC-20 or compatible)
Tatum provides you access to the blockchain via RPC.
Here's a guide on how to retrieve the token balance of an ERC-20 token from a blockchain address via RPC call. This makes use of standard eth_call
via balanceOf()
to call a SmartContract ABI.
Steps
Step_1: Fetch the Smart Contract Address
- For example USDC over ETH:
0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
Step_2: Fetch the Wallet Address
From which you want to retrieve the token balance
- For example:
0xCD531Ae9EFCCE479654c4926dec5F6209531Ca7b
Step_3: Make an RPC call with the method eth_call
eth_call
Example request:
- "
to
" is the ContractAddress for USDC - "
data
" has three parts:- Method ID:
70a08231
- This is the 4-byte function selector for ABIbalanceOf(address)
. - Padding: "000000000000000000000000"
- Wallet Address:
CD531Ae9EFCCE479654c4926dec5F6209531Ca7b
- The address to query the balance for, padded to 32 bytes (64 characters)CD531Ae9EFCCE479654c4926dec5F6209531Ca7b
- The full data field will be: "
0x70a08231
" + "000000000000000000000000
" + "CD531Ae9EFCCE479654c4926dec5F6209531Ca7b
"
- Method ID:
curl --location 'https://api.tatum.io/v3/blockchain/node/ethereum-mainnet/' \
--header 'Content-Type: application/json' \
--header 'x-api-key: ••••••' \
--data '{
"jsonrpc":"2.0",
"method":"eth_call",
"params":[
{
"to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"data": "0x70a08231000000000000000000000000CD531Ae9EFCCE479654c4926dec5F6209531Ca7b"
},
"latest"
],
"id":1
}'
//response:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x0000000000000000000000000000000000000000000000000000000000f4240"
}
Step_4: Interpret and convert the response.
Example request:
- The "
result
" field is the balance in hexadecimal (Wei). - Convert the result in hex to decimal and adjust for the token's decimals (USDC uses 6 decimals).
# Example response balance in hexadecimal
balance_hex = "0x0000000000000000000000000000000000000000000000000000000000f4240"
# Convert hexadecimal to integer
balance_wei = int(balance_hex, 16)
# USDC uses 6 decimals
decimals = 6
# Convert to human-readable format
balance_human_readable = balance_wei / (10 ** decimals)
print(balance_human_readable)
Good to know
- Depending on the Smart Contract code, the method to fetch the balance may vary.
- Tatum staff generally does not troubleshoot RPC requests, unless there's an error explicitly related to our infrastructure.
Updated about 1 month ago