6

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?

3
  • 1
    You check all Transfer events and filter it by receiving address. May 3 at 20:52
  • 1
    Is there a better way than that? What if the address received something and then sent it somewhere else later? May 3 at 20:57
  • 1
    You check also outgoing transfer events. May 4 at 9:48
9

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 saves this into a database where it's quickly accessible for every request. The transfer events to index for ERC721 is the following:

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.

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 ID

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

//get NFTs for current user on ETH Mainnet
const userEthNFTs = await Moralis.Web3API.account.getNFTs();

//get all owners of specific NFTS
const options = { address: "0xd...07", chain: "bsc" };
const nftOwners = await Moralis.Web3API.token.getNFTOwners(options);

Full disclusure, I work at moralis.

3

No, web3 doesn't know about all ERC721 token contracts, which would be required to call ownerOf() for each NFT.

What could be done: take a list of popular ERC721 contracts (like this one from Bloxy) and check the owner of each and every token, which is quite cumbersome.

Alternative, more convenient approaches would be to use third party APIs like Alchemy's Transfer API or Ethersan's Account API (see 'Get a list of "ERC721 - Token Transfer Events" by Address').

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.