Ethersv6 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 BrowserProvider. This structure extends ethers.JsonRpcSigner and so supports all the methods available for it.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), Provider.getDefaultProvider(types.Network.Sepolia));

getAllBalances

Returns all token balances of the account.

async getAllBalances(): Promise<BalancesMap>

Example

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), 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<bigint>

Example

Get ETH balance.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), Provider.getDefaultProvider(types.Network.Sepolia));

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

Get token balance.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), 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 { BrowserProvider, Provider, types } from "zksync-ethers";

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), 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 { BrowserProvider, Provider, types } from "zksync-ethers";

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), 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 { BrowserProvider, Provider, types } from "zksync-ethers";

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(await browserProvider.getSigner(), Number((await browserProvider.getNetwork()).chainId), 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.OverridesTransaction'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.Overrides;
}): Promise<ethers.ContractTransaction>

Examples

Transfer ETH.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(
  await browserProvider.getSigner(),
  Number((await browserProvider.getNetwork()).chainId),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

const transferTx = await signer.transfer({
  to: Wallet.createRandom().address,
  amount: ethers.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 { BrowserProvider, 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 BrowserProvider(window.ethereum);
const signer = await Signer.from(
  await browserProvider.getSigner(),
  Number((await browserProvider.getNetwork()).chainId),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

const transferTx = signer.transfer({
  to: Wallet.createRandom().address,
  amount: ethers.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 { BrowserProvider, Provider, Wallet, types } from "zksync-ethers";
import { ethers } from "ethers";

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(
    await browserProvider.getSigner(),
    Number((await browserProvider.getNetwork()).chainId),
    Provider.getDefaultProvider(types.Network.Sepolia)
);

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

const receipt = await transferTx.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 { BrowserProvider, 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 BrowserProvider(window.ethereum);
const signer = await Signer.from(
    await browserProvider.getSigner(),
    Number((await browserProvider.getNetwork()).chainId),
    Provider.getDefaultProvider(types.Network.Sepolia)
);

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const transferTx = signer.transfer({
  token: tokenL2,
  to: Wallet.createRandom().address,
  amount: ethers.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.
overrides?ethers.OverridesTransaction'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.Overrides;
}): Promise<TransactionResponse>

Examples

Withdraw ETH.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(
  await browserProvider.getSigner(),
  Number((await browserProvider.getNetwork()).chainId),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

const tokenL2 = "0x6a4Fb925583F7D4dF82de62d98107468aE846FD1";
const withdrawTx = await signer.withdraw({
  token: utils.ETH_ADDRESS,
  amount: 10_000_000n,
});

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

import { BrowserProvider, 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 BrowserProvider(window.ethereum);
const signer = Signer.from(
  await browserProvider.getSigner(),
  Number((await browserProvider.getNetwork()).chainId),
  Provider.getDefaultProvider(types.Network.Sepolia)
);

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

Withdraw token.

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(
    await browserProvider.getSigner(),
    Number((await browserProvider.getNetwork()).chainId),
    Provider.getDefaultProvider(types.Network.Sepolia)
);

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

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

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

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

const browserProvider = new BrowserProvider(window.ethereum);
const signer = Signer.from(
    await browserProvider.getSigner(),
    Number((await browserProvider.getNetwork()).chainId),
    Provider.getDefaultProvider(types.Network.Sepolia)
);

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

Made with ❤️ by the ZKsync Community