getBlock
Returns information about a block at a block number, hash or tag
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 fetch block data.
import ( "context" "log"
"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() }()
// Get latest blockblock, err := public.GetBlock(ctx, publicClient, public.GetBlockParameters{})if err != nil { log.Fatal(err)}log.Printf("Block number: %d", block.Number)Returns
*types.Block
Information about the block.
Parameters
Configuration options accepted by this action.
BlockHash
- Type:
*common.Hash - Optional
Information at a given block hash.
import ( "github.com/ethereum/go-ethereum/common" "github.com/ChefBingbong/viem-go/actions/public")
blockHash := common.HexToHash("0x89644bbd5c8d682a2e9611170e6c1f02573d866d286f006cbf517eec7254ec2d")block, err := public.GetBlock(ctx, publicClient, public.GetBlockParameters{ BlockHash: &blockHash,})BlockNumber
- Type:
*uint64 - Optional
Information at a given block number.
import "github.com/ChefBingbong/viem-go/actions/public"
blockNum := uint64(42069)block, err := public.GetBlock(ctx, publicClient, public.GetBlockParameters{ BlockNumber: &blockNum,})BlockTag
- Type:
BlockTag - Default:
"latest" - Optional
Information at a given block tag.
import "github.com/ChefBingbong/viem-go/actions/public"
block, err := public.GetBlock(ctx, publicClient, public.GetBlockParameters{ BlockTag: public.BlockTagSafe,})IncludeTransactions
- Type:
bool - Default:
false - Optional
Whether or not to include transactions (as a structured array of Transaction objects).
import "github.com/ChefBingbong/viem-go/actions/public"
block, err := public.GetBlock(ctx, publicClient, public.GetBlockParameters{ IncludeTransactions: true,})JSON-RPC Method
Underlying JSON-RPC methods used by this action.
- Calls
eth_getBlockByNumberforblockNumber&blockTag. - Calls
eth_getBlockByHashforblockHash.