Skip to content

Commit

Permalink
⚡ ~~
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Feb 15, 2024
1 parent 319bc31 commit 46f3661
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
38 changes: 19 additions & 19 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
IETest:testBalanceInERC20() (gas: 49207)
IETest:testBalanceInETH() (gas: 40333)
IETest:testCommandSendERC0() (gas: 102975)
IETest:testCommandSendETH() (gas: 69457)
IETest:testCommandSendUSDC() (gas: 135705)
IETest:testCommandSwapDAI() (gas: 102148)
IETest:testCommandSwapETH() (gas: 113570)
IETest:testCommandSwapForETH() (gas: 123455)
IETest:testCommandSwapUSDC() (gas: 157129)
IETest:testCommandSwapUSDCForWBTC() (gas: 165096)
IETest:testDeploy() (gas: 2656246)
IETest:testENSNameOwnership() (gas: 84066)
IETest:testIENameSetting() (gas: 8142)
IETest:testPreviewCommandSendDecimals() (gas: 91972)
IETest:testPreviewCommandSendUSDC() (gas: 66096)
IETest:testPreviewSend() (gas: 42673)
IETest:testPreviewSendCommand() (gas: 54554)
IETest:testSendETH() (gas: 59695)
IETest:testTotalSupply() (gas: 14844)
IETest:testBalanceInERC20() (gas: 52391)
IETest:testBalanceInETH() (gas: 43495)
IETest:testCommandSendERC0() (gas: 103640)
IETest:testCommandSendETH() (gas: 70119)
IETest:testCommandSendUSDC() (gas: 136370)
IETest:testCommandSwapDAI() (gas: 102128)
IETest:testCommandSwapETH() (gas: 113501)
IETest:testCommandSwapForETH() (gas: 123704)
IETest:testCommandSwapUSDC() (gas: 156135)
IETest:testCommandSwapUSDCForWBTC() (gas: 164392)
IETest:testDeploy() (gas: 2650229)
IETest:testENSNameOwnership() (gas: 87602)
IETest:testIENameSetting() (gas: 10978)
IETest:testPreviewCommandSendDecimals() (gas: 96485)
IETest:testPreviewCommandSendUSDC() (gas: 69606)
IETest:testPreviewSend() (gas: 46115)
IETest:testPreviewSendCommand() (gas: 57997)
IETest:testSendETH() (gas: 60357)
IETest:testTotalSupply() (gas: 18031)
25 changes: 10 additions & 15 deletions src/IE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract IE {
/// @dev 0-liquidity.
error InvalidSwap();

/// @dev Invalid command form.
/// @dev Invalid command.
error InvalidSyntax();

/// @dev Non-numeric character.
Expand Down Expand Up @@ -247,7 +247,7 @@ contract IE {
return keccak256(executeCallData) == keccak256(userOp.callData);
}

/// @dev Checks and returns the canonical constant for a matched intent string.
/// @dev Checks and returns the canonical token address constant for a matched intent string.
function _returnTokenConstant(bytes32 token) internal view virtual returns (address _token) {
if (token == "eth" || token == "ether") return ETH;
if (token == "usdc") return USDC;
Expand Down Expand Up @@ -340,12 +340,13 @@ contract IE {
if (amount0Delta <= 0 && amount1Delta <= 0) revert InvalidSwap();
(address pool, bool zeroForOne) = _computePoolAddress(tokenIn, tokenOut);
if (msg.sender != pool) revert Unauthorized(); // Only pair pool can call.
if (ETHIn) _wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta));
ETHIn
? _sendWETH(msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta))
: tokenIn.safeTransferFrom(
if (ETHIn) {
_wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta));
} else {
tokenIn.safeTransferFrom(
payer, msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta)
);
}
if (ETHOut) {
uint256 amount = uint256(-(zeroForOne ? amount1Delta : amount0Delta));
_unwrapETH(amount);
Expand Down Expand Up @@ -390,25 +391,19 @@ contract IE {
}
}

/// @dev Sends an `amount` of ETH to WETH to wrap.
/// @dev Wraps an `amount` of ETH to WETH and funds pool caller for swap.
function _wrapETH(uint256 amount) internal virtual {
assembly ("memory-safe") {
pop(call(gas(), WETH, amount, codesize(), 0x00, codesize(), 0x00))
}
}

/// @dev Sends an `amount` of WETH into pair `pool` for swap.
function _sendWETH(address pool, uint256 amount) internal virtual {
assembly ("memory-safe") {
mstore(0x14, pool) // Store the `pool` argument.
mstore(0x14, caller()) // Store the `pool` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
pop(call(gas(), WETH, 0, 0x10, 0x44, codesize(), 0x00))
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}

/// @dev Unwraps an `amount` of ETH from WETH contract.
/// @dev Unwraps an `amount` of ETH from WETH for return.
function _unwrapETH(uint256 amount) internal virtual {
assembly ("memory-safe") {
mstore(0x00, 0x2e1a7d4d) // `withdraw(uint256)`.
Expand Down

0 comments on commit 46f3661

Please sign in to comment.