Skip to content
Smart contracts Audit finding Intermediate

Incorrect price for negative ticks due to lack of rounding down

The function that calculates the time-weighted average price tick from Uniswap observations did not round down when the tick cumulative difference was negative, causing the returned tick to be larger than it should be. This could lead to incorrect prices being used in the protocol.

josephdara Follow with Tigerfrake, Naresh and 14 others

Originally disclosed on Code4rena

Submitted by josephdara, also found by Rhaydden (1, 2), Tigerfrake, Naresh, Bauchibred, ayden, ZanyBonzy, SBSecurity, kodyvim, WinSec, Kaysoft, Giorgio, jolah1, 0xhashiman, 0xabhay, Sparrow, 0xhere2learn, and grearlake

The function callUniswapObserve is used to get twap price tick using IUniswapV3PoolOracle.observe.selector which is then used to calculate the int24 tick.

The problem is that in case if (tickCumulatives[1] - tickCumulatives[0]) is negative, the tick should be rounded down as it’s done in the OracleLibrary from uniswap.

As result, in case if (tickCumulatives[1] - tickCumulatives[0])is negative and (tickCumulatives[1] - tickCumulatives[0]) % secondsAgo != 0, then returned tick will be bigger then it should be, hence incorrect prices would be used.

Proof of Concept

Unihelper:::callUniswapObserve():

        int56[] memory tickCumulatives = abi.decode(data, (int56[]));

        int24 tick = int24((tickCumulatives[1] - tickCumulatives[0]) / int56(int256(ago)));

        uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(tick);

In Uniswap’s OracleLibrary:::consult():

        int56 tickCumulativesDelta = tickCumulatives[1] - tickCumulatives[0];
        uint160 secondsPerLiquidityCumulativesDelta =
            secondsPerLiquidityCumulativeX128s[1] - secondsPerLiquidityCumulativeX128s[0];

        arithmeticMeanTick = int24(tickCumulativesDelta / secondsAgo);
        // Always round to negative infinity
        if (tickCumulativesDelta < 0 && (tickCumulativesDelta % secondsAgo != 0)) arithmeticMeanTick--;

Tools Used

Solodit

Recommended Mitigation Steps

Round down the int24 tick:

if (tickCumulativesDelta < 0 && (tickCumulativesDelta % secondsAgo != 0)) tick--;

Assessed type

Math

syuhei176 (Predy) confirmed via duplicate Issue #65

0xsomeone (judge) commented:

The Warden and its peers have demonstrated that the DEX price feed calculation does not round properly, resulting in a deviation of as much as one tick which, depending on the spacing of the pool, can be significant.

As such, I believe a medium risk rating is appropriate for this submission.


The original report as published on Code4rena, by josephdara. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The code used integer division without rounding towards negative infinity when the tick cumulative difference was negative, unlike Uniswap's reference implementation.

Impact

The incorrect tick could cause the protocol to use a price that is up to one tick too high, which may lead to financial losses depending on the pool's tick spacing.

What the attacker needed

Negative tick cumulative difference; non-zero remainder from division

The fix

The code should round down the tick when the tick cumulative difference is negative and not evenly divisible by the time elapsed, as done in Uniswap's OracleLibrary.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This report is about a smart contract that reads price information from a decentralised exchange. When the price difference is negative, the code rounded the number in a way that made the price slightly too high. That small error could affect trades or calculations that rely on the price.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by josephdara

View all

4 more disclosed reports, best first.

More from Predy

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.