Decentralized Exchanges and AMMs

Introduction

Main purpose in this notebook:

  • Explain constant-product AMM pricing.
  • Quantify slippage, arbitrage alignment, and LP tradeoffs.
  • Connect AMM mechanics to standard market-microstructure logic.

Market Structure and Motivation

Core contrast:

  • CEXs mainly use order books (CLOB-style matching).
  • AMMs execute against pooled reserves and formula-based quotes.

In AMMs, the pool is the immediate counterparty, and price updates through reserve changes (Adams et al. 2020).

Adams, Hayden, Noah Zinsmeister, Moody Salem, River Keefer, and Dan Robinson. 2020. Uniswap V2 Core. Uniswap Whitepaper. https://docs.uniswap.org/whitepaper.pdf.

Constant-Product Pricing

Baseline AMM invariant: xy=k. Marginal quote (in y per unit of x): P=\frac{y}{x}.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x0 = 1_000.0
y0 = 1_000.0
k = x0 * y0

def cp_swap_buy_x(delta_y, x=x0, y=y0):
    y_new = y + delta_y
    x_new = k / y_new
    dx_out = x - x_new
    avg_price = delta_y / dx_out
    return {
        "delta_y_in": delta_y,
        "x_out": dx_out,
        "avg_price": avg_price,
        "marginal_before": y / x,
        "marginal_after": y_new / x_new,
    }

trades = pd.DataFrame([cp_swap_buy_x(dy) for dy in [10, 50, 100, 200, 400]])
trades.round(4)
delta_y_in x_out avg_price marginal_before marginal_after
0 10 9.9010 1.01 1.0 1.0201
1 50 47.6190 1.05 1.0 1.1025
2 100 90.9091 1.10 1.0 1.2100
3 200 166.6667 1.20 1.0 1.4400
4 400 285.7143 1.40 1.0 1.9600

Key interpretation:

  • Larger trades move price more (endogenous impact), analogous to walking the book.

Slippage and Trade Size

Slippage metric used in the notebook: \text{slippage}=\frac{P_{\text{avg}}}{P_{\text{pre}}}-1.

Key result:

  • Slippage rises nonlinearly with trade size relative to pool depth.

Arbitrage and Price Alignment

When external price is P^*, no-fee aligned reserves satisfy: \frac{y}{x}=P^*,\quad xy=k, so x=\sqrt{\frac{k}{P^*}},\qquad y=\sqrt{kP^*}.

p_external x_reserve y_reserve pool_price
0 0.8 1118.0340 894.4272 0.8
1 1.0 1000.0000 1000.0000 1.0
2 1.2 912.8709 1095.4451 1.2
3 1.5 816.4966 1224.7449 1.5

Key interpretation:

  • AMM prices adjust via arbitrage flow; reserves transmit external information into on-chain prices (Capponi et al. 2026).
Capponi, Agostino, Ruizhe Jia, and Shuo Yu. 2026. “Price Discovery on Decentralized Exchanges.” Review of Financial Studies, ahead of print. https://doi.org/10.1093/rfs/hhag002.

LP Fees vs Impermanent Loss

Impermanent loss (IL) benchmark in a 50/50 pool: \mathrm{IL}(r)=\frac{2\sqrt r}{1+r}-1, \qquad r=\frac{P_1}{P_0}.

Fee income benchmark: \text{LP fee income} \approx s\,f\,V, where s is LP share, f fee rate, V traded notional.

Key interpretation:

  • LP outcomes depend on fee income versus volatility-driven inventory drift (IL) (Capponi et al. 2025).
Capponi, Agostino, Ruizhe Jia, Yubo Ma, John Wang, and Boyu Zhu. 2025. “Liquidity Provision on Blockchain-Based Decentralized Exchanges.” Review of Financial Studies 38 (10): 3040–85. https://doi.org/10.1093/rfs/hhaf046.

How Uniswap v3 and v4 Differ from This Notebook

Notebook baseline is v2-style full-range liquidity.

  • v3 adds concentrated liquidity (range choice, path-dependent fee earning).
  • v4 keeps concentration and adds programmable hooks/singleton architecture (Uniswap Labs 2025; Adams et al. 2024).
Uniswap Labs. 2025. Uniswap V4 Is Here. https://blog.uniswap.org/uniswap-v4-is-here.
Adams, Hayden, Noah Zinsmeister, Moody Salem, et al. 2024. Uniswap V4 Core. Uniswap v4 Whitepaper. https://app.uniswap.org/whitepaper-v4.pdf.

Takeaways

  • AMMs provide transparent, rule-based market making.
  • Three central objects for LP outcomes: depth, volume, volatility.
  • Arbitrage aligns prices, but LPs still bear inventory and implementation risk.
  • The notebook is a microstructure mechanics exercise, not a full production risk model.