AMM Lab

A constant-product market maker holds two tokens and prices every trade off one line of arithmetic: x × y = k. This page is a simulated pool — drag a control and every figure below is recomputed in your browser from that one invariant. Everything here is arithmetic over numbers you chose, and nothing on this page is measured.

Six steps, each adding one live control: the curve itself, a swap along it, what a fee does to that swap, what a liquidity provider owns, what the position is worth after the price moves, and what a sandwich bot can take out of a trade whose slippage tolerance leaves it room.

Every figure on this page is simulated — arithmetic over numbers you chose, computed in this tab. Nothing here is measured and nothing here contacts a chain, a node or an API.

Loading the model…

1 The curve

The pool opens holding 1000000 TKN and 2000000 USD, so a TKN costs 2 USD at the margin. The product of the two reserves is k, and every trade has to leave the pool somewhere on the hyperbola that product describes. The marked point is where the pool is sitting now.

2 USD per TKN, at the margin
1000000 TKN in the pool
2000000 USD in the pool
2000000000000 k, the invariant

Depth scales both reserves together, so the price does not move — only how far a trade of a given size has to walk along the curve. That is the whole difference between a thin pool and a deep one, and step 2 puts a number on it.

2 A swap

Sell TKN into the pool and it hands back USD from the other reserve. The rate you are quoted is the spot price, which is the price of an infinitesimal trade. The rate you actually get is the execution price, averaged over the whole size — and it is always worse, because your own trade moves the pool while it fills.

USD received
execution price
price impact
spot price afterwards

The same trade, thin pool and deep pool

These two columns run the trade above against the shallowest and deepest pool the depth control offers, leaving everything else alone. The price is identical in both; the impact is not.

Same trade, same price Thin pool (×0.2) Deep pool (×5)
USD received
Execution price
Price impact

3 Fees

The fee is taken off the input before the curve prices anything, so the pool receives the whole trade and only part of it counts. That is what makes k grow rather than hold: the difference is the fee, and it stays in the pool for whoever supplied the liquidity.

TKN kept as the fee
USD received
price impact, fee included
k after the trade

The fee tier applies to every figure on this page, not only to this step. Set it to 0% and watch k stop growing.

4 An LP position

A deposit has to arrive at the pool’s current ratio, or it would move the price — which would be a swap rather than a deposit. So you choose one side and the other is decided for you, and what you receive back is a share of the pool rather than a claim on particular tokens.

USD you must deposit alongside
shares minted
of the pool
shares outstanding after

5 Divergence loss

The pool does not know the outside price has moved — arbitrage is what tells it, by trading against the curve until the pool’s own marginal price matches. That leaves a liquidity provider holding more of whichever token fell and less of the one that rose, which is worth less than simply having kept both. Move the external price and watch the two lines separate.

USD if you had just held
USD the position is worth
divergence loss
external price now

Two things this figure deliberately leaves out, because they are what a reader would otherwise assume. It carries no fee income — it is the headwind the fees have to beat, not a prediction of the outcome. And the loss is the same whether the price doubles or halves: the curve does not care which way it moved. The usual name for this, “impermanent loss”, is a poor one — nothing becomes permanent or impermanent, and the loss is realized the moment the position is withdrawn.

6 The sandwich

Your trade sits in public before it is included, and it carries a minimum output: anything that would fill you below that reverts. A bot reads it, buys in front of you to push the price up, lets your trade fill at the worse rate, and sells straight after into the pool your trade just moved.

What stops it is your own slippage tolerance. The bot cannot push you past your minimum without the whole thing reverting, so the tolerance is the bot’s budget. The run below sizes the front-run to the largest the tolerance permits — drag the tolerance and watch both sides move together.

USD you were quoted
your minimum, after tolerance
USD you actually received
USD you lost
TKN the bot put in front
TKN the bot made, net of fees

What the model does and does not include

The whole model is the invariant and a fee on the input: out = y · in(1−f) ÷ (x + in(1−f)). Everything above is derived from that line and nothing else. There is deliberately no gas cost, no block ordering, no mempool, no failed transaction, no concentrated liquidity and no multi-hop routing. Each of those is real and each would make this page worse at its one job: halving the trade should halve the impact in a way you can check in your head, and a figure a reader can verify unaided is worth more here than a figure that looks realistic.

The pool, the two token names and every number on this page are invented. Nothing here is a quote, a price, a recommendation or financial advice, and the arithmetic is a teaching model rather than a description of any deployed contract.