Wallet

Unified interface for managing accounts

It contains the same functions as WalletL1, WalletL2, and Deployer since it implements the Adapter interface, and the usage of those methods is the same.

Init

Creates an instance of Wallet associated with the account provided by the rawPrivateKey. The clientL1 parameters is optional; if not provided, only method form WalletL2 and Deployer can be used, as the rest of the functionalities require communication with the L1 network. A Wallet can be configured to communicate with L1 networks by using and ConnectL1 method.

func NewWallet(rawPrivateKey []byte, clientL2 *clients.Client, clientL1 *ethclient.Client) (*Wallet, error)

Creates an instance of Wallet associated with the account provided by the signer. The clientL2 and clientL1 parameters are optional; if not provided, only SignTransaction, Address and Signer methods can be used, as the rest of the functionalities require communication with the network. A wallet that contains only a signer can be configured to communicate with L2 and L1 networks by using Connect and ConnectL1, respectively.

func NewWalletFromSigner(signer Signer, clientL2 *clients.Client, clientL1 *ethclient.Client) (*Wallet, error)

Creates a new instance of Wallet based on the provided mnemonic phrase. The clientL2 and clientL1 parameters are optional, and can be configured with Connect and ConnectL1, respectively.

func NewWalletFromMnemonic(mnemonic string, chainId int64, clientL2 *clients.Client, clientL1 *ethclient.Client) (*Wallet, error)

Creates a new instance of Wallet based on the provided private key of the account and chain ID. The clientL2 and clientL1 parameters are optional, and can be configured with Connect and ConnectL1, respectively.

func NewWalletFromRawPrivateKey(rawPk []byte, chainId int64, clientL2 *clients.Client, clientL1 *ethclient.Client) (*Wallet, error)

Creates an instance of Wallet with a randomly generated account. The clientL2 and clientL1 parameters are optional, and can be configured with Connect and ConnectL1, respectively.

func NewRandomWallet(chainId int64, clientL2 *clients.Client, clientL1 *ethclient.Client) (*Wallet, error)

Example

PrivateKey     := os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider := "https://sepolia.era.zksync.dev"
EthereumProvider := "https://rpc.ankr.com/eth_sepolia"

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

ethClient, err := ethclient.Dial(EthereumProvider)
if err != nil {
 log.Panic(err)
}
defer ethClient.Close()

wallet, err := accounts.NewWallet(common.Hex2Bytes(PrivateKey), client, ethClient)
if err != nil {
 log.Panic(err)
}

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

Connect

Returns a new instance of Wallet with the provided client for the L2 network.

Inputs

ParameterTypeDescription
client*clients.ClientL2 client.
Connect(client *clients.Client) (*Wallet, error)

Example

PrivateKey     := os.Getenv("PRIVATE_KEY")
ZkSyncEraProvider := "https://testnet.era.zksync.dev"

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

chainID, err := client.ChainID(context.Background())
if err != nil {
 log.Panic(err)
}

wallet, err := accounts.NewRandomWallet(chainID, nil, nil)
if err != nil {
 log.Panic(err)
}

// create new wallet with connection to L2
wallet, err = wallet.Connect(&client)
if err != nil {
 log.Panic(err)
}

ConnectL1

Returns a new instance of Wallet with the provided client for the L1 network.

Inputs

ParameterTypeDescription
client*ethclient.ClientL1 client.
ConnectL1(client *ethclient.Client) (*Wallet, error)

Example

chainID, err := client.ChainID(context.Background())
if err != nil {
 log.Panic(err)
}

wallet, err = accounts.NewRandomWallet(chainID, nil, nil)
if err != nil {
 log.Panic(err)
}

// create new wallet with connection to L1
wallet, err = wallet.Connect(&ethClient)
if err != nil {
 log.Panic(err)
}

Nonce

Returns the account nonce of the associated account. The block number can be nil, in which case the nonce is taken from the latest known block.

Inputs

ParameterTypeDescription
ctxcontext.ContextContext.
blockNumber*big.Int (optional)Block number.
Nonce(ctx context.Context, blockNumber *big.Int) (uint64, error)

Example

nonce, err := wallet.Nonce(context.Background(), big.NewInt(9000))
if err != nil {
 log.Panic(err)
}
fmt.Println("Nonce: ", nonce)

PendingNonce

Returns the account nonce of the associated account in the pending state. This is the nonce that should be used for the next transaction.

Inputs

ParameterTypeDescription
ctxcontext.ContextContext.
PendingNonce(ctx context.Context) (uint64, error)

Example

nonce, err := wallet.PendingNonce(context.Background())
if err != nil {
 log.Panic(err)
}
fmt.Println("Nonce: ", nonce)

Made with ❤️ by the ZKsync Community