USDCBridge
A class for interacting with the custom USDC bridge.
USDCBridge
class implements AbstractBridge
class and provides methods for depositing and
withdrawing USDC tokens using custom USDC bridge.
constructor(wallet: Wallet)
Creates a USDCBridge
object.
Parameters:
Parameter | Type | Description |
---|---|---|
wallet | Wallet | ZKsync wallet instance. |
Example:
import { Provider, types, Wallet, USDCBridge } from 'zksync-ethers';
import { ethers } from 'ethers';
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || '0x<WALLET_PRIVATE_KEY>';
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);
console.log('USDC Bridge initialized', usdcBridge);
deposit
Deposits USDC tokens using the custom USDC bridge
Inputs
Parameter | Type | Description |
---|---|---|
transaction | IDepositTransaction | Deposit transaction. |
async deposit(transaction: IDepositTransaction): Promise<PriorityOpResponse>
Example
import { Wallet, Provider, types, USDCBridge } from 'zksync-ethers';
import { ethers } from 'ethers';
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || '0x<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);
async function main() {
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();
}
withdraw
Withdraws USDC tokens using the custom USDC bridge from the associated account on L2 network to the target account on L1 network.
Inputs
Parameter | Type | Description |
---|---|---|
transaction | IWithdrawTransaction | Withdraw transaction. |
async withdraw(transaction: IWithdrawTransaction): Promise<TransactionResponse>
Examples
import { Wallet, Provider, types, USDCBridge } from 'zksync-ethers';
import { ethers } from 'ethers';
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || '0x<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);
async function main() {
const withdrawTx = await usdcBridge.withdraw({
token: USDC_TOKEN_L2_ADDRESS,
amount: ethers.parseUnits(AMOUNT, 6),
bridgeAddress: USDC_BRIDGE_L2_ADDRESS,
approveERC20: true,
});
console.log('Withdraw transaction hash:', withdrawTx.hash);
}
finalizeWithdrawal
Proves the inclusion of the L2 -> L1 withdrawal message.
Inputs
Parameter | Type | Description |
---|---|---|
bridgeAddress | Address | The address of the USDC bridge contract to be used. |
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. |
overrides? | ethers.Overrides | Transaction's overrides which may be used to pass L1 gasLimit , gasPrice , value , etc. |
async finalizeWithdrawal(
bridgeAddress: Address,
withdrawalHash: BytesLike,
index: number = 0,
overrides?: ethers.Overrides
): Promise<ethers.ContractTransactionResponse>
Example
import { Wallet, Provider, types, USDCBridge } from 'zksync-ethers';
import { ethers } from 'ethers';
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || '0x<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>';
async function finalizeWithdrawal() {
const finalizeWithdrawalTx = await usdcBridge.finalizeWithdrawal(USDC_BRIDGE_L1_ADDRESS, WITHDRAWAL_TX_HASH);
console.log('Finalize withdrawal transaction hash:', finalizeWithdrawalTx.hash);
}
isWithdrawalFinalized
Returns whether the withdrawal transaction has been finalized on the L1 network.
Inputs
Parameter | Type | Description |
---|---|---|
bridgeAddress | Address | The address of the USDC bridge contract to be used. |
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(
bridgeAddress: Address,
withdrawalHash: BytesLike,
index: number = 0
): Promise<boolean>
Example
import { Wallet, Provider, types, USDCBridge } from 'zksync-ethers';
import { ethers } from 'ethers';
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY || '0x<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>';
async function checkWithdrawalStatus() {
const isFinalized = await usdcBridge.isWithdrawalFinalized(USDC_BRIDGE_L1_ADDRESS, WITHDRAWAL_TX_HASH);
console.log('Withdrawal is finalized:', isFinalized);
}