The _totalAmount function suffers from precision loss due to integer division in Solidity. When converting the indexToken balance and positionData.netValue into collateral token units, the calculations involve division by prices.shortTokenPrice.min. However, Solidity truncates decimal values instead of rounding them, leading to a gradual loss of value over multiple transaction.
The multiplication of indexToken balance and prices.indexTokenPrice.min is performed before division by prices.shortTokenPrice.min.
Solidity truncates the result instead of rounding it, leading to a slight loss of precision accumulating overtime anytime totalAmount function is called.
The same issue occurs when dividing positionData.netValue by prices.shortTokenPrice.min.
Scenario:
Vault holds indexToken: 500,000 ETH
Vault holds collateralToken: $2,000,000 USDC
Open position net value (positionData.netValue): $3,000,000
ETH Price
(prices.indexTokenPrice.min): $1,750
Collateral Token Price (prices.shortTokenPrice.min): $1.2
Step 1:
Convert indexToken Balance to Collateral Units
Step 2:
Step 3:
No loss in this step, as the values divide evenly.
Step 4:
Analysis:
Expected: 733,666,666.67
Actual: 733,666,666
Precision lost: 0.67 per transaction
Imagine 1,000 transaction per day * 356 a year
= 356,000 transaction in a year
0.67 * 356,000 = $238,520 lost over time
Over time, vault users lose significant funds due to precision loss, causing inaccurate NAV calculations and unexpected liquidation risks.
Manual Review
To prevent precision loss, multiply by 1e30 before division and scale back after:
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelihood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
There is no real proof, concrete root cause, specific impact, or enough details in those submissions. Examples include: "It could happen" without specifying when, "If this impossible case happens," "Unexpected behavior," etc. Make a Proof of Concept (PoC) using external functions and realistic parameters. Do not test only the internal function where you think you found something.
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.