Degrees of Satoshi · Learn with evidence
Swap Lab
Change a pool, inspect the arithmetic, and see why tolerance does not improve a quote.
Try a swap from A to B
Fictional tokens with eight decimal places. Every change recalculates locally. No wallet or transaction is involved.
Edits stay in the calculator. A copied scenario link contains only the illustrative numeric settings.
- Expected output
- Minimum acceptable output
- Average execution rate, including input fee
- Modeled fee retained in the pool
- Price impact, excluding input fee
- Resulting reserve A
- Resulting reserve B
- Pool ratio before
- Pool ratio after
What if another trade executes first?
The earlier A-to-B swap changes the pool before your original input arrives. Your minimum stays fixed at the value from the original quote.
- Earlier trader receives
- Reserves after the earlier trade
- Your output against the changed reserves
- Your original minimum
This illustrates one ordering effect. Gas charges and other reasons a transaction may fail are excluded.
Where the quote comes from
Let x and y be the reserves, a the input, f the input-fee fraction, and s the slippage tolerance. The fee stays in the pool. This is an illustrative Uniswap v2 style constant-product model, not a quote from a live exchange.
output = floor₈[y × effective input / (x + effective input)]
new reserves = (x + a, y − output)
minimum output = floor₈[output × (1 − s)]
average execution rate = output / a
modeled retained fee = a × f
price impact = 1 − output / [effective input × y/x]
floor₈ means round down to eight decimal places. Price impact compares the received amount with the pre-trade reserve ratio after removing the input fee. The average execution rate includes the input fee. Output rounding is included; displayed percentages and ratios are rounded down too. The modeled fee can include fractions of a token unit because it describes the formula, not a separate fee transfer.
A worked example
Start with 100 A and 1,000 B, trade 10 A and charge 0.30%. Effective input is 9.97 A and the modeled retained fee is 0.03 A. Expected output is 90.66108938 B; the reserves become 110 A and 909.33891062 B. A 1% tolerance gives a minimum of 89.75447848 B. Raising tolerance changes only that minimum.
What this model leaves out
The base quote assumes no earlier trade; the ordering example adds exactly one same-direction swap. Neither includes gas charges, routes, transfer-tax tokens, rebasing tokens, hooks or external prices. All input reaches the pool; output is transferred exactly. Concentrated-liquidity pools have a different model. A real transaction may fail its minimum-output check or face ordering and contract risks.
Learn next: constant product pools · slippage and price impact · concentrated liquidity · maximal extractable value.
Inspect the model
- Uniswap v2 quote library, commit ed249913, function getAmountOut: integer quote formula and 0.30% fee in that version. This lab makes the fee adjustable for learning.
- Uniswap v2 pair v1.0.1, commit 4dd59067, functions swap and _update: fee-adjusted invariant check and reserve updates.
- Read this calculator’s arithmetic and the ordering scenario.