Advanced

AMMs Deep Dive — Key concepts

Build a minimal x·y=k AMM.

Developer Advanced
4/8 — AMMs Deep Dive — Key concepts

Invariantx·y=k must hold after each swap (post-fee). Fees grow k and shift prices to reward LPs.

Constant product
x · y = k
After applying a fee to the input amount; k should never shrink.

Spot priceInstantaneous price is the marginal slope of the curve.

Price of token0
P0 = y ÷ x
After the swap, update reserves before emitting events.

Slippage & impactLarge trades move you along the curve; slippage scales with trade size vs reserves.

Approx. price impact
impact ≈ Δx ÷ (x + Δx)
Use this to warn users and set max slippage.

Impermanent lossIL measures value drift vs simply holding tokens. Higher fees and balanced pairs offset it; volatile pairs magnify it.

Key points
  • Invariant guard:K must not decrease after fees.
  • Fee path:Apply fee to input before solving for output.
  • Price read:Use reserves after the swap is settled.
  • Observability:Emit Swap/Mint/Burn with reserves.
  • Oracle view:Maintain cumulative prices for TWAP.
← Previous section
Next section →