Bridging ERC20 Tokens and Withdrawing Manually
This page explains how to create and bridge ERC20 tokens from Ethereum (L1) to Zircuit (L2) and back programmatically using JavaScript with the ethers and zircuit-viem libraries.
Create Node Environment
npm init -y
npm install @zircuit/zircuit-viemConnect to Smart Contracts on L1 and L2
import { createWalletClient, createPublicClient, http, parseEventLogs, encodeFunctionData } from '@zircuit/zircuit-viem'
import { privateKeyToAccount } from '@zircuit/zircuit-viem/accounts'
import { mainnet, zircuit } from '@zircuit/zircuit-viem/chains'
// The L1 Address of the ERC20 Contract to be bridged
const l1Erc20ContractAddress = "0xYourERC20TokenAddressHere";
// Partial ABI
const l1Erc20ContractABI = [
{
"inputs": [
{
"internalType": "address",
"name": "guy",
"type": "address"
},
{
"internalType": "uint256",
"name": "wad",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
];
// Set up L1 and L2 with the correct URLs
const L1_RPC_URL = "L1_RPC_URL";
const L2_RPC_URL = "L2_RPC_URL";
// Import account from private key
const account = privateKeyToAccount('0xYOUR_PRIVATE_KEY')
// Create clients
const walletClientL1 = createWalletClient({
account,
chain: mainnet,
transport: http(L1_RPC_URL)
})
const walletClientL2 = createWalletClient({
account,
chain: zircuit,
transport: http(L2_RPC_URL)
})
const publicClientL1 = createPublicClient({
chain: mainnet,
transport: http(L1_RPC_URL)
})
const publicClientL2 = createPublicClient({
chain: zircuit,
transport: http(L2_RPC_URL)
})Create ERC20 contract on Zircuit L2
Bridge ERC20 Tokens to L2
Withdrawing ERC20 Tokens from L2 to L1
Start your withdrawal on L2
Wait until the withdrawal is ready to prove
Prove and release the withdrawal on L1
Last updated
Was this helpful?