Skip to main content

    Calculation method

    How MoonMath calculates swap execution

    Swap Execution & Fee CalculatorWhat will this swap really cost?

    The formula

    The fee is taken off the input first, then the constant product gives output = outReserve × netIn ÷ (inReserve + netIn). Price impact is the execution price against the pool's own pre-trade spot, not against an external quote.

    What it reads

    • The reserves of one observed pool and the direction of the swap
    • The input amount and the price of the input asset
    • The fee rate, defaulting to the chain default and labelled as an assumption
    • Slippage tolerance and, optionally, a gas cost

    What it assumes

    • The pool must be a validated constant-product observation with positive reserves; anything else is refused
    • The fee rate is a chain-level default unless supplied, and gas is zero unless supplied
    • Output is valued against the pool's pre-trade spot, because an unrelated external quote would mix two reference markets
    • The reserves are unchanged between observation and execution

    What this does not establish

    • It is a single-pool swap and not a best-available quote: no routing, no aggregator, no split
    • The minimum received is the tolerance floor the trader sets, not a predicted fill
    • It models no other trade, no block ordering and no pending transactions
    • The impact cost is measured against a spot price that is itself a snapshot

    Run it

    Estimate constant-product swap output, AMM price impact, pool fees, network cost and minimum received. Slippage tolerance protects execution but is not itself an additional protocol fee.

    Open the Swap Execution calculator

    How the market itself works

    This page describes MoonMath's arithmetic. The mechanics section describes the market: how an order book forms a price, why order-book depth is not the same thing as pool liquidity, and what a public book does and does not let you observe.

    Read the market mechanics