1

I'm wondering how does a marketplace like OpenSea, automatically know what all NFTs a user has, without the contract address?

this question can also be simplified as finding all erc20 tokens a user has, isn't that vague because anyone can create an erc20 anytime.

I couldn't find resources on searching up google on trying to ask in their community discord, anyone can shed light, that would be appreciated

1 Answer 1

2

In order to get all NFTs of a user, you need to have an indexed database where you save this data. These marketplaces have Ethereum full nodes running, and then they index all transfer events of ERC1155 and ERC721 contracts and eventually calculates balances for every address and saves this into a database where it's quickly accessible for every request. The transfer events to index are the following.

For ERC1155

TransferSingle(address,address,address,uint256,uint256)
TransferBatch(address,address,address,uint256[],uint256[])

For ERC721

Transfer(address,address,uint256)

It's a pretty cumbersome process, and unfortunately, nothing that is available with a simple web3.js call. This has to do with the way data is structured in the blockchain, which just contains transactions and not actual indexed user data.

Systems like these can continue to stay updated because even though people create new token contracts all the time, these new contracts will emit transfer events as well, which will then be used to update the balances.

If you're looking for an easy way to get NFT balances of users with a simple call, you can use the service I built, moralis.io. We built it because I had the exact same issue you have. It's completely free to use.

/nft/wallet/{WALLET_ADDRESS} - get all NFTs owned by address /nft/contract/{CONTRACT_ADDRESS}/token/{TOKEN_ID}/owner - get all owners of a specific NFT

Or you can use the moralis sdk and you can use the frontend javascript functions.

// get NFTs for current user on Mainnet
const userEthNFTs = await Moralis.Web3.getNFTs();
2
  • 1
    thanks for the detailed answer, I am going to try what you suggested via moralis
    – Yash
    Commented Jul 15, 2021 at 6:08
  • 1
    Thank you so much for building moralis, I tried it and its great! +100 Commented Dec 4, 2022 at 18:11

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.