You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function takeOrders(OrderTypes.MakerOrder[] calldata makerOrders, OrderTypes.OrderItem[][] calldata takerNfts)
external
payable
nonReentrant
{
...
if (!isMakerSeller) {
require(currency != address(0), 'offers only in ERC20');
}
...
// @audit it should only check `currency == address(0)` since
// at Line 9 it is already guaranteed that when currency == address(0),
// isMakerSeller must be true
//
// it is worth to optimize since this is within a loop (which
// is in the root caller)
if (isMakerSeller && currency == address(0)) {
require(msg.value >= totalPrice, 'invalid total price');
}
}
There are other similar code snippets whose gas can be further optimized in a similar way.
function matchOneToOneOrders(
OrderTypes.MakerOrder[] calldata makerOrders1,
OrderTypes.MakerOrder[] calldata makerOrders2
) external {
uint256 startGas = gasleft();
...
for (uint256 i = 0; i < numMakerOrders; ) {
// @audit should be unchecked since gas limit cannot exceed
// type(uint256).max
//
// it is worth to optimize since this is within a loop
uint256 startGasPerOrder = gasleft() + ((startGas - gasleft()) / numMakerOrders);
...
}
There are other similar code snippets whose gas can be further optimized in a similar way.
function _getCurrentPrice(OrderTypes.MakerOrder calldataorder) internalviewreturns (uint256) {
(uint256startPrice, uint256endPrice) = (order.constraints[1], order.constraints[2]);
// @audit can be unchecked, as order.constraints[4] and order.constraints[3] are both uint256, // and checks for [3] <= [4] exist in all the functions that // call _getCurrentPrice()//// worth to optimize since the code is involved in a root loopuint256 duration = order.constraints[4] - order.constraints[3];
uint256 priceDiff = startPrice > endPrice ? startPrice - endPrice : endPrice - startPrice;
if (priceDiff ==0|| duration ==0) {
return startPrice;
}
...
}
function stake(uint256 amount, Duration duration) external override nonReentrant whenNotPaused {
...
// @audit can be removed, since `safeTransferFrom` will guarantee
// there are enough funds.
//
// Can optimize out an external call
require(IERC20(INFINITY_TOKEN).balanceOf(msg.sender) >= amount, 'insufficient balance to stake');
...
IERC20(INFINITY_TOKEN).safeTransferFrom(msg.sender, address(this), amount);
...
}
function changeDuration(
uint256 amount,
Duration oldDuration,
Duration newDuration
) external override nonReentrant whenNotPaused {
...
// @audit can be removed, since the following checked substraction will
// guard the underflow
//
// can optimize out a few SLOAD opcodes
require(
userstakedAmounts[msg.sender][oldDuration].amount >= amount,
'insufficient staked amount to change duration'
);
require(newDuration > oldDuration, 'new duration must be greater than old duration');
userstakedAmounts[msg.sender][oldDuration].amount -= amount;
...
}
Functions matchOneToOneOrders and matchOneToManyOrders check whether _complications.contains(makerOrder.execParams[0]) while matchOrders does not.
Essentially, this _complications.contains(makerOrder.execParams[0]) is redundant since each orde will be checked by isOrderValid later. In redundant, the complication will be checked.
Also note that _complications.contains(makerOrder.execParams[0]) is not that gas-friendly. Hence:
If preferring gas-saving, remove the complication checks in functions matchOneToOneOrders and matchOneToManyOrders.
If preferring readability (and consistency), add the complication check in function matchOrders
Gas Optimization for Infinity NFT Marketplace
G1
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L361
See
@audit
comment.There are other similar code snippets whose gas can be further optimized in a similar way.
G2
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1152-L1165
See
@audit
comment.There are other similar code snippets whose gas can be further optimized in a similar way.
G3
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L1152-L1165
See
@audit
comment.There are other similar code snippets whose gas can be further optimized in a similar way.
G4
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L149
There are other similar code snippets whose gas can be further optimized in a similar way.
G5
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/token/InfinityToken.sol#L72
See
@audit
comment.G6
Link:https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L332
See
@audit
comment:G7
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/staking/InfinityStaker.sol#L69
see
@audit
comment:G8
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/staking/InfinityStaker.sol#L92
see
@audit
comment:G9
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/token/InfinityToken.sol#L65-L73
see
@audit
comment:G10
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L132
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L178
Link: https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityExchange.sol#L256
Functions
matchOneToOneOrders
andmatchOneToManyOrders
check whether_complications.contains(makerOrder.execParams[0])
whilematchOrders
does not.Essentially, this
_complications.contains(makerOrder.execParams[0])
is redundant since each orde will be checked byisOrderValid
later. Inredundant
, the complication will be checked.Also note that
_complications.contains(makerOrder.execParams[0])
is not that gas-friendly. Hence:matchOneToOneOrders
andmatchOneToManyOrders
.matchOrders
G11
Link:https://github.com/code-423n4/2022-06-infinity/blob/765376fa238bbccd8b1e2e12897c91098c7e5ac6/contracts/core/InfinityOrderBookComplication.sol#L337
See
@audit
comment:The text was updated successfully, but these errors were encountered: