code-423n4 / 2022-04-backed-findings

1 stars 1 forks source link

Gas Optimizations #92

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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 in totalOwed and interestOwed

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;

wilsoncusack commented 2 years ago

https://github.com/wilsoncusack/backed-protocol/pull/62