code-423n4 / 2022-06-infinity-findings

4 stars 0 forks source link

Gas Optimizations #298

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Report

[G-01] USE immutable

In InfinityStaker.sol:L25, marking INFINITY_TOKEN as immutable (as it never change outside the constructor) would avoid it taking space in the storage:

- address public INFINITY_TOKEN;
+ address public immutable INFINITY_TOKEN;

[G-02] Unnecesary checks on if

Since the conditions are order from lower to high you dont have to double check the lower bounds.

diff --git a/contracts/staking/InfinityStaker.sol b/contracts/staking/InfinityStaker.sol
index 9b36cb5..1533ddb 100644
--- a/contracts/staking/InfinityStaker.sol
+++ b/contracts/staking/InfinityStaker.sol
@@ -212,11 +212,11 @@ contract InfinityStaker is IStaker, Ownable, Pausable, ReentrancyGuard {

     if (totalPower <= BRONZE_STAKE_THRESHOLD) {
       return StakeLevel.NONE;
-    } else if (totalPower > BRONZE_STAKE_THRESHOLD && totalPower <= SILVER_STAKE_THRESHOLD) {
+    } else if (totalPower <= SILVER_STAKE_THRESHOLD) {
       return StakeLevel.BRONZE;
-    } else if (totalPower > SILVER_STAKE_THRESHOLD && totalPower <= GOLD_STAKE_THRESHOLD) {
+    } else if (totalPower <= GOLD_STAKE_THRESHOLD) {
       return StakeLevel.SILVER;
-    } else if (totalPower > GOLD_STAKE_THRESHOLD && totalPower <= PLATINUM_STAKE_THRESHOLD) {
+    } else if (totalPower <= PLATINUM_STAKE_THRESHOLD) {
       return StakeLevel.GOLD;
     } else {
       return StakeLevel.PLATINUM;

[G-03] Avoid caching array length for calldata variables

You could save 5 gas if you avoid caching array length of calldata variables on:

InfinityExchange.sol

InfinityExchange.sol#L137 InfinityExchange.sol#L191 InfinityExchange.sol#L262 InfinityExchange.sol#L301 InfinityExchange.sol#L341 InfinityExchange.sol#L391 InfinityExchange.sol#L1047 InfinityExchange.sol#L1085 InfinityExchange.sol#L1106 InfinityExchange.sol#L1188 InfinityExchange.sol#L1204

InfinityOrderBookComplication.sol

InfinityOrderBookComplication.sol#L75 InfinityOrderBookComplication.sol#L81 InfinityOrderBookComplication.sol#L198 InfinityOrderBookComplication.sol#L215 InfinityOrderBookComplication.sol#L237-L238 InfinityOrderBookComplication.sol#L283-L284 InfinityOrderBookComplication.sol#L319

[G-04] Unnecesary require

on InfinityStaker.sol#L69 there is no need to check if contract has enough balance, because safeTransferFrom will fail if contract doesnt have enough balance.