Ethers.js How to get the ETH & ERC-20 tokens balance of an address at a particular blocknumber

Hello Infura community,

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

Please don’t forget to use your Infura API Key .

const { ethers } = require('ethers');

// Set your Infura endpoint URL and project ID
const infuraUrl = '<your-api-key>';

// Set the Ethereum address you want to check the balance of
const address = '<your-ethereum-address>';

// Set the blockNumber you want to check the balance at
const timestamp = <your-blockNumber>;

// Set the contract address and ABI of the ERC-20 token you want to check the balance of
const tokenAddress = '<token-contract-address>';
const tokenAbi = ['function balanceOf(address) view returns (uint256)'];

// Create an Ethereum provider using Infura
const provider = new ethers.providers.JsonRpcProvider(infuraUrl);

// Get the ETH balance of the address at the specified timestamp
const ethBalancePromise = provider.getBalance(address, timestamp);

// Get the balance of the ERC-20 token at the specified timestamp
const tokenContract = new ethers.Contract(tokenAddress, tokenAbi, provider);
const tokenBalancePromise = tokenContract.balanceOf(address, timestamp);

Promise.all([ethBalancePromise, tokenBalancePromise])
  .then(([ethBalance, tokenBalance]) => {
    console.log(`ETH balance of ${address} at timestamp ${timestamp}: ${ethBalance}`);
    console.log(`Token balance of ${address} at timestamp ${timestamp}: ${tokenBalance}`);
  .catch((error) => {

Each function from above is having a comment to explain the steps needed to get the script running.

Hope you’ll enjoy it.