Following the recent changes, the given client-tests have successfully passed, resulting in gas savings of around 0.155%.
The gas optimization was made on the Lender.sol contract.
📘 Notice ℹ️: I've provided the optimized versions of the
Lender.solfunctions at the very end of this report.
In the methods refinance() and giveLoan(), there are several storage write operations which can be optimized. Specifically, lines 686-696 in the refinance() method are a prime example:
It's possible to leverage the loan variable declared in memory within this function, update the changes there, and eventually copy this memory variable to storage. This approach results in fewer storage write operations leading to less gas usage.
Here's an optimized approach for the refinance() method:
The giveLoan() function shares a similar context, and its changes can be reviewed in the optimized code appended at the end of the report.
📘 Notice ℹ️: I experimented applying this change to Pool memory objects too. But this didn't translate to gas savings but instead an increased gas consumption. This might be because writing just a couple of pool traits directly to storage may be more gas efficient than apply them with memory and subsequently save them to storage. This observation is intended to guide the development team in case they are thinking let's apply the same with the Pool object.
All impact metrics are from the gas consumption by the clients' provided tests.
| Optimization Method | Original Gas | Optimized Gas | Gas Saved | % Saved |
|---|---|---|---|---|
| Single Storage Write | 3,138,664 | 3,444,012 | 5,348 | 0.155% |
🚧 Note ⚠️: Only the test that have been affected by the change have been analyzed. The other tests had no variation in its gas consumption.
🚧 Note ⚠️: Negative numbers in the
Gas Savedcolumn means the "optimization" increased the consumption of gas in that test. But the overall outcome in this case is positive so it can be considered an optimization.
| Test Name | Optimized Gas | Original Gas | Gas Saved |
|---|---|---|---|
| LenderTest:testFuzz_buyLoan | 822,040 | 822,978 | 938 |
| LenderTest:testFuzz_refinance | 808,485 | 808,483 | -2 |
| LenderTest:test_buyLoan | 857,664 | 857,706 | 42 |
| LenderTest:testFuzz_createPool | 100,509 | 104,646 | 4137 |
| LenderTest:test_refinance | 849,966 | 850,199 | 233 |
| TOTAL | 3,438,664 | 3,444,012 | 5,348 |
Total saved percentage => 0.155%.
📘 Notice ℹ️: The percentage has been calculated with these numbers from the TOTAL:
( 5,348 / 3,444,012 ) * 100
They mean:
(totalGasSaved / originalGasCost) * 100
Original
Optimized
Manual audit
Forge Snapshot
Bash scripts tailored to analyze forge snapshots.
📘 Notice ℹ️: I've personally created the bash scripts. Here is a link to the github repo Forge-Snapshots-Analyzer.
This gas omptimization is easy to implement and it will save some gas, therefore reduce the overall cost of the project. Thats why it's implementation is advised.
Notice ⚠️: If a significant vulnerability necessitating major code changes arises, this gas optimization technique remains relevant and applicable. Nonetheless some gas analysis as the one used here is always needed to make sure it's actually saving gas.
Lender.sol functions
Changes are marked by the 🟢 emoji.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.