Skip to content
Audit finding Intermediate

Precision loss in `boostPrice` in `V3AMO`

The boostPrice function in SolidlyV3AMO suffers from precision loss when the square root price is at or above Q96, causing the computed price to round down to 1 instead of the correct value. This incorrect price causes the unfarmBuyBurn function to revert because the price check fails.

0x37 Follow with Bigsam, durov and 2 others

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2024-10-axion-judging/issues/191

Found by

0x37, Bigsam, durov, s1ce, yuza101

Summary

There is precision loss in a specific case in SolidlyV3AMO, which leads to incorrect value of boostPrice being reported. This leads to issues with upstream functions that use it.

Root Cause

https://github.com/sherlock-audit/2024-10-axion/blob/main/liquidity-amo/contracts/SolidlyV3AMO.sol#L343

Consider the following code:

Solidity
function boostPrice() public view override returns (uint256 price) {
        (uint160 _sqrtPriceX96, , , ) = ISolidlyV3Pool(pool).slot0();
        uint256 sqrtPriceX96 = uint256(_sqrtPriceX96);
        if (boost < usd) {
            price = (10 ** (boostDecimals - usdDecimals + PRICE_DECIMALS) * sqrtPriceX96 ** 2) / Q96 ** 2;
        } else {
            if (sqrtPriceX96 >= Q96) {
                // @audit: massive precision loss here
                price = 10 ** (boostDecimals - usdDecimals + PRICE_DECIMALS) / (sqrtPriceX96 ** 2 / Q96 ** 2);
            } else {
                price = (10 ** (boostDecimals - usdDecimals + PRICE_DECIMALS) * Q96 ** 2) / sqrtPriceX96 ** 2;
            }
        }
    }

Notice that in this specific clause:

Solidity
if (sqrtPriceX96 >= Q96) {
                // @audit: massive precision loss here
                price = 10 ** (boostDecimals - usdDecimals + PRICE_DECIMALS) / (sqrtPriceX96 ** 2 / Q96 ** 2);
            }

We divide (sqrtPriceX96 ** 2 / Q96 ** 2). However, consider a case where the value of the stablecoin is 20% higher than the value of boost; in this case a price of around 0.8 should be reported by the function but because (sqrtPriceX96 ** 2 / Q96 ** 2) will round down to 1, the function will end up reporting a price of 1.

Internal pre-conditions

No response

External pre-conditions

We need the price of the stablecoin to be at least a bit higher than the price of BOOST for this to be relevant

Attack Path

Described above; precision loss leads to boostPrice calculation reported by SolidlyV3AMO being incorrect.

Impact

Impact is that, because the price is reported incorrect, public calls to unfarmBuyBurn() will fail because the following check will fail:

if (newBoostPrice > boostUpperPriceBuy) revert PriceNotInRange(newBoostPrice);

newBoostPrice will be reported as 1 even though it should be much lower.

PoC

First, set sqrtPriceX96 to 87150978765690771352898345369 (10% above 2^96)

TypeScript
pool = await ethers.getContractAt("ISolidlyV3Pool", poolAddress);
await pool.initialize(sqrtPriceX96);

Then:

TypeScript
it("Showcases the incorrect price that is returned", async function() {
        console.log(await solidlyV3AMO.boostPrice()) // 1000000
})

Mitigation

No response

Discussion

sherlock-admin2

The protocol team fixed this issue in the following PRs/commits: https://github.com/AXION-MONEY/liquidity-amo/pull/8

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

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

Likely the integer division in the calculation `(sqrtPriceX96 ** 2 / Q96 ** 2)` rounds down to 1 when the numerator is slightly larger than the denominator, causing a massive precision loss.

Impact

Public calls to unfarmBuyBurn() will fail because the price check reverts when newBoostPrice is incorrectly reported as 1.

What the attacker needed

Price of stablecoin higher than BOOST

The fix

The protocol team fixed this issue in a pull request.

How it was found

  1. Set sqrtPriceX96 to a value 10% above 2^96.
  2. Call boostPrice() and observe it returns 1000000 instead of the correct lower value.

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 describes a smart contract bug where a price calculation loses precision due to integer division. When the price of a stablecoin is higher than the price of the BOOST token, the function incorrectly rounds down to 1 instead of a smaller decimal value, which then breaks another function that relies on this price.

More by 0x37

View all

79 more disclosed reports, best first.

More from AXION

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.