πŸ’°Get the native balance of a Signer

In this section we'll learn how to get the native balance for a Signer using ethers

Checking the native balance of an EVM address using ethers.js

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

async function getNativeBalance() {
  // Replace 'your_private_key' with the private key of your Ethereum account
  const privateKey = 'your_private_key';
  
  // Create a wallet using the private key
  const wallet = new ethers.Wallet(privateKey);

  const provider = new ethers.providers.JsonRpcProvider(`https://moonbeam.unitedbloc.com`);
  await wallet.connect(provider);

  // Get the native balance (in Wei) of the signer's Ethereum account
  const balanceWei = await wallet.getBalance();

  // Convert the balance from Wei to Ether
  const balanceEther = ethers.utils.formatEther(balanceWei);

  console.log('Native Balance:', balanceEther, 'ETH');
}

getNativeBalance();

The balance here will be fetched using the provider defined. In this example, the script will fetch the GLMR balance on the Moonbeam network.

Last updated