0

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?

Tiaerys
  • 13
  • 1

2 Answers2

0

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.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
0

actually it is not the case. as the yellowpaper of ethereum define it, the balance is stored the same way the storage, the code, and the nonce are stored : in the world state.

"The account state, σ[a], comprises the following four fields:

nonce: A scalar value equal to the number of transactions sent from this address or, in the case of accounts with associated code, the number of contract-creations made by this account. For account of address a in state σ, this would be formally denoted σ[a]n.

balance: A scalar value equal to the number of Wei owned by this address. Formally denoted σ[a]b.

storageRoot: A 256-bit hash of the root node of a Merkle Patricia tree that encodes the storage contents of the account (a mapping between 256-bit integer values), encoded into the trie as a mapping from the Keccak 256-bit hash of the 256-bit integer keys to the RLP-encoded 256-bit integer values. The hash is formally denoted σ[a]s.

codeHash: The hash of the EVM code of this account—this is the code that gets executed should"

yellowpaper

Raphael
  • 1
  • 2