Getting Started
Get up and running with viem-go in minutes
Get up and running with viem-go in minutes
to start using vime-go in your project just run the install command below
1go get github.com/ChefBingbong/viem-goThe 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@latest2go mod tidyfollow the quick guilde below to quickely set up a mmain.go file with a simple public client and native balance reader.
In order to use the features from viem-go we must first import the features we require.
1package main2
3import (4 "fmt"5 "context" 6 // import the required dependancies from viem-go7 "github.com/ChefBingbong/viem-go/client"8 "github.com/ChefBingbong/viem-go/client/transport"9 "github.com/ChefBingbong/viem-go/chain/definitions"10)Next Set up a Public Client with a Transport and optional Chain configuration.
1const RPC_URL = "https://eth.llamarpc.com" 2
3func main() {4 // Create a public client5 publicClient, err := client.CreatePublicClient(client.PublicClientConfig{6 Chain: &definitions.Mainnet,7 Transport: transport.HTTP(RPC_URL),8 })9
10 defer publicClient.Close()11
12 if err != nil {13 log.Fatal(err)14 }15}Once you have a client, you can query the blockchain. Note we usually pass in whats known as a context, to out client actions. Context is just a useful tool from the Go standard library that helps handle process lifetimes
1// Get the current block number2blockNumber, err := publicClient.GetBlockNumber(context.Background())3if err != nil {4 log.Fatal(err)5}6fmt.Println("Block number:", blockNumber)7
8balance, err := publicClient.getBalance(context.Background())9if err != nil {10 log.Fatal(err)11}12fmt.Println("Block number:", 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.the demo below is a runnable live example showing how to fetch balances from an ERC20 contract.
viem-go read-contract