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

[G‑08] Nested IFs are cheaper than &&

All optimizations were benchmarked using the protocol's tests using the following config: solc version 0.8.18, optimizer on, 200 runs, viaIR = true. In most cases forge test --gas-report was used

Each optimization was submitted individually.

Gas Optimizations

Issue Instances Total Gas Saved
[G‑01] Consider using clones * -70% cheaper deployment gas
[G‑02] ReentrancyGuard can be safely removed 1 42725
[G‑03] computeEscrowAddress() can be internal instead of public 1 55863 + 193
[G‑04] Redundant zero address checks 2 237
[G‑05] Input validation should be done in the beginning 2 110649(in the revert case)
[G‑06] Emit after the transfer has been made in case it fails 2 1381(in the revert case)
[G‑07] The bytecode can be removed from the function params 1 27
[G‑08] Nested ifs are cheaper than && 1 19

[G‑08] Nested IFs are cheaper than &&

Nested IFs are cheaper than a single && statement because the short-circuiting rules are removed.

Gas Savings for initiateDispute() obtained via protocol's tests: avg 19 gas

AVG MED MAX
Before 16855 23603 23603
After 16836 23584 23584

There is 1 instance of this issue:

https://github.com/Cyfrin/2023-07-escrow/blob/65a60eb0773803fa0be4ba72defaec7d8567bccc/src/Escrow.sol#L67

File: Escrow.sol
66: modifier onlyBuyerOrSeller() {
67: if (msg.sender != i_buyer && msg.sender != i_seller) {
68: revert Escrow__OnlyBuyerOrSeller();
69: }
70: _;
71: }

Use nested ifs instead of &&

Support

FAQs

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