Open code423n4 opened 2 years ago
1./2. Fixed in previous update
Re prefix increments: -- We internally decided to avoid prefix increments for now.
Re unnecessarily payable:
--
If the token to swap is native, the message value is used in _executeSwaps
.
Fixed in previous commit.
Duplicate of #197
Duplicate of #44
Duplicate of #197
Duplicate of #100
Duplicate of #39
Duplicate of #122
Duplicate of #196
We internally decided to avoid previx increments for now.
The following functions could be set external to save gas and improve code quality. External call cost is less expensive than of public functions.
Title: Unnecessary equals boolean Severity: GAS
Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.
Title: Consider inline the following functions to save gas Severity: GAS
Title: Unnecessary functions Severity: GAS
Title: Unnecessary array boundaries check when loading an array element twice Severity: GAS
Title: Prefix increments are cheaper than postfix increments Severity: GAS
Prefix increments are cheaper than postfix increments. Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change There is no risk of overflow caused by increamenting the iteration index in for loops (the
++i
infor (uint256 i = 0; i < numIterations; ++i)
). But increments perform overflow checks that are not necessary in this case. These functions use not using prefix increments (++x
) or not using the unchecked keyword:Title: Inline one time use functions Severity: GAS
The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.
Title: Unnecessary cast Severity: Gas
Title: Unused state variables Severity: GAS
Unused state variables are gas consuming at deployment (since they are located in storage) and are a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.
Title: Caching array length can save gas Severity: GAS
Caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient than query storage / calldata / memory. We recommend to change from:
to:
Title: Gas Optimization On The 2^256-1 Severity: GAS
Some projects (e.g. Uniswap - https://github.com/Uniswap/interface/blob/main/src/hooks/useApproveCallback.ts#L88) set the default value of the user's allowance to 2^256 - 1. Since the value 2^256 - 1 can also be represented in hex as 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff. From Ethereum's yellow paper we know that zeros are cheaper than non-zero values in the hex representation. Considering this fact, an alternative choice could be now 0x8000000000000000000000000000000000000000000000000000000000000000 or 2^255 to represent "infinity". If you do the calculations with Remix, you will see that the former costs 47'872 gas, while the latter costs 45'888 gas. If you accept that infinity can also be represented via 2^255 (instead of 2^256-1), which almost all projects can - you can already save about 4% gas leveraging this optimisation trick on those calculations.
Title: Internal functions to private Severity: GAS
The following functions could be set private to save gas and improve code quality:
Title: Unnecessary payable Severity: GAS
The following functions are payable but msg.value isn't used - therefore the function payable state modifier isn't necessary. Payable functions are more gas expensive than others, and it's danger the users if they send ETH by mistake.
Title: uint8 index Severity: GAS
Due to how the EVM natively works on 256 numbers, using a 8 bit number here introduces additional costs as the EVM has to properly enforce the limits of this smaller type. See the warning at this link: https://docs.soliditylang.org/en/v0.8.0/internals/layout_in_storage.html#layout-of-state-variables-in-storage We recommend to use uint256 for the index in every for loop instead using uint8: