eth_getBalance - Ethereum

Check the balance of a specified Ethereum address with the balance method. Query account balance at specific blocks or transaction states, supporting various time points like latest or pending

The eth_getBalance method is part of the Ethereum JSON-RPC API and is used to retrieve the balance of a specified Ethereum address.

This method allows you to check the account balance at a specific block or transaction state, making it useful for various DApps and Ethereum-based applications. The method can be queried with the latest, earliest, or pending block tags to get the balance at different points in time. It provides a convenient API method to access the account balance, supporting both request and parameters such as the account address and block tag.

Supported Networks

The eth_getBalance RPC Ethereum method works on the following Ethereum network types

  • Mainnet

  • Testnet: Sepolia, Holesky

Parameters

DATA: A 20-byte Ethereum account address to retrieve the balance for. The address must be in hexadecimal format, prefixed with 0x. QUANTITY|TAG: A string representing the block number or one of the following tags:

  • latest (default): Retrieves the balance of the account at the latest block.

  • earliest: Retrieves the balance at the first block (genesis block).

  • pending: Retrieves the balance of the account in the pending block.

Request

URL

https://go.getblock.io/<ACCESS-TOKEN>/

To make a request to the eth_getBalance method, you can use the following curl command. The request sends a JSON-RPC query to the Ethereum network via the GetBlock API

curl --location --request POST 'https://go.getblock.io/<ACCESS-TOKEN>/' 
--header 'Content-Type: application/json' 
--data-raw {
    "jsonrpc": "2.0",
    "method": "eth_getBalance",
    "params": [
        "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
        "latest"
    ],
    "id": "getblock.io"
}

Response

The response will contain the balance of the specified Ethereum address, represented as a hexadecimal string in Wei. Below is an example response

{
    "id": "getblock.io",
    "jsonrpc": "2.0",
    "result": "0x36ea"
}

Response Description

  • result: The balance of the Ethereum address in Wei, given as a hexadecimal string. In the example, the balance is 0x36ea, which is the equivalent of 14006 Wei.

Use Case

The eth_getBalance method is often used in decentralized applications (DApps), including those integrated with MetaMask, to check the balance of Ethereum accounts before performing transactions. For example, DApps may need to verify that a user has sufficient funds to complete a transaction or make a purchase. By using the eth_getBalance method, developers can easily retrieve the balance of an account at any given block or the latest state of the Ethereum blockchain. When interacting with MetaMask, this method can be called to display the user's wallet balance in real-time, providing a seamless experience for users during transactions.

Code Example

To retrieve the balance of an address using axios, you can use the eth_getBalance method as follows

import requests
import json

# Define the API URL and access token
url = 'https://go.getblock.io/<ACCESS-TOKEN>/'
headers = {'Content-Type': 'application/json'}

# Prepare the request data
data = {
    "jsonrpc": "2.0",
    "method": "eth_getBalance",
    "params": [
        "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73",
        "latest"
    ],
    "id": "getblock.io"
}

# Send the POST request
response = requests.post(url, headers=headers, data=json.dumps(data))

# Parse the JSON response
response_data = response.json()

# Print the result
print(json.dumps(response_data, indent=4))

This code will retrieve the balance of the specified address on the Ethereum mainnet and print it in ETH.

Common Errors

eth_getBalance error: Common issues when using the eth_getBalance method include incorrect address format, network connectivity problems, or invalid block/tag parameters. Ensure that the address is valid, and the network you are querying is correct.

Last updated

© 2019-2024 GetBlock LLC. All rights reserved ID: 21835790. Address: Belgrade, Serbia.