Skip to main content
QNews

Crypto markets, protocols and policy

AMM swap output follows reserves and fee

To calculate AMM swap output amount in code, apply the pool fee to input, then use reserves in the constant-product formula and round down in base units.

The QNews Desk4 min read

AMM swap output follows reserves and fee

How do you calculate an AMM swap’s output from pool reserves? For a constant-product pool, multiply the input by the fee-adjusted rate, then divide by the input reserve plus that adjusted amount. Use integer base units and the pool’s actual fee so your quote matches the amount the contract can pay.

The reserve and fee inputs set the quote

The output amount is set by four values: amountIn, reserveIn, reserveOut and the pool’s fee rate. Reserves are the token balances used by the pool’s pricing rule; “in” and “out” refer to the tokens you are swapping from and to.

SyncSwap is a decentralized exchange where users trade tokens through liquidity pools on supported networks. If a trade can take more than one pool path, syncswap routing changes which pools supply the quote and therefore which reserves and fees you need to account for.

Read both reserves and the fee from the pool you are quoting. Use current values, since trades change reserves and a stale snapshot can produce a different output from the one available when your transaction executes.

Fee-adjusted input preserves x·y=k

The constant-product rule, written x·y=k, says the product of a pool’s two token reserves must hold across a swap, with the fee treatment included in the pool’s pricing. The fee-adjusted input is the amount that enters the pricing calculation.

For an exact-input swap, calculate:

amountInWithFee = amountIn × (1 − fee)

amountOut = amountInWithFee × reserveOut ÷ (reserveIn + amountInWithFee)

Here, x is the input-token reserve and y is the output-token reserve. After the fee-adjusted input is added to x, the output is the amount removed from y while keeping the reserve product consistent. This is the constant-product calculation used by Uniswap V2’s getAmountOut function and fits SyncSwap Classic Pools.

Uniswap V2’s 0.3% fee is represented as 997/1000 of the input. Its integer form is amountOut = amountIn × 997 × reserveOut ÷ (reserveIn × 1000 + amountIn × 997). This is the fee math used by the Uniswap V2 Library.

Integer arithmetic keeps the quote executable

Calculate amounts in each token’s smallest unit, such as wei for ETH, and represent the fee as a numerator and denominator. Solidity integer division rounds down, so use the same rounding direction when calculating a quote; rounding up can make the displayed output exceed what the pool pays.

In code, first reject a zero input or a zero reserve. Then compute the fee-adjusted numerator and denominator with integer arithmetic, divide, and interpret the result as output-token base units. Avoid converting reserves or amounts to floating-point values: precision loss can shift the final unit, and an overestimated quote can make a transaction’s minimum-output check revert.

Set the minimum output from the quote and the slippage tolerance you intend to accept. The quote itself is determined by the input, reserves and fee; the minimum-output threshold is a separate transaction condition that limits how much less you will accept if the pool changes before execution.

A 1 ETH trade returns about 2,962 USDC

Take a pool with 100 ETH and 300,000 USDC in reserves, a 1 ETH input and a 0.3% fee. The fee-adjusted input is 1 × 0.997, or 0.997 ETH.

Substituting the values gives 0.997 × 300,000 ÷ (100 + 0.997), which is about 2,961.47 USDC. The on-chain calculation uses base units and rounds down, so the precise integer output depends on token decimals and the pool’s fee implementation; the human-readable quote is about 2,962 USDC.

This formula applies to constant-product pools. Stable Pools use a different invariant, so use the pool’s own quote function for those rather than applying x·y=k. For a quote that matches execution, use the correct pool type, fresh reserves, the actual fee and integer arithmetic throughout.