Core concept

How automated market makers work: the x · y = k formula

Automated market makers let you swap one token for another without waiting for a matching buyer or seller. A small piece of code holds the tokens and quotes a price using a simple piece of arithmetic.

On a traditional exchange, a trade only happens when a buyer and a seller agree on a price. Someone has to be on the other side of your order. Automated market makers (AMMs) take a different approach: instead of matching people, a smart contract holds a shared pool of two tokens and calculates a price by formula. Anyone can swap against that pool at any time, and the contract works out the rate automatically. Uniswap is one well-known decentralized exchange built on this idea, though the underlying mechanism is used by many independent projects.

The problem AMMs solve

Order-book exchanges rely on a steady stream of buyers and sellers posting offers. When few people are trading a particular pair, orders can sit unfilled and prices can be hard to pin down. An AMM removes the need for a counterparty entirely. A pool of, say, Token A and Token B sits inside a smart contract. If you want to swap Token A for Token B, you hand some Token A to the pool and the contract sends you Token B in return. There is no person to match with; the pool itself is always available.

The interesting question is: how does the contract decide how much Token B to give you? That is where the formula comes in.

The constant-product formula

The most common AMM design uses a rule called the constant product formula. It is written like this:

x · y = k

Here x is the amount of one token held in the pool (the reserve of Token A), and y is the amount of the other token (the reserve of Token B). Multiply them together and you get k, a number the contract tries to keep constant through each swap (before fees). When you add some Token A and remove some Token B, the two reserves change — but their product must stay equal to the same k. That single requirement is what determines the exchange rate.

A graph with reserve x on the horizontal axis and reserve y on the vertical axis. A curved line shows x times y equals k. A swap moves the pool from a 'before swap' point to an 'after swap' point along the curve, while k stays constant before fees.
A swap slides the pool from one point to another along the constant-product curve.

If you plot every combination of x and y that keeps their product equal to k, you get a smooth curve that bends but never touches either axis. Every swap simply slides the pool's position along that curve from one point to another.

How price emerges from the reserves

The price of one token in terms of the other is just the ratio of the reserves. If a pool holds equal amounts of Token A and Token B, then one of each trades for roughly one of the other. If the pool holds far more Token A than Token B, then Token A is relatively plentiful and each unit of it is worth less Token B. Because every swap changes the reserves, every swap also nudges the price. Buying Token B out of the pool makes Token B scarcer and Token A more plentiful, so the next person finds Token B a little more expensive.

A worked example (for illustration only)

Imagine a pool with 1,000 Token A and 1,000 Token B, so k = 1,000,000. These numbers are illustrative only and are not real prices. Suppose someone wants to take 100 Token B out of the pool. After the swap the pool must hold 900 Token B. Because the product has to stay at 1,000,000, the new Token A reserve must be 1,000,000 ÷ 900 ≈ 1,111. So the pool needs about 111 more Token A added to release 100 Token B. The swapper put in roughly 111 Token A to get 100 Token B — an effective rate worse than one-to-one, and the reserves (now ~1,111 A and 900 B) reflect a new, shifted price.

Bigger trades, arbitrage, and fees

Notice that the price moved during that single trade. The larger your swap relative to the size of the pool, the further it pushes the reserves along the curve, and the more the price shifts against you. This effect is closely related to why trades have slippage: a small swap barely moves the reserves, while a large one can move them noticeably.

Because a pool sets its own price from its own reserves, that price can drift away from prices on other venues. When it does, independent participants can swap against the pool in the direction that closes the gap — buying the token the pool has priced low and selling the token it has priced high. Each such swap nudges the reserves until the pool's ratio lines up with the wider market again. This process, called arbitrage, is simply how price differences between venues get competed away by many independent actors acting on their own.

Finally, most AMMs add a small fee to each swap. The fee is taken from the incoming tokens and left in the pool rather than handed back out, so the reserves grow slightly with every trade. That accumulated balance belongs to the people who supplied the tokens in the first place; the mechanics of who supplies pools and how their share is tracked are covered in how liquidity pools and LP tokens work.

Key takeaways

  • An AMM replaces a matched buyer and seller with a smart contract that holds a pool of two tokens and quotes prices by formula.
  • The constant-product rule x · y = k keeps the product of the two reserves constant through each swap (before fees).
  • Price is the ratio of the reserves, so every swap shifts the reserves and moves the price.
  • Larger trades move the price more; arbitrage between venues mechanically pulls a pool's price back toward the wider market.
  • A small swap fee stays in the pool, gradually increasing its reserves.

Next, see how liquidity pools and LP tokens work or why trades have slippage.

Disclaimer: This article is educational content only. It is not investment, financial, legal, or tax advice, and not a recommendation to buy, sell, or trade any asset. Crypto involves significant risk, including total loss of funds.