SolidityIn

Deploying to Layer 2s and bridges

Lesson 34 of 40 · Layer 2

To use an L2, add its network to your wallet with its chain ID and RPC URL, get a little ETH on it, and deploy as you would on Ethereum. Most tools take a network name or an RPC URL, so a Foundry script like the one in the Practice module works on an L2 by changing the RPC URL.

Funds move between layers through bridges. Depositing from Ethereum to an L2 is usually fast. Withdrawing from an optimistic rollup through its official bridge takes about a week because of the challenge period, so many users choose faster third-party bridges that charge a fee. Bridges hold large amounts of money and have been the target of some of the biggest hacks, so prefer official or well-audited ones.

L2s have a sequencer, the server that orders transactions. If it goes down, users may be unable to transact, and prices from oracles can be stale when it comes back. Chainlink offers a sequencer uptime feed. Good contracts check it and wait out a short grace period before trusting prices, as in the example.

Check what the chain you target supports. Some L2s treat certain opcodes or precompiles differently, and the cheapest L2 is not always the safest. Read the chain's documentation, test on its testnet and compare the security model before you choose.

interface IFeed {
    function latestRoundData() external view returns (uint80, int256, uint256, uint256, uint80);
}

contract SequencerGuard {
    IFeed public immutable uptimeFeed;
    uint256 public constant GRACE_PERIOD = 1 hours;

    constructor(address feed) { uptimeFeed = IFeed(feed); }

    // Reverts unless the L2 sequencer has been up for longer than the grace period
    function requireSequencerUp() public view {
        (, int256 answer, uint256 startedAt, , ) = uptimeFeed.latestRoundData();
        require(answer == 0, "Sequencer is down");          // 0 = up, 1 = down
        require(block.timestamp - startedAt > GRACE_PERIOD, "Grace period not over");
    }
    function safeAction() external view returns (bool) {
        requireSequencerUp();
        return true;
    }
}

Try it yourself

Add a modifier whenSequencerUp that calls requireSequencerUp, and use it on a new function. Then look up the sequencer uptime feed address for the L2 you want to use in Chainlink's documentation.

Press Compile to check your code in the browser. Short fragments are wrapped in a contract automatically.

Quick check: Why check a sequencer uptime feed before trusting prices?