Optimism Standard Bridge Contract Walkthrough

ยท

Optimism is an Optimistic Rollup solution that processes transactions at a lower cost than Ethereum Mainnet (Layer 1/L1) while maintaining security by writing all data to L1. To use L1 assets on Optimism (or any L2), assets must be bridged. This involves locking assets on L1 and receiving equivalent assets on L2, which can later be bridged back.

How the Optimism Standard Bridge Works

The Optimism standard bridge facilitates two main flows:

  1. Deposit Flow (L1 โ†’ L2)
  2. Withdrawal Flow (L2 โ†’ L1)

Deposit Flow

Layer 1 Process

  1. For ERC-20 deposits, the depositor grants the bridge an allowance
  2. The depositor calls the L1 bridge (depositERC20, depositERC20To, depositETH, or depositETHTo)
  3. The L1 bridge takes possession of the assets:

    • ETH: Transferred as part of the call
    • ERC-20: Transferred via the allowance
  4. The L1 bridge uses cross-domain messaging to call finalizeDeposit on the L2 bridge

Layer 2 Process

  1. The L2 bridge verifies the call's legitimacy
  2. Checks if the L2 ERC-20 contract is correct:

    • Matches the L1 counterpart
    • Supports the correct interface via ERC-165
  3. If valid, mints tokens to the specified address; otherwise initiates a withdrawal process

Withdrawal Flow

Layer 2 Process

  1. The withdrawer calls the L2 bridge (withdraw or withdrawTo)
  2. The L2 bridge burns the tokens
  3. Uses cross-domain messaging to call finalizeETHWithdrawal or finalizeERC20Withdrawal on L1

Layer 1 Process

  1. The L1 bridge verifies the call's legitimacy
  2. Transfers the asset (ETH or ERC-20) to the specified address

Layer 1 Contract Implementation

Key Components

  1. IL1ERC20Bridge Interface: Handles ERC-20 token bridging
  2. IL1StandardBridge Interface: Extends IL1ERC20Bridge for ETH handling
  3. CrossDomainEnabled: Facilitates cross-layer messaging

The L1 bridge contract:

Layer 2 Contract Implementation

Key Components

  1. IL2StandardERC20 Interface: Standard interface for L2 ERC-20 tokens
  2. L2StandardERC20 Contract: Implementation that:

    • Only allows the L2 bridge to mint/burn tokens
    • Tracks the corresponding L1 token address
    • Implements ERC-165 for interface support verification

The L2 bridge contract:

FAQ

What is the difference between deposits and withdrawals in Optimism's bridge?

How long do withdrawals take?

Withdrawals require a challenge period (typically 7 days) before they can be finalized on L1.

Can any ERC-20 token be bridged?

Yes, but the L2 token must implement the IL2StandardERC20 interface and properly track its L1 counterpart.

Is ETH handled differently from ERC-20 tokens?

On L1, yes - ETH uses separate functions. On L2, ETH is handled as an ERC-20 token with a special address.

What security measures are in place?

The bridge implements multiple security checks:

๐Ÿ‘‰ Learn more about Optimism bridging

Conclusion

The Optimism standard bridge provides a secure, flexible mechanism for moving assets between L1 and L2. While it offers maximum flexibility, users often prefer third-party bridges for faster withdrawals. Understanding the bridge's implementation helps developers create secure cross-chain applications and contributes to Optimism's growing ecosystem.