Design Decentralized Exchange

Hard45 min
1 / 30
understanding8 min read

Problem Statement: Permissionless Spot DEX

Problem Statement: Permissionless Spot DEX — decentralized exchange interview depth

Problem Statement: Permissionless Spot DEX

A decentralized exchange lets anyone swap ERC-20 tokens without a custodian or an order book — liquidity sits in on-chain pools and price is computed by a formula, not matched against resting orders. The canonical design is a Uniswap/Curve-class automated market maker (AMM): traders swap against a pool, the pool's reserves move along a constant-product curve x · y = k (or a stable-swap curve for pegged assets like USDC/USDT), and liquidity providers (LPs) earn the swap fee for supplying the reserves. The contracts are the product; your backend is the read-and-route layer around them.

The scale and economics to anchor: a major DEX holds billions in TVL (Uniswap has run $3–8B+), charges a 0.3% fee on v2-style swaps, and settles every swap atomically on-chain. The defining boundary, exactly as in the other DeFi questions: settlement truth lives on-chain (the pool contract is authoritative), while the indexer/API projections that power quotes and charts may lag a block. You never let an off-chain quote authorize a trade the on-chain swap would revert.

The core invariant that makes the whole thing safe: k never decreases within a swap except for the fee that accrues to LPs — the contract checks reserves after the token transfer and reverts if the product would shrink. That single post-condition is what stops a malformed or malicious swap from draining a pool.

The failure stories that define a DEX (and what an interviewer is really probing) are AMM-specific: a sandwich attack front-running and back-running a large trade with tight slippage, fee-on-transfer or rebasing tokens breaking naive balance accounting, indexer lag showing a stale price-impact preview, and a reorg double-applying a Swap event in your off-chain projection. These, not a generic microservices diagram, are the substance of the role.

Key Highlights

  • Non-custodial swaps with wallet-signed transactions
  • AMM pools instead of centralized order books for MVP
  • Composable with routers, aggregators, and LP tokens
  • Ethereum mainnet + L2 deployment assumptions
Mention this
Tie Problem Statement: Permissionless Spot DEX to **x·y=k**, **minAmountOut**, and **indexer block lag**—signals production DEX literacy.
Pro tip
Simulate router calldata with eth_call at `latest` before the user signs—never show APY-style numbers from a stale Postgres row.

Section Rescue Kit

Buzzwords to use:

Constant productImpermanent loss

Safe statements:

  • "For Problem Statement: Permissionless Spot DEX, I'll state reserve invariants before naming cloud SKUs."
  • "I'll walk approve → router swap → indexer event when stuck."
Design Decentralized Exchange - System Design | WinJob | WinJob