eth_getBalance | Ethereum

Ethereum API method that returns the balance of a specific Ethereum account in Wei, the smallest unit of ether. This method allows developers to retrieve the current balance of an Ethereum account for various purposes, such as checking available funds or displaying balance information.

👍

Get you own node endpoint today

Start for free and get your app to production levels immediately. No credit card required.

You can sign up with your GitHub, X, Google, or Microsoft account.

Parameters

  • address — the address to check the balance of.

  • quantity or tag — the integer of a block encoded as hexadecimal or the string with:

    • latest — the most recent block in the blockchain and the current state of the blockchain at the most recent block. A chain reorganization is to be expected.

    • safe — the block that received justification from the beacon chain. Although this block could be involved in a chain reorganization, it would necessitate either a coordinated attack by the majority of validators or an instance of severe propagation latency.

    • finalized — the block accepted as canonical by more than 2/3 of the validators. A chain reorganization is extremely unlikely, and it would require at least 1/3 of the staked ETH to be burned.

    • earliest — the earliest available or genesis block

    • pending — the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

      📘

      See the default block parameter and How The Merge Impacts Ethereum’s Application Layer.

Response

  • quantity — the integer value of the current balance in Wei

eth_getBalance code examples

📘

Learn more about the ChainstackProvider in ethers.js: ethers ChainstackProvider Documentation.

const ethers = require("ethers");

// Create a ChainstackProvider instance for Ethereum mainnet
const chainstack = new ethers.ChainstackProvider("mainnet");

const getBalance = async (address, block) => {
  const balanceHex = await chainstack.send("eth_getBalance", [address, block]);

  // Convert hex to decimal
  const balanceDecimal = parseInt(balanceHex, 16);

  // Convert wei to ether
  const balanceEther = ethers.formatEther(BigInt(balanceDecimal), "ether");

  console.log(balanceEther);
};

getBalance("0xCb6Ed7E78d27FDff28127F9CbD61d861F09a2324", "latest");

const { Web3 } = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

async function getBalance(address, block) {
  const balance = await web3.eth.getBalance(address, block)
  console.log(balance)
 }
 
 getBalance("0xCb6Ed7E78d27FDff28127F9CbD61d861F09a2324", "latest" )
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 

address = web3.to_checksum_address("0xCb6Ed7E78d27FDff28127F9CbD61d861F09a2324")
balance = web3.eth.get_balance(address, "latest") 
print(balance) 

Use case

One practical use case for eth_getBalance is to check the balance of an account for a program that scans the balance periodically and fills up the account when the balance drops below a certain value.

Here's an example of how you can achieve this in web3.js:

const { Web3 } = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

const accountAddress = '0xCb6Ed7E78d27FDff28127F9CbD61d861F09a2324';
const minimumBalance = web3.utils.toWei(10,'ether')

async function checkBalance(address) {
  const balance = await web3.eth.getBalance(address, 'latest');
  return balance;
}

async function fillUp(balance) {
  const minimumConverted = web3.utils.fromWei(String(minimumBalance), 'ether');
  if (balance < minimumBalance) {
    console.log(`The balance of the account ${accountAddress} is below ${minimumConverted} ETH.`);
    console.log(`Sending more funds...`)
    // Call another function to fill up your account
  } else {
    console.log(`The balance of the account ${accountAddress} is above ${minimumConverted} ETH.`);
    console.log(`No need to send more funds.`)
  }
}

async function main() {
  const balance = await checkBalance(accountAddress);
  const balanceInEth = web3.utils.fromWei(balance, 'ether');
  console.log(`The balance of the account ${accountAddress} is ${balanceInEth} ETH. \n`);
  await fillUp(balance);
}

main();

The code is a simple script that checks the balance of an account on the Ethereum network and takes action if the balance falls below a specified minimum.

The accountAddress variable holds the address for which you want to check the balance. The minimumBalance variable holds the minimum balance in Wei you want to check against.

The checkBalance function takes an address as an argument and returns the balance of the account in Wei. The fillUp function takes the balance as an argument and converts the minimum balance from Wei to MATIC using the fromWei method from the web3.js utility library, and checks if the balance is below the minimum balance.

📘

Note that the Wei value is converted only to display it, as it is more accurate to work with the Wei value.

If the balance is below the minimum, it logs a message to the console indicating that more funds need to be sent and calls another function to fill up the account. If the balance is above the minimum, it logs a message indicating that no additional funds need to be sent.

The main function calls the checkBalance and fillUp functions to convert the balance from Wei to MATIC using the fromWei method, and log the balance and the result of the check to the console.

In summary, this script allows you to easily check the balance of an account on the Ethereum network and take action if the balance falls below a specified minimum.

Try the eth_getBalance RPC method yourself

Language
Click Try It! to start a request and see the response here!