A clear and simple guide to the constant product formula (x * y = k), the core mathematical engine that powers decentralized exchanges like Uniswap.

x * y = k): The Engine of AMMsThe Constant Product Formula, expressed as x * y = k, serves as the mathematical foundation for many Automated Market Makers (AMMs) and Decentralized Exchanges (DEXs), including Uniswap v2. This formula establishes a "bonding curve" that sets asset prices in a liquidity pool automatically, eliminating the need for a traditional order book.
Grasping this formula is essential for understanding how DeFi liquidity pools function. It governs price settings, explains slippage, and clarifies the risks of impermanent loss faced by liquidity providers.
This article breaks down the constant product formula, its practical applications, and key characteristics.
| Term | Definition |
|---|---|
| Formula | x * y = k, where x represents the amount of Token A, y represents the amount of Token B, and k is a constant. |
| Rule | The value of k must remain unchanged during a trade. An increase in one token's quantity requires an equivalent decrease in the other. |
| Price Discovery | The asset price in the pool is determined by the ratio of the reserves (x / y). Trades alter this ratio and therefore the price. |
| Infinite Liquidity | The hyperbolic curve of x * y = k suggests the pool can theoretically provide liquidity at any price, extending from zero to infinity. |
| Foundation of DeFi | This formula enabled the establishment of permissionless, automated exchanges that are integral to the DeFi ecosystem. |
Each component of x * y = k plays an important role:
x: Total reserve of Token A in the liquidity pool.y: Total reserve of Token B in the liquidity pool.k: The constant product. This value is established when liquidity is first added to the pool and only changes when liquidity providers add or withdraw liquidity. During trades, k remains constant.x * y = kConsider a liquidity pool for ETH and USDC with the following conditions:
x (ETH reserve) = 10 ETHy (USDC reserve) = 35,000 USDCWe first calculate the constant, k:
k = x * y = 10 * 35,000 = 350,000After any trade, the protocol ensures that the product of the new reserves equals 350,000.
The current price of ETH is derived from the reserve ratio:
y / x = 35,000 / 10 = 3,500 USDC per ETH.Now, a trader aims to buy 1 ETH.
x') becomes 10 - 1 = 9 ETH.y') to maintain k:x' * y' = k9 * y' = 350,000y' = 350,000 / 9 = 38,888.89 USDCy' - y = 38,888.89 - 35,000 = 3,888.89 USDC.In this transaction:
38,888.89 / 9 = 4,320.98 USDC.The formula automatically adjusts the price according to the reserve ratio changes.
The formula x * y = k generates a hyperbolic graph. This shape has critical implications:
Asymptotic Nature (Infinite Liquidity): The curve never intersects the x or y-axis. Thus, no matter how much of one token is traded, the pool theoretically maintains liquidity for the other token, though at increasingly high prices.
Slippage: The curve's convexity means larger trades endure more significant price impacts. As traders move along the curve, the price adjusts steeply, resulting in slippage.
Impermanent Loss: When asset prices shift, the value of the assets held by a liquidity provider may decrease compared to simply holding the assets in their wallet. This loss reflects the constant rebalancing of the LP's portfolio due to price changes inherent in the x * y = k formula.
In practice, AMMs incorporate trading fees into the formula. For instance, Uniswap v2 applies a trading fee. The modified formulas are as follows:
x * y = k(x + Δx) * (y - Δy) = k'The trading fee slightly increases the k value with each trade. This increment represents profit distributed to liquidity providers.
x * y = kWhile the constant product formula was leading, it exhibits limitations in capital efficiency. Consequently, more advanced invariants have emerged:
The formula is based on fundamental mathematics. Its application to AMMs was first articulated by Vitalik Buterin and popularized by Hayden Adams, the founder of Uniswap.
Why is k constant?k is the invariant the AMM is designed to maintain. It changes only when liquidity providers add or remove assets from the pool, which establishes a new k.
No, the formula does not include the network gas fees required to execute a transaction. Traders pay the amount specified by the AMM formula in addition to the gas fee.
Is the price displayed on a DEX the price I will receive? No, the displayed price reflects the current spot price before your trade. Your transaction will incur price impact, leading to a slightly worse execution price. Larger trades will experience a more pronounced difference.
Explore more guides and career playbooks