code-423n4 / 2022-06-notional-coop-findings

1 stars 1 forks source link

Gas Optimizations #172

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1 Default value address

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L48

the default value of address is address (0), so just type this to reduce the gas. Use

         address internal constant ETH_ADDRESS;

instead of

         address internal constant ETH_ADDRESS = address(0);

2 Reduce string

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L199

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L378

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L573

reduce error message and change from string to bytes32 to reduce the gas fee if it possible.

3 Change memory to storage

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashLogic.sol#L128

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L237

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L253

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L388

Use storage instead memory to reduce the gas fee. i suggest to change

    PortfolioAsset[] memory assets = NotionalV2.getAccountPortfolio(address(this));

to

    PortfolioAsset[] storage assets = NotionalV2.getAccountPortfolio(address(this));

Apply it to others code.

4 Looping

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L238

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L254

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L393

use uint i instead uint i = 0 because default valueof uint is 0. caching modules.length because access to a local variable is more cheap than query storage / calldata / memory. use pre-increment (++i) are cheaper than post-increment (i++).

5 Use calldata instead of memory

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L358

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashLogic.sol#L205

In the external functions where the function argument is read-only, the function() has an inputed parameter that using memory, if this function didnt change the parameter, its cheaper to use calldata then memory. so i suggest to change it.

6 Increment

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L610

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L623

use pre incremeny is more cheaper than post increment. i suggest to change

                numFCashPositions++;

to ++numFCashPositions;

7 Use !=0 instead of >0

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashBase.sol#L37

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashERC4626.sol#L23

for unsigned integer, >0 is less efficient then !=0, so use !=0 instead of >0. aplly to all.

8 Use multiple require

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashLogic.sol#L116

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/notional-wrapped-fcash/contracts/wfCashLogic.sol#L129

use multiple require instead of && for cheap gas and the size on deployment can be a bit smaller. i suggest to change

    require(
        msg.sender == address(NotionalV2) &&
        // Only accept the fcash id that corresponds to the listed currency and maturity
        _id == fCashID &&
        // Protect against signed value underflows
        int256(_value) > 0,
        "Invalid"
    );

to

    require(msg.sender == address(NotionalV2), "invalid");
        // Only accept the fcash id that corresponds to the listed currency and maturity
    require( _id == fCashID, "invalid");
        // Protect against signed value underflows
    require(int256(_value) > 0, "Invalid");

9 Caching _setToken

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L231

caching _setToken.initializeModule(); to the memory because it use multiple times and can reduce the gas.

10 default value uint

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L519

default value uint32 is 0, so remove unnacessary explicit initializations for default values.

11 inequality

https://github.com/code-423n4/2022-06-notional-coop/blob/6f8c325f604e2576e2fe257b6b57892ca181509a/index-coop-notional-trade-module/contracts/protocol/modules/v1/NotionalTradeModule.sol#L501

non strict inequality are cheaper than strict one. i suggest to use >= or <= instead of > and < if possible.