DEV Community

Willson Lammy
Willson Lammy

Posted on

Constant Product AMM Formula: Swap Output and Minimums

A constant product AMM calculates swap output as (amountIn × (1 − fee) × reserveOut) ÷ (reserveIn + amountIn × (1 − fee)). This is the most the pool can pay while keeping the product of its fee-adjusted reserves at least as large as it was before the trade.

  • Use the pool’s current reserves and fee to calculate a quote.
  • Round the output down in token units, then set an acceptable minimum.

How Do Reserves and Fees Determine Swap Output?

The pool starts with reserveIn = x and reserveOut = y. A swap adds input and removes output, so the constant product equation is (x + amountIn × (1 − fee)) × (y − amountOut) = x × y. Solving for amountOut gives the formula above. The full input enters the pool; the fee adjustment limits how much output the trade can take.

Uniswap V2 implements its 0.3% fee with integers: amountOut = (amountIn × 997 × reserveOut) ÷ (reserveIn × 1000 + amountIn × 997). Multiply before dividing and round the final result down. This avoids prematurely truncating the fee-adjusted input and ensures the trade leaves the actual reserve product at least as large as before.

Take a pool holding 1,000 A and 1,000 B. Swapping 100 A produces about 90.66 B: (100 × 997 × 1,000) ÷ (1,000 × 1,000 + 100 × 997). Each additional unit of A buys B at a worse rate as the trade moves the reserves.

Suppose the input rises to 110 A while those starting reserves stay the same. Output rises to about 98.83 B, but output per A falls from about 0.9066 B to 0.8985 B. The same equation still applies; the trade’s size relative to the reserves changes its rate.

Pools on shiba swap derive from SushiSwap, a Uniswap V2 fork, and use the same formula. The shiba swap exchange provides context for where these reserve-based quotes become swaps.

Related Questions

What is price impact in an AMM?

Price impact is the change in the pool’s exchange rate caused by the trade itself. It grows as input becomes larger relative to the reserves, so an integration should calculate each quote from live reserves.

How is slippage tolerance set in a swap?

Slippage tolerance sets amountOutMin below the current quote. With a roughly 90.66 B quote and 1% tolerance, the minimum is roughly 89.75 B, rounded down in raw token units. If reserves change before execution and the resulting output falls below amountOutMin, the swap reverts. A quote from stale reserves can therefore pass in your app and fail on-chain.

What Happens Beyond a Single Pool?

How do multi-hop routes calculate output across pools?

Each hop uses its own live reserves and fee. Feed one pool’s rounded-down output into the next pool as amountIn, then apply amountOutMin to the route’s final output.

How are LP fees accrued in the reserves?

The fee stays in the input reserve. Because output is calculated from the fee-adjusted input while the pool receives the full input, the actual reserve product increases, accruing value to liquidity providers.

Top comments (0)