Verification checklist

  • SqrtPriceMath.getAmount0Delta and getAmount1Delta rely on roundUp to dictate truncation direction. Inflows pass true, outflows pass false.
  • For liquidity 1000 across sqrt price 100 to 101, theoretical token delta is 1000 / 10100 ≈ 0.099.
  • Inflows trigger divRoundingUp: quotient 0 with remainder 1000 rounds up to 1 wei; outflows trigger integer division: quotient 0 truncates to 0 wei.
  • Round-tripping through the same price interval enforces an irreversible 1 wei scissors gap, eliminating pool-draining precision exploits.

1. The essence of roundUp: the pool never loses

In Uniswap V3, whenever price transitions from one square-root ratio to another, the pool computes the exact token delta required. Whether minting, burning, or swapping, all delta calculations funnel into two core functions inside SqrtPriceMath.sol: getAmount0Delta and getAmount1Delta.

Both functions accept a critical boolean parameter: roundUp. Inside UniswapV3Pool.sol, passing this argument adheres to an unyielding security invariant:

// UniswapV3Pool.sol swap excerpt
// When the pool charges tokens from the user:
amount0 = SqrtPriceMath.getAmount0Delta(sqrtRatioTargetX96, state.sqrtPriceX96, state.liquidity, true);

// When the pool pays tokens to the user:
amount1 = SqrtPriceMath.getAmount1Delta(state.sqrtPriceX96, sqrtRatioTargetX96, state.liquidity, false);

This design is known as "strictly in favor of the pool": whenever tokens flow into the pool, roundUp is true, forcing any non-zero remainder to round up by 1 wei. Whenever tokens flow out of the pool, roundUp is false, truncating fractional units to zero. The pool never pays out or undercharges a single wei due to fixed-point rounding.

2. Price moving from 100 to 101: theoretical delta of 0.099

To inspect this rounding mechanism with exact arithmetic, consider an isolated scenario: virtual liquidity L = 1000, with square-root price moving between sqrtRatioA = 100 and sqrtRatioB = 101.

According to the concentrated liquidity invariant, the theoretical delta for token0 is:

Δx = L * (sqrtRatioB - sqrtRatioA) / (sqrtRatioA * sqrtRatioB)

Substituting the numbers:

Δx = 1000 * (101 - 100) / (100 * 101) = 1000 / 10100 ≈ 0.0990099...

In real-number math, the theoretical token amount is less than 0.1. But in the EVM's integer world, whether this evaluates to 0 or 1 is decided entirely by roundUp.

3. Inflows round up to 1, outflows truncate to 0

Tracing SqrtPriceMath.getAmount0Delta reveals the conditional execution branch:

// SqrtPriceMath.sol
return
    roundUp
        ? UnsafeMath.divRoundingUp(
            FullMath.mulDivRoundingUp(numerator1, numerator2, sqrtRatioBX96),
            sqrtRatioAX96
        )
        : FullMath.mulDiv(numerator1, numerator2, sqrtRatioBX96) / sqrtRatioAX96;

Numerator product numerator1 * numerator2 equals 1000, while the divisor is 10100.

  1. When the pool receives token0 (roundUp = true): Dividing 1000 by 10100 yields quotient 0 and remainder 1000. With remainder > 0, divRoundingUp increments the quotient to 0 + 1 = 1. The user must transfer 1 wei.
  2. When the pool pays token0 (roundUp = false): Dividing 1000 by 10100 yields quotient 0. Standard division truncates the remainder, yielding 0. The pool transfers 0 wei.
Operation DirectionLiquidity LPrice IntervalTheoretical DeltaroundUp FlagMath RoutineFinal Integer Settlement
Pool Receives Tokens (In)1000[100, 101]0.0990099...true (Up)divRoundingUp1 wei
Pool Pays Tokens (Out)1000[100, 101]0.0990099...false (Down)mulDiv / div0 wei

Under the exact same liquidity and price displacement, reversing the transfer direction flips the outcome between 1 and 0. This directional bias secures fixed-point protocol reserves.

4. The 1 wei scissors gap and anti-drain defense

Consider an arbitrageur attempting a round-trip swap across this identical interval: first pushing price from 100 to 101, then immediately returning it from 101 to 100.

Under an idealized zero-fee model, net capital should be perfectly conserved. However, in Uniswap V3's integer mechanics:

Paid to Pool (Inflow):     1 wei
Received from Pool (Outflow): 0 wei
Net Round-trip Loss:       1 wei

Without asymmetric rounding, if inflows also truncated downward, traders would pay 0 wei and receive 0 wei. More dangerously, if any division branch erroneously rounded in the user's favor, an attacker could trigger millions of micro-swaps to extract 1 wei per loop, executing a "rounding drain attack".

By forcing inflows to round up and outflows to truncate, Uniswap V3 ensures that every round-trip across fractional thresholds permanently surrenders at least 1 wei to pool reserves.

5. Drift pitfalls in off-chain simulation engines

When developing high-frequency market-making algorithms or offline backtesters, engineers frequently simulate SqrtPriceMath using standard floating-point operations or uniform integer division (//), neglecting the roundUp flag.

While negligible in isolated macro trades, this simplification causes cumulative distortion in high-frequency environments:

  1. Grid Trading Strategies: Rapidly executing orders across narrow tick bands accumulates fictitious paper gains in float simulators, whereas live execution suffers continuous 1-2 wei rounding drag;
  2. Micro-Arbitrage Detection: Bots detecting sub-cent arbitrage fail on-chain because contracts demand an additional 1 wei on deposit and truncate 1 wei on withdrawal, causing transaction reverts (e.g., IIA);
  3. Reserve Curve Desynchronization: Backtests ignoring divRoundingUp gradually desynchronize token balances from the underlying invariant curve.

When auditing token delta calculations, verify transfer direction: deposits must trigger divRoundingUp, while withdrawals must truncate. Across liquidity 1000 and range [100, 101], inflows must equal 1 wei and outflows must equal 0 wei.

This is an analytical framework. Rounding and truncation mechanics stem from SqrtPriceMath.sol and FullMath.sol v1.0.0. The integers 1, 0, and 0.099 represent exact fixed-point results and do not constitute investment advice.

6. Common misreadings

Why can't SqrtPriceMath simply use standard half-up rounding?

Because standard rounding truncates below 0.5 and rounds up above 0.5. If the pool truncates on inflows, traders pay less than required by the invariant curve; if it rounds up on outflows, the pool pays more than it holds. In high-frequency environments, attackers could craft micro-trades to drain token reserves using rounding biases.

Does paying 1 and receiving 0 mean traders always lose value?

In macro trades, a 1 wei discrepancy across 18 decimals is negligible. But in micro-trades, dust orders, or tight-range arbitrage, this 1 wei determines economic feasibility. It serves as an essential defense and a fixed cost that simulation engines must account for.

Does token1 have the same directional rounding protection?

Yes. getAmount1Delta also receives the roundUp parameter. It calculates liquidity times price delta divided by 2^96. When roundUp is true, it calls mulDivRoundingUp; when false, it calls mulDiv. The pool strictly enforces rounding up on inflows and truncation on outflows for both token0 and token1.