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

1 stars 1 forks source link

Gas Optimizations #203

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago
  1. Title : Using short reason string can be used for saving more gas

Every reason string takes at least 32 bytes. Use short reason strings that fits in 32 bytes or it will become more expensive.

Tool Used

Manual Review

Occurances

contracts/protocol/modules/v1/NotionalTradeModule.sol#L169        "Send token must be an index component"
contracts/protocol/modules/v1/NotionalTradeModule.sol#L199        "FCash to redeem must be an index component"
contracts/protocol/modules/v1/NotionalTradeModule.sol#L378        "WrappedfCash not deployed for given parameters"
contracts/protocol/modules/v1/NotionalTradeModule.sol#L485       "Not enough received amount"
contracts/protocol/modules/v1/NotionalTradeModule.sol#L573        "Token is neither asset nor underlying token"
  1. Title : Using ++i than i++ for saving more gas

Using i++ instead ++i for all the loops, the variable i is incremented using i++. It is known that implementation by using ++i costs less gas per iteration than i++ .

Tools Used

Manual Review

Occurances

contracts/protocol/modules/v1/NotionalTradeModule.sol#L238        for(uint256 i = 0; i < modules.length; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L254        for(uint256 i = 0; i < modules.length; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L393         for(uint256 i = 0; i < positionsLength; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L605         for(uint256 i = 0; i < positionsLength; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L618         for(uint256 i = 0; i < positionsLength; i++) {

and this can be used prefix instead of used postfix

contracts/protocol/modules/v1/NotionalTradeModule.sol#L610                     numFCashPositions++;
contracts/protocol/modules/v1/NotionalTradeModule.sol#L623                     j++;
  1. TItle : Change uint256 i = 0 into uint256 i for saving more gas

Using this implementation can saving more gas for each loops.

Tool Used

Manual Review

Recommended Mitigation

Change it

Occurances

contracts/protocol/modules/v1/NotionalTradeModule.sol#L238        for(uint256 i = 0; i < modules.length; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L254        for(uint256 i = 0; i < modules.length; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L393         for(uint256 i = 0; i < positionsLength; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L605         for(uint256 i = 0; i < positionsLength; i++) {
contracts/protocol/modules/v1/NotionalTradeModule.sol#L618         for(uint256 i = 0; i < positionsLength; i++) {