Back to Learn
Lesson 13 of 31 · Swap

How swap routing works

A single pool rarely gives the best price. Routing is the search for the path that leaves the most tokens in your wallet after fees and gas.

When you make a swap, a single liquidity pool rarely offers the best possible price. Routing is the process of finding the optimal path between your input token and your output token, splitting the order across one or more routes to maximize what you receive.

A route can be direct (ETH → USDC) or multi-hop (ETH → WBTC → USDC), when passing through an intermediary token yields a better price. Routers also perform order splitting — dividing the same order into fractions that travel different routes in parallel. Because price impact in an AMM is non-linear (the price worsens as an order grows within a single pool), splitting the order reduces the average impact.

Technically, a router solves an optimization problem over a graph: each token is a node, each liquidity pool is an edge, and each edge's weight is the effective price in that pool given its liquidity depth and the slippage your order will cause.

The three costs a router weighs

  • Price impact — how much your own order moves the pool's price.
  • Pool fees — for example 0.05%, 0.30%, or 1%, depending on the pool's tier.
  • Gas — the cost of executing extra hops.

A route with more hops may show a better gross price, but if the gas for two extra hops exceeds the gain, the router prefers the shorter route. That is why the best path is always the one with the highest net output, after fees and gas — not the best headline price.

Execution protection

Finally, execution protection kicks in. Your slippage tolerance sets the minimum acceptable price, and the minimum-output parameter makes the transaction revert if the price moves beyond your tolerance between the quote and on-chain confirmation. This protects you from executing at a much worse price than you were quoted.

Why it matters. Routing is what makes a fragmented market usable. Liquidity in DeFi is scattered across thousands of pools on many chains; without a router doing the graph search for you, you'd manually hunt for the best path on every trade. The quality of the router is a large part of the quality of the price you actually get.

Try it in the app

Open Cypay and put this lesson into practice.

Open Cypay