It is possible to check which address owns a particular NFT by using the ERC721 standard function:
function ownerOf(uint256 tokenId) public view virtual override returns (address)
in ERC721.sol
But I would like to know all NFTs owned by a particular address.
My Question:
Is there a way to retrieve all ERC721 tokens owned by a partcular Ethereum address using Web3.JS?
Best Answer
In order to get all NFTs of a user, you need to have an indexed database where you save this data. Then you index all
Transfer
events of ERC721 contracts and eventually calculate the balances for every address and save this into a database where it's quickly accessible for every request. The transfer events to index for ERC721 is the following: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.
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.
/{WALLET_ADDRESS]/nft
- get all NFTs owned by address/nft/{CONTRACT_ADDRESS}/owners
- get all owners of a specific NFT contract/nft/{CONTRACT_ADDRESS}/{TOKEN_ID}/owners
- get all owners of a specific NFT contract and IDOr you can use the moralis sdk and you can use the frontend JavaScript functions.
Full disclosure, I work at moralis.