diff --git a/src/contracts/aludel/AludelV3Lib.sol b/src/contracts/aludel/AludelV3Lib.sol index 3402252..fb6a723 100644 --- a/src/contracts/aludel/AludelV3Lib.sol +++ b/src/contracts/aludel/AludelV3Lib.sol @@ -215,6 +215,8 @@ library AludelV3Lib { amount ); + cleanupExpiredRewardSchedules(aludel); + // push new reward schedule to the store aludel.rewardSchedules.push( IAludelV3.RewardSchedule({ @@ -244,4 +246,25 @@ library AludelV3Lib { vault.totalStake += amount; 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(); + } + } + } + }