0

I've got a NFT collection with a maxSupply of 1000. All the NFTs have been minted where totalSupply == maxSupply.

What is the best way to retrieve all 1000 addresses along with their relevant tokenIds?

I've tried the below, but the results is not working as expected. Where am I going wrong?

 mapping(uint256 => address) private getMintedAddress;

 function getAllAddresses() public view onlyOwner returns (uint256[] memory) {
    uint256 totalMinted = totalSupply();
    uint256[] memory tokenIds = new uint256[](totalMinted);
    for (uint256 i = 1; i < totalMinted; i++) {
        tokenIds[i] = getMintedAddress[i];
    }
    return tokenIds;
  }

1 Answer 1

0

They way you should use your mapping is the other way around.

So you should use:

mapping(uint256 => address) private getMintedAddress;

As this way you can simply retrieve your addresses like this:

uint256 totalMinted = totalSupply();
uint256[] memory tokenIds = new uint256[](totalMinted);

for (uint256 i = 0; i < totalMinted; i++) {
    tokenIds[i] = getMintedAddress[i];
}

Here you will get this error: TypeError: Type address is not implicitly convertible to expected type uint256. tokenIds[i] = getMintedAddress[i];

And that is logical because you are saving the address as a uint now. So to just retrieve all balances in tokenId order you need to have this:

uint256 totalMinted = totalSupply();
address[] memory tokenAddresses = new address[](totalMinted);

for (uint256 i = 0; i < totalMinted; i++) {
    tokenAddresses[i] = getMintedAddress[i];
}

Depending how you increment the tokenIds in your minting function, it could be that you need to change getMintedAddress[i] to getMintedAddress[i + 1]

6
  • Hey thanks for the reply. I'm getting this error: TypeError: Type address is not implicitly convertible to expected type uint256. tokenIds[i] = getMintedAddress[i]; Feb 8, 2022 at 1:54
  • Hi, i added the problem to the solution given above. I haven't had time to double check but it should work. The error is saying that you save your addresses now as a uint ans they or not convertible like that. So you should ofcourse save those addresses as an address and not a uint :)
    – Zercon
    Feb 8, 2022 at 15:47
  • Thank you for your response. This is what I get when I call the getAllAddress() function: 0: address[]: 0x0000000000000000000000000000000000000000 Is there a reason why it isn't returning an address, even though I've minted one NFT? Feb 8, 2022 at 18:17
  • well, you got to add the address to the mapping in order for the mapping to hold any values.
    – Zercon
    Feb 8, 2022 at 20:39
  • @Zercon A problem is that arrays are zero based and the i starts at one. It could be fixed with tokenAddresses[i - 1] = getMintedAddress[i];
    – Ismael
    Feb 9, 2022 at 17:07

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.