This website uses cookies

Read our Privacy policy and Terms of use for more information.

Swapping on a decentralized exchange

Lesson 2 of 6
By the end of this lesson you’ll be able to work out what a swap on an automated market maker will cost you before you make it, set a slippage limit that protects you, and spot the three ways a swap goes wrong.
A collage. A huge hippopotamus hangs in a canvas sling, lowered onto a small stone birdbath. Its hind feet are in the bowl, and the water bursts out in torn sheets of deep blue.

Where a swap’s price comes from

A decentralized exchange of the kind most people use has no order book and nobody quoting prices. It has pools. Each pool holds two tokens that liquidity providers deposited, and a formula sets the price from the balance between them.

The simplest formula is constant product: multiply the two balances, and the answer must be the same before and after every trade.

Say a pool holds 100 ETH and 200,000 USDC. The product is 20,000,000, and the pool’s price is 200,000 divided by 100, or $2,000 an ETH. You put USDC in and the pool gives ETH out, keeping the product at 20,000,000.

100 ETH
One side of the pool
200,000 USDC
The other side
$2,000 an ETH
The pool’s price
The pool before any trade.

The more you take, the less ETH remains, so each further unit costs more. That rising cost is called price impact, and it’s the first thing to understand about a swap.

Continue with The Crossover Pro

The rest of this story, plus two paid editions a week. $24.99 a month, or $249 a year. Already a member? Sign in above.

Subscribe to Pro