在 L2 上开发
在 L2 上开发
本页内容正在整理中,欢迎贡献。
概述
Layer 2 是建立在以太坊主网之上的扩展方案,通过将交易处理移至链下并将证明提交主网,实现更低 Gas 费和更高吞吐量。对开发者而言,主流 L2 完全兼容 EVM,几乎无需修改代码即可迁移。
主要内容
主流 L2 网络对比
| 网络 | 类型 | 链 ID(主网) | 技术栈 | 特色 |
|---|---|---|---|---|
| Optimism | Optimistic Rollup | 10 | OP Stack | 超级链生态、治理代币 OP |
| Base | Optimistic Rollup | 8453 | OP Stack | Coinbase 运营,低费用 |
| Arbitrum One | Optimistic Rollup | 42161 | Nitro | 最大 L2 TVL |
| Arbitrum Nova | AnyTrust | 42170 | Nitro | 超低费用,游戏/社交 |
| zkSync Era | ZK Rollup | 324 | ZK Stack | 原生账户抽象 |
| Starknet | ZK Rollup | — | Cairo VM | 原生 ZK,高性能 |
| Polygon zkEVM | ZK Rollup | 1101 | CDK | EVM 等效 |
| Scroll | ZK Rollup | 534352 | — | 与以太坊高度兼容 |
| Linea | ZK Rollup | 59144 | ConsenSys | 深度集成 MetaMask |
网络配置
// wagmi 链配置import { optimism, optimismSepolia, arbitrum, arbitrumSepolia, base, baseSepolia, zkSync, zkSyncSepoliaTestnet,} from "wagmi/chains";
// Hardhat 网络配置const networks = { optimism: { url: "https://mainnet.optimism.io", accounts: [process.env.PRIVATE_KEY!], }, arbitrum: { url: "https://arb1.arbitrum.io/rpc", accounts: [process.env.PRIVATE_KEY!], }, base: { url: "https://mainnet.base.org", accounts: [process.env.PRIVATE_KEY!], },};EVM 兼容性差异
大多数合约可以直接部署,但需注意以下差异:
| 特性 | 以太坊 L1 | Optimism/Base/Arbitrum | zkSync Era |
|---|---|---|---|
block.number | L1 区块号 | L2 区块号 | L2 区块号 |
block.timestamp | L1 时间戳 | L2 时间戳 | L2 时间戳 |
tx.origin | 支持 | 支持 | 支持 |
PUSH0 操作码 | ✅ | ✅(较新版本) | ⚠️ 部分支持 |
SELFDESTRUCT | 已废弃 | 已废弃 | 已废弃 |
| Gas 价格 | EIP-1559 | L2 Gas + L1 数据费 | 自定义计费 |
OP Stack 特有功能
Optimism 和 Base 使用相同的 OP Stack,提供额外的系统合约:
// 读取 L1 区块信息interface IL1Block { function number() external view returns (uint64); function timestamp() external view returns (uint64); function basefee() external view returns (uint256);}
address constant L1_BLOCK = 0x4200000000000000000000000000000000000015;
uint64 l1BlockNumber = IL1Block(L1_BLOCK).number();Arbitrum 特有预编译
// ArbSys - Arbitrum 系统接口interface IArbSys { function arbBlockNumber() external view returns (uint256); function withdrawEth(address destination) external payable returns (uint256);}
address constant ARB_SYS = address(100);uint256 arbBlock = IArbSys(ARB_SYS).arbBlockNumber();跨链消息传递
OP Stack 原生桥(Optimism/Base):
// L2 → L1 消息interface IL2CrossDomainMessenger { function sendMessage( address _target, bytes calldata _message, uint32 _minGasLimit ) external payable;}Arbitrum Inbox:
// L1 → L2 消息IInbox(ARBITRUM_INBOX).createRetryableTicket{value: callValue}( to, l2CallValue, maxSubmissionCost, excessFeeRefundAddress, callValueRefundAddress, gasLimit, maxFeePerGas, data);测试网信息
| 测试网 | 链 ID | 水龙头 |
|---|---|---|
| Optimism Sepolia | 11155420 | faucet.quicknode.com |
| Base Sepolia | 84532 | faucet.quicknode.com |
| Arbitrum Sepolia | 421614 | faucet.quicknode.com |
| zkSync Sepolia | 300 | faucet.quicknode.com |
Gas 成本对比(参考值)
| 操作 | 以太坊 L1 | Optimism | Arbitrum | Base |
|---|---|---|---|---|
| ETH 转账 | ~$0.5-5 | ~$0.001-0.01 | ~$0.001-0.01 | ~$0.001-0.01 |
| ERC-20 转账 | ~$2-20 | ~$0.005-0.05 | ~$0.005-0.05 | ~$0.005-0.05 |
| Uniswap Swap | ~$10-100 | ~$0.05-0.5 | ~$0.05-0.5 | ~$0.05-0.5 |
实际费用随网络拥堵变化。
深入阅读
- L2 数据参考 —— L2BEAT TVL 与安全性数据
- Optimism 开发者文档
- Arbitrum 开发者文档
- Base 开发者文档
- zkSync 开发者文档