0

I'd like to have a page on my NFT site that shows which tokens a specific address owns. So if I own 10 NFTs for FoobarContract, I'd like to be able to show those 10 NFTs on this page.

It seems there's a private mapping in the ERC721 contract called _holderTokens with a description of Mapping from holder address to their (enumerable) set of owned tokens. Perfect. This is exactly what I need to access. But it's only accessible through the contract, and every other exposed public method that accesses it only gives me things like balanceOf, which isn't useful in this case, and tokenOfOwnerByIndex, which is also useless to me.

Am I missing something? This is one of the most important pieces of data in the ERC721 contract, yet it's inaccessible when inheriting from it. How else does someone go about getting the 10 tokens that this hypothetical address owns? I've searched for other solutions, but they all say to pull in all events and filter them down to a particular receiving address and do the math to figure out which tokens are in this address's possession. Isn't this an absurd waste of resources when the mapping is literally right there in the contract?

1

You cannot iterate a mapping.

Your two choices are to:

  1. Implement ERC721Enumerable which is designed to make this specific use case as simple as possible.
  2. Do not implement ERC721Enumerable and use an archival node to listen to all Transfer events to catalog the tokens.

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.