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 blockpending
— 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.
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.
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:
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.
Body
The address identifier.