DeFi Arbitrage Mastery
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.
The key is that the total liquidity, , only changes when liquidity providers add or remove funds from the pool. During a trade, must stay constant. This means if a trader increases the amount of token in the pool, the AMM must decrease the amount of token 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 ) and 2,000,000 USDC (token ).
First, we calculate our constant, :
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 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: ETH
Now, we solve for the new USDC reserve, , keeping constant: USDC
The amount of USDC the trader receives is the difference between the old and new USDC reserves: USDC
The effective price of this trade was about $1,974.32 per ETH (). Notice this is lower than the pool's initial price of $2,000 per ETH (). 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 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.
What is the primary purpose of the constant product formula, , in a traditional Automated Market Maker (AMM)?
In a liquidity pool using the formula, a trader sells a large amount of token X to receive token Y. What is the immediate effect of this trade?