viem-goviem-go

getBlockNumber

Returns the number of the most recent block seen

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 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)

Returns

uint64

The number of the most recent block.

Parameters

Configuration options accepted by this action.

CacheTime

  • Type: *time.Duration
  • Default: Client's CacheTime
  • Optional

Time that cached block number will remain in memory.

import (
"context"
"time"
"github.com/ChefBingbong/viem-go/actions/public"
)
cacheTime := 4 * time.Second
blockNumber, err := public.GetBlockNumber(ctx, publicClient, public.GetBlockNumberParameters{
CacheTime: &cacheTime,
})

By default, block numbers are cached for the period of the Client's CacheTime.

  • Setting a value above zero will make block number remain in the cache for that period.
  • Setting a value of 0 will disable the cache, and always retrieve a fresh block number.

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_blockNumber