Skip to main content

DeFi Automated Market Maker: Constant-Product Swap

IntermediateFintech & Digital FinanceAlso known as: DeFi, Uniswap, x*y=k, slippage, price impact, liquidity pool

Finds: what a swap pays and how far it moves the price.

When to use it: Use it to understand how a decentralized exchange sets prices without an order book. The price moves with every trade along a curve, so a large trade relative to the pool gets a worse average price. Liquidity providers earn the fee but face the loss shown in the next entry.

The formula

x y\displaystyle x\,y=k\displaystyle =kΔy\displaystyle \Delta y=y−kx+Δx(1−ϕ)\displaystyle =y-\frac{k}{x+\Delta x(1-\phi)}Slippage\displaystyle \text{Slippage}=1−Δy/Δxy/x\displaystyle =1-\frac{\Delta y/\Delta x}{y/x}

In words: A pool holds x of token A and y of token B and keeps x times y constant; Selling dx of A (after a fee share phi) pays out dy = y − x y / (x + dx x (1 − phi)); Pool price before = y / x; Average price paid = dy / dx; Slippage = 1 − average price / pool price

What each symbol means

x, yAmounts of each token in the pooltokens
dx, dyAmount put in and amount receivedtokens
phiFee charged by the pool (kept by liquidity providers)percent

Worked example

A pool holds 1,000 ETH and 3,000,000 of a stablecoin. A trader sells 50 ETH. The fee is 0.3%.

Constant k1,000 × 3,000,0003,000,000,000
Pool price before the trade3,000,000 ÷ 1,0003,000
Amount after the fee50 × (1 − 0.003)49.8500
Token B received3,000,000 − 3,000,000,000 ÷ (1,000 + 49.85)142,448.92
Average price received142,449 ÷ 502,848.98
Slippage1 − 2,848.98 ÷ 3,0005.03%
Pool price after the trade(3,000,000 − 142,449) ÷ (1,000 + 50)2,721.48
Fee paid in token A50 × 0.0030.1500
The answer

The pool price before the trade is 3,000. The trader receives 142,448.92, an average price of 2,848.98, which is 5.03% worse than the starting price. After the trade the pool price is 2,721.48, and the fee paid is 0.1500 ETH.

Want every formula on a few pages?Get the free Cheat Sheet (PDF) →

Calculator

Pool price before the trade3,000
Token B received142,448.92
Average price received2,848.98
Slippage5.03%
Pool price after the trade2,721.48

Keep going