DeCrypto Markets

Learn: exchanges

What is an automated market maker (AMM)?

An automated market maker is a smart contract that holds two tokens in a pool and prices swaps with a formula instead of an order book. In the common constant-product design, the reserves x and y must keep x times y at least equal to k. Every swap moves the price along that curve.

How it works

An order book needs people to post bids and asks. An AMM replaces them with a pool: two token reserves that anyone can trade against at a price set by a formula. People who deposit tokens into the pool are liquidity providers; they earn the pool's fees and carry the risks, including impermanent loss.

In a constant-product pool the rule is x times y = k, where x and y are the two reserves. The pool's spot price is simply y / x. When you swap in some of one token, the pool gives out enough of the other that the product does not fall. Because the curve bends, each extra unit you buy costs more than the last: that is price impact.

A fee, often a fraction of a percent, comes off what you put in and stays in the pool, so k grows slightly with each swap. Nothing about the formula checks whether the tokens are worth anything: anyone can create a pool for any token, including fake copies of real ones.

The exact rule the games grade by

The rule the games use: spot price = y / x. The fee f comes off the input and stays in the pool. Output = rOut x a(1 - f) / (rIn + a(1 - f)), on integer units, rounded down, as on chain, so x times y never decreases. Price impact = a(1 - f) / (rIn + a(1 - f)): your own trade's effect on the price, fee excluded. Minimum received = floor(quote x (10000 - tolerance in bps) / 10000); below it the swap reverts.

Step by step

  1. Read the reserves of the two tokens, x and y, and the fee.
  2. Take the fee off your input: a(1 - f) is what actually goes into the curve.
  3. Output = the other reserve x a(1 - f) / (your reserve + a(1 - f)).
  4. Your average price is what you paid divided by what you got; compare it with the spot price before the swap.
  5. Set a minimum received from the quote and your tolerance, so a worse fill reverts instead of executing.

An AMM next to an order book

  • Price: an order book shows prices people chose; a pool computes the price from its reserves.
  • Depth: on a book it is the size at each level; in a pool it is the size of the reserves. Bigger reserves mean less impact for the same trade.
  • Who trades with you: on a book, other traders; in a pool, the liquidity providers' tokens, through a contract.
  • Ordering: on a public chain, pending swaps can be seen and reordered by others before they execute. That is the opening for MEV.

Worked example

A swap moves along the constant-product curveThe curve x times y = 30,000,000. Buying ETH with 30,000 USDT moves the pool from 100 ETH and 300,000 USDT to 90.9339 ETH and 330,000 USDT, and the spot price from 3,000.00 to 3,629.01.200k300k400k500k6080100120140160USDT in the poolETH in the poolx * y = 30,000,000Before: spot 3,000.00After: spot 3,629.01
The curve x times y = 30,000,000. A 30,000 USDT buy moves the pool from the green point to the orange one: spot 3,000.00 to 3,629.01.

The pool in the diagram holds 100 ETH and 300,000 USDT, so k = 30,000,000 and the spot price is 3,000.00 USDT per ETH. The fee is 0.30%. You swap 30,000 USDT for ETH.

First the fee: 90.00 USDT stays in the pool and the rest goes into the curve. The pool gives out 9.0661 ETH, so your average price is 3,309.03 USDT per ETH, not 3,000.00. Part of that gap is the fee; the rest, your trade sliding along the curve, is a price impact of 9.07%.

After the swap the pool holds 90.9339 ETH and 330,000 USDT, and the spot price has moved to 3,629.01. The next buyer starts from that higher price. Arbitrage traders usually bring a pool back in line with other markets, which is part of why liquidity providers face impermanent loss.

Takeaway: In a pool of 300,000 USDT, a 30,000 USDT buy gets 9.0661 ETH at an average of 3,309.03, a 9.07% price impact. Size a swap against the reserves, not against the spot price.

Common mistakes

  • Expecting the spot price for a large swap. The spot price only applies to a tiny trade. Any real swap moves along the curve and fills at a worse average. Check the quoted output, not the price shown on the pool.
  • Trading in a pool with tiny reserves. Price impact depends on your size relative to the reserves. A swap that is a rounding error in a deep pool can move a small pool by double digits.
  • Trusting the token because a pool exists. Creating a pool needs no permission. Scammers create pools for copies of real tokens with the same name and symbol. Check the token's contract address from a source you trust; see rug pulls.
  • Setting no minimum received. Without a sensible slippage tolerance, your swap accepts any output, and a bot can place trades around it. See MEV.

What this does not tell you, and what can go wrong

The game's pool is a clean constant-product pool with no other traders except the ones it simulates. Real AMMs come in several designs, such as concentrated liquidity and pools built for stablecoins, and their maths differs. The x times y = k rule is the classic case, not the only one.

An AMM is software. Contracts can have bugs, admin keys or upgrade paths, and pools can hold tokens built to trap buyers. A correct price calculation tells you nothing about any of that, and losses from a contract failure are often not recovered.

Questions people ask

What does x*y=k mean?

It is the constant-product rule. x and y are the amounts of the two tokens in the pool, and their product k may not fall during a swap. When you add some of one token, the pool releases enough of the other to keep the product. The price is y divided by x.

Who sets the price in an AMM?

The reserves do. The pool's price is the ratio of its two reserves, and every swap changes that ratio. Arbitrage traders buy where it is cheap and sell where it is dear, which tends to keep a pool near prices elsewhere, but nothing forces it.

Where does the swap fee go in an AMM?

In the constant-product model used here, the fee comes off your input and stays in the pool, so the reserves grow and liquidity providers own a slightly larger pool. Real designs vary, and some send part of the fee elsewhere.

Is trading on an AMM safer than on an exchange?

It swaps one set of risks for another. You keep custody until the swap, but you take on contract bugs, fake tokens, approvals and bots that see your transaction. Neither route is safe in itself; each fails in its own ways.

Practise it

Reading about a rule is not the same as applying it. These games generate a fresh simulation each round, ask you to make the call first, and grade you against the same rule.

AMM Swap

Predict the price impact of a swap in an x*y=k pool, then set the slippage tolerance.

What it teaches

Impermanent Loss

Provide liquidity, then the price moves. Compare the pool with simply holding, and Decrypt the gap.

What it teaches