No history yet

Constant Product Foundations

The Constant Product Rule

The engine of most traditional Automated Market Makers (AMMs) is a surprisingly simple mathematical rule. It’s called the constant product formula, and it ensures that a liquidity pool can always make a price for a trade, no matter the size.

xy=kx \cdot y = k

The key is that the total liquidity, kk, only changes when liquidity providers add or remove funds from the pool. During a trade, kk must stay constant. This means if a trader increases the amount of token xx in the pool, the AMM must decrease the amount of token yy to keep the product of the two reserves the same.

Imagine a seesaw. If one side goes up, the other must go down to stay balanced. The constant product formula works in a similar way for token balances.

Calculating a Swap

Let's see how this works with a real example. Consider a v2 pool with 1,000 ETH (token xx) and 2,000,000 USDC (token yy).

First, we calculate our constant, kk: 1,0002,000,000=2,000,000,0001,000 \cdot 2,000,000 = 2,000,000,000

Now, a trader wants to swap 10 ETH for USDC. Before we calculate the swap, we must account for the trading fee. The standard fee is 0.3%, which is taken from the input amount. This fee is added back into the pool, increasing kk slightly and rewarding liquidity providers.

Fee = 10 ETH * 0.003 = 0.03 ETH
Amount for Swap (Δx) = 10 ETH - 0.03 ETH = 9.97 ETH

The 9.97 ETH is added to the pool. The new ETH reserve becomes: xnew=1,000+9.97=1,009.97x_{new} = 1,000 + 9.97 = 1,009.97 ETH

Now, we solve for the new USDC reserve, ynewy_{new}, keeping kk constant: 1,009.97ynew=2,000,000,0001,009.97 \cdot y_{new} = 2,000,000,000 ynew=2,000,000,000/1,009.971,980,256.84y_{new} = 2,000,000,000 / 1,009.97 \approx 1,980,256.84 USDC

The amount of USDC the trader receives is the difference between the old and new USDC reserves: 2,000,0001,980,256.84=19,743.162,000,000 - 1,980,256.84 = 19,743.16 USDC

The effective price of this trade was about $1,974.32 per ETH (19,743.16/1019,743.16 / 10). Notice this is lower than the pool's initial price of $2,000 per ETH (2,000,000/1,0002,000,000 / 1,000). This change is due to price impact.

Price Impact and Slippage

Price impact is the effect a trade has on the price of assets in the pool. Because the formula is a curve, not a straight line, larger trades will always result in a worse execution price. The more you buy, the more expensive each subsequent unit of the token becomes.

This is a fundamental property of the xy=kx \cdot y = k model. The change in price is not linear. It accelerates as the trade size grows relative to the pool's total liquidity.

This effect is less pronounced in pools with deep liquidity. If our example pool had 100,000 ETH and 200,000,000 USDC, the same 10 ETH trade would have a much smaller price impact because it represents a tiny fraction of the total reserves.

Slippage is related but different. It's the difference between the price a trader expects to get when they submit their transaction and the price they actually get when the transaction is confirmed on the blockchain. This can happen if other trades occur in the same block, changing the pool's reserves right before your trade executes.

Understanding this core mechanic is crucial. It dictates how liquidity moves, how prices are discovered in a decentralized way, and why arbitrage is the force that keeps AMM prices in line with the broader market.

Quiz Questions 1/6

What is the primary purpose of the constant product formula, xy=kx \cdot y = k, in a traditional Automated Market Maker (AMM)?

Quiz Questions 2/6

In a liquidity pool using the xy=kx \cdot y = k formula, a trader sells a large amount of token X to receive token Y. What is the immediate effect of this trade?