Assessment reports>H20 vlPSDN>Discussion>Gas optimization for `_removeReward`

Gas optimization for _removeReward

In LockRewards, the function _removeReward is used to remove a reward token from the rewardTokens list. However, the current implementation does so in an inefficient way: it copies all later elements one position to the left before popping the last element.

It can be rewritten to be more efficient, as shown below:

function _removeReward(uint256 index) internal {
    address addr = rewardTokens[index];
    rewardTokens[index] = rewardTokens[rewardTokens.length - 1];
    rewardTokens.pop();
    emit RemovedRewardToken(addr);

This optimization would provide significant gas savings. Additionally, this becomes a mechanism for cheaply removing tokens and would mitigate any possibility of max gas problems if rewardTokens became large.

Remediation

The issue has been fixed by H20 in commit 2c9ebd02.

Zellic © 2023Back to top ↑