Open code423n4 opened 2 years ago
The warding has shown a use of packing that would save 5k in the average case (non-zero to non-zero)
pack ts as uint64 - https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/VotingEscrow.sol#L34 pack blk as uint64 - https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/VotingEscrow.sol#L35
The rest of the findings would save less than 1k gas
The report could benefit by:
1. Cache array length outside of loop
Impact
Caching the array length outside a loop saves reading it on each iteration, as long as the array's length is not changed during the loop.
Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to cache the array length outside of the
for
loop.2. Long revert error messages
Impact
Shortening revert error messages to fit in 32 bytes will decrease gas costs for deployment and gas costs when the revert condition has been met.
Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to decrease revert messages to maximum 32 bytes. Alternatively custom error types should be used.
3. Use custom errors instead of revert strings to save gas
Impact
Usage of custom errors reduces the gas cost.
Proof of Concept
Contract that could be using custom errors:
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to add custom errors to listed contracts.
4. ++i/--i costs less gas compared to i++, i += 1, i-- or i -= 1
Impact
++i
or--i
costs less gas compared toi++
,i += 1
,i--
ori -= 1
for unsigned integer as pre-increment/pre-decrement is cheaper (about 5 gas per iteration).Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to use
++i
or--i
instead ofi++
,i += 1
,i--
ori -= 1
to increment value of an unsigned integer variable.5. Obsolete overflow/underflow check
Impact
Starting from solidity
0.8.0
there is built-in check for overflows/underflows. This mechanism automatically checks if the variable overflows or underflows and throws an error. Multiple contracts use increments that cannot overflow but consume additional gas for checks.Proof of Concept
Recommended Mitigation Steps
It is recommended wrap incrementing with
unchecked
block, for example:unchecked { ++i }
orunchecked { --i }
.6. No need to explicitly initialize variables with default values
Impact
If a variable is not set/initialized, it is assumed to have the default value (
0
for uint,false
for bool,address(0)
for addresses). Explicitly initializing it with its default value is an anti-pattern and waste of gas.Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to remove explicit initializations with default values.
7. Use != 0 instead of > 0 for unsigned integer comparison
Impact
When dealing with unsigned integer types, comparisons with
!= 0
are cheaper than with> 0
.Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to use
!= 0
instead of> 0
.8. Pack integer values
Impact
Packing integer variables into storage slots saves gas.
Proof of Concept
VotingEscrow.sol
:ts
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/VotingEscrow.sol#L34blk
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/VotingEscrow.sol#L35Pair.sol
:timestamp
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/Pair.sol#L39Gauge.sol
:timestamp
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/Gauge.sol#L57timestamp
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/Gauge.sol#L64timestamp
asuint64
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/Gauge.sol#L70Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to pack listed values in order to consume less storage and in effect less gas.
9. Use scientific notation
Impact
Multiple contracts are using math exponent calculation to express big numbers. This consumes additional gas and its better to use scienfic notation.
Proof of Concept
Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to use scientific notation, for example:
1e18
.10. Obsolete function logic
Impact
Contract
Velo.sol
in its constructor is executing function_mint
with argument of0
which does not affect storage in any way thus can be removed to save the gas.Proof of Concept
Velo.sol
:_mint
- https://github.com/code-423n4/2022-05-velodrome/blob/7fda97c570b758bbfa7dd6724a336c43d4041740/contracts/contracts/Velo.sol#L22Tools Used
Manual Review / VSCode
Recommended Mitigation Steps
It is recommended to remove obsolete function logic.