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

`buyLoan()` in `Lender.sol` can cache storage variables to save gas

Summary

buyloan() in Lender.sol accesses storage variables multiple times; can save gas by caching in memory

Vulnerability Details

buyloan() accesses pools[poolId].poolBalance and pools[poolId].interestRate multiple times each. Specifically, pools[poolId].poolBalance is accessed twice, and pools[poolId].interestRate is accessed three times for a total of 2 cold SLOADs (2100 gas each) and 3 warm SLOADs (100 gas each), costing a total of 4500 gas. If these variables are cached instead, the cost will be 2 cold SLOADs (2100 gas each) to access the variables, 2 MSTOREs (3 gas each) to store the variables in memory, and 5 MLOADs (3 gas each) to access the variables, costing a total of 4221 gas. Therefore, 279 gas is saved. Additionally, the gas cost of reading from an array is avoided.

Impact

Greater than 279 gas is saved.

Tools Used

Recommendations

Use memory variables to cache the values ofpools[poolId].poolBalance and pools[poolId].interestRate.

Support

FAQs

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