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();