# Constant-product pools: deriving the output from x times y

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.

Evidence: [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol); [Uniswap v2 pricing](https://developers.uniswap.org/docs/protocols/v2/concepts/pricing); [Uniswap v2 quote library](https://raw.githubusercontent.com/Uniswap/v2-periphery/ed24991304291297c3b4a52818d02f46a17aa9a2/contracts/libraries/UniswapV2Library.sol)

Canonical: https://degreesofsatoshi.com/encyclopedia/constant-product-pools/
Published: 2026-10-02
Substantively modified: 2026-10-02
Independently verified by an automated reviewer: 2026-10-02T15:28:10.667176+00:00

AI-assisted research and drafting with a separate automated source-verification pass; no external expert or named human review is implied.

## Key facts

- **Fee-free invariant:** The idealized swap preserves x × y. ([Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol))
- **v2 input fee:** The cited v2 pair checks balances adjusted by 3 per 1,000 input units. ([Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol))
- **Reserves with fees:** Retained input fees cause the reserve product to grow. ([Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol))

## 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.

Evidence: [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol); [Uniswap v2 pricing](https://developers.uniswap.org/docs/protocols/v2/concepts/pricing)

## 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.

Evidence: [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol)

## 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](/tools/swap-lab/), which uses fixed-point arithmetic and displays its rounding rules.

Evidence: [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol); [Uniswap v2 pricing](https://developers.uniswap.org/docs/protocols/v2/concepts/pricing); [Uniswap v2 quote library](https://raw.githubusercontent.com/Uniswap/v2-periphery/ed24991304291297c3b4a52818d02f46a17aa9a2/contracts/libraries/UniswapV2Library.sol)

## Questions

### 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.

Evidence: [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol)

## Claims and scope

### constant-product-pools-quick-answer

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: {"collection":"defi","dataAsOf":null,"blockHeight":null}

### constant-product-pools-fact-fee-free-invariant

Fee-free invariant: The idealized swap preserves x × y.

Scope: {"collection":"defi","dataAsOf":null,"blockHeight":null}

### constant-product-pools-fact-v2-input-fee

v2 input fee: The cited v2 pair checks balances adjusted by 3 per 1,000 input units.

Scope: {"collection":"defi","dataAsOf":null,"blockHeight":null}

### constant-product-pools-fact-reserves-with-fees

Reserves with fees: Retained input fees cause the reserve product to grow.

Scope: {"collection":"defi","dataAsOf":null,"blockHeight":null}

## Sources

- [Uniswap v2 pair source](https://raw.githubusercontent.com/Uniswap/v2-core/v1.0.1/contracts/UniswapV2Pair.sol) — Uniswap. Reserve accounting, LP shares, fee-adjusted invariant and optional protocol fee. Locator: mint; burn; swap; _mintFee. Retrieved: 2026-10-02.
- [Uniswap v2 pricing](https://developers.uniswap.org/docs/protocols/v2/concepts/pricing) — Uniswap. Reserve-dependent quotes, trade bounds and external price observations. Locator: Pricing Trades; Exact Input; Exact Output. Retrieved: 2026-10-02.
- [Uniswap v2 quote library](https://raw.githubusercontent.com/Uniswap/v2-periphery/ed24991304291297c3b4a52818d02f46a17aa9a2/contracts/libraries/UniswapV2Library.sol) — Uniswap. Fee-adjusted exact-input output uses integer division; exact-output input rounds upward. Locator: getAmountOut, lines 43–49; getAmountIn, lines 53–58. Retrieved: 2026-10-02.

## Revision history

- 2026-10-02: First publication after primary-source research and independent automated verification.
- 2026-10-02: 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.
- 2026-10-02: 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.

## Cite this entry

Degrees 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/
