blockchainethereumsoliditybitcoinether

how does Ethereum accounts works with regards to balance of Ethereum and ERC20 tokens


I know that for Bitcoin, each address get its BTC balance from its history of transactions. What about for ethereum? How do we know the ETH balance and ERC20 token balance(s) of an Eth address?

Thank you very much

  1. For each ethereum address, do we get its ETH balance from the ethereum world state's database?

  2. how do we know an ethereum account's balances of each ERC20 token - is there a data field for each address containing all the erc20 contracts an account has interacted with, then the wallet checks the smart contract's balanceOf field? If not, how do we know what erc20 tokens are found in an eth wallet?


Solution

  • The ERC-20 token balance of each address is stored in the storage slot of a smart contract

    The smart contract itself has an address. So you need to query address of a smart contract -> its storage slot.

    You cannot query what tokens any address has because of the broken design of ERC-20 (here more about the issue). Only way to know this is to build a full world indexer for Ethereum blockchain.