Skip to content
Blockchain Brief

Protocol, chain and market reporting

How to Estimate a Constant-Product Swap Price

A constant-product quote starts with pool reserves, then adjusts for trade size and fees; it gives an average execution price, not the reserve ratio.

The Blockchain Brief Desk3 min read

Cover artwork for How to Estimate a Constant-Product Swap Price

Estimate a constant-product swap price by applying the pool’s fee and trade size to its reserves, then dividing the output by the input. That gives an average execution price for the trade. The reserve ratio gives the pool’s spot price before the trade, but it does not account for price impact.

How does the constant-product formula set a swap quote?

A constant-product automated market maker keeps the product of its token reserves at or above a target value. If the reserves are x units of the input token and y units of the output token, the invariant is x × y = k. For an input amount Δx, the no-fee output is y × Δx ÷ (x + Δx). As the input grows, the pool must return a larger share of its output reserve, so each additional input unit buys less.

Most pools charge a fee on the input. If the fee fraction is f, the amount used to calculate output is Δx × (1 − f), and the output estimate becomes y × [Δx × (1 − f)] ÷ [x + Δx × (1 − f)]. The fee rate and exact accounting depend on the pool contract. For a separate question about transfer setup, see this guide to choosing a base swap pool. The quote itself still depends on the reserves and fee of the specific pool used.

How do you turn the output into a price?

Divide the estimated output by the input to get the average execution price, expressed as output tokens per input token. Compare it with the spot price, y ÷ x, after putting both reserve amounts into human-readable token units. On-chain amounts use each token’s smallest unit, so decimals must be applied before comparing prices. Reversing the trade direction also reverses the reserve ratio: the input token reserve belongs in the denominator.

The average price is lower than the starting spot price for a trade that moves the pool against itself. This difference is price impact: the trade changes the reserve balance that sets the next marginal price. A fee also reduces the trader’s output, but it is a separate cost from price impact. Keeping them separate makes it easier to tell whether a poor quote comes from the pool’s fee or from the size of the trade relative to its liquidity.

What changes the estimate in an actual swap?

Pool reserves, fee rules, and trade size determine the basic estimate. For a route through multiple pools, calculate each hop using that pool’s reserves and fee, then use its output as the next hop’s input. The route’s final output can differ from a single-pool quote because each hop adds its own fee and price impact. A deeper pool often has less price impact for the same trade size, but its fee and route still matter.

  • Read reserves for the exact pool and token pair being quoted.
  • Apply the pool’s fee rule to the input before calculating output.
  • Normalize token decimals before converting the quote into a price.
  • For multi-hop routes, account for every pool’s fee and reserve ratio.

How should you use a constant-product quote?

Use the formula to compare expected execution across trade sizes and routes, then treat the result as an estimate tied to a particular reserve state. Pool reserves can change when another swap is included first, so the eventual output may differ from a quote based on earlier state. A transaction’s minimum-output condition sets the least output the trader will accept; if the pool cannot meet it when execution occurs, the swap reverts. For a practical estimate, calculate the fee-adjusted output, convert it into token units, and compare the average price with the starting spot price.