MathFin
Map / Valuation · Static replication and model-free bounds / MathFin.Markets.DeFi.ConstantProductAMM

Constant-product AMM (after Pusceddu-Bartoletti 2024)

MathFin.Markets.DeFi.ConstantProductAMMMSC 91G80JEL G23 source

A constant-product Automated Market Maker (CPMM, Uniswap v2-style) holds reserves (x, y) of two tokens. The constant-product invariant x · y = k is preserved across swap transactions: a trader who deposits Δx of token X receives

Δy = y · Δx / (x + Δx)

of token Y, after which the new reserves (x + Δx, y − Δy) again satisfy (x + Δx)·(y − Δy) = x · y. This algorithmically-determined exchange rate is what makes CPMMs "automated" — no order book, no oracle.

This file formalises:

  • swapOutput: the closed-form Δy as a function of input Δx and reserves.
  • swap_preserves_invariant: (x + Δx)·(y − Δy) = x · y, the pricing rule: the swap is the trade that keeps the product fixed.
  • swap_round_trip: swapping Δx in and the proceeds straight back returns exactly Δx. This is the no-money-printing property, proved for that round trip: it neither gains nor loses.
  • marginal_exchange_rate: the average rate Δy / Δx = y / (x + Δx); hasDerivAt_swapOutput, hasDerivAt_swapOutput_zero: the marginal rate at size Δx is x y / (x + Δx)², and at zero size it is the internal price y / x.
  • internalPrice / arbitragePresent: the internal price y / x and the predicate for it diverging from an external oracle price P.
  • exists_profitable_swap_iff, exists_profitable_reverse_swap_iff, arbitragePresent_iff: the arbitrage meaning of arbitragePresent, derived. Some single swap against the pool earns a riskless profit, valued at P, if and only if y / x ≠ P: selling X to the pool pays exactly when P < y / x, buying X from it exactly when y / x < P. So a pool whose internal price equals the oracle price admits no profitable swap of any size, the fee-free case of the no-arbitrage band of Angeris et al. (2021).

Why this is quant finance

Pre-existing Markets/Execution/AlmgrenChriss.lean covers traditional execution cost / market impact. CPMM swap output Δy = y·Δx/(x+Δx) plays the same role for an automated market maker: the execution rate y/(x+Δx) depends on the trade size (an analogy only; no formal link to the Almgren-Chriss model is made here).

Main definitions

  • swapOutput: the output y * Δx / (x + Δx) of token Y for an input Δx of token X at reserves (x, y).
  • internalPrice: the reserve ratio y / x.
  • arbitragePresent: the pool's internal price differs from an oracle price.

Main results

  • swap_preserves_invariant: constant-product invariance under swap.
  • swap_output_pos: Δy > 0 when input is positive.
  • swap_output_lt_y: Δy < y (cannot drain pool).
  • swap_round_trip: a round trip through the pool returns exactly the input.
  • marginal_exchange_rate: the average rate Δy / Δx = y / (x + Δx).
  • hasDerivAt_swapOutput, hasDerivAt_swapOutput_zero: the marginal rate, and the internal price as the marginal rate at zero size.
  • exists_profitable_swap_iff, exists_profitable_reverse_swap_iff, arbitragePresent_iff: price divergence is equivalent to the existence of a profitable swap at oracle prices.

References

  • G. Angeris, H.-T. Kao, R. Chiang, C. Noyes, T. Chitra, An analysis of Uniswap markets [AngerisKaoChiangNoyesChitra2021], §2.1 (optimal arbitrage; the no-arbitrage bound (3), which at zero fee says the pool price equals the reference price).

Tags

automated market maker, Uniswap v2, constant product invariant, swap, arbitrage, decentralized exchange, marginal price, MSC 91G80, JEL G23

Import neighbourhood

Above: what it imports. Below: what imports it.

Corpus entries

mf-cpmm-arbitrage-iff-price-divergence · defi.json · full statement
Constant-Product Arbitrage Exists Iff the Pool Price Diverges

For a fee-free constant-product pool with reserves x, y > 0 and an external price P > 0, the pool's internal price y/x differs from P if and only if some single swap against the pool, valued at P, earns a riskless profit, by selling X to the pool or buying X from it (which direction pays, selling when P < y/x and buying when y/x < P, is exists_profitable_swap_iff / exists_profitable_reverse_swap_iff, not this statement). Equivalently, a pool at the external price admits no profitable swap of any size. Fees are not modelled.

Declarations 13

defMathFin.DeFi.swapOutputsource
ℝ → ℝ → ℝ → ℝ

Constant-product AMM swap output: given input Δx of token X and reserves (x, y) with x, y > 0, the output Δy of token Y under the constant-product invariant x · y = k is

Δy := y · Δx / (x + Δx).

This is the algebraic core of the Uniswap v2 model (Adams-Zinsmeister 2020), as formalised by Pusceddu-Bartoletti (2024).

theoremMathFin.DeFi.swap_preserves_invariantsource
∀ (x y Δx : ℝ), 0 < x → 0 < Δx → (x + Δx) * (y - swapOutput x y Δx) = x * y

Constant-product invariant preservation: after a swap of input Δx (with x, Δx > 0), the new reserves (x + Δx, y − Δy) satisfy

(x + Δx) · (y − Δy) = x · y,

where Δy := swapOutput x y Δx. This is the pricing rule of the CPMM: the swap is the trade that keeps the product fixed. That a round trip prints no money is swap_round_trip.

theoremMathFin.DeFi.swap_output_possource
∀ (x y Δx : ℝ), 0 < x → 0 < y → 0 < Δx → 0 < swapOutput x y Δx

Swap output is strictly positive when input is positive (and reserves are positive).

theoremMathFin.DeFi.swap_output_lt_ysource
∀ (x y Δx : ℝ), 0 < x → 0 < Δx → 0 < y → swapOutput x y Δx < y

Swap output bounded by reserve: Δy < y — the AMM cannot be drained by any single (finite) trade. The deficit is exactly the positive remainder y·x / (x + Δx).

theoremMathFin.DeFi.marginal_exchange_ratesource
∀ (x y Δx : ℝ), 0 < x → 0 < Δx → swapOutput x y Δx / Δx = y / (x + Δx)

Average execution rate: for any input Δx > 0, the rate Δy / Δx received is y / (x + Δx). The marginal rate at zero size, the internal price y / x, is hasDerivAt_swapOutput_zero.

defMathFin.DeFi.internalPricesource
ℝ → ℝ → ℝ

Internal price = reserve ratio at zero input: the spot price of token X (denominated in Y) implied by the AMM's curve at the current reserve point is y / x.

defMathFin.DeFi.arbitragePresentsource
ℝ → ℝ → ℝ → Prop

Arbitrage trigger condition: the AMM's internal price y/x differs from an external oracle's price p_oracle. That this is exactly the existence of a profitable swap at the oracle price is arbitragePresent_iff; the optimal arbitrage size is not formalized here (see Pusceddu-Bartoletti 2024 §3 and Bartoletti-Chiang-Lluch-Lafuente 2022, Theorem 4).

theoremMathFin.DeFi.swap_round_tripsource
∀ (x y Δx : ℝ), 0 < x → 0 < y → 0 < Δx → swapOutput (y - swapOutput x y Δx) (x + Δx) (swapOutput x y Δx) = Δx

A round trip prints no money. Swap Δx of X into the pool for Δy = swapOutput x y Δx of Y, then swap that Δy straight back into the new reserves (x + Δx, y − Δy): the pool returns exactly Δx, so this round trip neither gains nor loses.

theoremMathFin.DeFi.hasDerivAt_swapOutputsource
∀ (y : ℝ) {x Δx : ℝ}, x + Δx ≠ 0 → HasDerivAt (swapOutput x y) (x * y / (x + Δx) ^ 2) Δx

The marginal execution rate: the derivative of the swap output in the input size is x y / (x + Δx)².

theoremMathFin.DeFi.hasDerivAt_swapOutput_zerosource
∀ (y : ℝ) {x : ℝ}, 0 < x → HasDerivAt (swapOutput x y) (internalPrice x y) 0

The internal price is the marginal price. At zero size the swap pays y / x per unit of input: d(swapOutput x y)/dΔx (0) = internalPrice x y.

theoremMathFin.DeFi.exists_profitable_swap_iffsource
∀ {x y P : ℝ}, 0 < x → 0 < P → ((∃ Δx > 0, P * Δx < swapOutput x y Δx) ↔ P < internalPrice x y)

Selling X to the pool pays exactly when the pool overprices X. Buy Δx of X at the oracle price P and sell it to the pool: for some size Δx > 0 the proceeds swapOutput x y Δx exceed the cost P · Δx if and only if P < y / x.

theoremMathFin.DeFi.exists_profitable_reverse_swap_iffsource
∀ {x y P : ℝ}, 0 < x → 0 < y → ((∃ Δy > 0, Δy < P * swapOutput y x Δy) ↔ internalPrice x y < P)

Buying X from the pool pays exactly when the pool underprices X. Sell Δy of Y to the pool and the X received at the oracle price P: for some size Δy > 0 the proceeds P · swapOutput y x Δy exceed Δy if and only if y / x < P.

theoremMathFin.DeFi.arbitragePresent_iffsource
∀ {x y P : ℝ}, 0 < x → 0 < y → 0 < P → (arbitragePresent x y P ↔ (∃ Δx > 0, P * Δx < swapOutput x y Δx) ∨ ∃ Δy > 0, Δy < P * swapOutput y x Δy)

arbitragePresent means what it says. For positive reserves and a positive oracle price P, the pool price differs from P if and only if some single fee-free swap against the pool, valued at P, earns a riskless profit: selling X to the pool when it overprices X, or buying X from it when it underprices X. Equivalently, a pool at the oracle price admits no profitable swap of any size.