Open code423n4 opened 2 years ago
Those can be declared as private for gas savings, only creating view functions for variables that should be read externally.
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L17-L44
Declare these variables to private.
If storage is accessed more than once, it should be fetch into memory to save gas. loan.lastAccumulatedTimestamp is accessed twice in totalOwed and interestOwed
loan.lastAccumulatedTimestamp
totalOwed
interestOwed
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L339
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L343
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L352
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L356
Fetch loan.lastAccumulatedTimestamp into memory
uint40 lastAccumulatedTimestamp = loan.lastAccumulatedTimestamp;
https://github.com/wilsoncusack/backed-protocol/pull/62
Use private variables to save gas
Those can be declared as private for gas savings, only creating view functions for variables that should be read externally.
Proof of Concept
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L17-L44
Recommendation
Declare these variables to private.
Fetch loan fields into memory to save gas
If storage is accessed more than once, it should be fetch into memory to save gas.
loan.lastAccumulatedTimestamp
is accessed twice intotalOwed
andinterestOwed
Proof of Concept
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L339
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L343
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L352
https://github.com/code-423n4/2022-04-backed/blob/main/contracts/NFTLoanFacilitator.sol#L356
Recommendation
Fetch loan.lastAccumulatedTimestamp into memory
uint40 lastAccumulatedTimestamp = loan.lastAccumulatedTimestamp;