Skip to content

Commit

Permalink
⚡ Fallback callback
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Feb 13, 2024
1 parent 9c5b95f commit 7c3808f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
20 changes: 10 additions & 10 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
IETest:testBalanceInERC20() (gas: 62602)
IETest:testBalanceInETH() (gas: 53716)
IETest:testCommandSendERC0() (gas: 114380)
IETest:testCommandSendETH() (gas: 80852)
IETest:testCommandSendUSDC() (gas: 147088)
IETest:testCommandSwapDAI() (gas: 101806)
IETest:testCommandSwapUSDC() (gas: 156797)
IETest:testDeploy() (gas: 2132114)
IETest:testCommandSendERC0() (gas: 114314)
IETest:testCommandSendETH() (gas: 80786)
IETest:testCommandSendUSDC() (gas: 147022)
IETest:testCommandSwapDAI() (gas: 101074)
IETest:testCommandSwapUSDC() (gas: 156385)
IETest:testDeploy() (gas: 2106248)
IETest:testENSNameFromENSHelper() (gas: 29103)
IETest:testENSNameOwnership() (gas: 109220)
IETest:testENSNameOwnership() (gas: 109286)
IETest:testIENameSetting() (gas: 8187)
IETest:testPreviewCommandSendDecimals() (gas: 111568)
IETest:testPreviewCommandSendUSDC() (gas: 77413)
IETest:testPreviewCommandSendDecimals() (gas: 111612)
IETest:testPreviewCommandSendUSDC() (gas: 77435)
IETest:testPreviewSend() (gas: 54713)
IETest:testPreviewSendCommand() (gas: 66562)
IETest:testPreviewSendCommand() (gas: 66584)
IETest:testSendETH() (gas: 70967)
IETest:testTotalSupply() (gas: 14871)
48 changes: 25 additions & 23 deletions src/IE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -294,29 +294,6 @@ contract IE {
);
}

/// @dev Callback for IUniswapV3PoolActions#swap. If ETH is source, WETH is forwarded.
function uniswapV3SwapCallback(int256 amount0Delta, int256 amount1Delta, bytes calldata data)
public
payable
virtual
{
bool isETH;
bool zeroForOne;
address tokenIn;
address payer;
assembly ("memory-safe") {
isETH := byte(0, calldataload(data.offset))
zeroForOne := byte(0, calldataload(add(data.offset, 1)))
tokenIn := shr(96, calldataload(add(data.offset, 2)))
payer := shr(96, calldataload(add(data.offset, 22)))
}
isETH
? WETH.safeTransfer(msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta))
: tokenIn.safeTransferFrom(
payer, msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta)
);
}

/// @dev Computes the create2 address for given token pair.
function _computePoolAddress(address tokenA, address tokenB)
internal
Expand All @@ -341,6 +318,31 @@ contract IE {
);
}

/// @dev Fallback `uniswapV3SwapCallback`.
/// If ETH is swapped, WETH is forwarded.
fallback() external {
int256 amount0Delta;
int256 amount1Delta;
bool isETH;
bool zeroForOne;
address tokenIn;
address payer;
assembly ("memory-safe") {
let pos := 132
amount0Delta := calldataload(4)
amount1Delta := calldataload(36)
isETH := byte(0, calldataload(pos))
zeroForOne := byte(0, calldataload(add(pos, 1)))
tokenIn := shr(96, calldataload(add(pos, 2)))
payer := shr(96, calldataload(add(pos, 22)))
}
isETH
? WETH.safeTransfer(msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta))
: tokenIn.safeTransferFrom(
payer, msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta)
);
}

/// ================== BALANCE & SUPPLY HELPERS ================== ///

/// @dev Returns the balance of a named account in a named token.
Expand Down

0 comments on commit 7c3808f

Please sign in to comment.