// ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘ ⌘
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;
import {SafeTransferLib} from "../lib/solady/src/utils/SafeTransferLib.sol";
import {MetadataReaderLib} from "../lib/solady/src/utils/MetadataReaderLib.sol";
/// @title Intents Engine (IE) on Base (IEBase)
/// @notice Simple helper contract for turning transactional intents into executable code.
/// @dev V2 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 2.3.0
contract IEBase {
/// ======================= LIBRARY USAGE ======================= ///
/// @dev Token transfer library.
using SafeTransferLib for address;
/// @dev Token metadata reader library.
using MetadataReaderLib for address;
/// ======================= CUSTOM ERRORS ======================= ///
/// @dev Bad math.
error Overflow();
/// @dev 0-liquidity.
error InvalidSwap();
/// @dev Invalid command.
error InvalidSyntax();
/// @dev Invalid out receiver.
error InvalidReceiver();
/// @dev Non-numeric character.
error InvalidCharacter();
/// @dev Invalid function caller.
error Unauthorized();
/// @dev Order expiry has arrived.
error OrderExpired();
/// @dev Insufficient swap output.
error InsufficientSwap();
/// @dev Invalid selector for spend.
error InvalidSelector();
/// @dev Unauthorized reentrant call.
error Reentrancy();
/// =========================== EVENTS =========================== ///
/// @dev Logs the setting of a token name.
event NameSet(address token, string name);
/// @dev Logs the setting of a swap pool pair on Uniswap V3.
event PairSet(address token0, address token1, address pair);
/// ========================== STRUCTS ========================== ///
/// @dev The packed ERC4337 user operation (userOp) struct.
struct PackedUserOperation {
address sender;
uint256 nonce;
bytes initCode;
bytes callData;
bytes32 accountGasLimits;
uint256 preVerificationGas;
bytes32 gasFees;
bytes paymasterAndData;
bytes signature;
}
/// @dev The `swap()` command information struct.
struct SwapInfo {
bool ETHIn;
bool ETHOut;
address tokenIn;
address tokenOut;
uint256 amountIn;
}
/// @dev The `swap()` pool liquidity struct.
struct SwapLiq {
address pool;
uint256 liq;
}
/// @dev The string start and end indices.
struct StringPart {
uint256 start;
uint256 end;
}
/// @dev The onchain order struct.
struct Order {
address tokenIn;
address tokenOut;
uint256 amountIn;
uint256 amountOut;
address maker;
address receiver;
uint48 nonce;
uint48 expiry;
}
/// =========================== ENUMS =========================== ///
/// @dev `ENSAsciiNormalizer` rules.
enum Rule {
DISALLOWED,
VALID
}
/// ========================= CONSTANTS ========================= ///
/// @dev The governing DAO address.
address internal constant DAO = 0xDa000000000000d2885F108500803dfBAaB2f2aA;
/// @dev The conventional ERC7528 ETH address.
address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @dev The canonical wrapped ETH address.
address internal constant WETH = 0x4200000000000000000000000000000000000006;
/// @dev The Coinbase wrapped BTC address.
address internal constant CBBTC = 0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf;
/// @dev The threshold wrapped BTC address.
address internal constant TBTC = 0x236aa50979D5f3De3Bd1Eeb40E81137F22ab794b;
/// @dev The Circle USD stablecoin address.
address internal constant USDC = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913;
/// @dev The Tether USD stablecoin address.
address internal constant USDT = 0xfde4C96c8593536E31F229EA8f37b2ADa2699bb2;
/// @dev The Maker DAO USD stablecoin address.
address internal constant DAI = 0x50c5725949A6F0c72E6C4a641F24049A917DB0Cb;
/// @dev The Coinbase wrapped staked ETH token address.
address internal constant CBETH = 0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22;
/// @dev The Lido wrapped staked ETH token address.
address internal constant WSTETH = 0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452;
/// @dev The resolution registry smart account.
address internal constant CURIA = 0x0000000000001d8a2e7bf6bc369525A2654aa298;
/// @dev The Escrows protocol singleton.
address internal constant ESCROWS = 0x00000000000044992CB97CB1A57A32e271C04c11;
/// @dev Equivalent to: `uint72(bytes9(keccak256("_REENTRANCY_GUARD_SLOT")))`.
uint256 internal constant _REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268;
/// @dev The address of the Uniswap V3 Factory.
address internal constant UNISWAP_V3_FACTORY = 0x33128a8fC17869897dcE68Ed026d694621f6FDfD;
/// @dev The Uniswap V3 Pool `initcodehash`.
bytes32 internal constant UNISWAP_V3_POOL_INIT_CODE_HASH =
0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
/// @dev The minimum value that can be returned from `getSqrtRatioAtTick` (plus one).
uint160 internal constant MIN_SQRT_RATIO_PLUS_ONE = 4295128740;
/// @dev The maximum value that can be returned from `getSqrtRatioAtTick` (minus one).
uint160 internal constant MAX_SQRT_RATIO_MINUS_ONE =
1461446703485210103287273052203988822378723970341;
/// @dev The L2Resolver for `base.eth` domains.
address internal constant BASE_ENS = 0xC6d566A56A1aFf6508b41f6c90ff131615583BCD;
/// @dev The ReverseRegistrar for `base.eth` domains.
address internal constant BASE_ENS_REVERSE = 0x79EA96012eEa67A83431F1701B3dFf7e37F9E282;
/// @dev String mapping for `ENSAsciiNormalizer` logic.
bytes internal constant ASCII_MAP =
hex"2d00020101000a010700016101620163016401650166016701680169016a016b016c016d016e016f0170017101720173017401750176017701780179017a06001a010500";
/// ========================== STORAGE ========================== ///
/// @dev DAO-governed token names to addresses.
mapping(string name => address) public addresses;
/// @dev DAO-governed token addresses to names.
mapping(address addresses => string) public names;
/// @dev Open order book for p2p asset exchange.
mapping(bytes32 orderHash => Order) public orders;
/// @dev DAO-governed token swap pool routing on Uniswap V3.
mapping(address token0 => mapping(address token1 => address)) public pairs;
/// @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 Array of onchain order struct hashes.
bytes32[] public orderHashes;
/// ======================== CONSTRUCTOR ======================== ///
/// @dev Constructs this IE on the Base L2 of Ethereum with ENS `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);
}
}
}
}
/// ====================== COMMAND PREVIEW ====================== ///
/// @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'.
/// `lock` syntax uses send format: 'lock 1 WETH for vitalik'.
function previewCommand(string calldata intent)
public
view
virtual
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.
)
{
bytes memory normalized = _lowercase(bytes(intent));
bytes32 action = _extraction(normalized);
if (action == "send" || action == "transfer" || action == "pay" || action == "grant") {
(bytes memory _to, bytes memory _amount, bytes memory _token) = _extractSend(normalized);
(to, amount, token, callData, executeCallData) = _previewSend(_to, _amount, _token);
} else if (
action == "swap" || action == "sell" || action == "exchange" || action == "stake"
) {
(
bytes memory amountIn,
bytes memory amountOutMin,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
) = _extractSwap(normalized);
address _receiver;
(amount, minAmountOut, token, to, _receiver) =
_previewSwap(amountIn, amountOutMin, tokenIn, tokenOut, receiver);
callData = abi.encodePacked(_receiver);
} else if (action == "lock" || action == "lockup" || action == "escrow") {
(
bytes memory _to,
bytes memory _amount,
bytes memory _token,
bytes memory _time,
bytes memory _unit
) = _extractLock(normalized);
(to, amount, token, minAmountOut /*expiry*/ ) =
_previewLock(_to, _amount, _token, _time, _unit);
} else if (action == "order") {
(
bytes memory amountIn,
bytes memory amountOut,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
) = _extractSwap(normalized);
address _receiver;
(amount, minAmountOut, token, to, _receiver) =
_previewSwap(amountIn, amountOut, tokenIn, tokenOut, receiver);
callData = abi.encodePacked(_receiver);
} else {
revert InvalidSyntax(); // Invalid command format.
}
}
/// @dev Previews a `send` command from the parts of a matched intent string.
function _previewSend(bytes memory to, bytes memory amount, bytes memory token)
internal
view
virtual
returns (
address _to,
uint256 _amount,
address _token,
bytes memory callData,
bytes memory executeCallData
)
{
uint256 decimals;
if (token.length == 42) _token = _toAddress(token);
else (_token, decimals) = _returnTokenConstants(bytes32(token));
if (_token == address(0)) _token = addresses[string(token)];
bool isETH = _token == ETH;
(, _to,) = whatIsTheAddressOf(string(to));
_amount = _toUint(amount, decimals != 0 ? decimals : _token.readDecimals(), _token);
if (!isETH) callData = abi.encodeCall(IToken.transfer, (_to, _amount));
executeCallData =
abi.encodeCall(IExecutor.execute, (isETH ? _to : _token, isETH ? _amount : 0, callData));
}
/// @dev Previews a `lock` command from the parts of a matched intent string.
function _previewLock(
bytes memory to,
bytes memory amount,
bytes memory token,
bytes memory time,
bytes memory unit
)
internal
view
virtual
returns (address _to, uint256 _amount, address _token, uint256 _expiry)
{
uint256 decimals;
if (token.length == 42) _token = _toAddress(token);
else (_token, decimals) = _returnTokenConstants(bytes32(token));
if (_token == address(0)) _token = addresses[string(token)];
(, _to,) = whatIsTheAddressOf(string(to));
_amount = _toUint(amount, decimals != 0 ? decimals : _token.readDecimals(), _token);
uint256 _time = _simpleToUint256(time);
bytes32 _unit = bytes32(unit);
unchecked {
if (_unit == "minute" || _unit == "minutes") {
_time = _time * 1 minutes;
} else if (_unit == "day" || _unit == "days") {
_time = _time * 1 days;
} else if (_unit == "week" || _unit == "weeks") {
_time = _time * 1 weeks;
} else if (_unit == "month" || _unit == "months") {
_time = _time * 4 weeks;
} else if (_unit == "year" || _unit == "years") {
_time = _time * 52 weeks;
} else {
revert InvalidSyntax(); // Invalid `unit`.
}
_expiry = block.timestamp + _time;
}
}
/// @dev Previews a `swap` command from the parts of a matched intent string.
function _previewSwap(
bytes memory amountIn,
bytes memory amountOutMin,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
)
internal
view
virtual
returns (
uint256 _amountIn,
uint256 _amountOut,
address _tokenIn,
address _tokenOut,
address _receiver
)
{
uint256 decimalsIn;
uint256 decimalsOut;
if (tokenIn.length == 42) _tokenIn = _toAddress(tokenIn);
else (_tokenIn, decimalsIn) = _returnTokenConstants(bytes32(tokenIn));
if (_tokenIn == address(0)) _tokenIn = addresses[string(tokenIn)];
if (tokenOut.length == 42) _tokenOut = _toAddress(tokenOut);
else (_tokenOut, decimalsOut) = _returnTokenConstants(bytes32(tokenOut));
if (_tokenOut == address(0)) _tokenOut = addresses[string(tokenOut)];
_amountIn =
_toUint(amountIn, decimalsIn != 0 ? decimalsIn : _tokenIn.readDecimals(), _tokenIn);
_amountOut = _toUint(
amountOutMin, decimalsOut != 0 ? decimalsOut : _tokenOut.readDecimals(), _tokenOut
);
if (receiver.length != 0) (, _receiver,) = whatIsTheAddressOf(string(receiver));
}
/// @dev Checks packed ERC4337 userOp against the output of the command intent.
/// note: This function checks ETH and ERC20 transfers only with `execute()`.
function checkUserOp(string calldata intent, PackedUserOperation calldata userOp)
public
view
virtual
returns (bool intentMatched)
{
(,,,,, bytes memory executeCallData) = previewCommand(intent);
if (executeCallData.length != userOp.callData.length) return false;
return keccak256(executeCallData) == keccak256(userOp.callData);
}
/// @dev Checks and returns the canonical token address constant for a matched intent string.
function _returnTokenConstants(bytes32 token)
internal
pure
virtual
returns (address _token, uint256 _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 == "weth") return (WETH, 18);
if (token == "cbbtc" || token == "btc" || token == "bitcoin") return (CBBTC, 8);
if (token == "tbtc") return (TBTC, 18);
if (token == "cbeth" || token == "coinbase") return (CBETH, 18);
if (token == "steth" || token == "wsteth" || token == "lido") return (WSTETH, 18);
}
/// @dev Checks and returns the canonical token string constant for a matched address.
function _returnTokenAliasConstants(address token)
internal
pure
virtual
returns (string memory _token, uint256 _decimals)
{
if (token == USDC) return ("USDC", 6);
if (token == USDT) return ("USDT", 6);
if (token == DAI) return ("DAI", 18);
if (token == WETH) return ("WETH", 18);
if (token == CBBTC) return ("CBBTC", 8);
if (token == TBTC) return ("TBTC", 18);
if (token == CBETH) return ("CBETH", 18);
if (token == WSTETH) return ("WSTETH", 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 == CBETH && token1 == WETH) return 0xA9DaFa443a02FBc907Cb0093276B3E6F4ef02A46;
if (token0 == WETH && token1 == WSTETH) return 0x20E068D76f9E90b90604500B84c7e19dCB923e7e;
if (token0 == WETH && token1 == USDC) return 0xb4CB800910B228ED3d0834cF79D697127BBB00e5;
if (token0 == WETH && token1 == USDT) return 0xd92E0767473D1E3FF11Ac036f2b1DB90aD0aE55F;
if (token0 == WETH && token1 == DAI) return 0x93e8542E6CA0eFFfb9D57a270b76712b968A38f5;
if (token0 == WETH && token1 == CBBTC) return 0x7AeA2E8A3843516afa07293a10Ac8E49906dabD1;
if (token0 == TBTC && token1 == WETH) return 0x9fee7385a2979D15277C3467Db7D99EF1A2669D7;
}
/// ===================== COMMAND EXECUTION ===================== ///
/// @dev Executes a text command from an `intent` string.
function command(string calldata intent) public payable virtual {
bytes memory normalized = _lowercase(bytes(intent));
bytes32 action = _extraction(normalized);
if (action == "send" || action == "transfer" || action == "pay" || action == "grant") {
(bytes memory to, bytes memory amount, bytes memory token) = _extractSend(normalized);
send(string(to), string(amount), string(token));
} else if (
action == "swap" || action == "sell" || action == "exchange" || action == "stake"
) {
(
bytes memory amountIn,
bytes memory amountOutMin,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
) = _extractSwap(normalized);
swap(
string(amountIn),
string(amountOutMin),
string(tokenIn),
string(tokenOut),
string(receiver)
);
} else if (action == "lock" || action == "lockup") {
(
bytes memory to,
bytes memory amount,
bytes memory token,
bytes memory time,
bytes memory unit
) = _extractLock(normalized);
bytes32 id = lock(string(to), string(amount), string(token), string(time), string(unit));
assembly ("memory-safe") {
mstore(0x00, id)
return(0x00, 0x20)
}
} else if (action == "escrow") {
(
bytes memory to,
bytes memory amount,
bytes memory token,
bytes memory time,
bytes memory unit
) = _extractLock(normalized);
bytes32 id =
escrow(string(to), string(amount), string(token), string(time), string(unit));
assembly ("memory-safe") {
mstore(0x00, id)
return(0x00, 0x20)
}
} else if (action == "order") {
(
bytes memory amountIn,
bytes memory amountOut,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
) = _extractSwap(normalized);
bytes32 id = order(
string(tokenIn),
string(tokenOut),
string(amountIn),
string(amountOut),
string(receiver)
);
assembly ("memory-safe") {
mstore(0x00, id)
return(0x00, 0x20)
}
} else {
revert InvalidSyntax(); // Invalid command format.
}
}
/// @dev Executes batch of text commands from an `intents` string.
function command(string[] calldata intents) public payable virtual {
for (uint256 i; i != intents.length; ++i) {
command(intents[i]);
}
}
/// @dev Executes a `send` command from the parts of a matched intent string.
function send(string memory to, string memory amount, string memory token)
public
payable
virtual
{
address _token;
uint256 decimals;
if (bytes(token).length == 42) _token = _toAddress(bytes(token));
else (_token, decimals) = _returnTokenConstants(bytes32(bytes(token)));
if (_token == address(0)) _token = addresses[token];
(, address _to,) = whatIsTheAddressOf(to);
uint256 _amount =
_toUint(bytes(amount), decimals != 0 ? decimals : _token.readDecimals(), _token);
if (_token == ETH) {
require(msg.value == _amount);
_to.safeTransferETH(_amount);
} else {
_token.safeTransferFrom(msg.sender, _to, _amount);
}
}
/// @dev Executes a `lock` command from the parts of a matched intent string.
function lock(
string memory to,
string memory amount,
string memory token,
string memory time, /*'40'*/
string memory unit /*'days'*/
) public payable virtual returns (bytes32) {
return _escrow(bytes(to), bytes(amount), bytes(token), bytes(time), bytes(unit), true);
}
/// @dev Executes an `escrow` command from the parts of a matched intent string.
function escrow(
string memory to,
string memory amount,
string memory token,
string memory time, /*'40'*/
string memory unit /*'days'*/
) public payable virtual returns (bytes32) {
return _escrow(bytes(to), bytes(amount), bytes(token), bytes(time), bytes(unit), false);
}
/// @dev Handles either a `lock` or `escrow` command via Escrows protocol.
function _escrow(
bytes memory to,
bytes memory amount,
bytes memory token,
bytes memory time, /*'40'*/
bytes memory unit, /*'days'*/
bool lockup
) internal virtual returns (bytes32) {
address _token;
uint256 decimals;
if (bytes(token).length == 42) _token = _toAddress(bytes(token));
else (_token, decimals) = _returnTokenConstants(bytes32(token));
if (_token == address(0)) _token = addresses[string(token)];
(, address _to,) = whatIsTheAddressOf(string(to));
uint256 _amount = _toUint(amount, decimals != 0 ? decimals : _token.readDecimals(), _token);
uint256 _time = _simpleToUint256(time);
bytes32 _unit = bytes32(unit);
if (_unit == "minute" || _unit == "minutes") {
_time = _time * 1 minutes;
} else if (_unit == "day" || _unit == "days") {
_time = _time * 1 days;
} else if (_unit == "week" || _unit == "weeks") {
_time = _time * 1 weeks;
} else if (_unit == "month" || _unit == "months") {
_time = _time * 4 weeks;
} else if (_unit == "year" || _unit == "years") {
_time = _time * 52 weeks;
} else {
revert InvalidSyntax(); // Invalid `unit`.
}
if (_token == ETH) {
unchecked {
require(msg.value == _amount);
return IEscrows(ESCROWS).escrow{value: _amount}(
address(0),
lockup ? _to : msg.sender,
lockup ? msg.sender : _to,
CURIA,
_amount,
string(
abi.encodePacked(
"lock",
" ",
amount,
" ",
token,
" ",
"for",
" ",
to,
" ",
"for",
" ",
time,
" ",
unit
)
),
block.timestamp + _time
);
}
} else {
_token.safeTransferFrom(msg.sender, address(this), _amount);
_token.safeApprove(ESCROWS, _amount);
unchecked {
return IEscrows(ESCROWS).escrow(
_token,
lockup ? _to : msg.sender,
lockup ? msg.sender : _to,
CURIA,
_amount,
string(
abi.encodePacked(
"lock",
" ",
amount,
" ",
token,
" ",
"for",
" ",
to,
" ",
"for",
" ",
time,
" ",
unit
)
),
block.timestamp + _time
);
}
}
}
/// @dev Executes a `swap` command from the parts of a matched intent string.
function swap(
string memory amountIn,
string memory amountOutMin,
string memory tokenIn,
string memory tokenOut,
string memory receiver
) public payable virtual {
SwapInfo memory info;
uint256 decimalsIn;
uint256 decimalsOut;
if (bytes(tokenIn).length == 42) info.tokenIn = _toAddress(bytes(tokenIn));
else (info.tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn)));
if (info.tokenIn == address(0)) info.tokenIn = addresses[tokenIn];
if (bytes(tokenOut).length == 42) info.tokenOut = _toAddress(bytes(tokenOut));
else (info.tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut)));
if (info.tokenOut == address(0)) info.tokenOut = addresses[tokenOut];
uint256 minOut;
if (bytes(amountOutMin).length != 0) {
minOut = _toUint(
bytes(amountOutMin),
decimalsOut != 0 ? decimalsOut : info.tokenOut.readDecimals(),
info.tokenOut
);
}
bool exactOut = bytes(amountIn).length == 0;
info.amountIn = exactOut
? minOut
: _toUint(
bytes(amountIn),
decimalsIn != 0 ? decimalsIn : info.tokenIn.readDecimals(),
info.tokenIn
);
if (info.amountIn >= 1 << 255) revert Overflow();
info.ETHIn = info.tokenIn == ETH;
if (info.ETHIn) require(msg.value == info.amountIn);
if (info.ETHIn) info.tokenIn = WETH;
info.ETHOut = info.tokenOut == ETH;
if (info.ETHOut) info.tokenOut = WETH;
address _receiver;
if (bytes(receiver).length == 0) _receiver = msg.sender;
else (, _receiver,) = whatIsTheAddressOf(receiver);
(address pool, bool zeroForOne) = _computePoolAddress(info.tokenIn, info.tokenOut);
(int256 amount0, int256 amount1) = ISwapRouter(pool).swap(
!info.ETHOut ? _receiver : address(this),
zeroForOne,
!exactOut ? int256(info.amountIn) : -int256(info.amountIn),
zeroForOne ? MIN_SQRT_RATIO_PLUS_ONE : MAX_SQRT_RATIO_MINUS_ONE,
abi.encodePacked(
info.ETHIn, info.ETHOut, msg.sender, info.tokenIn, info.tokenOut, _receiver
)
);
if (minOut != 0) {
if (uint256(-(zeroForOne ? amount1 : amount0)) < minOut) revert InsufficientSwap();
}
}
/// @dev Fallback `uniswapV3SwapCallback`.
/// If ETH is swapped, WETH is forwarded.
fallback() external payable virtual {
int256 amount0Delta;
int256 amount1Delta;
bool ETHIn;
bool ETHOut;
address payer;
address tokenIn;
address tokenOut;
address receiver;
assembly ("memory-safe") {
amount0Delta := calldataload(0x4)
amount1Delta := calldataload(0x24)
ETHIn := byte(0, calldataload(0x84))
ETHOut := byte(0, calldataload(add(0x84, 1)))
payer := shr(96, calldataload(add(0x84, 2)))
tokenIn := shr(96, calldataload(add(0x84, 22)))
tokenOut := shr(96, calldataload(add(0x84, 42)))
receiver := shr(96, calldataload(add(0x84, 62)))
}
if (amount0Delta <= 0 && amount1Delta <= 0) revert InvalidSwap();
(address pool, bool zeroForOne) = _computePoolAddress(tokenIn, tokenOut);
assembly ("memory-safe") {
if iszero(eq(caller(), pool)) { revert(codesize(), codesize()) }
}
if (ETHIn) {
_wrapETH(uint256(zeroForOne ? amount0Delta : amount1Delta));
} else {
tokenIn.safeTransferFrom(payer, pool, uint256(zeroForOne ? amount0Delta : amount1Delta));
}
if (ETHOut) {
uint256 amount = uint256(-(zeroForOne ? amount1Delta : amount0Delta));
_unwrapETH(amount);
receiver.safeTransferETH(amount);
}
}
/// @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
virtual
returns (address pool, bool zeroForOne)
{
if (tokenA < tokenB) zeroForOne = true;
else (tokenA, tokenB) = (tokenB, tokenA);
pool = _returnPoolConstants(tokenA, tokenB);
if (pool == address(0)) {
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.
SwapLiq memory topPool;
uint256 liq;
if (pool100.code.length != 0) {
liq = _balanceOf(tokenA, pool100);
topPool = SwapLiq(pool100, liq);
}
if (pool500.code.length != 0) {
liq = _balanceOf(tokenA, pool500);
if (liq > topPool.liq) {
topPool = SwapLiq(pool500, liq);
}
}
if (pool3000.code.length != 0) {
liq = _balanceOf(tokenA, pool3000);
if (liq > topPool.liq) {
topPool = SwapLiq(pool3000, liq);
}
}
if (pool10000.code.length != 0) {
liq = _balanceOf(tokenA, pool10000);
if (liq > topPool.liq) {
topPool = SwapLiq(pool10000, liq);
}
}
pool = topPool.pool; // Return top pool.
}
}
}
/// @dev Computes the create2 deployment hash for a given token pair.
function _computePairHash(address token0, address token1, uint24 fee)
internal
pure
virtual
returns (address pool)
{
bytes32 salt = _hash(token0, token1, fee);
assembly ("memory-safe") {
mstore8(0x00, 0xff) // Write the prefix.
mstore(0x35, UNISWAP_V3_POOL_INIT_CODE_HASH)
mstore(0x01, shl(96, UNISWAP_V3_FACTORY))
mstore(0x15, salt)
pool := keccak256(0x00, 0x55)
mstore(0x35, 0) // Restore overwritten.
}
}
/// @dev Returns `keccak256(abi.encode(value0, value1, value2))`.
function _hash(address value0, address value1, uint24 value2)
internal
pure
virtual
returns (bytes32 result)
{
assembly ("memory-safe") {
let m := mload(0x40)
mstore(m, value0)
mstore(add(m, 0x20), value1)
mstore(add(m, 0x40), value2)
result := keccak256(m, 0x60)
}
}
/// @dev Wraps an `amount` of ETH to WETH and funds pool caller for swap.
function _wrapETH(uint256 amount) internal virtual {
assembly ("memory-safe") {
pop(call(gas(), WETH, amount, codesize(), 0x00, codesize(), 0x00))
mstore(0x14, caller()) // Store the `pool` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
pop(call(gas(), WETH, 0, 0x10, 0x44, codesize(), 0x00))
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Unwraps an `amount` of ETH from WETH for return.
function _unwrapETH(uint256 amount) internal virtual {
assembly ("memory-safe") {
mstore(0x00, 0x2e1a7d4d) // `withdraw(uint256)`.
mstore(0x20, amount) // Store the `amount` argument.
pop(call(gas(), WETH, 0, 0x1c, 0x24, codesize(), 0x00))
}
}
/// @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 {
assembly ("memory-safe") {
if iszero(eq(caller(), WETH)) { revert(codesize(), codesize()) }
}
}
/// @dev Guards a function from reentrancy.
modifier nonReentrant() virtual {
assembly ("memory-safe") {
if eq(sload(_REENTRANCY_GUARD_SLOT), address()) {
mstore(0x00, 0xab143c06) // `Reentrancy()`.
revert(0x1c, 0x04)
}
sstore(_REENTRANCY_GUARD_SLOT, address())
}
_;
assembly ("memory-safe") {
sstore(_REENTRANCY_GUARD_SLOT, codesize())
}
}
/// @dev Executes an `order` command from the parts of a matched intent string.
function order(
string memory tokenIn,
string memory tokenOut,
string memory amountIn,
string memory amountOut,
string memory receiver
) public payable nonReentrant returns (bytes32 hash) {
Order memory o;
uint256 decimalsIn;
uint256 decimalsOut;
if (bytes(tokenIn).length == 42) o.tokenIn = _toAddress(bytes(tokenIn));
else (o.tokenIn, decimalsIn) = _returnTokenConstants(bytes32(bytes(tokenIn)));
if (o.tokenIn == address(0)) o.tokenIn = addresses[string(tokenIn)];
if (bytes(tokenOut).length == 42) o.tokenOut = _toAddress(bytes(tokenOut));
else (o.tokenOut, decimalsOut) = _returnTokenConstants(bytes32(bytes(tokenOut)));
if (o.tokenOut == address(0)) o.tokenOut = addresses[string(bytes(tokenOut))];
o.amountIn = _toUint(
bytes(amountIn), decimalsIn != 0 ? decimalsIn : o.tokenIn.readDecimals(), o.tokenIn
);
o.amountOut = _toUint(
bytes(amountOut), decimalsOut != 0 ? decimalsOut : o.tokenOut.readDecimals(), o.tokenOut
);
if (o.tokenIn == ETH) require(msg.value == o.amountIn);
unchecked {
o.maker = msg.sender;
address _receiver;
if (bytes(receiver).length == 0) _receiver = msg.sender;
else (, _receiver,) = whatIsTheAddressOf(receiver);
o.receiver = _receiver;
o.nonce = uint48(block.timestamp);
o.expiry = uint48(block.timestamp + 1 weeks);
orders[hash = keccak256(abi.encode(o))] = o;
orderHashes.push(hash);
}
}
/// @dev Cancels a standing order by the `maker`.
function cancelOrder(bytes32 hash) public nonReentrant {
Order memory o = orders[hash];
delete orders[hash];
if (msg.sender != o.maker) revert Unauthorized();
if (o.tokenIn == ETH) msg.sender.safeTransferETH(o.amountIn);
}
/// @dev Executes a standing order for the `receiver`.
function executeOrder(bytes32 hash) public payable nonReentrant {
Order memory o = orders[hash];
delete orders[hash];
if (block.timestamp > o.expiry) revert OrderExpired();
if (o.tokenIn == ETH) msg.sender.safeTransferETH(o.amountIn);
else o.tokenIn.safeTransferFrom(o.maker, msg.sender, o.amountIn);
if (o.tokenOut == ETH) {
require(msg.value == o.amountOut);
o.receiver.safeTransferETH(msg.value);
} else {
o.tokenOut.safeTransferFrom(msg.sender, o.receiver, o.amountOut);
}
}
/// ==================== COMMAND TRANSLATION ==================== ///
/// @dev Translates an `intent` from raw `command()` calldata.
function translateCommand(bytes calldata callData)
public
pure
virtual
returns (string memory intent)
{
return string(callData[4:]);
}
/// @dev Translates an `intent` for send action from the solution `callData` of standard `execute()`.
/// note: The function selector technically doesn't need to be `execute()` but params should match.
function translateExecute(bytes calldata callData)
public
view
virtual
returns (string memory intent)
{
unchecked {
(address target, uint256 value) = abi.decode(callData[4:68], (address, uint256));
if (value != 0) {
return string(
abi.encodePacked(
"send ",
_convertWeiToString(value, 18),
" ETH to 0x",
_toAsciiString(target)
)
);
}
if (
bytes4(callData[132:136]) != IToken.transfer.selector
&& bytes4(callData[132:136]) != IToken.approve.selector
) revert InvalidSelector();
bool transfer = bytes4(callData[132:136]) == IToken.transfer.selector;
(string memory token, uint256 decimals) = _returnTokenAliasConstants(target);
if (bytes(token).length == 0) token = names[target];
if (decimals == 0) decimals = target.readDecimals(); // Sanity check.
(target, value) = abi.decode(callData[136:], (address, uint256));
return string(
abi.encodePacked(
transfer ? "send " : "approve ",
_convertWeiToString(value, decimals),
" ",
token,
" to 0x",
_toAsciiString(target)
)
);
}
}
/// @dev Translate packed ERC4337 userOp `callData` into readable `intent`.
function translateUserOp(PackedUserOperation calldata userOp)
public
view
virtual
returns (string memory intent)
{
return bytes4(userOp.callData) == IExecutor.execute.selector
? translateExecute(userOp.callData)
: translateCommand(userOp.callData);
}
/// ====================== ENS VERIFICATION ====================== ///
/// @dev Returns ENS name ownership details.
/// note: The `receiver` should be already set,
/// or, the command should use the raw address.
function whatIsTheAddressOf(string memory name)
public
view
virtual
returns (address owner, address receiver, bytes32 node)
{
// If address length, convert.
if (bytes(name).length == 42) {
receiver = _toAddress(bytes(name));
} else {
node = _namehash(string(abi.encodePacked(name, ".base.eth")));
receiver = owner = IENSHelper(BASE_ENS).addr(node);
if (receiver == address(0)) revert InvalidReceiver();
}
}
/// @dev Returns ENS reverse name resolution details.
function whatIsTheNameOf(address user) public view virtual returns (string memory) {
return IENSHelper(BASE_ENS).name(IENSHelper(BASE_ENS_REVERSE).node(user));
}
/// @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))
}
}
/// ========================= GOVERNANCE ========================= ///
/// @dev Sets a public `name` tag for a given `token` address. Governed by DAO.
function setName(address token, string calldata name) public payable virtual {
assembly ("memory-safe") {
if iszero(eq(caller(), DAO)) { revert(codesize(), codesize()) }
}
string memory normalized = string(_lowercase(bytes(name)));
names[token] = normalized;
emit NameSet(addresses[normalized] = token, normalized);
}
/// @dev Sets a public pool `pair` for swapping tokens. Governed by DAO.
function setPair(address tokenA, address tokenB, address pair) public payable virtual {
assembly ("memory-safe") {
if iszero(eq(caller(), DAO)) { revert(codesize(), codesize()) }
}
if (tokenB < tokenA) (tokenA, tokenB) = (tokenB, tokenA);
emit PairSet(tokenA, tokenB, pairs[tokenA][tokenB] = pair);
}
/// ===================== STRING OPERATIONS ===================== ///
/// @dev Returns copy of string in lowercase.
/// Modified from Solady LibString `toCase`.
function _lowercase(bytes memory subject) internal pure virtual returns (bytes memory result) {
assembly ("memory-safe") {
let len := mload(subject)
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 := len } 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, len) // Store the length.
let last := add(add(result, 0x20), len)
mstore(last, 0) // Zeroize the slot after the string.
mstore(0x40, add(last, 0x20)) // Allocate the memory.
}
}
/// @dev Extracts the first word (action) as bytes32.
function _extraction(bytes memory normalizedIntent)
internal
pure
virtual
returns (bytes32 result)
{
assembly ("memory-safe") {
let str := add(normalizedIntent, 0x20)
result := mload(str)
// Find the index of the first space or null terminator.
let spaceIndex := 32
for { let i := 0 } lt(i, 32) { i := add(i, 1) } {
let char := byte(i, result)
if or(eq(char, 0x20), eq(char, 0)) {
spaceIndex := i
break
}
}
// Create a mask to clear bytes after the first word.
let mask := shl(mul(8, sub(32, spaceIndex)), not(0))
result := and(result, mask)
}
}
/// @dev Extract the key words of normalized `send` intent.
function _extractSend(bytes memory normalizedIntent)
internal
pure
virtual
returns (bytes memory to, bytes memory amount, bytes memory token)
{
StringPart[] memory parts = _split(normalizedIntent, " ");
if (parts.length == 4) {
return (
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[3])
);
}
if (parts.length == 5) {
return (
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[2])
);
} else {
revert InvalidSyntax(); // Command is not formatted.
}
}
/// @dev Extract the key words of normalized `lock` intent.
function _extractLock(bytes memory normalizedIntent)
internal
pure
virtual
returns (
bytes memory to,
bytes memory amount,
bytes memory token,
bytes memory time,
bytes memory unit
)
{
StringPart[] memory parts = _split(normalizedIntent, " ");
if (parts.length == 7) {
return (
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[3]),
_getPart(normalizedIntent, parts[5]),
_getPart(normalizedIntent, parts[6])
);
}
if (parts.length == 8) {
return (
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[6]),
_getPart(normalizedIntent, parts[7])
);
} else {
revert InvalidSyntax(); // Command is not formatted.
}
}
/// @dev Extract the key words of normalized `swap` intent.
function _extractSwap(bytes memory normalizedIntent)
internal
pure
virtual
returns (
bytes memory amountIn,
bytes memory amountOutMin,
bytes memory tokenIn,
bytes memory tokenOut,
bytes memory receiver
)
{
StringPart[] memory parts = _split(normalizedIntent, " ");
bool isNumber;
if (parts.length == 5) {
isNumber = _isNumber(_getPart(normalizedIntent, parts[1]));
if (isNumber) {
return ( // 'exactIn'.
_getPart(normalizedIntent, parts[1]),
"",
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[4]),
""
);
} else {
return ( // 'exactOut'.
"",
_getPart(normalizedIntent, parts[3]),
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[4]),
""
);
}
} else if (parts.length == 6) {
return ( // 'minOut'.
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[5]),
""
);
} else if (parts.length == 7) {
isNumber = _isNumber(_getPart(normalizedIntent, parts[1]));
if (isNumber) {
return ( // 'exactIn' send.
_getPart(normalizedIntent, parts[1]),
"",
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[6])
);
} else {
return ( // 'exactOut' send.
"",
_getPart(normalizedIntent, parts[3]),
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[6])
);
}
} else if (parts.length == 8) {
// 'minOut' send.
return (
_getPart(normalizedIntent, parts[1]),
_getPart(normalizedIntent, parts[4]),
_getPart(normalizedIntent, parts[2]),
_getPart(normalizedIntent, parts[5]),
_getPart(normalizedIntent, parts[7])
);
} else {
revert InvalidSyntax(); // Unformatted.
}
}
/// @dev Validate whether given bytes string is number, percentage or 'all'.
function _isNumber(bytes memory s) internal pure virtual returns (bool) {
if (bytes32(s) == "all") return true;
return (s[0] >= 0x30 && s[0] <= 0x39);
}
/// @dev Splits a string into parts based on a delimiter.
function _split(bytes memory base, bytes1 delimiter)
internal
pure
virtual
returns (StringPart[] memory parts)
{
unchecked {
uint256 len = base.length;
uint256 count = 1;
// Count the number of parts.
for (uint256 i; i != len; ++i) {
if (base[i] == delimiter) {
++count;
}
}
parts = new StringPart[](count);
uint256 partIndex;
uint256 start;
// Split the string and populate parts array.
for (uint256 i; i != len; ++i) {
if (base[i] == delimiter) {
parts[partIndex++] = StringPart(start, i);
start = i + 1;
}
}
// Add the final part.
parts[partIndex] = StringPart(start, len);
}
}
/// @dev Converts a `StringPart` into its compact bytes.
function _getPart(bytes memory base, StringPart memory part)
internal
pure
virtual
returns (bytes memory)
{
unchecked {
bytes memory result = new bytes(part.end - part.start);
for (uint256 i; i != result.length; ++i) {
result[i] = base[part.start + i];
}
return result;
}
}
/// @dev Simple bytes string converter for time units.
function _simpleToUint256(bytes memory b) internal pure virtual returns (uint256) {
unchecked {
uint256 result;
for (uint256 i; i != b.length; ++i) {
if (uint8(b[i]) >= 48 && uint8(b[i]) <= 57) {
result = result * 10 + (uint8(b[i]) - 48);
} else {
revert InvalidCharacter(); // Non-digit character found.
}
}
return result;
}
}
/// @dev Convert string to decimalized numerical value.
function _toUint(bytes memory s, uint256 decimals, address token)
internal
view
virtual
returns (uint256 result)
{
unchecked {
// Check for "all" or "100%" first.
bytes32 sBytes32 = bytes32(s);
if (sBytes32 == bytes32("all") || sBytes32 == bytes32("100%")) {
return token == ETH ? msg.sender.balance + msg.value : _balanceOf(token, msg.sender);
}
uint256 len = s.length;
bool hasDecimal;
uint256 decimalPlaces;
bool isPercentage;
for (uint256 i; i < len; ++i) {
bytes1 c = s[i];
if (c >= 0x30 && c <= 0x39) {
result = result * 10 + uint8(c) - 48;
if (hasDecimal) {
if (++decimalPlaces > decimals) break;
}
} else if (c == 0x2E && !hasDecimal) {
hasDecimal = true;
} else if (c == 0x25 && i == len - 1) {
isPercentage = true;
} else if (c != 0x20) {
revert InvalidCharacter();
}
}
// Adjust for decimals.
if (!hasDecimal) {
result *= 10 ** decimals;
} else if (decimalPlaces < decimals) {
result *= 10 ** (decimals - decimalPlaces);
}
// Handle percentage.
if (isPercentage) {
uint256 balance =
token == ETH ? msg.sender.balance + msg.value : _balanceOf(token, msg.sender);
result = (balance * result) / (100 * 10 ** decimals);
}
}
}
/// @dev Converts a hexadecimal string to its `address` representation.
function _toAddress(bytes memory s) internal pure virtual returns (address addr) {
unchecked {
if (s.length != 42) revert InvalidSyntax();
uint256 result;
for (uint256 i = 2; i != 42; ++i) {
result *= 16;
uint8 b = uint8(s[i]);
if (b >= 48 && b <= 57) {
result += b - 48;
} else if (b >= 65 && b <= 70) {
result += b - 55;
} else if (b >= 97 && b <= 102) {
result += b - 87;
} else {
revert InvalidSyntax();
}
}
return address(uint160(result));
}
}
/// @dev Convert an address to an ASCII string representation.
function _toAsciiString(address x) internal pure virtual returns (string memory) {
unchecked {
bytes memory s = new bytes(40);
for (uint256 i; i != 20; ++i) {
bytes1 b = bytes1(uint8(uint256(uint160(x)) / (2 ** (8 * (19 - i)))));
bytes1 hi = bytes1(uint8(b) / 16);
bytes1 lo = bytes1(uint8(b) - 16 * uint8(hi));
s[2 * i] = _char(hi);
s[2 * i + 1] = _char(lo);
}
return string(s);
}
}
/// @dev Convert a single byte to a character in the ASCII string.
function _char(bytes1 b) internal pure virtual returns (bytes1 c) {
unchecked {
uint8 n = uint8(b) & 0xf;
c = bytes1(n + (n < 10 ? 0x30 : 0x57));
}
}
/// @dev Convert number to string and insert decimal point.
function _convertWeiToString(uint256 weiAmount, uint256 decimals)
internal
pure
virtual
returns (string memory)
{
unchecked {
uint256 scalingFactor = 10 ** decimals;
string memory wholeNumberStr = _toString(weiAmount / scalingFactor);
string memory decimalPartStr = _toString(weiAmount % scalingFactor);
while (bytes(decimalPartStr).length != decimals) {
decimalPartStr = string(abi.encodePacked("0", decimalPartStr));
}
decimalPartStr = _removeTrailingZeros(bytes(decimalPartStr));
if (bytes(decimalPartStr).length == 0) {
return wholeNumberStr;
}
return string(abi.encodePacked(wholeNumberStr, ".", decimalPartStr));
}
}
/// @dev Remove any trailing zeroes from bytes.
function _removeTrailingZeros(bytes memory str) internal pure virtual returns (string memory) {
unchecked {
uint256 len = str.length;
uint256 end = len;
while (end != 0 && str[end - 1] == 0x30) {
--end;
}
if (end == len) {
return string(str);
}
bytes memory trimmedBytes = new bytes(end);
for (uint256 i; i != end; ++i) {
trimmedBytes[i] = str[i];
}
return string(trimmedBytes);
}
}
/// @dev Returns the base 10 decimal representation of `value`.
/// Modified from (https://github.com/Vectorized/solady/blob/main/src/utils/LibString.sol)
function _toString(uint256 value) internal pure virtual returns (string memory str) {
assembly ("memory-safe") {
str := add(mload(0x40), 0x80)
mstore(0x40, add(str, 0x20))
mstore(str, 0)
let end := str
let w := not(0)
for { let temp := value } 1 {} {
str := add(str, w)
mstore8(str, add(48, mod(temp, 10)))
temp := div(temp, 10)
if iszero(temp) { break }
}
let len := sub(end, str)
str := sub(str, 0x20)
mstore(str, len)
}
}
}
/// @dev ENS name resolution helper contracts interface.
interface IENSHelper {
function addr(bytes32) external view returns (address);
function node(address) external view returns (bytes32);
function name(bytes32) external view returns (string memory);
}
/// @dev Simple token handler interface.
interface IToken {
function approve(address, uint256) external returns (bool);
function transfer(address, uint256) external returns (bool);
}
/// @notice Simple calldata executor interface.
interface IExecutor {
function execute(address, uint256, bytes calldata) external payable returns (bytes memory);
}
/// @dev Simple Escrows protocol locking interface.
interface IEscrows {
function escrow(address, address, address, address, uint256, string calldata, uint256)
external
payable
returns (bytes32);
}
/// @dev Simple Uniswap V3 swapping interface.
interface ISwapRouter {
function swap(address, bool, int256, uint160, bytes calldata)
external
returns (int256, int256);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Library for reading contract metadata robustly.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/MetadataReaderLib.sol)
library MetadataReaderLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Default gas stipend for contract reads. High enough for most practical use cases
/// (able to SLOAD about 1000 bytes of data), but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev Default string byte length limit.
uint256 internal constant STRING_LIMIT_DEFAULT = 1000;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* METADATA READING OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// Best-effort string reading operations.
// Should NOT revert as long as sufficient gas is provided.
//
// Performs the following in order:
// 1. Returns the empty string for the following cases:
// - Reverts.
// - No returndata (e.g. function returns nothing, EOA).
// - Returns empty string.
// 2. Attempts to `abi.decode` the returndata into a string.
// 3. With any remaining gas, scans the returndata from start to end for the
// null byte '\0', to interpret the returndata as a null-terminated string.
/// @dev Equivalent to `readString(abi.encodeWithSignature("name()"))`.
function readName(address target) internal view returns (string memory) {
return _string(target, _ptr(0x06fdde03), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);
}
/// @dev Equivalent to `readString(abi.encodeWithSignature("name()"), limit)`.
function readName(address target, uint256 limit) internal view returns (string memory) {
return _string(target, _ptr(0x06fdde03), limit, GAS_STIPEND_NO_GRIEF);
}
/// @dev Equivalent to `readString(abi.encodeWithSignature("name()"), limit, gasStipend)`.
function readName(address target, uint256 limit, uint256 gasStipend)
internal
view
returns (string memory)
{
return _string(target, _ptr(0x06fdde03), limit, gasStipend);
}
/// @dev Equivalent to `readString(abi.encodeWithSignature("symbol()"))`.
function readSymbol(address target) internal view returns (string memory) {
return _string(target, _ptr(0x95d89b41), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);
}
/// @dev Equivalent to `readString(abi.encodeWithSignature("symbol()"), limit)`.
function readSymbol(address target, uint256 limit) internal view returns (string memory) {
return _string(target, _ptr(0x95d89b41), limit, GAS_STIPEND_NO_GRIEF);
}
/// @dev Equivalent to `readString(abi.encodeWithSignature("symbol()"), limit, gasStipend)`.
function readSymbol(address target, uint256 limit, uint256 gasStipend)
internal
view
returns (string memory)
{
return _string(target, _ptr(0x95d89b41), limit, gasStipend);
}
/// @dev Performs a best-effort string query on `target` with `data` as the calldata.
/// The string will be truncated to `STRING_LIMIT_DEFAULT` (1000) bytes.
function readString(address target, bytes memory data) internal view returns (string memory) {
return _string(target, _ptr(data), STRING_LIMIT_DEFAULT, GAS_STIPEND_NO_GRIEF);
}
/// @dev Performs a best-effort string query on `target` with `data` as the calldata.
/// The string will be truncated to `limit` bytes.
function readString(address target, bytes memory data, uint256 limit)
internal
view
returns (string memory)
{
return _string(target, _ptr(data), limit, GAS_STIPEND_NO_GRIEF);
}
/// @dev Performs a best-effort string query on `target` with `data` as the calldata.
/// The string will be truncated to `limit` bytes.
function readString(address target, bytes memory data, uint256 limit, uint256 gasStipend)
internal
view
returns (string memory)
{
return _string(target, _ptr(data), limit, gasStipend);
}
// Best-effort unsigned integer reading operations.
// Should NOT revert as long as sufficient gas is provided.
//
// Performs the following in order:
// 1. Attempts to `abi.decode` the result into a uint256
// (equivalent across all Solidity uint types, downcast as needed).
// 2. Returns zero for the following cases:
// - Reverts.
// - No returndata (e.g. function returns nothing, EOA).
// - Returns zero.
// - `abi.decode` failure.
/// @dev Equivalent to `uint8(readUint(abi.encodeWithSignature("decimals()")))`.
function readDecimals(address target) internal view returns (uint8) {
return uint8(_uint(target, _ptr(0x313ce567), GAS_STIPEND_NO_GRIEF));
}
/// @dev Equivalent to `uint8(readUint(abi.encodeWithSignature("decimals()"), gasStipend))`.
function readDecimals(address target, uint256 gasStipend) internal view returns (uint8) {
return uint8(_uint(target, _ptr(0x313ce567), gasStipend));
}
/// @dev Performs a best-effort uint query on `target` with `data` as the calldata.
function readUint(address target, bytes memory data) internal view returns (uint256) {
return _uint(target, _ptr(data), GAS_STIPEND_NO_GRIEF);
}
/// @dev Performs a best-effort uint query on `target` with `data` as the calldata.
function readUint(address target, bytes memory data, uint256 gasStipend)
internal
view
returns (uint256)
{
return _uint(target, _ptr(data), gasStipend);
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* PRIVATE HELPERS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Attempts to read and return a string at `target`.
function _string(address target, bytes32 ptr, uint256 limit, uint256 gasStipend)
private
view
returns (string memory result)
{
/// @solidity memory-safe-assembly
assembly {
function min(x_, y_) -> _z {
_z := xor(x_, mul(xor(x_, y_), lt(y_, x_)))
}
for {} staticcall(gasStipend, target, add(ptr, 0x20), mload(ptr), 0x00, 0x20) {} {
let m := mload(0x40) // Grab the free memory pointer.
let s := add(0x20, m) // Start of the string's bytes in memory.
// Attempt to `abi.decode` if the returndatasize is greater or equal to 64.
if iszero(lt(returndatasize(), 0x40)) {
let o := mload(0x00) // Load the string's offset in the returndata.
// If the string's offset is within bounds.
if iszero(gt(o, sub(returndatasize(), 0x20))) {
returndatacopy(m, o, 0x20) // Copy the string's length.
// If the full string's end is within bounds.
// Note: If the full string doesn't fit, the `abi.decode` must be aborted
// for compliance purposes, regardless if the truncated string can fit.
if iszero(gt(mload(m), sub(returndatasize(), add(o, 0x20)))) {
let n := min(mload(m), limit) // Truncate if needed.
mstore(m, n) // Overwrite the length.
returndatacopy(s, add(o, 0x20), n) // Copy the string's bytes.
mstore(add(s, n), 0) // Zeroize the slot after the string.
mstore(0x40, add(0x20, add(s, n))) // Allocate memory for the string.
result := m
break
}
}
}
// Try interpreting as a null-terminated string.
let n := min(returndatasize(), limit) // Truncate if needed.
returndatacopy(s, 0, n) // Copy the string's bytes.
mstore8(add(s, n), 0) // Place a '\0' at the end.
let i := s // Pointer to the next byte to scan.
for {} byte(0, mload(i)) { i := add(i, 1) } {} // Scan for '\0'.
mstore(m, sub(i, s)) // Store the string's length.
mstore(i, 0) // Zeroize the slot after the string.
mstore(0x40, add(0x20, i)) // Allocate memory for the string.
result := m
break
}
}
}
/// @dev Attempts to read and return a uint at `target`.
function _uint(address target, bytes32 ptr, uint256 gasStipend)
private
view
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
result :=
mul(
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gasStipend, target, add(ptr, 0x20), mload(ptr), 0x20, 0x20)
)
)
}
}
/// @dev Casts the function selector `s` into a pointer.
function _ptr(uint256 s) private pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
// Layout the calldata in the scratch space for temporary usage.
mstore(0x04, s) // Store the function selector.
mstore(result, 4) // Store the length.
}
}
/// @dev Casts the `data` into a pointer.
function _ptr(bytes memory data) private pure returns (bytes32 result) {
/// @solidity memory-safe-assembly
assembly {
result := data
}
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/SafeTransferLib.sol)
/// @author Modified from Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @author Permit2 operations from (https://github.com/Uniswap/permit2/blob/main/src/libraries/Permit2Lib.sol)
///
/// @dev Note:
/// - For ETH transfers, please use `forceSafeTransferETH` for DoS protection.
/// - For ERC20s, this implementation won't check that a token has code,
/// responsibility is delegated to the caller.
library SafeTransferLib {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev The ETH transfer has failed.
error ETHTransferFailed();
/// @dev The ERC20 `transferFrom` has failed.
error TransferFromFailed();
/// @dev The ERC20 `transfer` has failed.
error TransferFailed();
/// @dev The ERC20 `approve` has failed.
error ApproveFailed();
/// @dev The Permit2 operation has failed.
error Permit2Failed();
/// @dev The Permit2 amount must be less than `2**160 - 1`.
error Permit2AmountOverflow();
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CONSTANTS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Suggested gas stipend for contract receiving ETH that disallows any storage writes.
uint256 internal constant GAS_STIPEND_NO_STORAGE_WRITES = 2300;
/// @dev Suggested gas stipend for contract receiving ETH to perform a few
/// storage reads and writes, but low enough to prevent griefing.
uint256 internal constant GAS_STIPEND_NO_GRIEF = 100000;
/// @dev The unique EIP-712 domain domain separator for the DAI token contract.
bytes32 internal constant DAI_DOMAIN_SEPARATOR =
0xdbb8cf42e1ecb028be3f3dbc922e1d878b963f411dc388ced501601c60f7c6f7;
/// @dev The address for the WETH9 contract on Ethereum mainnet.
address internal constant WETH9 = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
/// @dev The canonical Permit2 address.
/// [Github](https://github.com/Uniswap/permit2)
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
address internal constant PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ETH OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
// If the ETH transfer MUST succeed with a reasonable gas budget, use the force variants.
//
// The regular variants:
// - Forwards all remaining gas to the target.
// - Reverts if the target reverts.
// - Reverts if the current contract has insufficient balance.
//
// The force variants:
// - Forwards with an optional gas stipend
// (defaults to `GAS_STIPEND_NO_GRIEF`, which is sufficient for most cases).
// - If the target reverts, or if the gas stipend is exhausted,
// creates a temporary contract to force send the ETH via `SELFDESTRUCT`.
// Future compatible with `SENDALL`: https://eips.ethereum.org/EIPS/eip-4758.
// - Reverts if the current contract has insufficient balance.
//
// The try variants:
// - Forwards with a mandatory gas stipend.
// - Instead of reverting, returns whether the transfer succeeded.
/// @dev Sends `amount` (in wei) ETH to `to`.
function safeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gas(), to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Sends all the ETH in the current contract to `to`.
function safeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// Transfer all the ETH and check if it succeeded or not.
if iszero(call(gas(), to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function forceSafeTransferETH(address to, uint256 amount, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with a `gasStipend`.
function forceSafeTransferAllETH(address to, uint256 gasStipend) internal {
/// @solidity memory-safe-assembly
assembly {
if iszero(call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends `amount` (in wei) ETH to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferETH(address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
if lt(selfbalance(), amount) {
mstore(0x00, 0xb12d13eb) // `ETHTransferFailed()`.
revert(0x1c, 0x04)
}
if iszero(call(GAS_STIPEND_NO_GRIEF, to, amount, codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(amount, 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Force sends all the ETH in the current contract to `to`, with `GAS_STIPEND_NO_GRIEF`.
function forceSafeTransferAllETH(address to) internal {
/// @solidity memory-safe-assembly
assembly {
// forgefmt: disable-next-item
if iszero(call(GAS_STIPEND_NO_GRIEF, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)) {
mstore(0x00, to) // Store the address in scratch space.
mstore8(0x0b, 0x73) // Opcode `PUSH20`.
mstore8(0x20, 0xff) // Opcode `SELFDESTRUCT`.
if iszero(create(selfbalance(), 0x0b, 0x16)) { revert(codesize(), codesize()) } // For gas estimation.
}
}
}
/// @dev Sends `amount` (in wei) ETH to `to`, with a `gasStipend`.
function trySafeTransferETH(address to, uint256 amount, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, amount, codesize(), 0x00, codesize(), 0x00)
}
}
/// @dev Sends all the ETH in the current contract to `to`, with a `gasStipend`.
function trySafeTransferAllETH(address to, uint256 gasStipend)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
success := call(gasStipend, to, selfbalance(), codesize(), 0x00, codesize(), 0x00)
}
}
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* ERC20 OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for
/// the current contract to manage.
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
// Perform the transfer, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function trySafeTransferFrom(address token, address from, address to, uint256 amount)
internal
returns (bool success)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x60, amount) // Store the `amount` argument.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x23b872dd000000000000000000000000) // `transferFrom(address,address,uint256)`.
success :=
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
)
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends all of ERC20 `token` from `from` to `to`.
/// Reverts upon failure.
///
/// The `from` account must have their entire balance approved for the current contract to manage.
function safeTransferAllFrom(address token, address from, address to)
internal
returns (uint256 amount)
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40) // Cache the free memory pointer.
mstore(0x40, to) // Store the `to` argument.
mstore(0x2c, shl(96, from)) // Store the `from` argument.
mstore(0x0c, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x60, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x00, 0x23b872dd) // `transferFrom(address,address,uint256)`.
amount := mload(0x60) // The `amount` is already at 0x60. We'll need to return it.
// Perform the transfer, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x1c, 0x64, 0x00, 0x20)
)
) {
mstore(0x00, 0x7939f424) // `TransferFromFailed()`.
revert(0x1c, 0x04)
}
mstore(0x60, 0) // Restore the zero slot to zero.
mstore(0x40, m) // Restore the free memory pointer.
}
}
/// @dev Sends `amount` of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransfer(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sends all of ERC20 `token` from the current contract to `to`.
/// Reverts upon failure.
function safeTransferAll(address token, address to) internal returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x00, 0x70a08231) // Store the function selector of `balanceOf(address)`.
mstore(0x20, address()) // Store the address of the current contract.
// Read the balance, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x1c, 0x24, 0x34, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x14, to) // Store the `to` argument.
amount := mload(0x34) // The `amount` is already at 0x34. We'll need to return it.
mstore(0x00, 0xa9059cbb000000000000000000000000) // `transfer(address,uint256)`.
// Perform the transfer, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
)
) {
mstore(0x00, 0x90b8ec18) // `TransferFailed()`.
revert(0x1c, 0x04)
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// Reverts upon failure.
function safeApprove(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, reverting upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
)
) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Sets `amount` of ERC20 `token` for `to` to manage on behalf of the current contract.
/// If the initial attempt to approve fails, attempts to reset the approved amount to zero,
/// then retries the approval again (some tokens, e.g. USDT, requires this).
/// Reverts upon failure.
function safeApproveWithRetry(address token, address to, uint256 amount) internal {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, to) // Store the `to` argument.
mstore(0x34, amount) // Store the `amount` argument.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
// Perform the approval, retrying upon failure.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
)
) {
mstore(0x34, 0) // Store 0 for the `amount`.
mstore(0x00, 0x095ea7b3000000000000000000000000) // `approve(address,uint256)`.
pop(call(gas(), token, 0, 0x10, 0x44, codesize(), 0x00)) // Reset the approval.
mstore(0x34, amount) // Store back the original `amount`.
// Retry the approval, reverting upon failure.
if iszero(
and(
or(eq(mload(0x00), 1), iszero(returndatasize())), // Returned 1 or nothing.
call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20)
)
) {
mstore(0x00, 0x3e3f8f73) // `ApproveFailed()`.
revert(0x1c, 0x04)
}
}
mstore(0x34, 0) // Restore the part of the free memory pointer that was overwritten.
}
}
/// @dev Returns the amount of ERC20 `token` owned by `account`.
/// Returns zero if the `token` does not exist.
function balanceOf(address token, address account) internal view returns (uint256 amount) {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, account) // Store the `account` argument.
mstore(0x00, 0x70a08231000000000000000000000000) // `balanceOf(address)`.
amount :=
mul( // The arguments of `mul` are evaluated from right to left.
mload(0x20),
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x1f), // At least 32 bytes returned.
staticcall(gas(), token, 0x10, 0x24, 0x20, 0x20)
)
)
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to`.
/// If the initial attempt fails, try to use Permit2 to transfer the token.
/// Reverts upon failure.
///
/// The `from` account must have at least `amount` approved for the current contract to manage.
function safeTransferFrom2(address token, address from, address to, uint256 amount) internal {
if (!trySafeTransferFrom(token, from, to, amount)) {
permit2TransferFrom(token, from, to, amount);
}
}
/// @dev Sends `amount` of ERC20 `token` from `from` to `to` via Permit2.
/// Reverts upon failure.
function permit2TransferFrom(address token, address from, address to, uint256 amount)
internal
{
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(add(m, 0x74), shr(96, shl(96, token)))
mstore(add(m, 0x54), amount)
mstore(add(m, 0x34), to)
mstore(add(m, 0x20), shl(96, from))
// `transferFrom(address,address,uint160,address)`.
mstore(m, 0x36c78516000000000000000000000000)
let p := PERMIT2
let exists := eq(chainid(), 1)
if iszero(exists) { exists := iszero(iszero(extcodesize(p))) }
if iszero(and(call(gas(), p, 0, add(m, 0x10), 0x84, codesize(), 0x00), exists)) {
mstore(0x00, 0x7939f4248757f0fd) // `TransferFromFailed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(iszero(shr(160, amount))))), 0x04)
}
}
}
/// @dev Permit a user to spend a given amount of
/// another user's tokens via native EIP-2612 permit if possible, falling
/// back to Permit2 if native permit fails or is not implemented on the token.
function permit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
bool success;
/// @solidity memory-safe-assembly
assembly {
for {} shl(96, xor(token, WETH9)) {} {
mstore(0x00, 0x3644e515) // `DOMAIN_SEPARATOR()`.
if iszero(
and( // The arguments of `and` are evaluated from right to left.
lt(iszero(mload(0x00)), eq(returndatasize(), 0x20)), // Returns 1 non-zero word.
// Gas stipend to limit gas burn for tokens that don't refund gas when
// an non-existing function is called. 5K should be enough for a SLOAD.
staticcall(5000, token, 0x1c, 0x04, 0x00, 0x20)
)
) { break }
// After here, we can be sure that token is a contract.
let m := mload(0x40)
mstore(add(m, 0x34), spender)
mstore(add(m, 0x20), shl(96, owner))
mstore(add(m, 0x74), deadline)
if eq(mload(0x00), DAI_DOMAIN_SEPARATOR) {
mstore(0x14, owner)
mstore(0x00, 0x7ecebe00000000000000000000000000) // `nonces(address)`.
mstore(add(m, 0x94), staticcall(gas(), token, 0x10, 0x24, add(m, 0x54), 0x20))
mstore(m, 0x8fcbaf0c000000000000000000000000) // `IDAIPermit.permit`.
// `nonces` is already at `add(m, 0x54)`.
// `1` is already stored at `add(m, 0x94)`.
mstore(add(m, 0xb4), and(0xff, v))
mstore(add(m, 0xd4), r)
mstore(add(m, 0xf4), s)
success := call(gas(), token, 0, add(m, 0x10), 0x104, codesize(), 0x00)
break
}
mstore(m, 0xd505accf000000000000000000000000) // `IERC20Permit.permit`.
mstore(add(m, 0x54), amount)
mstore(add(m, 0x94), and(0xff, v))
mstore(add(m, 0xb4), r)
mstore(add(m, 0xd4), s)
success := call(gas(), token, 0, add(m, 0x10), 0xe4, codesize(), 0x00)
break
}
}
if (!success) simplePermit2(token, owner, spender, amount, deadline, v, r, s);
}
/// @dev Simple permit on the Permit2 contract.
function simplePermit2(
address token,
address owner,
address spender,
uint256 amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
/// @solidity memory-safe-assembly
assembly {
let m := mload(0x40)
mstore(m, 0x927da105) // `allowance(address,address,address)`.
{
let addressMask := shr(96, not(0))
mstore(add(m, 0x20), and(addressMask, owner))
mstore(add(m, 0x40), and(addressMask, token))
mstore(add(m, 0x60), and(addressMask, spender))
mstore(add(m, 0xc0), and(addressMask, spender))
}
let p := mul(PERMIT2, iszero(shr(160, amount)))
if iszero(
and( // The arguments of `and` are evaluated from right to left.
gt(returndatasize(), 0x5f), // Returns 3 words: `amount`, `expiration`, `nonce`.
staticcall(gas(), p, add(m, 0x1c), 0x64, add(m, 0x60), 0x60)
)
) {
mstore(0x00, 0x6b836e6b8757f0fd) // `Permit2Failed()` or `Permit2AmountOverflow()`.
revert(add(0x18, shl(2, iszero(p))), 0x04)
}
mstore(m, 0x2b67b570) // `Permit2.permit` (PermitSingle variant).
// `owner` is already `add(m, 0x20)`.
// `token` is already at `add(m, 0x40)`.
mstore(add(m, 0x60), amount)
mstore(add(m, 0x80), 0xffffffffffff) // `expiration = type(uint48).max`.
// `nonce` is already at `add(m, 0xa0)`.
// `spender` is already at `add(m, 0xc0)`.
mstore(add(m, 0xe0), deadline)
mstore(add(m, 0x100), 0x100) // `signature` offset.
mstore(add(m, 0x120), 0x41) // `signature` length.
mstore(add(m, 0x140), r)
mstore(add(m, 0x160), s)
mstore(add(m, 0x180), shl(248, v))
if iszero(call(gas(), p, 0, add(m, 0x1c), 0x184, codesize(), 0x00)) {
mstore(0x00, 0x6b836e6b) // `Permit2Failed()`.
revert(0x1c, 0x04)
}
}
}
}
{
"compilationTarget": {
"src/IEBase.sol": "IEBase"
},
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 9999
},
"remappings": [
":forge-std/=lib/forge-std/src/",
":solady/=lib/solady/src/"
],
"viaIR": true
}
[{"inputs":[],"stateMutability":"payable","type":"constructor"},{"inputs":[],"name":"InsufficientSwap","type":"error"},{"inputs":[],"name":"InvalidCharacter","type":"error"},{"inputs":[],"name":"InvalidReceiver","type":"error"},{"inputs":[],"name":"InvalidSelector","type":"error"},{"inputs":[],"name":"InvalidSwap","type":"error"},{"inputs":[],"name":"InvalidSyntax","type":"error"},{"inputs":[],"name":"OrderExpired","type":"error"},{"inputs":[],"name":"Overflow","type":"error"},{"inputs":[],"name":"Reentrancy","type":"error"},{"inputs":[],"name":"Unauthorized","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"string","name":"name","type":"string"}],"name":"NameSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"token0","type":"address"},{"indexed":false,"internalType":"address","name":"token1","type":"address"},{"indexed":false,"internalType":"address","name":"pair","type":"address"}],"name":"PairSet","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[{"internalType":"string","name":"name","type":"string"}],"name":"addresses","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"hash","type":"bytes32"}],"name":"cancelOrder","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"intent","type":"string"},{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"bytes","name":"initCode","type":"bytes"},{"internalType":"bytes","name":"callData","type":"bytes"},{"internalType":"bytes32","name":"accountGasLimits","type":"bytes32"},{"internalType":"uint256","name":"preVerificationGas","type":"uint256"},{"internalType":"bytes32","name":"gasFees","type":"bytes32"},{"internalType":"bytes","name":"paymasterAndData","type":"bytes"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct IEBase.PackedUserOperation","name":"userOp","type":"tuple"}],"name":"checkUserOp","outputs":[{"internalType":"bool","name":"intentMatched","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string[]","name":"intents","type":"string[]"}],"name":"command","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"intent","type":"string"}],"name":"command","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"to","type":"string"},{"internalType":"string","name":"amount","type":"string"},{"internalType":"string","name":"token","type":"string"},{"internalType":"string","name":"time","type":"string"},{"internalType":"string","name":"unit","type":"string"}],"name":"escrow","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"hash","type":"bytes32"}],"name":"executeOrder","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"to","type":"string"},{"internalType":"string","name":"amount","type":"string"},{"internalType":"string","name":"token","type":"string"},{"internalType":"string","name":"time","type":"string"},{"internalType":"string","name":"unit","type":"string"}],"name":"lock","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"addresses","type":"address"}],"name":"names","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"tokenIn","type":"string"},{"internalType":"string","name":"tokenOut","type":"string"},{"internalType":"string","name":"amountIn","type":"string"},{"internalType":"string","name":"amountOut","type":"string"},{"internalType":"string","name":"receiver","type":"string"}],"name":"order","outputs":[{"internalType":"bytes32","name":"hash","type":"bytes32"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"orderHashes","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"orderHash","type":"bytes32"}],"name":"orders","outputs":[{"internalType":"address","name":"tokenIn","type":"address"},{"internalType":"address","name":"tokenOut","type":"address"},{"internalType":"uint256","name":"amountIn","type":"uint256"},{"internalType":"uint256","name":"amountOut","type":"uint256"},{"internalType":"address","name":"maker","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint48","name":"nonce","type":"uint48"},{"internalType":"uint48","name":"expiry","type":"uint48"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token0","type":"address"},{"internalType":"address","name":"token1","type":"address"}],"name":"pairs","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"intent","type":"string"}],"name":"previewCommand","outputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"address","name":"token","type":"address"},{"internalType":"bytes","name":"callData","type":"bytes"},{"internalType":"bytes","name":"executeCallData","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"to","type":"string"},{"internalType":"string","name":"amount","type":"string"},{"internalType":"string","name":"token","type":"string"}],"name":"send","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"string","name":"name","type":"string"}],"name":"setName","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenA","type":"address"},{"internalType":"address","name":"tokenB","type":"address"},{"internalType":"address","name":"pair","type":"address"}],"name":"setPair","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"amountIn","type":"string"},{"internalType":"string","name":"amountOutMin","type":"string"},{"internalType":"string","name":"tokenIn","type":"string"},{"internalType":"string","name":"tokenOut","type":"string"},{"internalType":"string","name":"receiver","type":"string"}],"name":"swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"bytes","name":"callData","type":"bytes"}],"name":"translateCommand","outputs":[{"internalType":"string","name":"intent","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes","name":"callData","type":"bytes"}],"name":"translateExecute","outputs":[{"internalType":"string","name":"intent","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint256","name":"nonce","type":"uint256"},{"internalType":"bytes","name":"initCode","type":"bytes"},{"internalType":"bytes","name":"callData","type":"bytes"},{"internalType":"bytes32","name":"accountGasLimits","type":"bytes32"},{"internalType":"uint256","name":"preVerificationGas","type":"uint256"},{"internalType":"bytes32","name":"gasFees","type":"bytes32"},{"internalType":"bytes","name":"paymasterAndData","type":"bytes"},{"internalType":"bytes","name":"signature","type":"bytes"}],"internalType":"struct IEBase.PackedUserOperation","name":"userOp","type":"tuple"}],"name":"translateUserOp","outputs":[{"internalType":"string","name":"intent","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"name","type":"string"}],"name":"whatIsTheAddressOf","outputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"bytes32","name":"node","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"whatIsTheNameOf","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]