code-423n4 / 2022-07-swivel-findings

0 stars 1 forks source link

Gas Optimizations #105

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

instead of abi.encode use abi.encodepacked to save gas

because abi.encode padds up with zeros and abi.encodepacked packes.

instead of sloading the variable which costs 100 gas on warm ,make it memory (3 gas)

    uint256 maturityRatememory=maturityRate;
    yield = ((maturityRatememory * 1e26) / vlt.exchangeRate) - 1e26;
    

https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L70 change maturityRate to memory same thing with the if statement above make it into a memory variable when you start the function to save gas instead of sloads which costs more then mloads after you do that one sload to get the mstore then it costs less gas (200 gas saving each time ) https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L68 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L108-L110 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L142-L144 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L163 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L193-L195 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L212-L214 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L254-L257

make admin functions payable to save gas

because there is no check on msg.value =0 so it saves gas and you should use it only on admin functions https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L92 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L108-L110 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L142-L144 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L163 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L176 https://github.com/code-423n4/2022-07-swivel/blob/e64987b0f35f90c6578feb8e789c1fd53092fc7b/VaultTracker/VaultTracker.sol#L236

you can squaze in another 96bit variable at no cost

because an addres takes up 160 bites and there are 256 bit slot you can add 96 bit varaible for free

    struct Market {

        address cTokenAddr;
        uint96 
        address zcToken;
        uint96
        address vaultTracker;
        uint96 
        uint256 maturityRate;

    }

https://github.com/code-423n4/2022-07-swivel/blob/67c6900222cc4045d7fe2227a1ea73e0251374ed/Creator/IRedeemer.sol#L6-L11

allowance used multiple times in the function but allowance never changes when both are used

     uint256 allowed = allowance[holder][msg.sender];

            if (allowed >= previewAmount) {

                revert Approvals(allowed, previewAmount);

            }

            allowance[holder][msg.sender] -= previewAmount;

just use allowed -=previwAmount to save 100 gas on sload

https://github.com/code-423n4/2022-07-swivel/blob/67c6900222cc4045d7fe2227a1ea73e0251374ed/Creator/ZcToken.sol#L115 https://github.com/code-423n4/2022-07-swivel/blob/67c6900222cc4045d7fe2227a1ea73e0251374ed/Creator/ZcToken.sol#L115

robrobbins commented 2 years ago

abi.encode must be used because of signature mechanisms at work. ..encodePacked will not work here.

sload used vs mload in these many times to avoid stack-too-deep (or it only being used once or twice)