Book a free call

How to Build a Crypto Derivatives Exchange on Layer 2

A derivatives exchange on layer 2 cuts fees and speeds up trading by settling off the main chain. Learn the design choices, the parts and the risks.

Hertiv engineering teamReviewed by a senior engineer
6 min read
How to Build a Crypto Derivatives Exchange on Layer 2
On this page

Short answer

A crypto derivatives exchange on layer 2 is a trading venue where orders, margin and liquidations run on a rollup or sidechain while the main chain holds the final settlement. It keeps the security of the base chain, cuts trading costs and shortens confirmation times, which matters for products that use borrowed funds and settle often.

What is a derivatives exchange on layer 2?

A derivatives exchange on layer 2 is a trading venue for futures and perpetual contracts where the fast, frequent work happens off the main chain and only the final results settle there. Traders open positions, post margin and get liquidated on the layer 2 network, while deposits and withdrawals move through a bridge to the base chain.

Derivatives are a good fit for layer 2 because they trade often. A position opened with borrowed funds can change value every second, and each update on the main chain costs a fee and waits for a block. On a rollup, the same update costs a fraction of that and confirms in seconds, so traders can use tighter stops and smaller positions.

Layer 2 does not remove risk. The exchange still needs margin rules, liquidation logic and price feeds, and the bridge between the layers is itself a security risk. What layer 2 changes is cost and speed, not the need for sound risk controls.

Why build derivatives on layer 2?

Derivatives trading is expensive on a main chain because every order, margin top-up and liquidation is a transaction. Layer 2 moves that activity off-chain and posts a compressed summary back, so the cost per trade falls sharply while the base chain still guarantees the final state.

FactorMain chainLayer 2
Trade feeHigh, varies with congestionLow, mostly flat
ConfirmationSeconds to minutesAbout a second
ThroughputTens of trades per secondHundreds to thousands
SettlementDirect on chainVia bridge and rollup
ComplexityLowerHigher, bridge risk

Speed matters for liquidations. When a price moves fast, a liquidation that waits for a main-chain block can execute far from the intended price, leaving bad debt. A layer 2 network confirms faster, so liquidations happen closer to the trigger price and the insurance fund takes smaller losses.

Which layer 2 design fits derivatives?

Not every layer 2 design suits derivatives. The choice depends on how fast finality must be, how much data the exchange produces, and how much trust the team is willing to place in an operator.

DesignFinalityBest for
Optimistic rollupMinutes to daysHigh volume, lower cost
ZK rollupMinutesFast finality, strong proofs
ValidiumMinutesVery high throughput
App chainSecondsFull control, custom rules

Optimistic rollups are the most proven choice and the cheapest to build on, but withdrawals wait for the fraud-proof window, which can take days. ZK rollups settle faster with validity proofs, yet they are harder to build and audit. A validium keeps data off-chain, which cuts costs further but adds a data-availability trust assumption.

An app chain gives the team full control over block times, fees and upgrades, at the cost of running its own validator set and security. Most teams start on a general-purpose rollup and only move to an app chain when volume justifies the extra work.

What parts does the exchange need?

A derivatives exchange on layer 2 needs the same core parts as any derivatives venue, plus the layer 2 plumbing that connects it to the base chain. Each part below must work on the rollup, not just on the main chain.

  • Matching engine. Pairs buy and sell orders, or runs an automated market maker for perpetual contracts. It must keep up with rollup block times.
  • Margin and risk engine. Tracks each position's margin, maintenance level and liquidation price, and triggers liquidations automatically.
  • Price oracles. Feed the mark price that liquidations use. A stale or manipulated feed is the fastest way to lose user funds.
  • Settlement and custody. Holds collateral on the rollup and settles withdrawals through the bridge back to the main chain.
  • Bridge and deposits. Moves funds between the base chain and the rollup, with clear wait times and limits.
  • Insurance fund. Covers losses when a liquidation cannot close a position at a solvent price.

The oracle deserves special care. Derivatives liquidations depend on the mark price, and an oracle that lags during a fast move will liquidate healthy positions or miss insolvent ones. Use more than one feed and a rule that pauses liquidations when feeds disagree.

How to build it in 6 steps

Build the exchange in order, testing each layer on a testnet before real funds move. The bridge and the risk engine need the most testing time.

  1. Choose the rollup. Pick the design that fits your finality and cost needs, and check that its tooling, wallets and bridges are mature.
  2. Design the margin model. Decide isolated versus cross margin, the maximum margin multiple, the maintenance margin and the liquidation rules before writing code.
  3. Build the core contracts. Write the vault, the position manager and the liquidation logic on the rollup, with tests for every edge case.
  4. Connect oracles and the bridge. Wire price feeds and the deposit and withdrawal bridge, and test withdrawals under load.
  5. Build the trading interface. Add order entry, position views, margin warnings and a clear liquidation price for every open position.
  6. Audit, then open with limits. Get an external audit of the contracts and the bridge integration, fix the findings, and launch with low margin-multiple caps and a small insurance fund.

How do margin, liquidations and funding rates work on layer 2?

Margin, liquidations and funding rates work the same way on layer 2 as on any derivatives exchange. What changes is how fast they can run and how cheap each step is.

Margin is the collateral a trader posts to open a position with borrowed funds. The exchange tracks a maintenance margin level, and when a position's margin falls below it, the liquidation engine closes the position at the mark price. On layer 2 the engine can check positions every block, so liquidations trigger closer to the intended price.

Funding rates keep perpetual contract prices near the spot price. When the contract trades above spot, long positions pay short positions, and the reverse when it trades below. The rate updates on a schedule, often hourly, and the payment moves between traders rather than to the exchange.

Watch the bridge during fast markets. If withdrawals queue up while prices fall, users cannot exit, and the exchange looks insolvent even when it is not. Publish bridge wait times and withdrawal limits clearly, and keep an insurance fund sized for the margin multiple you allow.

Example (hypothetical): a first month of layer 2 derivatives trading

Example (hypothetical): a team launches a perpetual futures exchange on a ZK rollup with a 5x maximum margin multiple and an insurance fund of $50,000. It opens to 200 traders in the first month.

Traders open 4,000 positions in the month. The average trade costs about $0.05 in rollup fees, so total trading fees are about $200, against several thousand dollars the same activity would have cost on the main chain. Two hundred positions are liquidated, and the insurance fund pays $3,000 where liquidations closed below the maintenance level.

The team learns that oracle lag during one fast move caused 12 liquidations to trigger late, so it adds a second price feed and a rule that pauses liquidations when the two feeds differ by more than a set amount. It also raises the insurance fund to $80,000 before raising the margin-multiple cap.

What are the common mistakes?

The most common layer 2 derivatives mistakes are trusting a single oracle, ignoring bridge risk, allowing position sizes the insurance fund cannot cover, and launching without liquidation tests.

  • One price feed. A single oracle is a single point of failure. Use at least two feeds and a disagreement rule.
  • Ignoring the bridge. The bridge holds the funds between layers. Audit it, cap deposits at first, and publish withdrawal wait times.
  • Borrowing beyond the fund. High margin multiples mean bigger losses when liquidations slip. Size the insurance fund for the margin multiple you actually allow.
  • No liquidation drills. Test liquidations under fast price moves on a testnet, including the case where many positions liquidate at once.
  • Hidden funding costs. Show the funding rate and the next payment time on every open position, so traders are never surprised.

Key takeaways

  • Layer 2 moves frequent derivatives activity off the main chain while final settlement stays there
  • Faster confirmation means liquidations trigger closer to the intended price
  • Optimistic rollups are cheapest, ZK rollups settle faster, app chains give full control
  • Margin, oracles, the bridge and the insurance fund are the parts that decide safety
  • Launch with low margin-multiple caps and grow the insurance fund before raising them

Frequently asked questions

What is a layer 2 derivatives exchange?

It is a trading venue for futures and perpetual contracts where orders, margin and liquidations run on a rollup or sidechain, while deposits and final settlement use the main chain. The design cuts trading costs and speeds up confirmations.

Why are derivatives a good fit for layer 2?

Derivatives trade often and settle constantly, so main-chain fees and block times become expensive. A rollup confirms trades in about a second at a fraction of the cost, which suits products that use borrowed funds and update every second.

What is the difference between optimistic and ZK rollups?

Optimistic rollups assume transactions are valid and allow challenges during a fraud-proof window, so withdrawals can take days. ZK rollups use validity proofs, so they settle faster, but they are harder to build and audit.

What is the biggest risk of a layer 2 derivatives exchange?

The bridge between the layers and the price oracles are the main risks. A bridge flaw can strand funds, and a stale oracle can liquidate healthy positions. Both need audits, limits and more than one feed.

How do funding rates work on perpetual contracts?

Funding rates keep the perpetual price near the spot price. When the contract trades above spot, long positions pay short positions, and the reverse when it trades below. The payment moves between traders, not to the exchange.

How high should the margin multiple be for a new exchange?

Start low, often 5x or less, and raise the cap only after the insurance fund and liquidation tests prove the system handles fast moves. High margin multiples mean bigger losses when liquidations slip.

Found this useful? Share it.
Hertiv engineering team

Written from projects we build for clients: blockchain systems in production. Every post is reviewed by a senior engineer before publishing and updated when practice changes.

About HertivHow we work

Building a derivatives exchange on layer 2? Talk to Hertiv.

A senior engineer replies within one business day. Or see our DeFi Development service.

Book a free call

Keep reading

All posts

Tell us what you want to build

A senior engineer reads every request and replies within one business day with questions, a rough range, or a call link.

  • NDA on request before any details
  • You own all code, models and IP
  • US, UK and EU time-zone overlap

Your email app should open with your message ready to send. If it does not, write to contact@hertiv.com.

Prefer email? Write to contact@hertiv.com
Estimate costBook a call