1

I am trying to determine how to query a single token contract for all of its ERC-721 tokens, similar to the Inventory page of etherscan, for example: https://etherscan.io/token/0x60e4d786628fea6478f785a6d7e704777c86a7c6#inventory

I see that most contracts have a totalSupply which indicates the number of tokens, but what I'm looking for as a result is an array of all of the tokenIds. I am using ethers.js. Is there an easy way to do this?

1 Answer 1

1

If the contract supports ERC721Enumeration (check using ERC-165 introspection) you can use that get each token.

Also, if you happen to know the pattern the contract uses (typically 1, 2, ..., n), you can do that. But this is a specialization.

Lastly, another approach is to look for every token transfer and if it is one you haven't seen before, add that to your database.

Here is a main part you can use for this (shown in Node + Ethers):

const logs = await provider.getLogs({
    fromBlock: blockToSearch,
    toBlock: blockToSearch,
    address: null,
    topics: [ethers.utils.id("Transfer(address,address,uint256)")],
});

This is looking for ALL contracts, though you can specialize it to a specific contract(s) using the address parameter here.

This is excerpted from https://github.com/fulldecent/contract-scanner which is simple and meant to be studied. That is looking for all NFT contracts, but the same approach can be adapted to your use case.

Also, I have narrated and explain every line of code here if you want to dig deeper, see https://youtu.be/lukshxTYpUw and I host a weekly public call Community Service Hour to answer any questions.

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.