code-423n4 / 2022-05-cally-findings

2 stars 0 forks source link

Gas Optimizations #278

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/CallyNft.sol#L241 storage values data.length should get cached in memory for efficiency gas fee, and there are 2 data.length in line 244.

2

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/CallyNft.sol#L244 i suggest to use "uint256 i" instead of "uint256 i=0" because default value of uint256 is 0. Use ++i instead of i++, pre-increment is cheaper abput 5 gas per iteration

3

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L94-L95

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L282 If a variable is not set/initialized, it is assumed to have the default value (0 for uint). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

4

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L170

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L283

!= 0 costs less gas compared to > 0 for unsigned integers in require statements with the optimizer enabled (6 gas). So, I suggest changing > 0 with != 0

5

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L167-L168 if you know the length of array, you can write the value. read the array length too expensive.

6

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L217

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L220

https://github.com/code-423n4/2022-05-cally/blob/1849f9ee12434038aa80753266ce6a2f2b082c59/contracts/src/Cally.sol#L328

Comparing to a constant (true or false) is a bit more expensive than directly checking the returned boolean value. I suggest using require(!vault.isExercied) instead of require(vault.isExercied == false), and require(!vault.isWithdrawing) instead of require(vault.isWithdrawing == false).