From 93a317e331ee7aca12c8133d5b7a8f8b5527be33 Mon Sep 17 00:00:00 2001 From: MollyHe5 Date: Tue, 22 Aug 2023 21:20:58 -0400 Subject: [PATCH] replace && with nested if to save gas --- src/conduit/ConduitController.sol | 74 ++++++++++++++++--------------- src/helpers/TransferHelper.sol | 20 +++++---- 2 files changed, 51 insertions(+), 43 deletions(-) diff --git a/src/conduit/ConduitController.sol b/src/conduit/ConduitController.sol index 1782fc57..9dac7304 100644 --- a/src/conduit/ConduitController.sol +++ b/src/conduit/ConduitController.sol @@ -126,43 +126,47 @@ contract ConduitController is ConduitControllerInterface { bool channelPreviouslyOpen = channelIndexPlusOne != 0; // If the channel has been set to open and was previously closed... - if (isOpen && !channelPreviouslyOpen) { - // Add the channel to the channels array for the conduit. - conduitProperties.channels.push(channel); - - // Add new open channel length to associated mapping as index + 1. - conduitProperties.channelIndexesPlusOne[channel] = (conduitProperties.channels.length); - } else if (!isOpen && channelPreviouslyOpen) { - // Set a previously open channel as closed via "swap & pop" method. - // Decrement located index to get the index of the closed channel. - uint256 removedChannelIndex; - - // Skip underflow check as channelPreviouslyOpen being true ensures - // that channelIndexPlusOne is nonzero. - unchecked { - removedChannelIndex = channelIndexPlusOne - 1; - } - - // Use length of channels array to determine index of last channel. - uint256 finalChannelIndex = conduitProperties.channels.length - 1; - - // If closed channel is not last channel in the channels array... - if (finalChannelIndex != removedChannelIndex) { - // Retrieve the final channel and place the value on the stack. - address finalChannel = (conduitProperties.channels[finalChannelIndex]); - - // Overwrite the removed channel using the final channel value. - conduitProperties.channels[removedChannelIndex] = finalChannel; + if (isOpen) { + if (!channelPreviouslyOpen) { + // Add the channel to the channels array for the conduit. + conduitProperties.channels.push(channel); - // Update final index in associated mapping to removed index. - conduitProperties.channelIndexesPlusOne[finalChannel] = (channelIndexPlusOne); + // Add new open channel length to associated mapping as index + 1. + conduitProperties.channelIndexesPlusOne[channel] = (conduitProperties.channels.length); + } + } else { + if (channelPreviouslyOpen) { + // Set a previously open channel as closed via "swap & pop" method. + // Decrement located index to get the index of the closed channel. + uint256 removedChannelIndex; + + // Skip underflow check as channelPreviouslyOpen being true ensures + // that channelIndexPlusOne is nonzero. + unchecked { + removedChannelIndex = channelIndexPlusOne - 1; + } + + // Use length of channels array to determine index of last channel. + uint256 finalChannelIndex = conduitProperties.channels.length - 1; + + // If closed channel is not last channel in the channels array... + if (finalChannelIndex != removedChannelIndex) { + // Retrieve the final channel and place the value on the stack. + address finalChannel = (conduitProperties.channels[finalChannelIndex]); + + // Overwrite the removed channel using the final channel value. + conduitProperties.channels[removedChannelIndex] = finalChannel; + + // Update final index in associated mapping to removed index. + conduitProperties.channelIndexesPlusOne[finalChannel] = (channelIndexPlusOne); + } + + // Remove the last channel from the channels array for the conduit. + conduitProperties.channels.pop(); + + // Remove the closed channel from associated mapping of indexes. + delete conduitProperties.channelIndexesPlusOne[channel]; } - - // Remove the last channel from the channels array for the conduit. - conduitProperties.channels.pop(); - - // Remove the closed channel from associated mapping of indexes. - delete conduitProperties.channelIndexesPlusOne[channel]; } } diff --git a/src/helpers/TransferHelper.sol b/src/helpers/TransferHelper.sol index ba9adada..9a3a3136 100644 --- a/src/helpers/TransferHelper.sol +++ b/src/helpers/TransferHelper.sol @@ -222,15 +222,19 @@ contract TransferHelper is TransferHelperInterface, TransferHelperErrors { // Pass through the custom error in question if the revert data is // the correct length and matches an expected custom error selector. - if (data.length == 4 && customErrorSelector == InvalidItemType.selector) { - // "Bubble up" the revert reason. - assembly { - revert(add(data, 0x20), 0x04) + if (data.length == 4) { + if (customErrorSelector == InvalidItemType.selector) { + // "Bubble up" the revert reason. + assembly { + revert(add(data, 0x20), 0x04) + } } - } else if (data.length == 36 && customErrorSelector == InvalidERC721TransferAmount.selector) { - // "Bubble up" the revert reason. - assembly { - revert(add(data, 0x20), 0x24) + } else if (data.length == 36) { + if (customErrorSelector == InvalidERC721TransferAmount.selector) { + // "Bubble up" the revert reason. + assembly { + revert(add(data, 0x20), 0x24) + } } }