0

I'm learning to code in solidity and have started learning through making an ERC-20 token. I want to be able to see who is holding the tokens without using a for loop. So far I've mapped each address to see if they're a holder or not. But have no idea how to list all items in that map that are true for let's say a lottery or giving holders more tokens, etc.

mapping(address => bool) private isHolder;

I'm new so any help appreciated,

Thanks.

0

I don't understand what your use case is but the below example might help as it returns the array of addresses.

//SPDX-License-Identifier: NOLICENSE
pragma solidity >=0.7.0 <0.9.0;

contract holder {
    
    mapping(bool => address[]) holders;
    
    function buyToken() public {
        holders[true].push(msg.sender);
    }
    
    function getHolders() public view returns(address[] memory) {
        return holders[true];
    }
}

The above example demonstrates that how you can get all the token holders but still, you need to write logic to avoid duplicates.

Update: To get Nth holder

For that, you have to use EnumerableSet or you can get the length of the array. for example: holders[true].length. so the Nth holder is holders[true][length-1].

If you find this use full then please let me know by voting & accepting this answer.

5
  • thank you so much, when it says return holders[true] how would I find the address of the Nth holder
    – Joop1123
    Jul 23 at 6:36
  • @Joop1123 for that you have to use EnumerableSet. Jul 23 at 6:39
  • @Joop1123 updated the answer Jul 23 at 6:50
  • thanks for your help. If i wanted to check to see if an address is part of the holders[true] how would i do that?
    – Joop1123
    Jul 23 at 6:53
  • for that, you can maintain this ``` mapping(address => bool) private isHolder;``` or explore EnumerableSet as I said earlier Jul 23 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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