Skip to main content

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

In this short tutorial we'll look at how to retrieve the balance of an ERC-20 token with Ethers.js.

To learn how to create a project directory, install the Ethers.js library and set up your .env file, follow the first part of this guide. For more details on the library and other cool stuff 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 Sepolia from the Chainlink Sepolia faucet 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, you're 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_API_KEY)

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()
Was this helpful?
Connect MetaMask to provide feedback
What is this?
This is a trial feedback system that uses Verax to record your feedback as onchain attestations on Linea Mainnet. When you vote, submit a transaction in your wallet.