asashai.
Explainer 12 min read

How a DEX Actually Works: AMMs, Liquidity, and Slippage

No company, no order book, no one matching buyers to sellers — yet a decentralized exchange somehow gives you a price for any trade, instantly, at any hour. This is the long, plain-English explanation of the machine that makes that possible, and every place it can bite you.

How a DEX Actually Works: AMMs, Liquidity, and Slippage

Here's a puzzle worth sitting with. You open a decentralized exchange, ask to swap one token for another, and a price appears instantly — for any amount, at three in the morning, with no company involved and nobody on the other side clicking "accept." On a normal exchange, every trade needs a matching counterparty: for you to buy, someone has to be selling at that moment. A DEX has no such person. So where does the price come from? Who are you trading against?

The answer is one of the most elegant ideas in crypto, and once it clicks, the whole world of decentralized finance stops looking like magic and starts looking like a machine you can actually reason about. This is the long version — built from the ground up, with every gear named, and every place the machine can pinch you flagged along the way.

Start with the thing a DEX replaced

To understand why the DEX design is clever, you have to understand the old way it sidestepped: the order book.

An order book is a living list of offers. Buyers post bids ("I'll buy ETH at $3,000"), sellers post asks ("I'll sell ETH at $3,005"), and the exchange's job is to match them. It's how stock markets work, how centralized crypto exchanges work, how trading has worked for centuries. It's powerful and precise. It also has a fatal weakness for a decentralized world: it needs constant human (or bot) activity to function. If nobody's posting offers, the book is empty, and an empty book means no trades and no price. Order books depend on professional market makers — firms whose whole job is to stand there quoting prices all day so the market stays liquid.

Early decentralized exchanges tried to put the order book on-chain. It was a disaster. Every order placement and cancellation was a slow, expensive blockchain transaction. Markets sat empty. Spreads were enormous. The model that works so well on a centralized exchange simply doesn't survive contact with a slow, costly blockchain. Something else was needed — a way to always have a price without always needing people to provide one.

The core idea: trade against a pool, not a person

The breakthrough was to get rid of the counterparty entirely. Instead of matching you with another trader, a DEX lets you trade against a pool of money — a shared reserve of two tokens sitting in a smart contract. This system is called an automated market maker, or AMM, and it's the engine under most decentralized exchanges.

Picture a single pool that holds two tokens. Let's use ETH and a dollar stablecoin, USDC. The pool contains some amount of each — say 10 ETH and 30,000 USDC. That pool is the market. When you want to buy ETH, you don't find a seller; you put USDC into the pool and take ETH out. When you want to sell ETH, you put ETH in and take USDC out. The pool is always open, always willing to trade. There's no waiting, no matching, no counterparty staring at a screen. You trade with the machine.

But that raises the real question: if there's no seller negotiating with you, what determines the price?

The formula that sets the price

This is the heart of it, so let's go slow. The classic AMM uses a rule sometimes called the constant product formula. It says: multiply the two reserves together, and that product must stay the same after every trade.

In our example, 10 ETH × 30,000 USDC = 300,000. Call that number the constant. The pool's job, on every single trade, is to keep that product at 300,000.

Now watch what happens when you buy. Suppose you want to buy 1 ETH. You're taking ETH out, so the pool will have less ETH — 9 instead of 10. To keep the product at 300,000, the USDC side has to rise: 300,000 ÷ 9 = 33,333. So the pool needs 33,333 USDC. It had 30,000, which means you have to put in about 3,333 USDC to buy that 1 ETH. The price you paid was roughly 3,333 USDC per ETH — higher than the 3,000 the pool started at.

Why higher? Because by buying, you made ETH scarcer in the pool, and the formula automatically raised its price. This is the genius: the price isn't set by a person or even by an outside feed. It emerges from the changing ratio of the two reserves. Buy a token and you push its price up. Sell it and you push it down. The formula is a tireless, automatic pricing machine that never needs anyone to quote anything.

An order book asks, "Who wants to take the other side of this trade?" An AMM answers, "The pool does — always — at whatever price the formula produces given what's currently inside it." Replacing the matched human counterparty with a deterministic formula is the single idea that made on-chain trading actually work.

This same mechanism is what powered the explosion of decentralized finance, and it traces back to the protocol that popularized it — the full origin story is in Uniswap, the DEX that rewired finance.

Where the money comes from: liquidity providers

We've established that you trade against a pool. But who fills the pool with 10 ETH and 30,000 USDC in the first place? This is the second pillar, and it's just as clever as the first.

The pool is funded by liquidity providers — abbreviated LPs — and they can be anyone. An LP deposits an equal value of both tokens into the pool (say, some ETH and a matching dollar amount of USDC) and, in exchange, earns a slice of the trading fees that everyone pays when they trade against that pool. Every swap charges a small fee — often around 0.3%, though it varies — and that fee gets distributed to the LPs in proportion to how much of the pool they supplied.

Sit with how radical that is. On a traditional exchange, market making is a privileged business reserved for sophisticated firms with special access. The AMM model tore that gate down. Now your neighbor, with two tokens and a wallet, can be a market maker and earn the fees, passively, while they sleep. Thousands of ordinary people pooling their tokens together replaced the handful of professional firms. That's why DEXs can have deep liquidity for tokens no professional firm would ever bother quoting — and why a brand-new project can have a working market the instant it launches, no listing application required.

LPs receive a special token representing their share of the pool, which they can later redeem to withdraw their deposit plus their accumulated fees. Provide liquidity, earn fees, withdraw whenever you like. On the surface, it sounds like free money for holding tokens you already own. It is not free, and the reason why is the most important catch in all of DeFi.

Slippage: how your own trade moves against you

Before we get to that catch, there's a practical reality every DEX user feels immediately: slippage.

Remember our 1 ETH purchase? The pool started pricing ETH at 3,000, but you actually paid around 3,333. That gap — between the price you saw and the price you got — is slippage, and it happens because your own trade moved the pool's price as it executed. The bigger your trade relative to the size of the pool, the more violently you move the price against yourself.

This is the defining behavior of AMMs, and it has two big consequences. First, small pools are dangerous for large trades. If a pool only holds a little liquidity and you try to push a big order through it, you'll move the price enormously and get a brutal rate. Trade $10,000 against a $50,000 pool and you might lose a painful chunk to slippage alone. Second, this is exactly why thinly-traded tokens are so risky: a tiny pool means you can buy in at one price and discover that selling even a modest amount craters the price you can get out at. It's a close cousin of the trap described in how to spot a honeypot token before you buy — sometimes you can technically sell, but the slippage is so severe it barely matters.

Every DEX lets you set a "slippage tolerance" — the maximum price movement you'll accept before the trade auto-cancels. Set it too high and you invite disaster: on a thin or manipulated pool, a wide tolerance lets your trade fill at a terrible price, and it's the exact gap that sandwich bots exploit. Set it as tight as the pool's liquidity allows. If a trade keeps failing because of slippage, that's the pool telling you it's too thin for the size you're attempting — listen to it.

The bots that hunt loose slippage settings are part of a broader phenomenon — your transaction sitting publicly in the queue, waiting to be exploited — explained in what MEV is and why your trade pays an invisible tax.

Impermanent loss: the catch that traps liquidity providers

Now the catch. It has an unhelpfully gentle name — impermanent loss — for something that can quietly cost real money. We'll keep it intuitive; the full treatment is in liquidity pools and impermanent loss explained without the headache.

Here's the core problem. The AMM formula automatically rebalances the pool with every trade. If the price of one token in the pool rises sharply against the other, the formula has been busy selling the rising token and buying the falling one the whole way — because that's what keeping the product constant requires. The pool always ends up holding more of whichever token went down and less of whichever went up.

The result: when you withdraw your liquidity after prices have moved apart, you get back a different mix than you put in, and that mix is worth less than if you'd simply held the two tokens in your wallet and done nothing. The wider the prices diverge, the bigger the gap. That gap is impermanent loss.

Why "impermanent"? Because if the prices happen to drift back to where they started, the loss disappears. But there's no rule that says they will, and if you withdraw while they're apart, the loss becomes very permanent indeed. The trading fees you earned as an LP are meant to compensate you for this risk — and in a busy, stable pool they often more than do. But in a volatile pair, impermanent loss can swallow the fees and then some, leaving an LP who "earned yield all year" worse off than someone who lazily held the same two tokens. Providing liquidity is not passive free money. It's a real trade with a real risk, and anyone doing it should understand that risk cold, the same way they'd scrutinize any other strategy promising yield.

The newer designs (and why they exist)

The basic constant-product pool is beautiful but blunt — it spreads liquidity across every possible price, even prices a token will never realistically reach, which wastes most of it. Newer AMM designs let liquidity providers concentrate their funds within a chosen price range, so their capital does far more work where trading actually happens. Other specialized designs exist for assets that should always trade near the same value, like two different dollar stablecoins, using formulas tuned to keep slippage tiny around the peg.

You don't need the math to use a DEX. But knowing these variants exist explains why some pools feel deep and cheap to trade in while others punish you with slippage: it often comes down to how the liquidity is shaped, not just how much there is.

The risks the machine won't protect you from

The AMM mechanism is sound. The major protocols running it are among the most battle-tested code in crypto. And yet people lose money on DEXs constantly — almost never because the math failed, and almost always because of the things the math was never designed to guard against:

None of these are failures of the AMM. They're the bill that comes with self-custody and permissionlessness: total freedom, total responsibility. The protocol gives you a fair price and asks nothing about whether the trade is wise.

Why this design changed everything

Step back and the achievement is enormous. The AMM took the two hardest problems of running a market — needing a counterparty for every trade, and needing professional firms to provide liquidity — and dissolved both with a single mechanism. A formula replaced the matchmaker. A crowd of ordinary token-holders replaced the market makers. The result is a market that runs itself, twenty-four hours a day, for any pair of tokens anyone cares to create, with no company in the middle.

That's why DEXs are the foundation the rest of DeFi stands on. Lending protocols, yield strategies, on-chain trading of every kind — they all assume that liquidity exists and prices can be found on-chain, and the AMM is what makes that assumption true.

The honest bottom line

A decentralized exchange is not magic, and it's not a normal exchange with the company removed. It's a genuinely different machine: pools instead of order books, a formula instead of a counterparty, a crowd instead of market makers. Understand the formula and slippage stops being mysterious. Understand liquidity provision and impermanent loss stops being a nasty surprise. Understand that the protocol's correctness says nothing about a token's honesty, and you'll sidestep the way most people actually lose money here.

Learn the machine, and the DEX becomes one of the most powerful tools in crypto — open to anyone, owned by no one, and remarkably hard to break. Use it without understanding it, and the same openness that makes it powerful will hand you a fair price on the worst decision of your week, instantly, no questions asked.

Frequently asked questions

A typical DEX uses an automated market maker. Each market is a pool holding two tokens, and a formula sets the price based on the ratio of the two reserves. Every trade changes that ratio, which automatically moves the price. No buyer needs to be matched with a seller.

Slippage is the difference between the price you expect and the price you actually get, because your own trade moves the pool's price as it executes. Bigger trades against smaller pools cause more slippage. You can set a slippage tolerance to cancel a trade if it would move too far.

From liquidity providers — ordinary users who deposit a pair of tokens into the pool. In return they earn a share of the trading fees from everyone who trades against that pool. Anyone can become one, which is how DEXs get liquidity without professional market makers.

It's the gap between what a liquidity provider ends up with versus simply holding the two tokens. When the prices of the pooled tokens move apart, the pool's automatic rebalancing leaves the provider with less value than holding would have. Fees can offset it, but not always.

The mechanics are sound and the major protocols are well audited, but DEXs put full responsibility on you: trading scam tokens anyone can list, signing malicious approvals, slippage on thin pools, and smart contract bugs are all real risks the protocol won't shield you from.

Keep reading

Popular this week

  1. 01The Hidden Costs of Leverage: Funding, Fees, and SpreadsExplainer · 5 min
  2. 02Grid Trading Bots Explained, and When They Blow UpGuide · 4 min
  3. 03Where AI and Crypto Actually Meet: Compute, Data, and DePINAnalysis · 9 min
  4. 04Rug Pulls Hall of Shame: Famous Crypto Exit ScamsAnalysis · 4 min
  5. 05What Moves Crypto Prices? The Forces Behind the VolatilityAnalysis · 3 min