Ethersv5 API

Signer

Signer object and its usage in ZKsync.

This class is to be used in a browser environment. The easiest way to construct it is to use the getSigner method of the Web3Provider. This structure extends ethers.providers.JsonRpcSigner and so supports all the methods available for it.

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

getAllBalances

Returns all token balances of the account.

async getAllBalances(): Promise<BalancesMap>

Example

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

const allBalances = await signer.getAllBalances();

getBalance

Returns the amount of the token the Signer has.

Inputs

ParameterTypeDescription
token?AddressThe address of the token. ETH by default.
blockTagBlockTagIn which block a balance should be checked on. committed, i.e. the latest processed one is the default option.
async getBalance(token?: Address, blockTag: BlockTag = 'committed'): Promise<BigNumber>

Example

Get ETH balance.

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

console.log(`ETH balance: ${await signer.getBalance()}`);

Get token balance.

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

const token = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";

console.log(`Token balance: ${await signer.getBalance(token)}`);

getDeploymentNonce

Returns the deployment nonce of the account.

async getDeploymentNonce(): Promise<BigNumber>

Example

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

console.log(`Nonce: ${await signer.getDeploymentNonce()}`);

getL2BridgeContracts

Returns L2 bridge contracts.

async getL2BridgeContracts(): Promise<{
  erc20: IL2Bridge;
  weth: IL2Bridge;
  shared: IL2Bridge;
}>

Example

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

const l2BridgeContracts = await signer.getL2BridgeContracts();

getNonce

Returns account's nonce number.

Inputs

ParameterTypeDescription
blockTag?BlockTagIn which block a balance should be checked on. committed, i.e. the latest processed one is the default option.
async getNonce(blockTag?: BlockTag): Promise<number>

Example

import { Web3Provider, Provider, types } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

const nonce = await signer.getNonce();

transfer

Please note that for now, unlike Ethereum, ZKsync does not support native transfers, i.e. the value field of all transactions is equal to 0. All the token transfers are done through ERC20 transfer function calls.

But for convenience, the Wallet class has transfer method, which can transfer any ERC20 tokens.

Inputs

ParameterTypeDescription
transaction.toAddressThe address of the recipient.
transaction.amountBigNumberishThe amount of the token to transfer.
transaction.token?AddressThe address of the token. ETH by default.
transaction.paymasterParams?PaymasterParamsPaymaster parameters.
transaction.overrides?ethers.CallOverridesTransaction's overrides which may be used to pass L2 gasLimit, gasPrice, value, etc.
async transfer(transaction: {
    to: Address;
    amount: BigNumberish;
    token?: Address;
    paymasterParams?: PaymasterParams;
    overrides?: ethers.CallOverrides;
}): Promise<ethers.ContractTransaction>

Examples

Transfer ETH.

import { Web3Provider, Provider, Wallet, types } from "zksync-ethers";
import { ethers } from "ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(
  browserProvider.getSigner(),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

const transferTx = await signer.transfer({
  to: Wallet.createRandom().address,
  amount: ethers.utils.parseEther("0.01"),
});

const receipt = await transferTx.wait();

console.log(`The sum of ${receipt.value} ETH was transferred to ${receipt.to}`);

Transfer ETH using paymaster to facilitate fee payment with an ERC20 token.

import { Web3Provider, Provider, Wallet, types } from "zksync-ethers";
import { ethers } from "ethers";

const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(
  browserProvider.getSigner(),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

const transferTx = await signer.transfer({
  to: Wallet.createRandom().address,
  amount: ethers.utils.parseEther("0.01"),
  paymasterParams: utils.getPaymasterParams(paymaster, {
    type: "ApprovalBased",
    token: token,
    minimalAllowance: 1,
    innerInput: new Uint8Array(),
  }),
});

const receipt = await transferTx.wait();

console.log(`The sum of ${receipt.value} ETH was transferred to ${receipt.to}`);

Transfer token.

import { Wallet, Provider, types, utils } from "zksync-ethers";
import { ethers } from "ethers";

const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const transferTx = await signer.transfer({
  token: tokenL2,
  to: Wallet.createRandom().address,
  amount: ethers.utils.parseEther("0.01"),
});

const receipt = await transferHandle.wait();

console.log(`The sum of ${receipt.value} token was transferred to ${receipt.to}`);

Transfer token using paymaster to facilitate fee payment with an ERC20 token.

import { Wallet, Provider, types, utils } from "zksync-ethers";
import { ethers } from "ethers";

const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>";
const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token

const provider = Provider.getDefaultProvider(types.Network.Sepolia);
const wallet = new Wallet(PRIVATE_KEY, provider);

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const transferTx = await wallet.transfer({
  token: tokenL2,
  to: Wallet.createRandom().address,
  amount: ethers.utils.parseEther("0.01"),
  paymasterParams: utils.getPaymasterParams(paymaster, {
    type: "ApprovalBased",
    token: token,
    minimalAllowance: 1,
    innerInput: new Uint8Array(),
  }),
});

const receipt = await transferTx.wait();

console.log(`The sum of ${receipt.value} token was transferred to ${receipt.to}`);

withdraw

Initiates the withdrawal process which withdraws ETH or any ERC20 token from the associated account on L2 network to the target account on L1 network.

Inputs

ParameterTypeDescription
transaction.tokenAddressThe address of the token. ETH by default.
transaction.amountBigNumberishThe amount of the token to withdraw.
transaction.to?AddressThe address of the recipient on L1.
transaction.bridgeAddress?AddressThe address of the bridge contract to be used.
transaction.paymasterParams?PaymasterParamsPaymaster parameters.
transaction.overrides?ethers.CallOverridesTransaction's overrides which may be used to pass L2 gasLimit, gasPrice, value, etc.
async withdraw(transaction: {
    token: Address;
    amount: BigNumberish;
    to?: Address;
    bridgeAddress?: Address;
    paymasterParams?: PaymasterParams;
    overrides?: ethers.CallOverrides;
}): Promise<TransactionResponse>

Examples

Withdraw ETH.

import { Web3Provider, Provider, types, utils } from "zksync-ethers";

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(
  browserProvider.getSigner(),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

await signer.withdraw({
  token: utils.ETH_ADDRESS,
  amount: 10_000_000,
});

Withdraw ETH using paymaster to facilitate fee payment with an ERC20 token.

import { Web3Provider, Provider, types, utils } from "zksync-ethers";

const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token

const browserProvider = new Web3Provider(window.ethereum);
const signer = Signer.from(
  browserProvider.getSigner(),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

await signer.withdraw({
  token: utils.ETH_ADDRESS,
  amount: 10_000_000,
  paymasterParams: utils.getPaymasterParams(paymaster, {
    type: "ApprovalBased",
    token: token,
    minimalAllowance: 1,
    innerInput: new Uint8Array(),
  }),
});

Withdraw token.

import { Wallet, Provider, types, utils } from "zksync-ethers";

const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>";

const provider = Provider.getDefaultProvider(types.Network.Sepolia);
const wallet = new Wallet(PRIVATE_KEY, provider);

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const withdrawTx = await wallet.withdraw({
  token: tokenL2,
  amount: 10_000_000,
});

Withdraw token using paymaster to facilitate fee payment with an ERC20 token.

import { Wallet, Provider, types, utils } from "zksync-ethers";

const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>";
const token = "0x927488F48ffbc32112F1fF721759649A89721F8F"; // Crown token which can be minted for free
const paymaster = "0x13D0D8550769f59aa241a41897D4859c87f7Dd46"; // Paymaster for Crown token

const provider = Provider.getDefaultProvider(types.Network.Sepolia);
const wallet = new Wallet(PRIVATE_KEY, provider);

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const withdrawTx = await wallet.withdraw({
  token: tokenL2,
  amount: 10_000_000,
  paymasterParams: utils.getPaymasterParams(paymaster, {
    type: "ApprovalBased",
    token: token,
    minimalAllowance: 1,
    innerInput: new Uint8Array(),
  }),
});

Made with ❤️ by the ZKsync Community