[Ethereum] How does etherscan fetch the number of token holders from a smart contract
balancesetherscantokens
I was wondering How does etherscan fetch the number of token holders from a smart contract?
Best Answer
They keep track of Transfer events emitted by that smart contract. Most likely they record them in an SQL database and maintain a ledger based on these events. The event is defined in the ERC20 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
There is no way to look up this information from the smart contract directly. Even though the balances variable is public, the mapping data type in Solidity doesn't support enumerating the keys. Reading the low level database of an Ethereum client wouldn't help either since all storage keys are hashed before they are persisted.
I don't think they've released the details of their implementation, but presumably they track Transfer events and update the known balances for each account as those events come in.
It means Etherscan doesn't yet know which address or addresses own the remaining tokens. They keep track of the owners by watching the 'Transfer' events emitted by the smart contract. That's because you cannot simply get a list of token holders reading the smart contract. So if an account owns some tokens but no transfer event was emitted on issuance (for example it may have happened on initialization), it won't show up on Etherscan.
Best Answer
They keep track of
Transfer
events emitted by that smart contract. Most likely they record them in an SQL database and maintain a ledger based on these events. The event is defined in the ERC20 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.mdThere is no way to look up this information from the smart contract directly. Even though the
balances
variable is public, themapping
data type in Solidity doesn't support enumerating the keys. Reading the low level database of an Ethereum client wouldn't help either since all storage keys are hashed before they are persisted.