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

0 stars 0 forks source link

Gas Optimizations #54

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

1 Change public to private

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L35-L47

change from public to private can reduce the gas fee.

2 Default value and increment

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L118

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L153

    for(uint i = 0; i < length; i++){

default uint is 0 so remove unnecassary explicit can reduce gas pre increment ++i more cheaper gas than post increment i++. i suggest to use pre increment.

3 Use storage instead memory

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L150

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L143

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L257

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L232-L233

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L299

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L162

use storage instead memory for cheaper gas fee. i suggest to change

    IAuraLocker.EarnedData[] memory earnedData = LOCKER.claimableRewards(address(this));

to

    IAuraLocker.EarnedData[] storage earnedData = LOCKER.claimableRewards(address(this));

apply to others.

4 Use calldata instead memory

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L149

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L186

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L346

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

function balanceOfRewards() external view override returns (TokenAmount[] memory rewards) {

to

function balanceOfRewards() external view override returns (TokenAmount[] storage rewards) {

apply to others.

5 Reduce the string

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L186

reduce size of string error message can reduce the gas fee. reduce it if possible.

6 Caching claims.length because use multiple times

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#LL299-L300

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L317 caching _claims.length to memory because use multiple times can reduce the gas.

    uint256[] memory beforeBalance = new uint256[](_claims.length);
    for (uint256 i = 0; i < _claims.length; i++) {

7 Use !=0 instead of >0

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L330

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L230

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

8 Use inequality >= then >

https://github.com/Badger-Finance/vested-aura/blob/d504684e4f9b56660a9e6c6dfb839dcebac3c174/contracts/MyStrategy.sol#L197

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

GalloDaSballo commented 2 years ago

https://github.com/code-423n4/2022-06-badger-findings/issues/1 Change public to private Disagree it changes the functionality of the contract

https://github.com/code-423n4/2022-06-badger-findings/issues/2 Default value and increment

3+ 5 gas

https://github.com/code-423n4/2022-06-badger-findings/issues/3 Use storage instead memory

In lack of POC I must disagree

https://github.com/code-423n4/2022-06-badger-findings/issues/4 Use calldata instead memory

Same, in lack of POC I must disagree, also how do you edit calldata?

Rest I ack