getBlockNumber
Returns the number of the most recent block seen
Loading...
Returns the number of the most recent block seen
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 fetch the latest block number.
import ( "context" "log"
"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() }()
blockNumber, err := public.GetBlockNumber(ctx, publicClient, public.GetBlockNumberParameters{})if err != nil { log.Fatal(err)}log.Printf("Block number: %d", blockNumber)uint64
The number of the most recent block.
Configuration options accepted by this action.
*time.DurationCacheTimeTime that cached block number will remain in memory.
import ( "context" "time"
"github.com/ChefBingbong/viem-go/actions/public")
cacheTime := 4 * time.SecondblockNumber, err := public.GetBlockNumber(ctx, publicClient, public.GetBlockNumberParameters{ CacheTime: &cacheTime,})By default, block numbers are cached for the period of the Client's CacheTime.
0 will disable the cache, and always retrieve a fresh block number.Underlying JSON-RPC method used by this action.