diff --git a/src/contracts/aludel/AludelV3Lib.sol b/src/contracts/aludel/AludelV3Lib.sol index 8f57ab5..f9ad700 100644 --- a/src/contracts/aludel/AludelV3Lib.sol +++ b/src/contracts/aludel/AludelV3Lib.sol @@ -211,6 +211,8 @@ library AludelV3Lib { amount ); + cleanupExpiredRewardSchedules(aludel); + // push new reward schedule to the store aludel.rewardSchedules.push( IAludelV3.RewardSchedule({ @@ -241,4 +243,24 @@ library AludelV3Lib { aludel.totalStake += amount; } + function cleanupExpiredRewardSchedules( + IAludelV3.AludelData storage aludel + ) internal { + // check : measure how much gas this actually saves + uint256 length = aludel.rewardSchedules.length; + for (uint index = 0; index < length; index++) { + IAludelV3.RewardSchedule memory schedule = aludel.rewardSchedules[index]; + + // check if the period is already expired + if (block.timestamp - schedule.start >= schedule.duration) { + // length decreces + length--; + // overwrite current index with last schedule. + aludel.rewardSchedules[index] = aludel.rewardSchedules[length]; + // remove last element, already copied to _index_ + aludel.rewardSchedules.pop(); + } + } + } + }