What Is the Constant Product Formula in DeFi?
The constant product formula sets a token swap's output: x times y stays fixed. Here is the calculation, plus the fee effect.
The Top Crypto Editors 2 min read
A constant-product AMM is a token exchange that prices every trade by one rule: the product of its two reserves stays the same after the trade. That rule is the constant product formula, x × y = k. When you put Δx tokens in, the pool keeps the product, so it lets you take out the y that leaves (x + Δx) × (y − Δy) = x × y.
Solving that equation for the output gives Δy = y − xy/(x + Δx). Take a pool with 1,000 A and 1,000 B. You spend 100 A. The new A reserve is 1,100, so the B reserve must fall to 1,000,000 / 1,100 ≈ 909.09 B. You receive about 90.91 B, ignoring fees.
The average rate on that swap is 90.91 / 100, or about 0.9091 B per A, while the pool's mid-price before the trade was 1.0 B per A. A larger input moves the reserves further from the starting ratio, so the average rate worsens; that gap is price impact. In raw pool units, the mid-price is y/x; converting to a token price applies the decimal difference between the two tokens.
A route can pass through several pools. QuickSwap V2's getAmountsOut(amountIn,path) handles that route in one call, applying the same invariant at every hop; its pricing library shows the exact formulas. You can compare the displayed output against those reserve numbers on quickswap.io before you swap.
Fees keep the formula from being literal forever. A fee-taking pool applies the invariant to the fee-adjusted input: with a 0.3% fee, 100 A in counts as 99.7 A for pricing. The fee itself stays in the pool, so the actual reserve product drifts upward after every trade. Constant product is the pricing constraint on a single swap, not a promise that x × y never changes.