Encyclopedia DeFi · Entry 50
Constant-product pools: deriving the output from x times y
In this article
At a glance
Key facts
| Fact | Detail | Source |
|---|---|---|
| Fee-free invariant | The idealized swap preserves x × y. | [1] |
| v2 input fee | The cited v2 pair checks balances adjusted by 3 per 1,000 input units. | [1] |
| Reserves with fees | Retained input fees cause the reserve product to grow. | [1] |
Derive the fee-free expression
Let q be input A and b be output B. The equation (x + q) × (y − b) = x × y gives b = y × q ÷ (x + q). This is a curve: b is not q multiplied by a fixed exchange rate. The marginal starting rate y ÷ x is only an approximation for a very small trade.
For x = 100 A, y = 1,000 B and q = 10 A, the ideal output is 1,000 × 10 ÷ 110 = 90.909090… B. Remaining B is 909.090909… and the new product remains 100,000 in exact arithmetic.
Track the effective input and real reserves
Using the cited Uniswap v2 0.3% input-fee rule, effective input is 9.97 A. The illustrative output becomes 1,000 × 9.97 ÷ 109.97 = 90.661089… B. This value is slightly smaller than in the fee-free case.
The real A reserve becomes 110 A, not 109.97 A. The difference stays in the pool, so its real reserve product rises. Conflating the effective input used in the quote with the actual deposited amount breaks both reserve accounting and later quotes.
Tokens use integers and pools have boundaries
Smart contracts compute in the token’s smallest units. Integer division truncates output under the v2-style expression. A displayed decimal should therefore describe a rounded result rather than promise an infinitely precise fraction. Tokens can also use different decimal scales.
This model excludes transfer-tax and rebasing behavior, concentrated ranges and changing external markets. With positive finite reserves, a finite input cannot withdraw the whole output reserve under the ideal equation. An invalid reserve or zero-output rounded trade needs explicit handling in software. Try these reserve and fee assumptions in Swap Lab, which uses fixed-point arithmetic and displays its rounding rules.
Direct answers
Questions people ask
Why does k increase when the name says constant product?
The ideal invariant is constant without fees. In the cited v2 implementation, retained swap fees make the actual reserve product increase; the contract applies its invariant after a fee adjustment.
Inspect the evidence
The answer and key facts have stable claim links. These records retain the scope and qualification when reused.
A constant-product pool uses two reserves x and y and a rule based on their product. In the ideal fee-free model, output = y × input ÷ (x + input). With an input fee f retained by the pool, substitute input × (1 − f) when quoting output; the full input still enters the actual reserve.
Scope: DeFi. Verification: verified · 2026-10-02T15:28:10.667176+00:00.
Link to this claimFee-free invariant: The idealized swap preserves x × y.
Scope: DeFi. Verification: verified · 2026-10-02T15:28:10.667176+00:00.
Link to this claimv2 input fee: The cited v2 pair checks balances adjusted by 3 per 1,000 input units.
Scope: DeFi. Verification: verified · 2026-10-02T15:28:10.667176+00:00.
Link to this claimReserves with fees: Retained input fees cause the reserve product to grow.
Scope: DeFi. Verification: verified · 2026-10-02T15:28:10.667176+00:00.
Link to this claimRevision history
- — First publication after primary-source research and independent automated verification.
- — Before first publication, independent review checked and revised: quickAnswerSourceIds, sections, sources. Independently derived output=y*q/(x+q), recomputed 90.909090… without fees and 90.661089… with 0.3% retained input fee, and checked actual input reserve 110 rather than 109.97. Added pinned v2-periphery getAmountOut code: integer division floors output; fee-adjusted pair balances enforce the invariant. Confirmed positive finite inputs cannot drain the ideal output reserve. Added the existing Swap Lab link and checked its visible fixed-point/rounding assumptions.
- — Before first publication, independent review checked and revised: source locator. Reopened the pinned raw Uniswap v2 library and corrected line locators from rendered-text offsets to original-file lines, including blanks. Formula support and the retrieved source hash remain unchanged.
On the Understand a swap path · Learn next: Slippage and price impact: two reasons a swap rate can differ
Source register
Sources and references
Retrieval dates and locators are recorded individually.- Uniswap v2 pair sourceUniswap
Reserve accounting, LP shares, fee-adjusted invariant and optional protocol fee.
Locator: mint; burn; swap; _mintFee · Version / scope: v2-core v1.0.1 · Retrieved: 2026-10-02Open source - Uniswap v2 pricingUniswap
Reserve-dependent quotes, trade bounds and external price observations.
Locator: Pricing Trades; Exact Input; Exact Output · Version / scope: Uniswap v2 · Retrieved: 2026-10-02Open source - Uniswap v2 quote libraryUniswap
Fee-adjusted exact-input output uses integer division; exact-output input rounds upward.
Locator: getAmountOut, lines 43–49; getAmountIn, lines 53–58 · Version / scope: v2-periphery commit ed24991304291297c3b4a52818d02f46a17aa9a2 · Retrieved: 2026-10-02Open source
Research and drafting use AI assistance. A separate automated review checks claims against primary sources; no external expert or named human review is implied. Publication, substantive editing, source retrieval and verification are recorded separately. This version was independently checked by an automated reviewer on 2 October 2026.
Editorial method and correctionsDegrees of Satoshi editorial project. “Constant-product pools: deriving the output from x times y.” Published 2026-10-02; updated 2026-10-02. https://degreesofsatoshi.com/encyclopedia/constant-product-pools/