How decentralized exchanges work: pools, prices and the x × y = k rule

A decentralized exchange lets you swap tokens straight from your own wallet. Instead of matching buyers with sellers, most use a pool of tokens and a formula. Here is what that formula does to the price you get.

Narrow market lane lined with stalls selling goods

Photo: “Mercado de Arusha / Arusha Market” by Noel Feans, CC BY 2.0, via Flickr (edited: cropped and resized).

Quick answer

A decentralized exchange (DEX) is a set of smart contracts that swaps tokens directly from your wallet. DEXs such as Uniswap use an automated market maker: a pool holding two tokens, priced by the formula x × y = k instead of an order book [1].

Key points

  • 1A DEX is software on a blockchain, not a company holding your funds; you trade from a wallet you control.
  • 2Automated market makers price trades from the ratio of the two tokens in a pool, using the constant-product rule x × y = k.
  • 3The bigger your trade compared with the pool, the worse your average price — this is called price impact. A minimum amount out limits how much worse than the quote you accept.
  • 4Liquidity providers supply the pool’s tokens and earn the trading fee, 0.30% per trade in Uniswap v2.
  • 5Anyone can create a pool for any token, so fake tokens, front-running bots and smart contract bugs are real risks.
On this page
  1. What is a decentralized exchange?
  2. Why don’t most DEXs use an order book?
  3. How does the constant-product formula set the price?
  4. Do all DEXs price trades with x × y = k?
  5. Who puts the tokens in the pool, and what do they earn?
  6. How does a DEX price stay in line with other markets?
  7. What happens when you press “swap”?
  8. How can you limit slippage when you swap?
  9. What are the risks of using a DEX?
  10. What mistakes do beginners make here?
  11. Frequently asked questions
  12. The bottom line
  13. Sources

What is a decentralized exchange?#

ethereum.org defines a decentralized exchange (DEX) as an Ethereum app “that lets you swap tokens with peers on the network”, noting that DEXs are not subject to the geographical restrictions of centralized exchanges [2]. The app is a collection of smart contracts: programs that live on the blockchain and run exactly as written. ethereum.org stresses that when you trade on a DEX you never give up control of your assets [3].

Federal Reserve researchers list three practical differences from a centralized exchange (CEX). A CEX can take and pay out ordinary money; a DEX cannot, which is why stablecoins serve as the dollar-like asset on DEXs. A CEX uses market makers and a limit order book; a DEX uses an automated market maker. And a CEX often checks your identity, while a DEX runs on permissionless smart contracts [4]. For a side-by-side view, see CEX vs DEX.

Centralized exchange vs decentralized exchange

Centralized exchange vs decentralized exchange: Centralized exchange (CEX): Company holds your deposits, Order book matches buyers and sellers, Often requires identity checks, Can convert to and from bank money; Decentralized exchange (DEX): Tokens stay in your wallet until the swap, Pool and formula set the price, Open to any wallet (permissionless), Crypto only — no bank moneyCentralized exchange vs decentralized exchange: Centralized exchange (CEX): Company holds your deposits, Order book matches buyers and sellers, Often requires identity checks, Can convert to and from bank money; Decentralized exchange (DEX): Tokens stay in your wallet until the swap, Pool and formula set the price, Open to any wallet (permissionless), Crypto only — no bank money
Simplified from the Federal Reserve’s comparison of CEXs and DEXs and Uniswap’s documentation.

Why don’t most DEXs use an order book?#

On a stock exchange, buyers and sellers post orders at different prices in an order book, and professional market makers keep updating them. Uniswap’s documentation argues that order books need intermediary infrastructure to host and match orders, and active market makers with sophisticated tools — features that fit poorly on a blockchain where anyone can create a token and most tokens have low liquidity [5]. Read more about the traditional model in order books and liquidity.

An automated market maker (AMM) replaces the order book with a liquidity pool: a smart contract holding reserves of two tokens. You trade against the pool itself rather than against another person’s order, and the price updates automatically as the balance of the two tokens changes [1].

How does the constant-product formula set the price?#

x × y = k

x and y are the pool’s reserves of each token. After every trade, the product k must stay the same or grow [1]. The pool’s current (marginal) price is the ratio of the two reserves [6].

When you take one token out of the pool, you must put enough of the other in to keep the product constant [7]. In Uniswap v2 a 0.30% fee is taken from the amount you pay in before the formula is checked, and the fee stays in the pool for the liquidity providers [6]. The result: small trades get a price close to the pool’s current price, while large trades push the price further against themselves [1].

StepValue
Fee kept for liquidity providers = 0.30% × 10,00030 USDC
USDC counted for the formula = 10,000 − 309,970 USDC
ETH out = 100 × 9,970 ÷ (300,000 + 9,970)≈ 3.2164 ETH
Your average price = 10,000 ÷ 3.2164≈ 3,109.03 USDC per ETH
Gap versus the 3,000 starting price≈ 3.63% worse
Pool after the trade = 310,000 USDC and ≈ 96.7836 ETHNew price ≈ 3,203.02 USDC per ETH

Your trade moved the pool’s price up by almost 7%. That gap between the price you see and the price you get is what traders call slippage; the part caused by your own trade size is called price impact.

Bigger trades, worse average prices (same hypothetical pool, 0.30% fee)
USDC paid inETH receivedAverage price (USDC per ETH)Worse than 3,000 by
1,000≈ 0.3312≈ 3,019.03≈ 0.63%
10,000≈ 3.2164≈ 3,109.03≈ 3.63%
30,000≈ 9.0661≈ 3,309.03≈ 10.30%
100,000≈ 24.9437≈ 4,009.03≈ 33.63%

Computed from x × y = k with the fee applied to the input amount. In a pool ten times deeper (1,000 ETH and 3,000,000 USDC), the same 10,000 USDC trade would be only about 0.63% worse than 3,000.

Do all DEXs price trades with x × y = k?#

No. Michael Egorov’s 2019 StableSwap paper says the constant-product rule suits assets like ETH but works poorly for coins that are meant to stay at the same price: for them, price slippage is “enormous” unless the pool holds very large funds [8]. The opposite extreme is a constant-sum rule, x + y = constant, which always prices one coin at exactly one of the other [8]. The paper notes that this does not work in a fluctuating market unless the price is adjusted all the time [8].

StableSwap is a middle ground built for stablecoin pools. Near balance it behaves almost like constant-sum, so the price changes slowly and slippage stays small; as the pool becomes lopsided it shifts toward constant-product [8]. That shift means it still offers some liquidity at any price, unlike a pure constant-sum pool [8]. A setting called the amplification coefficient (A) controls the blend: the lower A is, the closer the pool behaves to constant-product [8]. The paper used A = 100 when calculating slippage and compared that with using Uniswap with 100× leverage [8].

Three ways an AMM can price two coins
RuleFormula ideaHow the price behavesWeak point
Constant sumx + y = constantAlways exactly 1:1 [8]Breaks when the market price moves away from 1 [8]
Constant product (Uniswap v2)x × y = kMoves with every trade; big trades move it a lot [1]High slippage for coins meant to stay equal [8]
StableSwapA blend of the two, set by AStays near 1:1 while the pool is balanced [8]Works at a “suboptimal point” once the price moves away from 1 [8]

The StableSwap paper gives the full equation for any number of coins; this table shows only the idea behind it.

Who puts the tokens in the pool, and what do they earn?#

Anyone can become a liquidity provider (LP) by depositing both tokens of a pair [1]. New deposits must match the pool’s current ratio. In return, the pool mints liquidity tokens that represent the provider’s share; every trade pays a 0.3% fee that is shared pro rata among LPs, and burning the liquidity tokens returns the provider’s share of both reserves plus accumulated fees [5].

Providing liquidity is not risk-free. When prices move, the pool automatically rebalances, which can leave a provider worse off than if they had simply held the two tokens. The Uniswap v2 whitepaper calls this impermanent loss [6]. We work through the numbers in liquidity pools and impermanent loss.

How does a DEX price stay in line with other markets?#

The pool itself has no idea what ETH costs elsewhere. Arbitrage traders close the gap: if the pool’s price differs from the wider market by more than the fee, they trade against it until it lines up again. The Uniswap v2 whitepaper notes that, for this reason, the price offered by Uniswap tends to track the relative market price of the assets [6]. ethereum.org describes DEX arbitrage as the simplest and most competitive form of maximal extractable value (MEV) [9].

What happens when you press “swap”?#

  1. Pick the tokens and amount

    The app shows how much you will receive; the AMM gives instant feedback on rates and slippage [7].

  2. Approve the token (first time only)

    Many contracts must first be approved on the token contract before they can move your tokens [7]. Approvals are powerful — see token approvals.

  3. Sign the transaction

    Your wallet asks you to confirm and pay a network fee. See Ethereum gas fees.

  4. The contract checks the rule

    A router contract computes the trade and the pool contract checks the x × y = k rule; if the pool cannot be paid in full, the whole transaction is reverted [6].

  5. Tokens arrive in your wallet

    The swap is final. Interactions with smart contracts are irreversible [10].

How can you limit slippage when you swap?#

Uniswap’s guide calls its router contract the easiest way to swap tokens safely, and each router swap takes a safety limit [11]. When you sell an exact amount, the limit is the minimum amount you will receive — called amountOutMin in the code; when you buy an exact amount, it is the maximum you are willing to pay [11]. Uniswap’s guide says the limit should be worked out from a price source outside the pool, and warns that without one, trades can be front-run “for considerable loss” [11].

Why outside the pool? The guide explains that a contract which treats the current Uniswap price as “fair” is vulnerable to manipulation: a bad actor could place trades just before and after yours — a sandwich attack — so that you trade at a much worse price [11]. For people trading on their own behalf, it calls the trader’s own off-chain observation of the current price the best check to pass into the trade [11].

StepValue
Quoted amount out≈ 3.2164 ETH
Minimum amount out = 3.2164 × (1 − 0.005)≈ 3.2003 ETH
Worst average price you accept = 10,000 ÷ 3.2003≈ 3,124.71 USDC per ETH
Worse than the 3,000 starting price by≈ 4.16%

The quote already includes the fee and your own price impact; the minimum adds a margin for the price moving before your transaction is confirmed. Uniswap’s guide adds that sandwich attacks are mitigated by trading in extremely liquid pools and/or at low values [11].

What are the risks of using a DEX?#

  • Price impact and slippage in small pools, as the table above shows. A minimum amount out caps how much worse than the quote you accept [11].
  • Sandwich attacks. Bots watch for large pending DEX trades, buy just before and sell just after. ethereum.org says users who are sandwiched face increased slippage and worse execution [9].
  • Fake or worthless tokens. Uniswap’s design is permissionless: anyone can swap, provide liquidity or create new markets [1]. Nothing stops a copycat token with a familiar name.
  • Smart contract and app risk. EU supervisors warn that crypto service providers have suffered cyber-attacks and severe operational problems [12].
  • No recourse. There is no customer-support desk that can reverse a swap sent with the wrong token or amount [10].

Uniswap v2 in one box

Pricing rule
x × y = k (constant product) [1]
Trading fee
0.30% of the amount paid in [6]
LP share if the optional protocol fee is switched on
0.25% (0.05% to the protocol) [6]
Whitepaper published
March 2020 [6]
Core contracts upgradeable?
No [1]

What mistakes do beginners make here?#

  • Swapping a large amount in a small pool

    Price impact grows quickly as your trade becomes a bigger share of the pool. Check the quoted amount out before you sign.

  • Trusting a token because it appears in the app

    Pools are permissionless. Verify the contract address from an official source.

  • Approving unlimited spending without thinking

    An approval lets a contract move your tokens later. Review and revoke approvals you no longer need.

  • Forgetting the network fee

    Each swap is a blockchain transaction. On a small trade, the network fee can be larger than the price difference you were chasing.

  • Assuming a DEX is “safer” than a CEX in every way

    You avoid trusting an exchange with custody, but you take on smart contract risk, front-running and the full responsibility for mistakes.

  • Swapping without a minimum amount out

    Uniswap’s guide warns that a swap without a price check from outside the pool can be front-run for considerable loss [11]. Check the minimum you will receive before you sign.

Frequently asked questions#

Do I need an account to use a DEX?

No sign-up is needed; you connect a self-custody wallet. Uniswap describes its protocol as permissionless, with no ability to selectively restrict who can use it [1].

Can I buy crypto with dollars on a DEX?

No. DEXs swap tokens for tokens. Fed researchers note that users cannot buy or sell stablecoins for fiat currency on DEXs, which is why stablecoins are so prominent there [4].

Who sets the trading fee?

It is written into each protocol version and pool. In Uniswap v2 it is 0.30%, paid to liquidity providers [5]; Uniswap says fees are now configured by pool and protocol version [1].

Can a DEX be shut down?

Uniswap says its core contracts are not upgradeable once deployed and that no party can pause them or reverse trades [1]. Websites and front ends that people use to reach the contracts are a separate matter.

Are DEX prices the same as on big exchanges?

Usually close, because arbitrage traders keep them in line. During the March 2023 stablecoin turmoil, Fed researchers found that very different trading volumes did not lead to major price gaps between centralized and decentralized markets [4].

The bottom line#

A decentralized exchange replaces the company and the order book with a pool and a formula. That makes trading open to any wallet at any hour, but it also means the price you get depends on how big your trade is compared with the pool, and that nobody can undo your mistakes.

Next, see what liquidity providers actually earn and risk in liquidity pools and impermanent loss, or look up DEX and slippage in the glossary.

Sources#

Grade A = primary source (regulator, protocol specification, client code, original author). Grade B = expert secondary source used for explanation only.

  1. AUniswap Labs (developer documentation). How Uniswap Works, 2026.
  2. Aethereum.org. Ethereum Glossary, 2026.
  3. Aethereum.org. What is DeFi?, 2026.
  4. ABoard of Governors of the Federal Reserve System. Primary and Secondary Markets for Stablecoins (FEDS Notes), 2024.
  5. AUniswap Labs (developer documentation). Pools (Uniswap v2 concepts), 2026.
  6. AHayden Adams, Noah Zinsmeister, Dan Robinson. Uniswap v2 Core (whitepaper), 2020.
  7. AUniswap Labs (developer documentation). Uniswap v2 Swaps, 2026.
  8. AMichael Egorov. StableSwap – efficient mechanism for Stablecoin liquidity, 2019.
  9. Aethereum.org. Maximal extractable value (MEV), 2026.
  10. Aethereum.org. Introduction to smart contracts, 2026.
  11. AUniswap Labs (developer documentation). Implement a Swap (Uniswap v2 guide), 2026.
  12. AEuropean Supervisory Authorities (EBA, ESMA, EIOPA). EU financial regulators warn consumers on the risks of crypto-assets, 2022.