Saltar al contenido principal

Ethers.js: How to retrieve the balance of an ERC-20 token

Hello everyone,

In this short tutorial we will be looking at how to retrieve the balance of an ERC-20 token with ethers.js.

For creating a project directory, installing the ethers library and setting up your .env file follow the first part of this guide. While for more details on the library and other cool stuff on what you can do with it check out this article.

Besides checking the balance of an ERC-20 token which an address holds, we will also get the name of the token contract and the token’s symbol. But before that, we will need to create a contract instance and load the contract ABI from a file. For testing purposes you can get some LINK test ERC-20 tokens on Goerli from this faucet https://faucets.chain.link/ and use the LINK token contract:

const fs = require("fs");
const jsonFile = "/path/to/ABI/file/ct_abi.json";
const abi = JSON.parse(fs.readFileSync(jsonFile));
const tokenContract = "0x326C977E6efc84E512bB9C30f76E30c160eD06FB"; //LINK
const contract = new ethers.Contract(tokenContract, abi, provider);

Once the contract instance is defined we are ready to retrieve the token’s contract name, the token’s symbol and the holder’s balance:

// Get the ERC-20 contract token name
const contractName = await contract.name();
console.log("The token's contract name is " + contractName);

// Get the ERC-20 token symbol
const tokenSymbol = await contract.symbol();
console.log("The token's symbol is " + tokenSymbol);

//get token balance as BigNumber
const balance = await contract.balanceOf(tokenHolder);

//Format the balance for displaying 18 decimal places
const balanceFormatted = ethers.utils.formatUnits(balance, 18);
console.log("Holder's balance is " + balanceFormatted + " " + tokenSymbol);

It’s as easy as that. The full piece of code can be found below:

const { ethers } = require("ethers");
require("dotenv").config({ path: "/path/to/env/file/.env" });

async function main() {
// Configuring the connection to an Ethereum node
const network = process.env.ETHEREUM_NETWORK;
const provider = new ethers.providers.InfuraProvider(
network,
process.env.INFURA_PROJECT_ID,
);

var fs = require("fs");
const jsonFile = "/path/to/ABI/file/ct_abi.json";
var abi = JSON.parse(fs.readFileSync(jsonFile));
const tokenContract = "0x326C977E6efc84E512bB9C30f76E30c160eD06FB"; //LINK
const tokenHolder = "<insert_token_destination_address_here>";

// Define the ERC-20 token contract
const contract = new ethers.Contract(tokenContract, abi, provider);

// Get the ERC-20 contract token name
const contractName = await contract.name();
console.log("The token's contract name is " + contractName);

// Get the ERC-20 token symbol
const tokenSymbol = await contract.symbol();
console.log("The token's symbol is " + tokenSymbol);

//get token balance as BigNumber
const balance = await contract.balanceOf(tokenHolder);

//Format the balance for display with 18 decimal places
const balanceFormatted = ethers.utils.formatUnits(balance, 18);
console.log("Holder's balance is " + balanceFormatted + " " + tokenSymbol);
}

main();