ERC-721/1155 – How to Retrieve Data for ERC-20 and NFT Tokens

erc-1155erc-20erc-721nft

How do I get all the NFTs (ERC-721 & 1155) and Tokens (ERC-20) for a wallet address? Is there an API for this?

Irwing Tello

Best Answer

You can get this data using the balanceOf function of the token smart contracts. But doing this for each token and NFT is not a nice experience.

Using a solution like QuickNode, which provides two solutions for this would be way easier.

The First option is using QuickNode custom RPCs

To use this, enable the Token and NFT API bundle Add-on on your QuickNode Ethereum endpoint.

Here's an example in ethersv6:

const ethers = require("ethers");
(async () => {
  const provider = new ethers.JsonRpcProvider("QUICKNODE_RPC_URL");
  const walletAddress = "ADDRESS_TO_BE_QUERIED";
  const coins = await provider.send("qn_getWalletTokenBalance", [
    {
      wallet: walletAddress,
    },
  ]);

  const nfts = await provider.send("qn_fetchNFTs", [
    {
      wallet: walletAddress,
      page: 1,
      perPage: 10,
    },
  ]);

  console.log(coins,nfts);
})();

The second option is QuickNode Graph API

By using the following GraphQL query:

query Query($address: String!) {
  ethereum {
    walletByAddress(address: $address) {
      tokenBalances {
        edges {
          node {
            contract {
              address
              decimals
              name
            }
            totalBalance
          }
        }
      }
      walletNFTs {
        edges {
          node {
            nft {
              externalUrl
              name
              metadata
            }
          }
        }
      }
    }
  }
}

With the wallet address as a variable:

{
  "address": "WALLET_ADDRESS_GOES_HERE"
}
Related Topic