code-423n4 / 2022-05-backd-findings

0 stars 0 forks source link

Gas Optimizations #127

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1 Memory to storage

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/RewardHandler.sol#L39

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/AddressProvider.sol#L54

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/RewardHandler.sol#L41

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/zaps/PoolMigrationZap.sol#L21

use storage instead of memory can reduce the gas. i suggest to change

    address[] memory pools = addressProvider.allPools();

to

    address[] storage pools = addressProvider.allPools();

apply to others

2 Use memory instead calldata

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/tokenomics/FeeBurner.sol#L43

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/Controller.sol#L124

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/AddressProvider.sol#L59

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 we suggest to change it.

function burnToTarget(address[] memory tokens_, address targetLpToken_)

to

function burnToTarget(address[] calldata tokens_, address targetLpToken_)

apply to others.

3 use != 0 instead of >0

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/BkdLocker.sol#L91

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/BkdLocker.sol#L137

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

4 Caching lpgauge

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/StakerVault.sol#L161-L162

        ILpGauge(lpGauge).userCheckpoint(src);
        ILpGauge(lpGauge).userCheckpoint(dst);

to the memory for reduce the gas fee because it use multiple times.

5 Pre increment

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/tokenomics/KeeperGauge.sol#L59

using pre increment more cheaper than post increment. so, i sugget to change

    epoch++;

to

    ++epoch;

6 change string to bytes32

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/tokenomics/Minter.sol#L152

reduce size of error message can reduce the gas fee. i suggest to convert string to bytes32

7 Caching the length

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/zaps/PoolMigrationZap.sol#L22

https://github.com/code-423n4/2022-05-backd/blob/2a5664d35cde5b036074edef3c1369b984d10010/protocol/contracts/zaps/PoolMigrationZap.sol#L39

caching the array length can reduce gas it caused access to a local variable is more cheap than query storage / calldata / memory in solidity.

GalloDaSballo commented 2 years ago

Memory to storage

Would love to see a detailed POC as common sense dictates that storage is more expensive

Use memory instead calldata

Same as above

use != 0 instead of >0

Only for require, <=0.8.13, using optimizer, saves 3 gas

3 * 2 = 6

Caching lpgauge

Disagree, it's already cached, the casting has no gas cost and it's a higher language construct

Pre increment

Saves 5 gas

change string to bytes32

Saves 6 gas per discussion in #17

 Caching the length

Saves 3 gas

3 * 2 = 6

Total Gas Saved 23