Go SDK

Getting Started

Getting Started with zksync2-go

This guide provides an overview of the most common operations developers need to start using Go with ZKsync Era.

Ensure you have installed the zksync2-go SDK. If you still need to do so, please refer to the installation guide for detailed instructions.

Overview

The zksync2-go SDK enables developers to interact with the ZKsync Era blockchain using the Go programming language. While many standard SDK features work out of the box, some advanced functionalities like deploying smart contracts or utilizing ZKsync-specific features (such as account abstraction) require additional fields beyond what typical Ethereum transactions include.

To start, it’s beneficial to understand the primary components of the SDK and their roles:

  • Client: Connects to the ZKsync Era blockchain, enabling you to query the blockchain state (e.g., account details, blocks, and transactions), query event logs, or execute read-only code. It also facilitates sending transactions to the blockchain.
  • Signer: Manages operations that interact with an account, typically involving a private key used to sign various payloads.
  • Wallet: A convenient wrapper around Client and Signer, providing easy access to the most commonly used features.

Examples

Connect to the ZKsync Era network:

ZkSyncEraProvider   := "https://testnet.era.zksync.dev"
ZkSyncEraWSProvider := "ws://testnet.era.zksync.dev:3051"

// Connect to ZKsync network
client, err := clients.Dial(ZkSyncEraProvider)
if err != nil {
  log.Panic(err)
}
defer client.Close()

// Connect to ZKsync network using Web Socket
wsClient, err := clients.Dial(ZkSyncEraWSProvider)
if err != nil {
  log.Panic(err)
}
defer wsClient.Close()

Get the chain ID:

chainID, err := client.ChainID(context.Background())
if err != nil {
  log.Panic(err)
}
fmt.Println("Chain ID: ", chainID)

Get the latest block:

block, err := client.BlockByNumber(context.Background(), nil)
if err != nil {
  log.Panic(err)
}
fmt.Printf("%+v\n", *block)

Get the block by hash:

blockHash := common.HexToHash("b472c070c9e121ba42702f6c322b7b266e287a4d8b5fa426ed265b105430c397")
block, err := client.BlockByHash(context.Background(), blockHash)
if err != nil {
  log.Panic(err)
}
fmt.Printf("%+v\n", *block)

Get the transaction by hash:

transactionByHash, _, err := client.TransactionByHash(context.Background(), common.HexToHash("0x9af27afed9a4dd018c0625ea1368afb8ba08e4cfb69b3e76dfb8521c8a87ecfc"))
if err != nil {
  log.Panic(err)
}
fmt.Printf("%+v\n", transactionByHash)

Also, the following examples demonstrate how to:

  1. Deposit ETH and tokens from Ethereum into ZKsync Era
  1. Transfer ETH and tokens on ZKsync Era
  1. Withdraw ETH and tokens from ZKsync Era to Ethereum
  1. Deploy a smart contract using CREATE method
  1. Deploy a smart contract using CREATE2 method
  1. Deploy custom token on ZKsync Era
  1. Deploy smart account
  1. Use paymaster to pay fee with token
Examples are configured to interact with ZKsync Era, and Sepolia test networks.

Made with ❤️ by the ZKsync Community