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

1 stars 0 forks source link

Gas Optimizations #152

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

[G-01] Unnecessary variable initialization of default value

When variable is not initialized, then it will have default values. Example: 0 for uint, false for bool and address(0) for address

I suggest removing default value initialization for following variables.

OrderValidator.sol
 272:            for (uint256 i = 0; i < totalOrders; ) {
 350:            for (uint256 i = 0; i < totalOrders; ) {
BasicOrderFulfiller.sol
 948:        for (uint256 i = 0; i < totalAdditionalRecipients; ) {
1040:        for (uint256 i = 0; i < totalAdditionalRecipients; ) {
AmountDeriver.sol
  44:            uint256 extraCeiling = 0;
CriteriaResolution.sol
  56:            for (uint256 i = 0; i < totalCriteriaResolvers; ++i) {
 166:            for (uint256 i = 0; i < totalAdvancedOrders; ++i) {
 184:                for (uint256 j = 0; j < totalItems; ++j) {
 199:                for (uint256 j = 0; j < totalItems; ++j) {
OrderCombiner.sol
 181:            for (uint256 i = 0; i < totalOrders; ++i) {
 247:                for (uint256 j = 0; j < offer.length; ++j) {
 291:                for (uint256 j = 0; j < consideration.length; ++j) {
 373:            for (uint256 i = 0; i < totalOrders; ++i) {
 470:            uint256 totalFilteredExecutions = 0;
 473:            for (uint256 i = 0; i < totalOfferFulfillments; ++i) {
 498:            for (uint256 i = 0; i < totalConsiderationFulfillments; ++i) {
 577:            for (uint256 i = 0; i < totalOrders; ++i) {
 598:                for (uint256 j = 0; j < consideration.length; ++j) {
 621:        for (uint256 i = 0; i < executions.length; ) {
 751:            uint256 totalFilteredExecutions = 0;
 754:            for (uint256 i = 0; i < totalFulfillments; ++i) {
OrderFulfiller.sol
 217:            for (uint256 i = 0; i < orderParameters.offer.length; ) {
 306:            for (uint256 i = 0; i < orderParameters.consideration.length; ) {
 471:            for (uint256 i = 0; i < totalOrders; ++i) {
TokenTransferrerConstants.sol
 163:uint256 constant Invalid1155BatchTransferEncoding_ptr = 0x00;
Conduit.sol
  66:        for (uint256 i = 0; i < totalStandardTransfers; ) {
 130:        for (uint256 i = 0; i < totalStandardTransfers; ) {

For example these can change to:

[G-02] Save Gas in For-Loops by storing array's length as a variable

3 gas per iteration can be saved by storing an array's length as a variable before the for-loop.

Issue found at:

OrderCombiner.sol
 247:                for (uint256 j = 0; j < offer.length; ++j) {
 291:                for (uint256 j = 0; j < consideration.length; ++j) {
 598:                for (uint256 j = 0; j < consideration.length; ++j) {
 621:        for (uint256 i = 0; i < executions.length; ) {
OrderFulfiller.sol
 217:            for (uint256 i = 0; i < orderParameters.offer.length; ) {
 306:            for (uint256 i = 0; i < orderParameters.consideration.length; ) {

For example, I suggest changing the ones for OrderCombiner.sol to:

offerLength = offer.length
for (uint256 j; j < offerLength; ++j) {

[G-03] ++i costs less gas than i++

It is better to use ++i than i++ when possible since it costs less gas. (Same for i-- as well)

Issue found at:

OrderCombiner.sol
 229:                maximumFulfilled--;
HardlyDifficult commented 2 years ago

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

The other two should provide small savings.