DeFiGrail
LOADING CONTENT INDEX…
LIVE
BTC$71,240+2.1%ETH$3,905+3.4%DeFi TVL$112.4B-0.8%
DATA AS OF 14:00 UTC
Esoteric

AMM Pricing Math

The exact arithmetic behind a swap quote: how a constant-product curve turns reserves into a price and a trade into impact.
TradFi →Market-impact / bonding-curve model

01 · Concept — what problem does it solve?

An has no human quoting prices — a curve does it. For a constant-product pool the curve is x · y = k, and everything about pricing falls out of that one equation: the spot price, the fill you actually get, the impact you cause, and the fee you pay. This page is the math under the hood of every Uniswap v2-style swap, so a quote stops being magic and becomes arithmetic you can reproduce.

02 · Mechanics

  • Spot price = reserve ratio: the marginal price of X in terms of Y is just y / x. Provide more of X and X gets cheaper — the curve is supply and demand.
  • The invariant holds across the trade: add Δx of X, the pool gives you the Δy of Y that keeps x · y = k (minus fee). Big trades ride further down the curve → worse average price.
  • Fee first: the 0.30% fee is taken off your input before it touches the curve, so it stays in the pool and grows value.
  • Impact is structural, not a setting: the worse-than-spot fill is the geometry of the hyperbola, the AMM's built-in "." It cannot be turned off, only diluted with deeper liquidity.

03 · Formulas

// the invariant
x · y = k

// output for input Δx (with 0.30% fee = factor 997/1000)
Δy = (y · Δx · 997) / (x · 1000 + Δx · 997)

// spot (marginal) price and execution price
spot = y / x
exec = Δy / Δx          // always worse than spot for any real trade

// price impact as a fraction of the reserve traded (fee aside)
impact = Δx / (x + Δx) ≈ f / (1 + f)   for buying;  ~f/(1−f) for selling X out
10% of reserve
Price impact ≈ 11.1%
Constant-product (x·y=k): impact = f / (1 − f). The deeper the pool relative to your trade, the smaller f — and impact stays near zero.

04 · Edge cases & risks

  • LP rebalancing is forced selling: the same math means the pool always sells the winner and buys the loser — the mechanical root of impermanent loss.
  • Sandwichable by construction: because the output is a deterministic function of reserves, a can compute your fill to the wei and bracket it. See MEV.
  • Different curve, different math: Curve's StableSwap flattens the curve near peg (tiny impact), and v3 concentrates liquidity — same idea, different f→impact function.
  • Rounding & decimals: real implementations use integer math and per-token decimals; naive float math drifts from on-chain results.