Learning Objectives:
-
Master how DEXs work and their key mechanisms
-
Understand AMM mathematics and formulas
-
Learn about different AMM models (Uniswap, Curve, Balancer)
-
Analyze DEX risks and optimization strategies
5.2.1: What are DEXs?
Definition and Purpose
Decentralized Exchanges (DEXs) are peer-to-peer marketplaces where users can trade cryptocurrencies directly with each other without intermediaries. Unlike centralized exchanges, DEXs are non-custodial and operate through smart contracts.
Order Book DEX vs AMM: Order Book DEX: ┌─────────────────────────────────────────────────────────────────────┐ │ Buy Orders Sell Orders │ │ ┌─────────────┐ ┌─────────────┐ │ │ │ Price $100 │ │ Price $105 │ │ │ │ 100 tokens │ │ 200 tokens │ │ │ └─────────────┘ └─────────────┘ │ │ ┌─────────────┐ ┌─────────────┐ │ │ │ Price $99 │ │ Price $106 │ │ │ │ 150 tokens │ │ 100 tokens │ │ │ └─────────────┘ └─────────────┘ │ │ │ │ Problems: │ │ • Low liquidity for low-volume tokens │ │ • Order book must be filled │ │ • Slippage issues │ └─────────────────────────────────────────────────────────────────────┘ AMM (Automated Market Maker): ┌─────────────────────────────────────────────────────────────────────┐ │ Liquidity Pool: │ │ ┌─────────────────────────────────────────────────────────────┐ │ │ │ Token A: 100 ETH │ │ │ │ Token B: 200,000 USDC │ │ │ │ Price determined by formula: x × y = k │ │ │ └─────────────────────────────────────────────────────────────┘ │ │ │ │ Benefits: │ │ • Always available liquidity │ │ • No order book required │ │ • Anyone can provide liquidity │ │ • Price discovery through math │ └─────────────────────────────────────────────────────────────────────┘
5.2.2: AMM Mathematics – The Constant Product Formula
The Core AMM Formula (Uniswap):
The constant product formula is the foundation of most AMMs:
x × y = k Where: - x = Reserve of token A - y = Reserve of token B - k = Constant product (invariant) Price Calculation: Price_A_in_B = y / x Price_B_in_A = x / y Swap Calculation: Given: Swap dx amount of token A for token B Δy = (y × dx) / (x + dx) After Swap: x' = x + dx y' = y - Δy k' = x' × y' = k (remains constant) Example: Pool: 100 ETH and 200,000 USDC k = 100 × 200,000 = 20,000,000 Price: 1 ETH = 2,000 USDC Swap 1 ETH for USDC: dx = 1 ETH Δy = (200,000 × 1) / (100 + 1) = 1,980.2 USDC After Swap: x' = 101 ETH y' = 198,019.8 USDC k' = 101 × 198,019.8 = 20,000,000 New Price: 1 ETH = 1,960.6 USDC
Slippage Calculation:
Slippage = (Expected_Price - Actual_Price) / Expected_Price × 100% Example (Large Swap): Expected Price: 2,000 USDC/ETH Actual Price: 1,960.6 USDC/ETH Slippage = (2,000 - 1,960.6) / 2,000 × 100 = 1.97% Slippage Factors: - Trade size relative to pool size - Pool depth (liquidity) - Trading volume Mitigation: - Split large trades - Use DEX aggregators - Set slippage tolerance
5.2.3: The Constant Sum Formula (Curve)
Curve’s Stable Swap Formula:
Curve is optimized for stablecoins (USDC, USDT, DAI) where prices are expected to remain stable.
Constant Sum Formula: x + y = k Benefits: - Minimal slippage for stable assets - Efficient trading - Low fees Problems: - No price discovery - Can be manipulated Solution: Hybrid Formula Curve uses a combination of constant product and constant sum: x + y + (x × y / (x + y)²) × k = k This provides: - Low slippage near equilibrium - Price discovery when imbalanced - Protection against manipulation
5.2.4: Balancer’s Weighted Pools
Balancer’s Formula:
Balancer extends the AMM concept to multiple tokens with custom weights.
Balancer Formula: Σ (x_i^w_i) = k Where: - x_i = Reserve of token i - w_i = Weight of token i - k = Invariant Example (80/20 Pool): - Token A: 80% weight - Token B: 20% weight Exit fee: 0.1% - 0.5% Trading fee: 0.1% - 1% Benefits: - Custom portfolio weights - Single-sided liquidity - Flexible fee structures - Multi-token pools
5.2.5: Liquidity Provision and LP Tokens
Providing Liquidity:
Liquidity Provision Process: 1. Deposit: - User deposits equal value of both tokens - Example: 1 ETH + 2,000 USDC 2. LP Tokens Minted: - Represent share of pool - Redeemable for underlying tokens - Earn trading fees 3. Share Calculation: Shares = (Deposited_Value / Total_Pool_Value) × Total_Shares Example: Pool: 100 ETH + 200,000 USDC (Total Value: $400,000) Total Shares: 100,000 Deposit: 1 ETH + 2,000 USDC ($4,000) Shares = (4,000 / 400,000) × 100,000 = 1,000 shares
5.2.6: Impermanent Loss
Understanding Impermanent Loss:
Impermanent loss is the temporary loss experienced by liquidity providers when the price ratio of deposited tokens changes.
Impermanent Loss Example: Initial Deposit: - 1 ETH + 2,000 USDC - ETH Price: $2,000 - Pool Share: 1% After Price Change: - ETH Price: $4,000 (doubles) - Pool arbitrageurs rebalance - Pool now has: 0.707 ETH + 2,828 USDC IF liquidated now: - Value of LP position: 0.707 × 4,000 + 2,828 = $5,656 - Value of HODL: 1 × 4,000 + 2,000 = $6,000 - Impermanent Loss: $344 (5.7%) Why "Impermanent": - Loss only realized when withdrawing - If prices return, loss disappears - Can be offset by trading fees IL by Price Change: ┌─────────────────────────────────────────────────────────────────────┐ │ Price Change | Impermanent Loss │ │ 1.25x | 0.6% │ │ 1.5x | 2.0% │ │ 2x | 5.7% │ │ 3x | 13.4% │ │ 5x | 25.5% │ │ 10x | 42.5% │ └─────────────────────────────────────────────────────────────────────┘
5.2.7: DEX Aggregators
How Aggregators Work:
DEX aggregators find the best prices across multiple DEXs.
Aggregation Process:
1. Split Order:
┌─────────────────────────────────────────────────────────────┐
│ User wants to swap 100 ETH │ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Query Multiple DEXs: │ │
│ • Uniswap: 200,000 USDC │ │
│ • Curve: 201,000 USDC │ │
│ • Balancer: 199,000 USDC │ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Optimal Routing: │ │
│ • 30 ETH → Balancer: 59,700 USDC │ │
│ • 70 ETH → Curve: 140,700 USDC │ │
│ • Total: 200,400 USDC │ │
└─────────────────────────────────────────────────────────────┘
Popular Aggregators:
| Aggregator | Features | Supported DEXs |
|---|---|---|
| 1inch | Best price, limit orders | 50+ DEXs |
| ParaSwap | Privacy, low slippage | 30+ DEXs |
| CowSwap | Intent-based, MEV protection | 20+ DEXs |
| 0x API | Open-source, customizable | 30+ DEXs |
5.2.8: DEX Security and Risks
Common DEX Risks:
| Risk | Description | Mitigation |
|---|---|---|
| Impermanent Loss | Temporary loss from price divergence | Choose stable pairs, monitor |
| Smart Contract Risk | Bugs or exploits | Audits, use established DEXs |
| Front-Running | MEV exploitation | Use slippage protection |
| Rug Pulls | Malicious liquidity removal | Verify contracts, use audited DEXs |
| Slippage | Price change during trade | Set slippage limits |
| Oracle Manipulation | Price manipulation attacks | Use multiple oracles |