getBalance
Returns the balance of an address in wei
Loading...
Returns the balance of an address in wei
Import the public actions package so you can call this action.
import "github.com/ChefBingbong/viem-go/actions/public"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())*big.Int
The balance of the address in wei.
Configuration options accepted by this action.
common.AddressThe 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"),})*uint64The 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,})BlockTagThe 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,})Additional hints for working with this action.
Underlying JSON-RPC method used by this action.