code-423n4 / 2022-04-phuture-findings

0 stars 0 forks source link

Gas Optimizations #75

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Using multiple require() instead of && can save gas

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ChainlinkPriceOracle.sol#L86

Recommended Mitigation Steps: Change to:

    require(basePrice > 0, "ChainlinkPriceOracle: NEGATIVE");
    require(quotePrice > 0, "ChainlinkPriceOracle: NEGATIVE");

========================================================================

  1. Caching .length() for loop can save gas

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L39 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L60 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L38 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L50

Recommended Mitigation Steps: Change to:

    uint length = assets.length();
    for (uint i; i < length ; ++i) {

========================================================================

  1. Using != instead of > is more gas efficient

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L76 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L86 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/IndexLogic.sol#L98

Recommended Mitigation Steps: Change to:

    require(lastAssetBalanceInBase != 0, "Index: INSUFFICIENT_AMOUNT");

========================================================================

  1. Using unchecked and prefix increment can save gas

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndex.sol#L30

Recommended Mitigation Steps: Change to:

for (uint i; i < _assets.length;) {
            address asset = _assets[i];
            uint8 weight = _weights[i];

            weightOf[asset] = weight;
            assets.add(asset);

            emit UpdateAnatomy(asset, weight);
    unchecked{
        ++i; //@audit-info: Place here with unchecked       
        }
    }

========================================================================

  1. Using += to increase value on var

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L71

Recommended Mitigation Steps: Change to:

    _totalWeight += newWeight - prevWeight;

========================================================================

  1. Using if statement instead else if

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/TrackedIndexReweightingLogic.sol#L74-L78 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L79-L83 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/ManagedIndexReweightingLogic.sol#L96

Recommended Mitigation Steps: Using if statement can save gas Change to:

        if (newShares > oldShares) {
                    orderer.addOrderDetails(orderId, asset, newShares - oldShares, IOrderer.OrderSide.Buy);
                } 
        if (oldShares > newShares) { //@audit-info: Replacing else if with if statement here
                    orderer.addOrderDetails(orderId, asset, oldShares - newShares, IOrderer.OrderSide.Sell);
                }

========================================================================

  1. Use immutable variables can save gas

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhuturePriceOracle.sol#L33 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhuturePriceOracle.sol#L24 https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/PhuturePriceOracle.sol#L27

Recommended Mitigation Steps: use immutable

========================================================================

  1. Gas improvement on calling SafeERC20.function

Proof of Concept: https://github.com/code-423n4/2022-04-phuture/blob/main/contracts/vToken.sol#L219

Recommended Mitigation Steps: by removing L#23 and directly call SafeERC20.function

Change to:

    SafeERC20.safeTransfer(asset, _recipient, Math.min(_amount, balance))

========================================================================