Skip to content
Request an audit

‹ All findings

Variables should be cached to memory to save gas

Low/InfoMetaZero Staking·Three Sigma · Omnichain RWA tokenisation · 6th April, 2024Codebase3S-MZ-N01

Description

Storage reads are expensive and should be avoided. This is usually done by caching storage variables in memory ones.

Recommendation

Replace all storage rewards that happen more than once per function (or even better, per flow) for memory caching. Example:

solidity
function rewardPerToken() public view returns (uint256) {
    uint256 totalStakedAccruingRewardsCached = totalStakedAccruingRewards;
    if (totalStakedAccruingRewardsCached == 0) {
        return rewardPerTokenStored;
    }
    return rewardPerTokenStored + (

(lastApplicableTime() - lastUpdateTime) * rewardRate * 1e18 / totalStakedAccruingRewardsCached

solidity
);
}

Status

Acknowledged