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

0 stars 0 forks source link

Gas Optimizations #217

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS

1. Title: Unnecessary _mint() in constructor() https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Velo.sol#L22

Minting 0 amount of token is unnecessary and wasting deployment gas. I recommend to remove L22

2. Title: Unnecessary allowed_from MSTORE https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Velo.sol#L61

MSTORing to allowed_from is wasting gas since its just called once in the function RECOMMENDED MITIGATION STEP

    function transferFrom(address _from, address _to, uint _value) external returns (bool) {
        if (allowance[_from][msg.sender] != type(uint).max) { //@audit-info: Call it directly
            allowance[_from][msg.sender] -= _value;
        }
        return _transfer(_from, _to, _value);
    }

3. Title: Returning _mint() value directly https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Velo.sol#L71 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Velo.sol#L77

Unlike transfer() and transferFrom() functions were implemented (which returning the _transfer() return value directly). mint() function is returning true RECOMMENDED MITIGATION STEP

    function mint(address account, uint amount) external returns (bool) {
        require(msg.sender == minter);
        return _mint(account, amount);

4. Title: Using prefix increment than postfix increment https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L137

Using prefix increment is way cheaper for gas usage than postfix increment Change to:

    ++digits;

5. Title: Better way to do decrement and increment https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L142 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L655

Instead of using -= 1 to decrease value by 1, using --var is more optimum Change to:

    --digits;

6. Title: Using && operator is inefficient https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L307

Instead of using && operator. Using multiple require can save 15 execution gas per call Change to:

require(attachments[_tokenId] == 0, "attached");
require(!voted[_tokenId], "attached");

7. Title: Using != instead of > Occurrence: https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L369 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L591 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L595 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L785

Using != operator is way more effective than > to validate that the var value is not zero

8. Title: Using delete statement https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L484-L486 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L498-L500 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1071

Instead of set the mapping value to 0, using delete statement is more effective and can save 4 gas each call Change to:

            delete ownerToNFTokenIdList[_from][current_count];

9. Title: Using unchecked for increase i value in for() https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L632 https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L1325

Using unchecked{++i;} can save a lot of gas

for(uint i; i < 255;){
    //the codes
unchecked{++i;}
}

10. Title: Unnecessary msg.sender MSTORE https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/VotingEscrow.sol#L746

MLOAD are way more expensive than just call msg.sender. So, MSTORing msg.sender is wasting gas. I recommend just remove L746 and just use msg.sender

11. Title: Storage slot packing impacts gas efficiency https://github.com/code-423n4/2022-05-velodrome/blob/main/contracts/contracts/Pair.sol#L18

The declaration order of state variables affects storage slot packing and gas impact from reads/writes of shared slots. By placing stable var next to any address var can save 1 slot for storage which can make more efficient for reading the storage and gas usage

GalloDaSballo commented 2 years ago

Report would save between 100 and 500 gas