Hashtag Web3 Logo

How Decentralized Exchanges (DEXs) Work

8 min
intermediate

The Order Book vs. The AMM

Traditional exchanges (like the NYSE or Coinbase) use an order book. Buyers state what they are willing to pay, sellers state what they will accept. When prices match, a trade happens. This requires active market makers to ensure there is always someone to trade with.

Decentralized Exchanges (DEXs) like Uniswap use an Automated Market Maker (AMM). There is no order book. You trade directly against a smart contract holding a pool of tokens.

Order Book (Centralized) Sell at $101 Sell at $100 Buy at $99 Buy at $98 AMM (Decentralized) Liquidity Pool 100 ETH 200,000 USDC User gives ETH User gets USDC

Liquidity Pools

A DEX cannot work without tokens to trade against. This is where Liquidity Providers (LPs) come in.

Anyone can be an LP. You deposit an equal value of two tokens (e.g., $1000 of ETH and $1000 of USDC) into a smart contract pool. In exchange, every time someone trades using that pool, a small fee (e.g., 0.3%) is collected and given to the LPs.

The Constant Product Formula (x * y = k)

How does the smart contract know what price to charge if there is no order book? Uniswap uses a simple, elegant formula: x * y = k.

  • x = amount of token A in the pool
  • y = amount of token B in the pool
  • k = a constant number

The pool must always maintain the constant k.

Example:

  1. A pool has 10 ETH and 20,000 USDC. (10 * 20,000 = 200,000). The constant k is 200,000.
  2. You want to buy 1 ETH from the pool.
  3. The pool will now only have 9 ETH.
  4. To keep k at 200,000, the USDC balance must become 200,000 / 9 = 22,222.22.
  5. The pool currently has 20,000 USDC. So you must pay the difference: 2,222.22 USDC.

This automatically prices the asset based on supply and demand. If a token becomes scarce in the pool, its price goes up exponentially.

Slippage

Because the formula changes the price as the token ratios change, buying a large amount of a token from a small pool will move the price significantly. This difference between the expected price and the execution price is called slippage.

High liquidity (large pools) = low slippage. Low liquidity (small pools) = high slippage.

Impermanent Loss

Being an LP carries a unique risk called Impermanent Loss (IL).

If you deposit ETH and USDC into a pool, and the price of ETH doubles, traders will buy ETH from the pool (using USDC) until the pool's ratio matches the market price. When you withdraw your liquidity, you will have less ETH and more USDC than you started with.

Compared to just holding the ETH and USDC in your wallet, you effectively lost some potential profit. It is "impermanent" because if the price returns to what it was when you deposited, the loss disappears. If you withdraw, the loss becomes permanent.

Key takeaways

  • DEXs replace order books with Automated Market Makers (AMMs) and liquidity pools.
  • Prices are set mathematically by the ratio of tokens in the pool (x * y = k).
  • Liquidity Providers (LPs) supply the tokens and earn trading fees.
  • Slippage occurs when large trades move the pool's ratio.
  • Impermanent loss is a risk LPs face when token prices diverge.

Quiz: How Decentralized Exchanges (DEXs) Work

1 / 5

What replaces the traditional order book in a DEX?