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.
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
Consider the following code:
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:
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)
pool = await ethers.getContractAt("ISolidlyV3Pool", poolAddress);
await pool.initialize(sqrtPriceX96);
Then:
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
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
- Set sqrtPriceX96 to a value 10% above 2^96.
- 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.