Skip to content

Commit

Permalink
Merge pull request #13 from euler-xyz/restore-encapsulation
Browse files Browse the repository at this point in the history
Restore encapsulation of distributions layout, broken by gas optimisa…
  • Loading branch information
kasperpawlowski authored Apr 30, 2024
2 parents 334083d + 67b6001 commit efcee13
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/BaseRewardStreams.sol
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard
address reward,
uint48 epoch
) public view virtual override returns (uint256) {
DistributionStorage storage distributionStorage = distributions[rewarded][reward];
return distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT];
return rewardAmount(distributions[rewarded][reward], epoch);
}

/// @notice Returns the current epoch based on the block timestamp.
Expand Down Expand Up @@ -444,6 +443,17 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard
}
}

/// @notice Returns the reward token amount for an epoch, given a pre-computed distribution storage pointer.
/// @param distributionStorage Pre-computed distribution storage pointer.
/// @param epoch The epoch to get the reward token amount for.
/// @return The reward token amount.
function rewardAmount(
DistributionStorage storage distributionStorage,
uint48 epoch
) internal view returns (uint256) {
return distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT];
}

/// @notice Increases the reward token amounts for a specific rewarded token.
/// @param rewarded The address of the rewarded token.
/// @param reward The address of the reward token.
Expand Down Expand Up @@ -574,7 +584,7 @@ abstract contract BaseRewardStreams is IRewardStreams, EVCUtil, ReentrancyGuard
for (uint48 epoch = epochStart; epoch < epochEnd; ++epoch) {
// Overflow safe because `totalRegistered * SCALER <= type(uint144).max < type(uint256).max`.
unchecked {
uint256 amount = distributionStorage.amounts[epoch / EPOCHS_PER_SLOT][epoch % EPOCHS_PER_SLOT];
uint256 amount = rewardAmount(distributionStorage, epoch);
delta += SCALER * timeElapsedInEpoch(epoch, lastUpdated) * amount / EPOCH_DURATION;
}
}
Expand Down

0 comments on commit efcee13

Please sign in to comment.