uint variables will never be lower than 0. Therefore, > 0 and != 0 have same meanings. Using != 0 can reduce the gas deployment cost, so it is worth using != 0 wherever possible.
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
There are more than 50 callsites of require check with inline error message. Using the custom errors instead of the inline error messages can reduce the gas cost.
[Gas-4] Potential usage of unchecked
Following variables or operations can be wrapped by unchecked to reduce the gas cost.
[1] The condition startPrice > endPrice assures that priceDiff will not underflow.
[2] The condition require(userstakedAmounts[msg.sender][oldDuration].amount >= amount, ...) assures that userstakedAmounts[msg.sender][oldDuration].amount -= amount will not underflow
[3] The conditions require(block.timestamp >= previousEpochTimestamp + getEpochDuration(), ...) and require(currentEpoch < getMaxEpochs(), ...) assure that block.timestamp - previousEpochTimestamp and getMaxEpochs() - currentEpoch will not underflow
[4] The conditions if (amount > noVesting), if (amount > vestedThreeMonths) and if (amount > vestedSixMonths) assure that amount = amount - noVesting, amount = amount - vestedThreeMonths and amount = amount - vestedSixMonths will not underflow respectively
[Gas-5] Usage of delete can reduce both deployments and methods gas cost
_clearUserStakedAmounts function
_clearUserStakedAmounts function sets 0 on amount and timestamp at each duration. Using delete keyword can reduce gas costs.
_updateUserStakedAmounts function
_updateUserStakedAmounts function sets 0 on amount and timestamp at each duration. Using delete keyword can reduce gas costs.
[Gas-1] Use
!= 0
instead of> 0
on uint variablesuint variables will never be lower than 0. Therefore,
> 0
and!= 0
have same meanings. Using!= 0
can reduce the gas deployment cost, so it is worth using!= 0
wherever possible.This part can be written as follows:
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L392
[Gas-2] No need to set 0 on uint variables
The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L148
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L200
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L219
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L272
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L308
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L349
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L393
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1048
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1086
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1109
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1190
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1206
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L76
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L82
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L197
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L199
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#L216
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L244
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L246
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L247
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L289
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L290
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L291
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L318
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityOrderBookComplication.sol#L320
[Gas-3] Use custom errors
Using custom errors can reduce the gas cost.
There are more than 50 callsites of require check with inline error message. Using the custom errors instead of the inline error messages can reduce the gas cost.
[Gas-4] Potential usage of unchecked
Following variables or operations can be wrapped by unchecked to reduce the gas cost.
[1] The condition
startPrice > endPrice
assures that priceDiff will not underflow.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/InfinityOrderBookComplication.sol#L333
[2] The condition
require(userstakedAmounts[msg.sender][oldDuration].amount >= amount, ...)
assures thatuserstakedAmounts[msg.sender][oldDuration].amount -= amount
will not underflowhttps://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L99
unchecked { userstakedAmounts[msg.sender][oldDuration].amount -= amount; }
[3] The conditions
require(block.timestamp >= previousEpochTimestamp + getEpochDuration(), ...)
andrequire(currentEpoch < getMaxEpochs(), ...)
assure thatblock.timestamp - previousEpochTimestamp
andgetMaxEpochs() - currentEpoch
will not underflowhttps://github.com/code-423n4/2022-06-infinity/blob/main/contracts/token/InfinityToken.sol#L65-L66
[4] The conditions
if (amount > noVesting)
,if (amount > vestedThreeMonths)
andif (amount > vestedSixMonths)
assure thatamount = amount - noVesting
,amount = amount - vestedThreeMonths
andamount = amount - vestedSixMonths
will not underflow respectivelyhttps://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
[5] Using constant
PROTOCOL_FEE_BPS
will not underflowamount - protocolFee
https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/core/InfinityExchange.sol#L1135-L1136
[Gas-5] Usage of delete can reduce both deployments and methods gas cost
_clearUserStakedAmounts
function_clearUserStakedAmounts
function sets 0 on amount and timestamp at each duration. Usingdelete
keyword can reduce gas costs.https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L328-L340
_updateUserStakedAmounts
function_updateUserStakedAmounts
function sets 0 on amount and timestamp at each duration. Usingdelete
keyword can reduce gas costs.https://github.com/code-423n4/2022-06-infinity/blob/main/contracts/staking/InfinityStaker.sol#L290-L325