...by Daniel Szego
"Simplicity is the ultimate sophistication."
Leonardo da Vinci

Wednesday, January 24, 2018

Solidity and Truffle Tips and Tricks - getting all token balances

Well, it is always a little bit problematic for a token to get a simple list on all of the accounts that have token balances at all. The problem behind is that the token balances are stored in a mapping structure which is practically a hash tree structure meaning that you can query the values for each of the individual elements but you can not iterate on the structure. So the things that you can basically do are the followings:

1. If you know the certain address or addresses, you can query the balance for these addresses with the help of the balanceOf function. Certainly there is a way to read out the total supply of a token as well with a standard function.

2. At each transfer, there is a Transfer function raised with the from and to address. Blockchain explorers can read these transfers events and query the balance of the accounts of the related addresses. 

3. If the previous point does not provide the necessary result, you can try to start scripting a taint analysis with explicitly enumerating all of the addresses that were somehow effected by the contract and query for each address explicitly the balance.