All coins

DeFi & staking

How a DEX Swap Works: Liquidity Pools, AMM Pricing and Fees

On a centralized exchange, buyers and sellers post orders to a book. A trade happens when a bid meets an ask. Most decentralized exchanges (DEXs) work differently. There is no order book to match. Uniswap, for example, runs as a set of…

By Dextape DeskUpdated 9 min read
Four linked blocks alternating between ivory ceramic and lavender glass.Four linked blocks alternating between ivory ceramic and lavender glass.
On this page

What you’ll learn

Automated market makers, how a pool sets the price of a swap, where the fee goes, and what price impact means for a trade.

Why it matters

An AMM prices your swap with a formula based on the pool's token balances, and arbitrage traders keep that price close to other markets.

Order books versus automated market makers

On a centralized exchange, buyers and sellers post orders to a book. A trade happens when a bid meets an ask. Most decentralized exchanges (DEXs) work differently. There is no order book to match. Uniswap, for example, runs as a set of onchain smart contracts that any wallet can use to trade tokens, deposit liquidity or launch a new market. This design is called an automated market maker, or AMM.

When you swap on an AMM, no single person takes the other side of your trade. You send one token into a pool, and the other token comes out of the pool's existing stock. The contract decides how much you get using a fixed mathematical rule, not by matching you with another trader.

What sits inside a liquidity pool

A basic pool holds two tokens. In Uniswap v2, a single pair contract looks after one pool, and that pool holds two ERC20 tokens.

The tokens come from liquidity providers, often called LPs. Providers deposit both tokens in matching value and get back LP tokens that track their slice of the pool. In return for putting their capital at risk, they earn a share of trading fees.

A newly created pool holds nothing. Whoever deposits first chooses the ratio of the two tokens, and that ratio becomes the opening price that later trades move away from.

How the constant product formula sets a price

The best-known AMM rule is the constant product formula, in which the two reserve balances multiplied together equal a constant called k. The letters x and y stand for how much of each token the pool holds, and k is an invariant: after any trade it has to stay level or grow, never fall.

Here is what that means when you swap. Your deposit raises the pool's balance of the token you sell. To keep the product from falling, the contract releases only as much of the other token as the formula allows. The pool's quoted price is simply the ratio between its two balances, and it shifts after every trade.

This gives the curve a useful property. Each purchase drains the reserve of the token being bought, so every additional unit costs more than the one before. The pool can get very expensive, but it does not run completely dry, because the price climbs steeply as a reserve shrinks.

Fees are built into the calculation too. In Uniswap v2, the contract takes a 0.30% fee off your input first, so your output is worked out from what remains after the fee.

Arbitrage keeps pool prices in line

A pool has no idea what a token trades for anywhere else. It only knows its own balances. When its price drifts away from other venues, the gap invites arbitrage trades, and those trades nudge the pool back toward the broader market.

For the same reason, big price moves on other markets show up in DEX pools quickly. Arbitrage is what ties an isolated formula to real-world prices.

Concentrated liquidity and other curve designs

The simple formula spreads liquidity across every possible price, from near zero to near infinity. Uniswap's v3 whitepaper noted that in earlier versions, only part of a pool's assets was actually working near the current price. It called this wasteful for pairs that tend to trade close to one level.

Version 3 went live on 5 May 2021 and introduced concentrated liquidity. Providers pick a band of prices for their capital instead of covering the full range. Each such position is minted as its own NFT, which records the chosen fee tier, the price band and the amount supplied. For a trader, more liquidity near the current price generally means a given swap moves the price less.

Other protocols use different curves. Balancer applies the constant product idea to pools of up to eight assets. Curve mixes constant-sum and constant-product behaviour, a design aimed at assets that should trade at about the same value, such as two dollar stablecoins.

Uniswap v4 adds another layer. A v4 pool with no hook attached behaves like v3 concentrated liquidity. Hooks are outside contracts that plug into a single pool and step in at set points. They can add features such as custom pricing logic or fee strategies that change over time.

Where the swap fee goes

Every swap pays a fee, and how providers receive it depends on the version. A v2 pool takes a fixed 0.30% from each trade and adds it to its balances. Because that raises k, providers' fee income compounds without them doing anything.

In v3, the same pair can have several pools, each charging a different rate. Uniswap's support pages list four levels per pair, ranging from 1% at the top down to 0.01%, with 0.3% and 0.05% in between. The original whitepaper launched with three of those, 1%, 0.30% and 0.05%, and UNI governance can approve extra tiers.

The way fees are paid out also changed. In v3 and v4, fees are held apart from the position, and the owner has to claim them. Only positions whose band covers the current price earn anything. When the price leaves a band, that position stops collecting until the price comes back.

Version 4 does away with fixed tiers. Pool creators can pick any rate between 0% and 100% in steps of 0.0001%, and hooks can make the fee adjust in real time. These dynamic fees can update on every swap or on a timetable, such as once per block or once a day. Traders pay them, and they go to providers. A v4 pool can also carry two other charges, a protocol fee and a hook fee, and all three can apply at once. Whether a pool can use dynamic fees is decided when it is created and cannot be changed afterwards.

The protocol fee switch

For most of Uniswap's history, the whole fee went to providers. For the first seven years, the code included a protocol cut that stayed switched off unless governance voted to turn it on. Governance approved Proposal 93, called UNIfication, in December 2025. It sends a slice of swap fees to the protocol, where the money funds UNI burns.

Traders do not pay more because of this. On a v2 pool the total charge is still 0.30%. Providers now get 0.25% of each trade, and the remaining 0.05% goes to the protocol. On v3, the protocol takes between one sixth and one quarter of what providers would otherwise earn, depending on the pool.

Coverage is still partial. As of Uniswap's current developer documentation, protocol fees apply to every v2 pool and to selected v3 pools. Votes covering v4 and other blockchains were left for later. Where v4 protocol fees do apply, they cover specific pool families and leave out pools that set their fees dynamically. Other DEXs set their own fee rules, so check the details for whichever venue you use.

Price impact: what your trade does to the pool

Price impact comes straight from the curve. A trade that is big compared with a pool's reserves pushes the price further than a small one, which fills near the spot price. Your average fill price is worse than the price shown before you traded, and the pool is left at a new price for the next trader.

One academic study of DEX trading costs measures price impact as the quoted price divided by the market mid price, minus one, expressed in basis points. The same study notes that shallower liquidity brings bigger price impact and more slippage. In practice, the same order can barely move a deep pool and badly move a thin one.

Because pool math is public, a DEX can calculate price impact before you confirm. Interfaces usually show it as an estimate next to the quote. Price impact is separate from the swap fee: you pay the fee even when impact is tiny. In very thin pools, impact can be severe. One explainer showed a PancakeSwap screen with a high price impact, a level that directly wipes out much of an order's value.

Splitting an order or using an aggregator is one common way people try to reduce impact. These approaches can route a trade across several pools, though they add their own complexity and costs.

Slippage, tolerance settings and MEV

Slippage is a related but wider idea. It measures how far the price you actually received differs from the price you were quoted. It includes your own price impact plus any movement while your transaction waits to be confirmed. Put simply, price impact is something you cause, and slippage is something that happens to you.

Most DEX interfaces let you set a slippage tolerance. It caps how far the execution price can differ from the quoted price before the trade is rejected. If the price moves past it before your trade executes, the swap reverts instead of filling at a worse level. On many networks, a reverted transaction can still cost you gas.

Other activity in the same block matters too. The price when your trade executes can differ from your quote because of colliding or adversarial transactions. MEV attacks are one of the causes of slippage. A common example is a bot that spots a pending swap, trades just before it and then trades again just after it. A loose tolerance gives that kind of bot more room. Higher volatility also tends to push slippage costs up.

Risks to keep in mind

Swapping on a DEX means trusting smart contracts that can have bugs, and tokens that may have thin liquidity or unusual transfer rules. Always check that you are using the right token contract, because anyone can create a pool for a lookalike token.

Leverage makes every one of these costs worse. If you move swapped tokens into a position funded with borrowed money, fees, price impact and slippage apply to the full borrowed size. Any price move against you is multiplied by the leverage, and that can wipe out your collateral quickly.

Providing liquidity has its own risks. Providers face impermanent loss, an opportunity cost caused by price swings in the assets they supplied, and these swings can get much sharper during market shocks. In concentrated pools, a position that falls out of range stops earning fees.

Key takeaways

An AMM prices your swap with a formula based on the pool's token balances, and arbitrage traders keep that price close to other markets.

The swap fee mostly goes to liquidity providers, and since Uniswap's December 2025 UNIfication vote, part of it goes to the protocol on all v2 pools and selected v3 pools.

Price impact is the price move your own trade causes, and it gets larger as your order grows relative to the liquidity in the pool.

Slippage covers price impact plus any movement before your trade confirms, and your slippage tolerance sets the point at which a swap reverts.

Thin pools, MEV bots, smart contract bugs and borrowed money can all make your losses larger, so check the quoted impact and your settings before you confirm any swap.

Machine-written from the sources listed and checked against them · How Dextape writes

Sources

Finished reading?

Mark it complete to track your progress through the learning paths.