Understanding Account Balances
Reading an account balance in Ethereum is straightforward using the Go programming language. The BalanceAt method from the Ethereum client allows you to retrieve the balance of a specific account address, with an optional block number parameter. Setting the block number to nil returns the latest balance.
account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f")
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance) // 25893180161173005034Retrieving Historical Balances
To check an account's balance at a specific block, pass the block number as a big.Int:
blockNumber := big.NewInt(5532993)
balance, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance) // 25729324269165216042Converting Wei to ETH
Ethereum values are represented in wei (the smallest unit). To convert wei to ETH:
- Import Go's
mathandmath/bigpackages. - Perform the calculation:
wei / 10^18.
fbalance := new(big.Float)
fbalance.SetString(balance.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue) // 25.729324269165216041Checking Pending Balances
For pending balances (e.g., during transaction confirmation), use PendingBalanceAt:
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
fmt.Println(pendingBalance) // 25729324269165216042Complete Code Example
👉 Explore more Ethereum development tools
package main
import (
"context"
"fmt"
"log"
"math"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
)
func main() {
client, err := ethclient.Dial("https://cloudflare-eth.com")
if err != nil {
log.Fatal(err)
}
account := common.HexToAddress("0x71c7656ec7ab88b098defb751b7401b5f6d8976f")
// Latest balance
balance, err := client.BalanceAt(context.Background(), account, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(balance)
// Historical balance
blockNumber := big.NewInt(5532993)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
}
fmt.Println(balanceAt)
// Wei to ETH conversion
fbalance := new(big.Float)
fbalance.SetString(balanceAt.String())
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue)
// Pending balance
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
fmt.Println(pendingBalance)
}FAQ
How do I connect to an Ethereum node in Go?
Use the ethclient.Dial method with your node's RPC endpoint. For public nodes, services like Infura or Alchemy provide reliable connections.
What's the difference between wei and ETH?
1 ETH = 10^18 wei. Wei allows precise calculations without floating-point rounding errors.
Why use big.Int for Ethereum values?
Ethereum often deals with numbers exceeding standard integer limits. The big package handles arbitrary-precision arithmetic safely.
Can I check balances for smart contracts?
Yes! Contract addresses have balances just like EOAs (Externally Owned Accounts).
👉 Discover advanced Ethereum development techniques
How often should I check pending balances?
For real-time applications, poll every 15-30 seconds. For less time-sensitive apps, checking every few minutes is sufficient.