Constant Product Formula
The Constant Product Formula, often used in decentralized exchanges (DEXs) like Uniswap, is a mathematical algorithm that maintains a constant product of two assets in a liquidity pool. In a liquidity pool, traders can swap one asset for another at a price determined by the ratio of the two assets in the pool.
The formula is:
x * y = k
Where:
-x is the amount of one asset in the pool.
-y is the amount of the other asset in the pool.
-k is the constant product.
Example:
Let's say we have a liquidity pool with 10 ETH and 1000 DAI. The constant product k would be 10 * 1000 = 10000.
Case 1: Adding Liquidity
- Suppose a trader wants to add 1 ETH to the pool.
- To maintain the constant product, the amount of DAI in the pool must increase.
- Using the formula: (10 + 1) * y = 10000
- Solving for y: y = 10000/11 = 909.09
- The trader must add approximately 909.09 DAI to the pool.
Case 2: Swapping Assets
- Another trader wants to swap 0.5 ETH for DAI.
- Before the swap, the formula applies: x * y = 10000
- After the swap, the formula still holds: (10 - 0.5) * (y + Δ y) = 10000
- Solving for Δ y : Δ y = (0.5 * 10000)/9.5 = 526.32
- The trader will receive approximately 526.32 DAI for 0.5 ETH.
In both cases, the constant product formula ensures that the product of the assets in the pool remains constant, thereby preserving the pricing mechanism for trades on the decentralized exchange.