1

Are there any best practices to fetching all ERC20 token balances for a given address? I am open to using external APIs.

2 Answers 2

1

One approach is identifying the ERC20 contract first, you can do this by different heuristics. Once you have the list of ERC20 contract addresses you use JSON RPC to do a eth_call for each ERC20 using the ERC20 ABI to obtain the balance of the address you want. To identify the ERC20 contracts in a blockchain you can:

  1. Download the code of each contract and search for the function signatures (32 bits) of the ERC20 methods. The bytecode of all the ERC20 contracts will include those 32 bits for each method. You can have false positives.
  2. You can do this after step 1, you check the events emitted for each address to identify the standard ERC20 events, this will eliminate false positives.
  3. You can just do a few eth_call to the candidates from step 1, to check the result is successful for functions such as totalSupply, decimals, symbol

You could do 3 without without doing 1 first but that would be more time consuming.

If you will do this often then you need to create an indexed DB. Once you have the ERC20 identified you create a service to monitor events from those contracts using JSON RPC. That server will extract the source and destination address from events such as Transfer and update the balance for those addresses in the DB.

If you use a third party service you will be trusting a third party and that is not a good decentralized system practice.

2
  • I don't have a particular contract in mind, I'd like to fetch all ERC20 tokens for a given address (kind of like what Etherscan does).
    – Tarek
    Jul 28, 2020 at 22:06
  • You can use a third party API, what I described is how those API work. There isn't an index in the blockchain where you can ask all the ERC20 tokens owned by a given address. You can search events but you don't know if they are from ERC20 tokens, you need to get the list of ERC20 tokens contracts first.
    – nultrino
    Jul 28, 2020 at 22:09
0

Developer advocate at Chainstack here.

This is one of the common tasks that should have a simple way but don't, unfortunately. Getting this kind of data from the chain is certainly possible but not as straightforward as it should be; I'll give you two options here.

Using Subgraphs

The first and best option if you want to index the data yourself is to use a Subgraph, which allows you to index data coming from smart contracts; since smart contracts manage ERC-20 tokens, this is a good solution.

Here, you can find a guide from the Chainstack dev portal showing you how to index ERC-20 tokens with Subgraphs, which include balances. You can essentially make an API from this.

You can then query the data using Python if you want to.

Indexing ERC-20 token balance using Subgraphs

Chainstack offers Subgraphs for various chains, so you can do this for multiple networks if needed.

Use third party APIs

A good solution for a quick way with close to zero setup is using an API like Covalent. You can also find an integration with Chainstack, which allows you to access their APIs.

You can use this endpoint, or this SDK I made.

Full disclosure: I am a dev advocate at Chainstack, and feel free to reach out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.