From d7073c995524b7168baa1c2d8a0d44b45d645fa1 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:27:07 +0000 Subject: [PATCH 01/22] =?UTF-8?q?L2=EF=B8=8F=E2=83=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 33 +++---- lib/solady | 2 +- src/IE.sol | 110 +++++------------------ src/Names.sol | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/IE.t.sol | 115 +++++++----------------- 5 files changed, 312 insertions(+), 192 deletions(-) create mode 100644 src/Names.sol diff --git a/.gas-snapshot b/.gas-snapshot index 38aeb2c..914c9b1 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,19 +1,14 @@ -IETest:testBalanceInERC20() (gas: 49154) -IETest:testBalanceInETH() (gas: 40280) -IETest:testCommandSendERC0() (gas: 102922) -IETest:testCommandSendETH() (gas: 69404) -IETest:testCommandSendUSDC() (gas: 135652) -IETest:testCommandSwapDAI() (gas: 110787) -IETest:testCommandSwapETH() (gas: 113487) -IETest:testCommandSwapForETH() (gas: 130366) -IETest:testCommandSwapUSDC() (gas: 157128) -IETest:testCommandSwapUSDCForWBTC() (gas: 165068) -IETest:testDeploy() (gas: 2657924) -IETest:testENSNameOwnership() (gas: 83907) -IETest:testIENameSetting() (gas: 8142) -IETest:testPreviewCommandSendDecimals() (gas: 91866) -IETest:testPreviewCommandSendUSDC() (gas: 66043) -IETest:testPreviewSend() (gas: 42620) -IETest:testPreviewSendCommand() (gas: 54501) -IETest:testSendETH() (gas: 59642) -IETest:testTotalSupply() (gas: 14808) \ No newline at end of file +IETest:testBalanceInERC20() (gas: 66269) +IETest:testBalanceInETH() (gas: 50934) +IETest:testCommandSwapDAI() (gas: 133298) +IETest:testCommandSwapETH() (gas: 158848) +IETest:testCommandSwapForETH() (gas: 140019) +IETest:testCommandSwapUSDC() (gas: 162390) +IETest:testCommandSwapUSDCForWBTC() (gas: 180085) +IETest:testDeploy() (gas: 3011723) +IETest:testENSNameOwnership() (gas: 44458) +IETest:testIENameSetting() (gas: 8186) +IETest:testPreviewCommandSendDecimals() (gas: 100697) +IETest:testPreviewCommandSendUSDC() (gas: 74194) +IETest:testPreviewSend() (gas: 53578) +IETest:testPreviewSendCommand() (gas: 64400) \ No newline at end of file diff --git a/lib/solady b/lib/solady index 9deb9ed..1372606 160000 --- a/lib/solady +++ b/lib/solady @@ -1 +1 @@ -Subproject commit 9deb9ed36a27261a8745db5b7cd7f4cdc3b1cd4e +Subproject commit 1372606383445c0a247e6c58eb255a529734258a diff --git a/src/IE.sol b/src/IE.sol index cf0eef8..f9be183 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -5,6 +5,8 @@ pragma solidity ^0.8.19; import {SafeTransferLib} from "../lib/solady/src/utils/SafeTransferLib.sol"; import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol"; +import "./Names.sol"; + /// @title Intents Engine (IE) /// @notice Simple helper contract for turning transactional intents into executable code. /// @dev V1 simulates typical commands (sending and swapping tokens) and includes execution. @@ -72,14 +74,6 @@ contract IE { bytes signature; } - /// =========================== ENUMS =========================== /// - - /// @dev `ENSAsciiNormalizer` rules. - enum Rule { - DISALLOWED, - VALID - } - /// ========================= CONSTANTS ========================= /// /// @dev The governing DAO address. @@ -88,31 +82,26 @@ contract IE { /// @dev The NANI token address. address internal constant NANI = 0x00000000000025824328358250920B271f348690; + /// @dev The NANI naming system on Arbitrum. + address public immutable NAMES; + /// @dev The conventional ERC7528 ETH address. address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; /// @dev The canonical wrapped ETH address. - address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; + address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; /// @dev The popular wrapped BTC address. - address internal constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599; + address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f; /// @dev The Circle USD stablecoin address. - address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; + address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; /// @dev The Tether USD stablecoin address. - address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7; + address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; /// @dev The Maker DAO USD stablecoin address. - address internal constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F; - - /// @dev ENS fallback registry contract. - IENSHelper internal constant ENS_REGISTRY = - IENSHelper(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e); - - /// @dev ENS name wrapper token contract. - IENSHelper internal constant ENS_WRAPPER = - IENSHelper(0xD4416b13d2b3a9aBae7AcD5D6C2BbDBE25686401); + address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; /// @dev The address of the Uniswap V3 Factory. address internal constant UNISWAP_V3_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984; @@ -128,34 +117,16 @@ contract IE { uint160 internal constant MAX_SQRT_RATIO_MINUS_ONE = 1461446703485210103287273052203988822378723970341; - /// @dev String mapping for `ENSAsciiNormalizer` logic. - bytes internal constant ASCII_MAP = - hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; - /// ========================== STORAGE ========================== /// /// @dev DAO-governed token address naming. mapping(string name => address) public tokens; - /// @dev Each index in idnamap refers to an ascii code point. - /// If idnamap[char] > 2, char maps to a valid ascii character. - /// Otherwise, idna[char] returns Rule.DISALLOWED or Rule.VALID. - /// Modified from `ENSAsciiNormalizer` deployed by royalfork.eth - /// (0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758). - bytes1[] internal _idnamap; - /// ======================== CONSTRUCTOR ======================== /// - /// @dev Constructs this IE with `ASCII_MAP`. + /// @dev Constructs this IE on the Arbitrum L2 of Ethereum. constructor() payable { - unchecked { - for (uint256 i; i != ASCII_MAP.length; i += 2) { - bytes1 r = ASCII_MAP[i + 1]; - for (uint8 j; j != uint8(ASCII_MAP[i]); ++j) { - _idnamap.push(r); - } - } - } + NAMES = address(new Names()); } /// ====================== COMMAND PREVIEW ====================== /// @@ -468,46 +439,7 @@ contract IE { virtual returns (address owner, address receiver, bytes32 node) { - node = _namehash(string(abi.encodePacked(name, ".eth"))); - owner = ENS_REGISTRY.owner(node); - if (IENSHelper(owner) == ENS_WRAPPER) owner = ENS_WRAPPER.ownerOf(uint256(node)); - receiver = IENSHelper(ENS_REGISTRY.resolver(node)).addr(node); // Fails on misname. - } - - /// @dev Computes an ENS domain namehash. - function _namehash(string memory domain) internal view virtual returns (bytes32 node) { - // Process labels (in reverse order for namehash). - uint256 i = bytes(domain).length; - uint256 lastDot = i; - unchecked { - for (; i != 0; --i) { - bytes1 c = bytes(domain)[i - 1]; - if (c == ".") { - node = keccak256(abi.encodePacked(node, _labelhash(domain, i, lastDot))); - lastDot = i - 1; - continue; - } - require(c < 0x80); - bytes1 r = _idnamap[uint8(c)]; - require(uint8(r) != uint8(Rule.DISALLOWED)); - if (uint8(r) > 1) { - bytes(domain)[i - 1] = r; - } - } - } - return keccak256(abi.encodePacked(node, _labelhash(domain, i, lastDot))); - } - - /// @dev Computes an ENS domain labelhash given its start and end. - function _labelhash(string memory domain, uint256 start, uint256 end) - internal - pure - virtual - returns (bytes32 hash) - { - assembly ("memory-safe") { - hash := keccak256(add(add(domain, 0x20), start), sub(end, start)) - } + (owner, receiver, node) = INames(NAMES).whatIsTheAddressOf(name); } /// ========================= GOVERNANCE ========================= /// @@ -666,14 +598,6 @@ contract IE { } } -/// @dev ENS name resolution helper contracts interface. -interface IENSHelper { - function addr(bytes32) external view returns (address); - function owner(bytes32) external view returns (address); - function ownerOf(uint256) external view returns (address); - function resolver(bytes32) external view returns (address); -} - /// @dev Simple token transfer interface. interface IToken { function transfer(address, uint256) external returns (bool); @@ -684,6 +608,14 @@ interface IExecutor { function execute(address, uint256, bytes calldata) external payable returns (bytes memory); } +/// @dev Simple Names interface for L2 ENS ownership. +interface INames { + function whatIsTheAddressOf(string calldata) + external + view + returns (address, address, bytes32); +} + /// @dev Simple Uniswap V3 swapping interface. interface ISwapRouter { function swap(address, bool, int256, uint160, bytes calldata) diff --git a/src/Names.sol b/src/Names.sol new file mode 100644 index 0000000..1393713 --- /dev/null +++ b/src/Names.sol @@ -0,0 +1,244 @@ +// ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ +// SPDX-License-Identifier: AGPL-3.0-only +pragma solidity ^0.8.19; + +/// @title Names +/// @notice A contract for managing ENS domain name ownership and resolution on Arbitrum L2. +/// @dev Provides functions for registering names, verifying ownership, and resolving addresses. +/// @author nani.eth (https://github.com/NaniDAO/ie) +/// @custom:version 1.0.0 +contract Names { + /// ======================= CUSTOM ERRORS ======================= /// + + /// @dev Caller fail. + error Unauthorized(); + + /// =========================== EVENTS =========================== /// + + /// @dev Logs the registration of a name to an owner. + event Registered(address indexed owner, bytes32 indexed node); + + /// =========================== ENUMS =========================== /// + + /// @dev `ENSAsciiNormalizer` rules. + enum Rule { + DISALLOWED, + VALID + } + + /// ========================= CONSTANTS ========================= /// + + /// @dev L1_DEPLOYER represents the address responsible for deploying ENS proxy contracts on Ethereum Layer 1. + /// This address is typically used in conjunction with create2 operations for deterministic deployment. + address internal constant L1_DEPLOYER = 0x000000008B009D81C933a72545Ed7500cbB5B9D1; + + /// @dev L2_DEPLOYER denotes the address that performs bridged ENS deployments on Arbitrum Layer 2. + /// Similar to L1_DEPLOYER, it's crucial for deterministic deployment but within the Arbitrum L2 context. + address internal constant L2_DEPLOYER = 0x3fE38087A94903A9D946fa1915e1772fe611000f; + + /// @dev IMPLEMENTATION refers to the address of the contract serving as the implementation for ENS proxies. + address internal constant IMPLEMENTATION = 0xEBb49317E567a40cF468c409409aD59a8f67ddE6; + + /// @dev COUNTERPART_GATEWAY is the address of the gateway contract on Arbitrum Layer 2 that pairs with a corresponding + /// gateway on Ethereum Layer 1. This gateway facilitates the bridging of tokens between L1 and L2, managing the + /// lock/mint and burn/release mechanics across layers. + address internal constant COUNTERPART_GATEWAY = 0x09e9222E96E7B4AE2a407B98d48e330053351EEe; + + /// @dev L2_HASH is a unique identifier, used as part of the create2 address computation for contracts on Arbitrum Layer 2. + bytes32 internal constant L2_HASH = + 0x4b11cb57b978697e0aec0c18581326376d6463fd3f6699cbe78ee5935617082d; + + /// @dev String mapping for `ENSAsciiNormalizer` logic. + bytes internal constant ASCII_MAP = + hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; + + /// ========================== STORAGE ========================== /// + + /// @dev Each index in idnamap refers to an ascii code point. + /// If idnamap[char] > 2, char maps to a valid ascii character. + /// Otherwise, idna[char] returns Rule.DISALLOWED or Rule.VALID. + /// Modified from `ENSAsciiNormalizer` deployed by royalfork.eth + /// (0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758). + bytes1[] internal _idnamap; + + /// @dev Internal mapping of registered name owners. + mapping(bytes32 => address) internal _owners; + + /// ======================== CONSTRUCTOR ======================== /// + + /// @dev Constructs this IE with `ASCII_MAP`. + constructor() payable { + unchecked { + for (uint256 i; i != ASCII_MAP.length; i += 2) { + bytes1 r = ASCII_MAP[i + 1]; + for (uint8 j; j != uint8(ASCII_MAP[i]); ++j) { + _idnamap.push(r); + } + } + } + } + + /// ====================== ENS VERIFICATION ====================== /// + + /// @dev Returns ENS name ownership details. + function whatIsTheAddressOf(string calldata name) + public + view + virtual + returns (address _owner, address _receiver, bytes32 _node) + { + _node = _namehash(string(abi.encodePacked(name, ".eth"))); + _owner = owner(_node); + _receiver = _owner; + } + + /// @dev Computes an ENS domain namehash. + function _namehash(string memory domain) internal view virtual returns (bytes32 node) { + uint256 i = bytes(domain).length; + uint256 lastDot = i; + unchecked { + for (; i != 0; --i) { + bytes1 c = bytes(domain)[i - 1]; + if (c == ".") { + node = keccak256(abi.encodePacked(node, _labelhash(domain, i, lastDot))); + lastDot = i - 1; + continue; + } + require(c < 0x80); + bytes1 r = _idnamap[uint8(c)]; + require(uint8(r) != uint8(Rule.DISALLOWED)); + if (uint8(r) > 1) { + bytes(domain)[i - 1] = r; + } + } + } + return keccak256(abi.encodePacked(node, _labelhash(domain, i, lastDot))); + } + + /// @dev Computes an ENS domain labelhash given its start and end. + function _labelhash(string memory domain, uint256 start, uint256 end) + internal + pure + virtual + returns (bytes32 hash) + { + assembly ("memory-safe") { + hash := keccak256(add(add(domain, 0x20), start), sub(end, start)) + } + } + + /// ========================= REGISTRATION ========================= /// + + /// @dev Registers a new name under an owner. + function register(address _owner, bytes32 _node) public payable virtual { + if (!isOwner(_owner, _node)) revert Unauthorized(); + emit Registered(_owners[_node] = _owner, _node); + } + + /// ====================== OWNERSHIP LOGIC ====================== /// + + /// @dev Checks if an address is the owner of a given node. + function owner(bytes32 _node) public view virtual returns (address) { + address _owner = _owners[_node]; + if (!isOwner(_owner, _node)) revert Unauthorized(); + return _owner; + } + + /// @dev Checks if an address is the owner of a given node. + function isOwner(address _owner, bytes32 _node) public view virtual returns (bool result) { + (, address token) = predictDeterministicAddresses(_node); + uint256 bal; + uint256 supply; + assembly ("memory-safe") { + mstore(0x14, _owner) // Store the `_owner` argument. + mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`. + bal := mload(staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)) + mstore(0x00, 0x18160ddd) // `totalSupply()`. + supply := mload(staticcall(gas(), token, 0x1c, 0x04, 0x20, 0x20)) + result := gt(bal, div(supply, 2)) + } + } + + /// @dev Returns the deterministic addresses for ENS proxy tokens on L1 & L2. + function predictDeterministicAddresses(bytes32 _node) + public + pure + virtual + returns (address l1, address l2) + { + l1 = _predictDeterministicAddress(_initCodeHash(bytes.concat(_node)), _node); + l2 = _calculateL2TokenAddress(l1); + } + + /// @dev Returns the predicted address on L1 using CWIA pattern. + function _predictDeterministicAddress(bytes32 hash, bytes32 salt) + internal + pure + virtual + returns (address predicted) + { + assembly ("memory-safe") { + // Compute and store the bytecode hash. + mstore8(0x00, 0xff) // Write the prefix. + mstore(0x35, hash) + mstore(0x01, shl(96, L1_DEPLOYER)) + mstore(0x15, salt) + predicted := keccak256(0x00, 0x55) + mstore(0x35, 0) // Restore the overwritten part of the free memory pointer. + } + } + + /// @dev Returns the initCodeHash for the predicted address on L1 using CWIA pattern. + function _initCodeHash(bytes memory data) internal pure virtual returns (bytes32 hash) { + assembly ("memory-safe") { + let mBefore3 := mload(sub(data, 0x60)) + let mBefore2 := mload(sub(data, 0x40)) + let mBefore1 := mload(sub(data, 0x20)) + let dataLength := mload(data) + let dataEnd := add(add(data, 0x20), dataLength) + let mAfter1 := mload(dataEnd) + returndatacopy(returndatasize(), returndatasize(), gt(dataLength, 0xff9b)) + let extraLength := add(dataLength, 2) + mstore(data, 0x5af43d3d93803e606057fd5bf3) + mstore(sub(data, 0x0d), IMPLEMENTATION) + mstore( + sub(data, 0x21), + or(shl(0x48, extraLength), 0x593da1005b363d3d373d3d3d3d610000806062363936013d73) + ) + mstore( + sub(data, 0x3a), 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff + ) + mstore( + sub(data, 0x5a), + or(shl(0x78, add(extraLength, 0x62)), 0x6100003d81600a3d39f336602c57343d527f) + ) + mstore(dataEnd, shl(0xf0, extraLength)) + hash := keccak256(sub(data, 0x4c), add(extraLength, 0x6c)) + mstore(dataEnd, mAfter1) + mstore(data, dataLength) + mstore(sub(data, 0x20), mBefore1) + mstore(sub(data, 0x40), mBefore2) + mstore(sub(data, 0x60), mBefore3) + } + } + + /// @dev Returns the predicted L2 token address using Arbitrum create2 methods. + function _calculateL2TokenAddress(address l1ERC20) internal pure virtual returns (address) { + return address( + uint160( + uint256( + keccak256( + abi.encodePacked( + bytes1(0xff), + L2_DEPLOYER, + keccak256( + abi.encode(COUNTERPART_GATEWAY, keccak256(abi.encode(l1ERC20))) + ), + L2_HASH + ) + ) + ) + ) + ); + } +} diff --git a/test/IE.t.sol b/test/IE.t.sol index 3934959..2a548a9 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -1,36 +1,38 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.19; -import {IENSHelper, IE} from "../src/IE.sol"; +import {IE} from "../src/IE.sol"; +import {Names} from "../src/Names.sol"; import {Test} from "../lib/forge-std/src/Test.sol"; contract IETest is Test { address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; - address internal constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F; - address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; - address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; - address internal constant SUSHI = 0x6B3595068778DD592e39A122f4f5a5cF09C90fE2; - address internal constant UNI = 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984; - address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7; - address internal constant OMG = 0xd26114cd6EE289AccF82350c8d8487fedB8A0C07; - address internal constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599; + address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; + address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f; + address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; + address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; + address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; address internal constant VITALIK_DOT_ETH = 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045; address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20; address internal constant NANI_DOT_ETH = 0x7AF890Ca7262D6accdA5c9D24AC42e35Bb293188; - address internal constant USDC_WHALE = 0xD6153F5af5679a75cC85D8974463545181f48772; - address internal constant DAI_WHALE = 0x075e72a5eDf65F0A5f44699c7654C1a76941Ddc8; + bytes32 constant Z0R0Z_NODE = 0xa5b4d411903b3ea236b2defe1f96e5a68505e58362e3d8d323fde0b6f8be8ad5; + + address internal constant USDC_WHALE = 0x1F7bc4dA1a0c2e49d7eF542F74CD46a3FE592cb1; + address internal constant DAI_WHALE = 0x2d070ed1321871841245D8EE5B84bD2712644322; bytes internal constant ASCII_MAP = hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; IE internal ie; // Intents Engine. + Names internal names; + function setUp() public payable { - vm.createSelectFork(vm.rpcUrl("main")); // Ethereum mainnet fork. + vm.createSelectFork(vm.rpcUrl("arbi")); // Arbitrum fork. ie = new IE(); vm.prank(DAO); ie.setName(ETH, "ETH"); @@ -49,15 +51,12 @@ contract IETest is Test { vm.prank(DAO); ie.setName(WETH, "wrapped ether"); vm.prank(DAO); - ie.setName(SUSHI, "SUSHI"); - vm.prank(DAO); - ie.setName(UNI, "UNI"); - vm.prank(DAO); ie.setName(USDT, "USDT"); vm.prank(DAO); ie.setName(USDT, "tether"); - vm.prank(DAO); - ie.setName(OMG, "omg"); + vm.prank(Z0R0Z_DOT_ETH); + address _names = ie.NAMES(); + Names(_names).register(Z0R0Z_DOT_ETH, Z0R0Z_NODE); } function testDeploy() public payable { @@ -65,25 +64,21 @@ contract IETest is Test { } function testENSNameOwnership() public payable { - (, address receiver,) = ie.whatIsTheAddressOf("vitalik"); - assertEq(receiver, VITALIK_DOT_ETH); - (, receiver,) = ie.whatIsTheAddressOf("z0r0z"); + (, address receiver,) = ie.whatIsTheAddressOf("z0r0z"); assertEq(receiver, Z0R0Z_DOT_ETH); - (, receiver,) = ie.whatIsTheAddressOf("nani"); - assertEq(receiver, NANI_DOT_ETH); } function testPreviewSendCommand() public payable { - string memory command = "send vitalik 20 dai"; + string memory command = "send z0r0z 20 dai"; (address to, uint256 amount, address asset,,) = ie.previewCommand(command); - assertEq(to, VITALIK_DOT_ETH); + assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20 ether); assertEq(asset, DAI); } function testPreviewSend() public payable { - (address to, uint256 amount, address asset,,) = ie.previewSend("vitalik", "20", "dai"); - assertEq(to, VITALIK_DOT_ETH); + (address to, uint256 amount, address asset,,) = ie.previewSend("z0r0z", "20", "dai"); + assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20 ether); assertEq(asset, DAI); } @@ -97,85 +92,39 @@ contract IETest is Test { } function testPreviewCommandSendDecimals() public payable { - string memory command = "send vitalik 20.2 dai"; + string memory command = "send z0r0z 20.2 dai"; (address to, uint256 amount, address asset,,) = ie.previewCommand(command); - assertEq(to, VITALIK_DOT_ETH); + assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20.2 ether); assertEq(asset, DAI); - command = "send vitalik 20.23345 eth"; + command = "send z0r0z 20.23345 eth"; (to, amount, asset,,) = ie.previewCommand(command); - assertEq(to, VITALIK_DOT_ETH); + assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20.23345 ether); assertEq(asset, ETH); } function testIENameSetting() public payable { - assertEq(ie.tokens("uni"), UNI); - } - - function testTotalSupply() public payable { - (uint256 supply, uint256 adjustedSupply) = ie.whatIsTheTotalSupplyOf("uni"); - assertEq(supply, 1000000000000000000000000000); - assertEq(adjustedSupply, 1000000000); + assertEq(ie.tokens("usdc"), USDC); } function testBalanceInERC20() public payable { - uint256 vBal = IERC20(OMG).balanceOf(VITALIK_DOT_ETH); - (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("VITALIK", "omg"); + uint256 vBal = IERC20(USDT).balanceOf(Z0R0Z_DOT_ETH); + (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("Z0R0Z", "usdt"); assertEq(balance, vBal); assertEq(adjustedBalance, vBal / 10 ** 18); } function testBalanceInETH() public payable { - uint256 vBal = VITALIK_DOT_ETH.balance; - (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("VITALIK", "eth"); + uint256 vBal = Z0R0Z_DOT_ETH.balance; + (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("Z0R0Z", "eth"); assertEq(balance, vBal); assertEq(adjustedBalance, vBal / 10 ** 18); } - function testCommandSendETH() public payable { - uint256 vBal = VITALIK_DOT_ETH.balance; - uint256 zBal = Z0R0Z_DOT_ETH.balance; - vm.prank(VITALIK_DOT_ETH); - ie.command{value: 1 ether}("send z0r0z 1 ETH"); - assertEq(VITALIK_DOT_ETH.balance, vBal - 1 ether); - assertEq(Z0R0Z_DOT_ETH.balance, zBal + 1 ether); - } - - function testCommandSendERC0() public payable { - vm.prank(VITALIK_DOT_ETH); - IERC20(OMG).approve(address(ie), 100 ether); - uint256 vBal = IERC20(OMG).balanceOf(VITALIK_DOT_ETH); - uint256 zBal = IERC20(OMG).balanceOf(Z0R0Z_DOT_ETH); - vm.prank(VITALIK_DOT_ETH); - ie.command("send z0r0z 100 OMG"); - assertEq(IERC20(OMG).balanceOf(VITALIK_DOT_ETH), vBal - 100 ether); - assertEq(IERC20(OMG).balanceOf(Z0R0Z_DOT_ETH), zBal + 100 ether); - } - - function testCommandSendUSDC() public payable { - vm.prank(USDC_WHALE); - IERC20(USDC).approve(address(ie), 100 ether); - uint256 wBal = IERC20(USDC).balanceOf(USDC_WHALE); - uint256 zBal = IERC20(USDC).balanceOf(Z0R0Z_DOT_ETH); - vm.prank(USDC_WHALE); - ie.command("send z0r0z 100 USDC"); - assertEq(IERC20(USDC).balanceOf(USDC_WHALE), wBal - 100000000); - assertEq(IERC20(USDC).balanceOf(Z0R0Z_DOT_ETH), zBal + 100000000); - } - - function testSendETH() public payable { - uint256 vBal = VITALIK_DOT_ETH.balance; - uint256 zBal = Z0R0Z_DOT_ETH.balance; - vm.prank(VITALIK_DOT_ETH); - ie.send{value: 1 ether}("z0r0z", "1", "eth"); - assertEq(VITALIK_DOT_ETH.balance, vBal - 1 ether); - assertEq(Z0R0Z_DOT_ETH.balance, zBal + 1 ether); - } - function testCommandSwapETH() public payable { vm.prank(VITALIK_DOT_ETH); - ie.command{value: 10 ether}("swap 10 eth for dai"); + ie.command{value: 1 ether}("swap 1 eth for dai"); } function testCommandSwapForETH() public payable { From 00e3b77e43a1fc9075f7bfc68a525d0d8ff322bb Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:30:48 +0000 Subject: [PATCH 02/22] =?UTF-8?q?=F0=9F=A5=A2=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 2 +- src/Names.sol | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 914c9b1..2e957dc 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -3,7 +3,7 @@ IETest:testBalanceInETH() (gas: 50934) IETest:testCommandSwapDAI() (gas: 133298) IETest:testCommandSwapETH() (gas: 158848) IETest:testCommandSwapForETH() (gas: 140019) -IETest:testCommandSwapUSDC() (gas: 162390) +IETest:testCommandSwapUSDC() (gas: 163400) IETest:testCommandSwapUSDCForWBTC() (gas: 180085) IETest:testDeploy() (gas: 3011723) IETest:testENSNameOwnership() (gas: 44458) diff --git a/src/Names.sol b/src/Names.sol index 1393713..3435c8a 100644 --- a/src/Names.sol +++ b/src/Names.sol @@ -127,7 +127,7 @@ contract Names { } } - /// ========================= REGISTRATION ========================= /// + /// ======================== REGISTRATION ======================== /// /// @dev Registers a new name under an owner. function register(address _owner, bytes32 _node) public payable virtual { From f0bae91f1c98f3d2fb4ecae353654845ca05ba1e Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Tue, 20 Feb 2024 19:48:21 +0000 Subject: [PATCH 03/22] ~~ --- .gas-snapshot | 14 ----- src/IE.sol | 132 ++++++++++++++++++++++++++------------- test/IE.t.sol | 169 -------------------------------------------------- 3 files changed, 89 insertions(+), 226 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 2e957dc..e69de29 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,14 +0,0 @@ -IETest:testBalanceInERC20() (gas: 66269) -IETest:testBalanceInETH() (gas: 50934) -IETest:testCommandSwapDAI() (gas: 133298) -IETest:testCommandSwapETH() (gas: 158848) -IETest:testCommandSwapForETH() (gas: 140019) -IETest:testCommandSwapUSDC() (gas: 163400) -IETest:testCommandSwapUSDCForWBTC() (gas: 180085) -IETest:testDeploy() (gas: 3011723) -IETest:testENSNameOwnership() (gas: 44458) -IETest:testIENameSetting() (gas: 8186) -IETest:testPreviewCommandSendDecimals() (gas: 100697) -IETest:testPreviewCommandSendUSDC() (gas: 74194) -IETest:testPreviewSend() (gas: 53578) -IETest:testPreviewSendCommand() (gas: 64400) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index f9be183..0d00b67 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -1,18 +1,16 @@ // ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ // SPDX-License-Identifier: AGPL-3.0-only -pragma solidity ^0.8.19; +pragma solidity ^0.8.24; import {SafeTransferLib} from "../lib/solady/src/utils/SafeTransferLib.sol"; import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol"; -import "./Names.sol"; - /// @title Intents Engine (IE) /// @notice Simple helper contract for turning transactional intents into executable code. /// @dev V1 simulates typical commands (sending and swapping tokens) and includes execution. /// IE also has a workflow to verify the intent of ERC4337 account userOps against calldata. /// @author nani.eth (https://github.com/NaniDAO/ie) -/// @custom:version 1.0.0 +/// @custom:version 1.1.0 contract IE { /// ======================= LIBRARY USAGE ======================= /// @@ -39,6 +37,9 @@ contract IE { /// @dev Non-numeric character. error InvalidCharacter(); + /// @dev Insufficient swap output. + error InsufficientSwap(); + /// =========================== EVENTS =========================== /// /// @dev Logs the registration of a token name. @@ -74,6 +75,15 @@ contract IE { bytes signature; } + /// @dev The `swap` command details. + struct SwapDetails { + address tokenIn; + address tokenOut; + uint256 amountIn; + bool ETHIn; + bool ETHOut; + } + /// ========================= CONSTANTS ========================= /// /// @dev The governing DAO address. @@ -83,7 +93,7 @@ contract IE { address internal constant NANI = 0x00000000000025824328358250920B271f348690; /// @dev The NANI naming system on Arbitrum. - address public immutable NAMES; + address internal constant NAMES = 0x871E6ba1a81DD52C7eeeBD6f37c5CeFE11207b90; /// @dev The conventional ERC7528 ETH address. address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; @@ -103,6 +113,9 @@ contract IE { /// @dev The Maker DAO USD stablecoin address. address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; + /// @dev The Arbitrum DAO governance token address. + address internal constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548; + /// @dev The address of the Uniswap V3 Factory. address internal constant UNISWAP_V3_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984; @@ -125,9 +138,7 @@ contract IE { /// ======================== CONSTRUCTOR ======================== /// /// @dev Constructs this IE on the Arbitrum L2 of Ethereum. - constructor() payable { - NAMES = address(new Names()); - } + constructor() payable {} /// ====================== COMMAND PREVIEW ====================== /// @@ -141,6 +152,7 @@ contract IE { returns ( address to, // Receiver address. uint256 amount, // Formatted amount. + uint256 minAmountOut, // Formatted amount. address token, // Asset to send `to`. bytes memory callData, // Raw calldata for send transaction. bytes memory executeCallData // Anticipates common execute API. @@ -153,9 +165,14 @@ contract IE { _extractSend(normalized); (to, amount, token, callData, executeCallData) = previewSend(_to, _amount, _token); } else if (action == "swap" || action == "exchange") { - (string memory amountIn, string memory tokenIn, string memory tokenOut) = - _extractSwap(normalized); - (amount, token, to) = previewSwap(amountIn, tokenIn, tokenOut); + ( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ) = _extractSwap(normalized); + (amount, minAmountOut, token, to) = + previewSwap(amountIn, amountOutMinimum, tokenIn, tokenOut); } else { revert InvalidSyntax(); // Invalid command format. } @@ -185,17 +202,24 @@ contract IE { } /// @dev Previews a `swap` command from the parts of a matched intent string. - function previewSwap(string memory amountIn, string memory tokenIn, string memory tokenOut) + function previewSwap( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ) public view virtual - returns (uint256 _amountIn, address _tokenIn, address _tokenOut) + returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut) { _tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn]; _tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); if (_tokenOut == address(0)) _tokenOut = tokens[tokenOut]; _amountIn = _stringToUint(amountIn, _tokenIn == ETH ? 18 : _tokenIn.readDecimals()); + _amountOut = + _stringToUint(amountOutMinimum, _tokenOut == ETH ? 18 : _tokenOut.readDecimals()); } /// @dev Checks ERC4337 userOp against the output of the command intent. @@ -205,7 +229,7 @@ contract IE { virtual returns (bool) { - (,,,, bytes memory executeCallData) = previewCommand(intent); + (,,,,, bytes memory executeCallData) = previewCommand(intent); if (executeCallData.length != userOp.callData.length) return false; return keccak256(executeCallData) == keccak256(userOp.callData); } @@ -217,7 +241,7 @@ contract IE { virtual returns (bool) { - (,,,, bytes memory executeCallData) = previewCommand(intent); + (,,,,, bytes memory executeCallData) = previewCommand(intent); if (executeCallData.length != userOp.callData.length) return false; return keccak256(executeCallData) == keccak256(userOp.callData); } @@ -228,6 +252,7 @@ contract IE { if (token == "usdc") return USDC; if (token == "usdt") return USDT; if (token == "dai") return DAI; + if (token == "arb" || token == "arbitrum") return ARB; if (token == "nani") return NANI; if (token == "weth") return WETH; if (token == "wbtc" || token == "btc" || token == "bitcoin") return WBTC; @@ -243,9 +268,13 @@ contract IE { (string memory to, string memory amount, string memory token) = _extractSend(normalized); send(to, amount, token); } else if (action == "swap" || action == "exchange") { - (string memory amountIn, string memory tokenIn, string memory tokenOut) = - _extractSwap(normalized); - swap(amountIn, tokenIn, tokenOut); + ( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ) = _extractSwap(normalized); + swap(amountIn, amountOutMinimum, tokenIn, tokenOut); } else { revert InvalidSyntax(); // Invalid command format. } @@ -268,29 +297,40 @@ contract IE { } /// @dev Executes a `swap` command from the parts of a matched intent string. - function swap(string memory amountIn, string memory tokenIn, string memory tokenOut) - public - payable - virtual - { - address _tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); - if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn]; - address _tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); - if (_tokenOut == address(0)) _tokenOut = tokens[tokenOut]; - bool ETHIn = _tokenIn == ETH; - bool ETHOut = _tokenOut == ETH; - if (ETHIn) _tokenIn = WETH; - if (ETHOut) _tokenOut = WETH; - uint256 _amountIn = _stringToUint(amountIn, ETHIn ? 18 : _tokenIn.readDecimals()); - if (_amountIn >= 1 << 255) revert Overflow(); - (address pool, bool zeroForOne) = _computePoolAddress(_tokenIn, _tokenOut); - ISwapRouter(pool).swap( - !ETHOut ? msg.sender : address(this), + function swap( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ) public payable virtual { + SwapDetails memory details; + details.tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); + if (details.tokenIn == address(0)) details.tokenIn = tokens[tokenIn]; + details.tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); + if (details.tokenOut == address(0)) details.tokenOut = tokens[tokenOut]; + + details.ETHIn = details.tokenIn == ETH; + if (details.ETHIn) details.tokenIn = WETH; + details.ETHOut = details.tokenOut == ETH; + if (details.ETHOut) details.tokenOut = WETH; + + details.amountIn = + _stringToUint(amountIn, details.ETHIn ? 18 : details.tokenIn.readDecimals()); + if (details.amountIn >= 1 << 255) revert Overflow(); + (address pool, bool zeroForOne) = _computePoolAddress(details.tokenIn, details.tokenOut); + (int256 amount0, int256 amount1) = ISwapRouter(pool).swap( + !details.ETHOut ? msg.sender : address(this), zeroForOne, - int256(_amountIn), + int256(details.amountIn), zeroForOne ? MIN_SQRT_RATIO_PLUS_ONE : MAX_SQRT_RATIO_MINUS_ONE, - abi.encodePacked(ETHIn, ETHOut, msg.sender, _tokenIn, _tokenOut) + abi.encodePacked( + details.ETHIn, details.ETHOut, msg.sender, details.tokenIn, details.tokenOut + ) ); + if ( + uint256(-(zeroForOne ? amount1 : amount0)) + < _stringToUint(amountOutMinimum, details.ETHOut ? 18 : details.tokenOut.readDecimals()) + ) revert InsufficientSwap(); } /// @dev Fallback `uniswapV3SwapCallback`. @@ -338,9 +378,9 @@ contract IE { { if (tokenA < tokenB) zeroForOne = true; else (tokenA, tokenB) = (tokenB, tokenA); - pool = _computePairHash(tokenA, tokenB, 3000); // Mid fee. + pool = _computePairHash(tokenA, tokenB, 500); // Low fee. if (pool.code.length != 0) return (pool, zeroForOne); - else pool = _computePairHash(tokenA, tokenB, 500); // Low fee. + else pool = _computePairHash(tokenA, tokenB, 3000); // Mid fee. if (pool.code.length != 0) return (pool, zeroForOne); else pool = _computePairHash(tokenA, tokenB, 100); // Lowest fee. if (pool.code.length != 0) return (pool, zeroForOne); @@ -526,10 +566,16 @@ contract IE { internal pure virtual - returns (string memory amountIn, string memory tokenIn, string memory tokenOut) + returns ( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ) { string[] memory parts = _split(normalizedIntent, " "); - if (parts.length == 5) return (parts[1], parts[2], parts[4]); + if (parts.length == 5) return (parts[1], "", parts[2], parts[4]); + if (parts.length == 6) return (parts[1], parts[4], parts[2], parts[5]); else revert InvalidSyntax(); // Command is not formatted. } @@ -608,7 +654,7 @@ interface IExecutor { function execute(address, uint256, bytes calldata) external payable returns (bytes memory); } -/// @dev Simple Names interface for L2 ENS ownership. +/// @dev Simple Names interface for resolving L2 ENS ownership. interface INames { function whatIsTheAddressOf(string calldata) external diff --git a/test/IE.t.sol b/test/IE.t.sol index 2a548a9..8b13789 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -1,170 +1 @@ -// SPDX-License-Identifier: AGPL-3.0-only -pragma solidity ^0.8.19; -import {IE} from "../src/IE.sol"; -import {Names} from "../src/Names.sol"; -import {Test} from "../lib/forge-std/src/Test.sol"; - -contract IETest is Test { - address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; - - address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; - address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; - address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f; - address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; - address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; - address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; - - address internal constant VITALIK_DOT_ETH = 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045; - address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20; - address internal constant NANI_DOT_ETH = 0x7AF890Ca7262D6accdA5c9D24AC42e35Bb293188; - - bytes32 constant Z0R0Z_NODE = 0xa5b4d411903b3ea236b2defe1f96e5a68505e58362e3d8d323fde0b6f8be8ad5; - - address internal constant USDC_WHALE = 0x1F7bc4dA1a0c2e49d7eF542F74CD46a3FE592cb1; - address internal constant DAI_WHALE = 0x2d070ed1321871841245D8EE5B84bD2712644322; - - bytes internal constant ASCII_MAP = - hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; - - IE internal ie; // Intents Engine. - - Names internal names; - - function setUp() public payable { - vm.createSelectFork(vm.rpcUrl("arbi")); // Arbitrum fork. - ie = new IE(); - vm.prank(DAO); - ie.setName(ETH, "ETH"); - vm.prank(DAO); - ie.setName(ETH, "ether"); - vm.prank(DAO); - ie.setName(ETH, "ethereum"); - vm.prank(DAO); - ie.setName(DAI, "DAI"); - vm.prank(DAO); - ie.setName(USDC, "USDC"); - vm.prank(DAO); - ie.setName(WETH, "WETH"); - vm.prank(DAO); - ie.setName(WETH, "wrapped eth"); - vm.prank(DAO); - ie.setName(WETH, "wrapped ether"); - vm.prank(DAO); - ie.setName(USDT, "USDT"); - vm.prank(DAO); - ie.setName(USDT, "tether"); - vm.prank(Z0R0Z_DOT_ETH); - address _names = ie.NAMES(); - Names(_names).register(Z0R0Z_DOT_ETH, Z0R0Z_NODE); - } - - function testDeploy() public payable { - new IE(); - } - - function testENSNameOwnership() public payable { - (, address receiver,) = ie.whatIsTheAddressOf("z0r0z"); - assertEq(receiver, Z0R0Z_DOT_ETH); - } - - function testPreviewSendCommand() public payable { - string memory command = "send z0r0z 20 dai"; - (address to, uint256 amount, address asset,,) = ie.previewCommand(command); - assertEq(to, Z0R0Z_DOT_ETH); - assertEq(amount, 20 ether); - assertEq(asset, DAI); - } - - function testPreviewSend() public payable { - (address to, uint256 amount, address asset,,) = ie.previewSend("z0r0z", "20", "dai"); - assertEq(to, Z0R0Z_DOT_ETH); - assertEq(amount, 20 ether); - assertEq(asset, DAI); - } - - function testPreviewCommandSendUSDC() public payable { - string memory command = "send z0r0z 20 usdc"; - (address to, uint256 amount, address asset,,) = ie.previewCommand(command); - assertEq(to, Z0R0Z_DOT_ETH); - assertEq(amount, 20000000); - assertEq(asset, USDC); - } - - function testPreviewCommandSendDecimals() public payable { - string memory command = "send z0r0z 20.2 dai"; - (address to, uint256 amount, address asset,,) = ie.previewCommand(command); - assertEq(to, Z0R0Z_DOT_ETH); - assertEq(amount, 20.2 ether); - assertEq(asset, DAI); - command = "send z0r0z 20.23345 eth"; - (to, amount, asset,,) = ie.previewCommand(command); - assertEq(to, Z0R0Z_DOT_ETH); - assertEq(amount, 20.23345 ether); - assertEq(asset, ETH); - } - - function testIENameSetting() public payable { - assertEq(ie.tokens("usdc"), USDC); - } - - function testBalanceInERC20() public payable { - uint256 vBal = IERC20(USDT).balanceOf(Z0R0Z_DOT_ETH); - (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("Z0R0Z", "usdt"); - assertEq(balance, vBal); - assertEq(adjustedBalance, vBal / 10 ** 18); - } - - function testBalanceInETH() public payable { - uint256 vBal = Z0R0Z_DOT_ETH.balance; - (uint256 balance, uint256 adjustedBalance) = ie.whatIsTheBalanceOf("Z0R0Z", "eth"); - assertEq(balance, vBal); - assertEq(adjustedBalance, vBal / 10 ** 18); - } - - function testCommandSwapETH() public payable { - vm.prank(VITALIK_DOT_ETH); - ie.command{value: 1 ether}("swap 1 eth for dai"); - } - - function testCommandSwapForETH() public payable { - uint256 startBalETH = DAI_WHALE.balance; - uint256 startBalDAI = IERC20(DAI).balanceOf(DAI_WHALE); - vm.prank(DAI_WHALE); - IERC20(DAI).approve(address(ie), 100 ether); - vm.prank(DAI_WHALE); - ie.command("swap 100 dai for eth"); - assert(startBalETH < DAI_WHALE.balance); - assertEq(startBalDAI - 100 ether, IERC20(DAI).balanceOf(DAI_WHALE)); - } - - function testCommandSwapDAI() public payable { - vm.prank(DAI_WHALE); - IERC20(DAI).approve(address(ie), 100 ether); - vm.prank(DAI_WHALE); - ie.command("swap 100 dai for weth"); - } - - function testCommandSwapUSDC() public payable { - vm.prank(USDC_WHALE); - IERC20(USDC).approve(address(ie), 100 ether); - vm.prank(USDC_WHALE); - ie.command("swap 100 usdc for weth"); - } - - function testCommandSwapUSDCForWBTC() public payable { - uint256 startBalUSDC = IERC20(USDC).balanceOf(USDC_WHALE); - uint256 startBalWBTC = IERC20(WBTC).balanceOf(USDC_WHALE); - vm.prank(USDC_WHALE); - IERC20(USDC).approve(address(ie), 100 ether); - vm.prank(USDC_WHALE); - ie.command("swap 100 usdc for wbtc"); - assert(startBalWBTC < IERC20(WBTC).balanceOf(USDC_WHALE)); - assertEq(startBalUSDC - 100 * 10 ** 6, IERC20(USDC).balanceOf(USDC_WHALE)); - } -} - -interface IERC20 { - function approve(address, uint256) external; // unsafe lol. - function balanceOf(address) external view returns (uint256); -} From b7a59157361965604e68d9057012cd48b2494864 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 12:27:03 +0000 Subject: [PATCH 04/22] =?UTF-8?q?=E2=9A=A1=20NAMI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 15 ++++ src/IE.sol | 162 ++++++++++++++++++++++++++---------- src/{Names.sol => NAMI.sol} | 116 ++++++++++++++++++-------- test/IE.t.sol | 155 ++++++++++++++++++++++++++++++++++ test/NAMI.t.sol | 33 ++++++++ 5 files changed, 403 insertions(+), 78 deletions(-) rename src/{Names.sol => NAMI.sol} (70%) create mode 100644 test/NAMI.t.sol diff --git a/.gas-snapshot b/.gas-snapshot index e69de29..96e7ed1 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -0,0 +1,15 @@ +IETest:testCommandSendETH() (gas: 71778) +IETest:testCommandSwapDAI() (gas: 169019) +IETest:testCommandSwapETH() (gas: 195733) +IETest:testCommandSwapForETH() (gas: 172378) +IETest:testCommandSwapUSDC() (gas: 203883) +IETest:testCommandSwapUSDCForWBTC() (gas: 204423) +IETest:testDeploy() (gas: 2775620) +IETest:testENSNameOwnership() (gas: 44476) +IETest:testIENameSetting() (gas: 8208) +IETest:testPreviewCommandSendDecimals() (gas: 100925) +IETest:testPreviewCommandSendUSDC() (gas: 74300) +IETest:testPreviewSend() (gas: 53627) +IETest:testPreviewSendCommand() (gas: 64537) +NAMITest:testFailRegister() (gas: 9392) +NAMITest:testRegister() (gas: 55668) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 0d00b67..79cd008 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -1,6 +1,6 @@ // ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ // SPDX-License-Identifier: AGPL-3.0-only -pragma solidity ^0.8.24; +pragma solidity ^0.8.19; import {SafeTransferLib} from "../lib/solady/src/utils/SafeTransferLib.sol"; import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol"; @@ -10,7 +10,7 @@ import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol"; /// @dev V1 simulates typical commands (sending and swapping tokens) and includes execution. /// IE also has a workflow to verify the intent of ERC4337 account userOps against calldata. /// @author nani.eth (https://github.com/NaniDAO/ie) -/// @custom:version 1.1.0 +/// @custom:version 1.1.1 contract IE { /// ======================= LIBRARY USAGE ======================= /// @@ -62,7 +62,7 @@ contract IE { bytes signature; } - /// @dev The packed ERC4337 user operation (userOp) struct. + /// @dev The packed ERC4337 userOp struct. struct PackedUserOperation { address sender; uint256 nonce; @@ -75,8 +75,8 @@ contract IE { bytes signature; } - /// @dev The `swap` command details. - struct SwapDetails { + /// @dev The `swap` command information struct. + struct SwapInfo { address tokenIn; address tokenOut; uint256 amountIn; @@ -84,6 +84,12 @@ contract IE { bool ETHOut; } + /// @dev The `swap` pool liquidity struct. + struct SwapLiq { + address pool; + uint96 liq; + } + /// ========================= CONSTANTS ========================= /// /// @dev The governing DAO address. @@ -92,8 +98,8 @@ contract IE { /// @dev The NANI token address. address internal constant NANI = 0x00000000000025824328358250920B271f348690; - /// @dev The NANI naming system on Arbitrum. - address internal constant NAMES = 0x871E6ba1a81DD52C7eeeBD6f37c5CeFE11207b90; + /// @dev The NAMI naming system on Arbitrum. + address internal constant NAMI = 0x871E6ba1a81DD52C7eeeBD6f37c5CeFE11207b90; /// @dev The conventional ERC7528 ETH address. address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; @@ -116,6 +122,12 @@ contract IE { /// @dev The Arbitrum DAO governance token address. address internal constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548; + /// @dev The Lido Wrapped Staked ETH token address. + address internal constant WSTETH = 0x5979D7b546E38E414F7E9822514be443A4800529; + + /// @dev The Rocket Pool Staked ETH token address. + address internal constant RETH = 0xEC70Dcb4A1EFa46b8F2D97C310C9c4790ba5ffA8; + /// @dev The address of the Uniswap V3 Factory. address internal constant UNISWAP_V3_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984; @@ -160,11 +172,14 @@ contract IE { { string memory normalized = _lowercase(intent); bytes32 action = _extraction(normalized); - if (action == "send" || action == "transfer") { + if (action == "send" || action == "transfer" || action == "pay" || action == "grant") { (string memory _to, string memory _amount, string memory _token) = _extractSend(normalized); (to, amount, token, callData, executeCallData) = previewSend(_to, _amount, _token); - } else if (action == "swap" || action == "exchange") { + } else if ( + action == "swap" || action == "exchange" || action == "stake" || action == "deposit" + || action == "withdraw" + ) { ( string memory amountIn, string memory amountOutMinimum, @@ -201,6 +216,29 @@ contract IE { abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData)); } + /// @dev Previews a `send` command like `previewSend` but includes raw `to`. + function previewSend(address to, string memory amount, string memory token) + public + view + virtual + returns ( + address _to, + uint256 _amount, + address _token, + bytes memory callData, + bytes memory executeCallData + ) + { + _to = to; // Flip it back. This just helps maintain formatted output. + _token = _returnTokenConstant(bytes32(bytes(token))); // Check constant. + if (_token == address(0)) _token = tokens[token]; // Check storage. + bool isETH = _token == ETH; // Memo whether the token is ETH or not. + _amount = _stringToUint(amount, isETH ? 18 : _token.readDecimals()); + if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount)); + executeCallData = + abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData)); + } + /// @dev Previews a `swap` command from the parts of a matched intent string. function previewSwap( string memory amountIn, @@ -250,12 +288,14 @@ contract IE { function _returnTokenConstant(bytes32 token) internal view virtual returns (address _token) { if (token == "eth" || token == "ether") return ETH; if (token == "usdc") return USDC; - if (token == "usdt") return USDT; + if (token == "usdt" || token == "tether") return USDT; if (token == "dai") return DAI; if (token == "arb" || token == "arbitrum") return ARB; if (token == "nani") return NANI; if (token == "weth") return WETH; if (token == "wbtc" || token == "btc" || token == "bitcoin") return WBTC; + if (token == "wsteth" || token == "lido") return WSTETH; + if (token == "reth" || token == "rocket pool") return RETH; } /// ===================== COMMAND EXECUTION ===================== /// @@ -264,10 +304,13 @@ contract IE { function command(string calldata intent) public payable virtual { string memory normalized = _lowercase(intent); bytes32 action = _extraction(normalized); - if (action == "send" || action == "transfer") { + if (action == "send" || action == "transfer" || action == "pay" || action == "grant") { (string memory to, string memory amount, string memory token) = _extractSend(normalized); send(to, amount, token); - } else if (action == "swap" || action == "exchange") { + } else if ( + action == "swap" || action == "exchange" || action == "stake" || action == "deposit" + || action == "withdraw" + ) { ( string memory amountIn, string memory amountOutMinimum, @@ -303,33 +346,30 @@ contract IE { string memory tokenIn, string memory tokenOut ) public payable virtual { - SwapDetails memory details; - details.tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); - if (details.tokenIn == address(0)) details.tokenIn = tokens[tokenIn]; - details.tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); - if (details.tokenOut == address(0)) details.tokenOut = tokens[tokenOut]; - - details.ETHIn = details.tokenIn == ETH; - if (details.ETHIn) details.tokenIn = WETH; - details.ETHOut = details.tokenOut == ETH; - if (details.ETHOut) details.tokenOut = WETH; - - details.amountIn = - _stringToUint(amountIn, details.ETHIn ? 18 : details.tokenIn.readDecimals()); - if (details.amountIn >= 1 << 255) revert Overflow(); - (address pool, bool zeroForOne) = _computePoolAddress(details.tokenIn, details.tokenOut); + SwapInfo memory info; + info.tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); + if (info.tokenIn == address(0)) info.tokenIn = tokens[tokenIn]; + info.tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); + if (info.tokenOut == address(0)) info.tokenOut = tokens[tokenOut]; + + info.ETHIn = info.tokenIn == ETH; + if (info.ETHIn) info.tokenIn = WETH; + info.ETHOut = info.tokenOut == ETH; + if (info.ETHOut) info.tokenOut = WETH; + + info.amountIn = _stringToUint(amountIn, info.ETHIn ? 18 : info.tokenIn.readDecimals()); + if (info.amountIn >= 1 << 255) revert Overflow(); + (address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut); (int256 amount0, int256 amount1) = ISwapRouter(pool).swap( - !details.ETHOut ? msg.sender : address(this), + !info.ETHOut ? msg.sender : address(this), zeroForOne, - int256(details.amountIn), + int256(info.amountIn), zeroForOne ? MIN_SQRT_RATIO_PLUS_ONE : MAX_SQRT_RATIO_MINUS_ONE, - abi.encodePacked( - details.ETHIn, details.ETHOut, msg.sender, details.tokenIn, details.tokenOut - ) + abi.encodePacked(info.ETHIn, info.ETHOut, msg.sender, info.tokenIn, info.tokenOut) ); if ( uint256(-(zeroForOne ? amount1 : amount0)) - < _stringToUint(amountOutMinimum, details.ETHOut ? 18 : details.tokenOut.readDecimals()) + < _stringToUint(amountOutMinimum, info.ETHOut ? 18 : info.tokenOut.readDecimals()) ) revert InsufficientSwap(); } @@ -370,6 +410,7 @@ contract IE { } /// @dev Computes the create2 address for given token pair. + /// note: This process checks all available pools for price. function _computePoolAddress(address tokenA, address tokenB) internal view @@ -378,14 +419,27 @@ contract IE { { if (tokenA < tokenB) zeroForOne = true; else (tokenA, tokenB) = (tokenB, tokenA); - pool = _computePairHash(tokenA, tokenB, 500); // Low fee. - if (pool.code.length != 0) return (pool, zeroForOne); - else pool = _computePairHash(tokenA, tokenB, 3000); // Mid fee. - if (pool.code.length != 0) return (pool, zeroForOne); - else pool = _computePairHash(tokenA, tokenB, 100); // Lowest fee. - if (pool.code.length != 0) return (pool, zeroForOne); - else pool = _computePairHash(tokenA, tokenB, 10000); // Highest fee. - if (pool.code.length != 0) return (pool, zeroForOne); + address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee. + address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee. + address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. + address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. + // Initialize an array to hold the liquidity information for each pool. + SwapLiq[4] memory pools = [ + SwapLiq(pool100, uint96(pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0)), + SwapLiq(pool500, uint96(pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0)), + SwapLiq(pool3000, uint96(pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0)), + SwapLiq(pool10000, uint96(pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0)) + ]; + uint96 topLiq; + address topPool; + // Iterate through the array to find the pool with the highest liquidity. + for (uint256 i; i != 4; ++i) { + if (pools[i].liq > topLiq) { + topLiq = pools[i].liq; + topPool = pools[i].pool; + } + } + pool = topPool; // Return the pool with best liquidity. } /// @dev Computes the create2 deployment hash for given token pair. @@ -427,9 +481,27 @@ contract IE { } } + /// @dev Returns the amount of ERC20 `token` owned by `account`. + function _balanceOf(address token, address account) + internal + view + virtual + returns (uint256 amount) + { + assembly ("memory-safe") { + mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`. + mstore(0x14, account) // Store the `account` argument. + pop(staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)) + amount := mload(0x20) + } + } + /// @dev ETH receiver fallback. + /// Only canonical WETH can call. receive() external payable virtual { - if (msg.sender != WETH) revert Unauthorized(); + assembly ("memory-safe") { + if iszero(eq(caller(), WETH)) { revert(codesize(), 0x00) } + } } /// ================== BALANCE & SUPPLY HELPERS ================== /// @@ -479,7 +551,11 @@ contract IE { virtual returns (address owner, address receiver, bytes32 node) { - (owner, receiver, node) = INames(NAMES).whatIsTheAddressOf(name); + if (bytes(name).length == 20) { + receiver = address(bytes20(bytes(name))); + } else { + (owner, receiver, node) = INames(NAMI).whatIsTheAddressOf(name); + } } /// ========================= GOVERNANCE ========================= /// diff --git a/src/Names.sol b/src/NAMI.sol similarity index 70% rename from src/Names.sol rename to src/NAMI.sol index 3435c8a..39c4722 100644 --- a/src/Names.sol +++ b/src/NAMI.sol @@ -2,16 +2,16 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.19; -/// @title Names +/// @title NANI ARBITRUM MESSAGE INVENTORY (NAMI) /// @notice A contract for managing ENS domain name ownership and resolution on Arbitrum L2. /// @dev Provides functions for registering names, verifying ownership, and resolving addresses. /// @author nani.eth (https://github.com/NaniDAO/ie) /// @custom:version 1.0.0 -contract Names { +contract NAMI { /// ======================= CUSTOM ERRORS ======================= /// - /// @dev Caller fail. - error Unauthorized(); + /// @dev Unregistered. + error Unregistered(); /// =========================== EVENTS =========================== /// @@ -28,6 +28,9 @@ contract Names { /// ========================= CONSTANTS ========================= /// + /// @dev The governing DAO address. + address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; + /// @dev L1_DEPLOYER represents the address responsible for deploying ENS proxy contracts on Ethereum Layer 1. /// This address is typically used in conjunction with create2 operations for deterministic deployment. address internal constant L1_DEPLOYER = 0x000000008B009D81C933a72545Ed7500cbB5B9D1; @@ -129,45 +132,46 @@ contract Names { /// ======================== REGISTRATION ======================== /// - /// @dev Registers a new name under an owner. + /// @dev Registers a new name under an owner. ENS L1 node must be bridged. function register(address _owner, bytes32 _node) public payable virtual { - if (!isOwner(_owner, _node)) revert Unauthorized(); + if (!isOwner(_owner, _node)) revert Unregistered(); emit Registered(_owners[_node] = _owner, _node); } + /// @dev Registers a new subname under an owner. Only the DAO may call this function. + function registerSub(address _owner, string calldata _subname) public payable virtual { + assembly ("memory-safe") { + if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat. + } + bytes32 subnode = _namehash(string(abi.encodePacked(_subname, ".nani.eth"))); + emit Registered(_owners[subnode] = _owner, subnode); + } + /// ====================== OWNERSHIP LOGIC ====================== /// - /// @dev Checks if an address is the owner of a given node. - function owner(bytes32 _node) public view virtual returns (address) { - address _owner = _owners[_node]; - if (!isOwner(_owner, _node)) revert Unauthorized(); - return _owner; + /// @dev Returns the registered owner of a given ENS L1 node. Must be bridged. + /// note: Alternatively, NAMI provides subdomains issued under nani.eth node. + function owner(bytes32 _node) public view virtual returns (address _owner) { + _owner = _owners[_node]; + if (!isOwner(_owner, _node)) revert Unregistered(); } - /// @dev Checks if an address is the owner of a given node. + /// @dev Checks if an address is the owner of a given ENS L1 node represented as `l2Token`. + /// note: NAMI operates under the assumption that the proper owner-receiver holds majority. function isOwner(address _owner, bytes32 _node) public view virtual returns (bool result) { - (, address token) = predictDeterministicAddresses(_node); - uint256 bal; - uint256 supply; - assembly ("memory-safe") { - mstore(0x14, _owner) // Store the `_owner` argument. - mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`. - bal := mload(staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)) - mstore(0x00, 0x18160ddd) // `totalSupply()`. - supply := mload(staticcall(gas(), token, 0x1c, 0x04, 0x20, 0x20)) - result := gt(bal, div(supply, 2)) - } + (, address l2Token) = predictDeterministicAddresses(_node); + return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2); } - /// @dev Returns the deterministic addresses for ENS proxy tokens on L1 & L2. + /// @dev Returns the deterministic create2 addresses for ENS node tokens on L1 & L2. function predictDeterministicAddresses(bytes32 _node) public pure virtual - returns (address l1, address l2) + returns (address l1Token, address l2Token) { - l1 = _predictDeterministicAddress(_initCodeHash(bytes.concat(_node)), _node); - l2 = _calculateL2TokenAddress(l1); + l1Token = _predictDeterministicAddress(_initCodeHash(bytes.concat(_node)), _node); + l2Token = _calculateL2TokenAddress(l1Token); } /// @dev Returns the predicted address on L1 using CWIA pattern. @@ -194,11 +198,10 @@ contract Names { let mBefore3 := mload(sub(data, 0x60)) let mBefore2 := mload(sub(data, 0x40)) let mBefore1 := mload(sub(data, 0x20)) - let dataLength := mload(data) - let dataEnd := add(add(data, 0x20), dataLength) + let dataEnd := add(add(data, 0x20), 0x20) let mAfter1 := mload(dataEnd) - returndatacopy(returndatasize(), returndatasize(), gt(dataLength, 0xff9b)) - let extraLength := add(dataLength, 2) + returndatacopy(returndatasize(), returndatasize(), gt(0x20, 0xff9b)) + let extraLength := add(0x20, 2) mstore(data, 0x5af43d3d93803e606057fd5bf3) mstore(sub(data, 0x0d), IMPLEMENTATION) mstore( @@ -215,15 +218,20 @@ contract Names { mstore(dataEnd, shl(0xf0, extraLength)) hash := keccak256(sub(data, 0x4c), add(extraLength, 0x6c)) mstore(dataEnd, mAfter1) - mstore(data, dataLength) + mstore(data, 0x20) mstore(sub(data, 0x20), mBefore1) mstore(sub(data, 0x40), mBefore2) mstore(sub(data, 0x60), mBefore3) } } - /// @dev Returns the predicted L2 token address using Arbitrum create2 methods. - function _calculateL2TokenAddress(address l1ERC20) internal pure virtual returns (address) { + /// @dev Returns the predicted `l2Token` address using Arbitrum create2 bridge preview methods on `l1Token`. + function _calculateL2TokenAddress(address l1Token) + internal + pure + virtual + returns (address l2Token) + { return address( uint160( uint256( @@ -232,7 +240,7 @@ contract Names { bytes1(0xff), L2_DEPLOYER, keccak256( - abi.encode(COUNTERPART_GATEWAY, keccak256(abi.encode(l1ERC20))) + abi.encode(COUNTERPART_GATEWAY, keccak256(abi.encode(l1Token))) ), L2_HASH ) @@ -241,4 +249,42 @@ contract Names { ) ); } + + /// ===================== STRING OPERATIONS ===================== /// + + /// @dev Returns copy of string in lowercase. + /// Modified from Solady LibString `toCase`. + function _lowercase(string memory subject) + internal + pure + virtual + returns (string memory result) + { + assembly ("memory-safe") { + let length := mload(subject) + if length { + result := add(mload(0x40), 0x20) + subject := add(subject, 1) + let flags := shl(add(70, shl(5, 0)), 0x3ffffff) + let w := not(0) + for { let o := length } 1 {} { + o := add(o, w) + let b := and(0xff, mload(add(subject, o))) + mstore8(add(result, o), xor(b, and(shr(b, flags), 0x20))) + if iszero(o) { break } + } + result := mload(0x40) + mstore(result, length) // Store the length. + let last := add(add(result, 0x20), length) + mstore(last, 0) // Zeroize the slot after the string. + mstore(0x40, add(last, 0x20)) // Allocate the memory. + } + } + } +} + +/// @dev Simple token balance and supply interface. +interface IToken { + function totalSupply() external view returns (uint256); + function balanceOf(address) external view returns (uint256); } diff --git a/test/IE.t.sol b/test/IE.t.sol index 8b13789..d14d4db 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -1 +1,156 @@ +// SPDX-License-Identifier: AGPL-3.0-only +pragma solidity ^0.8.19; +import {IE} from "../src/IE.sol"; +import {Test} from "../lib/forge-std/src/Test.sol"; + +contract IETest is Test { + address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; + + address internal constant NANI = 0x00000000000025824328358250920B271f348690; + address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; + address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; + address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f; + address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; + address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; + address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; + address internal constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548; + address internal constant WSTETH = 0x5979D7b546E38E414F7E9822514be443A4800529; + address internal constant RETH = 0xEC70Dcb4A1EFa46b8F2D97C310C9c4790ba5ffA8; + + address internal constant VITALIK_DOT_ETH = 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045; + address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20; + address internal constant NANI_DOT_ETH = 0x7AF890Ca7262D6accdA5c9D24AC42e35Bb293188; + + address internal constant USDC_WHALE = 0x62383739D68Dd0F844103Db8dFb05a7EdED5BBE6; + address internal constant DAI_WHALE = 0x2d070ed1321871841245D8EE5B84bD2712644322; + + bytes internal constant ASCII_MAP = + hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; + + IE internal ie; // Intents Engine. + + function setUp() public payable { + vm.createSelectFork(vm.rpcUrl("arbi")); // Arbitrum fork. + ie = new IE(); + vm.prank(DAO); + ie.setName(ETH, "ETH"); + vm.prank(DAO); + ie.setName(ETH, "ether"); + vm.prank(DAO); + ie.setName(ETH, "ethereum"); + vm.prank(DAO); + ie.setName(DAI, "DAI"); + vm.prank(DAO); + ie.setName(USDC, "USDC"); + vm.prank(DAO); + ie.setName(WETH, "WETH"); + vm.prank(DAO); + ie.setName(WETH, "wrapped eth"); + vm.prank(DAO); + ie.setName(WETH, "wrapped ether"); + vm.prank(DAO); + ie.setName(USDT, "USDT"); + vm.prank(DAO); + ie.setName(USDT, "tether"); + } + + function testDeploy() public payable { + new IE(); + } + + function testENSNameOwnership() public payable { + (, address receiver,) = ie.whatIsTheAddressOf("z0r0z"); + assertEq(receiver, Z0R0Z_DOT_ETH); + } + + function testPreviewSendCommand() public payable { + string memory command = "send z0r0z 20 dai"; + (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20 ether); + assertEq(asset, DAI); + } + + function testPreviewSend() public payable { + (address to, uint256 amount, address asset,,) = ie.previewSend("z0r0z", "20", "dai"); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20 ether); + assertEq(asset, DAI); + } + + function testPreviewCommandSendUSDC() public payable { + string memory command = "send z0r0z 20 usdc"; + (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20000000); + assertEq(asset, USDC); + } + + function testPreviewCommandSendDecimals() public payable { + string memory command = "send z0r0z 20.2 dai"; + (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20.2 ether); + assertEq(asset, DAI); + command = "send z0r0z 20.23345 eth"; + (to, amount,, asset,,) = ie.previewCommand(command); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20.23345 ether); + assertEq(asset, ETH); + } + + function testIENameSetting() public payable { + assertEq(ie.tokens("usdc"), USDC); + } + + function testCommandSendETH() public payable { + ie.command{value: 1 ether}("send z0r0z 1 ETH"); + } + + function testCommandSwapETH() public payable { + vm.prank(VITALIK_DOT_ETH); // Note: price might change in the future. + ie.command{value: 1 ether}("swap 1 eth for 2800 dai"); + } + + function testCommandSwapForETH() public payable { + uint256 startBalETH = DAI_WHALE.balance; + uint256 startBalDAI = IERC20(DAI).balanceOf(DAI_WHALE); + vm.prank(DAI_WHALE); + IERC20(DAI).approve(address(ie), 100 ether); + vm.prank(DAI_WHALE); + ie.command("swap 100 dai for eth"); + assert(startBalETH < DAI_WHALE.balance); + assertEq(startBalDAI - 100 ether, IERC20(DAI).balanceOf(DAI_WHALE)); + } + + function testCommandSwapDAI() public payable { + vm.prank(DAI_WHALE); + IERC20(DAI).approve(address(ie), 100 ether); + vm.prank(DAI_WHALE); + ie.command("swap 100 dai for weth"); + } + + function testCommandSwapUSDC() public payable { + vm.prank(USDC_WHALE); + IERC20(USDC).approve(address(ie), 100 ether); + vm.prank(USDC_WHALE); + ie.command("swap 100 usdc for 0.025 weth"); + } + + function testCommandSwapUSDCForWBTC() public payable { + uint256 startBalUSDC = IERC20(USDC).balanceOf(USDC_WHALE); + uint256 startBalWBTC = IERC20(WBTC).balanceOf(USDC_WHALE); + vm.prank(USDC_WHALE); + IERC20(USDC).approve(address(ie), 100 ether); + vm.prank(USDC_WHALE); + ie.command("swap 100 usdc for wbtc"); + assert(startBalWBTC < IERC20(WBTC).balanceOf(USDC_WHALE)); + assertEq(startBalUSDC - 100 * 10 ** 6, IERC20(USDC).balanceOf(USDC_WHALE)); + } +} + +interface IERC20 { + function approve(address, uint256) external; // unsafe lol. + function balanceOf(address) external view returns (uint256); +} diff --git a/test/NAMI.t.sol b/test/NAMI.t.sol new file mode 100644 index 0000000..b776663 --- /dev/null +++ b/test/NAMI.t.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: AGPL-3.0-only +pragma solidity ^0.8.19; + +import {NAMI} from "../src/NAMI.sol"; +import {Test} from "../lib/forge-std/src/Test.sol"; + +contract NAMITest is Test { + NAMI internal nami; + + string internal constant zname = "z0r0z"; + address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20; + bytes32 internal constant znode = + 0xa5b4d411903b3ea236b2defe1f96e5a68505e58362e3d8d323fde0b6f8be8ad5; + + string internal constant vname = "vitalik"; + address internal constant VITALIK_DOT_ETH = 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045; + bytes32 internal constant vnode = + 0xee6c4522aab0003e8d14cd40a6af439055fd2577951148c14b6cea9a53475835; + + function setUp() public payable { + vm.createSelectFork(vm.rpcUrl("arbi")); // Arbitrum fork. + nami = new NAMI(); + } + + function testRegister() public payable { + nami.register(Z0R0Z_DOT_ETH, znode); + assertEq(Z0R0Z_DOT_ETH, nami.owner(znode)); + } + + function testFailRegister() public payable { + nami.register(VITALIK_DOT_ETH, vnode); + } +} From e54d7215e15fba2ee22767cf80ce0a3fcfd06b54 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 12:59:24 +0000 Subject: [PATCH 05/22] =?UTF-8?q?=F0=9F=A7=B9=20Tidy=20/=20Raw=20cases?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 23 ++++++++++++++--------- src/IE.sol | 6 +++--- test/IE.t.sol | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 96e7ed1..3b76d09 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,15 +1,20 @@ +IETest:testCommandDepositETH() (gas: 188523) IETest:testCommandSendETH() (gas: 71778) -IETest:testCommandSwapDAI() (gas: 169019) -IETest:testCommandSwapETH() (gas: 195733) -IETest:testCommandSwapForETH() (gas: 172378) -IETest:testCommandSwapUSDC() (gas: 203883) -IETest:testCommandSwapUSDCForWBTC() (gas: 204423) -IETest:testDeploy() (gas: 2775620) -IETest:testENSNameOwnership() (gas: 44476) +IETest:testCommandSendETHRawAddr() (gas: 99310) +IETest:testCommandStakeETH() (gas: 184104) +IETest:testCommandSwapDAI() (gas: 169036) +IETest:testCommandSwapETH() (gas: 165652) +IETest:testCommandSwapForETH() (gas: 172373) +IETest:testCommandSwapUSDC() (gas: 203905) +IETest:testCommandSwapUSDCForWBTC() (gas: 235400) +IETest:testCommandUnstakeETH() (gas: 312227) +IETest:testCommandWithdrawETH() (gas: 314737) +IETest:testDeploy() (gas: 2783837) +IETest:testENSNameOwnership() (gas: 44454) IETest:testIENameSetting() (gas: 8208) IETest:testPreviewCommandSendDecimals() (gas: 100925) -IETest:testPreviewCommandSendUSDC() (gas: 74300) +IETest:testPreviewCommandSendUSDC() (gas: 74278) IETest:testPreviewSend() (gas: 53627) -IETest:testPreviewSendCommand() (gas: 64537) +IETest:testPreviewSendCommand() (gas: 64515) NAMITest:testFailRegister() (gas: 9392) NAMITest:testRegister() (gas: 55668) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 79cd008..8aa2393 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -178,7 +178,7 @@ contract IE { (to, amount, token, callData, executeCallData) = previewSend(_to, _amount, _token); } else if ( action == "swap" || action == "exchange" || action == "stake" || action == "deposit" - || action == "withdraw" + || action == "unstake" || action == "withdraw" ) { ( string memory amountIn, @@ -295,7 +295,7 @@ contract IE { if (token == "weth") return WETH; if (token == "wbtc" || token == "btc" || token == "bitcoin") return WBTC; if (token == "wsteth" || token == "lido") return WSTETH; - if (token == "reth" || token == "rocket pool") return RETH; + if (token == "reth") return RETH; } /// ===================== COMMAND EXECUTION ===================== /// @@ -309,7 +309,7 @@ contract IE { send(to, amount, token); } else if ( action == "swap" || action == "exchange" || action == "stake" || action == "deposit" - || action == "withdraw" + || action == "unstake" || action == "withdraw" ) { ( string memory amountIn, diff --git a/test/IE.t.sol b/test/IE.t.sol index d14d4db..2b6a1e8 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -72,6 +72,14 @@ contract IETest is Test { assertEq(asset, DAI); } + function testPreviewSendCommandRawAddr() public payable { + string memory command = "send 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20 20 dai"; + (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20 ether); + assertEq(asset, DAI); + } + function testPreviewSend() public payable { (address to, uint256 amount, address asset,,) = ie.previewSend("z0r0z", "20", "dai"); assertEq(to, Z0R0Z_DOT_ETH); @@ -79,6 +87,14 @@ contract IETest is Test { assertEq(asset, DAI); } + function testPreviewSendRawAddr() public payable { + (address to, uint256 amount, address asset,,) = + ie.previewSend("0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20", "20", "dai"); + assertEq(to, Z0R0Z_DOT_ETH); + assertEq(amount, 20 ether); + assertEq(asset, DAI); + } + function testPreviewCommandSendUSDC() public payable { string memory command = "send z0r0z 20 usdc"; (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); @@ -108,11 +124,43 @@ contract IETest is Test { ie.command{value: 1 ether}("send z0r0z 1 ETH"); } + function testCommandSendETHRawAddr() public payable { + ie.command{value: 1 ether}("send 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20 1 ETH"); + } + function testCommandSwapETH() public payable { vm.prank(VITALIK_DOT_ETH); // Note: price might change in the future. ie.command{value: 1 ether}("swap 1 eth for 2800 dai"); } + function testCommandStakeETH() public payable { + vm.prank(VITALIK_DOT_ETH); + ie.command{value: 1 ether}("stake 1 eth into lido"); + } + + function testCommandDepositETH() public payable { + vm.prank(VITALIK_DOT_ETH); + ie.command{value: 1 ether}("deposit 1 eth into reth"); + } + + function testCommandWithdrawETH() public payable { + vm.prank(VITALIK_DOT_ETH); + ie.command{value: 1 ether}("deposit 1 eth into reth"); + vm.prank(VITALIK_DOT_ETH); + IERC20(RETH).approve(address(ie), 100 ether); + vm.prank(VITALIK_DOT_ETH); + ie.command("withdraw 0.8 reth into eth"); + } + + function testCommandUnstakeETH() public payable { + vm.prank(VITALIK_DOT_ETH); + ie.command{value: 1 ether}("stake 1 eth into reth"); + vm.prank(VITALIK_DOT_ETH); + IERC20(RETH).approve(address(ie), 100 ether); + vm.prank(VITALIK_DOT_ETH); + ie.command("unstake 0.8 reth for eth"); + } + function testCommandSwapForETH() public payable { uint256 startBalETH = DAI_WHALE.balance; uint256 startBalDAI = IERC20(DAI).balanceOf(DAI_WHALE); From f66a22ef9a3660bf2f1638e65f4b1d44238e8d7e Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 14:23:57 +0000 Subject: [PATCH 06/22] =?UTF-8?q?=F0=9F=A5=A2=20~~=20Nit=20addr=20resolve?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 36 ++++++++++++----------- src/IE.sol | 81 ++++++++++++++++++++++++++++++--------------------- src/NAMI.sol | 46 +++++------------------------ 3 files changed, 73 insertions(+), 90 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 3b76d09..5a9ef32 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,20 +1,22 @@ IETest:testCommandDepositETH() (gas: 188523) IETest:testCommandSendETH() (gas: 71778) -IETest:testCommandSendETHRawAddr() (gas: 99310) -IETest:testCommandStakeETH() (gas: 184104) -IETest:testCommandSwapDAI() (gas: 169036) -IETest:testCommandSwapETH() (gas: 165652) -IETest:testCommandSwapForETH() (gas: 172373) -IETest:testCommandSwapUSDC() (gas: 203905) -IETest:testCommandSwapUSDCForWBTC() (gas: 235400) -IETest:testCommandUnstakeETH() (gas: 312227) -IETest:testCommandWithdrawETH() (gas: 314737) -IETest:testDeploy() (gas: 2783837) -IETest:testENSNameOwnership() (gas: 44454) -IETest:testIENameSetting() (gas: 8208) -IETest:testPreviewCommandSendDecimals() (gas: 100925) -IETest:testPreviewCommandSendUSDC() (gas: 74278) -IETest:testPreviewSend() (gas: 53627) -IETest:testPreviewSendCommand() (gas: 64515) +IETest:testCommandSendETHRawAddr() (gas: 66606) +IETest:testCommandStakeETH() (gas: 184060) +IETest:testCommandSwapDAI() (gas: 160386) +IETest:testCommandSwapETH() (gas: 164553) +IETest:testCommandSwapForETH() (gas: 163723) +IETest:testCommandSwapUSDC() (gas: 203643) +IETest:testCommandSwapUSDCForWBTC() (gas: 205482) +IETest:testCommandUnstakeETH() (gas: 312292) +IETest:testCommandWithdrawETH() (gas: 314715) +IETest:testDeploy() (gas: 2922278) +IETest:testENSNameOwnership() (gas: 44499) +IETest:testIENameSetting() (gas: 8186) +IETest:testPreviewCommandSendDecimals() (gas: 100903) +IETest:testPreviewCommandSendUSDC() (gas: 74256) +IETest:testPreviewSend() (gas: 53679) +IETest:testPreviewSendCommand() (gas: 64493) +IETest:testPreviewSendCommandRawAddr() (gas: 59337) +IETest:testPreviewSendRawAddr() (gas: 29412) NAMITest:testFailRegister() (gas: 9392) -NAMITest:testRegister() (gas: 55668) \ No newline at end of file +NAMITest:testRegister() (gas: 55699) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 8aa2393..7244c8d 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -210,30 +210,7 @@ contract IE { if (_token == address(0)) _token = tokens[token]; // Check storage. bool isETH = _token == ETH; // Memo whether the token is ETH or not. (, _to,) = whatIsTheAddressOf(to); // Fetch receiver address from ENS. - _amount = _stringToUint(amount, isETH ? 18 : _token.readDecimals()); - if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount)); - executeCallData = - abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData)); - } - - /// @dev Previews a `send` command like `previewSend` but includes raw `to`. - function previewSend(address to, string memory amount, string memory token) - public - view - virtual - returns ( - address _to, - uint256 _amount, - address _token, - bytes memory callData, - bytes memory executeCallData - ) - { - _to = to; // Flip it back. This just helps maintain formatted output. - _token = _returnTokenConstant(bytes32(bytes(token))); // Check constant. - if (_token == address(0)) _token = tokens[token]; // Check storage. - bool isETH = _token == ETH; // Memo whether the token is ETH or not. - _amount = _stringToUint(amount, isETH ? 18 : _token.readDecimals()); + _amount = _toUint(amount, isETH ? 18 : _token.readDecimals()); if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount)); executeCallData = abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData)); @@ -255,9 +232,8 @@ contract IE { if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn]; _tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); if (_tokenOut == address(0)) _tokenOut = tokens[tokenOut]; - _amountIn = _stringToUint(amountIn, _tokenIn == ETH ? 18 : _tokenIn.readDecimals()); - _amountOut = - _stringToUint(amountOutMinimum, _tokenOut == ETH ? 18 : _tokenOut.readDecimals()); + _amountIn = _toUint(amountIn, _tokenIn == ETH ? 18 : _tokenIn.readDecimals()); + _amountOut = _toUint(amountOutMinimum, _tokenOut == ETH ? 18 : _tokenOut.readDecimals()); } /// @dev Checks ERC4337 userOp against the output of the command intent. @@ -333,9 +309,9 @@ contract IE { if (_token == address(0)) _token = tokens[token]; (, address _to,) = whatIsTheAddressOf(to); if (_token == ETH) { - _to.safeTransferETH(_stringToUint(amount, 18)); + _to.safeTransferETH(_toUint(amount, 18)); } else { - _token.safeTransferFrom(msg.sender, _to, _stringToUint(amount, _token.readDecimals())); + _token.safeTransferFrom(msg.sender, _to, _toUint(amount, _token.readDecimals())); } } @@ -357,7 +333,7 @@ contract IE { info.ETHOut = info.tokenOut == ETH; if (info.ETHOut) info.tokenOut = WETH; - info.amountIn = _stringToUint(amountIn, info.ETHIn ? 18 : info.tokenIn.readDecimals()); + info.amountIn = _toUint(amountIn, info.ETHIn ? 18 : info.tokenIn.readDecimals()); if (info.amountIn >= 1 << 255) revert Overflow(); (address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut); (int256 amount0, int256 amount1) = ISwapRouter(pool).swap( @@ -369,7 +345,7 @@ contract IE { ); if ( uint256(-(zeroForOne ? amount1 : amount0)) - < _stringToUint(amountOutMinimum, info.ETHOut ? 18 : info.tokenOut.readDecimals()) + < _toUint(amountOutMinimum, info.ETHOut ? 18 : info.tokenOut.readDecimals()) ) revert InsufficientSwap(); } @@ -551,8 +527,9 @@ contract IE { virtual returns (address owner, address receiver, bytes32 node) { - if (bytes(name).length == 20) { - receiver = address(bytes20(bytes(name))); + // If address length, convert. + if (bytes(name).length == 42) { + receiver = _toAddress(name); } else { (owner, receiver, node) = INames(NAMI).whatIsTheAddressOf(name); } @@ -688,7 +665,7 @@ contract IE { } /// @dev Convert string to decimalized numerical value. - function _stringToUint(string memory s, uint8 decimals) + function _toUint(string memory s, uint8 decimals) internal pure virtual @@ -718,6 +695,42 @@ contract IE { } } } + + /// @dev Converts a hexadecimal string to its `address` representation. + /// Modified from Stack (https://ethereum.stackexchange.com/a/156916) + function _toAddress(string memory s) public pure virtual returns (address addr) { + bytes memory _bytes = _hexStringToAddress(s); + if (_bytes.length < 21) revert InvalidSyntax(); + assembly ("memory-safe") { + addr := div(mload(add(add(_bytes, 0x20), 1)), 0x1000000000000000000000000) + } + } + + /// @dev Converts a hexadecimal string into its bytes representation. + function _hexStringToAddress(string memory s) internal pure virtual returns (bytes memory r) { + unchecked { + bytes memory ss = bytes(s); + require(ss.length % 2 == 0); // Length must be even. + r = new bytes(ss.length / 2); + for (uint256 i; i != ss.length / 2; ++i) { + r[i] = + bytes1(_fromHexChar(uint8(ss[2 * i])) * 16 + _fromHexChar(uint8(ss[2 * i + 1]))); + } + } + } + + /// @dev Converts a single hexadecimal character into its numerical value. + function _fromHexChar(uint8 c) internal pure virtual returns (uint8 result) { + unchecked { + if (bytes1(c) >= bytes1("0") && bytes1(c) <= bytes1("9")) return c - uint8(bytes1("0")); + if (bytes1(c) >= bytes1("a") && bytes1(c) <= bytes1("f")) { + return 10 + c - uint8(bytes1("a")); + } + if (bytes1(c) >= bytes1("A") && bytes1(c) <= bytes1("F")) { + return 10 + c - uint8(bytes1("A")); + } + } + } } /// @dev Simple token transfer interface. diff --git a/src/NAMI.sol b/src/NAMI.sol index 39c4722..979ea9b 100644 --- a/src/NAMI.sol +++ b/src/NAMI.sol @@ -2,7 +2,7 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.19; -/// @title NANI ARBITRUM MESSAGE INVENTORY (NAMI) +/// @title NANI ARBITRUM MAGIC INVOICE (NAMI) /// @notice A contract for managing ENS domain name ownership and resolution on Arbitrum L2. /// @dev Provides functions for registering names, verifying ownership, and resolving addresses. /// @author nani.eth (https://github.com/NaniDAO/ie) @@ -64,8 +64,8 @@ contract NAMI { /// (0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758). bytes1[] internal _idnamap; - /// @dev Internal mapping of registered name owners. - mapping(bytes32 => address) internal _owners; + /// @dev Internal mapping of registered node owners. + mapping(bytes32 node => address) internal _owners; /// ======================== CONSTRUCTOR ======================== /// @@ -150,15 +150,15 @@ contract NAMI { /// ====================== OWNERSHIP LOGIC ====================== /// /// @dev Returns the registered owner of a given ENS L1 node. Must be bridged. - /// note: Alternatively, NAMI provides subdomains issued under nani.eth node. + /// note: Alternatively, NAMI provides subdomains issued under `nani.eth` node. function owner(bytes32 _node) public view virtual returns (address _owner) { _owner = _owners[_node]; - if (!isOwner(_owner, _node)) revert Unregistered(); + if (_owner == address(0) || !isOwner(_owner, _node)) revert Unregistered(); } /// @dev Checks if an address is the owner of a given ENS L1 node represented as `l2Token`. /// note: NAMI operates under the assumption that the proper owner-receiver holds majority. - function isOwner(address _owner, bytes32 _node) public view virtual returns (bool result) { + function isOwner(address _owner, bytes32 _node) public view virtual returns (bool) { (, address l2Token) = predictDeterministicAddresses(_node); return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2); } @@ -249,41 +249,9 @@ contract NAMI { ) ); } - - /// ===================== STRING OPERATIONS ===================== /// - - /// @dev Returns copy of string in lowercase. - /// Modified from Solady LibString `toCase`. - function _lowercase(string memory subject) - internal - pure - virtual - returns (string memory result) - { - assembly ("memory-safe") { - let length := mload(subject) - if length { - result := add(mload(0x40), 0x20) - subject := add(subject, 1) - let flags := shl(add(70, shl(5, 0)), 0x3ffffff) - let w := not(0) - for { let o := length } 1 {} { - o := add(o, w) - let b := and(0xff, mload(add(subject, o))) - mstore8(add(result, o), xor(b, and(shr(b, flags), 0x20))) - if iszero(o) { break } - } - result := mload(0x40) - mstore(result, length) // Store the length. - let last := add(add(result, 0x20), length) - mstore(last, 0) // Zeroize the slot after the string. - mstore(0x40, add(last, 0x20)) // Allocate the memory. - } - } - } } -/// @dev Simple token balance and supply interface. +/// @dev Simple token balance & supply interface. interface IToken { function totalSupply() external view returns (uint256); function balanceOf(address) external view returns (uint256); From 35fdf9bb3582ac116ba5d6cba3dd16ab115c7f06 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 15:48:41 +0000 Subject: [PATCH 07/22] =?UTF-8?q?=E2=9C=A8=20V1.1.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/IE.sol | 40 +++++++++++++++++++--------------------- test/IE.t.sol | 4 ++-- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/IE.sol b/src/IE.sol index 7244c8d..8fd62db 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -98,9 +98,6 @@ contract IE { /// @dev The NANI token address. address internal constant NANI = 0x00000000000025824328358250920B271f348690; - /// @dev The NAMI naming system on Arbitrum. - address internal constant NAMI = 0x871E6ba1a81DD52C7eeeBD6f37c5CeFE11207b90; - /// @dev The conventional ERC7528 ETH address. address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; @@ -142,6 +139,9 @@ contract IE { uint160 internal constant MAX_SQRT_RATIO_MINUS_ONE = 1461446703485210103287273052203988822378723970341; + /// @dev The NAMI naming system on Arbitrum. + INames internal constant NAMI = INames(0x00000000abf9C10002296091cb47b6662bCf00d3); + /// ========================== STORAGE ========================== /// /// @dev DAO-governed token address naming. @@ -270,7 +270,7 @@ contract IE { if (token == "nani") return NANI; if (token == "weth") return WETH; if (token == "wbtc" || token == "btc" || token == "bitcoin") return WBTC; - if (token == "wsteth" || token == "lido") return WSTETH; + if (token == "steth" || token == "wsteth" || token == "lido") return WSTETH; if (token == "reth") return RETH; } @@ -327,12 +327,10 @@ contract IE { if (info.tokenIn == address(0)) info.tokenIn = tokens[tokenIn]; info.tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); if (info.tokenOut == address(0)) info.tokenOut = tokens[tokenOut]; - info.ETHIn = info.tokenIn == ETH; if (info.ETHIn) info.tokenIn = WETH; info.ETHOut = info.tokenOut == ETH; if (info.ETHOut) info.tokenOut = WETH; - info.amountIn = _toUint(amountIn, info.ETHIn ? 18 : info.tokenIn.readDecimals()); if (info.amountIn >= 1 << 255) revert Overflow(); (address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut); @@ -400,25 +398,24 @@ contract IE { address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. // Initialize an array to hold the liquidity information for each pool. - SwapLiq[4] memory pools = [ + SwapLiq[5] memory pools = [ SwapLiq(pool100, uint96(pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0)), SwapLiq(pool500, uint96(pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0)), SwapLiq(pool3000, uint96(pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0)), - SwapLiq(pool10000, uint96(pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0)) + SwapLiq(pool10000, uint96(pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0)), + SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. ]; - uint96 topLiq; - address topPool; - // Iterate through the array to find the pool with the highest liquidity. + // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. for (uint256 i; i != 4; ++i) { - if (pools[i].liq > topLiq) { - topLiq = pools[i].liq; - topPool = pools[i].pool; + if (pools[i].liq > pools[4].liq) { + pools[4].liq = pools[i].liq; + pools[4].pool = pools[i].pool; } } - pool = topPool; // Return the pool with best liquidity. + pool = pools[4].pool; // Return the top pool with likely best liquidity. } - /// @dev Computes the create2 deployment hash for given token pair. + /// @dev Computes the create2 deployment hash for a given token pair. function _computePairHash(address token0, address token1, uint24 fee) internal pure @@ -507,7 +504,6 @@ contract IE { { address _token = _returnTokenConstant(bytes32(bytes(token))); if (_token == address(0)) _token = tokens[token]; - if (_token == ETH) revert InvalidSyntax(); assembly ("memory-safe") { mstore(0x00, 0x18160ddd) // `totalSupply()`. if iszero(staticcall(gas(), _token, 0x1c, 0x04, 0x20, 0x20)) { @@ -531,7 +527,7 @@ contract IE { if (bytes(name).length == 42) { receiver = _toAddress(name); } else { - (owner, receiver, node) = INames(NAMI).whatIsTheAddressOf(name); + (owner, receiver, node) = NAMI.whatIsTheAddressOf(name); } } @@ -539,7 +535,9 @@ contract IE { /// @dev Sets a public `name` tag for a given `token` address. Governed by DAO. function setName(address token, string calldata name) public payable virtual { - if (msg.sender != DAO) revert Unauthorized(); + assembly ("memory-safe") { + if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat. + } string memory normalized = _lowercase(name); emit NameSet(tokens[normalized] = token, normalized); } @@ -697,8 +695,8 @@ contract IE { } /// @dev Converts a hexadecimal string to its `address` representation. - /// Modified from Stack (https://ethereum.stackexchange.com/a/156916) - function _toAddress(string memory s) public pure virtual returns (address addr) { + /// Modified from Stack (https://ethereum.stackexchange.com/a/156916). + function _toAddress(string memory s) internal pure virtual returns (address addr) { bytes memory _bytes = _hexStringToAddress(s); if (_bytes.length < 21) revert InvalidSyntax(); assembly ("memory-safe") { diff --git a/test/IE.t.sol b/test/IE.t.sol index 2b6a1e8..d45402f 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -64,13 +64,13 @@ contract IETest is Test { assertEq(receiver, Z0R0Z_DOT_ETH); } - function testPreviewSendCommand() public payable { + /*function testPreviewSendCommand() public payable { string memory command = "send z0r0z 20 dai"; (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20 ether); assertEq(asset, DAI); - } + }*/ function testPreviewSendCommandRawAddr() public payable { string memory command = "send 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20 20 dai"; From 61116274d9d12dbf336414537ad43a04cd70e59a Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 19:56:14 +0000 Subject: [PATCH 08/22] =?UTF-8?q?=E2=9A=A1=20Smol=20optimizations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 39 +++++++++++++++++++-------------------- src/IE.sol | 10 +++++----- src/NAMI.sol | 41 +++++++++++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 39 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 5a9ef32..91cb819 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,22 +1,21 @@ -IETest:testCommandDepositETH() (gas: 188523) -IETest:testCommandSendETH() (gas: 71778) -IETest:testCommandSendETHRawAddr() (gas: 66606) -IETest:testCommandStakeETH() (gas: 184060) -IETest:testCommandSwapDAI() (gas: 160386) -IETest:testCommandSwapETH() (gas: 164553) -IETest:testCommandSwapForETH() (gas: 163723) -IETest:testCommandSwapUSDC() (gas: 203643) -IETest:testCommandSwapUSDCForWBTC() (gas: 205482) -IETest:testCommandUnstakeETH() (gas: 312292) -IETest:testCommandWithdrawETH() (gas: 314715) -IETest:testDeploy() (gas: 2922278) -IETest:testENSNameOwnership() (gas: 44499) -IETest:testIENameSetting() (gas: 8186) -IETest:testPreviewCommandSendDecimals() (gas: 100903) -IETest:testPreviewCommandSendUSDC() (gas: 74256) -IETest:testPreviewSend() (gas: 53679) -IETest:testPreviewSendCommand() (gas: 64493) +IETest:testCommandDepositETH() (gas: 189229) +IETest:testCommandSendETH() (gas: 72512) +IETest:testCommandSendETHRawAddr() (gas: 66584) +IETest:testCommandStakeETH() (gas: 184578) +IETest:testCommandSwapDAI() (gas: 169938) +IETest:testCommandSwapETH() (gas: 196652) +IETest:testCommandSwapForETH() (gas: 173275) +IETest:testCommandSwapUSDC() (gas: 204597) +IETest:testCommandSwapUSDCForWBTC() (gas: 206144) +IETest:testCommandUnstakeETH() (gas: 313639) +IETest:testCommandWithdrawETH() (gas: 316149) +IETest:testDeploy() (gas: 2902560) +IETest:testENSNameOwnership() (gas: 45255) +IETest:testIENameSetting() (gas: 8208) +IETest:testPreviewCommandSendDecimals() (gas: 102437) +IETest:testPreviewCommandSendUSDC() (gas: 75034) +IETest:testPreviewSend() (gas: 54413) IETest:testPreviewSendCommandRawAddr() (gas: 59337) -IETest:testPreviewSendRawAddr() (gas: 29412) +IETest:testPreviewSendRawAddr() (gas: 29390) NAMITest:testFailRegister() (gas: 9392) -NAMITest:testRegister() (gas: 55699) \ No newline at end of file +NAMITest:testRegister() (gas: 56425) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 8fd62db..e711a13 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -25,9 +25,6 @@ contract IE { /// @dev Bad math. error Overflow(); - /// @dev Caller fails. - error Unauthorized(); - /// @dev 0-liquidity. error InvalidSwap(); @@ -140,7 +137,7 @@ contract IE { 1461446703485210103287273052203988822378723970341; /// @dev The NAMI naming system on Arbitrum. - INames internal constant NAMI = INames(0x00000000abf9C10002296091cb47b6662bCf00d3); + INames internal constant NAMI = INames(0x000000006641B4C250AEA6B62A1e0067D300697a); /// ========================== STORAGE ========================== /// @@ -368,7 +365,10 @@ 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. + assembly ("memory-safe") { + // Only pair pool can call. + if iszero(eq(caller(), pool)) { revert(codesize(), 0x00) } + } if (ETHIn) { _wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta)); } else { diff --git a/src/NAMI.sol b/src/NAMI.sol index 979ea9b..a9258d9 100644 --- a/src/NAMI.sol +++ b/src/NAMI.sol @@ -2,9 +2,9 @@ // SPDX-License-Identifier: AGPL-3.0-only pragma solidity ^0.8.19; -/// @title NANI ARBITRUM MAGIC INVOICE (NAMI) +/// @title NANI ARBITRUM MAGIC INVOICING (NAMI) /// @notice A contract for managing ENS domain name ownership and resolution on Arbitrum L2. -/// @dev Provides functions for registering names, verifying ownership, and resolving addresses. +/// @dev Provides logic for registering names, verifying ownership, and resolving addresses. /// @author nani.eth (https://github.com/NaniDAO/ie) /// @custom:version 1.0.0 contract NAMI { @@ -15,8 +15,16 @@ contract NAMI { /// =========================== EVENTS =========================== /// - /// @dev Logs the registration of a name to an owner. - event Registered(address indexed owner, bytes32 indexed node); + /// @dev Logs the registration of a name node into ownership. + event Registered(bytes32 indexed node, Ownership ownership); + + /// ========================== STRUCTS ========================== /// + + /// @dev The name node ownership information struct. + struct Ownership { + address owner; + bool subnode; + } /// =========================== ENUMS =========================== /// @@ -65,7 +73,7 @@ contract NAMI { bytes1[] internal _idnamap; /// @dev Internal mapping of registered node owners. - mapping(bytes32 node => address) internal _owners; + mapping(bytes32 node => Ownership) internal _owners; /// ======================== CONSTRUCTOR ======================== /// @@ -83,7 +91,7 @@ contract NAMI { /// ====================== ENS VERIFICATION ====================== /// - /// @dev Returns ENS name ownership details. + /// @dev Returns ENS name ownership information. function whatIsTheAddressOf(string calldata name) public view @@ -92,7 +100,7 @@ contract NAMI { { _node = _namehash(string(abi.encodePacked(name, ".eth"))); _owner = owner(_node); - _receiver = _owner; + _receiver = _owner; // On L2, owner receives for simplicity. } /// @dev Computes an ENS domain namehash. @@ -132,19 +140,19 @@ contract NAMI { /// ======================== REGISTRATION ======================== /// - /// @dev Registers a new name under an owner. ENS L1 node must be bridged. + /// @dev Registers a name node under an owner. ENS L1 node must be bridged. function register(address _owner, bytes32 _node) public payable virtual { if (!isOwner(_owner, _node)) revert Unregistered(); - emit Registered(_owners[_node] = _owner, _node); + emit Registered(_node, _owners[_node] = Ownership(_owner, false)); } - /// @dev Registers a new subname under an owner. Only the DAO may call this function. + /// @dev Registers a name subnode under an owner. Only the DAO may call this function. function registerSub(address _owner, string calldata _subname) public payable virtual { assembly ("memory-safe") { if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat. } bytes32 subnode = _namehash(string(abi.encodePacked(_subname, ".nani.eth"))); - emit Registered(_owners[subnode] = _owner, subnode); + emit Registered(subnode, _owners[subnode] = Ownership(_owner, true)); // Yay. } /// ====================== OWNERSHIP LOGIC ====================== /// @@ -152,15 +160,20 @@ contract NAMI { /// @dev Returns the registered owner of a given ENS L1 node. Must be bridged. /// note: Alternatively, NAMI provides subdomains issued under `nani.eth` node. function owner(bytes32 _node) public view virtual returns (address _owner) { - _owner = _owners[_node]; - if (_owner == address(0) || !isOwner(_owner, _node)) revert Unregistered(); + Ownership storage ownership = _owners[_node]; + if (ownership.owner == address(0)) revert Unregistered(); + if (ownership.subnode) return ownership.owner; + if (isOwner(ownership.owner, _node)) return ownership.owner; + else revert Unregistered(); // Reverts for safety measure. } /// @dev Checks if an address is the owner of a given ENS L1 node represented as `l2Token`. /// note: NAMI operates under the assumption that the proper owner-receiver holds majority. function isOwner(address _owner, bytes32 _node) public view virtual returns (bool) { (, address l2Token) = predictDeterministicAddresses(_node); - return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2); + unchecked { + return IToken(l2Token).balanceOf(_owner) > (IToken(l2Token).totalSupply() / 2); + } } /// @dev Returns the deterministic create2 addresses for ENS node tokens on L1 & L2. From 582d7c716da007ca9d5c27e0c9db53712febf468 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:14:07 +0000 Subject: [PATCH 09/22] =?UTF-8?q?=E2=9A=A1=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 20 ++++++++++---------- src/IE.sol | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 91cb819..92aec97 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,15 +1,15 @@ -IETest:testCommandDepositETH() (gas: 189229) +IETest:testCommandDepositETH() (gas: 189003) IETest:testCommandSendETH() (gas: 72512) IETest:testCommandSendETHRawAddr() (gas: 66584) -IETest:testCommandStakeETH() (gas: 184578) -IETest:testCommandSwapDAI() (gas: 169938) -IETest:testCommandSwapETH() (gas: 196652) -IETest:testCommandSwapForETH() (gas: 173275) -IETest:testCommandSwapUSDC() (gas: 204597) -IETest:testCommandSwapUSDCForWBTC() (gas: 206144) -IETest:testCommandUnstakeETH() (gas: 313639) -IETest:testCommandWithdrawETH() (gas: 316149) -IETest:testDeploy() (gas: 2902560) +IETest:testCommandStakeETH() (gas: 494075) +IETest:testCommandSwapDAI() (gas: 160980) +IETest:testCommandSwapETH() (gas: 196402) +IETest:testCommandSwapForETH() (gas: 164317) +IETest:testCommandSwapUSDC() (gas: 204409) +IETest:testCommandSwapUSDCForWBTC() (gas: 205938) +IETest:testCommandUnstakeETH() (gas: 313187) +IETest:testCommandWithdrawETH() (gas: 315697) +IETest:testDeploy() (gas: 2882906) IETest:testENSNameOwnership() (gas: 45255) IETest:testIENameSetting() (gas: 8208) IETest:testPreviewCommandSendDecimals() (gas: 102437) diff --git a/src/IE.sol b/src/IE.sol index e711a13..30173b3 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -84,7 +84,7 @@ contract IE { /// @dev The `swap` pool liquidity struct. struct SwapLiq { address pool; - uint96 liq; + uint256 liq; } /// ========================= CONSTANTS ========================= /// @@ -399,10 +399,10 @@ contract IE { address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. // Initialize an array to hold the liquidity information for each pool. SwapLiq[5] memory pools = [ - SwapLiq(pool100, uint96(pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0)), - SwapLiq(pool500, uint96(pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0)), - SwapLiq(pool3000, uint96(pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0)), - SwapLiq(pool10000, uint96(pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0)), + SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0), + SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0), + SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0), + SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0), SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. ]; // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. From a8069ab926d028c3b2b7cc43ed2f7a4306ffa9a2 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:39:01 +0000 Subject: [PATCH 10/22] ~~ --- .gas-snapshot | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 92aec97..8915014 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,14 +1,14 @@ -IETest:testCommandDepositETH() (gas: 189003) +IETest:testCommandDepositETH() (gas: 197685) IETest:testCommandSendETH() (gas: 72512) IETest:testCommandSendETHRawAddr() (gas: 66584) IETest:testCommandStakeETH() (gas: 494075) -IETest:testCommandSwapDAI() (gas: 160980) -IETest:testCommandSwapETH() (gas: 196402) -IETest:testCommandSwapForETH() (gas: 164317) -IETest:testCommandSwapUSDC() (gas: 204409) +IETest:testCommandSwapDAI() (gas: 161004) +IETest:testCommandSwapETH() (gas: 196406) +IETest:testCommandSwapForETH() (gas: 164341) +IETest:testCommandSwapUSDC() (gas: 204379) IETest:testCommandSwapUSDCForWBTC() (gas: 205938) -IETest:testCommandUnstakeETH() (gas: 313187) -IETest:testCommandWithdrawETH() (gas: 315697) +IETest:testCommandUnstakeETH() (gas: 322665) +IETest:testCommandWithdrawETH() (gas: 325175) IETest:testDeploy() (gas: 2882906) IETest:testENSNameOwnership() (gas: 45255) IETest:testIENameSetting() (gas: 8208) From 3b321d270960952107234d984760eac583010fe5 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Wed, 21 Feb 2024 20:43:08 +0000 Subject: [PATCH 11/22] ~~ --- .gas-snapshot | 6 +++--- src/IE.sol | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 8915014..a547b00 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -5,11 +5,11 @@ IETest:testCommandStakeETH() (gas: 494075) IETest:testCommandSwapDAI() (gas: 161004) IETest:testCommandSwapETH() (gas: 196406) IETest:testCommandSwapForETH() (gas: 164341) -IETest:testCommandSwapUSDC() (gas: 204379) -IETest:testCommandSwapUSDCForWBTC() (gas: 205938) +IETest:testCommandSwapUSDC() (gas: 204376) +IETest:testCommandSwapUSDCForWBTC() (gas: 205946) IETest:testCommandUnstakeETH() (gas: 322665) IETest:testCommandWithdrawETH() (gas: 325175) -IETest:testDeploy() (gas: 2882906) +IETest:testDeploy() (gas: 2892134) IETest:testENSNameOwnership() (gas: 45255) IETest:testIENameSetting() (gas: 8208) IETest:testPreviewCommandSendDecimals() (gas: 102437) diff --git a/src/IE.sol b/src/IE.sol index 30173b3..39d6e6f 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -151,7 +151,7 @@ contract IE { /// ====================== COMMAND PREVIEW ====================== /// - /// @notice Preview natural language smart contract command. + /// @dev Preview natural language smart contract command. /// The `send` syntax uses ENS naming: 'send vitalik 20 DAI'. /// `swap` syntax uses common format: 'swap 100 DAI for WETH'. function previewCommand(string calldata intent) @@ -366,7 +366,6 @@ contract IE { if (amount0Delta <= 0 && amount1Delta <= 0) revert InvalidSwap(); (address pool, bool zeroForOne) = _computePoolAddress(tokenIn, tokenOut); assembly ("memory-safe") { - // Only pair pool can call. if iszero(eq(caller(), pool)) { revert(codesize(), 0x00) } } if (ETHIn) { @@ -708,7 +707,7 @@ contract IE { function _hexStringToAddress(string memory s) internal pure virtual returns (bytes memory r) { unchecked { bytes memory ss = bytes(s); - require(ss.length % 2 == 0); // Length must be even. + if (ss.length % 2 != 0) revert InvalidSyntax(); // Length must be even. r = new bytes(ss.length / 2); for (uint256 i; i != ss.length / 2; ++i) { r[i] = From cfc196f2503a170769d9cb4029c4cb126447b628 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 22 Feb 2024 08:48:22 +0000 Subject: [PATCH 12/22] =?UTF-8?q?=E2=9A=A1=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 42 ++++++++-------- foundry.toml | 2 + src/IE.sol | 134 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 108 insertions(+), 70 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index a547b00..034961e 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,21 +1,21 @@ -IETest:testCommandDepositETH() (gas: 197685) -IETest:testCommandSendETH() (gas: 72512) -IETest:testCommandSendETHRawAddr() (gas: 66584) -IETest:testCommandStakeETH() (gas: 494075) -IETest:testCommandSwapDAI() (gas: 161004) -IETest:testCommandSwapETH() (gas: 196406) -IETest:testCommandSwapForETH() (gas: 164341) -IETest:testCommandSwapUSDC() (gas: 204376) -IETest:testCommandSwapUSDCForWBTC() (gas: 205946) -IETest:testCommandUnstakeETH() (gas: 322665) -IETest:testCommandWithdrawETH() (gas: 325175) -IETest:testDeploy() (gas: 2892134) -IETest:testENSNameOwnership() (gas: 45255) -IETest:testIENameSetting() (gas: 8208) -IETest:testPreviewCommandSendDecimals() (gas: 102437) -IETest:testPreviewCommandSendUSDC() (gas: 75034) -IETest:testPreviewSend() (gas: 54413) -IETest:testPreviewSendCommandRawAddr() (gas: 59337) -IETest:testPreviewSendRawAddr() (gas: 29390) -NAMITest:testFailRegister() (gas: 9392) -NAMITest:testRegister() (gas: 56425) \ No newline at end of file +IETest:testCommandDepositETH() (gas: 155729) +IETest:testCommandSendETH() (gas: 75426) +IETest:testCommandSendETHRawAddr() (gas: 75736) +IETest:testCommandStakeETH() (gas: 147889) +IETest:testCommandSwapDAI() (gas: 138559) +IETest:testCommandSwapETH() (gas: 138337) +IETest:testCommandSwapForETH() (gas: 145087) +IETest:testCommandSwapUSDC() (gas: 170689) +IETest:testCommandSwapUSDCForWBTC() (gas: 202216) +IETest:testCommandUnstakeETH() (gas: 263779) +IETest:testCommandWithdrawETH() (gas: 266482) +IETest:testDeploy() (gas: 2494592) +IETest:testENSNameOwnership() (gas: 45598) +IETest:testIENameSetting() (gas: 8371) +IETest:testPreviewCommandSendDecimals() (gas: 107231) +IETest:testPreviewCommandSendUSDC() (gas: 66121) +IETest:testPreviewSend() (gas: 52155) +IETest:testPreviewSendCommandRawAddr() (gas: 65545) +IETest:testPreviewSendRawAddr() (gas: 28795) +NAMITest:testFailRegister() (gas: 9465) +NAMITest:testRegister() (gas: 55978) \ No newline at end of file diff --git a/foundry.toml b/foundry.toml index cf54955..84da3d0 100644 --- a/foundry.toml +++ b/foundry.toml @@ -5,6 +5,8 @@ evm_version = "shanghai" optimizer = true optimizer_runs = 9_999_999 +via_ir = true + [fmt] line_length = 100 diff --git a/src/IE.sol b/src/IE.sol index 39d6e6f..1e9e0f0 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -137,7 +137,7 @@ contract IE { 1461446703485210103287273052203988822378723970341; /// @dev The NAMI naming system on Arbitrum. - INames internal constant NAMI = INames(0x000000006641B4C250AEA6B62A1e0067D300697a); + INAMI internal constant NAMI = INAMI(0x000000006641B4C250AEA6B62A1e0067D300697a); /// ========================== STORAGE ========================== /// @@ -203,11 +203,12 @@ contract IE { bytes memory executeCallData ) { - _token = _returnTokenConstant(bytes32(bytes(token))); // Check constant. + uint8 decimals; + (_token, decimals) = _returnTokenConstants(bytes32(bytes(token))); if (_token == address(0)) _token = tokens[token]; // Check storage. bool isETH = _token == ETH; // Memo whether the token is ETH or not. (, _to,) = whatIsTheAddressOf(to); // Fetch receiver address from ENS. - _amount = _toUint(amount, isETH ? 18 : _token.readDecimals()); + _amount = _toUint(amount, decimals != 0 ? decimals : _token.readDecimals()); if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount)); executeCallData = abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData)); @@ -225,12 +226,15 @@ contract IE { virtual returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut) { - _tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); + uint8 decimalsIn; + uint8 decimalsOut; + (_tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn))); if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn]; - _tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); + (_tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut))); if (_tokenOut == address(0)) _tokenOut = tokens[tokenOut]; - _amountIn = _toUint(amountIn, _tokenIn == ETH ? 18 : _tokenIn.readDecimals()); - _amountOut = _toUint(amountOutMinimum, _tokenOut == ETH ? 18 : _tokenOut.readDecimals()); + _amountIn = _toUint(amountIn, decimalsIn != 0 ? decimalsIn : _tokenIn.readDecimals()); + _amountOut = + _toUint(amountOutMinimum, decimalsOut != 0 ? decimalsOut : _tokenOut.readDecimals()); } /// @dev Checks ERC4337 userOp against the output of the command intent. @@ -258,17 +262,38 @@ contract IE { } /// @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; - if (token == "usdt" || token == "tether") return USDT; - if (token == "dai") return DAI; - if (token == "arb" || token == "arbitrum") return ARB; - if (token == "nani") return NANI; - if (token == "weth") return WETH; - if (token == "wbtc" || token == "btc" || token == "bitcoin") return WBTC; - if (token == "steth" || token == "wsteth" || token == "lido") return WSTETH; - if (token == "reth") return RETH; + function _returnTokenConstants(bytes32 token) + internal + pure + virtual + returns (address _token, uint8 _decimals) + { + if (token == "eth" || token == "ether") return (ETH, 18); + if (token == "usdc") return (USDC, 6); + if (token == "usdt" || token == "tether") return (USDT, 6); + if (token == "dai") return (DAI, 18); + if (token == "arb" || token == "arbitrum") return (ARB, 18); + if (token == "nani") return (NANI, 18); + if (token == "weth") return (WETH, 18); + if (token == "wbtc" || token == "btc" || token == "bitcoin") return (WBTC, 8); + if (token == "steth" || token == "wsteth" || token == "lido") return (WSTETH, 18); + if (token == "reth") return (RETH, 18); + } + + /// @dev Checks and returns popular pool pairs for WETH swaps. + function _returnPoolConstants(address token0, address token1) + internal + pure + virtual + returns (address pool) + { + if (token0 == WSTETH && token1 == WETH) return 0x35218a1cbaC5Bbc3E57fd9Bd38219D37571b3537; + if (token0 == WETH && token1 == RETH) return 0x09ba302A3f5ad2bF8853266e271b005A5b3716fe; + if (token0 == WETH && token1 == USDC) return 0xC6962004f452bE9203591991D15f6b388e09E8D0; + if (token0 == WETH && token1 == USDT) return 0x641C00A822e8b671738d32a431a4Fb6074E5c79d; + if (token0 == WETH && token1 == DAI) return 0xA961F0473dA4864C5eD28e00FcC53a3AAb056c1b; + if (token0 == WETH && token1 == ARB) return 0xC6F780497A95e246EB9449f5e4770916DCd6396A; + if (token0 == WBTC && token1 == WETH) return 0x2f5e87C9312fa29aed5c179E456625D79015299c; } /// ===================== COMMAND EXECUTION ===================== /// @@ -302,13 +327,15 @@ contract IE { payable virtual { - address _token = _returnTokenConstant(bytes32(bytes(token))); + (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(token))); if (_token == address(0)) _token = tokens[token]; (, address _to,) = whatIsTheAddressOf(to); if (_token == ETH) { - _to.safeTransferETH(_toUint(amount, 18)); + _to.safeTransferETH(_toUint(amount, decimals)); } else { - _token.safeTransferFrom(msg.sender, _to, _toUint(amount, _token.readDecimals())); + _token.safeTransferFrom( + msg.sender, _to, _toUint(amount, decimals != 0 ? decimals : _token.readDecimals()) + ); } } @@ -320,15 +347,18 @@ contract IE { string memory tokenOut ) public payable virtual { SwapInfo memory info; - info.tokenIn = _returnTokenConstant(bytes32(bytes(tokenIn))); + uint8 decimalsIn; + uint8 decimalsOut; + (info.tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn))); if (info.tokenIn == address(0)) info.tokenIn = tokens[tokenIn]; - info.tokenOut = _returnTokenConstant(bytes32(bytes(tokenOut))); + (info.tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut))); if (info.tokenOut == address(0)) info.tokenOut = tokens[tokenOut]; info.ETHIn = info.tokenIn == ETH; if (info.ETHIn) info.tokenIn = WETH; info.ETHOut = info.tokenOut == ETH; if (info.ETHOut) info.tokenOut = WETH; - info.amountIn = _toUint(amountIn, info.ETHIn ? 18 : info.tokenIn.readDecimals()); + info.amountIn = + _toUint(amountIn, decimalsIn != 0 ? decimalsIn : info.tokenIn.readDecimals()); if (info.amountIn >= 1 << 255) revert Overflow(); (address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut); (int256 amount0, int256 amount1) = ISwapRouter(pool).swap( @@ -340,7 +370,9 @@ contract IE { ); if ( uint256(-(zeroForOne ? amount1 : amount0)) - < _toUint(amountOutMinimum, info.ETHOut ? 18 : info.tokenOut.readDecimals()) + < _toUint( + amountOutMinimum, decimalsOut != 0 ? decimalsOut : info.tokenOut.readDecimals() + ) ) revert InsufficientSwap(); } @@ -392,26 +424,29 @@ contract IE { { if (tokenA < tokenB) zeroForOne = true; else (tokenA, tokenB) = (tokenB, tokenA); - address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee. - address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee. - address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. - address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. - // Initialize an array to hold the liquidity information for each pool. - SwapLiq[5] memory pools = [ - SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0), - SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0), - SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0), - SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0), - SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. - ]; - // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. - for (uint256 i; i != 4; ++i) { - if (pools[i].liq > pools[4].liq) { - pools[4].liq = pools[i].liq; - pools[4].pool = pools[i].pool; + pool = _returnPoolConstants(tokenA, tokenB); + if (pool == address(0)) { + address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee. + address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee. + address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. + address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. + // Initialize an array to hold the liquidity information for each pool. + SwapLiq[5] memory pools = [ + SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0), + SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0), + SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0), + SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0), + SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. + ]; + // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. + for (uint256 i; i != 4; ++i) { + if (pools[i].liq > pools[4].liq) { + pools[4].liq = pools[i].liq; + pools[4].pool = pools[i].pool; + } } + pool = pools[4].pool; // Return the top pool with likely best liquidity. } - pool = pools[4].pool; // Return the top pool with likely best liquidity. } /// @dev Computes the create2 deployment hash for a given token pair. @@ -487,11 +522,11 @@ contract IE { { (, address _name,) = whatIsTheAddressOf(name); string memory normalized = _lowercase(token); - address _token = _returnTokenConstant(bytes32(bytes(normalized))); + (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); if (_token == address(0)) _token = tokens[token]; bool isETH = _token == ETH; balance = isETH ? _name.balance : _token.balanceOf(_name); - balanceAdjusted = balance / 10 ** (isETH ? 18 : _token.readDecimals()); + balanceAdjusted = balance / 10 ** (decimals != 0 ? decimals : _token.readDecimals()); } /// @dev Returns the total supply of a named token. @@ -501,7 +536,8 @@ contract IE { virtual returns (uint256 supply, uint256 supplyAdjusted) { - address _token = _returnTokenConstant(bytes32(bytes(token))); + string memory normalized = _lowercase(token); + (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); if (_token == address(0)) _token = tokens[token]; assembly ("memory-safe") { mstore(0x00, 0x18160ddd) // `totalSupply()`. @@ -510,7 +546,7 @@ contract IE { } supply := mload(0x20) } - supplyAdjusted = supply / 10 ** _token.readDecimals(); + supplyAdjusted = supply / 10 ** (decimals != 0 ? decimals : _token.readDecimals()); } /// ====================== ENS VERIFICATION ====================== /// @@ -740,8 +776,8 @@ interface IExecutor { function execute(address, uint256, bytes calldata) external payable returns (bytes memory); } -/// @dev Simple Names interface for resolving L2 ENS ownership. -interface INames { +/// @dev Simple NAMI names interface for resolving L2 ENS ownership. +interface INAMI { function whatIsTheAddressOf(string calldata) external view From e2f462d17554c7cad363ee506f9cbfb6cf844eaf Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 22 Feb 2024 08:52:05 +0000 Subject: [PATCH 13/22] =?UTF-8?q?=E2=9A=A1=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 34 +++++++++++++++++----------------- src/IE.sol | 20 ++++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 034961e..3873e99 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,21 +1,21 @@ -IETest:testCommandDepositETH() (gas: 155729) -IETest:testCommandSendETH() (gas: 75426) -IETest:testCommandSendETHRawAddr() (gas: 75736) -IETest:testCommandStakeETH() (gas: 147889) -IETest:testCommandSwapDAI() (gas: 138559) -IETest:testCommandSwapETH() (gas: 138337) -IETest:testCommandSwapForETH() (gas: 145087) -IETest:testCommandSwapUSDC() (gas: 170689) -IETest:testCommandSwapUSDCForWBTC() (gas: 202216) -IETest:testCommandUnstakeETH() (gas: 263779) -IETest:testCommandWithdrawETH() (gas: 266482) -IETest:testDeploy() (gas: 2494592) +IETest:testCommandDepositETH() (gas: 155699) +IETest:testCommandSendETH() (gas: 75417) +IETest:testCommandSendETHRawAddr() (gas: 75727) +IETest:testCommandStakeETH() (gas: 147859) +IETest:testCommandSwapDAI() (gas: 138529) +IETest:testCommandSwapETH() (gas: 138307) +IETest:testCommandSwapForETH() (gas: 145057) +IETest:testCommandSwapUSDC() (gas: 171619) +IETest:testCommandSwapUSDCForWBTC() (gas: 202186) +IETest:testCommandUnstakeETH() (gas: 263713) +IETest:testCommandWithdrawETH() (gas: 266416) +IETest:testDeploy() (gas: 2490783) IETest:testENSNameOwnership() (gas: 45598) IETest:testIENameSetting() (gas: 8371) -IETest:testPreviewCommandSendDecimals() (gas: 107231) -IETest:testPreviewCommandSendUSDC() (gas: 66121) -IETest:testPreviewSend() (gas: 52155) -IETest:testPreviewSendCommandRawAddr() (gas: 65545) -IETest:testPreviewSendRawAddr() (gas: 28795) +IETest:testPreviewCommandSendDecimals() (gas: 107165) +IETest:testPreviewCommandSendUSDC() (gas: 66106) +IETest:testPreviewSend() (gas: 52140) +IETest:testPreviewSendCommandRawAddr() (gas: 65530) +IETest:testPreviewSendRawAddr() (gas: 28780) NAMITest:testFailRegister() (gas: 9465) NAMITest:testRegister() (gas: 55978) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 1e9e0f0..970bb92 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -203,7 +203,7 @@ contract IE { bytes memory executeCallData ) { - uint8 decimals; + uint256 decimals; (_token, decimals) = _returnTokenConstants(bytes32(bytes(token))); if (_token == address(0)) _token = tokens[token]; // Check storage. bool isETH = _token == ETH; // Memo whether the token is ETH or not. @@ -226,8 +226,8 @@ contract IE { virtual returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut) { - uint8 decimalsIn; - uint8 decimalsOut; + uint256 decimalsIn; + uint256 decimalsOut; (_tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn))); if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn]; (_tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut))); @@ -266,7 +266,7 @@ contract IE { internal pure virtual - returns (address _token, uint8 _decimals) + returns (address _token, uint256 _decimals) { if (token == "eth" || token == "ether") return (ETH, 18); if (token == "usdc") return (USDC, 6); @@ -327,7 +327,7 @@ contract IE { payable virtual { - (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(token))); + (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(token))); if (_token == address(0)) _token = tokens[token]; (, address _to,) = whatIsTheAddressOf(to); if (_token == ETH) { @@ -347,8 +347,8 @@ contract IE { string memory tokenOut ) public payable virtual { SwapInfo memory info; - uint8 decimalsIn; - uint8 decimalsOut; + uint256 decimalsIn; + uint256 decimalsOut; (info.tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn))); if (info.tokenIn == address(0)) info.tokenIn = tokens[tokenIn]; (info.tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut))); @@ -522,7 +522,7 @@ contract IE { { (, address _name,) = whatIsTheAddressOf(name); string memory normalized = _lowercase(token); - (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); + (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); if (_token == address(0)) _token = tokens[token]; bool isETH = _token == ETH; balance = isETH ? _name.balance : _token.balanceOf(_name); @@ -537,7 +537,7 @@ contract IE { returns (uint256 supply, uint256 supplyAdjusted) { string memory normalized = _lowercase(token); - (address _token, uint8 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); + (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); if (_token == address(0)) _token = tokens[token]; assembly ("memory-safe") { mstore(0x00, 0x18160ddd) // `totalSupply()`. @@ -698,7 +698,7 @@ contract IE { } /// @dev Convert string to decimalized numerical value. - function _toUint(string memory s, uint8 decimals) + function _toUint(string memory s, uint256 decimals) internal pure virtual From e162f3fad724b911c78799768a50f85c464a4f8c Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:38:36 +0000 Subject: [PATCH 14/22] =?UTF-8?q?=E2=9A=A1=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 30 +++++++++++++++--------------- src/IE.sol | 11 +++++------ 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 3873e99..58b920f 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,21 +1,21 @@ -IETest:testCommandDepositETH() (gas: 155699) -IETest:testCommandSendETH() (gas: 75417) -IETest:testCommandSendETHRawAddr() (gas: 75727) -IETest:testCommandStakeETH() (gas: 147859) -IETest:testCommandSwapDAI() (gas: 138529) -IETest:testCommandSwapETH() (gas: 138307) -IETest:testCommandSwapForETH() (gas: 145057) -IETest:testCommandSwapUSDC() (gas: 171619) -IETest:testCommandSwapUSDCForWBTC() (gas: 202186) -IETest:testCommandUnstakeETH() (gas: 263713) -IETest:testCommandWithdrawETH() (gas: 266416) -IETest:testDeploy() (gas: 2490783) +IETest:testCommandDepositETH() (gas: 155644) +IETest:testCommandSendETH() (gas: 75368) +IETest:testCommandSendETHRawAddr() (gas: 75678) +IETest:testCommandStakeETH() (gas: 147804) +IETest:testCommandSwapDAI() (gas: 138510) +IETest:testCommandSwapETH() (gas: 138204) +IETest:testCommandSwapForETH() (gas: 145038) +IETest:testCommandSwapUSDC() (gas: 171546) +IETest:testCommandSwapUSDCForWBTC() (gas: 202135) +IETest:testCommandUnstakeETH() (gas: 263603) +IETest:testCommandWithdrawETH() (gas: 266306) +IETest:testDeploy() (gas: 2577214) IETest:testENSNameOwnership() (gas: 45598) IETest:testIENameSetting() (gas: 8371) -IETest:testPreviewCommandSendDecimals() (gas: 107165) -IETest:testPreviewCommandSendUSDC() (gas: 66106) +IETest:testPreviewCommandSendDecimals() (gas: 107067) +IETest:testPreviewCommandSendUSDC() (gas: 66057) IETest:testPreviewSend() (gas: 52140) -IETest:testPreviewSendCommandRawAddr() (gas: 65530) +IETest:testPreviewSendCommandRawAddr() (gas: 65481) IETest:testPreviewSendRawAddr() (gas: 28780) NAMITest:testFailRegister() (gas: 9465) NAMITest:testRegister() (gas: 55978) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index 970bb92..37b8727 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -521,11 +521,10 @@ contract IE { returns (uint256 balance, uint256 balanceAdjusted) { (, address _name,) = whatIsTheAddressOf(name); - string memory normalized = _lowercase(token); - (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); + (address _token, uint256 decimals) = + _returnTokenConstants(bytes32(bytes(_lowercase(token)))); if (_token == address(0)) _token = tokens[token]; - bool isETH = _token == ETH; - balance = isETH ? _name.balance : _token.balanceOf(_name); + balance = _token == ETH ? _name.balance : _token.balanceOf(_name); balanceAdjusted = balance / 10 ** (decimals != 0 ? decimals : _token.readDecimals()); } @@ -536,8 +535,8 @@ contract IE { virtual returns (uint256 supply, uint256 supplyAdjusted) { - string memory normalized = _lowercase(token); - (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(normalized))); + (address _token, uint256 decimals) = + _returnTokenConstants(bytes32(bytes(_lowercase(token)))); if (_token == address(0)) _token = tokens[token]; assembly ("memory-safe") { mstore(0x00, 0x18160ddd) // `totalSupply()`. From 44717af19e86fee469275f05c90b9edc2af129a2 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:46:30 +0000 Subject: [PATCH 15/22] =?UTF-8?q?=F0=9F=A5=A2=20~~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 58b920f..f06e7f4 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -5,8 +5,8 @@ IETest:testCommandStakeETH() (gas: 147804) IETest:testCommandSwapDAI() (gas: 138510) IETest:testCommandSwapETH() (gas: 138204) IETest:testCommandSwapForETH() (gas: 145038) -IETest:testCommandSwapUSDC() (gas: 171546) -IETest:testCommandSwapUSDCForWBTC() (gas: 202135) +IETest:testCommandSwapUSDC() (gas: 170552) +IETest:testCommandSwapUSDCForWBTC() (gas: 202143) IETest:testCommandUnstakeETH() (gas: 263603) IETest:testCommandWithdrawETH() (gas: 266306) IETest:testDeploy() (gas: 2577214) From a39cf518913f073cb8e4314c6d73792d4d49441b Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Mon, 25 Mar 2024 08:39:25 +0000 Subject: [PATCH 16/22] =?UTF-8?q?=E2=9C=A6=201.2.2=20-=20Governed=20Pairs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 42 +-- docs/src/SUMMARY.md | 4 +- docs/src/src/IE.sol/contract.IE.md | 241 ++++++++++----- docs/src/src/IE.sol/interface.IExecutor.md | 2 +- docs/src/src/IE.sol/interface.INAMI.md | 14 + docs/src/src/IE.sol/interface.ISwapRouter.md | 2 +- docs/src/src/IE.sol/interface.IToken.md | 2 +- docs/src/src/NAMI.sol/contract.NAMI.md | 296 +++++++++++++++++++ docs/src/src/NAMI.sol/interface.IToken.md | 21 ++ docs/src/src/README.md | 4 +- foundry.toml | 4 +- lib/forge-std | 2 +- lib/solady | 2 +- src/IE.sol | 60 ++-- 14 files changed, 566 insertions(+), 130 deletions(-) create mode 100644 docs/src/src/IE.sol/interface.INAMI.md create mode 100644 docs/src/src/NAMI.sol/contract.NAMI.md create mode 100644 docs/src/src/NAMI.sol/interface.IToken.md diff --git a/.gas-snapshot b/.gas-snapshot index f06e7f4..57c0580 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,21 +1,21 @@ -IETest:testCommandDepositETH() (gas: 155644) -IETest:testCommandSendETH() (gas: 75368) -IETest:testCommandSendETHRawAddr() (gas: 75678) -IETest:testCommandStakeETH() (gas: 147804) -IETest:testCommandSwapDAI() (gas: 138510) -IETest:testCommandSwapETH() (gas: 138204) -IETest:testCommandSwapForETH() (gas: 145038) -IETest:testCommandSwapUSDC() (gas: 170552) -IETest:testCommandSwapUSDCForWBTC() (gas: 202143) -IETest:testCommandUnstakeETH() (gas: 263603) -IETest:testCommandWithdrawETH() (gas: 266306) -IETest:testDeploy() (gas: 2577214) -IETest:testENSNameOwnership() (gas: 45598) -IETest:testIENameSetting() (gas: 8371) -IETest:testPreviewCommandSendDecimals() (gas: 107067) -IETest:testPreviewCommandSendUSDC() (gas: 66057) -IETest:testPreviewSend() (gas: 52140) -IETest:testPreviewSendCommandRawAddr() (gas: 65481) -IETest:testPreviewSendRawAddr() (gas: 28780) -NAMITest:testFailRegister() (gas: 9465) -NAMITest:testRegister() (gas: 55978) \ No newline at end of file +IETest:testCommandDepositETH() (gas: 155593) +IETest:testCommandSendETH() (gas: 75320) +IETest:testCommandSendETHRawAddr() (gas: 75733) +IETest:testCommandStakeETH() (gas: 147756) +IETest:testCommandSwapDAI() (gas: 138445) +IETest:testCommandSwapETH() (gas: 138173) +IETest:testCommandSwapForETH() (gas: 145324) +IETest:testCommandSwapUSDC() (gas: 171439) +IETest:testCommandSwapUSDCForWBTC() (gas: 205050) +IETest:testCommandUnstakeETH() (gas: 263540) +IETest:testCommandWithdrawETH() (gas: 266243) +IETest:testDeploy() (gas: 2562769) +IETest:testENSNameOwnership() (gas: 48402) +IETest:testIENameSetting() (gas: 11105) +IETest:testPreviewCommandSendDecimals() (gas: 108639) +IETest:testPreviewCommandSendUSDC() (gas: 67684) +IETest:testPreviewSend() (gas: 53778) +IETest:testPreviewSendCommandRawAddr() (gas: 67129) +IETest:testPreviewSendRawAddr() (gas: 30521) +NAMITest:testFailRegister() (gas: 9471) +NAMITest:testRegister() (gas: 58853) \ No newline at end of file diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 1ff6b86..80d45d8 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -2,7 +2,9 @@ - [Home](README.md) # src - [IE](src/IE.sol/contract.IE.md) - - [IENSHelper](src/IE.sol/interface.IENSHelper.md) - [IToken](src/IE.sol/interface.IToken.md) - [IExecutor](src/IE.sol/interface.IExecutor.md) + - [INAMI](src/IE.sol/interface.INAMI.md) - [ISwapRouter](src/IE.sol/interface.ISwapRouter.md) + - [NAMI](src/NAMI.sol/contract.NAMI.md) + - [IToken](src/NAMI.sol/interface.IToken.md) diff --git a/docs/src/src/IE.sol/contract.IE.md b/docs/src/src/IE.sol/contract.IE.md index 4dd61b1..5f8d5cb 100644 --- a/docs/src/src/IE.sol/contract.IE.md +++ b/docs/src/src/IE.sol/contract.IE.md @@ -1,5 +1,5 @@ # IE -[Git Source](https://github.com/NaniDAO/ie/blob/0e07baacb225bae6af6d37dff531a21dd06e0665/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) @@ -27,7 +27,7 @@ address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; ```solidity -address internal constant NANI = 0x00000000000025824328358250920B271f348690; +address internal constant NANI = 0x000000000000C6A645b0E51C9eCAA4CA580Ed8e8; ``` @@ -45,7 +45,7 @@ address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; ```solidity -address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; +address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1; ``` @@ -54,7 +54,7 @@ address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2; ```solidity -address internal constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599; +address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f; ``` @@ -63,7 +63,7 @@ address internal constant WBTC = 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599; ```solidity -address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; +address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831; ``` @@ -72,7 +72,7 @@ address internal constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48; ```solidity -address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7; +address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9; ``` @@ -81,25 +81,34 @@ address internal constant USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7; ```solidity -address internal constant DAI = 0x6B175474E89094C44Da98b954EedeAC495271d0F; +address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1; ``` -### ENS_REGISTRY -*ENS fallback registry contract.* +### ARB +*The Arbitrum DAO governance token address.* ```solidity -IENSHelper internal constant ENS_REGISTRY = IENSHelper(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e); +address internal constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548; ``` -### ENS_WRAPPER -*ENS name wrapper token contract.* +### WSTETH +*The Lido Wrapped Staked ETH token address.* ```solidity -IENSHelper internal constant ENS_WRAPPER = IENSHelper(0xD4416b13d2b3a9aBae7AcD5D6C2BbDBE25686401); +address internal constant WSTETH = 0x5979D7b546E38E414F7E9822514be443A4800529; +``` + + +### RETH +*The Rocket Pool Staked ETH token address.* + + +```solidity +address internal constant RETH = 0xEC70Dcb4A1EFa46b8F2D97C310C9c4790ba5ffA8; ``` @@ -141,13 +150,12 @@ uint160 internal constant MAX_SQRT_RATIO_MINUS_ONE = ``` -### ASCII_MAP -*String mapping for ENSAsciiNormalizer logic.* +### NAMI +*The NAMI naming system on Arbitrum.* ```solidity -bytes internal constant ASCII_MAP = - hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; +INAMI internal constant NAMI = INAMI(0x000000006641B4C250AEA6B62A1e0067D300697a); ``` @@ -162,16 +170,12 @@ mapping(string name => address) public tokens; ``` -### _idnamap -*Each index in idnamap refers to an ascii code point. -If idnamap[char] > 2, char maps to a valid ascii character. -Otherwise, idna[char] returns Rule.DISALLOWED or Rule.VALID. -Modified from ENSAsciiNormalizer deployed by royalfork.eth -(0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758).* +### pairs +*DAO-governed token swap pool routing.* ```solidity -bytes1[] internal _idnamap; +mapping(address token0 => mapping(address token1 => address)) public pairs; ``` @@ -180,7 +184,7 @@ bytes1[] internal _idnamap; ======================== CONSTRUCTOR ======================== /// -*Constructs this IE with `ASCII_MAP`.* +*Constructs this IE on the Arbitrum L2 of Ethereum.* ```solidity @@ -191,9 +195,9 @@ constructor() payable; ====================== COMMAND PREVIEW ====================== /// -Preview natural language smart contract command. +*Preview natural language smart contract command. The `send` syntax uses ENS naming: 'send vitalik 20 DAI'. -`swap` syntax uses common format: 'swap 100 DAI for WETH'. +`swap` syntax uses common format: 'swap 100 DAI for WETH'.* ```solidity @@ -204,6 +208,7 @@ function previewCommand(string calldata intent) returns ( address to, uint256 amount, + uint256 minAmountOut, address token, bytes memory callData, bytes memory executeCallData @@ -235,11 +240,16 @@ function previewSend(string memory to, string memory amount, string memory token ```solidity -function previewSwap(string memory amountIn, string memory tokenIn, string memory tokenOut) +function previewSwap( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut +) public view virtual - returns (uint256 _amountIn, address _tokenIn, address _tokenOut); + returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut); ``` ### checkUserOp @@ -268,13 +278,30 @@ function checkPackedUserOp(string calldata intent, PackedUserOperation calldata returns (bool); ``` -### _returnTokenConstant +### _returnTokenConstants *Checks and returns the canonical token address constant for a matched intent string.* ```solidity -function _returnTokenConstant(bytes32 token) internal view virtual returns (address _token); +function _returnTokenConstants(bytes32 token) + internal + pure + virtual + returns (address _token, uint256 _decimals); +``` + +### _returnPoolConstants + +*Checks and returns popular pool pairs for WETH swaps.* + + +```solidity +function _returnPoolConstants(address token0, address token1) + internal + pure + virtual + returns (address pool); ``` ### command @@ -303,10 +330,12 @@ function send(string memory to, string memory amount, string memory token) publi ```solidity -function swap(string memory amountIn, string memory tokenIn, string memory tokenOut) - public - payable - virtual; +function swap( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut +) public payable virtual; ``` ### fallback @@ -321,7 +350,8 @@ fallback() external payable virtual; ### _computePoolAddress -*Computes the create2 address for given token pair.* +*Computes the create2 address for given token pair. +note: This process checks all available pools for price.* ```solidity @@ -334,7 +364,7 @@ function _computePoolAddress(address tokenA, address tokenB) ### _computePairHash -*Computes the create2 deployment hash for given token pair.* +*Computes the create2 deployment hash for a given token pair.* ```solidity @@ -363,9 +393,23 @@ function _wrapETH(uint256 amount) internal virtual; function _unwrapETH(uint256 amount) internal virtual; ``` +### _balanceOf + +*Returns the amount of ERC20 `token` owned by `account`.* + + +```solidity +function _balanceOf(address token, address account) + internal + view + virtual + returns (uint256 amount); +``` + ### receive -*ETH receiver fallback.* +*ETH receiver fallback. +Only canonical WETH can call.* ```solidity @@ -415,28 +459,6 @@ function whatIsTheAddressOf(string memory name) returns (address owner, address receiver, bytes32 node); ``` -### _namehash - -*Computes an ENS domain namehash.* - - -```solidity -function _namehash(string memory domain) internal view virtual returns (bytes32 node); -``` - -### _labelhash - -*Computes an ENS domain labelhash given its start and end.* - - -```solidity -function _labelhash(string memory domain, uint256 start, uint256 end) - internal - pure - virtual - returns (bytes32 hash); -``` - ### setName ========================= GOVERNANCE ========================= /// @@ -457,6 +479,15 @@ function setName(address token, string calldata name) public payable virtual; function setNameAndTicker(address token) public payable virtual; ``` +### setPair + +*Sets a public pool `pair` for swapping. Governed by DAO.* + + +```solidity +function setPair(address tokenA, address tokenB, address pair) public payable virtual; +``` + ### _lowercase ===================== STRING OPERATIONS ===================== /// @@ -505,7 +536,12 @@ function _extractSwap(string memory normalizedIntent) internal pure virtual - returns (string memory amountIn, string memory tokenIn, string memory tokenOut); + returns ( + string memory amountIn, + string memory amountOutMinimum, + string memory tokenIn, + string memory tokenOut + ); ``` ### _split @@ -521,19 +557,47 @@ function _split(string memory base, bytes1 delimiter) returns (string[] memory parts); ``` -### _stringToUint +### _toUint *Convert string to decimalized numerical value.* ```solidity -function _stringToUint(string memory s, uint8 decimals) +function _toUint(string memory s, uint256 decimals) internal pure virtual returns (uint256 result); ``` +### _toAddress + +*Converts a hexadecimal string to its `address` representation. +Modified from Stack (https://ethereum.stackexchange.com/a/156916).* + + +```solidity +function _toAddress(string memory s) internal pure virtual returns (address addr); +``` + +### _hexStringToAddress + +*Converts a hexadecimal string into its bytes representation.* + + +```solidity +function _hexStringToAddress(string memory s) internal pure virtual returns (bytes memory r); +``` + +### _fromHexChar + +*Converts a single hexadecimal character into its numerical value.* + + +```solidity +function _fromHexChar(uint8 c) internal pure virtual returns (uint8 result); +``` + ## Events ### NameSet =========================== EVENTS =========================== /// @@ -545,6 +609,14 @@ function _stringToUint(string memory s, uint8 decimals) event NameSet(address indexed token, string name); ``` +### PairSet +*Logs the registration of a token swap pool pair route.* + + +```solidity +event PairSet(address indexed token0, address indexed token1, address indexed pair); +``` + ## Errors ### Overflow ======================= LIBRARY USAGE ======================= /// @@ -561,14 +633,6 @@ event NameSet(address indexed token, string name); error Overflow(); ``` -### Unauthorized -*Caller fails.* - - -```solidity -error Unauthorized(); -``` - ### InvalidSwap *0-liquidity.* @@ -593,6 +657,14 @@ error InvalidSyntax(); error InvalidCharacter(); ``` +### InsufficientSwap +*Insufficient swap output.* + + +```solidity +error InsufficientSwap(); +``` + ## Structs ### UserOperation ========================== STRUCTS ========================== /// @@ -617,7 +689,7 @@ struct UserOperation { ``` ### PackedUserOperation -*The packed ERC4337 user operation (userOp) struct.* +*The packed ERC4337 userOp struct.* ```solidity @@ -634,17 +706,28 @@ struct PackedUserOperation { } ``` -## Enums -### Rule -=========================== ENUMS =========================== /// +### SwapInfo +*The `swap` command information struct.* + + +```solidity +struct SwapInfo { + address tokenIn; + address tokenOut; + uint256 amountIn; + bool ETHIn; + bool ETHOut; +} +``` -*ENSAsciiNormalizer rules.* +### SwapLiq +*The `swap` pool liquidity struct.* ```solidity -enum Rule { - DISALLOWED, - VALID +struct SwapLiq { + address pool; + uint256 liq; } ``` diff --git a/docs/src/src/IE.sol/interface.IExecutor.md b/docs/src/src/IE.sol/interface.IExecutor.md index a0c6084..aae5c7d 100644 --- a/docs/src/src/IE.sol/interface.IExecutor.md +++ b/docs/src/src/IE.sol/interface.IExecutor.md @@ -1,5 +1,5 @@ # IExecutor -[Git Source](https://github.com/NaniDAO/ie/blob/0e07baacb225bae6af6d37dff531a21dd06e0665/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) Simple calldata executor interface. diff --git a/docs/src/src/IE.sol/interface.INAMI.md b/docs/src/src/IE.sol/interface.INAMI.md new file mode 100644 index 0000000..01a6428 --- /dev/null +++ b/docs/src/src/IE.sol/interface.INAMI.md @@ -0,0 +1,14 @@ +# INAMI +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) + +*Simple NAMI names interface for resolving L2 ENS ownership.* + + +## Functions +### whatIsTheAddressOf + + +```solidity +function whatIsTheAddressOf(string calldata) external view returns (address, address, bytes32); +``` + diff --git a/docs/src/src/IE.sol/interface.ISwapRouter.md b/docs/src/src/IE.sol/interface.ISwapRouter.md index 65f1fef..52ac3b4 100644 --- a/docs/src/src/IE.sol/interface.ISwapRouter.md +++ b/docs/src/src/IE.sol/interface.ISwapRouter.md @@ -1,5 +1,5 @@ # ISwapRouter -[Git Source](https://github.com/NaniDAO/ie/blob/0e07baacb225bae6af6d37dff531a21dd06e0665/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) *Simple Uniswap V3 swapping interface.* diff --git a/docs/src/src/IE.sol/interface.IToken.md b/docs/src/src/IE.sol/interface.IToken.md index 0128bea..cc378b4 100644 --- a/docs/src/src/IE.sol/interface.IToken.md +++ b/docs/src/src/IE.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/0e07baacb225bae6af6d37dff531a21dd06e0665/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) *Simple token transfer interface.* diff --git a/docs/src/src/NAMI.sol/contract.NAMI.md b/docs/src/src/NAMI.sol/contract.NAMI.md new file mode 100644 index 0000000..c82266b --- /dev/null +++ b/docs/src/src/NAMI.sol/contract.NAMI.md @@ -0,0 +1,296 @@ +# NAMI +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/NAMI.sol) + +**Author:** +nani.eth (https://github.com/NaniDAO/ie) + +A contract for managing ENS domain name ownership and resolution on Arbitrum L2. + +*Provides logic for registering names, verifying ownership, and resolving addresses.* + + +## State Variables +### DAO +========================= CONSTANTS ========================= /// + +*The governing DAO address.* + + +```solidity +address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; +``` + + +### L1_DEPLOYER +*L1_DEPLOYER represents the address responsible for deploying ENS proxy contracts on Ethereum Layer 1. +This address is typically used in conjunction with create2 operations for deterministic deployment.* + + +```solidity +address internal constant L1_DEPLOYER = 0x000000008B009D81C933a72545Ed7500cbB5B9D1; +``` + + +### L2_DEPLOYER +*L2_DEPLOYER denotes the address that performs bridged ENS deployments on Arbitrum Layer 2. +Similar to L1_DEPLOYER, it's crucial for deterministic deployment but within the Arbitrum L2 context.* + + +```solidity +address internal constant L2_DEPLOYER = 0x3fE38087A94903A9D946fa1915e1772fe611000f; +``` + + +### IMPLEMENTATION +*IMPLEMENTATION refers to the address of the contract serving as the implementation for ENS proxies.* + + +```solidity +address internal constant IMPLEMENTATION = 0xEBb49317E567a40cF468c409409aD59a8f67ddE6; +``` + + +### COUNTERPART_GATEWAY +*COUNTERPART_GATEWAY is the address of the gateway contract on Arbitrum Layer 2 that pairs with a corresponding +gateway on Ethereum Layer 1. This gateway facilitates the bridging of tokens between L1 and L2, managing the +lock/mint and burn/release mechanics across layers.* + + +```solidity +address internal constant COUNTERPART_GATEWAY = 0x09e9222E96E7B4AE2a407B98d48e330053351EEe; +``` + + +### L2_HASH +*L2_HASH is a unique identifier, used as part of the create2 address computation for contracts on Arbitrum Layer 2.* + + +```solidity +bytes32 internal constant L2_HASH = + 0x4b11cb57b978697e0aec0c18581326376d6463fd3f6699cbe78ee5935617082d; +``` + + +### ASCII_MAP +*String mapping for `ENSAsciiNormalizer` logic.* + + +```solidity +bytes internal constant ASCII_MAP = + hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500"; +``` + + +### _idnamap +========================== STORAGE ========================== /// + +*Each index in idnamap refers to an ascii code point. +If idnamap[char] > 2, char maps to a valid ascii character. +Otherwise, idna[char] returns Rule.DISALLOWED or Rule.VALID. +Modified from `ENSAsciiNormalizer` deployed by royalfork.eth +(0x4A5cae3EC0b144330cf1a6CeAD187D8F6B891758).* + + +```solidity +bytes1[] internal _idnamap; +``` + + +### _owners +*Internal mapping of registered node owners.* + + +```solidity +mapping(bytes32 node => Ownership) internal _owners; +``` + + +## Functions +### constructor + +======================== CONSTRUCTOR ======================== /// + +*Constructs this IE with `ASCII_MAP`.* + + +```solidity +constructor() payable; +``` + +### whatIsTheAddressOf + +====================== ENS VERIFICATION ====================== /// + +*Returns ENS name ownership information.* + + +```solidity +function whatIsTheAddressOf(string calldata name) + public + view + virtual + returns (address _owner, address _receiver, bytes32 _node); +``` + +### _namehash + +*Computes an ENS domain namehash.* + + +```solidity +function _namehash(string memory domain) internal view virtual returns (bytes32 node); +``` + +### _labelhash + +*Computes an ENS domain labelhash given its start and end.* + + +```solidity +function _labelhash(string memory domain, uint256 start, uint256 end) + internal + pure + virtual + returns (bytes32 hash); +``` + +### register + +======================== REGISTRATION ======================== /// + +*Registers a name node under an owner. ENS L1 node must be bridged.* + + +```solidity +function register(address _owner, bytes32 _node) public payable virtual; +``` + +### registerSub + +*Registers a name subnode under an owner. Only the DAO may call this function.* + + +```solidity +function registerSub(address _owner, string calldata _subname) public payable virtual; +``` + +### owner + +====================== OWNERSHIP LOGIC ====================== /// + +*Returns the registered owner of a given ENS L1 node. Must be bridged. +note: Alternatively, NAMI provides subdomains issued under `nani.eth` node.* + + +```solidity +function owner(bytes32 _node) public view virtual returns (address _owner); +``` + +### isOwner + +*Checks if an address is the owner of a given ENS L1 node represented as `l2Token`. +note: NAMI operates under the assumption that the proper owner-receiver holds majority.* + + +```solidity +function isOwner(address _owner, bytes32 _node) public view virtual returns (bool); +``` + +### predictDeterministicAddresses + +*Returns the deterministic create2 addresses for ENS node tokens on L1 & L2.* + + +```solidity +function predictDeterministicAddresses(bytes32 _node) + public + pure + virtual + returns (address l1Token, address l2Token); +``` + +### _predictDeterministicAddress + +*Returns the predicted address on L1 using CWIA pattern.* + + +```solidity +function _predictDeterministicAddress(bytes32 hash, bytes32 salt) + internal + pure + virtual + returns (address predicted); +``` + +### _initCodeHash + +*Returns the initCodeHash for the predicted address on L1 using CWIA pattern.* + + +```solidity +function _initCodeHash(bytes memory data) internal pure virtual returns (bytes32 hash); +``` + +### _calculateL2TokenAddress + +*Returns the predicted `l2Token` address using Arbitrum create2 bridge preview methods on `l1Token`.* + + +```solidity +function _calculateL2TokenAddress(address l1Token) + internal + pure + virtual + returns (address l2Token); +``` + +## Events +### Registered +=========================== EVENTS =========================== /// + +*Logs the registration of a name node into ownership.* + + +```solidity +event Registered(bytes32 indexed node, Ownership ownership); +``` + +## Errors +### Unregistered +======================= CUSTOM ERRORS ======================= /// + +*Unregistered.* + + +```solidity +error Unregistered(); +``` + +## Structs +### Ownership +========================== STRUCTS ========================== /// + +*The name node ownership information struct.* + + +```solidity +struct Ownership { + address owner; + bool subnode; +} +``` + +## Enums +### Rule +=========================== ENUMS =========================== /// + +*`ENSAsciiNormalizer` rules.* + + +```solidity +enum Rule { + DISALLOWED, + VALID +} +``` + diff --git a/docs/src/src/NAMI.sol/interface.IToken.md b/docs/src/src/NAMI.sol/interface.IToken.md new file mode 100644 index 0000000..9c82615 --- /dev/null +++ b/docs/src/src/NAMI.sol/interface.IToken.md @@ -0,0 +1,21 @@ +# IToken +[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/NAMI.sol) + +*Simple token balance & supply interface.* + + +## Functions +### totalSupply + + +```solidity +function totalSupply() external view returns (uint256); +``` + +### balanceOf + + +```solidity +function balanceOf(address) external view returns (uint256); +``` + diff --git a/docs/src/src/README.md b/docs/src/src/README.md index 55ca2ad..79936c6 100644 --- a/docs/src/src/README.md +++ b/docs/src/src/README.md @@ -2,7 +2,9 @@ # Contents - [IE](IE.sol/contract.IE.md) -- [IENSHelper](IE.sol/interface.IENSHelper.md) - [IToken](IE.sol/interface.IToken.md) - [IExecutor](IE.sol/interface.IExecutor.md) +- [INAMI](IE.sol/interface.INAMI.md) - [ISwapRouter](IE.sol/interface.ISwapRouter.md) +- [NAMI](NAMI.sol/contract.NAMI.md) +- [IToken](NAMI.sol/interface.IToken.md) diff --git a/foundry.toml b/foundry.toml index 84da3d0..22929b7 100644 --- a/foundry.toml +++ b/foundry.toml @@ -1,6 +1,6 @@ [profile.default] -solc_version = "0.8.24" -evm_version = "shanghai" +solc_version = "0.8.25" +evm_version = "cancun" optimizer = true optimizer_runs = 9_999_999 diff --git a/lib/forge-std b/lib/forge-std index 1d0766b..bb4ceea 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit 1d0766bc5d814f117c7b1e643828f7d85024fb51 +Subproject commit bb4ceea94d6f10eeb5b41dc2391c6c8bf8e734ef diff --git a/lib/solady b/lib/solady index 1372606..3a4e387 160000 --- a/lib/solady +++ b/lib/solady @@ -1 +1 @@ -Subproject commit 1372606383445c0a247e6c58eb255a529734258a +Subproject commit 3a4e38702e1a87f53c21f6afaf70eb8738535560 diff --git a/src/IE.sol b/src/IE.sol index 37b8727..019a369 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -10,7 +10,7 @@ import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol"; /// @dev V1 simulates typical commands (sending and swapping tokens) and includes execution. /// IE also has a workflow to verify the intent of ERC4337 account userOps against calldata. /// @author nani.eth (https://github.com/NaniDAO/ie) -/// @custom:version 1.1.1 +/// @custom:version 1.2.0 contract IE { /// ======================= LIBRARY USAGE ======================= /// @@ -42,6 +42,9 @@ contract IE { /// @dev Logs the registration of a token name. event NameSet(address indexed token, string name); + /// @dev Logs the registration of a token swap pool pair route. + event PairSet(address indexed token0, address indexed token1, address indexed pair); + /// ========================== STRUCTS ========================== /// /// @dev The ERC4337 user operation (userOp) struct. @@ -67,7 +70,7 @@ contract IE { bytes callData; bytes32 accountGasLimits; uint256 preVerificationGas; - bytes32 gasFees; // `maxPriorityFee` and `maxFeePerGas`. + bytes32 gasFees; bytes paymasterAndData; bytes signature; } @@ -93,7 +96,7 @@ contract IE { address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA; /// @dev The NANI token address. - address internal constant NANI = 0x00000000000025824328358250920B271f348690; + address internal constant NANI = 0x000000000000C6A645b0E51C9eCAA4CA580Ed8e8; /// @dev The conventional ERC7528 ETH address. address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; @@ -144,6 +147,9 @@ contract IE { /// @dev DAO-governed token address naming. mapping(string name => address) public tokens; + /// @dev DAO-governed token swap pool routing. + mapping(address token0 => mapping(address token1 => address)) public pairs; + /// ======================== CONSTRUCTOR ======================== /// /// @dev Constructs this IE on the Arbitrum L2 of Ethereum. @@ -426,26 +432,29 @@ contract IE { else (tokenA, tokenB) = (tokenB, tokenA); pool = _returnPoolConstants(tokenA, tokenB); if (pool == address(0)) { - address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee. - address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee. - address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. - address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. - // Initialize an array to hold the liquidity information for each pool. - SwapLiq[5] memory pools = [ - SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0), - SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0), - SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0), - SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0), - SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. - ]; - // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. - for (uint256 i; i != 4; ++i) { - if (pools[i].liq > pools[4].liq) { - pools[4].liq = pools[i].liq; - pools[4].pool = pools[i].pool; + pool = pairs[tokenA][tokenB]; + if (pool == address(0)) { + address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee. + address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee. + address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee. + address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee. + // Initialize an array to hold the liquidity information for each pool. + SwapLiq[5] memory pools = [ + SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0), + SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0), + SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0), + SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0), + SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison. + ]; + // Iterate through the array to find the top pool with the highest liquidity in `tokenA`. + for (uint256 i; i != 4; ++i) { + if (pools[i].liq > pools[4].liq) { + pools[4].liq = pools[i].liq; + pools[4].pool = pools[i].pool; + } } + pool = pools[4].pool; // Return the top pool with likely best liquidity. } - pool = pools[4].pool; // Return the top pool with likely best liquidity. } } @@ -584,6 +593,15 @@ contract IE { emit NameSet(tokens[normalizedSymbol] = token, normalizedSymbol); } + /// @dev Sets a public pool `pair` for swapping. Governed by DAO. + function setPair(address tokenA, address tokenB, address pair) public payable virtual { + assembly ("memory-safe") { + if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat. + } + if (tokenB < tokenA) (tokenA, tokenB) = (tokenB, tokenA); + emit PairSet(tokenA, tokenB, pairs[tokenA][tokenB] = pair); + } + /// ===================== STRING OPERATIONS ===================== /// /// @dev Returns copy of string in lowercase. From f31f555ae821c0432ed1c7cd6e93b1e7bba98a37 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Mon, 25 Mar 2024 12:35:44 +0000 Subject: [PATCH 17/22] =?UTF-8?q?=E2=9A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 20 ++++++++++---------- src/IE.sol | 12 +++++------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 57c0580..972d6c5 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,15 +1,15 @@ -IETest:testCommandDepositETH() (gas: 155593) +IETest:testCommandDepositETH() (gas: 155576) IETest:testCommandSendETH() (gas: 75320) IETest:testCommandSendETHRawAddr() (gas: 75733) -IETest:testCommandStakeETH() (gas: 147756) -IETest:testCommandSwapDAI() (gas: 138445) -IETest:testCommandSwapETH() (gas: 138173) -IETest:testCommandSwapForETH() (gas: 145324) -IETest:testCommandSwapUSDC() (gas: 171439) -IETest:testCommandSwapUSDCForWBTC() (gas: 205050) -IETest:testCommandUnstakeETH() (gas: 263540) -IETest:testCommandWithdrawETH() (gas: 266243) -IETest:testDeploy() (gas: 2562769) +IETest:testCommandStakeETH() (gas: 146774) +IETest:testCommandSwapDAI() (gas: 129775) +IETest:testCommandSwapETH() (gas: 138143) +IETest:testCommandSwapForETH() (gas: 136968) +IETest:testCommandSwapUSDC() (gas: 171451) +IETest:testCommandSwapUSDCForWBTC() (gas: 196378) +IETest:testCommandUnstakeETH() (gas: 263506) +IETest:testCommandWithdrawETH() (gas: 266209) +IETest:testDeploy() (gas: 2563979) IETest:testENSNameOwnership() (gas: 48402) IETest:testIENameSetting() (gas: 11105) IETest:testPreviewCommandSendDecimals() (gas: 108639) diff --git a/src/IE.sol b/src/IE.sol index 019a369..7b857de 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -42,8 +42,8 @@ contract IE { /// @dev Logs the registration of a token name. event NameSet(address indexed token, string name); - /// @dev Logs the registration of a token swap pool pair route. - event PairSet(address indexed token0, address indexed token1, address indexed pair); + /// @dev Logs the registration of a token swap pool pair route on Uniswap V3. + event PairSet(address indexed token0, address indexed token1, address pair); /// ========================== STRUCTS ========================== /// @@ -147,7 +147,7 @@ contract IE { /// @dev DAO-governed token address naming. mapping(string name => address) public tokens; - /// @dev DAO-governed token swap pool routing. + /// @dev DAO-governed token swap pool routing on Uniswap V3. mapping(address token0 => mapping(address token1 => address)) public pairs; /// ======================== CONSTRUCTOR ======================== /// @@ -279,11 +279,11 @@ contract IE { if (token == "usdt" || token == "tether") return (USDT, 6); if (token == "dai") return (DAI, 18); if (token == "arb" || token == "arbitrum") return (ARB, 18); - if (token == "nani") return (NANI, 18); if (token == "weth") return (WETH, 18); if (token == "wbtc" || token == "btc" || token == "bitcoin") return (WBTC, 8); if (token == "steth" || token == "wsteth" || token == "lido") return (WSTETH, 18); if (token == "reth") return (RETH, 18); + if (token == "nani") return (NANI, 18); } /// @dev Checks and returns popular pool pairs for WETH swaps. @@ -730,9 +730,7 @@ contract IE { result = result * 10 + uint8(b[i]) - 48; if (hasDecimal) { ++decimalPlaces; - if (decimalPlaces > decimals) { - break; - } + if (decimalPlaces > decimals) break; } } else if (b[i] == "." && !hasDecimal) { hasDecimal = true; From 66bd764378eec8970bccde40d88e1c58a0ad524e Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 28 Mar 2024 05:32:13 +0000 Subject: [PATCH 18/22] =?UTF-8?q?=E2=9C=B4=201.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 29 +- README.md | 14 +- abi/IE.json | 621 +++++++++++++++++++ docs/src/README.md | 14 +- docs/src/src/IE.sol/contract.IE.md | 8 +- docs/src/src/IE.sol/interface.IExecutor.md | 2 +- docs/src/src/IE.sol/interface.INAMI.md | 2 +- docs/src/src/IE.sol/interface.ISwapRouter.md | 2 +- docs/src/src/IE.sol/interface.IToken.md | 2 +- docs/src/src/NAMI.sol/contract.NAMI.md | 2 +- docs/src/src/NAMI.sol/interface.IToken.md | 2 +- etherscan.json | 1 + lib/forge-std | 2 +- lib/solady | 2 +- scripts/initCode.sh | 4 + scripts/initCodeHash.sh | 3 + test/IE.t.sol | 4 +- 17 files changed, 684 insertions(+), 30 deletions(-) create mode 100644 abi/IE.json create mode 100644 etherscan.json create mode 100644 scripts/initCode.sh create mode 100644 scripts/initCodeHash.sh diff --git a/.gas-snapshot b/.gas-snapshot index 972d6c5..129862d 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,21 +1,22 @@ -IETest:testCommandDepositETH() (gas: 155576) +IETest:testCommandDepositETH() (gas: 154672) IETest:testCommandSendETH() (gas: 75320) -IETest:testCommandSendETHRawAddr() (gas: 75733) -IETest:testCommandStakeETH() (gas: 146774) -IETest:testCommandSwapDAI() (gas: 129775) -IETest:testCommandSwapETH() (gas: 138143) -IETest:testCommandSwapForETH() (gas: 136968) -IETest:testCommandSwapUSDC() (gas: 171451) -IETest:testCommandSwapUSDCForWBTC() (gas: 196378) -IETest:testCommandUnstakeETH() (gas: 263506) -IETest:testCommandWithdrawETH() (gas: 266209) +IETest:testCommandSendETHRawAddr() (gas: 75755) +IETest:testCommandStakeETH() (gas: 147764) +IETest:testCommandSwapDAI() (gas: 137475) +IETest:testCommandSwapETH() (gas: 168371) +IETest:testCommandSwapForETH() (gas: 144376) +IETest:testCommandSwapUSDC() (gas: 171415) +IETest:testCommandSwapUSDCForWBTC() (gas: 196380) +IETest:testCommandUnstakeETH() (gas: 261654) +IETest:testCommandWithdrawETH() (gas: 264357) IETest:testDeploy() (gas: 2563979) -IETest:testENSNameOwnership() (gas: 48402) +IETest:testENSNameOwnership() (gas: 48424) IETest:testIENameSetting() (gas: 11105) IETest:testPreviewCommandSendDecimals() (gas: 108639) IETest:testPreviewCommandSendUSDC() (gas: 67684) -IETest:testPreviewSend() (gas: 53778) -IETest:testPreviewSendCommandRawAddr() (gas: 67129) -IETest:testPreviewSendRawAddr() (gas: 30521) +IETest:testPreviewSend() (gas: 53800) +IETest:testPreviewSendCommand() (gas: 67248) +IETest:testPreviewSendCommandRawAddr() (gas: 67151) +IETest:testPreviewSendRawAddr() (gas: 30543) NAMITest:testFailRegister() (gas: 9471) NAMITest:testRegister() (gas: 58853) \ No newline at end of file diff --git a/README.md b/README.md index 2841814..b5e604a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,19 @@ -# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.24-black)](https://docs.soliditylang.org/en/v0.8.24/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) +# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) The **Intents Engine** (IE): A Basic *Text-to-tx* Simulator Contract. +## Deployments + +### Mainnet + +V1: [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) + +### Arbitrum + +V1.2: [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) + +Note: L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. + ## Uses From natural language: diff --git a/abi/IE.json b/abi/IE.json new file mode 100644 index 0000000..6c2e6d0 --- /dev/null +++ b/abi/IE.json @@ -0,0 +1,621 @@ +[ + { + "type": "constructor", + "inputs": [], + "stateMutability": "payable" + }, + { + "type": "fallback", + "stateMutability": "payable" + }, + { + "type": "receive", + "stateMutability": "payable" + }, + { + "type": "function", + "name": "checkPackedUserOp", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + }, + { + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.PackedUserOperation", + "components": [ + { + "name": "sender", + "type": "address", + "internalType": "address" + }, + { + "name": "nonce", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "initCode", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "accountGasLimits", + "type": "bytes32", + "internalType": "bytes32" + }, + { + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "gasFees", + "type": "bytes32", + "internalType": "bytes32" + }, + { + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } + ], + "outputs": [ + { + "name": "", + "type": "bool", + "internalType": "bool" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "checkUserOp", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + }, + { + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.UserOperation", + "components": [ + { + "name": "sender", + "type": "address", + "internalType": "address" + }, + { + "name": "nonce", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "initCode", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callGasLimit", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "verificationGasLimit", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "maxFeePerGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "maxPriorityFeePerGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } + ], + "outputs": [ + { + "name": "", + "type": "bool", + "internalType": "bool" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "command", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "pairs", + "inputs": [ + { + "name": "token0", + "type": "address", + "internalType": "address" + }, + { + "name": "token1", + "type": "address", + "internalType": "address" + } + ], + "outputs": [ + { + "name": "", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewCommand", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "to", + "type": "address", + "internalType": "address" + }, + { + "name": "amount", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "minAmountOut", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "token", + "type": "address", + "internalType": "address" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewSend", + "inputs": [ + { + "name": "to", + "type": "string", + "internalType": "string" + }, + { + "name": "amount", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "_to", + "type": "address", + "internalType": "address" + }, + { + "name": "_amount", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_token", + "type": "address", + "internalType": "address" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewSwap", + "inputs": [ + { + "name": "amountIn", + "type": "string", + "internalType": "string" + }, + { + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenIn", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenOut", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "_amountIn", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_amountOut", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_tokenIn", + "type": "address", + "internalType": "address" + }, + { + "name": "_tokenOut", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "send", + "inputs": [ + { + "name": "to", + "type": "string", + "internalType": "string" + }, + { + "name": "amount", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setName", + "inputs": [ + { + "name": "token", + "type": "address", + "internalType": "address" + }, + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setNameAndTicker", + "inputs": [ + { + "name": "token", + "type": "address", + "internalType": "address" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setPair", + "inputs": [ + { + "name": "tokenA", + "type": "address", + "internalType": "address" + }, + { + "name": "tokenB", + "type": "address", + "internalType": "address" + }, + { + "name": "pair", + "type": "address", + "internalType": "address" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "swap", + "inputs": [ + { + "name": "amountIn", + "type": "string", + "internalType": "string" + }, + { + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenIn", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenOut", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "tokens", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheAddressOf", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "owner", + "type": "address", + "internalType": "address" + }, + { + "name": "receiver", + "type": "address", + "internalType": "address" + }, + { + "name": "node", + "type": "bytes32", + "internalType": "bytes32" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheBalanceOf", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "balance", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "balanceAdjusted", + "type": "uint256", + "internalType": "uint256" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheTotalSupplyOf", + "inputs": [ + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "supply", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "supplyAdjusted", + "type": "uint256", + "internalType": "uint256" + } + ], + "stateMutability": "view" + }, + { + "type": "event", + "name": "NameSet", + "inputs": [ + { + "name": "token", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "name", + "type": "string", + "indexed": false, + "internalType": "string" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "PairSet", + "inputs": [ + { + "name": "token0", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "token1", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "pair", + "type": "address", + "indexed": false, + "internalType": "address" + } + ], + "anonymous": false + }, + { + "type": "error", + "name": "InsufficientSwap", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidCharacter", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidSwap", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidSyntax", + "inputs": [] + }, + { + "type": "error", + "name": "Overflow", + "inputs": [] + } + ] \ No newline at end of file diff --git a/docs/src/README.md b/docs/src/README.md index 2841814..b5e604a 100644 --- a/docs/src/README.md +++ b/docs/src/README.md @@ -1,7 +1,19 @@ -# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.24-black)](https://docs.soliditylang.org/en/v0.8.24/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) +# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) The **Intents Engine** (IE): A Basic *Text-to-tx* Simulator Contract. +## Deployments + +### Mainnet + +V1: [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) + +### Arbitrum + +V1.2: [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) + +Note: L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. + ## Uses From natural language: diff --git a/docs/src/src/IE.sol/contract.IE.md b/docs/src/src/IE.sol/contract.IE.md index 5f8d5cb..8b4cc6a 100644 --- a/docs/src/src/IE.sol/contract.IE.md +++ b/docs/src/src/IE.sol/contract.IE.md @@ -1,5 +1,5 @@ # IE -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) @@ -171,7 +171,7 @@ mapping(string name => address) public tokens; ### pairs -*DAO-governed token swap pool routing.* +*DAO-governed token swap pool routing on Uniswap V3.* ```solidity @@ -610,11 +610,11 @@ event NameSet(address indexed token, string name); ``` ### PairSet -*Logs the registration of a token swap pool pair route.* +*Logs the registration of a token swap pool pair route on Uniswap V3.* ```solidity -event PairSet(address indexed token0, address indexed token1, address indexed pair); +event PairSet(address indexed token0, address indexed token1, address pair); ``` ## Errors diff --git a/docs/src/src/IE.sol/interface.IExecutor.md b/docs/src/src/IE.sol/interface.IExecutor.md index aae5c7d..e544b52 100644 --- a/docs/src/src/IE.sol/interface.IExecutor.md +++ b/docs/src/src/IE.sol/interface.IExecutor.md @@ -1,5 +1,5 @@ # IExecutor -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) Simple calldata executor interface. diff --git a/docs/src/src/IE.sol/interface.INAMI.md b/docs/src/src/IE.sol/interface.INAMI.md index 01a6428..a2ef0b7 100644 --- a/docs/src/src/IE.sol/interface.INAMI.md +++ b/docs/src/src/IE.sol/interface.INAMI.md @@ -1,5 +1,5 @@ # INAMI -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) *Simple NAMI names interface for resolving L2 ENS ownership.* diff --git a/docs/src/src/IE.sol/interface.ISwapRouter.md b/docs/src/src/IE.sol/interface.ISwapRouter.md index 52ac3b4..7276427 100644 --- a/docs/src/src/IE.sol/interface.ISwapRouter.md +++ b/docs/src/src/IE.sol/interface.ISwapRouter.md @@ -1,5 +1,5 @@ # ISwapRouter -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) *Simple Uniswap V3 swapping interface.* diff --git a/docs/src/src/IE.sol/interface.IToken.md b/docs/src/src/IE.sol/interface.IToken.md index cc378b4..4f4b72f 100644 --- a/docs/src/src/IE.sol/interface.IToken.md +++ b/docs/src/src/IE.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) *Simple token transfer interface.* diff --git a/docs/src/src/NAMI.sol/contract.NAMI.md b/docs/src/src/NAMI.sol/contract.NAMI.md index c82266b..c91d21c 100644 --- a/docs/src/src/NAMI.sol/contract.NAMI.md +++ b/docs/src/src/NAMI.sol/contract.NAMI.md @@ -1,5 +1,5 @@ # NAMI -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/NAMI.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) diff --git a/docs/src/src/NAMI.sol/interface.IToken.md b/docs/src/src/NAMI.sol/interface.IToken.md index 9c82615..1baf29f 100644 --- a/docs/src/src/NAMI.sol/interface.IToken.md +++ b/docs/src/src/NAMI.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/44717af19e86fee469275f05c90b9edc2af129a2/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/NAMI.sol) *Simple token balance & supply interface.* diff --git a/etherscan.json b/etherscan.json new file mode 100644 index 0000000..c34feb4 --- /dev/null +++ b/etherscan.json @@ -0,0 +1 @@ +{"language":"Solidity","sources":{"src/IE.sol":{"content":"// ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘\n// SPDX-License-Identifier: AGPL-3.0-only\npragma solidity ^0.8.19;\n\nimport {SafeTransferLib} from \"../lib/solady/src/utils/SafeTransferLib.sol\";\nimport {MetadataReaderLib} from \"../lib/solady/src/utils/MetadataReaderLib.sol\";\n\n/// @title Intents Engine (IE)\n/// @notice Simple helper contract for turning transactional intents into executable code.\n/// @dev V1 simulates typical commands (sending and swapping tokens) and includes execution.\n/// IE also has a workflow to verify the intent of ERC4337 account userOps against calldata.\n/// @author nani.eth (https://github.com/NaniDAO/ie)\n/// @custom:version 1.2.0\ncontract IE {\n /// ======================= LIBRARY USAGE ======================= ///\n\n /// @dev Metadata reader library.\n using MetadataReaderLib for address;\n\n /// @dev Safe token transfer library.\n using SafeTransferLib for address;\n\n /// ======================= CUSTOM ERRORS ======================= ///\n\n /// @dev Bad math.\n error Overflow();\n\n /// @dev 0-liquidity.\n error InvalidSwap();\n\n /// @dev Invalid command.\n error InvalidSyntax();\n\n /// @dev Non-numeric character.\n error InvalidCharacter();\n\n /// @dev Insufficient swap output.\n error InsufficientSwap();\n\n /// =========================== EVENTS =========================== ///\n\n /// @dev Logs the registration of a token name.\n event NameSet(address indexed token, string name);\n\n /// @dev Logs the registration of a token swap pool pair route on Uniswap V3.\n event PairSet(address indexed token0, address indexed token1, address pair);\n\n /// ========================== STRUCTS ========================== ///\n\n /// @dev The ERC4337 user operation (userOp) struct.\n struct UserOperation {\n address sender;\n uint256 nonce;\n bytes initCode;\n bytes callData;\n uint256 callGasLimit;\n uint256 verificationGasLimit;\n uint256 preVerificationGas;\n uint256 maxFeePerGas;\n uint256 maxPriorityFeePerGas;\n bytes paymasterAndData;\n bytes signature;\n }\n\n /// @dev The packed ERC4337 userOp struct.\n struct PackedUserOperation {\n address sender;\n uint256 nonce;\n bytes initCode;\n bytes callData;\n bytes32 accountGasLimits;\n uint256 preVerificationGas;\n bytes32 gasFees;\n bytes paymasterAndData;\n bytes signature;\n }\n\n /// @dev The `swap` command information struct.\n struct SwapInfo {\n address tokenIn;\n address tokenOut;\n uint256 amountIn;\n bool ETHIn;\n bool ETHOut;\n }\n\n /// @dev The `swap` pool liquidity struct.\n struct SwapLiq {\n address pool;\n uint256 liq;\n }\n\n /// ========================= CONSTANTS ========================= ///\n\n /// @dev The governing DAO address.\n address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA;\n\n /// @dev The NANI token address.\n address internal constant NANI = 0x000000000000C6A645b0E51C9eCAA4CA580Ed8e8;\n\n /// @dev The conventional ERC7528 ETH address.\n address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;\n\n /// @dev The canonical wrapped ETH address.\n address internal constant WETH = 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1;\n\n /// @dev The popular wrapped BTC address.\n address internal constant WBTC = 0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f;\n\n /// @dev The Circle USD stablecoin address.\n address internal constant USDC = 0xaf88d065e77c8cC2239327C5EDb3A432268e5831;\n\n /// @dev The Tether USD stablecoin address.\n address internal constant USDT = 0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9;\n\n /// @dev The Maker DAO USD stablecoin address.\n address internal constant DAI = 0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1;\n\n /// @dev The Arbitrum DAO governance token address.\n address internal constant ARB = 0x912CE59144191C1204E64559FE8253a0e49E6548;\n\n /// @dev The Lido Wrapped Staked ETH token address.\n address internal constant WSTETH = 0x5979D7b546E38E414F7E9822514be443A4800529;\n\n /// @dev The Rocket Pool Staked ETH token address.\n address internal constant RETH = 0xEC70Dcb4A1EFa46b8F2D97C310C9c4790ba5ffA8;\n\n /// @dev The address of the Uniswap V3 Factory.\n address internal constant UNISWAP_V3_FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984;\n\n /// @dev The Uniswap V3 Pool `initcodehash`.\n bytes32 internal constant UNISWAP_V3_POOL_INIT_CODE_HASH =\n 0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;\n\n /// @dev The minimum value that can be returned from `getSqrtRatioAtTick` (plus one).\n uint160 internal constant MIN_SQRT_RATIO_PLUS_ONE = 4295128740;\n\n /// @dev The maximum value that can be returned from `getSqrtRatioAtTick` (minus one).\n uint160 internal constant MAX_SQRT_RATIO_MINUS_ONE =\n 1461446703485210103287273052203988822378723970341;\n\n /// @dev The NAMI naming system on Arbitrum.\n INAMI internal constant NAMI = INAMI(0x000000006641B4C250AEA6B62A1e0067D300697a);\n\n /// ========================== STORAGE ========================== ///\n\n /// @dev DAO-governed token address naming.\n mapping(string name => address) public tokens;\n\n /// @dev DAO-governed token swap pool routing on Uniswap V3.\n mapping(address token0 => mapping(address token1 => address)) public pairs;\n\n /// ======================== CONSTRUCTOR ======================== ///\n\n /// @dev Constructs this IE on the Arbitrum L2 of Ethereum.\n constructor() payable {}\n\n /// ====================== COMMAND PREVIEW ====================== ///\n\n /// @dev Preview natural language smart contract command.\n /// The `send` syntax uses ENS naming: 'send vitalik 20 DAI'.\n /// `swap` syntax uses common format: 'swap 100 DAI for WETH'.\n function previewCommand(string calldata intent)\n public\n view\n virtual\n returns (\n address to, // Receiver address.\n uint256 amount, // Formatted amount.\n uint256 minAmountOut, // Formatted amount.\n address token, // Asset to send `to`.\n bytes memory callData, // Raw calldata for send transaction.\n bytes memory executeCallData // Anticipates common execute API.\n )\n {\n string memory normalized = _lowercase(intent);\n bytes32 action = _extraction(normalized);\n if (action == \"send\" || action == \"transfer\" || action == \"pay\" || action == \"grant\") {\n (string memory _to, string memory _amount, string memory _token) =\n _extractSend(normalized);\n (to, amount, token, callData, executeCallData) = previewSend(_to, _amount, _token);\n } else if (\n action == \"swap\" || action == \"exchange\" || action == \"stake\" || action == \"deposit\"\n || action == \"unstake\" || action == \"withdraw\"\n ) {\n (\n string memory amountIn,\n string memory amountOutMinimum,\n string memory tokenIn,\n string memory tokenOut\n ) = _extractSwap(normalized);\n (amount, minAmountOut, token, to) =\n previewSwap(amountIn, amountOutMinimum, tokenIn, tokenOut);\n } else {\n revert InvalidSyntax(); // Invalid command format.\n }\n }\n\n /// @dev Previews a `send` command from the parts of a matched intent string.\n function previewSend(string memory to, string memory amount, string memory token)\n public\n view\n virtual\n returns (\n address _to,\n uint256 _amount,\n address _token,\n bytes memory callData,\n bytes memory executeCallData\n )\n {\n uint256 decimals;\n (_token, decimals) = _returnTokenConstants(bytes32(bytes(token)));\n if (_token == address(0)) _token = tokens[token]; // Check storage.\n bool isETH = _token == ETH; // Memo whether the token is ETH or not.\n (, _to,) = whatIsTheAddressOf(to); // Fetch receiver address from ENS.\n _amount = _toUint(amount, decimals != 0 ? decimals : _token.readDecimals());\n if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount));\n executeCallData =\n abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData));\n }\n\n /// @dev Previews a `swap` command from the parts of a matched intent string.\n function previewSwap(\n string memory amountIn,\n string memory amountOutMinimum,\n string memory tokenIn,\n string memory tokenOut\n )\n public\n view\n virtual\n returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut)\n {\n uint256 decimalsIn;\n uint256 decimalsOut;\n (_tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn)));\n if (_tokenIn == address(0)) _tokenIn = tokens[tokenIn];\n (_tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut)));\n if (_tokenOut == address(0)) _tokenOut = tokens[tokenOut];\n _amountIn = _toUint(amountIn, decimalsIn != 0 ? decimalsIn : _tokenIn.readDecimals());\n _amountOut =\n _toUint(amountOutMinimum, decimalsOut != 0 ? decimalsOut : _tokenOut.readDecimals());\n }\n\n /// @dev Checks ERC4337 userOp against the output of the command intent.\n function checkUserOp(string calldata intent, UserOperation calldata userOp)\n public\n view\n virtual\n returns (bool)\n {\n (,,,,, bytes memory executeCallData) = previewCommand(intent);\n if (executeCallData.length != userOp.callData.length) return false;\n return keccak256(executeCallData) == keccak256(userOp.callData);\n }\n\n /// @dev Checks packed ERC4337 userOp against the output of the command intent.\n function checkPackedUserOp(string calldata intent, PackedUserOperation calldata userOp)\n public\n view\n virtual\n returns (bool)\n {\n (,,,,, bytes memory executeCallData) = previewCommand(intent);\n if (executeCallData.length != userOp.callData.length) return false;\n return keccak256(executeCallData) == keccak256(userOp.callData);\n }\n\n /// @dev Checks and returns the canonical token address constant for a matched intent string.\n function _returnTokenConstants(bytes32 token)\n internal\n pure\n virtual\n returns (address _token, uint256 _decimals)\n {\n if (token == \"eth\" || token == \"ether\") return (ETH, 18);\n if (token == \"usdc\") return (USDC, 6);\n if (token == \"usdt\" || token == \"tether\") return (USDT, 6);\n if (token == \"dai\") return (DAI, 18);\n if (token == \"arb\" || token == \"arbitrum\") return (ARB, 18);\n if (token == \"weth\") return (WETH, 18);\n if (token == \"wbtc\" || token == \"btc\" || token == \"bitcoin\") return (WBTC, 8);\n if (token == \"steth\" || token == \"wsteth\" || token == \"lido\") return (WSTETH, 18);\n if (token == \"reth\") return (RETH, 18);\n if (token == \"nani\") return (NANI, 18);\n }\n\n /// @dev Checks and returns popular pool pairs for WETH swaps.\n function _returnPoolConstants(address token0, address token1)\n internal\n pure\n virtual\n returns (address pool)\n {\n if (token0 == WSTETH && token1 == WETH) return 0x35218a1cbaC5Bbc3E57fd9Bd38219D37571b3537;\n if (token0 == WETH && token1 == RETH) return 0x09ba302A3f5ad2bF8853266e271b005A5b3716fe;\n if (token0 == WETH && token1 == USDC) return 0xC6962004f452bE9203591991D15f6b388e09E8D0;\n if (token0 == WETH && token1 == USDT) return 0x641C00A822e8b671738d32a431a4Fb6074E5c79d;\n if (token0 == WETH && token1 == DAI) return 0xA961F0473dA4864C5eD28e00FcC53a3AAb056c1b;\n if (token0 == WETH && token1 == ARB) return 0xC6F780497A95e246EB9449f5e4770916DCd6396A;\n if (token0 == WBTC && token1 == WETH) return 0x2f5e87C9312fa29aed5c179E456625D79015299c;\n }\n\n /// ===================== COMMAND EXECUTION ===================== ///\n\n /// @dev Executes a text command from an intent string.\n function command(string calldata intent) public payable virtual {\n string memory normalized = _lowercase(intent);\n bytes32 action = _extraction(normalized);\n if (action == \"send\" || action == \"transfer\" || action == \"pay\" || action == \"grant\") {\n (string memory to, string memory amount, string memory token) = _extractSend(normalized);\n send(to, amount, token);\n } else if (\n action == \"swap\" || action == \"exchange\" || action == \"stake\" || action == \"deposit\"\n || action == \"unstake\" || action == \"withdraw\"\n ) {\n (\n string memory amountIn,\n string memory amountOutMinimum,\n string memory tokenIn,\n string memory tokenOut\n ) = _extractSwap(normalized);\n swap(amountIn, amountOutMinimum, tokenIn, tokenOut);\n } else {\n revert InvalidSyntax(); // Invalid command format.\n }\n }\n\n /// @dev Executes a `send` command from the parts of a matched intent string.\n function send(string memory to, string memory amount, string memory token)\n public\n payable\n virtual\n {\n (address _token, uint256 decimals) = _returnTokenConstants(bytes32(bytes(token)));\n if (_token == address(0)) _token = tokens[token];\n (, address _to,) = whatIsTheAddressOf(to);\n if (_token == ETH) {\n _to.safeTransferETH(_toUint(amount, decimals));\n } else {\n _token.safeTransferFrom(\n msg.sender, _to, _toUint(amount, decimals != 0 ? decimals : _token.readDecimals())\n );\n }\n }\n\n /// @dev Executes a `swap` command from the parts of a matched intent string.\n function swap(\n string memory amountIn,\n string memory amountOutMinimum,\n string memory tokenIn,\n string memory tokenOut\n ) public payable virtual {\n SwapInfo memory info;\n uint256 decimalsIn;\n uint256 decimalsOut;\n (info.tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn)));\n if (info.tokenIn == address(0)) info.tokenIn = tokens[tokenIn];\n (info.tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut)));\n if (info.tokenOut == address(0)) info.tokenOut = tokens[tokenOut];\n info.ETHIn = info.tokenIn == ETH;\n if (info.ETHIn) info.tokenIn = WETH;\n info.ETHOut = info.tokenOut == ETH;\n if (info.ETHOut) info.tokenOut = WETH;\n info.amountIn =\n _toUint(amountIn, decimalsIn != 0 ? decimalsIn : info.tokenIn.readDecimals());\n if (info.amountIn >= 1 << 255) revert Overflow();\n (address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut);\n (int256 amount0, int256 amount1) = ISwapRouter(pool).swap(\n !info.ETHOut ? msg.sender : address(this),\n zeroForOne,\n int256(info.amountIn),\n zeroForOne ? MIN_SQRT_RATIO_PLUS_ONE : MAX_SQRT_RATIO_MINUS_ONE,\n abi.encodePacked(info.ETHIn, info.ETHOut, msg.sender, info.tokenIn, info.tokenOut)\n );\n if (\n uint256(-(zeroForOne ? amount1 : amount0))\n < _toUint(\n amountOutMinimum, decimalsOut != 0 ? decimalsOut : info.tokenOut.readDecimals()\n )\n ) revert InsufficientSwap();\n }\n\n /// @dev Fallback `uniswapV3SwapCallback`.\n /// If ETH is swapped, WETH is forwarded.\n fallback() external payable virtual {\n int256 amount0Delta;\n int256 amount1Delta;\n bool ETHIn;\n bool ETHOut;\n address payer;\n address tokenIn;\n address tokenOut;\n assembly (\"memory-safe\") {\n amount0Delta := calldataload(0x4)\n amount1Delta := calldataload(0x24)\n ETHIn := byte(0, calldataload(0x84))\n ETHOut := byte(0, calldataload(add(0x84, 1)))\n payer := shr(96, calldataload(add(0x84, 2)))\n tokenIn := shr(96, calldataload(add(0x84, 22)))\n tokenOut := shr(96, calldataload(add(0x84, 42)))\n }\n if (amount0Delta <= 0 && amount1Delta <= 0) revert InvalidSwap();\n (address pool, bool zeroForOne) = _computePoolAddress(tokenIn, tokenOut);\n assembly (\"memory-safe\") {\n if iszero(eq(caller(), pool)) { revert(codesize(), 0x00) }\n }\n if (ETHIn) {\n _wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta));\n } else {\n tokenIn.safeTransferFrom(\n payer, msg.sender, uint256(zeroForOne ? amount0Delta : amount1Delta)\n );\n }\n if (ETHOut) {\n uint256 amount = uint256(-(zeroForOne ? amount1Delta : amount0Delta));\n _unwrapETH(amount);\n payer.safeTransferETH(amount);\n }\n }\n\n /// @dev Computes the create2 address for given token pair.\n /// note: This process checks all available pools for price.\n function _computePoolAddress(address tokenA, address tokenB)\n internal\n view\n virtual\n returns (address pool, bool zeroForOne)\n {\n if (tokenA < tokenB) zeroForOne = true;\n else (tokenA, tokenB) = (tokenB, tokenA);\n pool = _returnPoolConstants(tokenA, tokenB);\n if (pool == address(0)) {\n pool = pairs[tokenA][tokenB];\n if (pool == address(0)) {\n address pool100 = _computePairHash(tokenA, tokenB, 100); // Lowest fee.\n address pool500 = _computePairHash(tokenA, tokenB, 500); // Lower fee.\n address pool3000 = _computePairHash(tokenA, tokenB, 3000); // Mid fee.\n address pool10000 = _computePairHash(tokenA, tokenB, 10000); // Hi fee.\n // Initialize an array to hold the liquidity information for each pool.\n SwapLiq[5] memory pools = [\n SwapLiq(pool100, pool100.code.length != 0 ? _balanceOf(tokenA, pool100) : 0),\n SwapLiq(pool500, pool500.code.length != 0 ? _balanceOf(tokenA, pool500) : 0),\n SwapLiq(pool3000, pool3000.code.length != 0 ? _balanceOf(tokenA, pool3000) : 0),\n SwapLiq(pool10000, pool10000.code.length != 0 ? _balanceOf(tokenA, pool10000) : 0),\n SwapLiq(pool, 0) // Placeholder for top pool. This will hold outputs for comparison.\n ];\n // Iterate through the array to find the top pool with the highest liquidity in `tokenA`.\n for (uint256 i; i != 4; ++i) {\n if (pools[i].liq > pools[4].liq) {\n pools[4].liq = pools[i].liq;\n pools[4].pool = pools[i].pool;\n }\n }\n pool = pools[4].pool; // Return the top pool with likely best liquidity.\n }\n }\n }\n\n /// @dev Computes the create2 deployment hash for a given token pair.\n function _computePairHash(address token0, address token1, uint24 fee)\n internal\n pure\n virtual\n returns (address pool)\n {\n bytes32 salt = keccak256(abi.encode(token0, token1, fee));\n assembly (\"memory-safe\") {\n mstore8(0x00, 0xff) // Write the prefix.\n mstore(0x35, UNISWAP_V3_POOL_INIT_CODE_HASH)\n mstore(0x01, shl(96, UNISWAP_V3_FACTORY))\n mstore(0x15, salt)\n pool := keccak256(0x00, 0x55)\n mstore(0x35, 0) // Restore overwritten.\n }\n }\n\n /// @dev Wraps an `amount` of ETH to WETH and funds pool caller for swap.\n function _wrapETH(uint256 amount) internal virtual {\n assembly (\"memory-safe\") {\n pop(call(gas(), WETH, amount, codesize(), 0x00, codesize(), 0x00))\n mstore(0x14, caller()) // Store the `pool` argument.\n mstore(0x34, amount) // Store the `amount` argument.\n mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.\n pop(call(gas(), WETH, 0, 0x10, 0x44, codesize(), 0x00))\n mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\n }\n }\n\n /// @dev Unwraps an `amount` of ETH from WETH for return.\n function _unwrapETH(uint256 amount) internal virtual {\n assembly (\"memory-safe\") {\n mstore(0x00, 0x2e1a7d4d) // `withdraw(uint256)`.\n mstore(0x20, amount) // Store the `amount` argument.\n pop(call(gas(), WETH, 0, 0x1c, 0x24, codesize(), 0x00))\n }\n }\n\n /// @dev Returns the amount of ERC20 `token` owned by `account`.\n function _balanceOf(address token, address account)\n internal\n view\n virtual\n returns (uint256 amount)\n {\n assembly (\"memory-safe\") {\n mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\n mstore(0x14, account) // Store the `account` argument.\n pop(staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20))\n amount := mload(0x20)\n }\n }\n\n /// @dev ETH receiver fallback.\n /// Only canonical WETH can call.\n receive() external payable virtual {\n assembly (\"memory-safe\") {\n if iszero(eq(caller(), WETH)) { revert(codesize(), 0x00) }\n }\n }\n\n /// ================== BALANCE & SUPPLY HELPERS ================== ///\n\n /// @dev Returns the balance of a named account in a named token.\n function whatIsTheBalanceOf(string calldata name, /*(bob)*/ /*in*/ string calldata token)\n public\n view\n virtual\n returns (uint256 balance, uint256 balanceAdjusted)\n {\n (, address _name,) = whatIsTheAddressOf(name);\n (address _token, uint256 decimals) =\n _returnTokenConstants(bytes32(bytes(_lowercase(token))));\n if (_token == address(0)) _token = tokens[token];\n balance = _token == ETH ? _name.balance : _token.balanceOf(_name);\n balanceAdjusted = balance / 10 ** (decimals != 0 ? decimals : _token.readDecimals());\n }\n\n /// @dev Returns the total supply of a named token.\n function whatIsTheTotalSupplyOf(string calldata token)\n public\n view\n virtual\n returns (uint256 supply, uint256 supplyAdjusted)\n {\n (address _token, uint256 decimals) =\n _returnTokenConstants(bytes32(bytes(_lowercase(token))));\n if (_token == address(0)) _token = tokens[token];\n assembly (\"memory-safe\") {\n mstore(0x00, 0x18160ddd) // `totalSupply()`.\n if iszero(staticcall(gas(), _token, 0x1c, 0x04, 0x20, 0x20)) {\n revert(codesize(), 0x00)\n }\n supply := mload(0x20)\n }\n supplyAdjusted = supply / 10 ** (decimals != 0 ? decimals : _token.readDecimals());\n }\n\n /// ====================== ENS VERIFICATION ====================== ///\n\n /// @dev Returns ENS name ownership details.\n function whatIsTheAddressOf(string memory name)\n public\n view\n virtual\n returns (address owner, address receiver, bytes32 node)\n {\n // If address length, convert.\n if (bytes(name).length == 42) {\n receiver = _toAddress(name);\n } else {\n (owner, receiver, node) = NAMI.whatIsTheAddressOf(name);\n }\n }\n\n /// ========================= GOVERNANCE ========================= ///\n\n /// @dev Sets a public `name` tag for a given `token` address. Governed by DAO.\n function setName(address token, string calldata name) public payable virtual {\n assembly (\"memory-safe\") {\n if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat.\n }\n string memory normalized = _lowercase(name);\n emit NameSet(tokens[normalized] = token, normalized);\n }\n\n /// @dev Sets a public name and ticker for a given `token` address.\n function setNameAndTicker(address token) public payable virtual {\n string memory normalizedName = _lowercase(token.readName());\n string memory normalizedSymbol = _lowercase(token.readSymbol());\n emit NameSet(tokens[normalizedName] = token, normalizedName);\n emit NameSet(tokens[normalizedSymbol] = token, normalizedSymbol);\n }\n\n /// @dev Sets a public pool `pair` for swapping. Governed by DAO.\n function setPair(address tokenA, address tokenB, address pair) public payable virtual {\n assembly (\"memory-safe\") {\n if iszero(eq(caller(), DAO)) { revert(codesize(), 0x00) } // Optimized for repeat.\n }\n if (tokenB < tokenA) (tokenA, tokenB) = (tokenB, tokenA);\n emit PairSet(tokenA, tokenB, pairs[tokenA][tokenB] = pair);\n }\n\n /// ===================== STRING OPERATIONS ===================== ///\n\n /// @dev Returns copy of string in lowercase.\n /// Modified from Solady LibString `toCase`.\n function _lowercase(string memory subject)\n internal\n pure\n virtual\n returns (string memory result)\n {\n assembly (\"memory-safe\") {\n let length := mload(subject)\n if length {\n result := add(mload(0x40), 0x20)\n subject := add(subject, 1)\n let flags := shl(add(70, shl(5, 0)), 0x3ffffff)\n let w := not(0)\n for { let o := length } 1 {} {\n o := add(o, w)\n let b := and(0xff, mload(add(subject, o)))\n mstore8(add(result, o), xor(b, and(shr(b, flags), 0x20)))\n if iszero(o) { break }\n }\n result := mload(0x40)\n mstore(result, length) // Store the length.\n let last := add(add(result, 0x20), length)\n mstore(last, 0) // Zeroize the slot after the string.\n mstore(0x40, add(last, 0x20)) // Allocate the memory.\n }\n }\n }\n\n /// @dev Extracts the first word (action) as bytes32.\n function _extraction(string memory normalizedIntent)\n internal\n pure\n virtual\n returns (bytes32 result)\n {\n assembly (\"memory-safe\") {\n let str := add(normalizedIntent, 0x20)\n for { let i } lt(i, 0x20) { i := add(i, 1) } {\n let char := byte(0, mload(add(str, i)))\n if eq(char, 0x20) { break }\n result := or(result, shl(sub(248, mul(i, 8)), char))\n }\n }\n }\n\n /// @dev Extract the key words of normalized `send` intent.\n function _extractSend(string memory normalizedIntent)\n internal\n pure\n virtual\n returns (string memory to, string memory amount, string memory token)\n {\n string[] memory parts = _split(normalizedIntent, \" \");\n if (parts.length == 4) return (parts[1], parts[2], parts[3]);\n if (parts.length == 5) return (parts[4], parts[1], parts[2]);\n else revert InvalidSyntax(); // Command is not formatted.\n }\n\n /// @dev Extract the key words of normalized `swap` intent.\n function _extractSwap(string memory normalizedIntent)\n internal\n pure\n virtual\n returns (\n string memory amountIn,\n string memory amountOutMinimum,\n string memory tokenIn,\n string memory tokenOut\n )\n {\n string[] memory parts = _split(normalizedIntent, \" \");\n if (parts.length == 5) return (parts[1], \"\", parts[2], parts[4]);\n if (parts.length == 6) return (parts[1], parts[4], parts[2], parts[5]);\n else revert InvalidSyntax(); // Command is not formatted.\n }\n\n /// @dev Split the intent into an array of words.\n function _split(string memory base, bytes1 delimiter)\n internal\n pure\n virtual\n returns (string[] memory parts)\n {\n unchecked {\n bytes memory baseBytes = bytes(base);\n uint256 count = 1;\n for (uint256 i; i != baseBytes.length; ++i) {\n if (baseBytes[i] == delimiter) {\n ++count;\n }\n }\n parts = new string[](count);\n uint256 partIndex;\n uint256 start;\n for (uint256 i; i <= baseBytes.length; ++i) {\n if (i == baseBytes.length || baseBytes[i] == delimiter) {\n bytes memory part = new bytes(i - start);\n for (uint256 j = start; j != i; ++j) {\n part[j - start] = baseBytes[j];\n }\n parts[partIndex] = string(part);\n ++partIndex;\n start = i + 1;\n }\n }\n }\n }\n\n /// @dev Convert string to decimalized numerical value.\n function _toUint(string memory s, uint256 decimals)\n internal\n pure\n virtual\n returns (uint256 result)\n {\n unchecked {\n bool hasDecimal;\n uint256 decimalPlaces;\n bytes memory b = bytes(s);\n for (uint256 i; i != b.length; ++i) {\n if (b[i] >= \"0\" && b[i] <= \"9\") {\n result = result * 10 + uint8(b[i]) - 48;\n if (hasDecimal) {\n ++decimalPlaces;\n if (decimalPlaces > decimals) break;\n }\n } else if (b[i] == \".\" && !hasDecimal) {\n hasDecimal = true;\n } else {\n revert InvalidCharacter();\n }\n }\n if (decimalPlaces < decimals) {\n result *= 10 ** (decimals - decimalPlaces);\n }\n }\n }\n\n /// @dev Converts a hexadecimal string to its `address` representation.\n /// Modified from Stack (https://ethereum.stackexchange.com/a/156916).\n function _toAddress(string memory s) internal pure virtual returns (address addr) {\n bytes memory _bytes = _hexStringToAddress(s);\n if (_bytes.length < 21) revert InvalidSyntax();\n assembly (\"memory-safe\") {\n addr := div(mload(add(add(_bytes, 0x20), 1)), 0x1000000000000000000000000)\n }\n }\n\n /// @dev Converts a hexadecimal string into its bytes representation.\n function _hexStringToAddress(string memory s) internal pure virtual returns (bytes memory r) {\n unchecked {\n bytes memory ss = bytes(s);\n if (ss.length % 2 != 0) revert InvalidSyntax(); // Length must be even.\n r = new bytes(ss.length / 2);\n for (uint256 i; i != ss.length / 2; ++i) {\n r[i] =\n bytes1(_fromHexChar(uint8(ss[2 * i])) * 16 + _fromHexChar(uint8(ss[2 * i + 1])));\n }\n }\n }\n\n /// @dev Converts a single hexadecimal character into its numerical value.\n function _fromHexChar(uint8 c) internal pure virtual returns (uint8 result) {\n unchecked {\n if (bytes1(c) >= bytes1(\"0\") && bytes1(c) <= bytes1(\"9\")) return c - uint8(bytes1(\"0\"));\n if (bytes1(c) >= bytes1(\"a\") && bytes1(c) <= bytes1(\"f\")) {\n return 10 + c - uint8(bytes1(\"a\"));\n }\n if (bytes1(c) >= bytes1(\"A\") && bytes1(c) <= bytes1(\"F\")) {\n return 10 + c - uint8(bytes1(\"A\"));\n }\n }\n }\n}\n\n/// @dev Simple token transfer interface.\ninterface IToken {\n function transfer(address, uint256) external returns (bool);\n}\n\n/// @notice Simple calldata executor interface.\ninterface IExecutor {\n function execute(address, uint256, bytes calldata) external payable returns (bytes memory);\n}\n\n/// @dev Simple NAMI names interface for resolving L2 ENS ownership.\ninterface INAMI {\n function whatIsTheAddressOf(string calldata)\n external\n view\n returns (address, address, bytes32);\n}\n\n/// @dev Simple Uniswap V3 swapping interface.\ninterface ISwapRouter {\n function swap(address, bool, int256, uint160, bytes calldata)\n external\n returns (int256, int256);\n}\n"},"lib/solady/src/utils/SafeTransferLib.sol":{"content":"// SPDX-License-Identifier: MIT\npragma solidity ^0.8.4;\n\n/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.\n/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)\n/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)\n/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)\n///\n/// @dev Note:\n/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.\n/// - For ERC20s, this implementation won't check that a token has code,\n/// responsibility is delegated to the caller.\nlibrary SafeTransferLib {\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* CUSTOM ERRORS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n /// @dev The ETH transfer has failed.\n error ETHTransferFailed();\n\n /// @dev The ERC20 `transferFrom` has failed.\n error TransferFromFailed();\n\n /// @dev The ERC20 `transfer` has failed.\n error TransferFailed();\n\n /// @dev The ERC20 `approve` has failed.\n error ApproveFailed();\n\n /// @dev The Permit2 operation has failed.\n error Permit2Failed();\n\n /// @dev The Permit2 amount must be less than `2**160 - 1`.\n error Permit2AmountOverflow();\n\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* CONSTANTS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n /// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.\n uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;\n\n /// @dev Suggested gas stipend for contract receiving ETH to perform a few\n /// storage reads and writes, but low enough to prevent griefing.\n uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;\n\n /// @dev The unique EIP-712 domain domain separator for the DAI token contract.\n bytes32 internal constant DAI_DOMAIN_SEPARATOR =\n 0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;\n\n /// @dev The address for the WETH9 contract on Ethereum mainnet.\n address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;\n\n /// @dev The canonical Permit2 address.\n /// [Github](https://github.com/Uniswap/permit2)\n /// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)\n address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;\n\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* ETH OPERATIONS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n // If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.\n //\n // The regular variants:\n // - Forwards all remaining gas to the target.\n // - Reverts if the target reverts.\n // - Reverts if the current contract has insufficient balance.\n //\n // The force variants:\n // - Forwards with an optional gas stipend\n // (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).\n // - If the target reverts, or if the gas stipend is exhausted,\n // creates a temporary contract to force send the ETH via `SELFDESTRUCT`.\n // Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.\n // - Reverts if the current contract has insufficient balance.\n //\n // The try variants:\n // - Forwards with a mandatory gas stipend.\n // - Instead of reverting, returns whether the transfer succeeded.\n\n /// @dev Sends `amount` (in wei) ETH to `to`.\n function safeTransferETH(address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\n revert(0x1c, 0x04)\n }\n }\n }\n\n /// @dev Sends all the ETH in the current contract to `to`.\n function safeTransferAllETH(address to) internal {\n /// @solidity memory-safe-assembly\n assembly {\n // Transfer all the ETH and check if it succeeded or not.\n if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\n revert(0x1c, 0x04)\n }\n }\n }\n\n /// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.\n function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {\n /// @solidity memory-safe-assembly\n assembly {\n if lt(selfbalance(), amount) {\n mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\n revert(0x1c, 0x04)\n }\n if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, to) // Store the address in scratch space.\n mstore8(0x0b, 0x73) // Opcode `PUSH20`.\n mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\n if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\n }\n }\n }\n\n /// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.\n function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {\n /// @solidity memory-safe-assembly\n assembly {\n if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, to) // Store the address in scratch space.\n mstore8(0x0b, 0x73) // Opcode `PUSH20`.\n mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\n if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\n }\n }\n }\n\n /// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.\n function forceSafeTransferETH(address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n if lt(selfbalance(), amount) {\n mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.\n revert(0x1c, 0x04)\n }\n if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, to) // Store the address in scratch space.\n mstore8(0x0b, 0x73) // Opcode `PUSH20`.\n mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\n if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\n }\n }\n }\n\n /// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.\n function forceSafeTransferAllETH(address to) internal {\n /// @solidity memory-safe-assembly\n assembly {\n // forgefmt: disable-next-item\n if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {\n mstore(0x00, to) // Store the address in scratch space.\n mstore8(0x0b, 0x73) // Opcode `PUSH20`.\n mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.\n if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.\n }\n }\n }\n\n /// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.\n function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)\n internal\n returns (bool success)\n {\n /// @solidity memory-safe-assembly\n assembly {\n success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)\n }\n }\n\n /// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.\n function trySafeTransferAllETH(address to, uint256 gasStipend)\n internal\n returns (bool success)\n {\n /// @solidity memory-safe-assembly\n assembly {\n success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)\n }\n }\n\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* ERC20 OPERATIONS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n /// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\n /// Reverts upon failure.\n ///\n /// The `from` account must have at least `amount` approved for\n /// the current contract to manage.\n function safeTransferFrom(address token, address from, address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n let m := mload(0x40) // Cache the free memory pointer.\n mstore(0x60, amount) // Store the `amount` argument.\n mstore(0x40, to) // Store the `to` argument.\n mstore(0x2c, shl(96, from)) // Store the `from` argument.\n mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.\n // Perform the transfer, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x60, 0) // Restore the zero slot to zero.\n mstore(0x40, m) // Restore the free memory pointer.\n }\n }\n\n /// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\n ///\n /// The `from` account must have at least `amount` approved for the current contract to manage.\n function trySafeTransferFrom(address token, address from, address to, uint256 amount)\n internal\n returns (bool success)\n {\n /// @solidity memory-safe-assembly\n assembly {\n let m := mload(0x40) // Cache the free memory pointer.\n mstore(0x60, amount) // Store the `amount` argument.\n mstore(0x40, to) // Store the `to` argument.\n mstore(0x2c, shl(96, from)) // Store the `from` argument.\n mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.\n success :=\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\n )\n mstore(0x60, 0) // Restore the zero slot to zero.\n mstore(0x40, m) // Restore the free memory pointer.\n }\n }\n\n /// @dev Sends all of ERC20 `token` from `from` to `to`.\n /// Reverts upon failure.\n ///\n /// The `from` account must have their entire balance approved for the current contract to manage.\n function safeTransferAllFrom(address token, address from, address to)\n internal\n returns (uint256 amount)\n {\n /// @solidity memory-safe-assembly\n assembly {\n let m := mload(0x40) // Cache the free memory pointer.\n mstore(0x40, to) // Store the `to` argument.\n mstore(0x2c, shl(96, from)) // Store the `from` argument.\n mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\n // Read the balance, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n gt(returndatasize(), 0x1f), // At least 32 bytes returned.\n staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)\n )\n ) {\n mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.\n amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.\n // Perform the transfer, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x7939f424) // `TransferFromFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x60, 0) // Restore the zero slot to zero.\n mstore(0x40, m) // Restore the free memory pointer.\n }\n }\n\n /// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.\n /// Reverts upon failure.\n function safeTransfer(address token, address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n mstore(0x14, to) // Store the `to` argument.\n mstore(0x34, amount) // Store the `amount` argument.\n mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.\n // Perform the transfer, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\n }\n }\n\n /// @dev Sends all of ERC20 `token` from the current contract to `to`.\n /// Reverts upon failure.\n function safeTransferAll(address token, address to) internal returns (uint256 amount) {\n /// @solidity memory-safe-assembly\n assembly {\n mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.\n mstore(0x20, address()) // Store the address of the current contract.\n // Read the balance, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n gt(returndatasize(), 0x1f), // At least 32 bytes returned.\n staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)\n )\n ) {\n mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x14, to) // Store the `to` argument.\n amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.\n mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.\n // Perform the transfer, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x90b8ec18) // `TransferFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\n }\n }\n\n /// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.\n /// Reverts upon failure.\n function safeApprove(address token, address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n mstore(0x14, to) // Store the `to` argument.\n mstore(0x34, amount) // Store the `amount` argument.\n mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\n // Perform the approval, reverting upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.\n revert(0x1c, 0x04)\n }\n mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\n }\n }\n\n /// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.\n /// If the initial attempt to approve fails, attempts to reset the approved amount to zero,\n /// then retries the approval again (some tokens, e.g. USDT, requires this).\n /// Reverts upon failure.\n function safeApproveWithRetry(address token, address to, uint256 amount) internal {\n /// @solidity memory-safe-assembly\n assembly {\n mstore(0x14, to) // Store the `to` argument.\n mstore(0x34, amount) // Store the `amount` argument.\n mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\n // Perform the approval, retrying upon failure.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\n )\n ) {\n mstore(0x34, 0) // Store 0 for the `amount`.\n mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.\n pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.\n mstore(0x34, amount) // Store back the original `amount`.\n // Retry the approval, reverting upon failure.\n if iszero(\n and(\n or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.\n call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)\n )\n ) {\n mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.\n revert(0x1c, 0x04)\n }\n }\n mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.\n }\n }\n\n /// @dev Returns the amount of ERC20 `token` owned by `account`.\n /// Returns zero if the `token` does not exist.\n function balanceOf(address token, address account) internal view returns (uint256 amount) {\n /// @solidity memory-safe-assembly\n assembly {\n mstore(0x14, account) // Store the `account` argument.\n mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.\n amount :=\n mul( // The arguments of `mul` are evaluated from right to left.\n mload(0x20),\n and( // The arguments of `and` are evaluated from right to left.\n gt(returndatasize(), 0x1f), // At least 32 bytes returned.\n staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)\n )\n )\n }\n }\n\n /// @dev Sends `amount` of ERC20 `token` from `from` to `to`.\n /// If the initial attempt fails, try to use Permit2 to transfer the token.\n /// Reverts upon failure.\n ///\n /// The `from` account must have at least `amount` approved for the current contract to manage.\n function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {\n if (!trySafeTransferFrom(token, from, to, amount)) {\n permit2TransferFrom(token, from, to, amount);\n }\n }\n\n /// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.\n /// Reverts upon failure.\n function permit2TransferFrom(address token, address from, address to, uint256 amount)\n internal\n {\n /// @solidity memory-safe-assembly\n assembly {\n let m := mload(0x40)\n mstore(add(m, 0x74), shr(96, shl(96, token)))\n mstore(add(m, 0x54), amount)\n mstore(add(m, 0x34), to)\n mstore(add(m, 0x20), shl(96, from))\n // `transferFrom(address,address,uint160,address)`.\n mstore(m, 0x36c78516000000000000000000000000)\n let p := PERMIT2\n let exists := eq(chainid(), 1)\n if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }\n if iszero(and(call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00), exists)) {\n mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.\n revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)\n }\n }\n }\n\n /// @dev Permit a user to spend a given amount of\n /// another user's tokens via native EIP-2612 permit if possible, falling\n /// back to Permit2 if native permit fails or is not implemented on the token.\n function permit2(\n address token,\n address owner,\n address spender,\n uint256 amount,\n uint256 deadline,\n uint8 v,\n bytes32 r,\n bytes32 s\n ) internal {\n bool success;\n /// @solidity memory-safe-assembly\n assembly {\n for {} shl(96, xor(token, WETH9)) {} {\n mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.\n // Gas stipend to limit gas burn for tokens that don't refund gas when\n // an non-existing function is called. 5K should be enough for a SLOAD.\n staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)\n )\n ) { break }\n // After here, we can be sure that token is a contract.\n let m := mload(0x40)\n mstore(add(m, 0x34), spender)\n mstore(add(m, 0x20), shl(96, owner))\n mstore(add(m, 0x74), deadline)\n if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {\n mstore(0x14, owner)\n mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.\n mstore(add(m, 0x94), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))\n mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.\n // `nonces` is already at `add(m, 0x54)`.\n // `1` is already stored at `add(m, 0x94)`.\n mstore(add(m, 0xb4), and(0xff, v))\n mstore(add(m, 0xd4), r)\n mstore(add(m, 0xf4), s)\n success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)\n break\n }\n mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.\n mstore(add(m, 0x54), amount)\n mstore(add(m, 0x94), and(0xff, v))\n mstore(add(m, 0xb4), r)\n mstore(add(m, 0xd4), s)\n success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)\n break\n }\n }\n if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);\n }\n\n /// @dev Simple permit on the Permit2 contract.\n function simplePermit2(\n address token,\n address owner,\n address spender,\n uint256 amount,\n uint256 deadline,\n uint8 v,\n bytes32 r,\n bytes32 s\n ) internal {\n /// @solidity memory-safe-assembly\n assembly {\n let m := mload(0x40)\n mstore(m, 0x927da105) // `allowance(address,address,address)`.\n {\n let addressMask := shr(96, not(0))\n mstore(add(m, 0x20), and(addressMask, owner))\n mstore(add(m, 0x40), and(addressMask, token))\n mstore(add(m, 0x60), and(addressMask, spender))\n mstore(add(m, 0xc0), and(addressMask, spender))\n }\n let p := mul(PERMIT2, iszero(shr(160, amount)))\n if iszero(\n and( // The arguments of `and` are evaluated from right to left.\n gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.\n staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)\n )\n ) {\n mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.\n revert(add(0x18, shl(2, iszero(p))), 0x04)\n }\n mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).\n // `owner` is already `add(m, 0x20)`.\n // `token` is already at `add(m, 0x40)`.\n mstore(add(m, 0x60), amount)\n mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.\n // `nonce` is already at `add(m, 0xa0)`.\n // `spender` is already at `add(m, 0xc0)`.\n mstore(add(m, 0xe0), deadline)\n mstore(add(m, 0x100), 0x100) // `signature` offset.\n mstore(add(m, 0x120), 0x41) // `signature` length.\n mstore(add(m, 0x140), r)\n mstore(add(m, 0x160), s)\n mstore(add(m, 0x180), shl(248, v))\n if iszero(call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00)) {\n mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.\n revert(0x1c, 0x04)\n }\n }\n }\n}\n"},"lib/solady/src/utils/MetadataReaderLib.sol":{"content":"// SPDX-License-Identifier: MIT\npragma solidity ^0.8.4;\n\n/// @notice Library for reading contract metadata robustly.\n/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/MetadataReaderLib.sol)\nlibrary MetadataReaderLib {\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* CONSTANTS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n /// @dev Default gas stipend for contract reads. High enough for most practical use cases\n /// (able to SLOAD about 1000 bytes of data), but low enough to prevent griefing.\n uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;\n\n /// @dev Default string byte length limit.\n uint256 internal constant STRING_LIMIT_DEFAULT = 1000;\n\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* METADATA READING OPERATIONS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n // Best-effort string reading operations.\n // Should NOT revert as long as sufficient gas is provided.\n //\n // Performs the following in order:\n // 1. Returns the empty string for the following cases:\n // - Reverts.\n // - No returndata (e.g. function returns nothing, EOA).\n // - Returns empty string.\n // 2. Attempts to `abi.decode` the returndata into a string.\n // 3. With any remaining gas, scans the returndata from start to end for the\n // null byte '\\0', to interpret the returndata as a null-terminated string.\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"name()\"))`.\n function readName(address target) internal view returns (string memory) {\n return _string(target, _ptr(0x06fdde03), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"name()\"), limit)`.\n function readName(address target, uint256 limit) internal view returns (string memory) {\n return _string(target, _ptr(0x06fdde03), limit, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"name()\"), limit, gasStipend)`.\n function readName(address target, uint256 limit, uint256 gasStipend)\n internal\n view\n returns (string memory)\n {\n return _string(target, _ptr(0x06fdde03), limit, gasStipend);\n }\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"symbol()\"))`.\n function readSymbol(address target) internal view returns (string memory) {\n return _string(target, _ptr(0x95d89b41), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"symbol()\"), limit)`.\n function readSymbol(address target, uint256 limit) internal view returns (string memory) {\n return _string(target, _ptr(0x95d89b41), limit, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Equivalent to `readString(abi.encodeWithSignature(\"symbol()\"), limit, gasStipend)`.\n function readSymbol(address target, uint256 limit, uint256 gasStipend)\n internal\n view\n returns (string memory)\n {\n return _string(target, _ptr(0x95d89b41), limit, gasStipend);\n }\n\n /// @dev Performs a best-effort string query on `target` with `data` as the calldata.\n /// The string will be truncated to `STRING_LIMIT_DEFAULT` (1000) bytes.\n function readString(address target, bytes memory data) internal view returns (string memory) {\n return _string(target, _ptr(data), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Performs a best-effort string query on `target` with `data` as the calldata.\n /// The string will be truncated to `limit` bytes.\n function readString(address target, bytes memory data, uint256 limit)\n internal\n view\n returns (string memory)\n {\n return _string(target, _ptr(data), limit, GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Performs a best-effort string query on `target` with `data` as the calldata.\n /// The string will be truncated to `limit` bytes.\n function readString(address target, bytes memory data, uint256 limit, uint256 gasStipend)\n internal\n view\n returns (string memory)\n {\n return _string(target, _ptr(data), limit, gasStipend);\n }\n\n // Best-effort unsigned integer reading operations.\n // Should NOT revert as long as sufficient gas is provided.\n //\n // Performs the following in order:\n // 1. Attempts to `abi.decode` the result into a uint256\n // (equivalent across all Solidity uint types, downcast as needed).\n // 2. Returns zero for the following cases:\n // - Reverts.\n // - No returndata (e.g. function returns nothing, EOA).\n // - Returns zero.\n // - `abi.decode` failure.\n\n /// @dev Equivalent to `uint8(readUint(abi.encodeWithSignature(\"decimal()\")))`.\n function readDecimals(address target) internal view returns (uint8) {\n return uint8(_uint(target, _ptr(0x313ce567), GAS_STIPEND_NO_GRIEF));\n }\n\n /// @dev Equivalent to `uint8(readUint(abi.encodeWithSignature(\"decimal()\"), gasStipend))`.\n function readDecimals(address target, uint256 gasStipend) internal view returns (uint8) {\n return uint8(_uint(target, _ptr(0x313ce567), gasStipend));\n }\n\n /// @dev Performs a best-effort uint query on `target` with `data` as the calldata.\n function readUint(address target, bytes memory data) internal view returns (uint256) {\n return _uint(target, _ptr(data), GAS_STIPEND_NO_GRIEF);\n }\n\n /// @dev Performs a best-effort uint query on `target` with `data` as the calldata.\n function readUint(address target, bytes memory data, uint256 gasStipend)\n internal\n view\n returns (uint256)\n {\n return _uint(target, _ptr(data), gasStipend);\n }\n\n /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/\n /* PRIVATE HELPERS */\n /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/\n\n /// @dev Attempts to read and return a string at `target`.\n function _string(address target, bytes32 ptr, uint256 limit, uint256 gasStipend)\n private\n view\n returns (string memory result)\n {\n /// @solidity memory-safe-assembly\n assembly {\n function min(x_, y_) -> _z {\n _z := xor(x_, mul(xor(x_, y_), lt(y_, x_)))\n }\n for {} staticcall(gasStipend, target, add(ptr, 0x20), mload(ptr), 0x00, 0x20) {} {\n let m := mload(0x40) // Grab the free memory pointer.\n let s := add(0x20, m) // Start of the string's bytes in memory.\n // Attempt to `abi.decode` if the returndatasize is greater or equal to 64.\n if iszero(lt(returndatasize(), 0x40)) {\n let o := mload(0x00) // Load the string's offset in the returndata.\n // If the string's offset is within bounds.\n if iszero(gt(o, sub(returndatasize(), 0x20))) {\n returndatacopy(m, o, 0x20) // Copy the string's length.\n // If the full string's end is within bounds.\n // Note: If the full string doesn't fit, the `abi.decode` must be aborted\n // for compliance purposes, regardless if the truncated string can fit.\n if iszero(gt(mload(m), sub(returndatasize(), add(o, 0x20)))) {\n let n := min(mload(m), limit) // Truncate if needed.\n mstore(m, n) // Overwrite the length.\n returndatacopy(s, add(o, 0x20), n) // Copy the string's bytes.\n mstore(add(s, n), 0) // Zeroize the slot after the string.\n mstore(0x40, add(0x20, add(s, n))) // Allocate memory for the string.\n result := m\n break\n }\n }\n }\n // Try interpreting as a null-terminated string.\n let n := min(returndatasize(), limit) // Truncate if needed.\n returndatacopy(s, 0, n) // Copy the string's bytes.\n mstore8(add(s, n), 0) // Place a '\\0' at the end.\n let i := s // Pointer to the next byte to scan.\n for {} byte(0, mload(i)) { i := add(i, 1) } {} // Scan for '\\0'.\n mstore(m, sub(i, s)) // Store the string's length.\n mstore(i, 0) // Zeroize the slot after the string.\n mstore(0x40, add(0x20, i)) // Allocate memory for the string.\n result := m\n break\n }\n }\n }\n\n /// @dev Attempts to read and return a uint at `target`.\n function _uint(address target, bytes32 ptr, uint256 gasStipend)\n private\n view\n returns (uint256 result)\n {\n /// @solidity memory-safe-assembly\n assembly {\n result :=\n mul(\n mload(0x20),\n and( // The arguments of `and` are evaluated from right to left.\n gt(returndatasize(), 0x1f), // At least 32 bytes returned.\n staticcall(gasStipend, target, add(ptr, 0x20), mload(ptr), 0x20, 0x20)\n )\n )\n }\n }\n\n /// @dev Casts the function selector `s` into a pointer.\n function _ptr(uint256 s) private pure returns (bytes32 result) {\n /// @solidity memory-safe-assembly\n assembly {\n // Layout the calldata in the scratch space for temporary usage.\n mstore(0x04, s) // Store the function selector.\n mstore(result, 4) // Store the length.\n }\n }\n\n /// @dev Casts the `data` into a pointer.\n function _ptr(bytes memory data) private pure returns (bytes32 result) {\n /// @solidity memory-safe-assembly\n assembly {\n result := data\n }\n }\n}\n"}},"settings":{"remappings":["ds-test/=lib/forge-std/lib/ds-test/src/","forge-std/=lib/forge-std/src/","solady/=lib/solady/src/"],"optimizer":{"enabled":true,"runs":9999999},"metadata":{"useLiteralContent":false,"bytecodeHash":"ipfs","appendCBOR":true},"outputSelection":{"*":{"*":["abi","evm.bytecode","evm.deployedBytecode","evm.methodIdentifiers","metadata"]}},"evmVersion":"cancun","viaIR":true,"libraries":{}}} diff --git a/lib/forge-std b/lib/forge-std index bb4ceea..e4aef94 160000 --- a/lib/forge-std +++ b/lib/forge-std @@ -1 +1 @@ -Subproject commit bb4ceea94d6f10eeb5b41dc2391c6c8bf8e734ef +Subproject commit e4aef94c1768803a16fe19f7ce8b65defd027cfd diff --git a/lib/solady b/lib/solady index 3a4e387..e4a14a5 160000 --- a/lib/solady +++ b/lib/solady @@ -1 +1 @@ -Subproject commit 3a4e38702e1a87f53c21f6afaf70eb8738535560 +Subproject commit e4a14a5b365b353352f7c38e699a2bc9363d6576 diff --git a/scripts/initCode.sh b/scripts/initCode.sh new file mode 100644 index 0000000..3abfab2 --- /dev/null +++ b/scripts/initCode.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +echo $(cast concat-hex "$(forge inspect $1 bytecode)" "$(cast abi-encode "constructor($(forge inspect $1 abi \ + | jq -r '.[] | select(.type == "constructor") | .inputs | map(.type) | join(",")'))" ${@:2})") \ No newline at end of file diff --git a/scripts/initCodeHash.sh b/scripts/initCodeHash.sh new file mode 100644 index 0000000..d053302 --- /dev/null +++ b/scripts/initCodeHash.sh @@ -0,0 +1,3 @@ +#!/bin/bash +echo $(cast keccak $(cast concat-hex "$(forge inspect $1 bytecode)" "$(cast abi-encode "constructor($(forge inspect $1 abi \ + | jq -r '.[] | select(.type == "constructor") | .inputs | map(.type) | join(",")'))" ${@:2})")) \ No newline at end of file diff --git a/test/IE.t.sol b/test/IE.t.sol index d45402f..2b6a1e8 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -64,13 +64,13 @@ contract IETest is Test { assertEq(receiver, Z0R0Z_DOT_ETH); } - /*function testPreviewSendCommand() public payable { + function testPreviewSendCommand() public payable { string memory command = "send z0r0z 20 dai"; (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20 ether); assertEq(asset, DAI); - }*/ + } function testPreviewSendCommandRawAddr() public payable { string memory command = "send 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20 20 dai"; From 71dad8511a1b902c9d38a2516d690ed775d760a2 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:16:49 +0000 Subject: [PATCH 19/22] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5e604a..9af5525 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ V1: [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/ V1.2: [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) -Note: L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. +`Note:` L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. ## Uses From 6d1168cdd42d680ba733a4cce0562383696cbd44 Mon Sep 17 00:00:00 2001 From: z0r0z Date: Thu, 28 Mar 2024 15:24:10 +0700 Subject: [PATCH 20/22] forge install: solady v0.0.181 --- .gitmodules | 5 ++++- lib/solady | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) create mode 160000 lib/solady diff --git a/.gitmodules b/.gitmodules index 4c1b977..0f07815 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "lib/forge-std"] path = lib/forge-std - url = https://github.com/foundry-rs/forge-std \ No newline at end of file + url = https://github.com/foundry-rs/forge-std +[submodule "lib/solady"] + path = lib/solady + url = https://github.com/vectorized/solady diff --git a/lib/solady b/lib/solady new file mode 160000 index 0000000..e4a14a5 --- /dev/null +++ b/lib/solady @@ -0,0 +1 @@ +Subproject commit e4a14a5b365b353352f7c38e699a2bc9363d6576 From f061f69f55a660146bbc3247dded252faef04a99 Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:51:11 +0000 Subject: [PATCH 21/22] =?UTF-8?q?=E2=9C=B5=201.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 18 +- README.md | 42 +- abi/DeploymentV1.json | 539 -------- abi/IE.json | 1240 +++++++++--------- docs/src/README.md | 28 +- docs/src/src/IE.sol/contract.IE.md | 44 +- docs/src/src/IE.sol/interface.IExecutor.md | 2 +- docs/src/src/IE.sol/interface.INAMI.md | 2 +- docs/src/src/IE.sol/interface.ISwapRouter.md | 2 +- docs/src/src/IE.sol/interface.IToken.md | 2 +- docs/src/src/NAMI.sol/contract.NAMI.md | 2 +- docs/src/src/NAMI.sol/interface.IToken.md | 2 +- src/IE.sol | 21 - test/IE.t.sol | 2 - 14 files changed, 664 insertions(+), 1282 deletions(-) delete mode 100644 abi/DeploymentV1.json diff --git a/.gas-snapshot b/.gas-snapshot index 129862d..c0fa0d3 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,14 +1,14 @@ -IETest:testCommandDepositETH() (gas: 154672) +IETest:testCommandDepositETH() (gas: 155598) IETest:testCommandSendETH() (gas: 75320) IETest:testCommandSendETHRawAddr() (gas: 75755) -IETest:testCommandStakeETH() (gas: 147764) -IETest:testCommandSwapDAI() (gas: 137475) -IETest:testCommandSwapETH() (gas: 168371) -IETest:testCommandSwapForETH() (gas: 144376) -IETest:testCommandSwapUSDC() (gas: 171415) -IETest:testCommandSwapUSDCForWBTC() (gas: 196380) -IETest:testCommandUnstakeETH() (gas: 261654) -IETest:testCommandWithdrawETH() (gas: 264357) +IETest:testCommandStakeETH() (gas: 147756) +IETest:testCommandSwapDAI() (gas: 138503) +IETest:testCommandSwapETH() (gas: 138209) +IETest:testCommandSwapForETH() (gas: 145404) +IETest:testCommandSwapUSDC() (gas: 171497) +IETest:testCommandSwapUSDCForWBTC() (gas: 196200) +IETest:testCommandUnstakeETH() (gas: 263506) +IETest:testCommandWithdrawETH() (gas: 266209) IETest:testDeploy() (gas: 2563979) IETest:testENSNameOwnership() (gas: 48424) IETest:testIENameSetting() (gas: 11105) diff --git a/README.md b/README.md index acdac9c..7910b4a 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,18 @@ -# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) +# [`IE`](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) -The **Intents Engine** (IE): A Basic *Text-to-tx* Simulator Contract. +The **Intents Engine** (`IE`): A Basic *Text-to-tx* Simulator Contract. -<<<<<<< HEAD ## Deployments ### Mainnet -V1: [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) +`V1:` [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) ### Arbitrum -V1.2: [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) +`V1.2:` [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) -`Note:` L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. -======= -## Deployment - -### IE V1 - -#### Ethereum Mainnet (L1) - -[`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) - -##### Deployed Bytecode - -[`QmZVdxJyTCJBF7gfE5BrSavCHtMDScZjpLYH5qaMExhW49`](https://content.wrappr.wtf/ipfs/QmZVdxJyTCJBF7gfE5BrSavCHtMDScZjpLYH5qaMExhW49) ->>>>>>> main +`Note:` L2 will be used to rapidly prototype a stable and sufficient `IE` for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of `V2` on L1. ## Uses @@ -36,13 +22,13 @@ From natural language: - Security checkpoint (forbid tx) - Command translation (make tx) -IE should deterministically and transparently operate to provide these utilities in an uncensorable medium like a Solidity smart contract. +`IE` should deterministically and transparently operate to provide these utilities in an uncensorable medium like a Solidity smart contract. [`V1`](./src/IE.sol) is a POC of this. [*Short demo and explainer thread on X.*](https://x.com/z0r0zzz/status/1758392014737920209?s=20) ## Command Syntax (⌘) -IE is approaching things from first-principles and a "show" rather than "tell" approach. There will be some experimentation. +`IE` is approaching things from first-principles and a "show" rather than "tell" approach. There will be some experimentation. Some things in `V1` are likely very underoptimized for this particular use case. @@ -86,21 +72,21 @@ aliases: *exchange* aliases: *exchange* -Note: In `V1.1` on Arbitrum, a `minOutputAmount` can be specified for swaps. It ensures that you receive a minimum output amount of `object` at the end of the swap, otherwise the transaction will revert. The default value is set to `0`. +`Note:` In `V1.2` on Arbitrum, a `minOutputAmount` can be specified for swaps. It ensures that you receive a minimum output amount of `object` at the end of the swap, otherwise the transaction will revert. The default value is set to `0`. ------------------------------------ -Phrases are provided in the order in which they are most expected. They are "naturalized" to lower case. The IE contract automatically does this, but front-ends should nonetheless try and format as close as possible (*i.e.*, through a simple LLM trained or prompted on these examples below). +Phrases are provided in the order in which they are most expected. They are "naturalized" to lower case. The `IE` contract automatically does this, but front-ends should nonetheless try and format as close as possible (*i.e.*, through a simple LLM trained or prompted on these examples below). -In terms of usual English, we assume the subject of each command is the user account which is more explicit in the case of checking an ERC4337 userOp (where `sender` is the user). And the `object` receives assets or contract calls. `value` is the token or ETH amount involved in the action and the `asset` is the particular item sent or issued from (initially ETH or ERC20 to cover fungibles and most immediate security needs that could benefit from IE). +In terms of usual English, we assume the subject of each command is the user account which is more explicit in the case of checking an ERC4337 userOp (where `sender` is the user). And the `object` receives assets or contract calls. `value` is the token or ETH amount involved in the action and the `asset` is the particular item sent or issued from (initially ETH or ERC20 to cover fungibles and most immediate security needs that could benefit from `IE`). -You MUST include spaces in the string provided to IE in order for it to understand word separation. +You **MUST** include spaces in the string provided to `IE` in order for it to understand word separation. -Note: `to/for` is an identified filler word common to most of the transactions we will cover so it is highlighted. +`Note:` `to/for` is an identified filler word common to most of the transactions we will cover so it is highlighted. As you might notice, there are patterns. Because after all this is typical language and logic we are talking about here. For example, `value` will precede `asset`. `object` will either follow the `action` or be at the end. If at the end, there will be a filler of `for/to`. (Yeah I know this is what people learn in grammar schools but the exercise will likely yield good results.) -Also, let's try and be as helpful as possible at the top of the command funnel. E.g., if there is `msg.value` in a command, then we should assume ETH is involved. +Also, let's try and be as helpful as possible at the top of the command funnel. *E.g.*, if there is `msg.value` in a command, then we should assume ETH is involved. Actions should also have aliases to catch more cases. Though it will be cheaper to use the primary word (for example, 'send' or 'swap' with preference to familiarity, and if there indecision, the shorter), it is helpful to do more and catch different ways of phrasing transactional commands, like "send" can equate to "transfer" when it comes to onchain assets. Adhering to Solidity and smart contract functions themselves in word choice makes the most sense as well (*e.g.*, ETH.*send*/*transfer*, IERC20.*transfer*, UNI.*swap*, CURVE.*exchange*). @@ -110,7 +96,7 @@ Run: `curl -L https://foundry.paradigm.xyz | bash && source ~/.bashrc && foundry Build the foundry project with `forge build`. Run tests with `forge test`. Measure gas with `forge snapshot`. Format with `forge fmt`. -*Note:* Tests currently run on a fork of mainnet to check ENS properly. +`Note:` Tests currently run on a virtual blockchain fork to check ENS properly. ## Blueprint diff --git a/abi/DeploymentV1.json b/abi/DeploymentV1.json deleted file mode 100644 index 23d0bf4..0000000 --- a/abi/DeploymentV1.json +++ /dev/null @@ -1,539 +0,0 @@ -{ - "contracts": [ - { - "name": "IE", - "address": "0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00", - "version": "1.0.0", - "abi": [ - { - "type": "constructor", - "inputs": [], - "stateMutability": "payable" - }, - { - "type": "fallback", - "stateMutability": "payable" - }, - { - "type": "receive", - "stateMutability": "payable" - }, - { - "type": "function", - "name": "checkPackedUserOp", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - }, - { - "name": "userOp", - "type": "tuple", - "internalType": "struct IE.PackedUserOperation", - "components": [ - { - "name": "sender", - "type": "address", - "internalType": "address" - }, - { - "name": "nonce", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "initCode", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "accountGasLimits", - "type": "bytes32", - "internalType": "bytes32" - }, - { - "name": "preVerificationGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "gasFees", - "type": "bytes32", - "internalType": "bytes32" - }, - { - "name": "paymasterAndData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "signature", - "type": "bytes", - "internalType": "bytes" - } - ] - } - ], - "outputs": [ - { - "name": "", - "type": "bool", - "internalType": "bool" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "checkUserOp", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - }, - { - "name": "userOp", - "type": "tuple", - "internalType": "struct IE.UserOperation", - "components": [ - { - "name": "sender", - "type": "address", - "internalType": "address" - }, - { - "name": "nonce", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "initCode", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callGasLimit", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "verificationGasLimit", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "preVerificationGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "maxFeePerGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "maxPriorityFeePerGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "paymasterAndData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "signature", - "type": "bytes", - "internalType": "bytes" - } - ] - } - ], - "outputs": [ - { - "name": "", - "type": "bool", - "internalType": "bool" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "command", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "previewCommand", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "to", - "type": "address", - "internalType": "address" - }, - { - "name": "amount", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "token", - "type": "address", - "internalType": "address" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "executeCallData", - "type": "bytes", - "internalType": "bytes" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "previewSend", - "inputs": [ - { - "name": "to", - "type": "string", - "internalType": "string" - }, - { - "name": "amount", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "_to", - "type": "address", - "internalType": "address" - }, - { - "name": "_amount", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "_token", - "type": "address", - "internalType": "address" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "executeCallData", - "type": "bytes", - "internalType": "bytes" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "previewSwap", - "inputs": [ - { - "name": "amountIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenOut", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "_amountIn", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "_tokenIn", - "type": "address", - "internalType": "address" - }, - { - "name": "_tokenOut", - "type": "address", - "internalType": "address" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "send", - "inputs": [ - { - "name": "to", - "type": "string", - "internalType": "string" - }, - { - "name": "amount", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "setName", - "inputs": [ - { - "name": "token", - "type": "address", - "internalType": "address" - }, - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "setNameAndTicker", - "inputs": [ - { - "name": "token", - "type": "address", - "internalType": "address" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "swap", - "inputs": [ - { - "name": "amountIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenOut", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "tokens", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "", - "type": "address", - "internalType": "address" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheAddressOf", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "owner", - "type": "address", - "internalType": "address" - }, - { - "name": "receiver", - "type": "address", - "internalType": "address" - }, - { - "name": "node", - "type": "bytes32", - "internalType": "bytes32" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheBalanceOf", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "balance", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "balanceAdjusted", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheTotalSupplyOf", - "inputs": [ - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "supply", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "supplyAdjusted", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, - { - "type": "event", - "name": "NameSet", - "inputs": [ - { - "name": "token", - "type": "address", - "indexed": true, - "internalType": "address" - }, - { - "name": "name", - "type": "string", - "indexed": false, - "internalType": "string" - } - ], - "anonymous": false - }, - { - "type": "error", - "name": "InvalidCharacter", - "inputs": [] - }, - { - "type": "error", - "name": "InvalidSwap", - "inputs": [] - }, - { - "type": "error", - "name": "InvalidSyntax", - "inputs": [] - }, - { - "type": "error", - "name": "Overflow", - "inputs": [] - }, - { - "type": "error", - "name": "Unauthorized", - "inputs": [] - } - ] - } - ] - } - \ No newline at end of file diff --git a/abi/IE.json b/abi/IE.json index 6c2e6d0..da2aad5 100644 --- a/abi/IE.json +++ b/abi/IE.json @@ -1,621 +1,621 @@ [ - { - "type": "constructor", - "inputs": [], - "stateMutability": "payable" - }, - { - "type": "fallback", - "stateMutability": "payable" - }, - { - "type": "receive", - "stateMutability": "payable" - }, - { - "type": "function", - "name": "checkPackedUserOp", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - }, - { - "name": "userOp", - "type": "tuple", - "internalType": "struct IE.PackedUserOperation", - "components": [ - { - "name": "sender", - "type": "address", - "internalType": "address" - }, - { - "name": "nonce", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "initCode", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "accountGasLimits", - "type": "bytes32", - "internalType": "bytes32" - }, - { - "name": "preVerificationGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "gasFees", - "type": "bytes32", - "internalType": "bytes32" - }, - { - "name": "paymasterAndData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "signature", - "type": "bytes", - "internalType": "bytes" - } - ] - } - ], - "outputs": [ - { - "name": "", - "type": "bool", - "internalType": "bool" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "checkUserOp", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - }, - { - "name": "userOp", - "type": "tuple", - "internalType": "struct IE.UserOperation", - "components": [ - { - "name": "sender", - "type": "address", - "internalType": "address" - }, - { - "name": "nonce", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "initCode", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "callGasLimit", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "verificationGasLimit", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "preVerificationGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "maxFeePerGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "maxPriorityFeePerGas", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "paymasterAndData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "signature", - "type": "bytes", - "internalType": "bytes" - } - ] - } - ], - "outputs": [ - { - "name": "", - "type": "bool", - "internalType": "bool" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "command", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "pairs", - "inputs": [ - { - "name": "token0", - "type": "address", - "internalType": "address" - }, - { - "name": "token1", - "type": "address", - "internalType": "address" - } - ], - "outputs": [ - { - "name": "", - "type": "address", - "internalType": "address" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "previewCommand", - "inputs": [ - { - "name": "intent", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "to", - "type": "address", - "internalType": "address" - }, - { - "name": "amount", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "minAmountOut", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "token", - "type": "address", - "internalType": "address" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "executeCallData", - "type": "bytes", - "internalType": "bytes" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "previewSend", - "inputs": [ - { - "name": "to", - "type": "string", - "internalType": "string" - }, - { - "name": "amount", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "_to", - "type": "address", - "internalType": "address" - }, - { - "name": "_amount", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "_token", - "type": "address", - "internalType": "address" - }, - { - "name": "callData", - "type": "bytes", - "internalType": "bytes" - }, - { - "name": "executeCallData", - "type": "bytes", - "internalType": "bytes" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "previewSwap", - "inputs": [ - { - "name": "amountIn", - "type": "string", - "internalType": "string" - }, - { - "name": "amountOutMinimum", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenOut", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "_amountIn", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "_amountOut", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "_tokenIn", - "type": "address", - "internalType": "address" - }, - { - "name": "_tokenOut", - "type": "address", - "internalType": "address" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "send", - "inputs": [ - { - "name": "to", - "type": "string", - "internalType": "string" - }, - { - "name": "amount", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "setName", - "inputs": [ - { - "name": "token", - "type": "address", - "internalType": "address" - }, - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "setNameAndTicker", - "inputs": [ - { - "name": "token", - "type": "address", - "internalType": "address" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "setPair", - "inputs": [ - { - "name": "tokenA", - "type": "address", - "internalType": "address" - }, - { - "name": "tokenB", - "type": "address", - "internalType": "address" - }, - { - "name": "pair", - "type": "address", - "internalType": "address" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "swap", - "inputs": [ - { - "name": "amountIn", - "type": "string", - "internalType": "string" - }, - { - "name": "amountOutMinimum", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenIn", - "type": "string", - "internalType": "string" - }, - { - "name": "tokenOut", - "type": "string", - "internalType": "string" - } - ], - "outputs": [], - "stateMutability": "payable" - }, - { - "type": "function", - "name": "tokens", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "", - "type": "address", - "internalType": "address" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheAddressOf", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "owner", - "type": "address", - "internalType": "address" - }, - { - "name": "receiver", - "type": "address", - "internalType": "address" - }, - { - "name": "node", - "type": "bytes32", - "internalType": "bytes32" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheBalanceOf", - "inputs": [ - { - "name": "name", - "type": "string", - "internalType": "string" - }, - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "balance", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "balanceAdjusted", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, - { - "type": "function", - "name": "whatIsTheTotalSupplyOf", - "inputs": [ - { - "name": "token", - "type": "string", - "internalType": "string" - } - ], - "outputs": [ - { - "name": "supply", - "type": "uint256", - "internalType": "uint256" - }, - { - "name": "supplyAdjusted", - "type": "uint256", - "internalType": "uint256" - } - ], - "stateMutability": "view" - }, - { - "type": "event", - "name": "NameSet", - "inputs": [ - { - "name": "token", - "type": "address", - "indexed": true, - "internalType": "address" - }, - { - "name": "name", - "type": "string", - "indexed": false, - "internalType": "string" - } - ], - "anonymous": false - }, - { - "type": "event", - "name": "PairSet", - "inputs": [ - { - "name": "token0", - "type": "address", - "indexed": true, - "internalType": "address" - }, - { - "name": "token1", - "type": "address", - "indexed": true, - "internalType": "address" - }, - { - "name": "pair", - "type": "address", - "indexed": false, - "internalType": "address" - } - ], - "anonymous": false - }, - { - "type": "error", - "name": "InsufficientSwap", - "inputs": [] - }, - { - "type": "error", - "name": "InvalidCharacter", - "inputs": [] - }, - { - "type": "error", - "name": "InvalidSwap", - "inputs": [] - }, - { - "type": "error", - "name": "InvalidSyntax", - "inputs": [] - }, - { - "type": "error", - "name": "Overflow", - "inputs": [] - } - ] \ No newline at end of file + { + "type": "constructor", + "inputs": [], + "stateMutability": "payable" + }, + { + "type": "fallback", + "stateMutability": "payable" + }, + { + "type": "receive", + "stateMutability": "payable" + }, + { + "type": "function", + "name": "checkPackedUserOp", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + }, + { + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.PackedUserOperation", + "components": [ + { + "name": "sender", + "type": "address", + "internalType": "address" + }, + { + "name": "nonce", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "initCode", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "accountGasLimits", + "type": "bytes32", + "internalType": "bytes32" + }, + { + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "gasFees", + "type": "bytes32", + "internalType": "bytes32" + }, + { + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } + ], + "outputs": [ + { + "name": "", + "type": "bool", + "internalType": "bool" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "checkUserOp", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + }, + { + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.UserOperation", + "components": [ + { + "name": "sender", + "type": "address", + "internalType": "address" + }, + { + "name": "nonce", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "initCode", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "callGasLimit", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "verificationGasLimit", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "maxFeePerGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "maxPriorityFeePerGas", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } + ], + "outputs": [ + { + "name": "", + "type": "bool", + "internalType": "bool" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "command", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "pairs", + "inputs": [ + { + "name": "token0", + "type": "address", + "internalType": "address" + }, + { + "name": "token1", + "type": "address", + "internalType": "address" + } + ], + "outputs": [ + { + "name": "", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewCommand", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "to", + "type": "address", + "internalType": "address" + }, + { + "name": "amount", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "minAmountOut", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "token", + "type": "address", + "internalType": "address" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewSend", + "inputs": [ + { + "name": "to", + "type": "string", + "internalType": "string" + }, + { + "name": "amount", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "_to", + "type": "address", + "internalType": "address" + }, + { + "name": "_amount", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_token", + "type": "address", + "internalType": "address" + }, + { + "name": "callData", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewSwap", + "inputs": [ + { + "name": "amountIn", + "type": "string", + "internalType": "string" + }, + { + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenIn", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenOut", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "_amountIn", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_amountOut", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "_tokenIn", + "type": "address", + "internalType": "address" + }, + { + "name": "_tokenOut", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "send", + "inputs": [ + { + "name": "to", + "type": "string", + "internalType": "string" + }, + { + "name": "amount", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setName", + "inputs": [ + { + "name": "token", + "type": "address", + "internalType": "address" + }, + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setNameAndTicker", + "inputs": [ + { + "name": "token", + "type": "address", + "internalType": "address" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setPair", + "inputs": [ + { + "name": "tokenA", + "type": "address", + "internalType": "address" + }, + { + "name": "tokenB", + "type": "address", + "internalType": "address" + }, + { + "name": "pair", + "type": "address", + "internalType": "address" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "swap", + "inputs": [ + { + "name": "amountIn", + "type": "string", + "internalType": "string" + }, + { + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenIn", + "type": "string", + "internalType": "string" + }, + { + "name": "tokenOut", + "type": "string", + "internalType": "string" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "tokens", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheAddressOf", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "owner", + "type": "address", + "internalType": "address" + }, + { + "name": "receiver", + "type": "address", + "internalType": "address" + }, + { + "name": "node", + "type": "bytes32", + "internalType": "bytes32" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheBalanceOf", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + }, + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "balance", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "balanceAdjusted", + "type": "uint256", + "internalType": "uint256" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "whatIsTheTotalSupplyOf", + "inputs": [ + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ + { + "name": "supply", + "type": "uint256", + "internalType": "uint256" + }, + { + "name": "supplyAdjusted", + "type": "uint256", + "internalType": "uint256" + } + ], + "stateMutability": "view" + }, + { + "type": "event", + "name": "NameSet", + "inputs": [ + { + "name": "token", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "name", + "type": "string", + "indexed": false, + "internalType": "string" + } + ], + "anonymous": false + }, + { + "type": "event", + "name": "PairSet", + "inputs": [ + { + "name": "token0", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "token1", + "type": "address", + "indexed": true, + "internalType": "address" + }, + { + "name": "pair", + "type": "address", + "indexed": false, + "internalType": "address" + } + ], + "anonymous": false + }, + { + "type": "error", + "name": "InsufficientSwap", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidCharacter", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidSwap", + "inputs": [] + }, + { + "type": "error", + "name": "InvalidSyntax", + "inputs": [] + }, + { + "type": "error", + "name": "Overflow", + "inputs": [] + } +] \ No newline at end of file diff --git a/docs/src/README.md b/docs/src/README.md index c2742b7..7910b4a 100644 --- a/docs/src/README.md +++ b/docs/src/README.md @@ -1,18 +1,18 @@ -# [IE](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) +# [`IE`](https://github.com/NaniDAO/IE) [![License: AGPL-3.0-only](https://img.shields.io/badge/License-AGPL-black.svg)](https://opensource.org/license/agpl-v3/) [![solidity](https://img.shields.io/badge/solidity-%5E0.8.25-black)](https://docs.soliditylang.org/en/v0.8.25/) [![Foundry](https://img.shields.io/badge/Built%20with-Foundry-000000.svg)](https://getfoundry.sh/) ![tests](https://github.com/z0r0z/zenplate/actions/workflows/ci.yml/badge.svg) -The **Intents Engine** (IE): A Basic *Text-to-tx* Simulator Contract. +The **Intents Engine** (`IE`): A Basic *Text-to-tx* Simulator Contract. ## Deployments ### Mainnet -V1: [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) +`V1:` [`0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00`](https://etherscan.io/address/0x1e00000000cf8ba83e0005c59c1bf1c4682c8e00#code) ### Arbitrum -V1.2: [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) +`V1.2:` [`0x1e00003a669bb466d6B49800000099E1abDD6600`](https://arbiscan.io/address/0x1e00003a669bb466d6b49800000099e1abdd6600#code) -Note: L2 will be used to rapidly prototype a stable and sufficient IE for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of V2 on L1. +`Note:` L2 will be used to rapidly prototype a stable and sufficient `IE` for common crypto commands. Many dev resources here will cater to the current L2 prototype until the release of `V2` on L1. ## Uses @@ -22,13 +22,13 @@ From natural language: - Security checkpoint (forbid tx) - Command translation (make tx) -IE should deterministically and transparently operate to provide these utilities in an uncensorable medium like a Solidity smart contract. +`IE` should deterministically and transparently operate to provide these utilities in an uncensorable medium like a Solidity smart contract. [`V1`](./src/IE.sol) is a POC of this. [*Short demo and explainer thread on X.*](https://x.com/z0r0zzz/status/1758392014737920209?s=20) ## Command Syntax (⌘) -IE is approaching things from first-principles and a "show" rather than "tell" approach. There will be some experimentation. +`IE` is approaching things from first-principles and a "show" rather than "tell" approach. There will be some experimentation. Some things in `V1` are likely very underoptimized for this particular use case. @@ -72,21 +72,21 @@ aliases: *exchange* aliases: *exchange* -Note: In `V1.1` on Arbitrum, a `minOutputAmount` can be specified for swaps. It ensures that you receive a minimum output amount of `object` at the end of the swap, otherwise the transaction will revert. The default value is set to `0`. +`Note:` In `V1.2` on Arbitrum, a `minOutputAmount` can be specified for swaps. It ensures that you receive a minimum output amount of `object` at the end of the swap, otherwise the transaction will revert. The default value is set to `0`. ------------------------------------ -Phrases are provided in the order in which they are most expected. They are "naturalized" to lower case. The IE contract automatically does this, but front-ends should nonetheless try and format as close as possible (*i.e.*, through a simple LLM trained or prompted on these examples below). +Phrases are provided in the order in which they are most expected. They are "naturalized" to lower case. The `IE` contract automatically does this, but front-ends should nonetheless try and format as close as possible (*i.e.*, through a simple LLM trained or prompted on these examples below). -In terms of usual English, we assume the subject of each command is the user account which is more explicit in the case of checking an ERC4337 userOp (where `sender` is the user). And the `object` receives assets or contract calls. `value` is the token or ETH amount involved in the action and the `asset` is the particular item sent or issued from (initially ETH or ERC20 to cover fungibles and most immediate security needs that could benefit from IE). +In terms of usual English, we assume the subject of each command is the user account which is more explicit in the case of checking an ERC4337 userOp (where `sender` is the user). And the `object` receives assets or contract calls. `value` is the token or ETH amount involved in the action and the `asset` is the particular item sent or issued from (initially ETH or ERC20 to cover fungibles and most immediate security needs that could benefit from `IE`). -You MUST include spaces in the string provided to IE in order for it to understand word separation. +You **MUST** include spaces in the string provided to `IE` in order for it to understand word separation. -Note: `to/for` is an identified filler word common to most of the transactions we will cover so it is highlighted. +`Note:` `to/for` is an identified filler word common to most of the transactions we will cover so it is highlighted. As you might notice, there are patterns. Because after all this is typical language and logic we are talking about here. For example, `value` will precede `asset`. `object` will either follow the `action` or be at the end. If at the end, there will be a filler of `for/to`. (Yeah I know this is what people learn in grammar schools but the exercise will likely yield good results.) -Also, let's try and be as helpful as possible at the top of the command funnel. E.g., if there is `msg.value` in a command, then we should assume ETH is involved. +Also, let's try and be as helpful as possible at the top of the command funnel. *E.g.*, if there is `msg.value` in a command, then we should assume ETH is involved. Actions should also have aliases to catch more cases. Though it will be cheaper to use the primary word (for example, 'send' or 'swap' with preference to familiarity, and if there indecision, the shorter), it is helpful to do more and catch different ways of phrasing transactional commands, like "send" can equate to "transfer" when it comes to onchain assets. Adhering to Solidity and smart contract functions themselves in word choice makes the most sense as well (*e.g.*, ETH.*send*/*transfer*, IERC20.*transfer*, UNI.*swap*, CURVE.*exchange*). @@ -96,7 +96,7 @@ Run: `curl -L https://foundry.paradigm.xyz | bash && source ~/.bashrc && foundry Build the foundry project with `forge build`. Run tests with `forge test`. Measure gas with `forge snapshot`. Format with `forge fmt`. -*Note:* Tests currently run on a fork of mainnet to check ENS properly. +`Note:` Tests currently run on a virtual blockchain fork to check ENS properly. ## Blueprint diff --git a/docs/src/src/IE.sol/contract.IE.md b/docs/src/src/IE.sol/contract.IE.md index 38d9db4..dd5a7e3 100644 --- a/docs/src/src/IE.sol/contract.IE.md +++ b/docs/src/src/IE.sol/contract.IE.md @@ -1,5 +1,5 @@ # IE -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) @@ -209,7 +209,6 @@ function previewCommand(string calldata intent) address to, uint256 amount, uint256 minAmountOut, - uint256 minAmountOut, address token, bytes memory callData, bytes memory executeCallData @@ -241,12 +240,6 @@ function previewSend(string memory to, string memory amount, string memory token ```solidity -function previewSwap( - string memory amountIn, - string memory amountOutMinimum, - string memory tokenIn, - string memory tokenOut -) function previewSwap( string memory amountIn, string memory amountOutMinimum, @@ -257,7 +250,6 @@ function previewSwap( view virtual returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut); - returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut); ``` ### checkUserOp @@ -344,12 +336,6 @@ function swap( string memory tokenIn, string memory tokenOut ) public payable virtual; -function swap( - string memory amountIn, - string memory amountOutMinimum, - string memory tokenIn, - string memory tokenOut -) public payable virtual; ``` ### fallback @@ -556,12 +542,6 @@ function _extractSwap(string memory normalizedIntent) string memory tokenIn, string memory tokenOut ); - returns ( - string memory amountIn, - string memory amountOutMinimum, - string memory tokenIn, - string memory tokenOut - ); ``` ### _split @@ -681,14 +661,6 @@ error InvalidCharacter(); *Insufficient swap output.* -```solidity -error InsufficientSwap(); -``` - -### InsufficientSwap -*Insufficient swap output.* - - ```solidity error InsufficientSwap(); ``` @@ -734,20 +706,6 @@ struct PackedUserOperation { } ``` -### SwapDetails -*The `swap` command details.* - - -```solidity -struct SwapDetails { - address tokenIn; - address tokenOut; - uint256 amountIn; - bool ETHIn; - bool ETHOut; -} -``` - ### SwapInfo *The `swap` command information struct.* diff --git a/docs/src/src/IE.sol/interface.IExecutor.md b/docs/src/src/IE.sol/interface.IExecutor.md index e544b52..8d076ca 100644 --- a/docs/src/src/IE.sol/interface.IExecutor.md +++ b/docs/src/src/IE.sol/interface.IExecutor.md @@ -1,5 +1,5 @@ # IExecutor -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) Simple calldata executor interface. diff --git a/docs/src/src/IE.sol/interface.INAMI.md b/docs/src/src/IE.sol/interface.INAMI.md index a2ef0b7..b8935d6 100644 --- a/docs/src/src/IE.sol/interface.INAMI.md +++ b/docs/src/src/IE.sol/interface.INAMI.md @@ -1,5 +1,5 @@ # INAMI -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) *Simple NAMI names interface for resolving L2 ENS ownership.* diff --git a/docs/src/src/IE.sol/interface.ISwapRouter.md b/docs/src/src/IE.sol/interface.ISwapRouter.md index 7276427..fd48cf8 100644 --- a/docs/src/src/IE.sol/interface.ISwapRouter.md +++ b/docs/src/src/IE.sol/interface.ISwapRouter.md @@ -1,5 +1,5 @@ # ISwapRouter -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) *Simple Uniswap V3 swapping interface.* diff --git a/docs/src/src/IE.sol/interface.IToken.md b/docs/src/src/IE.sol/interface.IToken.md index 4f4b72f..d1732e5 100644 --- a/docs/src/src/IE.sol/interface.IToken.md +++ b/docs/src/src/IE.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) *Simple token transfer interface.* diff --git a/docs/src/src/NAMI.sol/contract.NAMI.md b/docs/src/src/NAMI.sol/contract.NAMI.md index c91d21c..aa4975f 100644 --- a/docs/src/src/NAMI.sol/contract.NAMI.md +++ b/docs/src/src/NAMI.sol/contract.NAMI.md @@ -1,5 +1,5 @@ # NAMI -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/NAMI.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) diff --git a/docs/src/src/NAMI.sol/interface.IToken.md b/docs/src/src/NAMI.sol/interface.IToken.md index 1baf29f..76ff225 100644 --- a/docs/src/src/NAMI.sol/interface.IToken.md +++ b/docs/src/src/NAMI.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/f31f555ae821c0432ed1c7cd6e93b1e7bba98a37/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/NAMI.sol) *Simple token balance & supply interface.* diff --git a/src/IE.sol b/src/IE.sol index 530f66b..7b857de 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -37,9 +37,6 @@ contract IE { /// @dev Insufficient swap output. error InsufficientSwap(); - /// @dev Insufficient swap output. - error InsufficientSwap(); - /// =========================== EVENTS =========================== /// /// @dev Logs the registration of a token name. @@ -171,7 +168,6 @@ contract IE { address to, // Receiver address. uint256 amount, // Formatted amount. uint256 minAmountOut, // Formatted amount. - uint256 minAmountOut, // Formatted amount. address token, // Asset to send `to`. bytes memory callData, // Raw calldata for send transaction. bytes memory executeCallData // Anticipates common execute API. @@ -225,12 +221,6 @@ contract IE { } /// @dev Previews a `swap` command from the parts of a matched intent string. - function previewSwap( - string memory amountIn, - string memory amountOutMinimum, - string memory tokenIn, - string memory tokenOut - ) function previewSwap( string memory amountIn, string memory amountOutMinimum, @@ -241,7 +231,6 @@ contract IE { view virtual returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut) - returns (uint256 _amountIn, uint256 _amountOut, address _tokenIn, address _tokenOut) { uint256 decimalsIn; uint256 decimalsOut; @@ -261,7 +250,6 @@ contract IE { virtual returns (bool) { - (,,,,, bytes memory executeCallData) = previewCommand(intent); (,,,,, bytes memory executeCallData) = previewCommand(intent); if (executeCallData.length != userOp.callData.length) return false; return keccak256(executeCallData) == keccak256(userOp.callData); @@ -274,7 +262,6 @@ contract IE { virtual returns (bool) { - (,,,,, bytes memory executeCallData) = previewCommand(intent); (,,,,, bytes memory executeCallData) = previewCommand(intent); if (executeCallData.length != userOp.callData.length) return false; return keccak256(executeCallData) == keccak256(userOp.callData); @@ -688,18 +675,10 @@ contract IE { string memory tokenIn, string memory tokenOut ) - returns ( - string memory amountIn, - string memory amountOutMinimum, - string memory tokenIn, - string memory tokenOut - ) { string[] memory parts = _split(normalizedIntent, " "); if (parts.length == 5) return (parts[1], "", parts[2], parts[4]); if (parts.length == 6) return (parts[1], parts[4], parts[2], parts[5]); - if (parts.length == 5) return (parts[1], "", parts[2], parts[4]); - if (parts.length == 6) return (parts[1], parts[4], parts[2], parts[5]); else revert InvalidSyntax(); // Command is not formatted. } diff --git a/test/IE.t.sol b/test/IE.t.sol index 6481647..2b6a1e8 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -98,7 +98,6 @@ contract IETest is Test { function testPreviewCommandSendUSDC() public payable { string memory command = "send z0r0z 20 usdc"; (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); - (address to, uint256 amount,, address asset,,) = ie.previewCommand(command); assertEq(to, Z0R0Z_DOT_ETH); assertEq(amount, 20000000); assertEq(asset, USDC); @@ -185,7 +184,6 @@ contract IETest is Test { IERC20(USDC).approve(address(ie), 100 ether); vm.prank(USDC_WHALE); ie.command("swap 100 usdc for 0.025 weth"); - ie.command("swap 100 usdc for 0.025 weth"); } function testCommandSwapUSDCForWBTC() public payable { From 27284db656d05ae1ea23693c2b4eae568e779a9a Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Thu, 28 Mar 2024 08:57:42 +0000 Subject: [PATCH 22/22] =?UTF-8?q?=E2=9C=B5=20IE:=201.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/src/src/IE.sol/contract.IE.md | 2 +- docs/src/src/IE.sol/interface.IExecutor.md | 2 +- docs/src/src/IE.sol/interface.INAMI.md | 2 +- docs/src/src/IE.sol/interface.ISwapRouter.md | 2 +- docs/src/src/IE.sol/interface.IToken.md | 2 +- docs/src/src/NAMI.sol/contract.NAMI.md | 2 +- docs/src/src/NAMI.sol/interface.IToken.md | 2 +- ui/components/version.tsx | 2 +- ui/lib/abi/IntentsEngineAbiArb.ts | 778 ++++++++++--------- ui/lib/constants.ts | 2 +- 10 files changed, 434 insertions(+), 362 deletions(-) diff --git a/docs/src/src/IE.sol/contract.IE.md b/docs/src/src/IE.sol/contract.IE.md index dd5a7e3..ccb90ab 100644 --- a/docs/src/src/IE.sol/contract.IE.md +++ b/docs/src/src/IE.sol/contract.IE.md @@ -1,5 +1,5 @@ # IE -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/IE.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) diff --git a/docs/src/src/IE.sol/interface.IExecutor.md b/docs/src/src/IE.sol/interface.IExecutor.md index 8d076ca..5aaf28f 100644 --- a/docs/src/src/IE.sol/interface.IExecutor.md +++ b/docs/src/src/IE.sol/interface.IExecutor.md @@ -1,5 +1,5 @@ # IExecutor -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/IE.sol) Simple calldata executor interface. diff --git a/docs/src/src/IE.sol/interface.INAMI.md b/docs/src/src/IE.sol/interface.INAMI.md index b8935d6..3b13333 100644 --- a/docs/src/src/IE.sol/interface.INAMI.md +++ b/docs/src/src/IE.sol/interface.INAMI.md @@ -1,5 +1,5 @@ # INAMI -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/IE.sol) *Simple NAMI names interface for resolving L2 ENS ownership.* diff --git a/docs/src/src/IE.sol/interface.ISwapRouter.md b/docs/src/src/IE.sol/interface.ISwapRouter.md index fd48cf8..b4801cd 100644 --- a/docs/src/src/IE.sol/interface.ISwapRouter.md +++ b/docs/src/src/IE.sol/interface.ISwapRouter.md @@ -1,5 +1,5 @@ # ISwapRouter -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/IE.sol) *Simple Uniswap V3 swapping interface.* diff --git a/docs/src/src/IE.sol/interface.IToken.md b/docs/src/src/IE.sol/interface.IToken.md index d1732e5..c57ed0e 100644 --- a/docs/src/src/IE.sol/interface.IToken.md +++ b/docs/src/src/IE.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/IE.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/IE.sol) *Simple token transfer interface.* diff --git a/docs/src/src/NAMI.sol/contract.NAMI.md b/docs/src/src/NAMI.sol/contract.NAMI.md index aa4975f..bfdfd92 100644 --- a/docs/src/src/NAMI.sol/contract.NAMI.md +++ b/docs/src/src/NAMI.sol/contract.NAMI.md @@ -1,5 +1,5 @@ # NAMI -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/NAMI.sol) **Author:** nani.eth (https://github.com/NaniDAO/ie) diff --git a/docs/src/src/NAMI.sol/interface.IToken.md b/docs/src/src/NAMI.sol/interface.IToken.md index 76ff225..526fecb 100644 --- a/docs/src/src/NAMI.sol/interface.IToken.md +++ b/docs/src/src/NAMI.sol/interface.IToken.md @@ -1,5 +1,5 @@ # IToken -[Git Source](https://github.com/NaniDAO/ie/blob/6d1168cdd42d680ba733a4cce0562383696cbd44/src/NAMI.sol) +[Git Source](https://github.com/NaniDAO/ie/blob/f061f69f55a660146bbc3247dded252faef04a99/src/NAMI.sol) *Simple token balance & supply interface.* diff --git a/ui/components/version.tsx b/ui/components/version.tsx index 059410f..be58219 100644 --- a/ui/components/version.tsx +++ b/ui/components/version.tsx @@ -5,7 +5,7 @@ const versions: { [key: number]: string; } = { 1: "1.0.0", - 42161: "1.1.1 (nightly)", + 42161: "1.2.0 (nightly)", }; export const Version = () => { diff --git a/ui/lib/abi/IntentsEngineAbiArb.ts b/ui/lib/abi/IntentsEngineAbiArb.ts index 000b698..ad14ef0 100644 --- a/ui/lib/abi/IntentsEngineAbiArb.ts +++ b/ui/lib/abi/IntentsEngineAbiArb.ts @@ -1,549 +1,621 @@ export const IntentsEngineAbiArb = [ { - type: "constructor", - inputs: [], - stateMutability: "payable", + "type": "constructor", + "inputs": [], + "stateMutability": "payable" }, { - type: "fallback", - stateMutability: "payable", + "type": "fallback", + "stateMutability": "payable" }, { - type: "receive", - stateMutability: "payable", + "type": "receive", + "stateMutability": "payable" }, { - type: "function", - name: "checkPackedUserOp", - inputs: [ + "type": "function", + "name": "checkPackedUserOp", + "inputs": [ { - name: "intent", - type: "string", - internalType: "string", + "name": "intent", + "type": "string", + "internalType": "string" }, { - name: "userOp", - type: "tuple", - internalType: "struct IE.PackedUserOperation", - components: [ + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.PackedUserOperation", + "components": [ { - name: "sender", - type: "address", - internalType: "address", + "name": "sender", + "type": "address", + "internalType": "address" }, { - name: "nonce", - type: "uint256", - internalType: "uint256", + "name": "nonce", + "type": "uint256", + "internalType": "uint256" }, { - name: "initCode", - type: "bytes", - internalType: "bytes", + "name": "initCode", + "type": "bytes", + "internalType": "bytes" }, { - name: "callData", - type: "bytes", - internalType: "bytes", + "name": "callData", + "type": "bytes", + "internalType": "bytes" }, { - name: "accountGasLimits", - type: "bytes32", - internalType: "bytes32", + "name": "accountGasLimits", + "type": "bytes32", + "internalType": "bytes32" }, { - name: "preVerificationGas", - type: "uint256", - internalType: "uint256", + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" }, { - name: "gasFees", - type: "bytes32", - internalType: "bytes32", + "name": "gasFees", + "type": "bytes32", + "internalType": "bytes32" }, { - name: "paymasterAndData", - type: "bytes", - internalType: "bytes", + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" }, { - name: "signature", - type: "bytes", - internalType: "bytes", - }, - ], - }, + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } ], - outputs: [ + "outputs": [ { - name: "", - type: "bool", - internalType: "bool", - }, + "name": "", + "type": "bool", + "internalType": "bool" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "checkUserOp", - inputs: [ + "type": "function", + "name": "checkUserOp", + "inputs": [ { - name: "intent", - type: "string", - internalType: "string", + "name": "intent", + "type": "string", + "internalType": "string" }, { - name: "userOp", - type: "tuple", - internalType: "struct IE.UserOperation", - components: [ + "name": "userOp", + "type": "tuple", + "internalType": "struct IE.UserOperation", + "components": [ { - name: "sender", - type: "address", - internalType: "address", + "name": "sender", + "type": "address", + "internalType": "address" }, { - name: "nonce", - type: "uint256", - internalType: "uint256", + "name": "nonce", + "type": "uint256", + "internalType": "uint256" }, { - name: "initCode", - type: "bytes", - internalType: "bytes", + "name": "initCode", + "type": "bytes", + "internalType": "bytes" }, { - name: "callData", - type: "bytes", - internalType: "bytes", + "name": "callData", + "type": "bytes", + "internalType": "bytes" }, { - name: "callGasLimit", - type: "uint256", - internalType: "uint256", + "name": "callGasLimit", + "type": "uint256", + "internalType": "uint256" }, { - name: "verificationGasLimit", - type: "uint256", - internalType: "uint256", + "name": "verificationGasLimit", + "type": "uint256", + "internalType": "uint256" }, { - name: "preVerificationGas", - type: "uint256", - internalType: "uint256", + "name": "preVerificationGas", + "type": "uint256", + "internalType": "uint256" }, { - name: "maxFeePerGas", - type: "uint256", - internalType: "uint256", + "name": "maxFeePerGas", + "type": "uint256", + "internalType": "uint256" }, { - name: "maxPriorityFeePerGas", - type: "uint256", - internalType: "uint256", + "name": "maxPriorityFeePerGas", + "type": "uint256", + "internalType": "uint256" }, { - name: "paymasterAndData", - type: "bytes", - internalType: "bytes", + "name": "paymasterAndData", + "type": "bytes", + "internalType": "bytes" }, { - name: "signature", - type: "bytes", - internalType: "bytes", - }, - ], - }, + "name": "signature", + "type": "bytes", + "internalType": "bytes" + } + ] + } ], - outputs: [ + "outputs": [ { - name: "", - type: "bool", - internalType: "bool", - }, + "name": "", + "type": "bool", + "internalType": "bool" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "command", - inputs: [ - { - name: "intent", - type: "string", - internalType: "string", - }, + "type": "function", + "name": "command", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } ], - outputs: [], - stateMutability: "payable", + "outputs": [], + "stateMutability": "payable" }, { - type: "function", - name: "previewCommand", - inputs: [ + "type": "function", + "name": "pairs", + "inputs": [ { - name: "intent", - type: "string", - internalType: "string", + "name": "token0", + "type": "address", + "internalType": "address" }, + { + "name": "token1", + "type": "address", + "internalType": "address" + } ], - outputs: [ + "outputs": [ { - name: "to", - type: "address", - internalType: "address", - }, + "name": "", + "type": "address", + "internalType": "address" + } + ], + "stateMutability": "view" + }, + { + "type": "function", + "name": "previewCommand", + "inputs": [ + { + "name": "intent", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ { - name: "amount", - type: "uint256", - internalType: "uint256", + "name": "to", + "type": "address", + "internalType": "address" }, { - name: "minAmountOut", - type: "uint256", - internalType: "uint256", + "name": "amount", + "type": "uint256", + "internalType": "uint256" }, { - name: "token", - type: "address", - internalType: "address", + "name": "minAmountOut", + "type": "uint256", + "internalType": "uint256" }, { - name: "callData", - type: "bytes", - internalType: "bytes", + "name": "token", + "type": "address", + "internalType": "address" }, { - name: "executeCallData", - type: "bytes", - internalType: "bytes", + "name": "callData", + "type": "bytes", + "internalType": "bytes" }, + { + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "previewSend", - inputs: [ + "type": "function", + "name": "previewSend", + "inputs": [ { - name: "to", - type: "string", - internalType: "string", + "name": "to", + "type": "string", + "internalType": "string" }, { - name: "amount", - type: "string", - internalType: "string", + "name": "amount", + "type": "string", + "internalType": "string" }, { - name: "token", - type: "string", - internalType: "string", - }, + "name": "token", + "type": "string", + "internalType": "string" + } ], - outputs: [ + "outputs": [ { - name: "_to", - type: "address", - internalType: "address", + "name": "_to", + "type": "address", + "internalType": "address" }, { - name: "_amount", - type: "uint256", - internalType: "uint256", + "name": "_amount", + "type": "uint256", + "internalType": "uint256" }, { - name: "_token", - type: "address", - internalType: "address", + "name": "_token", + "type": "address", + "internalType": "address" }, { - name: "callData", - type: "bytes", - internalType: "bytes", + "name": "callData", + "type": "bytes", + "internalType": "bytes" }, { - name: "executeCallData", - type: "bytes", - internalType: "bytes", - }, + "name": "executeCallData", + "type": "bytes", + "internalType": "bytes" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "previewSwap", - inputs: [ + "type": "function", + "name": "previewSwap", + "inputs": [ { - name: "amountIn", - type: "string", - internalType: "string", + "name": "amountIn", + "type": "string", + "internalType": "string" }, { - name: "amountOutMinimum", - type: "string", - internalType: "string", + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" }, { - name: "tokenIn", - type: "string", - internalType: "string", + "name": "tokenIn", + "type": "string", + "internalType": "string" }, { - name: "tokenOut", - type: "string", - internalType: "string", - }, + "name": "tokenOut", + "type": "string", + "internalType": "string" + } ], - outputs: [ + "outputs": [ { - name: "_amountIn", - type: "uint256", - internalType: "uint256", + "name": "_amountIn", + "type": "uint256", + "internalType": "uint256" }, { - name: "_amountOut", - type: "uint256", - internalType: "uint256", + "name": "_amountOut", + "type": "uint256", + "internalType": "uint256" }, { - name: "_tokenIn", - type: "address", - internalType: "address", + "name": "_tokenIn", + "type": "address", + "internalType": "address" }, { - name: "_tokenOut", - type: "address", - internalType: "address", - }, + "name": "_tokenOut", + "type": "address", + "internalType": "address" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "send", - inputs: [ + "type": "function", + "name": "send", + "inputs": [ { - name: "to", - type: "string", - internalType: "string", + "name": "to", + "type": "string", + "internalType": "string" }, { - name: "amount", - type: "string", - internalType: "string", + "name": "amount", + "type": "string", + "internalType": "string" }, { - name: "token", - type: "string", - internalType: "string", - }, + "name": "token", + "type": "string", + "internalType": "string" + } ], - outputs: [], - stateMutability: "payable", + "outputs": [], + "stateMutability": "payable" }, { - type: "function", - name: "setName", - inputs: [ + "type": "function", + "name": "setName", + "inputs": [ { - name: "token", - type: "address", - internalType: "address", + "name": "token", + "type": "address", + "internalType": "address" }, { - name: "name", - type: "string", - internalType: "string", - }, + "name": "name", + "type": "string", + "internalType": "string" + } ], - outputs: [], - stateMutability: "payable", + "outputs": [], + "stateMutability": "payable" }, { - type: "function", - name: "setNameAndTicker", - inputs: [ + "type": "function", + "name": "setNameAndTicker", + "inputs": [ + { + "name": "token", + "type": "address", + "internalType": "address" + } + ], + "outputs": [], + "stateMutability": "payable" + }, + { + "type": "function", + "name": "setPair", + "inputs": [ + { + "name": "tokenA", + "type": "address", + "internalType": "address" + }, { - name: "token", - type: "address", - internalType: "address", + "name": "tokenB", + "type": "address", + "internalType": "address" }, + { + "name": "pair", + "type": "address", + "internalType": "address" + } ], - outputs: [], - stateMutability: "payable", + "outputs": [], + "stateMutability": "payable" }, { - type: "function", - name: "swap", - inputs: [ + "type": "function", + "name": "swap", + "inputs": [ { - name: "amountIn", - type: "string", - internalType: "string", + "name": "amountIn", + "type": "string", + "internalType": "string" }, { - name: "amountOutMinimum", - type: "string", - internalType: "string", + "name": "amountOutMinimum", + "type": "string", + "internalType": "string" }, { - name: "tokenIn", - type: "string", - internalType: "string", + "name": "tokenIn", + "type": "string", + "internalType": "string" }, { - name: "tokenOut", - type: "string", - internalType: "string", - }, + "name": "tokenOut", + "type": "string", + "internalType": "string" + } ], - outputs: [], - stateMutability: "payable", + "outputs": [], + "stateMutability": "payable" }, { - type: "function", - name: "tokens", - inputs: [ - { - name: "name", - type: "string", - internalType: "string", - }, + "type": "function", + "name": "tokens", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } ], - outputs: [ + "outputs": [ { - name: "", - type: "address", - internalType: "address", - }, + "name": "", + "type": "address", + "internalType": "address" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "whatIsTheAddressOf", - inputs: [ - { - name: "name", - type: "string", - internalType: "string", - }, + "type": "function", + "name": "whatIsTheAddressOf", + "inputs": [ + { + "name": "name", + "type": "string", + "internalType": "string" + } ], - outputs: [ + "outputs": [ { - name: "owner", - type: "address", - internalType: "address", + "name": "owner", + "type": "address", + "internalType": "address" }, { - name: "receiver", - type: "address", - internalType: "address", + "name": "receiver", + "type": "address", + "internalType": "address" }, { - name: "node", - type: "bytes32", - internalType: "bytes32", - }, + "name": "node", + "type": "bytes32", + "internalType": "bytes32" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "whatIsTheBalanceOf", - inputs: [ + "type": "function", + "name": "whatIsTheBalanceOf", + "inputs": [ { - name: "name", - type: "string", - internalType: "string", + "name": "name", + "type": "string", + "internalType": "string" }, { - name: "token", - type: "string", - internalType: "string", - }, + "name": "token", + "type": "string", + "internalType": "string" + } ], - outputs: [ + "outputs": [ { - name: "balance", - type: "uint256", - internalType: "uint256", + "name": "balance", + "type": "uint256", + "internalType": "uint256" }, { - name: "balanceAdjusted", - type: "uint256", - internalType: "uint256", - }, + "name": "balanceAdjusted", + "type": "uint256", + "internalType": "uint256" + } ], - stateMutability: "view", + "stateMutability": "view" }, { - type: "function", - name: "whatIsTheTotalSupplyOf", - inputs: [ + "type": "function", + "name": "whatIsTheTotalSupplyOf", + "inputs": [ + { + "name": "token", + "type": "string", + "internalType": "string" + } + ], + "outputs": [ { - name: "token", - type: "string", - internalType: "string", + "name": "supply", + "type": "uint256", + "internalType": "uint256" }, + { + "name": "supplyAdjusted", + "type": "uint256", + "internalType": "uint256" + } ], - outputs: [ + "stateMutability": "view" + }, + { + "type": "event", + "name": "NameSet", + "inputs": [ { - name: "supply", - type: "uint256", - internalType: "uint256", + "name": "token", + "type": "address", + "indexed": true, + "internalType": "address" }, { - name: "supplyAdjusted", - type: "uint256", - internalType: "uint256", - }, + "name": "name", + "type": "string", + "indexed": false, + "internalType": "string" + } ], - stateMutability: "view", + "anonymous": false }, { - type: "event", - name: "NameSet", - inputs: [ + "type": "event", + "name": "PairSet", + "inputs": [ { - name: "token", - type: "address", - indexed: true, - internalType: "address", + "name": "token0", + "type": "address", + "indexed": true, + "internalType": "address" }, { - name: "name", - type: "string", - indexed: false, - internalType: "string", + "name": "token1", + "type": "address", + "indexed": true, + "internalType": "address" }, + { + "name": "pair", + "type": "address", + "indexed": false, + "internalType": "address" + } ], - anonymous: false, + "anonymous": false }, { - type: "error", - name: "InsufficientSwap", - inputs: [], + "type": "error", + "name": "InsufficientSwap", + "inputs": [] }, { - type: "error", - name: "InvalidCharacter", - inputs: [], + "type": "error", + "name": "InvalidCharacter", + "inputs": [] }, { - type: "error", - name: "InvalidSwap", - inputs: [], + "type": "error", + "name": "InvalidSwap", + "inputs": [] }, { - type: "error", - name: "InvalidSyntax", - inputs: [], + "type": "error", + "name": "InvalidSyntax", + "inputs": [] }, { - type: "error", - name: "Overflow", - inputs: [], - }, + "type": "error", + "name": "Overflow", + "inputs": [] + } ] as const; diff --git a/ui/lib/constants.ts b/ui/lib/constants.ts index 690a9e5..7231fc4 100644 --- a/ui/lib/constants.ts +++ b/ui/lib/constants.ts @@ -4,7 +4,7 @@ export const IE_ADDRESS: { [key: number]: Address; } = { 1: "0x1E00000000Cf8ba83e0005c59c1Bf1C4682C8E00", - 42161: "0x000000001e202b5b84e56400f409800065cb6d3c", + 42161: "0x1e00003a669bb466d6B49800000099E1abDD6600", }; export const ETH_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";