DeFi 101 — Initiate · Lesson 2 of 5

AMMs — constant product, slippage, and why pools work

6 min · read

An Automated Market Maker is a smart contract that holds two (or more) tokens and lets users swap between them according to a formula. No order book, no matching engine — just a pool and a curve.

The seminal AMM curve is constant product: x · y = k.

The formula

A pool holds X tokens of asset A and Y tokens of asset B. Their product k = x * y is invariant — every trade must preserve it.

To buy Δy of asset B, you must add enough A so the new pool satisfies the invariant:

(x + Δx) * (y − Δy) = k

Solving for Δx:

Δx = (k / (y − Δy)) − x

This means the price of B in terms of A rises as you buy more B — the pool runs out of B, so its scarcity increases. Slippage is this price change relative to your order size. Small orders barely move the pool; large ones move it a lot.

Why pools work

A market maker is anyone willing to be on both sides of a trade. In TradFi, professional firms quote bids and asks all day, profiting from the spread. AMM pools replace those firms with passive capital: anyone can be the market maker by depositing the two assets in equal value, and the pool's curve sets the prices automatically.

The depositors (liquidity providers, "LPs") earn the trading fees. A typical pool fee is 0.30% on each trade; high-volume pools split that among LPs proportional to their share.

Constant product is one of several

  • Constant product (Uniswap V2): x · y = k. Universal but inefficient at narrow price ranges.
  • Constant sum (lossy): x + y = k. Used for tightly-pegged pairs like USDC/USDT before depegs got cheap.
  • Stableswap (Curve): Hybrid — flat near the peg, curved at the edges. Designed for stablecoin pairs.
  • Concentrated liquidity (Uniswap V3): LPs choose price ranges; capital efficiency 100-4000× higher but more active management.

XRPL's native AMM uses constant product. Osmosis uses a variant. Most chains have at least one AMM.

Slippage protection

Every AMM swap UI lets you set a slippage tolerance — typically 0.5%-3%. If the actual execution price deviates from the quoted price by more than the tolerance, the trade reverts.

Two failure modes:

  • Slippage too tight. Price moved between your click and the block. Trade reverts; you pay gas for nothing.
  • Slippage too loose. A sandwich bot front-runs you, pushing the price up before your buy, and dumping after.

Most DEX UIs default to 0.5% for stable pairs, 1-3% for volatile pairs. Gopnik's swap surface uses the same defaults but exposes the slider.

Front-running and MEV

When you submit a swap to a public mempool, MEV bots see it before inclusion. They can:

  1. Buy the same token in front of you (push price up).
  2. Wait for your buy (which now happens at the higher price).
  3. Sell after your buy (capture the difference).

This is "sandwich attacks". Mitigations:

  • Private mempools (Flashbots Protect, MEV Blocker)
  • Tight slippage (forces the bot to use thin profit margins)
  • AMMs with threshold-encrypted mempools (Osmosis — see iter-G's cosmos.101)

The wallet's swap UI routes through MEV Blocker by default on Ethereum L1.

What this means for you

When you swap on an AMM:

  1. The bigger your order relative to pool size, the worse your effective price.
  2. The narrower your slippage, the more likely the trade reverts but the less you can be front-run.
  3. The chain's MEV landscape determines how much you're being squeezed.

For small swaps (< €100), AMMs are usually fine. For €1k+, look at multiple pools, use limit orders if the protocol supports them, and consider MEV protection.

Next: lending protocols.