All articles
Web3 FoundationsJuly 14, 202611 min read

How AMMs Work: The Formula That Replaced the Order Book

How automated market makers price trades with one formula: the balance scale behind Uniswap, why big trades cost more, and who quietly pays to keep a blind machine honest.

By Carlos (Bloqarl)

TL;DR

  • An AMM (automated market maker) replaces the order book with a pot of two tokens and one rule: the product of the two amounts must never fall. That rule is the famous formula x × y = k.
  • Nobody quotes prices. The ratio of the two tokens IS the price. Divide one side of the pot by the other and you are reading the quote.
  • Big trades get worse prices than small ones, not as punishment but as geometry: every trade slides the pool along a curve, and the curve steepens against size.
  • The pool has no price feed and reads no news. Arbitrageurs, traders paid by price gaps, are the only thing keeping its quotes in line with the rest of the world.
  • Who is paying for that correction service? The people who filled the pot. Liquidity providers quietly pay arbitrageurs every time the world moves.

How do AMMs work, in one sentence?

An AMM is a smart contract holding two tokens that lets anyone trade one for the other, at a price set entirely by the ratio of what it holds, under one rule: the product of the two amounts can never fall.

No trading firm stands behind it. No employee updates a price each morning. No feed pipes in quotes from somewhere else. A pot and a rule, always open, and it will quote you a precise price at 3am on a Sunday for a token nobody else on earth is awake to trade.

That machine is the beating heart of decentralized exchanges like Uniswap, and it is the reason most of DeFi exists in the shape it does. If you are still fuzzy on what DeFi itself is, start with the DeFi primer and come back. This article opens the machine.

Why did DeFi need to replace the order book?

Traditional markets run on order books: long queues of buy offers and sell offers, with professional market makers standing in those queues all day quoting both sides. Instant fills exist because those firms are always there. We walk through that whole machine in how crypto markets actually work.

Now picture a brand new token at 3am. No firm is paid to quote it. The order book is empty. Your buy order arrives and meets nothing. A market with no makers is just an empty room.

Early DeFi lived in that empty room. Thousands of tokens existed, and no crowd of professional makers was ever going to show up for any of them. An order book only works where a crowd gathers, and no crowd was coming.

The fix was strange and beautiful: replace the queues with a pot and a rule, so that a counterparty is simply always there.

What is the constant product formula, x × y = k?

Here is the pot. Think of it as a balance scale with a pan on each side. One pan holds some amount of one token, the other pan holds some amount of the second token. A concrete pool:

  • Pan X: 10 ETH
  • Pan Y: 30,000 USDC

Anyone can put tokens into one pan and take tokens out of the other. That is a trade. And one rule governs every trade: multiply what sits in the two pans, and that product must never fall.

For this pool, 10 × 30,000 = 300,000. That number is the k in x × y = k. Take ETH out of pan X, and you must put enough USDC into pan Y to keep the product at 300,000 or above. That single constraint is the entire pricing engine. There is no second mechanism hiding underneath.

If you plot every combination of the two pans that keeps the product whole, you get a curve. The pool is always exactly one point on that curve, and a trade does one thing only: it slides the point along the curve. Nothing else can move it.

Now the names, since you have already met the thing they name. A pot that always quotes both sides is doing a market maker's job with no human inside: an automated market maker, an AMM. The rule, two amounts whose product holds, is the constant product formula. Uniswap runs on exactly this.

Who sets the price in an AMM?

This is the question that trips up almost everyone the first time. There are no queues of humans naming prices. There is no oracle feeding numbers in. Yet when you swap, you get a precise price instantly. Where does the number come from?

Divide the pans. In our pool, 30,000 USDC over 10 ETH reads 3,000 USDC per ETH. Nobody typed that in. The tilt of the scale is the price, and every trade that retilts the pans rewrites it.

This is worth sitting with, because it is the single most load-bearing fact about AMMs. The pool's state IS the quote. There is no separate place where the price lives. The price is not stored, announced, or scheduled. It is read off the ratio, fresh, every time someone asks.

Notice what is missing from that description: no price feed, no announcement, no human judgment. The pool does not know what ETH is worth. It only knows what it holds. Hold that thought, because in a moment it becomes a problem.

Why does the price move when you trade?

Because your trade retilts the scale. Buy a little ETH and the point slides a little along the curve: the ETH pan lightens slightly, and the price barely moves. Buy a lot and the point travels into the bend of the curve, where every extra ETH costs more USDC than the one before. The curve steepens against you.

Run the numbers on our pool. It starts at 10 ETH and 30,000 USDC, quoting 3,000 per ETH. Now you make a big buy that takes 2 ETH out. To keep the product at 300,000, the pool must end at 8 ETH and 37,500 USDC. You paid 7,500 USDC for those 2 ETH, an average of 3,750 each, well above the 3,000 the scale read when you walked up. And the pool's new quote, 37,500 divided by 8, is about 4,700 USDC per ETH.

That worsening is called price impact, and it is geometry, not punishment. In an order book, a big order eats through a thin queue of offers, each one worse than the last. In an AMM, the same order climbs a steepening curve. Same lesson, new machine: size moves price, and this machine never pretended otherwise. Price impact is one of the two things people lump together as slippage, which we untangle properly in what is slippage in crypto.

Who keeps an AMM's price honest?

Your big buy just left the pool quoting about 4,700 USDC per ETH while the rest of the world still says 3,000. The pool cannot notice. It has no eyes. But a trader watching both screens can, and the gap is free money: buy ETH where it is cheap, sell it where it is dear, keep the difference.

Traders who do this are called arbitrageurs. Nobody hires them, nobody thanks them, and they are the only reason a blind machine quotes sane prices. They sell ETH into your overpriced pool until the tilt matches the world again, pocketing the gap as their pay. The pool is honest again, and it never knew anything happened.

Now flip the direction. So far you moved the pool and the world corrected it. What happens when the world moves and the pool stands still?

Say ETH doubles on Binance within an hour, and nobody happens to trade against our pool. What is it quoting? The old price. Exactly the old price, to anyone who asks, for as long as nobody trades. The pool learns prices only through trades. There is no feed to catch up and nothing to freeze. Its ETH now sits at half the world price, which is a screaming invitation: arbitrageurs buy the cheap ETH until the tilt matches reality, and the gap is their pay.

The classic wrong mental model is that the pool "updates" to the new price on its own. It cannot. Its price is its pans, and pans move only when a trade moves them.

Who is paying for all of this?

Here is the question this whole series keeps asking, because following the money is how you actually understand a machine. The arbitrageur just made a profit. Profit comes from somewhere. Where?

Out of the pot itself. The arbitrageur bought cheap ETH from the pool and sold it dear elsewhere, and the value they extracted came from the pool's holdings. Whoever filled that pot quietly pays, every time the world moves.

Those pot-fillers are called liquidity providers, LPs, and they are not charity. They earn a small fee on every swap, which is why they show up at all. But the arbitrage cost is real, it is constant, and almost nobody mentions it at the door. Whether the fees outrun that quiet cost is the entire business question of being an LP, and it has a famously misleading name. We open that ledger fully in impermanent loss explained. If you have ever been tempted by a juicy pool APY, read it before you deposit anything.

Did AMMs make order books obsolete?

No, and anyone who tells you otherwise is selling something. The honest answer is a split map, not a winner.

Where makers are plentiful, order books give tighter prices and faster fills. The big exchanges, the biggest pairs, and professional flow still run through books, because a deep crowd beats a curve on price wherever the crowd exists.

Where no crowd will ever gather, the pot quotes anyway. The AMM won the long tail: countless small tokens, any hour, no listings desk. Anyone can fill a pot for any pair of tokens, and a market exists by dinner.

And the AMM has one more property the order book never had: because it is a smart contract, other machines can plug straight into it. A lending protocol, a yield vault, an aggregator can all call the pool directly, with no API keys and no business development meeting. That composability made permissionless markets real, and it is why the AMM sits at the center of DeFi's map rather than at its edge.

Two machines, two jobs. One needs a crowd. One needs a formula.

How do you learn this properly, not just read about it?

An AMM is one of those machines you understand ten times faster by poking it than by reading about it. Push a price, watch the pans retilt, watch the arbitrage walk it back.

That is exactly how we teach it in Your First 90 Days in DeFi, a free, guided course by the security firm Zealynx. The AMM checkpoint has you predicting who sets the price, sliding the pool along its curve, and mapping the arbitrage walk step by step. The first three checkpoints need no account. It is understanding first, decisions about money later, if ever.

The next machine on the tour is the natural follow-up to this one: what it is like to be the person who filled the pot. And after that, concentrated liquidity, where Uniswap v3 let LPs choose where on the curve to stand and turned passive pot-filling into a job.

Related questions

What does x × y = k actually mean? x and y are the amounts of the two tokens in the pool, and k is their product. The rule is that no trade may make the product fall. That single constraint forces big trades to pay progressively worse prices and makes the token ratio act as the price.

Do AMMs use price oracles? The basic constant product AMM does not. It has no external price feed at all. Its price comes purely from the ratio of tokens it holds, and it only moves when a trade moves it. Arbitrageurs are what keep that internal price aligned with the rest of the market.

Why do I get a worse price on a big trade? Because every unit you buy shrinks one side of the pool and steepens the curve against you. The pool charges more for each additional unit by construction. This price impact is not a hidden fee anyone collects; it is geometry built into the formula.

Who are arbitrageurs and are they bad for me? Arbitrageurs are traders who profit from price gaps between venues, buying where an asset is cheap and selling where it is dear. For traders they are mostly good news: they are the reason AMM prices track reality. The people they quietly cost money are the liquidity providers whose pot funds the correction.

Is Uniswap an AMM? Yes. Uniswap is the best known implementation of the constant product AMM, and its pools work exactly as described here: two tokens, one formula, prices set by the ratio, kept honest by arbitrage.

Can anyone create an AMM pool? Yes, and that is much of the point. There is no listings desk. Anyone can stock a pot with any pair of tokens and a market exists immediately. That openness created markets no order book could host, and it also means nobody vetted the token on the other side of the pool.

Where to go next

You now know the half second behind a swap: your trade met a pot, slid a point along a curve, retilted a scale, and left behind a price gap that arbitrageurs will close for pay. No staff, no feed, no office hours. One formula doing a market maker's job.

One thread is left hanging, and it is the expensive one. Somebody filled that pot, they earn a fee from every swap, and they quietly fund every arbitrage correction. Whether that business wins or loses is the subject of impermanent loss explained, and it is the single most misunderstood topic in DeFi. Read it before you ever provide liquidity. Or start the interactive version below, where you can push the prices yourself.

Tagged

DeFiAMMsUniswapCrypto for Beginners