20,000 USDC
View results
Submission Details
Severity: gas
Valid

State variables should be cached in stack variables rather than re-reading them from storage

Summary

State variables should be cached in stack variables rather than re-reading them from storage

Vulnerability Details

The instances below point to the second+ access of a state variable within a function.
Caching of a state variable replaces each Gwarmaccess (100 gas) with a much cheaper stack read.
Other less obvious fixes/optimizations include having local memory caches of state variable structs,
or having local caches of state variable contracts/addresses.

Saves 100 gas per instance

File: src/Lender.sol
656: IERC20(loan.loanToken).transfer(feeReceiver, protocolInterest);

Link to code - https://github.com/Cyfrin/2023-07-beedle/tree/main/src/Lender.sol

File: src/Staking.sol
92: supplyIndex[recipient] = index;

Link to code - https://github.com/Cyfrin/2023-07-beedle/tree/main/src/Staking.sol

Tools Used

Code Review

Recommendations

Cache state variables if they need to be accessed more than once

Auditor

NeoCrao

Support

FAQs

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