TL;DR
The constant product formula (x * y = k) is the core mathematical rule behind most decentralized exchange pools. It states that the product of the two token reserves must remain constant during any trade. When a trader adds one token to the pool, the formula determines exactly how many of the other token they receive, keeping the product at k (before fees).
How It Works
The constant product formula is elegant in its simplicity. Two numbers multiplied together must always equal the same result. Price discovery, slippage, market making: all of it emerges from this single rule.
Start with a pool holding 1,000,000 tokens (x) and $10,000 USDC (y). The constant product is k = 1,000,000 * 10,000 = 10,000,000,000. Now a trader wants to buy tokens by depositing $1,000 USDC. After the deposit, y = 11,000. To maintain k, x must equal 10,000,000,000 / 11,000 = 909,091. The trader receives 1,000,000 - 909,091 = 90,909 tokens.
Notice what happened: the trader deposited $1,000 and received 90,909 tokens. The spot price before the trade was $0.01 per token, so “ideal” execution would have yielded 100,000 tokens. The 9,091 token shortfall is the price impact: the cost of moving along the curve.
The formula creates a hyperbolic curve that has two important properties. First, it approaches but never touches either axis. You can never buy all the tokens out of the pool, because as the token reserve approaches zero, the price approaches infinity. Second, the curve is steeper at the extremes and flatter in the middle, which means small trades near the center get good prices while large trades toward the edges get progressively worse.
In the Token Launch Simulator, every calculation flows from this formula. computeK(x, y) computes the invariant. spotPrice(x, y) returns y/x. buyTokens(x, y, amountIn) solves for the new reserves and returns the tokens out. The entire results panel (market cap, price appreciation, slippage table, simulation) is derived from applying x * y = k at different points.
One misconception: the formula doesn’t account for trading fees directly. Fees are handled by reducing the effective input amount before applying the formula (e.g., only 99.7% of the input counts toward the swap if the fee is 0.3%). The result is that k actually increases slightly with each trade, as the fee adds value to the pool.
Try It Yourself
Watch x * y = k in action: every number in the Token Launch Simulator comes from this formula. Set your supply and budget, and see how the calculator derives initial price, post-buy price, market cap, and slippage: all derived from the product of two reserves. Try the Token Launch Simulator →
Related Concepts
- Constant Product AMM: The broader AMM design that uses this formula as its pricing rule
- Spot Price: Derived directly from the formula as y/x at any point
- Slippage: Caused by the curvature of the x * y = k hyperbola
- Liquidity Pool: Holds the x and y reserves that the formula operates on
- Price Impact: The result of moving along the curve from one reserve state to another
Frequently Asked Questions
What does x * y = k mean?
In the formula x * y = k, x is the amount of one token in the pool, y is the amount of the other token, and k is a constant value (the product). When someone trades, they change x and y, but the product must stay the same. If a trader adds tokens to x (increasing it), the formula requires y to decrease proportionally: that decrease is the tokens the trader receives.
Does k ever change?
In theory, k stays constant during trades. In practice, k increases when trading fees are collected (fees add to reserves without withdrawing from the other side) and when new liquidity is added to the pool. k decreases when liquidity is removed. The key rule is that each individual swap must respect the invariant: the post-trade product must equal or exceed the pre-trade product.
How does the formula create a price curve?
The equation x * y = k traces a hyperbola when graphed. At any point on this curve, the slope gives the marginal price (approximately y/x). As you move along the curve by executing larger trades, the slope changes, which is why larger trades get progressively worse prices. The curve approaches but never reaches zero on either axis, meaning you can theoretically never fully drain one side of the pool.
Read the Full Article
Enter your email for free access to this article and all simulation tools.