Blockchain Development: Retrieving BTC and ETH Balances, BTC UTXO List, and ETH Nonce

·

Retrieving ETH Balance

Obtaining an Ethereum (ETH) balance is straightforward using a node's RPC interface. The primary method used is eth_getBalance.

Parameters:

  1. Wallet Address: The Ethereum address you wish to query.
  2. Balance State:

    • "latest" (confirmed balance)
    • "earliest" (genesis block balance)
    • "pending" (balance including unconfirmed transactions)

Example Request:

params: [
  "0xc94770007dda54cF92009BFF0dE90c06F603a09f",
  "latest"
]

Example:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xc94770007dda54cF92009BFF0dE90c06F603a09f", "latest"],"id":1}'

Response:

{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0x0234c8a3397aab58" // 158972490234375000
}

Retrieving BTC Balance and UTXO List

Due to Bitcoin's UTXO model, directly querying a BTC address balance via RPC is inefficient. Nodes must scan the entire blockchain to maintain a local UTXO list, which can take over 20 minutes. A practical solution is to deploy a BTC blockchain explorer like Insight-API.

Why Use Insight-API?

👉 Explore Insight-API for BTC data


Retrieving ETH Nonce

The nonce in Ethereum prevents double-spending. Each transaction from an account must have a sequential nonce (starting at 0).

Key Rules:

  1. Transactions with too low a nonce are rejected.
  2. Transactions with a nonce too high remain queued.
  3. Gaps in nonces must be filled before later transactions process.

Method: eth_getTransactionCount

Parameters:

  1. Wallet Address: The Ethereum address.
  2. Nonce State:

    • "latest" (confirmed)
    • "pending" (recommended for unconfirmed transactions)

Example Request:

params: [
  "0xc94770007dda54cF92009BFF0dE90c06F603a09f",
  "pending"
]

Example:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionCount","params":["0xc94770007dda54cF92009BFF0dE90c06F603a09f","pending"],"id":1}'

Response:

{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0x1" // Nonce = 1
}

FAQ

1. Why can't I get BTC balances directly from an RPC node?

Bitcoin's UTXO model requires full blockchain scanning to calculate balances, which is time-consuming. A blockchain explorer like Insight-API simplifies this process.

2. What happens if I use the wrong nonce in an ETH transaction?

3. How do I handle pending ETH transactions?

Always query the nonce with "pending" to include unconfirmed transactions.

👉 Learn more about blockchain development