Get the Token Balance of an address - RPC Call
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.
ERC-20 or compatible
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
- Exclude the
0x
-->CD531Ae9EFCCE479654c4926dec5F6209531Ca7b
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: {YOUR_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)
TRC-20 (Tron)
Steps
Step_1: Fetch the Smart Contract Address
- For example USDT over Tron:
TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t
- Convert the Address in hexadecimal format. You may use an online service.
- Address converted:
0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C
Step_2: Fetch the Wallet Address
- For example
TJDENsfBJs4RFETt1X1W8wMDc8M5XnJhCe
- Convert the Address in hexadecimal format. You may use an online service.
- Exclude the
0x
-->415a67fa7cc56bd6d043a98e17d329c1dc9e14753f
Step_3: Make an RPC call with the method eth_call
eth_call
Example request:
- "
to
" is the ContractAddress for USDT (converted)0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C
- "
data
" has three parts:- Method ID:
70a08231
- This is the 4-byte function selector for ABIbalanceOf(address)
. - Padding: "000000000000000000000000"
- Wallet Address:
415a67fa7cc56bd6d043a98e17d329c1dc9e14753f
- The address to query the balance for, padded to 32 bytes (64 characters)415a67fa7cc56bd6d043a98e17d329c1dc9e14753f
- The full data field will be: "
0x70a08231
" + "000000000000000000000000
" + "415a67fa7cc56bd6d043a98e17d329c1dc9e14753f
"
- Method ID:
curl --location 'https://api.tatum.io/v3/blockchain/node/tron-mainnet/jsonrpc' \
--header 'Content-Type: application/json' \
--header 'x-api-key: {YOUR_API_KEY}' \
--data '{
"jsonrpc":"2.0",
"method":"eth_call",
"params":[
{
"to": "0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C",
"data": "0x70a082310000000000000000000000415a67fa7cc56bd6d043a98e17d329c1dc9e14753f"
},
"latest"
],
"id":1
}'
//response:
{
"jsonrpc": "2.0",
"id": 1,
"result": "0x00000000000000000000000000000000000000000000000000003444b797a568"
}
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 (USDT 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 4 months ago