Skip to main content

How to Look Up Wallet Balance of an ERC20 Token using RPC API

Updated on
Dec 11, 2023

4 min read

Overview

Looking up the balance of an ERC20 token within a wallet using only RPC eth_call requests is possible, but it is a tedious and time-consuming process. Alternatively, QuickNode’s Token API is a free add-on that enables instant access to ERC20 token information, balances, and transfers with a single line of code.

What We Will Do

  • Look up an ERC20 token balance in a wallet using native RPC eth_call requests.
  • Look up an ERC20 token balance in a wallet using QuickNode's Token API

What you will need

Set Up Your Free QuickNode Endpoint

To build on Ethereum, you'll need an API endpoint to connect with the network. You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like 8x faster response times, you can leave the heavy lifting to us. Sign up for a free account here. We're going to use an Ethereum Mainnet node with the free Token API add-on enabled.

Look Up ERC20 Balance with eth_call RPC Request

  1. Identify a wallet address and an ERC20 token contract address. For this example, we are using Vitalik’s Ethereum wallet (0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045) and BAT Token (0x0D8775F648430679A709E98d2b0Cb6250d2887EF).
  2. The to parameter in our eth_call is the address of the BAT Token contract.
  3. Next, use the following process to create the data parameter of the eth_call so that the Ethereum node understands the request:
    • ERC20 compliant contracts include a method balanceOf(address) . The first thing we need to do is get the Keccak hash of balanceOf(address) and add the 0x prefix, which forms the string: 0x70a08231b98ef4ca268c9cc3f6b4590e4bfec28280db06bb5d45e689f2a360be
    • Next, select the first 10 characters of the hashed string, 0x70a08231.
    • Append 24 0s to the string 000000000000000000000000.
    • Remove the 0x prefix from Vitalik’s address d8dA6BF26964aF9D7eEd9e03E53415D37aA96045
    • Finally, combine all 3 strings together to create the data parameter for our eth_call. The string is: 0x70a08231000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045
  4. Make the following eth_call to the Ethereum RPC node:
curl https://your-subdomain-here.quiknode.pro/yourtoken/ \
-X POST \
-H "Content-Type: application/json" \
-H "x-qn-api-version: 1" \
--data '{
"id":67,
"jsonrpc":"2.0",
"method":"eth_call",
"params":[{"data":"0x70a08231000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa96045","to":"0x0D8775F648430679A709E98d2b0Cb6250d2887EF"}, "latest"]
}'

This is the response:

{"jsonrpc":"2.0","id":67,"result":"0x000000000000000000000000000000000000000000000000f26822562e5e767f"}

5. The value in response is a hex encoded number, which decodes to 17467248908296943231.

6. To determine the balance of BAT, you will find out how many decimals that BAT contract implements by making another RPC request. Get the Keccak hash for decimals(), add a 0x prefix to the beginning of the hash, and make this eth_call

curl https://your-subdomain-here.quiknode.pro/yourtoken/ \
-X POST \
-H "Content-Type: application/json" \
-H "x-qn-api-version: 1" \
--data '{
"id":67,
"jsonrpc":"2.0",
"method":"eth_call",
"params":[{"data":"0x313ce567add4d438edf58b94ff345d7d38c45b17dfc0f947988d7819dca364f9","to":"0x0D8775F648430679A709E98d2b0Cb6250d2887EF"}, "latest"]
}'

{"jsonrpc":"2.0","id":67,"result":"0x0000000000000000000000000000000000000000000000000000000000000012"}

7. Decode the hex string in the result, which decodes to 18. BAT implements 18 decimal places, so the balance of BAT in Vitalik’s wallet is 17.467248908296943231

That was a lot of work! There are hundreds of thousands of ERC20 token contracts on the Ethereum Blockchain and millions of active wallets. If you are not sure what tokens are in a wallet, you cannot find this information using eth_call without a potentially huge amount of trial and error. QuickNode's Token API simplifies this effort into one simple API call. Token API returns ERC-20 token data in a human-readable format, with hex data converted to integer format, and decimals retrieved without having to make additional requests.

Look Up ERC20 Token Balance with Token API

Token API enables you to look up tokens and balances within a wallet in one simple request. The Token API is available as a FREE add-on to your QuickNode Ethereum endpoint 🎉

This example demonstrates using the contracts param and limiting results to BAT Token. If you choose to omit the contracts param, the response will contain all of the tokens and balances within the wallet.

curl --request POST \
--url https://your-subdomain-here.quiknode.pro/yourtoken/ \
--header 'content-type: application/json' \
--header 'accept: application/json' \
-H "x-qn-api-version: 1" \
--data '{
"id":67,
"jsonrpc":"2.0",
"method":"qn_getWalletTokenBalance",
"params":{
"wallet": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
"contracts": ["0x0D8775F648430679A709E98d2b0Cb6250d2887EF"]
}
}'

This is the response:

{"jsonrpc":"2.0","id":67,"result":{"assets":[{"address":"0x0D8775F648430679A709E98d2b0Cb6250d2887EF","name":"Basic Attention Token","decimals":18,"symbol":"BAT","logoURI":"","chain":"ETH","network":"mainnet","amount":"17467248908296943231"}],"owner":"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045","totalPages":1,"totalItems":1,"pageNumber":1}}

Conclusion

Token API eliminates the need to compose tedious eth_call requests to get the ERC-20 token data you want, and also provides instant access to valuable indexed data that is not available through eth_call requests, like all token balances within a wallet, wallet-token transfers, and more.

Congratulations! You’ve learned how to look up ERC20 token balances and successfully used Token API. To learn more, check out some of our other amazing tutorials here.

We ❤️ Feedback!

If you have any feedback or questions on this guide, let us know! You can also reach out to us via Twitter or on our Discord community server. We’d love to hear from you!

Share this guide