viem-goviem-go

getChainId

Returns the chain ID of the current network

Import

Import the public actions package so you can call this action.

import "github.com/ChefBingbong/viem-go/actions/public"

Usage

An example showing how to construct a public client and query the current chain ID.

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() }()
chainID, err := public.GetChainID(ctx, publicClient)
if err != nil {
log.Fatal(err)
}
log.Printf("Chain ID: %d", chainID)

Returns

uint64

The chain ID of the current network.

Parameters

This action takes no parameters.

JSON-RPC Method

Underlying JSON-RPC method used by this action.

eth_chainId