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

0 stars 0 forks source link

Gas Optimizations #168

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS OPT

  1. Title : Value can be caching in calldata instead of memory

1.) File : contracts/tokenomics/FeeBurner.sol (Line.43)

    function burnToTarget(address[] memory tokens_, address targetLpToken_)
  1. Title : better way for epoch++ for saving more gas

using prefix is common that more saved gas than using postfix.

Tool Used

Remix

Recommended Mitigation

        ++epoch;

Occurances

KeeperGauge.sol#L59 KeeperGauge.sol#L98

  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

e.g. (xxxxxx, IAS) //Inflation has Already Started

contracts/tokenomics/Minter.sol#L105                "Inflation has already started."
contracts/tokenomics/Minter.sol#L152                "Maximum non-inflation amount exceeded."
contracts/tokenomics/VestedEscrow.sol#L82           "Supply already initialized once"
contracts/tokenomics/VestedEscrow.sol#L84           "No reward tokens in contract"
contracts/tokenomics/VestedEscrow.sol#L91           "Supply must be initialized"
contracts/tokenomics/VestedEscrowRevocable.sol#L53  "Recipient already revoked"
contracts/tokenomics/VestedEscrowRevocable.sol#L54  "Treasury cannot be revoked!"
contracts/tokenomics/InflationManager.sol#L95         "Weight-based dist. deactivated."
  1. Title : Saving gas by removing = 0

This implementation code can be saving more gas by removing = 0, it because If a variable was not set/initialized, it is assumed to have default value to 0

Tool Used

Manual Review

Mitigation Step

Remove = 0

Occurances

contracts/utils/Preparable.sol#L87          deadlines[key] = 0;
contracts/utils/Preparable.sol#L88          pendingUInts256[key] = 0;
contracts/utils/Preparable.sol#L99          deadlines[key] = 0;
contracts/utils/Preparable.sol#L143         deadlines[key] = 0;
contracts/utils/Preparable.sol#L150         pendingUInts256[key] = 0;
contracts/utils/Preparable.sol#L151         deadlines[key] = 0;
contracts/tokenomics/LpGauge.sol#L60        perUserShare[beneficiary] = 0;
  1. Title : Shorter code for save gas.

This can be shorter code for gas opt

File : contracts/StakerVault.sol (Line.169)

        balances[dst] = balances[dst] + amount;     

changed to

        balances[dst] +=  amount;
chase-manning commented 2 years ago

Looks like most of these are not valid. Seems like is just some script that is run to auto gen gas findings.

For example contracts/tokenomics/Minter.sol#L105 is this require(lastEvent == 0, "Inflation has already started.");. They are talking about initialising a variable. But we're not doing that here, we're doing a comparison...

GalloDaSballo commented 2 years ago

Value can be caching in calldata instead of memory

In lack of math I can only rate it as 0 as when done as an external call it won't save gas

Title : better way for epoch++ for saving more gas

5 gas

Title : Using short reason string can be used for saving more gas

Only 2 of the lines were above 32 chars, assuming 1 char = 1 bytes in lack of any explanation 6 * 2 = 12

Saving gas by removing = 0

Screenshot 2022-06-18 at 00 06 24

The warden is suggesting to brick the functionality of the code to save gas, I'm gonna have to dispute

Shorter code for save gas.

Disagree, it's the same code

Total Gas Saved 17 gas

Indeed could have done way better than this