viem-goviem-go

Getting Started

Get up and running with viem-go in minutes

Installation

to start using vime-go in your project just run the install command below

1go get github.com/ChefBingbong/viem-go

Versions

The viem-go library is in active development and is changing regulkarly as new features roll out. Because of this make signature that you are always on the latest version. to check and update tot he latest version, run.

1go get github.com/ChefBingbong/viem-go@latest
2go mod tidy
Make sure to keep up with the latest changes and releases by checking out our changelogs and GitHub releases.

Quick Start

1. Create a Client

Set up a Public Client with a Transport and optional Chain configuration.

1package main
2
3import (
4 "context"
5 "fmt"
6 "log"
7
8 "github.com/ChefBingbong/viem-go/client"
9 "github.com/ChefBingbong/viem-go/client/transport"
10 "github.com/ChefBingbong/viem-go/chain/definitions"
11)
12
13func main() {
14 // Create a public client
15 publicClient, err := client.CreatePublicClient(client.PublicClientConfig{
16 Chain: definitions.Mainnet,
17 Transport: transport.HTTP("https://eth.llamarpc.com"),
18 })
19 if err != nil {
20 log.Fatal(err)
21 }
22 defer publicClient.Close()
23
24 fmt.Println("Client created successfully!")
25}

In production, always use an authenticated RPC endpoint (Infura, Alchemy, etc.) instead of public endpoints

2. Read Blockchain Data

Once you have a client, you can query the blockchain:

1// Get the current block number
2blockNumber, err := publicClient.GetBlockNumber(context.Background())
3if err != nil {
4 log.Fatal(err)
5}
6fmt.Println("Block number:", blockNumber)
7
8// Read USDC balance
9result, err := publicClient.ReadContract(context.Background(), client.ReadContractOptions{
10 Address: common.HexToAddress("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"),
11 ABI: `[{"name":"balanceOf","type":"function","inputs":[{"name":"owner","type":"address"}],"outputs":[{"type":"uint256"}]}]`,
12 FunctionName: "balanceOf",
13 Args: []any{common.HexToAddress("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")},
14})
15if err != nil {
16 log.Fatal(err)
17}
18
19balance := result.(*big.Int)
20fmt.Println("USDC Balance:", balance)
ReadContract works with any contract function, not just balanceOf. You can call any ERC-20, ERC-721, or ERC-1155 method -- or any custom contract ABI -- using the same pattern.

Interactive Demo

the demo below is a runnable live example showing how to fetch balances from an ERC20 contract.

Try viem-go (readContract)
Gomain.go
Loading...
Terminaldocs runnerCommandviem-go read-contract
Click Run to execute a real readContract against RPC.

Next Steps

Curious how viem-go stacks up against TypeScript viem? Check out the benchmark results on the introduction page -- viem-go wins all 59 benchmarks with a 7.12x geometric mean speedup.