⚠ Trading involves significant risk of loss. Everything on this site is for education only and is not financial advice.
Lesson 4 of 13 · Free

Decentralised Exchanges and Automated Market Makers

See how an automated market maker prices a swap with the constant-product formula, and calculate fees, price impact and safe slippage settings.

Author: Moustafa HijaziContent reviewed: 2026-10-305 min readSelf-study course

What you will learn

  • What a decentralised exchange (DEX) is and how it differs from a centralised exchange.
  • How the constant-product formula x × y = k sets prices.
  • How to calculate the tokens you receive, the average price and the price impact.
  • How slippage tolerance and MEV (maximal extractable value) affect your trade.

Explanation

A centralised exchange (CEX) holds your funds and matches orders in an order book (order books are covered in the course "Order Books, Liquidity & How Orders Are Filled"). A decentralised exchange (DEX) is a set of smart contracts. You keep custody in your own wallet until the moment of the swap, and the trade settles on-chain in one transaction.

Automated market makers

Most DEXs use an automated market maker (AMM). Instead of buyers and sellers placing orders, a liquidity pool holds two tokens, for example ETH and USDC. Anyone can trade against the pool. The classic rule is the constant-product formula:

x × y = k, where x = amount of token A in the pool, y = amount of token B, and k stays constant during a swap (before fees).

The pool price of A in units of B is y ÷ x. When you buy A, you add B and remove A, so A becomes scarcer in the pool and its price rises. The larger your trade compared to the pool, the worse your average price. This difference between the pool price before the trade and your average price is called price impact.

Fees

Each swap pays a swap fee to the pool, for example 0.3% in the classic design. Newer designs offer several fee levels (such as 0.05% for stable pairs or 1% for exotic tokens). The fee is taken from the input amount before the formula is applied. Gas is paid on top (Lesson 1).

Arbitrage keeps prices aligned

A pool does not know the "market price". If the pool price drifts away from prices elsewhere, arbitrageurs trade against it until it matches again. This keeps DEX prices close to other markets, but it also creates the costs for liquidity providers you will calculate in Lesson 5.

Slippage tolerance and MEV

Slippage tolerance is the maximum worse price you accept between quote and execution, for example 0.5%. If the result would be worse, the transaction fails (you still pay gas). MEV (maximal extractable value) is profit that block builders and bots can take by ordering transactions. A common form is a sandwich attack: a bot sees your pending buy, buys just before you (raising the price), lets your trade execute at the worse price, and sells right after. A high slippage tolerance invites this. Some wallets offer private transaction submission to reduce this risk.

Other DEX designs

Concentrated liquidity lets providers place liquidity only within a price range, which gives deeper markets near the current price. Aggregators split a trade across several pools to reduce price impact. The formula above is the clearest model to understand them.

Worked example

Hypothetical example: a pool holds 100 ETH and 200,000 USDC. Pool price = 200,000 ÷ 100 = 2,000 USDC per ETH. k = 100 × 200,000 = 20,000,000. Swap fee 0.3%. Gas, MEV and taxes are excluded.

  1. You swap 10,000 USDC for ETH. Fee: 0.3% × 10,000 = 30 USDC, so 9,970 USDC enters the formula.
  2. New USDC in pool: 200,000 + 9,970 = 209,970.
  3. New ETH in pool: 20,000,000 ÷ 209,970 = 95.2517 ETH.
  4. ETH you receive: 100 − 95.2517 = 4.7483 ETH.
  5. Average price: 10,000 ÷ 4.7483 = 2,106.02 USDC per ETH, which is 5.3% above the starting pool price.
  6. New pool price: 209,970 (plus the 30 USDC fee, which stays in the pool) ÷ 95.2517 ≈ 2,204.69 USDC. Arbitrageurs will now sell ETH into the pool until it matches other markets.

Comparison: a swap of only 1,000 USDC would give 0.4960 ETH, an average of 2,016.02 USDC, just 0.8% above the pool price (0.3% fee plus 0.5% impact). Trade size relative to pool depth matters a lot.

Common mistake

Setting a very high slippage tolerance "so the trade goes through"A 10% tolerance tells every bot that you accept a price up to 10% worse. Use the lowest tolerance that works for normal conditions, split large trades, check the expected price impact in the interface, and check that the token contract address is the real one, not a copy.

Practice activity

On paper, using the same starting pool (100 ETH, 200,000 USDC, 0.3% fee): you sell 5 ETH. Calculate (a) the USDC you receive, (b) your average price, (c) the price impact versus the starting price of 2,000 USDC.

Show solution

(a) ETH entering the formula: 5 × 0.997 = 4.985. New ETH in pool: 104.985. New USDC: 20,000,000 ÷ 104.985 = 190,503.41. You receive 200,000 − 190,503.41 = 9,496.59 USDC.

(b) Average price: 9,496.59 ÷ 5 = 1,899.32 USDC per ETH.

(c) (1,899.32 − 2,000) ÷ 2,000 = −5.0%, of which about 0.3% is the fee and the rest is price impact.

Limitations: real pools may use concentrated liquidity or different fee levels, so impact can be smaller or larger.

Key takeaways

  • An AMM prices trades with a formula; the classic one is x × y = k.
  • Price impact grows with trade size relative to pool depth.
  • Fees come from the input amount; gas comes on top.
  • Keep slippage tolerance tight to limit MEV and sandwich attacks.

Next step

In the next lesson you switch sides and become a liquidity provider, and you will calculate impermanent loss.

Self-check

Answer the questions, then check your answers. You can retry as often as you like.

1. A pool holds 200 ETH and 400,000 USDC. Ignoring fees, how much USDC do you receive for selling 4 ETH?

2. Why does a large swap on an AMM get a worse average price than a small one?

3. You set a 10% slippage tolerance on a swap in a popular pool. What is the main risk?

Automatic self-check for your own revision. Your answers are checked in your browser and are not seen or assessed by us. It is not an exam and not a qualification.

Your progress

“Complete” only means you marked the lesson as done. It is not a test of your understanding. Progress and notes are saved only in this browser on this device — not in an account.