Core Contracts

Regnum Aurum Acquisition Corp
HardhatReal World AssetsNFT
77,280 USDC
View results
Submission Details
Severity: medium
Valid

Incorrect Time-Weighted average calculation due to inconsistent weight application

Summary

The calculateTimeWeightedAverage function in the TimeWeightedAverage library applies weights incorrectly, resulting in mathematically inaccurate time-weighted averages. The function applies weights to the value component but fails to apply corresponding weights to the duration component.

Vulnerability Details

totalDuration += duration;

The function applies weights to values but not to durations:

totalWeightedSum += timeWeightedValue * periods[i].weight; // Weights applied
totalDuration += duration; // Weights missing

This creates a mathematical inconsistency in the final calculation:

return totalDuration == 0 ? 0 : totalWeightedSum / (totalDuration * 1e18);

The numerator (totalWeightedSum) includes weights while the denominator (totalDuration) does not, breaking the mathematical principles of weighted averages.

Impact

  1. The function returns mathematically incorrect time-weighted averages

  2. All calculations involving different weights produce wrong results

  3. The error magnitude increases with greater disparity between period weights

  4. Applications relying on this function receive incorrect data for decision-making

Tools Used

Manual code review

Recommendations

  1. Apply weights to duration calculations:

totalDuration += duration * periods[i].weight;
  1. Remove the 1e18 scaling factor from the final calculation as both numerator and denominator will be weighted:

-
return totalDuration == 0 ? 0 : totalWeightedSum / (totalDuration * 1e18);
+
return totalDuration == 0 ? 0 : totalWeightedSum / totalDuration;
Updates

Lead Judging Commences

inallhonesty Lead Judge 7 months ago
Submission Judgement Published
Validated
Assigned finding tags:

TimeWeightedAverage library fails to consistently apply weights in calculations, causing incorrect time-weighted averages

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.

Give us feedback!