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

1 stars 0 forks source link

Gas Optimizations #268

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Gas Optimizations

Loop Optimizations

for(uint256 i = 0; i < modules.length; i++) {
            try IDebtIssuanceModule(modules[i]).registerToIssuanceModule(_setToken) {} catch {}
        }

proposed change:

for(uint256 i; i < modules.length;) {
            try IDebtIssuanceModule(modules[i]).registerToIssuanceModule(_setToken) {} catch {}
            unchecked {++i;}
        }

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

for(uint256 i = 0; i < modules.length; i++) {
            if(modules[i].isContract()){
                try IDebtIssuanceModule(modules[i]).unregisterFromIssuanceModule(setToken) {} catch {}
            }
        }

proposed change:

for(uint256 i; i < modules.length;) {
            if(modules[i].isContract()){
                try IDebtIssuanceModule(modules[i]).unregisterFromIssuanceModule(setToken) {} catch {}
            }
            unchecked {++i;}
        }

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


for(uint256 i = 0; i < positionsLength; i++) {
            if(positions[i].unit > 0) {
                address component = positions[i].component;
                if(_isWrappedFCash(component)) {
                    fCashPositions[j] = component;
                    j++;
                }
            }
        }

proposed change:

for(uint256 i; i < positionsLength;) {
            if(positions[i].unit > 0) {
                address component = positions[i].component;
                if(_isWrappedFCash(component)) {
                    fCashPositions[j] = component;
                    j++;
                }
            }
            unchecked {++i;}
        }

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


for(uint256 i = 0; i < positionsLength; i++) {
            // Check that the given position is an equity position
            if(positions[i].unit > 0) {
                address component = positions[i].component;
                if(_isWrappedFCash(component)) {
                    numFCashPositions++;
                }
            }
        }

proposed change:

for(uint256 i; i < positionsLength;) {
            // Check that the given position is an equity position
            if(positions[i].unit > 0) {
                address component = positions[i].component;
                if(_isWrappedFCash(component)) {
                    numFCashPositions++;
                }
            }
            unchecked {++i;}

        }

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

Default Initialized variable

uint32 minImpliedRate = 0;

proposed change:

uint32 minImpliedRate;

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

Cache Array length in memory

address[] memory modules = setToken.getModules();
        for(uint256 i = 0; i < modules.length; i++)

proposed change:

address[] memory modules = setToken.getModules();
uint modulesLength = modules.length;
        for(uint256 i = 0; i < modulesLength; i++)

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


address[] memory modules = _setToken.getModules();
        for(uint256 i = 0; i < modules.length; i++) {

proposed change:

address[] memory modules = _setToken.getModules();
uint modulesLength = modules.length;
        for(uint256 i = 0; i < modulesLength; i++) {

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

liu-zhipeng commented 2 years ago

It seems like the wrong repo. but pretty helpful.

liu-zhipeng commented 2 years ago

Labeled as invalid - this seems to be referring to a different project!

0xleastwood commented 1 year ago

Looks like that's the case. RIP gas report.