L1Signer
This class is to be used in a browser environment to do ZKsync-related operations on layer 1.
This class extends ethers.providers.JsonRpcSigner
and so supports all the methods available for it.
The easiest way to construct it is from an Web3Provider
object.
import { Provider, L1Signer, types, utils } from "zksync-ethers";
import { ethers } from "ethers";
const provider = new ethers.providers.Web3Provider(window.ethereum);
const zksyncProvider = Provider.getDefaultProvider(types.Network.Sepolia);
const signer = L1Signer.from(provider.getSigner(), zksyncProvider);
approveERC20
Bridging ERC20 tokens from Ethereum requires approving the tokens to the ZKsync Ethereum smart contract.
Inputs
Parameter | Type | Description |
---|---|---|
token | Address | The Ethereum address of the token. |
amount | BigNumberish | The amount of the token to be approved. |
overrides? | ethers.Overrides? | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
async approveERC20(
token: Address,
amount: BigNumberish,
overrides?: ethers.Overrides & { bridgeAddress?: Address }
): Promise<ethers.TransactionResponse>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x5C221E77624690fff6dd741493D735a17716c26B";
await signer.approveERC20(tokenL1, 5);
claimFailedDeposit
The claimFailedDeposit
method withdraws funds from the initiated deposit, which failed when finalizing on L2.
If the deposit L2 transaction has failed, it sends an L1 transaction calling claimFailedDeposit
method of
the L1 bridge, which results in returning L1 tokens back to the depositor, otherwise throws the error.
Inputs
Parameter | Type | Description |
---|---|---|
depositHash | bytes32 | The L2 transaction hash of the failed deposit. |
async claimFailedDeposit(depositHash: BytesLike): Promise<ethers.ContractTransaction>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const FAILED_DEPOSIT_HASH = "<FAILED_DEPOSIT_TX_HASH>";
const claimFailedDepositHandle = await signer.claimFailedDeposit(FAILED_DEPOSIT_HASH);
deposit
Transfers the specified token from the associated account on the L1 network to the target account on the L2 network.
The token can be either ETH or any ERC20 token. For ERC20 tokens, enough approved tokens must be associated with the
specified L1 bridge (default one or the one defined in transaction.bridgeAddress
).
In this case, depending on is the chain ETH-based or not transaction.approveERC20
or transaction.approveBaseERC20
can be enabled to perform token approval.
If there are already enough approved tokens for the L1 bridge, token approval will be skipped.
To check the amount of approved tokens for a specific bridge, use
the allowanceL1
method.
Inputs
Parameter | Type | Description |
---|---|---|
transaction.token | Address | The address of the token to deposit. ETH by default. |
transaction.amount | BigNumberish | The amount of the token to deposit. |
transaction.to? | Address | The address that will receive the deposited tokens on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.bridgeAddress? | Address | The address of the bridge contract to be used. Defaults to the default ZKsync bridge (either L1EthBridge or L1Erc20Bridge ). |
transaction.approveERC20? | boolean | Whether or not should the token approval be performed under the hood. Set this flag to true if you bridge an ERC20 token and didn't call the approveERC20 function beforehand. |
transaction.approveBaseERC20? | boolean | Whether or not should the base token approval be performed under the hood. Set this flag to true if you bridge a base token and didn't call the approveERC20 function beforehand. |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.gasPerPubdataByte? | BigNumberish | Whether or not should the token approval be performed under the hood. Set this flag to true if you bridge an ERC20 token and didn't call the approveERC20 function beforehand. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value. |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
transaction.approveOverrides? | ethers.Overrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
transaction.approveBaseOverrides? | ethers.Overrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
transaction.customBridgeData? | BytesLike | Additional data that can be sent to a bridge. |
async deposit(transaction: {
token: Address;
amount: BigNumberish;
to?: Address;
operatorTip?: BigNumberish;
bridgeAddress?: Address;
approveERC20?: boolean;
approveBaseERC20?: boolean;
l2GasLimit?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
approveOverrides?: ethers.Overrides;
approveBaseOverrides?: ethers.Overrides;
customBridgeData?: BytesLike;
}): Promise<PriorityOpResponse>
Example
Deposit ETH on ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
await signer.deposit({
token: utils.ETH_ADDRESS,
amount: 10_000_000,
});
Deposit token on ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const tokenL1 = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be";
await signer.deposit({
token: tokenL1,
amount: 10_000_000,
approveERC20: true,
});
Deposit ETH on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
await signer.deposit({
token: utils.ETH_ADDRESS,
amount: 10_000_000,
approveBaseERC20: true,
});
Deposit base token on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
await signer.deposit({
token: await signer.getBaseToken(),
amount: 10_000_000,
approveERC20: true, // or approveBaseERC20: true
});
Deposit non-base token on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const tokenL1 = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be";
await signer.deposit({
token: tokenL1,
amount: 10_000_000,
approveERC20: true,
approveBaseERC20: true,
});
estimateGasDeposit
Estimates the amount of gas required for a deposit transaction on L1 network. Gas of approving ERC20 token is not included in estimation.
Inputs
Parameter | Type | Description |
---|---|---|
transaction.token | Address | The address of the token to deposit. ETH by default. |
transaction.amount | BigNumberish | The amount of the token to deposit. |
transaction.to? | Address | The address that will receive the deposited tokens on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.bridgeAddress? | Address | The address of the bridge contract to be used. Defaults to the default ZKsync bridge (either L1EthBridge or L1Erc20Bridge ). |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.gasPerPubdataByte? | BigNumberish | Whether or not should the token approval be performed under the hood. Set this flag to true if you bridge an ERC20 token and didn't call the approveERC20 function beforehand. |
transaction.customBridgeData? | BytesLike | Additional data that can be sent to a bridge. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value. |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
async estimateGasDeposit(transaction:
token: Address;
amount: BigNumberish;
to?: Address;
operatorTip?: BigNumberish;
bridgeAddress?: Address;
customBridgeData?: BytesLike;
l2GasLimit?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<BigNumber>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x5C221E77624690fff6dd741493D735a17716c26B";
const gas = await signer.estimateGasDeposit({
token: tokenL1,
amount: 10_000_000,
});
console.log(`Gas: ${gas}`);
estimateGasRequestExecute
Estimates the amount of gas required for a request execute transaction.
Inputs
Name | Type | Description |
---|---|---|
transaction.contractAddress? | Address | The L2 contract to be called. |
transaction.calldata? | BytesLike | The input of the L2 transaction. |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.mintValue? | BigNumberish | The amount of base token that needs to be minted on non-ETH-based L2.. |
transaction.l2Value? | BigNumberish | msg.value of L2 transaction. |
transaction.factoryDeps? | ethers.BytesLike[] | An array of L2 bytecodes that will be marked as known on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.gasPerPubdataByte? | BigNumberish | The L2 gas price for each published L1 calldata byte. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value . |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L2 gasLimit , gasPrice , value , etc. |
async estimateGasRequestExecute(transaction: {
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
mintValue?: BigNumberish;
l2Value?: BigNumberish;
factoryDeps?: BytesLike[];
operatorTip?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<BigNumber>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const gas = await signer.estimateGasRequestExecute({
contractAddress: await signer.providerL2.getMainContractAddress(),
calldata: "0x",
l2Value: 7_000_000_000,
});
console.log(`Gas: ${gas}`);
finalizeWithdrawal
Proves the inclusion of the L2 -> L1 withdrawal message.
Inputs
Name | Type | Description |
---|---|---|
withdrawalHash | BytesLike | Hash of the L2 transaction where the withdrawal was initiated. |
index? | number | In case there were multiple withdrawals in one transaction, you may pass an index of the withdrawal you want to finalize. Defaults to 0. |
async finalizeWithdrawal(withdrawalHash: BytesLike, index: number = 0): Promise<ethers.ContractTransactionResponse>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const WITHDRAWAL_HASH = "<WITHDRAWAL_TX_HASH>";
const finalizeWithdrawHandle = await signer.finalizeWithdrawal(WITHDRAWAL_HASH);
finalizeWithdrawalParams
Returns the parameters required for finalizing a withdrawal from the withdrawal transaction's log on the L1 network.
Inputs
Name | Type | Description |
---|---|---|
withdrawalHash | BytesLike | Hash of the L2 transaction where the withdrawal was initiated. |
index? | number | In case there were multiple withdrawals in one transaction, you may pass an index of the withdrawal you want to finalize. Defaults to 0). |
async finalizeWithdrawalParams(withdrawalHash: BytesLike, index: number = 0): Promise<FinalizeWithdrawalParams>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const WITHDRAWAL_HASH = "<WITHDRAWAL_TX_HASH>";
const params = await signer.finalizeWithdrawalParams(WITHDRAWAL_HASH);
getAllowanceL1
Returns the amount of approved tokens for a specific L1 bridge.
Inputs
Parameter | Type | Description |
---|---|---|
token | Address | The Ethereum address of the token. |
bridgeAddress? | Address | The address of the bridge contract to be used. Defaults to the default ZKsync bridge, either L1EthBridge or L1Erc20Bridge . |
blockTag? | BlockTag | In which block an allowance should be checked on. committed , i.e. the latest processed one is the default option. |
async getAllowanceL1(
token: Address,
bridgeAddress?: Address,
blockTag?: ethers.BlockTag
): Promise<BigNumber>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x5C221E77624690fff6dd741493D735a17716c26B";
console.log(`Token allowance: ${await signer.getAllowanceL1(tokenL1)}`);
getBalanceL1
Returns the amount of the token the L1Signer
has on Ethereum.
Inputs
Parameter | Type | Description |
---|---|---|
token? | Address | The address of the token. ETH by default. |
blockTag? | BlockTag | In which block a balance should be checked on. committed , i.e. the latest processed one is the default option. |
async getBalanceL1(token?: Address, blockTag?: BlockTag): Promise<BigNumber>
Example
Get ETH balance.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
console.log(await signer.getBalanceL1());
Get token balance.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be";
console.log(await signer.getBalanceL1(tokenL1));
getBaseCost
Returns base cost for L2 transaction.
Inputs
Name | Type | Description |
---|---|---|
params.gasLimit | BigNumberish | The gasLimit for the L2 contract call. |
params.gasPerPubdataByte? | BigNumberish | The L2 gas price for each published L1 calldata byte. |
params.gasPrice? | BigNumberish | The L1 gas price of the L1 transaction that will send the request for an execute call. |
async getBaseCost(params: {
gasLimit: BigNumberish;
gasPerPubdataByte?: BigNumberish;
gasPrice?: BigNumberish;
}): Promise<BigNumber>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
console.log(`Base cost: ${await signer.getBaseCost({ gasLimit: 100_000 })}`);
getBaseToken
Returns the address of the base token on L1.
async getBaseToken(): Promise<string>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
console.log(`Base token: ${await signer.getBaseToken()}`);
getBridgehubContract
Returns Contract
wrapper of the Bridgehub smart contract.
async getBridgehubContract(): Promise<IBridgehub>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const bridgehub = await signer.getBridgehubContract();
getDepositAllowanceParams
Returns the parameters for the approval token transaction based on the deposit token and amount. Some deposit transactions require multiple approvals. Existing allowance for the bridge is not checked; allowance is calculated solely based on the specified amount.
Inputs
Parameter | Type | Description |
---|---|---|
token | Address | The address of the token to deposit. |
amount | BigNumberish | The amount of the token to deposit. |
async getDepositAllowanceParams(
token: Address,
amount: BigNumberish
): Promise<
{
token: Address;
allowance: BigNumberish;
}[]
>
Example
Get allowance parameters for depositing token on ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const token = "<L1_TOKEN>";
const amount = 5;
const approveParams = await signer.getDepositAllowanceParams(token, amount);
await (
await signer.approveERC20(
approveParams[0].token,
approveParams[0].allowance
)
).wait();
Get allowance parameters for depositing ETH on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const token = utils.LEGACY_ETH_ADDRESS;
const amount = 5;
const approveParams = await signer.getDepositAllowanceParams(token, amount);
await (
await signer.approveERC20(
approveParams[0].token,
approveParams[0].allowance
)
).wait();
Get allowance parameters for depositing base token on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const token = await signer.getBaseToken();
const amount = 5;
const approveParams = await signer.getDepositAllowanceParams(token, amount);
await (
await signer.approveERC20(
approveParams[0].token,
approveParams[0].allowance
)
).wait();
Get allowance parameters for depositing non-base token on non-ETH-based chain.
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const token = "<L1_TOKEN>";
const amount = 5;
const approveParams = await signer.getDepositAllowanceParams(token, amount);
await (
await signer.approveERC20(
approveParams[0].token,
approveParams[0].allowance
)
).wait();
await (
await signer.approveERC20(
approveParams[1].token,
approveParams[1].allowance
)
).wait();
getDepositTx
Returns populated deposit transaction.
Inputs
Parameter | Type | Description |
---|---|---|
transaction.token | Address | The address of the token to deposit. ETH by default. |
transaction.amount | BigNumberish | The amount of the token to deposit. |
transaction.to? | Address | The address that will receive the deposited tokens on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.bridgeAddress? | Address | The address of the bridge contract to be used. Defaults to the default ZKsync bridge (either L1EthBridge or L1Erc20Bridge ). |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.gasPerPubdataByte? | BigNumberish | Whether or not should the token approval be performed under the hood. Set this flag to true if you bridge an ERC20 token and didn't call the approveERC20 function beforehand. |
transaction.customBridgeData? | BytesLike | Additional data that can be sent to a bridge. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value. |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
async getDepositTx(transaction: {
token: Address;
amount: BigNumberish;
to?: Address;
operatorTip?: BigNumberish;
bridgeAddress?: Address;
l2GasLimit?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
customBridgeData?: BytesLike;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<any>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be";
const tx = await signer.getDepositTx({
token: tokenL1,
amount: 10_000_000,
});
getFullRequiredDepositFee
Retrieves the full needed ETH fee for the deposit. Returns the L1 fee and the L2 fee FullDepositFee
.
Inputs
Parameter | Type | Description |
---|---|---|
transaction.token | Address | The address of the token to deposit. ETH by default. |
transaction.to? | Address | The address that will receive the deposited tokens on L2. |
transaction.bridgeAddress? | Address | The address of the bridge contract to be used. Defaults to the default ZKsync bridge (either L1EthBridge or L1Erc20Bridge ). |
transaction.customBridgeData? | BytesLike | Additional data that can be sent to a bridge. |
transaction.gasPerPubdataByte? | BigNumberish | Whether or not should the token approval be performed under the hood. Set this flag to true if you bridge an ERC20 token and didn't call the approveERC20 function beforehand. |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
async getFullRequiredDepositFee(transaction: {
token: Address;
to?: Address;
bridgeAddress?: Address;
customBridgeData?: BytesLike;
gasPerPubdataByte?: BigNumberish;
overrides?: ethers.PayableOverrides;
}): Promise<FullDepositFee>
Example
import { Provider, L1Signer, Wallet, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x56E69Fa1BB0d1402c89E3A4E3417882DeA6B14Be";
const fee = await signer.getFullRequiredDepositFee({
token: tokenL1,
to: Wallet.createRandom().address,
});
console.log(`Fee: ${fee}`);
getL1BridgeContracts
Returns L1 bridge contracts.
async getL1BridgeContracts(): Promise<{
erc20: IL1ERC20Bridge;
weth: IL1ERC20Bridge;
shared: IL1SharedBridge;
}>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const l1BridgeContracts = await signer.getL1BridgeContracts();
getMainContract
Returns the main ZKsync Era smart contract address.
async getMainContract(): Promise<IZkSyncStateTransition>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const mainContract = await signer.getMainContract();
console.log(mainContract.address);
getPriorityOpConfirmation
Returns the transaction confirmation data that is part of L2->L1
message.
Inputs
Name | Type | Description |
---|---|---|
txHash | BytesLike | Hash of the L2 transaction where the withdrawal was initiated. |
index? | number | In case there were multiple transactions in one message, you may pass an index of the transaction which confirmation data should be fetched. Defaults to 0. |
async getPriorityOpConfirmation(txHash: string, index: number = 0): Promise<{
l1BatchNumber: number;
l2MessageIndex: number;
l2TxNumberInBlock: number;
proof: string[]
}>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
// Any L2 -> L1 transaction can be used.
// In this case, withdrawal transaction is used.
const tx = "0x2a1c6c74b184965c0cb015aae9ea134fd96215d2e4f4979cfec12563295f610e";
console.log(`Confirmation data: ${utils.toJSON(await signer.getPriorityOpConfirmation(tx, 0))}`);
getRequestExecuteAllowanceParams
Returns the parameters for the approval token transaction based on the request execute transaction. Existing allowance for the bridge is not checked; allowance is calculated solely based on the specified transaction.
Inputs
Name | Type | Description |
---|---|---|
transaction.contractAddress | Address | The L2 contract to be called. |
transaction.calldata | BytesLike | The input of the L2 transaction. |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.l2Value? | BigNumberish | msg.value of L2 transaction. |
transaction.factoryDeps? | ethers.BytesLike[] | An array of L2 bytecodes that will be marked as known on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.gasPerPubdataByte? | BigNumberish | The L2 gas price for each published L1 calldata byte. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value . |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L2 gasLimit , gasPrice , value , etc. |
async getRequestExecuteAllowanceParams(transaction: {
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
l2Value?: BigNumberish;
factoryDeps?: BytesLike[];
operatorTip?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<{token: Address; allowance: BigNumberish}>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
const tx = {
contractAddress: await signer.getAddress(),
calldata: '0x',
l2Value: 7_000_000_000,
};
const approveParams = await signer.getRequestExecuteAllowanceParams(tx);
await (
await signer.approveERC20(
approveParams.token,
approveParams.allowance
)
).wait();
getRequestExecuteTx
Returns populated deposit transaction.
Inputs
Name | Type | Description |
---|---|---|
transaction.contractAddress? | Address | The L2 contract to be called. |
transaction.calldata? | BytesLike | The input of the L2 transaction. |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.mintValue? | BigNumberish | The amount of base token that needs to be minted on non-ETH-based L2.. |
transaction.l2Value? | BigNumberish | msg.value of L2 transaction. |
transaction.factoryDeps? | ethers.BytesLike[] | An array of L2 bytecodes that will be marked as known on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.gasPerPubdataByte? | BigNumberish | The L2 gas price for each published L1 calldata byte. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value . |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L2 gasLimit , gasPrice , value , etc. |
async getRequestExecuteTx(transaction: {
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
mintValue?: BigNumberish;
l2Value?: BigNumberish;
factoryDeps?: BytesLike[];
operatorTip?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<PopulatedTransaction>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tx = await signer.getRequestExecuteTx({
contractAddress: await signer.providerL2.getMainContractAddress(),
calldata: "0x",
l2Value: 7_000_000_000,
});
isETHBasedChain
Returns whether the chain is ETH-based.
async isETHBasedChain(): Promise<boolean>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(
browserProvider.getSigner(),
Provider.getDefaultProvider(types.Network.Sepolia)
);
console.log(`Is ETH-based chain: ${await signer.isETHBasedChain()}`);
isWithdrawalFinalized
Returns weather the withdrawal transaction is finalized on the L1 network.
Inputs
Name | Type | Description |
---|---|---|
withdrawalHash | BytesLike | Hash of the L2 transaction where the withdrawal was initiated. |
index? | number | In case there were multiple withdrawals in one transaction, you may pass an index of the withdrawal you want to finalize. Defaults to 0). |
async isWithdrawalFinalized(withdrawalHash: BytesLike, index: number = 0): Promise<boolean>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const WITHDRAWAL_HASH = "<WITHDRAWAL_TX_HASH>";
const isFinalized = await signer.isWithdrawalFinalized(WITHDRAWAL_HASH);
l2TokenAddress
Returns the L2 token address equivalent for a L1 token address as they are not equal. ETH's address is set to zero address.
Inputs
Parameter | Type | Description |
---|---|---|
token | Address | The address of the token on L1. |
async l2TokenAddress(token: Address): Promise<string>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
const tokenL1 = "0x5C221E77624690fff6dd741493D735a17716c26B";
console.log(`Token L2 address: ${await signer.l2TokenAddress(tokenL1)}`);
requestExecute
Request execution of L2 transaction from L1.
Inputs
Name | Type | Description |
---|---|---|
transaction.contractAddress? | Address | The L2 contract to be called. |
transaction.calldata? | BytesLike | The input of the L2 transaction. |
transaction.l2GasLimit? | BigNumberish | Maximum amount of L2 gas that transaction can consume during execution on L2. |
transaction.mintValue? | BigNumberish | The amount of base token that needs to be minted on non-ETH-based L2.. |
transaction.l2Value? | BigNumberish | msg.value of L2 transaction. |
transaction.factoryDeps? | ethers.BytesLike[] | An array of L2 bytecodes that will be marked as known on L2. |
transaction.operatorTip? | BigNumberish | (currently is not used) If the ETH value passed with the transaction is not explicitly stated in the overrides, this field will be equal to the tip the operator will receive on top of the base cost of the transaction. |
transaction.gasPerPubdataByte? | BigNumberish | The L2 gas price for each published L1 calldata byte. |
transaction.refundRecipient? | Address | The address on L2 that will receive the refund for the transaction. If the transaction fails, it will also be the address to receive l2Value . |
transaction.overrides? | ethers.PayableOverrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
requestExecute(transaction: {
contractAddress: Address;
calldata: BytesLike;
l2GasLimit?: BigNumberish;
mintValue?: BigNumberish;
l2Value?: BigNumberish;
factoryDeps?: BytesLike[];
operatorTip?: BigNumberish;
gasPerPubdataByte?: BigNumberish;
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<PriorityOpResponse>
Example
import { Provider, L1Signer, types } from "zksync-ethers";
import { ethers } from "ethers";
const browserProvider = new ethers.providers.Web3Provider(window.ethereum);
const signer = L1Signer.from(browserProvider.getSigner(), Provider.getDefaultProvider(types.Network.Sepolia));
await signer.requestExecute({
contractAddress: await signer.providerL2.getMainContractAddress(),
calldata: "0x",
l2Value: 7_000_000_000,
});