Verification checklist
- Inside SwapMath.computeSwapStep, whether price reaches sqrtRatioTargetX96 dictates fee branch selection.
- When target is reached (sqrtRatioNextX96 == sqrtRatioTargetX96), fee is re-computed from actual curve input multiplied by feePips divided by 1000000 - feePips and rounded up.
- When target is missed (sqrtRatioNextX96 != sqrtRatioTargetX96), fee equals amountRemaining - amountIn, exhausting input for the current step.
- At fee 3000, curve taking 998 requires paying 1002; passing 1001 falls short, dropping curve input to 997 and preventing the cross transition.
1. Two divergent fee calculation paths
In Uniswap V3, any trade crossing multiple tick intervals is sliced by the pool loop into discrete steps. Each step executes SwapMath.computeSwapStep, which houses two contrasting fee calculation paths:
// SwapMath.sol
if (exactIn) {
if (sqrtRatioNextX96 != sqrtRatioTargetX96) {
// Target not reached: fee is remaining input minus curve intake
feeAmount = uint256(amountRemaining) - amountIn;
} else {
// Target reached: fee rounds up from actual curve intake
feeAmount = FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips);
}
}
This if-else branch is a core architectural boundary in concentrated liquidity:
- Hit Target: The provided input is sufficient to push price exactly to the boundary
tickNext. The step must consume only the exact tokens required to hit that point, preserving unspent input for subsequent iterations; - Miss Target: The input cannot push price across the interval. The trade terminates within the current step. The pool does not preserve residual tokens; it absorbs all
amountRemaining, and the difference fromamountInbecomes the fee.
2. Reaching target: curve takes 998, fee rounds up to 4
Assume pool fee rate is 3000 (0.3%), and the step's target boundary is sqrtRatioTargetX96. Advancing the curve to this target requires amountIn = 998 tokens.
Assume the trader passes amountRemaining = 5000. Because 5000 comfortably exceeds requirements, the step achieves the target (sqrtRatioNextX96 = sqrtRatioTargetX96).
The code branches to the target-hit fee calculation, ignoring 5000 and deriving fee strictly from 998:
feeAmount = FullMath.mulDivRoundingUp(998, 3000, 1000000 - 3000)
= FullMath.mulDivRoundingUp(998, 3000, 997000)
The product is 998 * 3000 = 2994000. Dividing by 997000:
2994000 / 997000 = quotient 3, remainder 3000
Because remainder 3000 > 0, mulDivRoundingUp forces an upward increment, making the fee 4. Total tokens spent for this step equal:
totalStepIn = amountIn + feeAmount = 998 + 4 = 1002
Deducting 1002 from 5000 leaves 3998, which carries forward intact to the next step, where ticks.cross triggers liquidity updates.
3. Passing 1001: falling short forces the missed-target branch
Now analyze the boundary edge case: hitting the target still requires 998, but the user provides amountRemaining = 1001.
To naive observers, 1001 exceeds 998. However, computeSwapStep evaluates whether 1001 can afford the step by first deducting fee upfront:
amountRemainingLessFee = 1001 * 997000 / 1000000 = 997997000 / 1000000 = 997
After upfront fee deduction, the curve receives only 997! But reaching the target requires 998. Because 997 < 998, the contract decides: target not reached.
Consequently, execution diverts to the missed-target branch:
amountIn = 997
feeAmount = amountRemaining - amountIn = 1001 - 997 = 4
Price stops just short of the target tick (sqrtRatioNextX96 != sqrtRatioTargetX96). All 1001 tokens are consumed. Remaining tokens drop to 0, terminating the swap loop completely. The pool never crosses the tick.
| amountRemaining | Curve Target In | Target Reached? | Fee Branch | Curve Intake amountIn | Step Fee | Step Total Spent | Carried to Next Step |
|---|---|---|---|---|---|---|---|
| 5000 | 998 | Yes | Hit Target | 998 | 4 (Round up) | 1002 | 3998 (Crosses tick) |
| 1002 | 998 | Yes (Threshold) | Hit Target | 998 | 4 (Round up) | 1002 | 0 (Lands on tick) |
| 1001 | 998 | No (Short by 1) | Missed Target | 997 | 4 (Exhausted) | 1001 | 0 (Aborts before tick) |
| 1000 | 997 | Yes (Exact) | Hit Target | 997 | 3 (Exact div) | 1000 | 0 (Lands on tick) |
4. The 1 wei cliff and aborted tick transitions
As the table demonstrates, shifting from 1002 to 1001 causes a stark operational cliff over a single wei:
- Curve Intake Drop: 1002 delivers 998 to the curve, whereas 1001 drops intake to 997;
- Tick Transition Aborted: 1002 hits
step.tickNextand executesticks.cross; 1001 halts before the boundary, skipping active liquidity updates; - Arbitrage Simulation Distortion: Simulators assuming continuous curves anticipate liquidity transitions at 1001, but on-chain reality traps the trade inside the old interval, causing unexpected slippage.
5. Standardized protocol for step fee verification
When auditing swap trace logs or building off-chain routers, adhere to this evaluation protocol:
- Read
amountRemainingand target intakeamountInfor the current step; - Compute net available input via
amountRemaining * (1e6 - feePips) / 1e6with downward truncation; - If net input < target
amountIn, classify as missed target: set curve intake to net input and fee toamountRemaining - netInput; - If net input ≥ target
amountIn, classify as hit target: lock curve intake to targetamountInand re-compute fee viaamountIn * feePips / (1e6 - feePips)with upward rounding, forwarding residual tokens.
This is an analytical framework. Branch mechanics originate from SwapMath.sol and UniswapV3Pool.sol v1.0.0. The figures 998, 4, 1002, and 997 represent exact integer derivations and do not constitute investment advice.
6. Common misreadings
Why can't single-step swap fees always be calculated as input times fee rate?
Because large cross-tick swaps are sliced across multiple price ranges. If a step hits the target tickNext, it must consume only the exact amount required to reach that boundary, leaving all remaining input intact for subsequent steps. Therefore, fees upon hitting the target are re-derived from curve input; only when input is exhausted inside the range is the fee set to total remaining input minus curve input.
Why is the fee divisor 1000000 minus feePips when hitting the target price?
Because the known base quantity is the net token amount amountIn entering the curve, not the gross payment. The fee is stacked outside this net amount, which follows the exact-output formula: FullMath.mulDivRoundingUp(amountIn, feePips, 1e6 - feePips).
Why does inputting 1001 fail to deliver 998 to the curve?
Because delivering 998 requires paying 1002: 998 times 3000 divided by 997000 leaves quotient 3 and remainder 3000, rounding the fee up to 4. Passing 1001 cannot afford 1002, so it falls into the missed-target branch. There, deducting fee via 1001 times 997000 divided by 1000000 truncates to 997. Falling short by 1 wei reduces curve intake and aborts the tick cross.