function _updateWeights(uint256 newWeight) internal {
uint256 currentTime = block.timestamp;
uint256 duration = getPeriodDuration();
if (weightPeriod.startTime == 0) {
// For initial period, start from next period boundary
uint256 nextPeriodStart = ((currentTime / duration) + 1) * duration;
TimeWeightedAverage.createPeriod(
weightPeriod,
nextPeriodStart,
duration,
newWeight,
WEIGHT_PRECISION
);
} else {
- // For subsequent periods, ensure we're creating a future period
- uint256 nextPeriodStart = ((currentTime / duration) + 1) * duration;
- TimeWeightedAverage.createPeriod(
- weightPeriod,
- nextPeriodStart,
- duration,
- newWeight,
- WEIGHT_PRECISION
- );
+ // If current period has ended, create new period
+ if (currentTime >= getCurrentPeriodStart() + duration) {
+ TimeWeightedAverage.createPeriod(
+ weightPeriod,
+ currentTime,
+ duration,
+ newWeight,
+ WEIGHT_PRECISION
+ );
+ return;
+ }
+
+ TimeWeightedAverage.updateValue(
+ weightPeriod,
+ newWeight,
+ block.timestamp
+ );
}
}
Weights aren't properly updated which can impact reward distribution.