Bridging ERC-20 token can be done using L1StandardBridge contract. Let's define ABIs for required functions.
What is an ABI?
An ABI (Application Binary Interface) is the interface required to interact with a smart contract. It defines the contract’s function names, parameters, and return values. Clients, SDKs, and other tools use the ABI to make calls to the contract.
Get L1 faucet tokens
To make a deposit (Ethereum -> GIWA), you’ll need faucet tokens on the Ethereum Sepolia network. The L1 token defined above includes a claimFaucet function. Run the code below to call claimFaucet and receive your L1 faucet tokens.
1
Writing the code
2
Running
Deposit ERC-20 Token (Ethereum -> GIWA)
Now let’s bridge the faucet tokens from Ethereum to GIWA.
When you run the code below, your faucet tokens on Ethereum will actually be sent to the L1StandardBridge contract, and the same amount will be credited to your GIWA wallet. This process is called Lock-and-Mint .
1
Flow
Grant approval to L1StandardBridge contract so that it can transfer your ERC-20 tokens
Send the deposit transaction on L1
The corresponding L2 deposit transaction is created by the sequencer
Deposit complete
2
Writing the code
3
Running
Withdraw ERC-20 Token (GIWA -> Ethereum)
Have you successfully sent ERC-20 from Ethereum to GIWA? Now let’s bridge it back in the opposite direction — from GIWA to Ethereum.
When you run the code below, your ERC-20 tokens on GIWA are burned, and the same amount is released to your Ethereum wallet. In this case, the tokens locked in the L1StandardBridge contract during the Deposit are now unlocked. This process is called Burn-and-Unlock.
1
Flow
Send the withdrawal initiation transaction on L2
Send the withdrawal prove transaction on L1
Send the withdrawal finalize transaction on L1
Withdrawal complete
2
Writing the code
3
Running
Learn more
Interested in bridging data in addition to assets like ETH and ERC-20 tokens? Read the OP Stack docs and build your own implementation.
import {
account,
publicClientL1,
walletClientL1
} from "./config";
import {
l1StandardBridgeAbi,
l1StandardBridgeAddress,
l1TokenAddress, l2TokenAddress,
testTokenAbi
} from "./contract";
import {formatEther, parseUnits} from "viem";
import {getL2TransactionHashes} from "viem/op-stack";
async function main() {
// Before bridging to GIWA, check your L1 ERC-20 token balance.
const l1TokenBalance = await publicClientL1.readContract({
address: l1TokenAddress,
abi: testTokenAbi,
functionName: 'balanceOf',
args: [account.address],
});
console.log(`L1 Token Balance: ${formatUnits(l1TokenBalance, 18)} FAUCET`);
// During bridging, your ERC-20 tokens must be transferred to the L1StandardBridge contract.
// This is not a direct transfer by you; it happens internally when L1StandardBridge functions execute.
// First call approve to grant transfer permission to the L1StandardBridge contract.
const approveHash = await walletClientL1.writeContract({
address: l1TokenAddress,
abi: testTokenAbi,
functionName: 'approve',
args: [l1StandardBridgeAddress, parseUnits('1', 18)],
});
console.log(`Approve transaction hash on L1: ${approveHash}`);
// Wait until the L1 transaction above is fully processed.
const approveReceipt = await publicClientL1.waitForTransactionReceipt({ hash: approveHash });
console.log('L1 transaction confirmed:', approveReceipt);
// Send the deposit transaction on L1.
// In this process, your ERC-20 tokens are sent to the L1StandardBridge contract.
const depositHash = await walletClientL1.writeContract({
address: l1StandardBridgeAddress,
abi: l1StandardBridgeAbi,
functionName: 'depositERC20To',
args: [
l1TokenAddress,
l2TokenAddress,
account.address,
parseUnits('1', 18),
200000,
'0x',
],
});
console.log(`Deposit transaction hash on L1: ${depositHash}`);
// Wait until the L1 transaction above is fully processed.
const depositReceipt = await publicClientL1.waitForTransactionReceipt({ hash: depositHash });
console.log('L1 transaction confirmed:', depositReceipt);
// From the L1 receipt, precompute the L2 deposit transaction hash.
const [l2Hash] = getL2TransactionHashes(depositReceipt);
console.log(`Corresponding L2 transaction hash: ${l2Hash}`);
// Wait until the L2 deposit transaction above is processed.
// This takes roughly 1–3 minutes.
const l2Receipt = await publicClientL2.waitForTransactionReceipt({
hash: l2Hash,
});
console.log('L2 transaction confirmed:', l2Receipt);
console.log('Deposit completed successfully!');
}
main().then();
node --import=tsx src/deposit_erc20.ts
src/withdraw_erc20.ts
import {account, publicClientL1, walletClientL1, publicClientL2, walletClientL2} from "./config";
import {l2StandardBridgeAbi, l2StandardBridgeAddress, l2TokenAddress, testTokenAbi} from "./contract";
import {formatUnits, parseUnits} from "viem";
async function main() {
// Before bridging back to Ethereum, check your L2 ERC-20 token balance.
const l2TokenBalance = await publicClientL2.readContract({
address: l2TokenAddress,
abi: testTokenAbi,
functionName: 'balanceOf',
args: [account.address],
});
console.log(`L2 Token Balance: ${formatUnits(l2TokenBalance, 18)} FAUCET`);
// Send the withdrawal transaction on L2.
// In this process, your ERC-20 tokens are burned.
const withdrawalHash = await walletClientL2.writeContract({
address: l2StandardBridgeAddress,
abi: l2StandardBridgeAbi,
functionName: 'withdrawTo',
args: [
l2TokenAddress,
account.address,
parseUnits('0.5', 18),
200000,
'0x',
],
});
console.log(`Withdrawal transaction hash on L2: ${withdrawalHash}`);
// Wait until the L2 transaction above is fully processed.
const withdrawalReceipt = await publicClientL2.waitForTransactionReceipt({ hash: withdrawalHash });
console.log('L2 transaction confirmed:', withdrawalReceipt);
// Wait until the L2 withdrawal can be proven on L1.
// This can take up to 2 hours.
const { output, withdrawal } = await publicClientL1.waitToProve({
receipt: withdrawalReceipt,
targetChain: walletClientL2.chain
});
// Build parameters to send the prove transaction on L1.
const proveArgs = await publicClientL2.buildProveWithdrawal({
output,
withdrawal,
});
// Prove the withdrawal on L1.
const proveHash = await walletClientL1.proveWithdrawal(proveArgs);
console.log(`Prove transaction hash on L1: ${proveHash}`);
// Wait until the L1 transaction above is fully processed.
const proveReceipt = await publicClientL1.waitForTransactionReceipt({ hash: proveHash });
console.log('Prove transaction confirmed:', proveReceipt);
// Wait until the withdrawal can be finalized.
// This period is called the challenge period and takes about 7 days.
await publicClientL1.waitToFinalize({
targetChain: walletClientL2.chain,
withdrawalHash: withdrawal.withdrawalHash,
});
// Finalize the withdrawal on L1.
const finalizeHash = await walletClientL1.finalizeWithdrawal({
targetChain: walletClientL2.chain,
withdrawal,
});
console.log(`Finalize transaction hash on L1: ${finalizeHash}`);
// Wait until the L1 transaction above is fully processed.
const finalizeReceipt = await publicClientL1.waitForTransactionReceipt({
hash: finalizeHash
});
console.log('Finalize transaction confirmed:', finalizeReceipt);
// Read withdrawal status on L1.
// Withdrawals take a long time to complete, so you can query status with this when needed.
const status = await publicClientL1.getWithdrawalStatus({
receipt: withdrawalReceipt,
targetChain: walletClientL2.chain,
});
console.log('Withdrawal completed successfully!');
}
main().then();