iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

L2s & Rollups

Layer 2: rollups (optimistic and ZK), sidechains, and validiums. Why they exist, how they secure transactions, and which to pick.

Web3 — Layer 2

EXAMPLE
# ===== Why L2 =====
# L1s (Ethereum mainnet) are global trust-minimised settlement.
# Throughput is limited (~15-30 TPS for ETH L1) and gas is expensive.
# L2s execute transactions off the L1 then post compressed proofs / data back.
# Result: 10-1000x cheaper + faster for users; L1 still settles disputes.

# ===== The taxonomy =====
# 1. Optimistic rollups
#    Execute off-chain; assume valid; allow a CHALLENGE period (typically 7 days)
#    during which anyone can submit fraud proofs.
#    Examples: Arbitrum One, Optimism (OP Mainnet), Base.
#    Pros: Solidity-compatible (EVM-equivalent), easy to deploy to.
#    Cons: 7-day exit delay for trust-minimised withdrawal; ~7-day finality.
#
# 2. ZK rollups
#    Execute off-chain; cryptographic VALIDITY proof posted to L1.
#    Once the proof verifies, the state is final.
#    Examples: zkSync Era, Starknet, Polygon zkEVM, Scroll, Linea.
#    Pros: faster finality, no challenge window.
#    Cons: prover complexity; EVM-equivalence varies; high gas to verify proofs.
#
# 3. Sidechains
#    Independent chains with their own consensus, connected via bridges.
#    Examples: Polygon PoS (BSC-style chain), Gnosis Chain.
#    Pros: cheap + fast.
#    Cons: NOT inherit L1 security; depend on the sidechain's own validators.
#
# 4. Validiums + Volitions
#    ZK rollups with off-chain data availability (cheaper, weaker assumption).
#    Volitions let users CHOOSE per transaction.

# ===== Reading from an L2 =====
import { createPublicClient, http } from 'viem';
import { arbitrum } from 'viem/chains';

const client = createPublicClient({ chain: arbitrum, transport: http() });
const block = await client.getBlockNumber();
const balance = await client.getBalance({ address: '0x...' });

# ===== Writing to an L2 =====
# Same flow as L1, just point the wallet at the L2 RPC.
# MetaMask + Rabby + most wallets allow adding custom networks (or pick from defaults).

# ===== Bridges =====
# Native bridges (canonical): one-way trust-minimised; slow for optimistic rollups (7 days)
# Third-party bridges (Across, Stargate, Hop): fast + slightly more trust
# Always verify the BRIDGE; many exploits have targeted custom bridges.

# ===== Picking an L2 =====
# - Default: Arbitrum or Optimism for general dApps (mature, EVM-equivalent)
# - Cheaper tx, NFT mints: Base (Coinbase), Polygon zkEVM
# - Privacy + ZK research: zkSync Era, Starknet (Cairo language)
# - Gaming + high throughput: ronin (sidechain) or app-specific L2s
# - Native account abstraction: zkSync, Starknet

# ===== Deploying a contract to L2 =====
# Same Solidity workflow; deploy to the L2 RPC.
# Verify on the L2 block explorer (Arbiscan, Optimism Etherscan, etc).
# Use addresses + storage carefully — gas semantics are slightly different.

# ===== Patterns to internalise =====
# - L2-first for any user-facing dApp in 2026
# - Read with viem / ethers chains presets
# - Verify which bridge users will use; document it
# - Test on a TESTNET equivalent (Sepolia + Optimism Sepolia + Arbitrum Sepolia)

# ===== Pitfalls =====
# - Treating sidechains as L2s (they have different security)
# - Using a custom third-party bridge without auditing
# - Assuming finality on optimistic rollups before the challenge window
# - Hardcoding chain IDs (use viem's chains module)

Why it matters

L2 is the centre of gravity for user-facing dApps in 2026. Optimistic rollups for general EVM-equivalent work, ZK rollups for faster finality, sidechains when their security model fits. Read + write via viem/ethers with the chain preset; pick a canonical bridge; verify on the L2 explorer.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
// L2s settle to Ethereum but execute cheaply off-chain.
// Optimism, Arbitrum (optimistic rollups). Base, Polygon zkEVM (zk rollups).
// Deploy the same Solidity unchanged; only the chain id and RPC differ.
Try it Yourself »

Discussion

Loading…