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

1 stars 0 forks source link

Gas Optimizations #100

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

GAS

1. Title: Initializing var with default value

Occurrences: https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L49 https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L97

By declaring var by not set its default value (0 for uint) can save gas cost Change to:

uint256 totalAmount;

2. Title: Using unchecked for i in a for() loop

Occurence: https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L50 https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L72 https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L122 https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/ERC1155Enumerable.sol#L51

Using unchecked for i can save execution gas fee:

for (uint256 i; i < length;) {
            if (!is1155) {
                require(amounts[i] == 1, "CNFT: Amounts must be all 1s for non-ERC1155s.");
unchecked{++i;}
            }

3. Title: Using && is not effective

Occurrences: https://github.com/bunkerfinance/bunker-protocol/blob/752126094691e7457d08fc62a6a5006df59bd2fe/contracts/CNft.sol#L66

Instead of using && which cost 15 execution gas fee per call. Using multiple require() is more effective RECOMMENDED MITIGATION STEP

                    require(checkSuccess, "Not the NFT owner");
                    require(nftOwner == msg.sender, "Not the NFT owner");