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

4 stars 0 forks source link

Gas Optimizations #349

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Report for Infinity by PeritoFlores

Tautology in require (uint are always equal or higher than zero)

This is tautology

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/staking/InfinityStaker.sol#L193

 require(totalStaked >= 0, 'nothing staked to rage quit');

I believe that you mean != 0, however this is not affecting the logic so I believe you could just remove it saving gas

Use delete instead of assigning zero to storage variable

The keyword delete refunds you with 15,000 gas. Consider using it for example in this function

function _clearUserStakedAmounts(address user) internal {
  // clear amounts
  userstakedAmounts[user][Duration.NONE].amount = 0;@audit gas "use delete instead of = assign zero in storage"
  userstakedAmounts[user][Duration.THREE_MONTHS].amount = 0;
  userstakedAmounts[user][Duration.SIX_MONTHS].amount = 0;
  userstakedAmounts[user][Duration.TWELVE_MONTHS].amount = 0;

  // clear timestamps
  userstakedAmounts[user][Duration.NONE].timestamp = 0;
  userstakedAmounts[user][Duration.THREE_MONTHS].timestamp = 0;
  userstakedAmounts[user][Duration.SIX_MONTHS].timestamp = 0;
  userstakedAmounts[user][Duration.TWELVE_MONTHS].timestamp = 0;
 }

Use constants when you can to save gas

The following variables could be constants .

 uint256 PRECISION = 10**4;

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1161

 bytes32 ORDER_HASH = 0x7bcfb5a29031e6b8d34ca1a14dd0a1f5cb11b20f755bb2a31ee3c4b143477e4a;

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1170

 bytes32 ORDER_ITEM_HASH = 0xf73f37e9f570369ceaab59cef16249ae1c0ad1afd592d656afac0be6f63b87e0;

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1187

bytes32 TOKEN_INFO_HASH = 0x88f0bd19d14f8b5d22c0605a15d9fffc285ebc8c86fb21139456d305982906f1;

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1203

Use immutable variables when you can to save gas

Infinity token is a predefined address that you would not change

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/staking/InfinityStaker.sol#L25

Consider not using SafeERC20 for INFINITY TOKEN

This is maybe a contradictory idea but when you know the implementation of your token you can save gas if you use transfer/transferFrom instead of safeTransfer/safeTransferFrom.

You can consider removing the library in lines like that.

IERC20(INFINITY_TOKEN).safeTransferFrom(msg.sender, address(this), amount);@audit gas check if neccessary

Maybe you comment explaining why you use transfer. I believe that this would save lot of gas.

Function using nonReentrant modifier without making any external call

The function changeDuration#InfinityStaker.sol is using nonReentrant modifier but only modifies internal variables without making any external call.

Avoid unnecessary initializations

Booleans initialized to false

bool _isPriceValid = false;

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L42

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L108

Uint initialized to zero

In for loops avoid initializing i or j to zero

Also in the following lines the initialization is unnecessary

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L197

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L214

https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L197

getMaxEpochs() called twice in advanceEpoch#InifityToken.sol

In the function advanceEpoch#InifityToken.sol you are calling twice getMaxEpochs() which is an external contract call. Consider catching in a variable instead.

nneverlander commented 2 years ago

Thanks