Skip to content

Commit

Permalink
clean up expired reward schedules in fund
Browse files Browse the repository at this point in the history
This should reduce the computational cost of `calculateLockedShares`
because if we remove expired schedules, which are fully unlocked at that
stage, we need to iterate less elements during the calculation.
  • Loading branch information
itirabasso committed Jan 17, 2023
1 parent bb00a8f commit bfb9db2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/contracts/aludel/AludelV3Lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ library AludelV3Lib {
amount
);

cleanupExpiredRewardSchedules(aludel);

// push new reward schedule to the store
aludel.rewardSchedules.push(
IAludelV3.RewardSchedule({
Expand Down Expand Up @@ -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();
}
}
}

}

0 comments on commit bfb9db2

Please sign in to comment.