eth_getBalance | Avalanche

Avalanche API method that returns the balance of a specific Avalanche account in Wei, the smallest unit of ether. This method allows developers to retrieve the current balance of an Avalanche 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

    • 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.

Response

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

eth_getBalance code examples

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("0x38545c4b331D8BFb3bee94C62D77a6735b5eF8c0", "latest" )
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const getBalance = async (address, block) => {
    const balance = await provider.send("eth_getBalance", [address, block]);
    console.log(balance);
  };

  getBalance("0x38545c4b331D8BFb3bee94C62D77a6735b5eF8c0", "latest")
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 

address = web3.to_checksum_address("0x38545c4b331D8BFb3bee94C62D77a6735b5eF8c0")
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 = '0xB18614D1e3A3B67A6E0c83Be98AC04157b674083';
const minimumBalance = 10000000000000000000; // 10 Avax in wei

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} AVAX.`);
    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} AVAX.`);
    console.log(`No need to send more funds.`)
  }
}

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

main();

The code is a simple script that checks the balance of an account on the Avalanche 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 AVAX 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 AVAX 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 Avalanche 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!