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

2 stars 0 forks source link

Gas Optimizations #264

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Unnecessary equals boolean

Boolean variables can be checked within conditionals directly without the use of equality operators to true/false.

Code instances:

    Cally.sol, 220: require(vault.isWithdrawing == false, "Vault is being withdrawn");
    Cally.sol, 328: require(vault.isExercised == false, "Vault already exercised");
    Cally.sol, 217: require(vault.isExercised == false, "Vault already exercised");

Unused state variables

Unused state variables are gas consuming at deployment (since they are located in storage) and are a bad code practice. Removing those variables will decrease deployment gas cost and improve code quality. This is a full list of all the unused storage variables we found in your code base.

Code instance:

    Utils.sol, NULL

Change transferFrom to transfer

'transferFrom(address(this), *, *)' could be replaced by the following more gas efficient 'transfer(, **)' This replacement is more gas efficient and improves the code quality.

Code instances:

    Cally.sol, 295 : ? ERC721(vault.token).transferFrom(address(this), msg.sender, vault.tokenIdOrAmount)
    Cally.sol, 344 : ? ERC721(vault.token).transferFrom(address(this), msg.sender, vault.tokenIdOrAmount)

Caching array length can save gas

Caching the array length is more gas efficient. This is because access to a local variable in solidity is more efficient than query storage / calldata / memory. We recommend to change from:

for (uint256 i=0; i<array.length; i++) { ... }

to:

uint len = array.length  
for (uint256 i=0; i<len; i++) { ... }

Code instances:

    Test.sol, b, 362
    CallyNft.sol, data, 244

Prefix increments are cheaper than postfix increments

Prefix increments are cheaper than postfix increments. Further more, using unchecked {++x} is even more gas efficient, and the gas saving accumulates every iteration and can make a real change There is no risk of overflow caused by increamenting the iteration index in for loops (the ++i in for (uint256 i = 0; i < numIterations; ++i)). But increments perform overflow checks that are not necessary in this case. These functions use not using prefix increments (++x) or not using the unchecked keyword:

Code instances:

    change to prefix increment and unchecked: Test.sol, i, 353
    change to prefix increment and unchecked: Test.sol, i, 362
    change to prefix increment and unchecked: CallyNft.sol, i, 244

Unnecessary index init

In for loops you initialize the index to start from 0, but it already initialized to 0 in default and this assignment cost gas. It is more clear and gas efficient to declare without assigning 0 and will have the same meaning:

Code instances:

    Test.sol, 362
    Test.sol, 353
    CallyNft.sol, 244

Unnecessary default assignment

Unnecessary default assignments, you can just declare and it will save gas and have the same meaning.

Code instances:

    Cally.sol (L#95) : uint256 public protocolUnclaimedFees = 0; 
    Cally.sol (L#94) : uint256 public feeRate = 0;

Use bytes32 instead of string to save gas whenever possible

Use bytes32 instead of string to save gas whenever possible.
String is a dynamic data structure and therefore is more gas consuming then bytes32.

Code instances:

    CallyNft.sol (L70), string memory nftType = isVault_ ? "Vault" : "Option"; 
    SVG.sol (L186), return string.concat(_key, '=', '"', _val, '" ');
    Renderer.sol (L10), string.concat( '<svg xmlns="http:

Use != 0 instead of > 0

Using != 0 is slightly cheaper than > 0. (see https://github.com/code-423n4/2021-12-maple-findings/issues/75 for similar issue)

Code instances:

    Cally.sol, 283: change 'feeRate > 0' to 'feeRate != 0'
    Cally.sol, 170: change 'durationDays > 0' to 'durationDays != 0'

Use unchecked to save gas for certain additive calculations that cannot overflow

You can use unchecked in the following calculations since there is no risk to overflow:

Code instances:

    Cally.sol (L#238) - vault.currentExpiration = uint32(block.timestamp) + (vault.durationDays * 1 days); 
    Test.sol (L#19) - vm.warp(block.timestamp + time);

Consider inline the following functions to save gas

You can inline the following functions instead of writing a specific function to save gas.
(see https://github.com/code-423n4/2021-11-nested-findings/issues/167 for a similar issue.)

Code instances

    SVG.sol, text, { return el('text', _props, _children); }
    Bytes32AddressLib.sol, fillLast12Bytes, { return bytes32(bytes20(addressValue)); }
    Test.sol, sigs, { return bytes4(keccak256(bytes(sigStr))); }
    FixedPointMathLib.sol, divWadUp, { return mulDivUp(x, WAD, y); // Equivalent to (x * WAD) / y rounded up. }
    FixedPointMathLib.sol, divWadDown, { return mulDivDown(x, WAD, y); // Equivalent to (x * WAD) / y rounded down. }

Inline one time use functions

The following functions are used exactly once. Therefore you can inline them and save gas and improve code clearness.

Code instances:

    CREATE3.sol, getDeployed
    FixedPointMathLib.sol, sqrt

Upgrade pragma to at least 0.8.4

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.

Code instances:

    console.sol
    base64.sol
    Vm.sol
    Test.sol