Verification checklist
- First record whether the step reaches the price limit. If it does not, the fee is the remaining input minus what the curve actually took, not the input times the fee rounded down.
- Fee tier 3000, input 997, liquidity 994, and a limit of 2^97 plus 1. The curve takes 994 and the fee is 3. Input times 3000 divided by 1000000, rounded down, is 2.
- Input 100 lets the curve take 99, and the fee is 1. Input 1 lets the curve take 0, and the fee is still 1. Both floors are 0.
- Input 100301, liquidity 100000, and a limit of exactly 2^97. The curve takes 100000, the fee is 301, and the remainder is 900000. Flooring the whole input produces 300.
1. The curve sees an amount that has already lost its fee
SwapMath.sol v1.0.0 splits one step in computeSwapStep into four return values: the next square-root price, the amountIn the curve takes, the amountOut the curve pays, and feeAmount. On an exact input, amountRemaining is zero or positive. The function first computes amountRemainingLessFee: the remaining input times 1000000 minus feePips, divided by 1000000, with FullMath.mulDiv rounding down. At fee tier 3000 the factor is 997000. The price function receives that floored amount, not the full amount the user signed.
The comment says that on an exact input the fee plus amountIn never exceeds the amount remaining. Two paths make that sentence into two different formulas. If the new price is not the price limit passed in, feeAmount is the remaining input minus the amountIn the curve actually took. If the step reaches the limit, or the swap is not an exact input, feeAmount is amountIn times feePips divided by 1000000 minus feePips, through mulDivRoundingUp, so any remainder adds 1. The first path keeps the fraction dropped by the opening division inside the fee. The second path rounds up against what the curve already took, and the denominator is 997000, not 1000000.
So "fee tier 3000 is 0.3 percent of the trade, rounded down" matches only on inputs that divide cleanly. It collapses two paths into one, and it uses the wrong denominator for the round-up. This note pays the pool in token1, so zeroForOne is false, and the current square-root price is 2^96, which is 79228162514264337593543950336. That is the stored price at tick 0. The gap between one division and the quoted price is the subject of Swap Check: Dividing the Sqrt Price Once Is Not the Pool Quote. This note does not take the square root again. It follows the slice removed before the curve.
2. Input 997: the curve takes 994, and the fee is 3
The teaching pool has liquidity 994, fee tier 3000, and an exact input of 997 token1. The price limit is 2^97 plus 1, which is 158456325028528675187087900673. The amount offered to the curve is 997 times 997000, which is 994009000, divided by 1000000 and rounded down, which is 994. A fraction of 0.009 token is already gone.
SqrtPriceMath.sol v1.0.0 moves the price up when token1 is added by shifting the input left by 96 and dividing by liquidity, rounding down, then adding the quotient to the current square-root price. 994 shifted left by 96 divides evenly by liquidity 994, and the quotient is 2^96. The new square-root price is 2^96 plus 2^96, which is 2^97, or 158456325028528675187087900672. It is 1 below the limit, so the step does not reach the limit.
When the limit is missed, amountIn is recomputed from the new price and rounded up. Liquidity 994 times the price gap of 2^96, divided by 2^96, is exactly 994, so nothing rounds up. The fee is 997 minus 994, which is 3. The user's 997 splits in two: the curve takes 994, and the fee takes 3. Input 997 times 3000 is 2991, and dividing by 1000000 rounds down to 2. The missing 1 is the 0.009 from the first division, kept inside the fee. It is not a second protocol cut.
Liquidity is set to 994 so the square-root division comes out even and the fee can be read apart from price rounding. If liquidity does not divide 994 times 2^96, the recomputed amountIn can be smaller than 994. The fee is still 997 minus that smaller amountIn, so it is larger than 3. The 3 is the fee when the curve takes the entire 994 it was offered. It is not a lower bound for every liquidity.
| Exact input | Liquidity | Curve takes | Fee | Input times 3000 over 1000000, rounded down |
|---|---|---|---|---|
| 997 | 994 | 994 | 3 | 2 |
| 100 | 99 | 99 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1000000 | 997000 | 997000 | 3000 | 3000 |
3. Inputs of 100 and 1 both round down to a fee of zero
Exact input 100, liquidity 99, and the same limit of 2^97 plus 1. 100 times 997000 is 99700000, which divided by 1000000 rounds down to 99. 99 shifted left by 96 and divided by liquidity 99 still yields 2^96, so the new price is 2^97 and the limit is missed. The recomputed amountIn is 99. The fee is 100 minus 99, which is 1. 100 times 3000 divided by 1000000 is 0. That second formula would write the swap as free. On chain, the 1 token the curve did not take is the fee.
Exact input 1, liquidity 1. 1 times 997000 divided by 1000000 rounds down to 0. SqrtPriceMath returns the current square-root price when the input amount is 0, provided liquidity and the current price are both above 0. Otherwise the price function reverts first. The price stays at 2^96, which is not the limit, so the fee is remaining input minus amountIn. The price did not move, the recomputed amountIn is 0, and the fee is 1 minus 0, which is 1. The whole token becomes a fee. The curve takes 0 and pays no token0. 1 times 3000 divided by 1000000 is still 0. On a small exact input, rounding down writes a real fee as zero.
These two inputs use the same missed-limit path as 997. What changes is how large the dropped fraction is beside the input. A larger input hides the fraction. Using one divisible result at 1000000 to stand for every input erases the fee on 100 and on 1 together.
4. At an input of 1000000 both formulas return 3000
Exact input 1000000, liquidity 997000, and the limit still at 2^97 plus 1. 1000000 times 997000 divided by 1000000 is exactly 997000. 997000 shifted left by 96 and divided by that same liquidity yields 2^96, so the new price is 2^97. The limit is missed, the recomputed amountIn is 997000, and the fee is 1000000 minus 997000, which is 3000. 1000000 times 3000 divided by 1000000 is also 3000. The two formulas agree on this row.
They agree because 1000000 divides the fee denominator, so the first mulDiv has no remainder. That does not make the fee "input times 3000, rounded down." The previous section uses the same code on 997, 100, and 1 and gets 3, 1, and 1, while the floor produces 2, 0, and 0. A check needs an input that does not divide evenly. A sample that only uses 1000000 leaves the wrong branch in the report.
The comparison with the limit uses greater-than-or-equal. If the limit is moved to exactly 2^97, the amount the curve needs for input 997 is also 994, and the amount offered to the curve is 994, so the comparison succeeds and the step switches to the round-up. 994 times 3000 is 2982000. Divided by 997000 the quotient is 2 and the remainder is 988000, so rounding up still produces 3. The integer matches by chance. The path has changed. Read whether the next price equals the limit before reading feeAmount. The final 3 by itself does not name the path.
5. Input 100301 reaches the limit, and the fee is 301
A second teaching pool sets liquidity to 100000 and the price limit to exactly 2^97. The gap from 2^96 to 2^97 is 2^96. Liquidity 100000 times that gap, divided by 2^96 and rounded up, is still 100000. The exact input is 100301. 100301 times 997000 is 100000097000, which divided by 1000000 rounds down to 100000. A fraction near 0.097 is dropped. The 100000 offered to the curve equals the amount required to reach the limit, so the greater-than-or-equal test passes and the next price is the limit.
After the limit is reached, the fee is no longer remaining input minus amountIn. It is 100000 times 3000, divided by 997000, rounded up when a remainder remains. 100000 times 3000 is 300000000. 997000 times 300 is 299100000. The remainder is 900000, so the fee is 301. The curve takes 100000, the fee is 301, and the user pays 100301 on this step, the same as the exact input. The comment's ceiling holds with equality here: the fee plus amountIn does not exceed the amount remaining.
The whole 100301 times 3000 divided by 1000000, rounded down, uses 100301 times 3000, which is 300903000, and the quotient is 300. A report written that way says the fee is 300 and the curve takes 100001. On chain the curve takes 100000, and the extra 1 sits in the fee. The 301 is 1 above 300 because the remainder against the denominator 997000 is 900000. The fee tier did not change.
| Exact input | Liquidity | Curve takes | Remainder | Fee | Whole input, rounded down |
|---|---|---|---|---|---|
| 100301 | 100000 | 100000 | 900000 | 301 | 300 |
6. The 3 and the 301 are not yet a protocol cut, and they are not tokensOwed
An exact output makes amountRemaining negative, so exactIn is false. The missed-limit line "remaining input minus amountIn" is not used. Whether the price stops halfway or reaches the limit, an exact-output fee takes the round-up path: amountIn times 3000, divided by 997000. If the curve must take 100000 to produce the requested output, the fee is again 301 and the remainder is again 900000. Exact output is not "0.3 percent of the output, rounded down."
The 3 and the 301 in this note are the feeAmount returned by computeSwapStep. They have not been split by a protocol fee. How fee tier 3000, once a fee exists, leaves liquidity providers 2250 or 2500 is the subject of Fee Check: The Pool Fee Tier Is Not the Cut LPs Keep. Multiplying 3 by a protocol denominator is another rounding layer. It is not the fee of this step.
After feeAmount is written into fee growth, tokensOwed on a position still waits for Position.update. That is Fee Check: Growth Records 3000, and tokensOwed Is Still 0 Before the Poke. This note stops at what the curve takes and what the fee keeps. When 997 misses the limit, those amounts are 994 and 3. When 100301 reaches the limit, they are 100000 and 301. Writing either pair as "input times 3000, rounded down" drops 1 from 997 and drops 1 from 100301. The missing 1 sits in a different place: once in the fee, and once in the curve.
This is an analytical frame. The exact-input floor, the missed-limit fee as remaining input minus amountIn, and the round-up when the limit is reached or the swap is an exact output come from SwapMath.sol v1.0.0. The integer price step when token1 is added, and the unchanged price when the input is 0, come from SqrtPriceMath.sol v1.0.0. 994, 3, 99, 1, 997000, 3000, 100000, 301, and the remainder 900000 are teaching results of those integer operations, not a pool reading from 9 November 2026, and they are not investment advice.
7. Misreadings that keep coming back
Is the fee 2 on an input of 997 at fee tier 3000?
When the step misses the price limit, 997 times 997000 divided by 1000000, rounded down, is 994. The teaching pool's liquidity is also 994, and the curve takes all of it. The fee is 997 minus 994, which is 3. Input times 3000 divided by 1000000, rounded down, is 2.
If an input of 100 rounds down to a fee of 0, is the swap free?
When the step misses the price limit, the curve takes 99 and the fee is 1. On an input of 1 the amount passed to the price function is 0, the price does not move, the curve takes 0, and the fee is still 1. Both times, input times 3000 divided by 1000000 rounded down is 0.
Input 100301 reaches the price limit. Is the fee 300?
Once the limit is reached, the fee is the 100000 the curve took, times 3000, divided by 997000, rounded up when a remainder remains. 100000 times 3000 is 300000000. The quotient is 300 and the remainder is 900000, so the fee is 301. Input 100301 times 3000 divided by 1000000, rounded down, is 300.