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.
The function applies weights to values but not to durations:
This creates a mathematical inconsistency in the final calculation:
The numerator (totalWeightedSum) includes weights while the denominator (totalDuration) does not, breaking the mathematical principles of weighted averages.
The function returns mathematically incorrect time-weighted averages
All calculations involving different weights produce wrong results
The error magnitude increases with greater disparity between period weights
Applications relying on this function receive incorrect data for decision-making
Manual code review
Apply weights to duration calculations:
Remove the 1e18 scaling factor from the final calculation as both numerator and denominator will be weighted:
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.