Get the balance of an Ethereum blockchain address using ethers.js and Infura API

In this post I show an easy way to interact with any Ethereum-compatible blockchain using TypeScript and Node.js.

Why?

I have recently been developing an app that interacts with an Ethereum blockchain and was expecting it to be incredibly complicated. But it’s not.

Here is the surprisingly simple setup. All we need are three things:

  • ethers.js - a library for interacting with Ethereum nodes
  • Infura - a service providing access to Ethereum nodes
  • An Ethereum-compatible wallet address (e.g. from MetaMask)

First we will sign up for a free Infura account, then create a new project from the Infura dashboard. This gives a Project ID which we will use to access the Ethereum node API.

Set up the project folder

$ mkdir get-balance $ cd get-balance

Add dependencies

We are going to use TypeScript and ts-node which require a little bit of extra setup but a big pay-off in helping with code correctness.

$ yarn add typescript ts-node ethers @tsconfig/node16

Configure TypeScript

Create a file called tsconfig.json with the following configuration:

{ "extends": "@tsconfig/node16/tsconfig.json", "compilerOptions": { "lib": ["es2015", "dom"], } }

Create the script

Create a file called index.ts with the following script, replacing the Infura Project ID and wallet address where indicated:

import { ethers } from "ethers"; // Replace with a project ID from [Infura](https://infura.io/) const INFURA_PROJECT_ID = "REPLACE_WITH_INFURA_PROJECT_ID"; // Replace with an Ethereum-compatible wallet address const WALLET_ADDRESS = "0xREPLACE_WITH_WALLET_ADDRESS"; const fetchBalance = async () => { // Create an ethers provider using Infura Mainnet const provider = new ethers.providers.JsonRpcProvider( `https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}` ); // Fetch the balance, this will be in Wei const balanceInWei = await provider.getBalance(WALLET_ADDRESS); // Convert balance to Eth const balanceInEth = ethers.utils.formatEther(balanceInWei); // Print a summary to the console console.log(`Address: ${WALLET_ADDRESS}`); console.log(`- balance in Wei: ${balanceInWei}`); console.log(`- balance in ETH: ${balanceInEth}`); }; (async () => { await fetchBalance(); })();

Get the balance

We can now run the script:

$ yarn ts-node index.ts

… an should expect the following output to the console:

Address: 0x**************************************** - balance in Wei: 214066444119271200 - balance in ETH: 0.2140664441192712

That’s it

That is all there is to it, but read on for a bit more of a code breakdown and examples of using other Layer 2 chains.

Code breakdown

There are a few concepts to break down:

const provider = new ethers.providers.JsonRpcProvider( `https://mainnet.infura.io/v3/${INFURA_PROJECT_ID}` );

This is creating a provider using Infura Mainnet, the main Ethereum blockchain. There are other test networks available.

Ethers has a specific Infura provider however we will use JsonRpcProvider as it is more generic.

For example with one small change we can now also query a balance on the Layer 2 Polygon network by changing just the RPC:

const provider = new ethers.providers.JsonRpcProvider( `https://polygon-rpc.com/` );

…or any other Layer 2 chain, e.g. Gnosis Beacon Chain

const provider = new ethers.providers.JsonRpcProvider( `https://rpc.gnosischain.com/` );

Once we have the provider we use it to fetch the raw balance in Wei which is the smallest unit.

const balanceInWei = await provider.getBalance(WALLET_ADDRESS); // Wei: 214066444119271200

Thankfully ethers provides a convenient way to convert this to more familiar ETH:

const balanceInEth = ethers.utils.formatEther(balanceInWei); // Eth: 0.2140664441192712

Summary

In this post we learned an easy way to interact with any Ethereum blockchain using TypeScript and Node.js. We fetched a balance from an Ethereum-compatible wallet address by using ethers.js (a library for interacting with Ethereum nodes) and Infura (a service providing access to Ethereum nodes).

We also learned how to connect to other Layer 2 chains directly such as Polygon and Gnosis Beacon Chain and use the same method to fetch a balance.

Finally we learned how to convert the balance from Wei into ETH.

This gives the basic building blocks and a starting point for interacting with any Ethereum blockchain.

About

I am an experienced Product Designer and Software Engineer with a passion for making and crafting digital products, tools and services that are useful, empowering or inspiring to others.