'How to find erc20 tokens balance
I'm trying to find the balance of ERC20 tokens (such as Tether) of specific addresses.
Actually, I hope to find an API that works like below, get the account address and contract address and return the balance of that specific token.
To clarify my purpose, in the TRC20 network, I use this endpoint and there is an array of TRC20 token balances in the response.
Solution 1:[1]
ERC-20 (and ERC-20-like such as TRC-20, BEP-20, etc.) token balance of each address is stored in the contract of the token.
Trongrid uses a separate DB that aggregates token balances from all Transfer() events (defined in the ERC-20 standard) throughout the history of the Tron network. There's possibly a similar third-party service for ERC-20 tokens, but I don't know any. For example Etherscan does have the same info, but they only display it on the address detail page, and they don't provide an API containing this data.
In order to get all ERC-20 token balances of an address, the easiest solution (apart from finding an API that does the same thing as the Trongrid endpoint) is to loop through all token contracts (or just the tokens that you're interested in), and call their balanceOf(address) function.
Solution 2:[2]
You can call this using one simple Covlanet API call: https://www.covalenthq.com/docs/api/#get-/v1/{chain_id}/address/{address}/balances_v2/
If you want to switch to any other network, just change the chain_id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
| Solution 2 | anadi pandharkar |
