[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?
enter image description here

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.

Related Topic