Incorrect price representation
The BondOracleAdapter's latestRoundData function returns a price in PriceX96 format without converting it to a decimal-based format (e.g., 18 decimals). This causes the returned price to be vastly different from the expected value, leading to incorrect asset valuations and potentially financial losses for users.
Source: https://github.com/sherlock-audit/2024-12-plaza-finance-judging/issues/1016
Found by
0x23r0, 0xc0ffEE, 10ap17, Ryonen, wickie
Summary
The latestRoundData() function in the BondOracleAdapter contract calculates a price using the PriceX96 format. However, the returned price is not converted to a format with a specified decimal precision (e.g., 18 decimals, as in a Chainlink oracle). As a result, the price returned by the function will be completely different from the expected decimal-based price format. This discrepancy leads to significant mistakes in asset valuation and renders every calculation using this price incorrect.
function latestRoundData()
external
view
returns (uint80, int256, uint256, uint256, uint80){
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = twapInterval; // from (before)
secondsAgos[1] = 0; // to (now)
(int56[] memory tickCumulatives, ) = ICLPool(dexPool).observe(secondsAgos);
uint160 getSqrtTwapX96 = TickMath.getSqrtRatioAtTick(
int24((tickCumulatives[1] - tickCumulatives[0]) / int56(uint56(twapInterval)))
);
return (uint80(0), int256(getPriceX96FromSqrtPriceX96(getSqrtTwapX96)), block.timestamp, block.timestamp, uint80(0));
}
Root Cause
In BondOracleAdapter's latestRoundData function the calculated price is not converted from priceX96 format to format with decimals (e.g. 18 decimals), which will result that the returned price is fundamentally different from what is required for correct asset valuation and subsequent calculations.
Internal Pre-conditions
No response
External Pre-conditions
No response
Attack Path
- The
latestRoundData()function will be called. - The function will calculate the price in the
PriceX96format. - The returned price will not be converted to a format with decimals of precision (e.g., 18 decimals).
- Since the returned format is incorrect, the valuation of asset will be wrong.
- All dependent calculations, that use the returned price, will produce incorrect results.
Impact
The failure to return a price in the correct format causes incorrect asset valuations, which would lead to incorrect calculations, potentially causing financial losses for users.
PoC
Step 1: Fetch sqrtPriceX96 from the Pool Contract We will be using usdc/weth pool (random pool on polygon network, which doesn't matter, since we are just proving concept)
Contract Address: 0x45dda9cb7c25131df268515131f647d726f50608 Function: slot0() Output: sqrtPriceX96 = 1390618563380010078436460929963734
Step 2: Calculate priceX96 from sqrtPriceX96 Function: getPriceX96FromSqrtPriceX96
function getPriceX96FromSqrtPriceX96(sqrtPriceX96) {
return FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);
}
Output: 24408239790603697707980053009065971527
This result is result that would be return form latestRoundData function, by current implementation, which is nowhere near the realistic price of weth in usdc token.
Step 3: Convert priceX96 to Actual Price with 18 Decimals Next step, is to convert that price to the actual price that is in 18 decimals, since usdc has 6 decimals, and weth has 18, the formula would be:
FullMath.mulDiv(10 ** (18 + decimals1 - decimals0), FixedPoint96.Q96, priceX96)
Output: 3245959692053023671448
The result is in 18 decimals and represents around 3245,95 usdc fer weth.
Mitigation
Consider using the approach used by the Saltio.IO project, which ensures proper conversion to the 18-decimal (in this case) standard. The implementation is as follows:
// Returns the price of token0 * (10**18) in terms of token1
function _getUniswapTwapWei(IUniswapV3Pool pool, uint256 twapInterval) public view returns (uint256) {
uint32;
secondsAgo[0] = uint32(twapInterval); // from (before)
secondsAgo[1] = 0; // to (now)
// Get the historical tick data using the observe() function
(int56[] memory tickCumulatives, ) = pool.observe(secondsAgo);
int24 tick = int24((tickCumulatives[1] - tickCumulatives[0]) / int56(uint56(twapInterval)));
uint160 sqrtPriceX96 = TickMath.getSqrtRatioAtTick(tick);
// Convert the sqrtPriceX96 to a price with 18 decimals
uint256 p = FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);
uint8 decimals0 = ERC20(pool.token0()).decimals();
uint8 decimals1 = ERC20(pool.token1()).decimals();
if (decimals1 > decimals0) {
return FullMath.mulDiv(10 ** (18 + decimals1 - decimals0), FixedPoint96.Q96, p);
}
if (decimals0 > decimals1) {
return (FixedPoint96.Q96 * (10 ** 18)) / (p * (10 ** (decimals0 - decimals1)));
}
return (FixedPoint96.Q96 * (10 ** 18)) / p;
}
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/Convexity-Research/plaza-evm/pull/158
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 latestRoundData function fails to convert the PriceX96 value to a decimal-based format with 18 decimals.
Impact
The failure to return a price in the correct format causes incorrect asset valuations, which could lead to incorrect calculations and potentially financial losses for users.
What the attacker needed
None
The fix
The protocol team fixed this issue in a pull request.
How it was found
- The latestRoundData() function is called.
- The function calculates the price in PriceX96 format.
- The returned price is not converted to a format with decimals of precision.
- The incorrect format leads to wrong asset valuation.
- All dependent calculations produce incorrect results.
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.
A smart contract returns a price in a special fixed-point format called PriceX96, which is not the usual decimal format that other contracts expect. Because the price is not converted, any calculation using it will be wrong, like using a price in cents instead of dollars.