1.
Title: Reduce the size of error messages (Long revert Strings)
Impact:
Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met.
Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.
Recommended Mitigation Steps:
Consider shortening the revert strings to fit in 32 bytes
2.
Title: Custom errors from Solidity 0.8.4 are cheaper than revert strings
Impact:
Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met) while providing the same amount of information
Impact:
If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). Explicitly initializing it with its default value is an anti-pattern and wastes gas.
1. Title: Reduce the size of error messages (Long revert Strings)
Impact: Shortening revert strings to fit in 32 bytes will decrease deployment time gas and will decrease runtime gas when the revert condition is met. Revert strings that are longer than 32 bytes require at least one additional mstore, along with additional overhead for computing memory offset, etc.
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L92-L96
Recommended Mitigation Steps: Consider shortening the revert strings to fit in 32 bytes
2. Title: Custom errors from Solidity 0.8.4 are cheaper than revert strings
Impact: Custom errors from Solidity 0.8.4 are cheaper than revert strings (cheaper deployment cost and runtime cost when the revert condition is met) while providing the same amount of information
Custom errors are defined using the error statement reference: https://blog.soliditylang.org/2021/04/21/custom-errors/
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol
Recommended Mitigation Steps: Replace require statements with custom errors.
3. Title: Consider make
constant
asprivate
to save gasProof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/InfinityToken.sol#L25-L28 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/TimelockConfig.sol#L9-L10
Recommended Mitigation Steps: I suggest changing the visibility from
public
tointernal
orprivate
4. Title: Comparison operators
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/InfinityToken.sol#L62-L63 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/TimelockConfig.sol#L52
Recommended Mitigation Steps: Replace
<=
with<
, and>=
with>
for gas opt5. Title: Gas improvement on calling SafeERC20.function
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L16
Recommended Mitigation Steps: by removing L#16 and directly call
SafeERC20
Example L#74:
6. Title: Using
storage
to declare Struct variable inside functionProof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L246
Recommended Mitigation Steps:
7. Title:
>=
is cheaper than>
Impact: Strict inequalities (
>
) are more expensive than non-strict ones (>=
). This is due to some supplementary checks (ISZERO, 3 gas)Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1156 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1164 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L341 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/InfinityToken.sol#L67
Recommended Mitigation Steps: Consider using
>=
instead of>
to avoid some opcodes8. Title: Using
!=
is more gas efficientProof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L392
Recommended Mitigation Steps: Change from
>
to!=
9. Title: Expression for
constant
values such as a call tokeccak256()
, should useimmutable
rather thanconstant
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/InfinityToken.sol#L25-L28 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/TimelockConfig.sol#L9-L10
Recommended Mitigation Steps: Change from
constant
toimmutable
reference: https://github.com/ethereum/solidity/issues/923210. Title: Default value initialization
Impact: If a variable is not set/initialized, it is assumed to have the default value (0, false, 0x0 etc depending on the data type). Explicitly initializing it with its default value is an anti-pattern and wastes gas.
Proof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L42 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L108 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L214 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L244
Recommended Mitigation Steps: Remove explicit initialization for default values.
11. Title: Using
unchecked
can save gasProof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L301 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L305 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L309
Recommended Mitigation Steps: Because of the condition in L#298, 302, 306
12. Title: Using multiple
require
instead&&
can save gasProof of Concept: https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L949 https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L264
Recommended Mitigation Steps: Change to: