DeFi & Web3

DEXs & Automated Market Makers

How you can swap tokens with no order book and no middleman.

A decentralized exchange (DEX) is a protocol that lets users swap tokens directly from their own wallets, without depositing funds into a centralized platform or relying on a company to match buyers with sellers. Most modern DEXs achieve this through an automated market maker (AMM) — a smart contract that replaces the traditional order book with a mathematical formula and pools of deposited tokens.

Understanding how AMMs work demystifies a large slice of decentralized finance and explains why DeFi behaves so differently from the exchanges you may already know.

Why order books don’t translate well on-chain

A traditional exchange matches a buyer’s bid against a seller’s ask in real time. That works smoothly on centralized servers, but on a blockchain every action costs gas and takes time to confirm. Maintaining thousands of live orders, updating them tick by tick, and settling each match as an on-chain transaction would be prohibitively expensive and slow.

Centralized exchanges solve this by keeping the order book off-chain and only settling final balances on a ledger. A DEX, by contrast, wants every step — custody, matching, and settlement — to happen on-chain so users never give up control of their keys. The AMM model was the breakthrough that made this practical.

Liquidity pools: the engine under the hood

Instead of a list of open orders, an AMM holds a liquidity pool: a smart contract that stores reserves of two (or more) tokens. Anyone can deposit equal value of both tokens to become a liquidity provider (LP) and earn a share of trading fees. Traders swap against the pool rather than against another human being.

Because the pool is always there and always willing to trade, the AMM guarantees continuous liquidity — no waiting for a counterparty to show up.

The constant-product formula

The most widely adopted AMM design — popularized by Uniswap — governs prices with a simple rule:

x * y = k — the product of the two token reserves must remain constant after every swap (ignoring fees).

If the pool holds x units of Token A and y units of Token B, then k is fixed. When a trader sends Token A into the pool, x increases, so y must decrease to keep the product equal to k. The amount of Token B the trader receives is determined purely by the formula, not by a human quoting a price.

A concrete walk-through:

Pool stateToken A reservesToken B reservesk
Before swap1,0001,0001,000,000
Trader sends 100 A1,100~9091,000,000
Trader receives~91 B

Notice that the effective exchange rate (91 B for 100 A) is slightly worse than a naive 1:1. That gap is price impact — the larger the trade relative to pool size, the more the formula moves against the trader.

Slippage and price impact

Slippage is the difference between the price you expected and the price you actually got. It has two sources on a DEX:

  1. Price impact — your own trade moves the pool’s ratio.
  2. Market movement — other transactions confirmed between the moment you submitted and the moment your transaction was mined.

DEX interfaces let you set a slippage tolerance (often 0.5%–1% for liquid pairs). If the final price moves beyond that threshold before your transaction confirms, the smart contract reverts it and you only lose the gas you spent.

Large trades in shallow pools suffer high price impact. Deeper pools (more total liquidity) absorb large trades with less movement. This is why total value locked (TVL) matters: it is a rough proxy for how efficiently a pool can handle size.

Concentrated liquidity

The original constant-product design spreads LP capital across every possible price from zero to infinity — most of it sitting idle at prices the asset never trades near. Uniswap v3 introduced concentrated liquidity, letting LPs choose a specific price range within which their capital is active.

An LP who believes a token will trade between $1,800 and $2,200 can deploy all their capital inside that band. While the price stays in range, they earn fees as if they held a much larger position. If the price exits their range, their position stops earning and they hold only the worse-performing token — a real risk to understand before providing liquidity.

Fees and how LPs get paid

Every swap charges a small fee (commonly 0.01%–1% depending on the pool’s tier and expected volatility). That fee is distributed proportionally to LPs based on their share of the pool. Over time, accumulated fees can offset or exceed the impermanent loss that LPs face when relative token prices shift.

Fee income is automatic — it accrues in the pool and LPs claim it by withdrawing their position. Many protocols also distribute a governance token on top of trading fees, though those incentives can change and should not be the sole reason for providing liquidity.

How a swap actually executes

When you click “swap” on a DEX interface, the flow looks roughly like this:

  1. Your wallet signs a transaction calling the pool’s contract.
  2. The contract checks your slippage tolerance.
  3. Tokens move atomically: your input tokens enter the pool, your output tokens leave — in the same transaction, with no intermediate custody step.
  4. The new reserves satisfy the invariant formula. The transaction is settled on-chain.

Because it is all handled by open smart contracts, anyone can audit exactly what the code does, and no company can block your trade, freeze your funds, or require identity verification.

Risks to keep in mind

DEXs remove counterparty risk but introduce new ones:

  • Smart contract bugs — a flaw in the pool contract can be exploited, draining funds. High-profile hacks have drained hundreds of millions from AMMs.
  • Impermanent loss — your LP position can be worth less than simply holding the two tokens, depending on how prices move.
  • Front-running and MEV — bots watch pending transactions in the mempool and can insert trades ahead of yours to capture value.
  • Fake tokens — anyone can create a token with any name. Verify contract addresses before swapping an unfamiliar asset.

Understanding these risks is not a reason to avoid DEXs, but it is a reason to use them thoughtfully.

Key takeaways

  • A DEX lets you swap tokens directly from your wallet using smart contracts, with no central intermediary holding your funds.
  • AMMs replace order books with liquidity pools governed by a mathematical formula (most commonly x * y = k).
  • Price impact increases as trade size grows relative to pool depth; slippage tolerance protects you from confirming a trade at an unexpected price.
  • Concentrated liquidity lets LPs earn fees more efficiently within a chosen price range, but exits that range if the price moves too far.
  • LP fees can be attractive, but impermanent loss and smart contract risk are real costs that must be weighed.
  • Because all logic lives in auditable contracts, DEXs are permissionless — but that openness also means no recourse if something goes wrong.

Next up: Liquidity Pools and Yield Farming