viem-goviem-go

Chains

Use built-in chain definitions or define custom EVM-compatible chains

The chain package provides chain metadata (ID, name, native currency, RPC URLs, block explorers, and optional contracts) used by Public Client and Wallet Client. viem-go mirrors viem's chain concept but uses a single Chain struct and DefineChain instead of separate formatters/fees.

Import

import (
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
  • chain: Chain type, DefineChain, ExtractChain, AssertCurrentChain, and helpers (DefaultRpcUrl, DefaultBlockExplorer).
  • definitions: Prebuilt chain definitions (e.g. definitions.Mainnet, definitions.Polygon). Use with clients by passing a pointer: Chain: &definitions.Mainnet.

Usage

Import a built-in chain from chain/definitions and pass it to the client config. The client expects a pointer to a chain (*chain.Chain).

import (
"github.com/ChefBingbong/viem-go/client"
"github.com/ChefBingbong/viem-go/client/transport"
"github.com/ChefBingbong/viem-go/chain/definitions"
)
publicClient, err := client.CreatePublicClient(client.PublicClientConfig{
Chain: &definitions.Mainnet,
Transport: transport.HTTP("https://eth.llamarpc.com"),
})
if err != nil {
log.Fatal(err)
}
defer publicClient.Close()

Built-in chains

viem-go ships with these chain definitions in chain/definitions:

VariableChainID
definitions.MainnetEthereum1
definitions.ArbitrumArbitrum One42_161
definitions.OptimismOP Mainnet10
definitions.PolygonPolygon PoS137
definitions.BscBNB Smart Chain56
definitions.AvalancheAvalanche C-Chain43_114

Testnets (e.g. Sepolia) are not included; define them with DefineChain and the same struct.

Custom chains

To support an EVM-compatible chain not in the list, define a chain with chain.DefineChain(chain.Chain{...}) and pass a pointer to your client. See Custom Chains for the full Chain type and examples (including Sepolia).

Go notes

  • Pointer: Client config expects Chain *chain.Chain. Built-ins are values, so use &definitions.Mainnet. For a custom chain stored in a variable, use &myChain.
  • DefineChain returns a value (chain.Chain), not a pointer. It copies the config so the returned chain is independent. Store the result and pass &myChain to the client.
  • Primary RPC/explorer: Use the "default" key in RpcUrls and BlockExplorers. Helpers: chain.DefaultRpcUrl() and chain.DefaultBlockExplorer().
  • Block time: When set, the client uses it to derive a default PollingInterval (e.g. for watch actions).

See also

  • Mainnet — Ethereum mainnet
  • Sepolia — Sepolia testnet (custom definition)
  • Custom ChainsDefineChain, Chain type, ExtractChain, AssertCurrentChain