Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace && with nested if to save gas #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 39 additions & 35 deletions src/conduit/ConduitController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/helpers/TransferHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down