The getOwnValuation() function contains errors in the price calculation
The getOwnValuation() function in USSDRebalancer calculates the pool price incorrectly when USSD is one of the tokens, due to wrong decimal scaling in the sqrtPriceX96 formula. This produces inaccurate asset valuations that could affect trading and liquidity decisions.
Source: https://github.com/sherlock-audit/2023-05-USSD-judging/issues/222
Found by
0xPkhatri, 0xpinky, AlexCzm, Bauer, J4de, carrotsmuggler, kiki_dev, peanuts, sam_gmk, sashik_eth, simon135, theOwl, twicek, warRoom
Summary
The getOwnValuation() function in the provided code has incorrect price calculation logic when token0() or token1() is equal to USSD. The error leads to inaccurate price calculations.
Vulnerability Detail
The USSDRebalancer.getOwnValuation() function calculates the price based on the sqrtPriceX96 value obtained from the uniPool.slot0() function. The calculation depends on whether token0() is equal to USSD or not.
If token0() is equal to USSD, the price calculation is performed as follows:
price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))/(1e6) >> (96 * 2);
However,there is an error in the price calculation logic. The calculation should be:
price = uint(sqrtPriceX96) * uint(sqrtPriceX96) * 1e6 >> (96 * 2);
If token0() is not equal to USSD, the price calculation is slightly different:
price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))*(1e18 /* 1e12 + 1e6 decimal representation */) >> (96 * 2);
// flip the fraction
price = (1e24 / price) / 1e12;
The calculation should be:
price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))*(1e6 /* 1e12 + 1e6 decimal representation */) >> (96 * 2);
// flip the fraction
price = (1e24 / price) / 1e12;
Reference link: https://blog.uniswap.org/uniswap-v3-math-primer
Impact
The incorrect price calculation in the getOwnValuation() function can lead to significant impact on the valuation of assets in the UniSwap V3 pool. The inaccurate prices can result in incorrect asset valuations, which may affect trading decisions, liquidity provision, and overall financial calculations based on the UniSwap V3 pool.
Code Snippet
https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L74 https://github.com/sherlock-audit/2023-05-USSD/blob/main/ussd-contracts/contracts/USSDRebalancer.sol#L76
Tool used
Manual Review
Recommendation
When token0() is USSD, the correct calculation should be uint(sqrtPriceX96) * uint(sqrtPriceX96) * 1e6 >> (96 * 2). When token1() is USSD, the correct calculation should be
price = uint(sqrtPriceX96)*(uint(sqrtPriceX96))*(1e6 /* 1e12 + 1e6 decimal representation */) >> (96 * 2);
// flip the fraction
price = (1e24 / price) / 1e12;
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 decimal scaling factor in the sqrtPriceX96 price formula is wrong: it divides by 1e6 instead of multiplying by 1e6 in one branch, and uses 1e18 instead of 1e6 in the other.
Impact
The incorrect price could lead to inaccurate asset valuations, which may affect trading decisions, liquidity provision, and financial calculations based on the pool.
What the attacker needed
USSD is token0 or token1 in the pool
The fix
The calculation should be fixed to multiply by 1e6 in both branches, as shown in the recommendation.
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 is a smart contract bug in a price calculation. The code tries to work out the value of a token from a pool's internal price number, but it multiplies or divides by the wrong power of ten. That makes the reported price wrong, which could mislead anyone relying on it.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.