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

1 stars 0 forks source link

Gas Optimizations #128

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

G01 No need to explicitly initialize variables with default values

If a variable is not initialized it is automatically set to the default value (0 for uint, false for bool, address(0) for address...). Explicitly initializing it with its default value is an anti-pattern and wastes gas.

Example Instance: uint256 extraCeiling = 0; -> uint256 extraCeiling;

In AmountDeriver._locateCurrentAmount

This pattern is used everywhere in the protocol.

G02 In some places, loop memory variables which don't change are not cached

Calling storage/memory variable which does not change, every loop iteration, is wrong and wastes gas. In most places this is taken care of, however not everywhere.

For example, OrderCombiner.sol#L247:

// Retrieve array of offer items for the order in question.
OfferItem[] memory offer = advancedOrder.parameters.offer;

// Iterate over each offer item on the order.
for (uint256 j = 0; j < offer.length; ++j) {
    // Retrieve the offer item.
    OfferItem memory offerItem = offer[j];

instead, cache the length:

// Retrieve array of offer items for the order in question.
OfferItem[] memory offer = advancedOrder.parameters.offer;

uint256 totalOffers = offer.length
// Iterate over each offer item on the order.
for (uint256 j = 0; j < totalOffers; ++j) {
    // Retrieve the offer item.
    OfferItem memory offerItem = offer[j];

Other Instances Include (but not limited to):

OrderCombiner.sol#L291

OrderCombiner.sol#L621

OrderFulfiller.sol#L217

OrderFulfiller.sol#L306

ReferenceBasicOrderFulfiller.sol#L643

ReferenceBasicOrderFulfiller.sol#L712