I'm trying to make a list of holders of a specific token in solidity, thing is,

I can't iterate over a mapping because that's not even a thing in sol. That only works for arrays and even then there's another limitation...

So I tried maintaining an array that adds an address to the list on each transfer, thing is we can't iterate over the array to search for dupes because that would use wayyy too much gas so there would be a BIG duplication problem... then the case when an address' balance hits 0, I want it to be deleted, can't do that either, I'm starting to run out of ways. Could anyone help find a way this can be done on-chain ?

  • Are you going to use it for airdrop?
    – trizin
    Jul 1, 2021 at 13:01
  • @trizin A kind of airdrop yes.
    – Duff
    Jul 1, 2021 at 13:02

1 Answer 1


Perhaps you can create a mapping to check if the address is included in array.

mapping(address => bool) isholder;
if(isholder[msg.sender] == false) holders.push(msg.sender);
isholder[msg.sender] = true;

What else you can do is get a list of all token holders from the explorer. For example, to get a list of CAKE holders you can go to https://bscscan.com/token/0x0e09fabb73bd3ade0a17ecc321fd13a19e81ce82#balances and there is a button to export it as CSV at the bottom.
Then use a function that will take an array of addresses and manually pass the addresses to the function.


Off the chain, you can write a script that will iterate through the list of addresses and make a transaction for each one.

  • That is incredible advice, thanks a lot! An on-chain approach is harder and will cost more gas when transacting for no apparent reason, so I'll do a script with those ideas in mind then. Thanks a lot again!
    – Duff
    Jul 1, 2021 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.