Links

Retrieve the balance of an ERC-20 token

In this tutorial, you'll retrieve the balance of an ERC-20 token using the Web3 JavaScript library.

Prerequisites

Steps

1. Create a project directory

Create a new directory for your project. This can be done from the command line:
mkdir retrieveBalance
Change into the new directory:
cd retrieveBalance

2. Install required packages

Install the web3 package in the project directory:
npm install web3

3. Set up the script

Create a file called retrieveBalance.js. At the top of file, add the following lines to import the web3.js library and connect to the Infura HTTPS endpoint:
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/<YOUR_API_KEY>'));
Make sure to replace <YOUR_API_KEY> with your Infura API key.

4. Set the ABI

You'll only use the balanceOf method, so you don’t need the entire ABI for ERC-20 smart contracts. Define the ABI for the balanceOf method by adding the following to the script:
const balanceOfABI = [
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
];

5. Select a token address

You need the contract address of each token you'd like to retrieve the balance of. You can find this using a block explorer such as Etherscan.
This example uses a DAI token contract. However, you can use any ERC-20 token address. Copy the address you wish to use.

6. Request the token balance

Define the addresses to use in the script:
const tokenContract = "0x6b175474e89094c44da98b954eedeac495271d0f"
const tokenHolder = "0xf326e4de8f66a0bdc0970b79e0924e33c79f1915"
tokenContract is the address of the token contract on which to call the balanceOf function, and tokenHolder is the account address of which to request the balance.
Define the contract using web3.eth.Contract(), passing the ABI and contract address as parameters:
const contract = new web3.eth.Contract(balanceOfABI, tokenContract)
Next, create an async function that interacts with the smart contract.
You can call methods.balanceOf() on the contract, which sends a request to your Infura endpoint to request the token balance.
async function getTokenBalance() {
const result = await.contract.methods.balanceOf(walletAddress).call();
console.log(result)
}
getTokenBalance();

7. Convert the token units

By default, calling balanceOf returns the balance value in wei, which is the smallest unit in Ethereum (equal to 0.000000000000000001 Ether).
Use web3.utils.fromWei(result, "ether") to get the actual number of DAI tokens, by adding the following line to the async function:
const formattedResult = web3.utils.fromWei(result, "ether");
Also, update your console.log(format):
console.log(formattedResult);

8. Run the script

Run the script using the following command:
Command
Example output
node retrieveBalance.js
3142422.965167994254806984

Complete code overview

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/<YOUR_API_KEY>'));
const balanceOfABI = [
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
];
const tokenContract = "0x6b175474e89094c44da98b954eedeac495271d0f"
const tokenHolder = "0xf326e4de8f66a0bdc0970b79e0924e33c79f1915"
const contract = new web3.eth.Contract(balanceOfABI, tokenContract)
async function getTokenBalance() {
const result = await contract.methods.balanceOf(tokenHolder).call();
const formattedResult = web3.utils.fromWei(result, "ether");
console.log(formattedResult);
}
getTokenBalance();