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

1 stars 0 forks source link

Gas Optimizations #199

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[Gas-1] No need to set 0 on uint variables

The default value of uint varibles are 0. Therefore, there is no need to set 0 on uint variables. Not setting 0 on uint variables can reduce the deployment gas cost.

Here are some examples:

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/AmountDeriver.sol#L44

uint256 extraCeiling = 0;

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/CriteriaResolution.sol#L56

for (uint256 i = 0; i < totalCriteriaResolvers; ++i) {

They can be modified as follows:

uint256 extraCeiling;
for (uint256 i; i < totalCriteriaResolvers; ++i) {

[Gas-2][FulfillmentApplier.sol] unchecked can be used in _applyFulfillment function

Since it has if (considerationItem.amount > execution.item.amount) check, following parts can be wrapped by unchecked like them.

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L97-L100

        if (considerationItem.amount > execution.item.amount) {
            ...

            unchecked {
                advancedOrders[targetComponent.orderIndex]
                    .parameters
                    .consideration[targetComponent.itemIndex]
                    .startAmount = considerationItem.amount - execution.item.amount;
            }

https://github.com/code-423n4/2022-05-opensea-seaport/blob/main/contracts/lib/FulfillmentApplier.sol#L109-L112

        } else {
            ...

            unchecked {
                advancedOrders[targetComponent.orderIndex]
                    .parameters
                    .offer[targetComponent.itemIndex]
                    .startAmount = execution.item.amount - considerationItem.amount;
            }
HardlyDifficult commented 2 years ago

No need to set 0 on uint variables

I tested this and got mixed results - some functions saved a tiny amount, others got a bit worse.

The other suggestion should have small savings.