zksync2-go

module
v0.0.0-...-f1447cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2023 License: MIT

README ΒΆ

πŸš€ zksync2-go Golang SDK πŸš€

In order to provide easy access to all the features of zkSync Era, the zksync2-go Golang SDK was created, which is made in a way that has an interface very similar to those of geth. In fact, geth is a dependency of our library and most of the objects exported by zksync2-go ( e.g. Wallet, Client etc.) inherit from the corresponding geth objects and override only the fields that need to be changed.

While most of the existing SDKs should work out of the box, deploying smart contracts or using unique zkSync features, like account abstraction, requires providing additional fields to those that Ethereum transactions have by default.

The library is made in such a way that after replacing geth with zksync2-go most client apps will work out of box.

πŸ”— For a detailed walkthrough, refer to the official documentation.

πŸ“Œ Overview

To begin, it is useful to have a basic understanding of the types of objects available and what they are responsible for, at a high level:

  • Client provides connection to the zkSync Era blockchain, which allows querying the blockchain state, such as account, block or transaction details, querying event logs or evaluating read-only code using call. Additionally, the client facilitates writing to the blockchain by sending transactions.
  • Wallet wraps all operations that interact with an account. An account generally has a private key, which can be used to sign a variety of types of payloads. It provides easy usage of the most common features.

πŸ›  Prerequisites

πŸ“₯ Installation & Setup

go get github.com/zksync-sdk/zksync2-go

πŸ“ Examples

The complete examples with various use cases are available here.

Connect to the zkSync Era network:
import (
    "github.com/ethereum/go-ethereum/ethclient"
    "github.com/zksync-sdk/zksync2-go/clients"
    "log"
    "os"
)

var (
    ZkSyncProvider   = "https://testnet.era.zksync.dev" // zkSync Era testnet  
    EthereumProvider = "https://rpc.ankr.com/eth_goerli" // goerli testnet
)
// Connect to zkSync network
client, err := clients.Dial(ZkSyncProvider)
if err != nil {
    log.Panic(err)
}
defer client.Close()

// Connect to Ethereum network
ethClient, err := ethclient.Dial(EthereumProvider)
if err != nil {
    log.Panic(err)
}
defer ethClient.Close()
Get the latest block number
blockNumber, err := client.BlockNumber(context.Background())
if err != nil {
    log.Panic(err)
}
fmt.Println("Block number: ", blockNumber)
Get the latest block
block, err := client.BlockByNumber(context.Background(), nil)
if err != nil {
    log.Panic(err)
}
fmt.Printf("%+v\n", block)
Create a wallet
privateKey := os.Getenv("PRIVATE_KEY")
w, err := accounts.NewWallet(common.Hex2Bytes(privateKey), &client, ethClient)
if err != nil {
    log.Panic(err)
}
Check account balances
balance, err := w.Balance(context.Background(), utils.EthAddress, nil) // balance on zkSync Era network
if err != nil {
    log.Panic(err)
}
fmt.Println("Balance: ", balance)

balanceL1, err := w.BalanceL1(nil, utils.EthAddress) // balance on goerli network
if err != nil {
    log.Panic(err)
}
fmt.Println("Balance L1: ", balanceL1)
Transfer funds

Transfer funds among accounts on L2 network.

chainID, err := client.ChainID(context.Background())
if err != nil {
    log.Panic(err)
}
receiver, err := accounts.NewRandomWallet(chainID.Int64(), &client, ethClient)
if err != nil {
    log.Panic(err)
}

tx, err := w.Transfer(accounts.TransferTransaction{
    To:     receiver.Address(),
    Amount: big.NewInt(7_000_000_000_000_000_000),
    Token:  utils.EthAddress,
})
if err != nil {
    log.Panic(err)
}
fmt.Println("Transaction: ", tx.Hash())
Deposit funds

Transfer funds from L1 to L2 network.

tx, err := w.Deposit(accounts.DepositTransaction{
    Token:  utils.EthAddress, 
    Amount: big.NewInt(2_000_000_000_000_000_000),
    To:     w.Address(),
})
if err != nil {
    log.Panic(err)
}
fmt.Println("L1 transaction: ", tx.Hash())
Withdraw funds

Transfer funds from L2 to L1 network.

tx, err := w.Withdraw(accounts.WithdrawalTransaction{
    To:     w.Address(),
        Amount: big.NewInt(1_000_000_000_000_000_000),
        Token:  utils.EthAddress,
})
if err != nil {
    log.Panic(err)
}
fmt.Println("Withdraw transaction: ", tx.Hash())

🀝 Contributing

We welcome contributions from the community! If you're interested in contributing to the zksync2-go Golang SDK, please take a look at our CONTRIBUTING.md for guidelines and details on the process.

Thank you for making zksync2-go Golang SDK better! πŸ™Œ

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL