DEX essentials

How constant-product pools price a swap

Work through constant-product swap output, input-side trading fees and the difference between quoted rate and reserve ratio.

A constant-product AMM constrains two token reserves with an invariant commonly written as x × y = k. Selling one token increases its reserve and removes an amount of the other consistent with the rule. Larger inputs move further along the curve.

Calculate a fee-free swap

Take a hypothetical pool with 2,000 A and 6,000 B. Its product is 12,000,000. If a trader adds 100 A with no fee, the new A reserve is 2,100. The B reserve becomes 12,000,000 ÷ 2,100, or approximately 5,714.286 B.

The trader receives 6,000 − 5,714.286 = 285.714 B. Although the initial marginal relationship was 3 B per A, the average result is roughly 2.85714 B per A. The trade did not exchange every unit at the initial reserve ratio.

The general fee-free output formula is output = y × input / (x + input), where x is the input-token reserve and y the output-token reserve.

Introduce an input-side fee

For an illustrative fee f, the amount used to price the exchange is effective input = input × (1 − f). Using an invented fee of 0.2%, the 100 A input contributes 99.8 A to the pricing formula. Output is then 6000 × 99.8 / 2099.8, approximately 285.170 B.

This is a simplified mathematical model, not a current fee schedule. In a design that retains the fee in the pool, actual post-trade reserves include the full deposited input, so the reserve product can increase. “Constant product” names the pricing invariant; it does not imply that k remains numerically unchanged forever despite fees and liquidity changes.

Uniswap's v2 pricing guide describes reserve-based pricing and fee-adjusted invariant checks. Other AMM families use different invariants, so this formula should not be applied indiscriminately to stable or weighted pools.

Why a router cares

The output function is nonlinear. Doubling the input does not double the output at an unchanged average rate. A router can compare several pools and allocate input according to the curve each offers.

A pool's current ratio is therefore useful context but insufficient for quoting a finite trade. The calculation requires reserves, direction, size and the applicable fee rule. Real contract arithmetic also introduces integer rounding, which these decimal examples intentionally simplify.

Sources & verification (2)

Source-check date is recorded in the article details. URLs are provided for manual verification. Use Copy to keep this page open.

  1. Uniswap v2 Pricing

    Reserve pricing, invariant enforcement and exact swap modes.

    https://developers.uniswap.org/docs/protocols/v2/concepts/pricing
  2. How Uniswap Works

    Liquidity pools use reserve-based automated pricing and swaps alter reserves.

    https://developers.uniswap.org/docs/get-started/concepts/how-uniswap-works

Continue reading

How stable-swap pools handle correlated assets How unequal pool weights affect swap prices Concentrated liquidity from a swapper's perspective