Constant Product Formula
- Category
- Decentralised Finance
- Level
- Expert
Simple definition
The rule that keeps the two reserves of a pool multiplying to the same number. Buy one asset and you must put in enough of the other to keep that product unchanged.
Professional definition
The invariant x times y equals k, enforced on every swap. It fixes the quote at the reserve ratio, makes the cost of a trade rise with its size relative to the reserves, and never lets a reserve reach zero, so the pool can always quote however bad the price becomes.
Technical definition
For reserves x and y with proportional fee f, an input of dx returns dy equal to y dx (1 minus f) divided by x plus dx (1 minus f). The marginal price is y over x and the realised average price is strictly worse, the gap being price impact. Fees are retained in the reserves, so k grows slightly with each swap. The design is a conservation law, not a forecast: it has no view on value and relies on arbitrage to track the outside market.
Academic definition
A constant-function market maker whose feasible reserve set is the hyperbola xy = k, yielding a closed-form trade function with deterministic, size-dependent price impact and no dependence on external price information.
Mechanism
Holding the product fixed forces the quote to be the reserve ratio, and that ratio has to deteriorate as one side is drained — otherwise the pool could be emptied at a flat price. The hyperbola never touches either axis, so liquidity is spread over every price from zero to infinity, which is exactly why depth near the current price is thin compared with a concentrated book.
In practice
- Who uses it
- Protocol designers choosing a curve, liquidity providers sizing a position, routers splitting an order, and arbitrageurs computing the profitable trade size.
- Why it matters
- It gives an exact, closed-form answer to what a given order will receive and what it will cost in price impact, before the order is sent.
- Limitations
- It assumes a single pool, a proportional fee and no change of reserves between quote and execution. Routed and concentrated-liquidity trades need a different calculation.
Common mistakes
- Quoting price impact from the marginal price rather than the average fill.
- Forgetting the fee is taken from the input, not the output.
- Applying the plain formula to a stable-curve or range-based pool.
Examples
A pool holds 10,000 of asset A and 1,000,000 of asset B, a spot price of 100. A swap of 100 A at a 0.3 percent fee returns about 9,871.6 B, an average of 98.72 — about 1.28 percent worse than spot.
- x = 10,000 units of A
- y = 1,000,000 units of B
- fee f = 0.003
- input dx = 100 A
- spot price before = y / x = 100 B per A
- effective input = 100 x (1 - 0.003) = 99.7
- dy = (1,000,000 x 99.7) / (10,000 + 99.7) = 99,700,000 / 10,099.7 = 9,871.60 B
- average fill = 9,871.60 / 100 = 98.716 B per A
- total cost vs spot = (100 - 98.716) / 100 = 1.28%, of which 0.30% is fee and about 0.99% is price impact
New reserves are 10,100 A and 990,128.40 B, so the quoted price drops to about 98.03. The product rises slightly above 1.0e10 because the fee stays in the pool.
Doubling the trade size in the same pool more than doubles the price impact, because impact scales with the ratio of order size to reserves. Splitting an order across two pools of equal depth therefore fills better than sending it all to one.
Mathematical foundation
Variables
| x | Reserve of the asset being sold into the pool | token units |
| y | Reserve of the asset being bought out of the pool | token units |
| k | The invariant: the product of the two reserves, held fixed by each swap and grown by retained fees | token units squared |
| \Delta x | Input amount sent to the pool | token units |
| \Delta y | Output amount received from the pool | token units |
| f | Proportional swap fee taken from the input | fraction |
Procedure
Take the fee off the input first: the effective input is dx times (1 minus f). The new x reserve is x plus the full dx, and the new y reserve must satisfy the invariant against the effective input, which gives the closed form above. Divide dy by dx for the average fill price, and compare it with y over x, the marginal price before the trade, to get price impact including the fee.
Worked example
- x = 10,000
- y = 1,000,000
- f = 0.003
- dx = 100
- dx(1-f) = 100 x 0.997 = 99.7
- numerator = 1,000,000 x 99.7 = 99,700,000
- denominator = 10,000 + 99.7 = 10,099.7
- dy = 99,700,000 / 10,099.7 = 9,871.60
- average fill = 9,871.60 / 100 = 98.716
- marginal price before = 1,000,000 / 10,000 = 100
Interpretation
The output is what the pool will actually pay for a given input, and the gap between the average fill and the pre-trade quote is the full execution cost: fee plus price impact. Impact is a function of size relative to reserves, so the same order is cheap in a deep pool and expensive in a shallow one.
Assumptions
- A single two-asset pool with the fee taken proportionally from the input and retained in the reserves. Protocols that send part of the fee elsewhere change k's growth, not the trade formula.
- Reserves do not change between quote and execution. If another transaction lands first, the realised output differs — this is why a slippage limit is needed.
- Only the plain constant-product invariant. Stable-swap curves, constant-sum segments, weighted pools and concentrated-liquidity ranges each use a different invariant and a different trade function.
- The curve places liquidity at every price from zero to infinity, so depth near the current price is low relative to the capital committed.
- The formula prices the trade but says nothing about whether the pool's price is right; alignment with outside markets depends entirely on arbitrage, and the value arbitrageurs take is borne by providers as divergence loss.
- Gas and routing costs are outside the formula and can dominate it for small orders.
Related terminology
- parent
- Automated Market Maker
- related
- Automated Market Maker Impermanent Loss Liquidity Pool Slippage Slippage Tolerance Swap Arbitrage Decentralised Exchange
Slippage
Price impact from this curve is deterministic and knowable before sending. Slippage is the further difference caused by the state changing between quote and execution.
Impermanent Loss
This formula prices a trade. Impermanent loss measures what the same invariant does to a provider's position value once the price ratio has moved.
Sources
BIS Quarterly Review · 2021-12 · box on automated market makers and constant-product pricing
https://www.bis.org/publ/qtrpdf/r_qt2112b.pdf URL verified
IOSCO · description of automated market-maker pricing mechanisms
https://www.iosco.org/library/pubdocs/pdf/ioscopd754.pdf URL verified
Editorial status
Flagged by the editor as uncertain on a linguistic, mathematical or regulatory point.
