getBalance
Returns the balance of an address in wei
Import
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"Usage
A minimal example showing how to construct a public client and call this action.
import ( "context" "log" "math/big"
"github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public" "github.com/ChefBingbong/viem-go/client" "github.com/ChefBingbong/viem-go/client/transport" "github.com/ChefBingbong/viem-go/chain/definitions")
ctx := context.Background()
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{ Chain: definitions.Mainnet, Transport: transport.HTTP("https://eth.llamarpc.com"),})if err != nil { log.Fatal(err)}defer func() { _ = publicClient.Close() }()
balance, err := public.GetBalance(ctx, publicClient, public.GetBalanceParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"),})if err != nil { log.Fatal(err)}log.Printf("Balance: %s wei", balance.String())Returns
*big.Int
The balance of the address in wei.
Parameters
Configuration options accepted by this action.
Address
- Type:
common.Address - Required
The address of the account.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
balance, err := public.GetBalance(ctx, publicClient, public.GetBalanceParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"),})BlockNumber
- Type:
*uint64 - Optional
The balance of the account at a block number.
import "github.com/ChefBingbong/viem-go/actions/public"
blockNum := uint64(69420)balance, err := public.GetBalance(ctx, publicClient, public.GetBalanceParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"), BlockNumber: &blockNum,})BlockTag
- Type:
BlockTag - Optional
The balance of the account at a block tag.
import "github.com/ChefBingbong/viem-go/actions/public"
balance, err := public.GetBalance(ctx, publicClient, public.GetBalanceParameters{ Address: common.HexToAddress("0xA0Cf798816D4b9b9866b5330EEa46a18382f251e"), BlockTag: public.BlockTagSafe,})Tips
Additional hints for working with this action.
- You can convert the balance to ether units using utility functions from your math library.
JSON-RPC Method
Underlying JSON-RPC method used by this action.