Simple examples for bridging assets between Ethereum L1 (Sepolia) and Optimism L2 (Optimism Sepolia) using the OP Stack.
-
Install dependencies
yarn install
-
Set up environment
cp .env.example .env # Edit .env with your private key
-
Run examples
# Deposit native ETH from L1 to L2 yarn native-deposit # Deposit ERC20 tokens from L1 to L2 yarn erc20-deposit # Withdraw native ETH from L2 to L1 yarn native-withdrawal # Withdraw ERC20 tokens from L2 to L1 yarn erc20-withdrawal # Check withdrawal status yarn withdrawal-status
src/
├── config/
│ ├── clients.ts # Viem clients configuration
│ ├── erc20ABI.ts # ERC20 contract ABI
│ └── logger.ts # Logging utility
├── parentToChild/ # L1 → L2 operations
│ ├── nativeDeposit.ts
│ └── erc20Deposit.ts
├── childToParent/ # L2 → L1 operations
│ ├── nativeWithdrawal.ts
│ └── erc20Withdrawal.ts
└── getWithdrawlStatus/
└── index.ts # Check withdrawal status
Create a .env
file in the root directory:
PRIVATE_KEY=your_private_key_here
- L1 & L2 : Configure networks in
config/clients.ts
Script | Description |
---|---|
yarn native-deposit |
Deposit native ETH from L1 to L2 |
yarn erc20-deposit |
Deposit ERC20 tokens from L1 to L2 |
yarn native-withdrawal |
Withdraw native ETH from L2 to L1 |
yarn erc20-withdrawal |
Withdraw ERC20 tokens from L2 to L1 |
yarn withdrawal-status |
Check withdrawal status |
- Native ETH: Single transaction, instant on L2
- ERC20: Two transactions (approve + deposit), instant on L2
- Initiate: Submit withdrawal on L2
- Prove: Prove withdrawal on L1
- Finalize: Finalize withdrawal on L1
@eth-optimism/viem
: Optimism-specific Viem actionsviem
: Ethereum client librarytypescript
: Type safety
Feel free to submit issues and enhancement requests!