Gravitational plate of three masses and a slashed discABC0Static engraved plate. Three-dimensional view is unavailable or reduced motion is requested.

← back to fieldarticle

articleJan 30, 2023

AMM and PMM mechanisms: math beyond the limit order book

From LOB limits to AMM conservation functions: actors, assets, slippage, divergence loss, and a state-space formalization of swaps and liquidity changes.

AMM and PMM mechanisms: math beyond the limit order book

Goal

This note starts from Limit Order Book (LOB) economics, then moves to Automated Market Makers (AMMs) and Proactive Market Makers (PMMs). The aim is to define how liquidity state changes under swaps and liquidity moves, and how conservation (invariant) functions pin those transitions down.

I reviewed the SoK paper Decentralized Exchanges (DEX) with Automated Market Maker (AMM) Protocols.

Limit Order Book

A LOB is the CEX matching engine. Buyers and sellers post prices; the book matches on price-time priority. The last match sets the traded price; supply and demand set the rest.

In finance literature, LOBs are modeled as multi-dimensional Markov systems — continuous-time Markov chains, stochastic PDEs, or queuing networks. State is the distribution of limit orders (price, size) and market orders (arrival, trade size). Models capture price formation, execution, impact, and liquidity provision.

Bids and asks define supply and demand. The gap between best bid and best ask is the spread. A new order that changes that spread is price discovery. Market clearing is the point where quantity demanded equals quantity supplied at the prevailing price.

Common formal models include Almgren–Chriss (price impact vs trading speed), Kyle (dynamic impact of sequential market orders), Obizhaeva–Wang (best bid/ask evolution), and Gatheral (impact vs volatility and speed).

AMM and PMM

An AMM prices assets with a mathematical rule instead of an order book. Liquidity pools are the counterparty. A conservation function (bonding curve) moves price along a predefined path as reserves change. Liquidity providers (LPs) deposit assets; traders swap against the pool; LPs earn fees. Keeping a full on-chain order book is expensive; the conservation function replaces that state.

A PMM is an AMM variant that leans on predictive / proactive quoting to track expected future supply and demand more tightly than a pure reactive curve.

Tradeoffs vs LOB

LOB — strengths: transparent order flow and price discovery; anyone can post; pricing is supply/demand driven. Weaknesses: latency and infra dependence; complexity for retail; slippage when the book moves; partial fills.

AMM — strengths: liquidity even at low volume; continuous quotes. Weaknesses: weaker native price discovery; impermanent (divergence) loss for LPs; pool depth dependence; smart-contract risk.

PMM — strengths: tighter spreads and lower impact when the quoting model is good. Weaknesses: algorithm complexity and model risk.

AMM components

Actors

Liquidity providers seed and top up pools, receive pool shares proportional to contribution, earn swap fees, and can withdraw by burning shares (sometimes with a penalty).

Traders send swaps that specify input/output assets and amounts. The contract prices them off the conservation function and fees. Arbitrageurs close gaps versus other venues (DODO-style designs lean into this).

Protocol foundation designs and ships the contracts. Revenue often funds that work, so volume-friendly UX is an incentive, not charity.

Assets

Risk assets are the illiquid or speculative tokens AMMs were built to list (IDO-style launches need whitelist + standard interfaces such as ERC-20).

Base assets appear when a design forces every pair to include a designated numeraire (Bancor/BNT; Uniswap V1/ETH). Balancer and Curve can pair risk assets directly.

Pool shares (LP tokens) represent ownership of the reserve basket and claim on fees.

Protocol tokens usually carry governance rights and sometimes liquidity-mining rewards used to bootstrap early pools.

Invariants

How you encode the invariant depends on the conservation function. Uniswap-style constant product keeps the product of (value-weighted) reserves. Each trade removes value in one asset and adds equal value in the other along that curve. Allowed mechanisms are exactly those that preserve the chosen invariant under swap and under proportional liquidity add/remove.

Economics: rewards and costs

Rewards. LPs earn swap fees for locking capital. Some designs add staking or governance-token emissions. Bug bounties pay for finding contract flaws on top of audits.

Explicit costs. Swap fees (paid into the pool / protocol), gas on every on-chain call, and occasional withdrawal penalties.

Implicit costs.

Slippage
Slippage

Slippage is the gap between spot price and realized execution price. On a bonding curve every trade moves price; small trades sit near spot, large trades do not. Thin pools amplify that. Continuous slippage is also why sandwich attacks exist — users set slippage tolerance because of it.

Divergence loss
Divergence loss

Divergence loss (impermanent loss) compares LP portfolio value inside the pool to holding the same assets outside. Swaps reweight reserves along the curve; relative price moves can leave the LP worse than HODL until prices revert. Fees are supposed to compensate. Multi-asset pools can also expose an LP to assets they never held directly.

Formalization

State space

Treat the protocol as a blockchain-based system of states and agents. Pool state evolves under a transition function.

State Space Representation
State Space Representation

Liquidity pool State X
Liquidity pool State X

The important design knob is the conservation function: it ties reserve variables to invariant(s) II.

Liquidity provision and withdrawal
Liquidity provision and withdrawal

Liquidity Swap
Liquidity Swap

Reserve quantities are the health of the system. Typical invariants constrain sums or products of those reserves.

Notation and transitions

mathematical notations
mathematical notations

Hyperparameters are set at pool creation and should change rarely (governance), not every trade.

On a swap, invariant II stays fixed while reserves move and the outgoing asset gets more expensive relative to the incoming one. Pure liquidity add/remove should not move relative prices — reserves change in proportion.

Liquidity Pool State change && Swap
Liquidity Pool State change && Swap

Conservation function

Conservation function
Conservation function

Also called a bonding curve: it relates invariants to reserve quantities.

uniswap conservation function
uniswap conservation function

For a pair riror_i \to r_o, the curve should be concave, non-negative, and non-decreasing. Complex designs (Curve) often keep the conservation function implicit and still recover exchange rates.

A bonding curve also shows up in token distribution: early buyers pay less; later buyers pay more — automatic price adjustment as supply is sold.

Initial liquidity fixes II. Later, changing one reserve lets you solve for the other.

Init Liquidity Pool create Invariants
Init Liquidity Pool create Invariants

Spot exchange rate

Spot between token ii and token oo is the slope of the rir_iror_o curve from partial derivatives of conservation function ZZ.

spot exchange rate
spot exchange rate

Swap amounts

Given input xix_i (with xi>rix_i > -r_i, xo>rox_o > -r_o), update reserves, solve the conservation function for the unknown outgoing reserve, then take the difference as the swapped amount. Lower bounds are pool depletion; most AMMs have no useful theoretical upper bound on input size other than catastrophic slippage.

Update reserve quantities
Update reserve quantities

unknown quantity solve conservation function
unknown quantity solve conservation function

reserve token must be constant
reserve token must be constant

Compute Swapped quantity
Compute Swapped quantity

Slippage (formal)

Slippage compares the effective rate xi/xox_i / x_o to the pre-trade spot. Zero means no deviation; larger values mean worse execution.

Slippage calc
Slippage calc

Divergence loss (formal)

Measure how much pool value lags holding the same reserves outside the pool after a relative price move pp.

Reserve Value
Reserve Value

Calculate the reserve value if held outside of the pool
Calculate the reserve value if held outside of the pool

exchange rate, conservation function
exchange rate, conservation function

new pool value
new pool value

Calculate the divergence loss
Calculate the divergence loss

Divergence loss is the relative gap between rebalanced pool value VV' and the held-outside value VheldV_{\mathrm{held}}.

Takeaway

LOB friction pushed DEX design toward conservation-function AMMs. Uniswap, Curve, Balancer, DODO and their forks share that core and differ in invariants, fees, and extras. This note fixed the shared math: state, invariants, swaps, slippage, and divergence loss.

Next pass: protocol-specific behavior and failure modes at network, contract, and application layers.

Reference

https://arxiv.org/abs/2103.12732

related

  1. Feb 2, 2023/articleUniswap V2 math and contract review
  2. Aug 3, 2022/articleUniswap V2 smart contracts
  3. Aug 4, 2022/articleUniswapV2 subgraph event queries

graphfeed