Automated Market Maker (AMM): How It Works in DeFi

Updated

The short answer

An automated market maker (AMM) is a smart contract that holds two or more tokens in a pool and sets prices with a formula instead of an order book. Traders swap against the pool, and liquidity providers who fund it earn a share of the fees. Uniswap popularized the model.

On a traditional exchange, buyers and sellers post orders and a matching engine pairs them. An AMM removes the matching: you trade against a pool of tokens, and the price comes from a formula. This is how most trading on decentralized exchanges (DEXs) works.

How an AMM works

  1. Someone creates a pool. A pool holds two tokens, for example ETH and USDC, in a smart contract.
  2. Liquidity providers fund it. They deposit both tokens and receive a receipt that tracks their share. See how liquidity pools work.
  3. A trader swaps. The trader sends one token in and takes the other out. The contract applies its formula and returns the amount.
  4. The price moves. Taking one token out makes it scarcer in the pool, so the price of what remains goes up.
  5. Fees are paid. Each swap pays a fee, which goes mostly to the liquidity providers.

The constant product formula

The best-known AMM rule is x × y = k. Here x and y are the amounts of the two tokens in the pool, and k stays constant during a swap.

Example: a pool holds 100 ETH and 200,000 USDC, so k = 100 × 200,000 = 20,000,000, and the implied price is 2,000 USDC per ETH. A trader buys 1 ETH. The pool must keep k, so with 99 ETH left it needs 20,000,000 ÷ 99 = about 202,020 USDC. The trader pays about 2,020 USDC for 1 ETH, before fees, which is more than the starting price of 2,000.

That gap between the quoted price and the price you actually get is slippage. It grows with trade size relative to pool size, so large trades in small pools get a much worse price. Read what slippage is for how to set a tolerance. For more background, see DeFi wallets, which covers how a self-custody wallet connects to apps and what approvals you are signing. For more background, see the impermanent loss calculator, which covers how a price move changes a liquidity position against simply holding.

AMM vs order book

AMM Order book
Price set by A formula and the pool’s balances Buyers’ and sellers’ orders
Counterparty The pool Another trader
Best for Any token, even new ones, with no market maker needed Deep, active markets
Main trade-off Slippage and impermanent loss for providers Needs enough orders to trade

Variations

  • Constant product pools spread liquidity across all prices, which is simple but capital-hungry.
  • Concentrated liquidity lets providers choose a price range for their funds, improving trading depth for the same capital. Uniswap v3 introduced this, and the Uniswap guide explains the versions in use.
  • Stable-asset pools use formulas tuned for tokens that should trade near the same price, such as two dollar stablecoins.

Risks

  • Impermanent loss. Liquidity providers can end up with less value than if they had simply held the tokens. See impermanent loss.
  • Slippage and bots. Prices can move between your quote and the fill, and bots can trade around your order.
  • Smart-contract risk. Bugs and exploits can drain pools. Audits lower the odds but do not remove them.
  • Fake tokens and rug pulls. Anyone can create a pool for any token. A scam token can be paired with a real one and then drained. Check the token’s contract address from an official source.
  • Token approvals. Swapping usually requires approving a contract to spend your token. Approve only what you need and revoke old approvals regularly.
  • Fake front-ends. Reach a DEX through a bookmarked official address, not a search ad or a link in a message.

Only use money you can afford to lose.

How trade size changes the price you get

Using the same pool (100 ETH and 200,000 USDC, k = 20,000,000, starting price 2,000 USDC per ETH), here is what buying different amounts costs, before the pool fee. This is an example, not a quote from any real pool.

ETH bought USDC paid Average price per ETH Worse than 2,000 by
1 about 2,020 about 2,020 about 1%
5 about 10,526 about 2,105 about 5%
10 about 22,222 about 2,222 about 11%
20 50,000 2,500 25%

Buying 10 ETH leaves 90 ETH in the pool, so the pool needs 20,000,000 ÷ 90 = about 222,222 USDC in total, and you pay the 22,222 USDC difference. If the pool charges a 0.30% fee, add about 67 USDC. The pool never runs out, because each extra ETH costs more than the last, but a trade that large moves the price a long way.

The cost from the size of your own trade is called price impact. Slippage tolerance is the limit you set on how far the final price may differ from the quote, for example because someone traded just before you.

Swapping on an AMM step by step

  1. Open the DEX from a bookmarked official address, and check the web address before connecting your wallet.
  2. Choose the token you pay with and the token you want. If it is not a well-known token, paste its contract address from an official source.
  3. Enter the amount and read the quote: expected output, price impact, the fee, the network fee and the minimum received.
  4. The first time you spend that token, your wallet asks for an approval. Prefer an amount equal to this trade over “unlimited”.
  5. Confirm the swap. If the price moves past your slippage tolerance before the swap is included in a block, it fails and you pay only the network fee.
  6. When you are done with the app, revoke the approval. Block explorers such as Etherscan offer a token approval checker, and many wallets show approvals too.

What can go wrong

Problem What it means What to do
Price impact shows a high percentage Your trade is large for the pool Trade a smaller amount, or look for a deeper pool
Swap fails The price moved past your slippage setting, or the gas limit was too low Retry with a fresh quote; do not raise slippage to a very high figure
You received far less than expected A thin pool, a token that takes a tax on sales, or a bot traded around you Check the pool size and the token’s contract before the next trade
You cannot sell a token A scam token built to block selling Do not send more money to “fix” it; see what a rug pull is
Wallet asks you to sign an odd message after “connect” Possible fake front-end Reject it, close the tab and reopen the DEX from your bookmark

Common questions

Who sets the price in an AMM?

The pool’s formula does, based on the ratio of tokens inside. Arbitrage traders keep it in line with other markets: if the pool’s price drifts from the wider market, they trade until it comes back.

Is an AMM the same as a DEX?

No. A DEX is the app or protocol; an AMM is one way a DEX can set prices. Some DEXs use order books instead. See what a DEX is.

What is the difference between price impact and slippage?

Price impact is how much your own trade moves the pool’s price, and it follows from the formula and the pool size. Slippage is the difference between the quote and the final fill, including price moves from other trades while yours waits to be included.

What is the receipt a liquidity provider gets?

Most AMMs give providers a token or position that records their share of the pool. It is burned to withdraw, and what comes back depends on the pool’s balance at that moment, not only on what was deposited.

Sources

  1. uniswap.org supports: The constant product market maker design (x * y = k) used by Uniswap
  2. ethereum.org supports: How decentralized exchanges and liquidity pools fit into DeFi