UNPKG

zksync-ethers

Version:

A Web3 library for interacting with the ZkSync Layer 2 scaling solution.

195 lines (194 loc) 8.27 kB
import { BigNumberish, BytesLike, ethers, ContractTransactionResponse, Overrides } from 'ethers'; import { Address, PaymasterParams, PriorityOpResponse, TransactionResponse, TransactionLike, FinalizeL1DepositParams } from '../types'; import { Wallet } from '../wallet'; export interface IDepositTransaction { token: Address; amount: BigNumberish; to?: Address; operatorTip?: BigNumberish; bridgeAddress: Address; approveERC20?: boolean; l2GasLimit?: BigNumberish; gasPerPubdataByte?: BigNumberish; refundRecipient?: Address; overrides?: Overrides; approveOverrides?: Overrides; } export interface IWithdrawTransaction { token: Address; amount: BigNumberish; to?: Address; bridgeAddress: Address; paymasterParams?: PaymasterParams; overrides?: Overrides; approveERC20?: boolean; approveOverrides?: Overrides; } /** * `AbstractBridge` is an abstract class that provides a base implementation for bridging assets. */ export declare abstract class AbstractBridge { protected readonly wallet: Wallet; constructor(wallet: Wallet); /** * Returns the amount of approved tokens for a specific L2 bridge. * * @param token The address of the token. * @param bridgeAddress The address of the bridge contract to be used. * @param [blockTag] The block in which an allowance should be checked. * Defaults to 'committed', i.e., the latest processed block. */ protected getAllowanceL2(token: Address, bridgeAddress: Address, blockTag?: ethers.BlockTag): Promise<bigint>; /** * Approves token for the specified bridge. * * @param token The L2 address of the token. * @param bridgeAddress The address of the bridge contract to be used. * @param amount The amount of the token to be approved. * @param [overrides] Transaction's overrides which may be used to pass L2 `gasLimit`, `gasPrice`, `value`, etc. * @returns A promise that resolves to the response of the approval transaction. */ protected approveERC20L2(token: Address, bridgeAddress: Address, amount: BigNumberish, overrides?: ethers.Overrides): Promise<ethers.TransactionResponse>; /** * Returns the deposit calldata for the second bridge. * * @param transaction Deposit transaction. */ protected abstract getSecondBridgeDepositCalldata(transaction: IDepositTransaction): Promise<string>; /** * Validates the deposit parameters. * * @param transaction Deposit transaction. * By default, does nothing. * Override this method in subclasses to implement custom validation logic. */ protected validateDepositParams(_: IDepositTransaction): Promise<void>; /** * Deposit USDC. * * @example * * import { Wallet, Provider, types, USDCBridge } from "zksync-ethers"; * import { ethers } from "ethers"; * * const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>"; * const USDC_TOKEN_L1_ADDRESS = "<USDC_TOKEN_ADDRESS>"; * const USDC_BRIDGE_L1_ADDRESS = "<USDC_BRIDGE_L1_ADDRESS>"; * const AMOUNT = "5"; * * const provider = Provider.getDefaultProvider(types.Network.Sepolia); * const ethProvider = ethers.getDefaultProvider("sepolia"); * const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); * const usdcBridge = new USDCBridge(wallet); * * const depositTx = await usdcBridge.deposit({ * token: USDC_TOKEN_L1_ADDRESS, * amount: ethers.parseUnits(AMOUNT, 6), * approveERC20: true, * bridgeAddress: USDC_BRIDGE_L1_ADDRESS, * }); * * // Note that we wait not only for the L1 transaction to complete but also for it to be * // processed by zkSync. If we want to wait only for the transaction to be processed on L1, * // we can use `await depositTx.waitL1Commit()` * await depositTx.wait(); */ deposit(transaction: IDepositTransaction): Promise<PriorityOpResponse>; /** * Populates the withdraw transaction for the bridge. * * @param transaction Withdraw transaction. */ protected abstract populateWithdrawTransaction(transaction: IWithdrawTransaction): Promise<TransactionLike>; /** * Validates the withdraw parameters. * * @param transaction Withdraw transaction. * By default, does nothing. * Override this method in subclasses to implement custom validation logic. */ protected validateWithdrawParams(_: IWithdrawTransaction): Promise<void>; /** * Withdraw USDC. * * @example * * import { Wallet, Provider, types, USDCBridge } from "zksync-ethers"; * * const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>"; * const USDC_TOKEN_L2_ADDRESS = "<USDC_TOKEN_L2_ADDRESS>"; * const USDC_BRIDGE_L2_ADDRESS = "<USDC_BRIDGE_L2_ADDRESS>"; * const AMOUNT = "5"; * * const provider = Provider.getDefaultProvider(types.Network.Sepolia); * const wallet = new Wallet(PRIVATE_KEY, provider); * const usdcBridge = new USDCBridge(wallet); * * const withdrawTx = await usdcBridge.withdraw({ * token: USDC_TOKEN_L2_ADDRESS, * amount: ethers.parseUnits(AMOUNT, 6), * bridgeAddress: USDC_BRIDGE_L2_ADDRESS, * approveERC20: true, * }); */ withdraw(transaction: IWithdrawTransaction): Promise<TransactionResponse>; /** * Finalizes the L1 deposit. * @param bridgeAddress The address of the bridge contract to use. * @param finalizeParams Finalize L1 deposit params. * @param [overrides] Transaction's overrides for the finalization. */ protected abstract finalizeL1Deposit(bridgeAddress: Address, finalizeParams: FinalizeL1DepositParams, overrides?: Overrides): Promise<ContractTransactionResponse>; /** * Finalizes the withdrawal on L1. * * @example * * import { Wallet, Provider, types, USDCBridge } from "zksync-ethers"; * import { ethers } from "ethers"; * * const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>"; * const USDC_BRIDGE_L1_ADDRESS = "<USDC_BRIDGE_L1_ADDRESS>"; * * const provider = Provider.getDefaultProvider(types.Network.Sepolia); * const ethProvider = ethers.getDefaultProvider("sepolia"); * const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); * const usdcBridge = new USDCBridge(wallet); * * const WITHDRAWAL_TX_HASH = "<WITHDRAWAL_TX_HASH>"; * const finalizeWithdrawalTx = await usdcBridge.finalizeWithdrawal(USDC_BRIDGE_L1_ADDRESS, WITHDRAWAL_TX_HASH); */ finalizeWithdrawal(bridgeAddress: Address, withdrawalHash: BytesLike, index?: number, overrides?: Overrides): Promise<ContractTransactionResponse>; /** * Checks if the withdrawal is finalized. * @param bridgeAddress The address of the bridge contract to use. * @param finalizeParams Params of the L1 finalize. */ protected abstract checkIfWithdrawalIsFinalized(bridgeAddress: Address, finalizeParams: FinalizeL1DepositParams): Promise<boolean>; /** * Checks if the withdrawal is finalized. * * @param bridgeAddress The address of the bridge contract to use. * @param withdrawalHash The hash of the withdrawal transaction. * @param [index] The index of the withdrawal. * * @returns A promise that resolves to a boolean indicating whether the withdrawal is finalized. * * @example * * import { Wallet, Provider, types, USDCBridge } from "zksync-ethers"; * import { ethers } from "ethers"; * * const PRIVATE_KEY = "<WALLET_PRIVATE_KEY>"; * const USDC_BRIDGE_L1_ADDRESS = "<USDC_BRIDGE_L1_ADDRESS>"; * * const provider = Provider.getDefaultProvider(types.Network.Sepolia); * const ethProvider = ethers.getDefaultProvider("sepolia"); * const wallet = new Wallet(PRIVATE_KEY, provider, ethProvider); * const usdcBridge = new USDCBridge(wallet); * * const WITHDRAWAL_TX_HASH = "<WITHDRAWAL_TX_HASH>"; * const isFinalized = await usdcBridge.isWithdrawalFinalized(USDC_BRIDGE_L1_ADDRESS, WITHDRAWAL_TX_HASH); */ isWithdrawalFinalized(bridgeAddress: Address, withdrawalHash: BytesLike, index?: number): Promise<boolean>; }