Home/Solana 101

How a Swap Works: AMMs, Order Books and Aggregators

· Solana 101 · by the ROCKY on SOL desk

What happens when you swap SOL for a token: liquidity pools, the constant-product formula, slippage, price impact and the aggregators that route trades.

An illuminated currency exchange rates board on a wall
An illuminated currency exchange rates board on a wall. Photo: Dvortygirl, CC BY-SA 4.0, via Wikimedia Commons.

The last step of the old "how to buy" instructions was a single sentence: swap SOL for the token on a decentralised exchange or aggregator. A lot happens inside that sentence, and knowing what happens explains why the price you get is often not the price you saw.

Liquidity pools

An automated market maker (AMM) holds two assets in a pool. The simplest and most common design keeps the product of the two balances constant: if the pool holds x SOL and y tokens, then x × y stays the same after every trade (fees aside). When you add SOL, the pool must give out enough tokens to keep the product unchanged. The bigger your trade relative to the pool, the worse the rate.

Price impact and slippage

  • Price impact is the move your own trade causes in a pool. In a small pool, even a modest swap can shift the price by several percent.
  • Slippage is the difference between the quote you saw and the price you received, caused by other trades landing first. Your slippage tolerance setting is the worst result you agree to accept.

A very high slippage tolerance invites trouble. Bots watch for transactions that accept a bad price and trade around them, a pattern known as a sandwich.

CostWho receives itTypical size
Network feeValidatorsA tiny fraction of a cent, more with priority fees
Pool feeLiquidity providersOften a fraction of a percent
Price impactThe pool's other sideSmall to very large, depending on pool depth
SlippageWhoever traded firstUp to your tolerance setting

Order books

Solana also has on-chain order books, where buyers and sellers post prices and wait to be matched, just as on a traditional exchange. They suit larger, more liquid markets; brand-new tokens almost always start in AMM pools.

Aggregators

An aggregator does not hold liquidity itself. It checks many pools and order books, then splits your trade across several routes to get a better overall price. Aggregators are why a swap on Solana can show three or four hops for what looks like a simple trade.

Spread, fee, edge

Every swap carries small costs that add up, much as a casino's edge adds up over many bets. The comparison is closer than it sounds, and we lay it out side by side in house edge, slippage and swap fees.

Quick answers