How to list all ERC721 tokens deployed on the smart contract?

I have deployed a basic ERC721 token with some image metadata. I want to build a dashboard to view all the tokens and looking for an effective way of doing it.
One approach I have tried is:

Loop through all the tokens given the number of tokens deployed and make a request for each tokenURI as shown below:

var numTokens = 10
var datas = []

for (let i = 0; i < numTokens; i++) {
  let data = await contract.methods.tokenURI(i);
  datas.push(data);
}

Here, for 10 tokens 10 requests will be made and 100 requests for 100 tokens which seems very ineffective. What would be an effective way to list all tokens in the dashboard?

1 Like

You can use events to find all token ids. Check out the snippet I shared on #2563 (comment), that you can adapt to list all ids rather than a single owner’s.

If you want the token URIs, it depends on whether you’re using a custom URI for each token id, or you’re using a base URI concatenated to token id. In the first case, you will need to do all the 100 requests of tokenURI. In the second case, you can just query for the base URI and concatenate it locally with all of the ids that you find.

1 Like