getCalculatedFee() performs two sequential divisions instead of combining them into a single final division, which truncates intermediate results and causes the computed fee to be systematically lower than the mathematically correct value.
Flash-loan fees are computed in two steps: first the borrowed amount is converted to a WETH-denominated value (dividing by s_feePrecision), then the fee rate is applied (dividing by s_feePrecision again). Integer arithmetic truncates after each division, so precision is lost twice.
Dividing before multiplying is a well-known Solidity anti-pattern. Multiplying all numerators together before performing a single combined division preserves the maximum number of significant bits and minimises rounding loss.
Likelihood:
Every single flash loan call invokes getCalculatedFee(), so the precision loss applies unconditionally on every transaction.
Loans whose amount * price is not a clean multiple of s_feePrecision (the common case) experience rounding loss on the first division, lowering the base for the second multiplication and compounding the error.
Impact:
Liquidity providers receive slightly less fee revenue than the protocol formula intends on every flash loan, with the shortfall growing in aggregate over time.
The lost amount per transaction is bounded (at most s_flashLoanFee / s_feePrecision units = a few wei), making this low-severity, but the accumulated deficit is non-trivial across high-volume usage.
Choose values that expose the truncation clearly:
Each individual loss is sub-wei, but the pattern guarantees the protocol always rounds against liquidity providers rather than neutrally, and cumulative leakage over millions of loans is measurable.
Combine both multiplications before performing a single division. This is the standard fix for the divide-before-multiply anti-pattern in Solidity.
Apply the same fix to ThunderLoanUpgraded.sol. Note: s_feePrecision * s_feePrecision = 1e36, which fits comfortably in a uint256 for all realistic token amounts.
## Description In a manual review of the ThunderLoan.sol contract, it was discovered that the mathematical operations within the getCalculatedFee() function do not handle precision appropriately. Specifically, the calculations in this function could lead to precision loss when processing fees. This issue is of low priority but may impact the accuracy of fee calculations. ## Vulnerability Details The identified problem revolves around the handling of mathematical operations in the getCalculatedFee() function. The code snippet below is the source of concern: ``` uint256 valueOfBorrowedToken = (amount * getPriceInWeth(address(token))) / s_feePrecision; fee = (valueOfBorrowedToken * s_flashLoanFee) / s_feePrecision; ``` The above code, as currently structured, may lead to precision loss during the fee calculation process, potentially causing accumulated fees to be lower than expected. ## Impact This issue is assessed as low impact. While the contract continues to operate correctly, the precision loss during fee calculations could affect the final fee amounts. This discrepancy may result in fees that are marginally different from the expected values. ## Recommendations To mitigate the risk of precision loss during fee calculations, it is recommended to handle mathematical operations differently within the getCalculatedFee() function. One of the following actions should be taken: Change the order of operations to perform multiplication before division. This reordering can help maintain precision. Utilize a specialized library, such as math.sol, designed to handle mathematical operations without precision loss. By implementing one of these recommendations, the accuracy of fee calculations can be improved, ensuring that fees align more closely with expected values.
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.