Precision loss in VaultReader.sol due to the use of fixed-point arithmetic and division operations in calculations. Improper handling of these scaling factors or division operations can lead to rounding errors, which compound over time and result in incorrect calculations.
Division in expectedSizeInTokensDelta
If prices.indexTokenPrice.min is large, the division will truncate the result, leading to a loss of precision.
For example, if sizeDeltaInUsd = 100 and prices.indexTokenPrice.min = 3, the result will be 33 instead of 33.333....
Multiplication and Division in priceImpactInCollateralTokens
If priceImpactInTokens is small and prices.shortTokenPrice.min is large, the division will truncate the result, leading to precision loss.
For example, if priceImpactInTokens = 1, prices.indexTokenPrice.min = 1000, and prices.shortTokenPrice.min = 3, the result will be 333 instead of 333.333....
Precision loss can lead to incorrect calculations of the price impact in collateral tokens.
Liquidity providers may suffer losses if the price impact is underestimated or overestimated.
Precision loss can erode user trust in the system.
Manual code review
Increase the precision scaling factor ( 1e36 instead of 1e30) to reduce the impact of rounding errors.
Perform division operations as late as possible in calculations to minimize precision loss.
Implement rounding functions to ensure that results are rounded appropriately (e.g., rounding up or down based on the context).
GMX github documentation: “Prices stored within the Oracle contract represent the price of one unit of the token using a value with 30 decimals of precision. Representing the prices in this way allows for conversions between token amounts and fiat values to be simplified, e.g. to calculate the fiat value of a given number of tokens the calculation would just be: token amount * oracle price, to calculate the token amount for a fiat value it would be: fiat value / oracle price.” Sponsor confirmed the keeper does the same, so price decimals change in function of the token, to be sure the above rule is true. Example for USDC (6 decimals): Prices will have 24 decimals → 1e6 * 1e24 = 1e30. Just a reminder for some submissions: shortToken == collateralTokens, so the decimals is 1e24 for shortToken prices.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.