DeFi Automated Market Maker: Constant-Product Swap
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
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, y | Amounts of each token in the pool | tokens |
| dx, dy | Amount put in and amount received | tokens |
| phi | Fee 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 k | 1,000 × 3,000,000 | 3,000,000,000 |
| Pool price before the trade | 3,000,000 ÷ 1,000 | 3,000 |
| Amount after the fee | 50 × (1 − 0.003) | 49.8500 |
| Token B received | 3,000,000 − 3,000,000,000 ÷ (1,000 + 49.85) | 142,448.92 |
| Average price received | 142,449 ÷ 50 | 2,848.98 |
| Slippage | 1 − 2,848.98 ÷ 3,000 | 5.03% |
| Pool price after the trade | (3,000,000 − 142,449) ÷ (1,000 + 50) | 2,721.48 |
| Fee paid in token A | 50 × 0.003 | 0.1500 |
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.