You open your wallet, choose a token to give, choose a token to get, and tap swap. A few seconds later your balance has changed. It feels like the app simply found someone to trade with. It did not. There was no other person on the other side of your swap, and there was no order book matching buyers to sellers. Instead, your swap was handled by a smart contract and a small piece of arithmetic that has powered most on-chain trading for years.
Understanding that arithmetic is worth a few minutes. It explains why prices move when you trade, why slippage exists, and why a large trade can cost more than a small one. None of this requires math beyond multiplication.
The old way: order books
On a traditional exchange, trading runs on an order book. Buyers post the price they are willing to pay, sellers post the price they want, and the exchange matches them when the two meet in the middle. This works well when there are many active traders, because there is almost always someone on the other side.
On a blockchain, that model is awkward. Every order placed and cancelled would be a separate transaction with its own fee, and thinly traded tokens would sit with no one to match against. So most decentralized exchanges took a different path.
The new way: liquidity pools
Instead of matching you against another trader, a decentralized exchange matches you against a pool of tokens.
A liquidity pool is a smart contract that holds a reserve of two tokens, say ETH and USDC. People called liquidity providers deposit both tokens into the pool in advance, and in return they earn a share of the fees that traders pay. When you swap, you are not trading with a person. You are adding one token to the pool and removing some of the other.
This design is called an automated market maker, or AMM. There is no human deciding the price. A formula does.
The formula, in plain English
The most common AMM uses what is known as the constant product formula. It looks like this:
x * y = k
Here x is the amount of one token in the pool, y is the amount of the other, and k is simply their product. The rule the pool follows is that after any trade, x multiplied by y must still equal the same k as before.
Picture a pool with 10 ETH and 20,000 USDC. Multiply them and you get 200,000. That is k, and the pool will defend it.
Now suppose you want to buy ETH with USDC. You add USDC to the pool, so the USDC side grows. To keep the product at 200,000, the ETH side must shrink, and the ETH that leaves the pool is what lands in your wallet. The price you paid is just the ratio of how much you put in to how much you took out.
That is the entire engine. Add to one side, the other side gives way, and the product stays constant.
Why the price moves when you trade
Because the two sides must always multiply to the same number, every trade nudges the ratio between them, and that ratio is the price.
Buy ETH from the pool and you remove ETH while adding USDC. ETH is now scarcer in the pool relative to USDC, so the next person pays slightly more for it. Your own trade moved the price a little against you before it even completed. This is called price impact, and it is not a glitch. It is the formula doing exactly what it is designed to do.
For a small trade against a large pool, the move is tiny and you barely notice. For a large trade, or a trade against a small pool, the move can be significant.
Why pool depth matters
The size of the pool, often called its depth or liquidity, decides how much your trade disturbs the price.
A deep pool holds large reserves. Your trade is small compared to what is sitting there, so the ratio barely shifts and you get a price close to what you saw quoted. A shallow pool holds little. The same trade is now large compared to the reserves, the ratio swings hard, and you receive noticeably less than a naive quote would suggest.
This is the real reason a well known token usually swaps at a fair price while an obscure one can cost far more than expected. It is not that the obscure token is a scam, though it might be. It is that almost no one has funded its pool, so there is little depth to absorb your trade.
Where slippage fits in
The price you are quoted is a snapshot of the pool at that instant. Between the moment you tap swap and the moment your transaction is confirmed, other people may trade against the same pool and move the ratio. The price you actually get can drift from the price you saw.
Slippage tolerance is the guardrail for this. It tells the swap how far the result is allowed to drift before the transaction cancels itself rather than fill at a bad rate. Set it too tight on a busy pool and your swaps may keep failing. Set it too loose and you accept a worse fill. We cover this in more detail in our guide on swap fees and slippage.
What this means for you
You do not need to do mental arithmetic before every swap. The wallet quotes the expected result for you. But knowing the machinery underneath leads to a few habits that genuinely help.
- A surprising quote usually means a shallow pool, not a bargain. If swapping a small amount of a token gives a strangely poor rate, the pool is thin. Treat that as a caution flag, especially for tokens you do not recognize.
- Splitting a very large swap into a few smaller ones can reduce total price impact, because each piece moves the ratio less than one big trade would.
- The quoted price is a moment in time. On a fast moving or thinly traded token, expect the final result to differ a little from the preview, and let your slippage setting protect you.
Underneath the clean tap of a swap is one of the simplest ideas in crypto: two reserves, one constant, and a formula that lets anyone trade at any time without waiting for a matching buyer or seller. It is elegant, it is transparent, and once you see it, the behavior of every swap you make starts to make sense.