1

Intro:

I am trying to use ERC721Enumerable in my NFT contract so I can get the set of all NFTs owned by a particular address.

Research:

The ERC721Enumerable contract was implemented to enumerate unique NFTs, to make them discoverable. The way to discover them is by iterating through the enumerated NFT ids using the following function:

function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);

I presume the best way to get an entire set of an address's assets is to query this function multiple times, while incrementing the _index variable from 0 -> n, until no such NFT exists - then you will end up with all of the assets of that contract owned by the particular address. But that seems hacky to me.

Question:

I'm wondering if the ERC721Enumerable contract has a better way to tell me the entire list of an address's owned NFTs.

1
  • 1
    Not “until no such NFT exists”, but simply until _index == balanceOf(_owner) - 1.
    – dwardu
    May 9 at 1:28
1

Yes, you have the correct approach.

This is the only way to retrieve an arbitrarily large amount of data out of the blockchain.

Other options we considered were to return an array. However, this opens attack scenarios or denial of service when the returned array might be too large for a certain client.

2
  • 1
    Thanks for the answer, and thanks for writing ERC721 :) May 8 at 22:25
  • You are very welcome! And don't be a stranger, send along what's you've made once it's built. May 10 at 19:17

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.