Price will not always be 18 decimals, as expected and outlined in the comments
The price() function returns a price with the same number of decimals as the base token, not the expected 18 decimals. This causes a large mismatch between expected and actual prices when the base token has fewer than 18 decimals, such as USDC.
obront Follow with cryptostellar5, Tricko and 8 others
Originally disclosed on Code4rena
Submitted by obront, also found by cryptostellar5, Tricko, CRYP70, 0xmuxyz, koxuan, 8olidity, yixxas, cozzetti, ktg, and ladboy233
The price() function is expected to return the price of one fractional tokens, represented in base tokens, to 18 decimals of precision. This is laid out clearly in the comments:
/// @notice The current price of one fractional token in base tokens with 18 decimals of precision.
/// @dev Calculated by dividing the base token reserves by the fractional token reserves.
/// @return price The price of one fractional token in base tokens * 1e18.
However, the formula incorrectly calculates the price to be represented in whatever number of decimals the base token is in. Since there are many common base tokens (such as USDC) that will have fewer than 18 decimals, this will create a large mismatch between expected prices and the prices that result from the function.
Proof of Concept
Prices are calculated with the following formula, where ONE = 1e18:
return (_baseTokenReserves() * ONE) / fractionalTokenReserves();
We know that fractionalTokenReserves will always be represented in 18 decimals. This means that the ONE and the fractionalTokenReserves will cancel each other out, and we are left with the baseTokenReserves number of decimals for the final price.
As an example:
- We have
$1000USDC in reserves, which at 6 decimals is 1e9 - We have 1000 fractional tokens in reserve, which at 18 decimals is 1e21
- The price calculation is
1e9 * 1e18 / 1e21 = 1e6 - While the value should be 1 token, the 1e6 will be interpreted as just 1/1e12 tokens if we expect the price to be in 1e18
Recommended Mitigation Steps
The formula should use the decimals value of the baseToken to ensure that the decimals of the resulting price ends up with 18 decimals as expected:
return (_baseTokenReserves() * 10 ** (36 - ERC20(baseToken).decimals()) / fractionalTokenReserves();
This will multiple baseTokenReserves by 1e18, and then additionally by the gap between 1e18 and its own decimals count, which will result in the correct decimals value for the outputted price.
outdoteth (Caviar) confirmed and commented:
Fixed in: https://github.com/outdoteth/caviar/pull/5
Always ensure that the exponent is 18 greater than the denominator.
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 price calculation multiplies by a constant 1e18 instead of adjusting for the base token's actual decimals, so the result inherits the base token's decimal count rather than always having 18 decimals.
Impact
Prices returned by the function could be misinterpreted, leading to incorrect financial decisions or trades, as the price is off by a factor of 10^(18 - baseTokenDecimals).
What the attacker needed
Base token has fewer than 18 decimals
The fix
The formula should be updated to use the base token's decimals to ensure the output always has 18 decimals, and this was fixed in a pull request.
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.
Smart contracts often use fixed-point numbers to represent prices, with a set number of decimal places. Here, the function was supposed to always return a price with 18 decimal places, but it actually returned a price with as many decimal places as the base token (e.g., 6 for USDC). This means anyone reading the price would get a number that is off by a huge factor, potentially leading to incorrect trades or valuations.