code-423n4 / 2022-10-blur-findings

1 stars 0 forks source link

Gas Optimizations #675

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Gas Optimizations Report for Blur Exchange contest

Overview

During the audit, 7 gas issues were found.

Title Instance Count
G-1 Postfix increment 5
G-2 <>.length in loops 4
G-3 Initializing variables with default value 7
G-4 Some variables can be immutable 1
G-5 x += y is more expensive than x = x + y 2
G-6 Using unchecked blocks saves gas 5
G-7 Custom errors may be used 20

Gas Optimizations Findings (7)

G-1. Postfix increment

Description

Prefix increment costs less gas than postfix.

Instances
Recommendation

Consider using prefix increment where it is relevant.

#

G-2. <>.length in loops

Description

Reading the length of an array at each iteration of the loop consumes extra gas.

Instances
Recommendation

Store the length of an array in a variable before the loop, and use it.

#

G-3. Initializing variables with default value

Description

It costs gas to initialize integer variables with 0 or bool variables with false but it is not necessary.

Instances
Recommendation

Remove initialization for default values.
For example: for (uint256 i; i < array.length; ++i) {

#

G-4. Some variables can be immutable

Description

Using immutables is cheaper than storage-writing operations.

Instances
Recommendation

Use immutables where possible.
Change to address public immutable weth;

#

G-5. x += y is more expensive than x = x + y

Instances
Recommendation

Use x = x + y instead of x += y. Use x = x - y instead of x -= y.

#

G-6. Using unchecked blocks saves gas

Description

In Solidity 0.8+, there’s a default overflow and underflow check on unsigned integers. When an overflow or underflow isn’t possible, some gas can be saved by using unchecked blocks.

Instances
Recommendation

Change:

for (uint256 i; i < n; ++i) {
 // ...
}

to:

for (uint256 i; i < n;) { 
 // ...
 unchecked { ++i; }
}

#

G-7. Custom errors may be used

Description

Custom errors from Solidity 0.8.4 are cheaper than revert strings.

Instances
Recommendation

For example, change:

require(msg.sender == owner, "INVALID_ADDRESS");

to:

error InvalidAddress();
...
if (msg.sender != owner) revert InvalidAddress();
GalloDaSballo commented 2 years ago

weth -> 2.1k 150 rest

2250