3

How do I get balances of all the ERC-20 tokens for a wallet address

Irwing Tello

1
  • Please clarify your question Commented Jun 18, 2023 at 9:36

2 Answers 2

2

Balance for an ERC-20 token can be fetched using the balanceOf function of that particular token's smart contract. But hundreds and thousands of tokens are out there, so doing this process for all of them is not ideal.

There are various data providers who have this data indexed and provide an easy way to fetch them, one of them is QuickNode.

You can get all the ERC-20 tokens a wallet address holds using QuickNode custom RPC qn_getWalletTokenBalance

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

var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "id": 67,
  "jsonrpc": "2.0",
  "method": "qn_getWalletTokenBalance",
  "params": [{
    "wallet": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
  }]
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("QUICKNODE_URL_HERE", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Check this video and learn how to create a token balance app: https://youtu.be/ReW6Giid43w

If you prefer a powerful GraphQL interface, you can check out quickNode's Graph API which I covered in my answer here: https://ethereum.stackexchange.com/a/151886/122985

0

Your question is not so clear, are you referring to an ERC20 token? If that is the case, use the "balanceOf" function defined in the standard. eg:

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract TokenBalance {
    function getTokenBalance(address tokenAddress, address userAddress) external view returns (uint256) {
        IERC20 tokenContract = IERC20(tokenAddress);
        return tokenContract.balanceOf(userAddress);
    }
}

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.