code-423n4 / 2022-04-badger-citadel-findings

0 stars 1 forks source link

Gas Optimizations #211

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Gas Optimization Report

Make if statement inclusive

if (amount > claimable) -> if (amount >= claimable)

making the inequality strinct can save gas

https://github.com/code-423n4/2022-04-badger-citadel/blob/18f8c392b6fc303fe95602eba6303725023e53da/src/StakedCitadelVester.sol#L87

Use saleEnd instead of saleDuration to avoid calulating saleStart + saleDuration repeatadly

the only use of saleDuration is in the calculations of saleStart + saleDuration. Hence, it will save gas to just save in storage the sale end instead of saleDuration. Update the initializer and function setSaleDuration accordingly

https://github.com/code-423n4/2022-04-badger-citadel/blob/18f8c392b6fc303fe95602eba6303725023e53da/src/KnightingRound.sol#L36

cache vesting[recipient]

StakedCitadelVester.claimableBalance, cache vesting[recipient]. Can save 5 storage calls.

 function claimableBalance(address recipient) public view returns (uint256) {
    uint256 locked = vesting[recipient].lockedAmounts;
    uint256 claimed = vesting[recipient].claimedAmounts;
    if (block.timestamp >= vesting[recipient].unlockEnd) {
        return locked - claimed;
    }
    return
        ((locked * (block.timestamp - vesting[recipient].unlockBegin)) /
            (vesting[recipient].unlockEnd -
                vesting[recipient].unlockBegin)) - claimed;
}

https://github.com/code-423n4/2022-04-badger-citadel/blob/18f8c392b6fc303fe95602eba6303725023e53da/src/StakedCitadelVester.sol#L108

StakedCitadel._withdraw, calling token.balanceOf(address(this)) twice.

takedCitadel._withdraw calls balance() and then token.balanceOf(address(this)), which do exactly the same and will return the same.

Instead, call uint256 b = balance(); after the require, then use b for the 2 usages, here and here

uint256 r = (balance() * _shares) / totalSupply();
_burn(msg.sender, _shares);

// Check balance
uint256 b = token.balanceOf(address(this));

->

uint256 b = balance();
uint256 r = (b * _shares) / totalSupply();
_burn(msg.sender, _shares);

Upgrade pragma to at least 0.8.4 for StakedCitadelLocker.sol, MedianOracle.sol

Using newer compiler versions and the optimizer gives gas optimizations and additional safety checks are available for free.

The advantages of versions 0.8.* over <0.8.0 are:

  1. Safemath by default from 0.8.0 (can be more gas efficient than library based safemath.)
  2. Low level inliner : from 0.8.2, leads to cheaper runtime gas. Especially relevant when the contract has small functions. For example, OpenZeppelin libraries typically have a lot of small helper functions and if they are not inlined, they cost an additional 20 to 40 gas because of 2 extra jump instructions and additional stack operations needed for function calls.
  3. Optimizer improvements in packed structs: Before 0.8.3, storing packed structs, in some cases used an additional storage read operation. After EIP-2929, if the slot was already cold, this means unnecessary stack operations and extra deploy time costs. However, if the slot was already warm, this means additional cost of 100 gas alongside the same unnecessary stack operations and extra deploy time costs.
  4. Custom errors from 0.8.4, leads to cheaper deploy time cost and run time cost. Note: the run time cost is only relevant when the revert condition is met. In short, replace revert strings by custom errors.

https://github.com/Citadel-DAO/staked-citadel-locker/blob/efc27b442fd4089369e4d2df163f82c35aa81537/src/StakedCitadelLocker.sol#L2

https://github.com/ampleforth/market-oracle/blob/5e7fd1506784f074748ab6bd5df740ca2227b14f/contracts/MedianOracle.sol#L1

General Gas Saving Good Practices

Change i++ to ++i

i++ is generally more expensive because it must increment a value and return the old value, so it may require holding two numbers in memory. ++i only uses one number in memory.

Example:

for (uint256 i = 0; i < numPools; i++) -> for (uint256 i = 0; i < numPools ++i)

Use calldata

Use calldata instead of memory for external functions where the function argument is read-only. Reading directly from calldata using calldataload instead of going via memory saves the gas from the intermediate memory operations that carry the values.

No need to explicitly initialize variables with default values

If a variable is not initialized it is automatically set to the default value (0 for uint, false for bool, address(0) for address...). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

Contracts: CitadelMinter.sol StakedCitadelLocker.sol SupplySchedule.sol