0

The tokenIds on the contract I want to query are 77 characters long each and don't seem to be in sequential order....is there a way to query the contract for balances and tokenIds without entering the exact tokenId, and address into a .balanceOf(address, exactTokenId)? Each nft in the erc1155 collection has a different 77 character tokenId. Someone used opensea interface to make this collection. Thank you for any insight.

enter image description here

1
  • Or is it possible to query each tokenId existing? Commented Jan 9, 2023 at 2:13

2 Answers 2

1

You can query all the NFTs from a smart contract using Moralis NFT API. Here is an example of that implementation with all the required steps: https://docs.moralis.io/web3-data-api/evm/how-to-get-all-the-nfts-from-a-collection.

Let me know if you need any help or if you have any questions!

1
  • Thank you! This is what I ultimately chose to use Commented Jan 15, 2023 at 19:24
1

If you look at the implementation of ERC1155, the balances use mapping between tokenID and users balances. For this data type, if we don't know the key (tokenID), it's impossible to query among 2^256 possible keys from the smart contract itself, especially when the tokenID is not sequential as you said.

Nonetheless, it can be done offchain by keeping track of all the mint events in the NFT contract. If you only track a few ERC1155 you can track all of these historical events and transactions related to the contracts of interest if you have an ETH archive node or using some paid archive RPC service.

Fortunately, there are indexing services that keep track of all of these and you can use them effortlessly. I think etherscan (like this ERC1155 inventory) and opensea are quite good at it (there may be other popular services that I'm not aware of).

1
  • Thank you I appreciate your answer and context provided Commented Jan 15, 2023 at 19:25

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.