15,000 USDC
View results
Submission Details
Severity: gas

storing a (state)variable ,that is used once, in memory is not gas efficient

Summary

storing a storage (state) variable ,which is used once, in memory is not gas efficient

Vulnerability Details

for (uint256 i = 0; i < s_collateralTokens.length; i++) {
address token = s_collateralTokens[i];
355: -> uint256 amount = s_collateralDeposited[user][token];
totalCollateralValueInUsd += getUsdValue(token, amount);
}

storing a storage variable in the memory cost one SLOAD and MSTORE and when use the memory variable it will cost MLOAD .
if the state variable s_collateralDeposited[user][token] used once it will cost only SLOAD . which will save gas

Impact

gas saved = 6 gas per iteration .

Tools Used

Recommendations

do not store s_collateralDeposited[user][token] in memory and perform the loop like this

for (uint256 i = 0; i < s_collateralTokens.length; i++) {
address token = s_collateralTokens[i];
355: ->
totalCollateralValueInUsd += getUsdValue(token,s_collateralDeposited[user][token] );
}

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.