code-423n4 / 2022-05-opensea-seaport-findings

1 stars 0 forks source link

Gas Optimizations #134

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Non-required initialisation:

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L350 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L272 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L217 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L306 It's not required to initialise i to 0, by default it is 0.

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L271 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L568 amount is 0 by default, not required to initialise

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L274 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L571 errorBuffer is 0 by default, not required to initialise

>= and <= is cheaper than > and <:

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/ZoneInteraction.sol#L47 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/ZoneInteraction.sol#L116 Change uint256(orderType) > 1 to uint256(orderType) >= 2

Unused or redundant named return variable

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L215-L237 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L300-L323 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L349-L360 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L398-L410 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L517-L530 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Consideration.sol#L556-L568 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L104-L243 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L418-L438 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L457-L479 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L48-L121

In order to reduce contract size and save gas, use either the returns or return statement.

Save gas by reducing number of SSTORE

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L70-L73 Instead of doing 4 SStore, we can first save the values in memory, and then store the whole object directly OrderStatus memory orderStatus = _orderStatus[orderHash]; orderStatus.isValidated = true; orderStatus.isCancelled = false; orderStatus.numerator = 1; orderStatus.denominator = 1; _orderStatus[orderHash] = orderStatus; https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L226-L238 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L303-L304

Save gas by caching

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L217 https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L306 Cache the value of orderParameters.offer.length in a variable, and use that variable in the loop

Save gas in if else

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderValidator.sol#L197 Change to if (filledDenominator) { https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/OrderFulfiller.sol#L376 Change to if (etherRemaining) { https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/Verifiers.sol#L120 Change to if (orderStatus.numerator) {

HardlyDifficult commented 2 years ago

Non-required initialisation

The optimizer will improve this automatically.

= and <= is cheaper than > and <:

It's the other way around -- >= requires 2 op codes vs < which uses only one.

Unused or redundant named return variable

This has minimal impact and often done this way for improved code readability.

Save gas by reducing number of SSTORE

I ran the gas report for the recommended change, saves 771-797 gas on fulfillBasicOrder per the hardhat profile report.

Save gas by caching

This should provide small savings.

Save gas in if else

The recommended change is throwing a compile error.