Cryo Explorer Ethereum Mainnet

Address Contract Verified

Address 0x004BE7a661985c963d13C9F336E298570DfAF6D4
Balance 0.000018405 ETH
Nonce 1
Code Size 8400 bytes
Indexed Transactions 0
External Etherscan · Sourcify

Contract Bytecode

8400 bytes
0x60806040526004361061009f575f3560e01c8063827f6e5c11610063578063827f6e5c1461017c578063af48bcb9146101a6578063b8039e98146101d0578063d81a055314610200578063deab3ebd1461022a578063ee7d72b414610254576100ae565b806306af20da146100b2578063249bf7e5146100da57806331ac9920146101045780633c5fd9821461012c578063710e5d2f14610154576100ae565b366100ae576100ac61027c565b005b5f80fd5b3480156100bd575f80fd5b506100d860048036038101906100d391906112f3565b6102e3565b005b3480156100e5575f80fd5b506100ee610361565b6040516100fb9190611365565b60405180910390f35b34801561010f575f80fd5b5061012a600480360381019061012591906113a8565b610366565b005b348015610137575f80fd5b50610152600480360381019061014d91906113a8565b610378565b005b34801561015f575f80fd5b5061017a600480360381019061017591906113a8565b61038a565b005b348015610187575f80fd5b5061019061039c565b60405161019d9190611365565b60405180910390f35b3480156101b1575f80fd5b506101ba6103a2565b6040516101c79190611365565b60405180910390f35b6101ea60048036038101906101e5919061148a565b6103a8565b6040516101f79190611365565b60405180910390f35b34801561020b575f80fd5b50610214610ae3565b6040516102219190611539565b60405180910390f35b348015610235575f80fd5b5061023e610b07565b60405161024b9190611365565b60405180910390f35b34801561025f575f80fd5b5061027a600480360381019061027591906113a8565b610b0d565b005b3273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16036102e1576040517f1b10b0f900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b6102eb610b1e565b6102f481610ba5565b5f825190505f5b8181101561035b575f61032885838151811061031a57610319611552565b5b602002602001015130610c0d565b905061034f8583815181106103405761033f611552565b5b60200260200101518583610cf0565b816001019150506102fb565b50505050565b5f5481565b61036e610b1e565b8060028190555050565b610380610b1e565b8060038190555050565b610392610b1e565b8060018190555050565b60035481565b60025481565b5f814211156103ec576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016103e3906115d9565b60405180910390fd5b6127106001548660a001356104019190611624565b61040b9190611692565b8560e001351115610451576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104489061170c565b60405180910390fd5b6127106002548660a001356104669190611624565b6104709190611692565b8560e0013510156104b6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104ad90611774565b60405180910390fd5b6104d18560400160208101906104cc9190611792565b610ba5565b6104ec8560600160208101906104e79190611792565b610ba5565b5f85610120013511610533576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161052a90611807565b60405180910390fd5b8460a00135856101200135111561057f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016105769061186f565b60405180910390fd5b846020016020810190610592919061188d565b73ffffffffffffffffffffffffffffffffffffffff16855f0160208101906105ba919061188d565b73ffffffffffffffffffffffffffffffffffffffff1603610610576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161060790611902565b60405180910390fd5b5f8560e0013511610656576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161064d9061196a565b60405180910390fd5b5f8560c00135101561069d576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610694906119d2565b60405180910390fd5b6003548560e001358660c001358761010001356106ba91906119f0565b6106c491906119f0565b6106ce9190611a23565b8560a00135108061071257506003548560e001358660c001358761010001356106f791906119f0565b61070191906119f0565b61070b91906119f0565b8560a00135115b15610752576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161074990611aa0565b60405180910390fd5b7f000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff16855f01602081019061079b919061188d565b73ffffffffffffffffffffffffffffffffffffffff160361080157846080013534146107fc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016107f390611b2e565b60405180910390fd5b6108ad565b5f3414610843576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161083a90611b2e565b60405180910390fd5b6108ac3386604001602081019061085a9190611792565b8760800135886101400160208101906108739190611b81565b895f016020810190610885919061188d565b73ffffffffffffffffffffffffffffffffffffffff16610e9b90949392919063ffffffff16565b5b5f8560200160208101906108c1919061188d565b90505f6108ce8230610c0d565b90508773ffffffffffffffffffffffffffffffffffffffff1663c0dcf8043488888b6020016020810190610902919061188d565b6040518563ffffffff1660e01b815260040161092093929190611e6b565b5f604051808303818588803b158015610937575f80fd5b505af1158015610949573d5f803e3d5ffd5b50505050505f6109598330610c0d565b90505f82826109689190611a23565b90505f8960e001358a610100013561098091906119f0565b8211156109a5578960e00135826109979190611a23565b95508960e0013590506109d7565b8961010001358211156109d2578961010001359550896101000135826109cb9190611a23565b90506109d6565b8195505b5b896101200135861015610a1f576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610a1690611ee5565b60405180910390fd5b610a3c858b6060016020810190610a369190611792565b88610cf0565b89806101600190610a4d9190611f0f565b604051610a5b929190611f9f565b60405180910390207f629a08419b072539e5155010332f1ff6f6a44cefdd7b20cca2aa40ef69d0a2f48b5f016020810190610a96919061188d565b8c608001358d6020016020810190610aae919061188d565b8e60a001358f60c00135878d604051610acd9796959493929190611fb7565b60405180910390a2505050505095945050505050565b7f000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee81565b60015481565b610b15610b1e565b805f8190555050565b7f00000000000000000000000084dbae2549d67caf00f65c355de3d6f4df59a32c73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614610ba3576040517ffcb6435400000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b565b5f73ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1603610c0a576040517fd92e233d00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b50565b5f7f000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610c6f575f8231905080915050610cea565b8273ffffffffffffffffffffffffffffffffffffffff166370a08231836040518263ffffffff1660e01b8152600401610ca89190611539565b602060405180830381865afa158015610cc3573d5f803e3d5ffd5b505050506040513d601f19601f82011682018060405250810190610ce79190612038565b90505b92915050565b5f811115610e64577f000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee73ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff1603610e33575f47905081811015610d89576040517ff4d678b800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f8373ffffffffffffffffffffffffffffffffffffffff16835f5490604051610db190612086565b5f60405180830381858888f193505050503d805f8114610dec576040519150601f19603f3d011682016040523d82523d5f602084013e610df1565b606091505b5050905080610e2c576040517ff4b3b1bc00000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5050610e5f565b610e5e82828573ffffffffffffffffffffffffffffffffffffffff16610ec69092919063ffffffff16565b5b610e96565b6040517ff4d678b800000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505050565b8015610eb257610ead85858585610f14565b610ebf565b610ebe85858585611004565b5b5050505050565b610ed98363a9059cbb60e01b848461109c565b610f0f576040517ffb7f507900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505050565b73ffffffffffffffffffffffffffffffffffffffff8016811115610f64576040517f8112e11900000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b5f6336c7851660e01b90505f6040518281528560048201528460248201528360448201528660648201525f806084835f6e22d473030f116ddee9f6b43ac78ba35af191508115610fc4575f6e22d473030f116ddee9f6b43ac78ba33b1191505b5080610ffc576040517ff405907100000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505050505050565b5f6323b872dd60e01b90505f60405182815285600482015284602482015283604482015260205f6064835f8b5af19150811561105c573d5f81146110535760015f5114601f3d1116925061105a565b5f883b1192505b505b5080611094576040517ff405907100000000000000000000000000000000000000000000000000000000815260040160405180910390fd5b505050505050565b5f60405184815283600482015282602482015260205f6044835f8a5af1915081156110e3573d5f81146110da5760015f5114601f3d111692506110e1565b5f873b1192505b505b50949350505050565b5f604051905090565b5f80fd5b5f80fd5b5f80fd5b5f601f19601f8301169050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52604160045260245ffd5b61114782611101565b810181811067ffffffffffffffff8211171561116657611165611111565b5b80604052505050565b5f6111786110ec565b9050611184828261113e565b919050565b5f67ffffffffffffffff8211156111a3576111a2611111565b5b602082029050602081019050919050565b5f80fd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6111e1826111b8565b9050919050565b5f6111f2826111d7565b9050919050565b611202816111e8565b811461120c575f80fd5b50565b5f8135905061121d816111f9565b92915050565b5f61123561123084611189565b61116f565b90508083825260208201905060208402830185811115611258576112576111b4565b5b835b81811015611281578061126d888261120f565b84526020840193505060208101905061125a565b5050509392505050565b5f82601f83011261129f5761129e6110fd565b5b81356112af848260208601611223565b91505092915050565b5f6112c2826111b8565b9050919050565b6112d2816112b8565b81146112dc575f80fd5b50565b5f813590506112ed816112c9565b92915050565b5f8060408385031215611309576113086110f5565b5b5f83013567ffffffffffffffff811115611326576113256110f9565b5b6113328582860161128b565b9250506020611343858286016112df565b9150509250929050565b5f819050919050565b61135f8161134d565b82525050565b5f6020820190506113785f830184611356565b92915050565b6113878161134d565b8114611391575f80fd5b50565b5f813590506113a28161137e565b92915050565b5f602082840312156113bd576113bc6110f5565b5b5f6113ca84828501611394565b91505092915050565b5f6113dd826111d7565b9050919050565b6113ed816113d3565b81146113f7575f80fd5b50565b5f81359050611408816113e4565b92915050565b5f80fd5b5f61018082840312156114285761142761140e565b5b81905092915050565b5f80fd5b5f8083601f84011261144a576114496110fd565b5b8235905067ffffffffffffffff81111561146757611466611431565b5b602083019150836020820283011115611483576114826111b4565b5b9250929050565b5f805f805f608086880312156114a3576114a26110f5565b5b5f6114b0888289016113fa565b955050602086013567ffffffffffffffff8111156114d1576114d06110f9565b5b6114dd88828901611412565b945050604086013567ffffffffffffffff8111156114fe576114fd6110f9565b5b61150a88828901611435565b9350935050606061151d88828901611394565b9150509295509295909350565b611533816111d7565b82525050565b5f60208201905061154c5f83018461152a565b92915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52603260045260245ffd5b5f82825260208201905092915050565b7f446561646c696e652070617373656400000000000000000000000000000000005f82015250565b5f6115c3600f8361157f565b91506115ce8261158f565b602082019050919050565b5f6020820190508181035f8301526115f0816115b7565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61162e8261134d565b91506116398361134d565b92508282026116478161134d565b9150828204841483151761165e5761165d6115f7565b5b5092915050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601260045260245ffd5b5f61169c8261134d565b91506116a78361134d565b9250826116b7576116b6611665565b5b828204905092915050565b7f526f7574696e672066656520746f6f20686967680000000000000000000000005f82015250565b5f6116f660148361157f565b9150611701826116c2565b602082019050919050565b5f6020820190508181035f830152611723816116ea565b9050919050565b7f526f7574696e672066656520746f6f206c6f77000000000000000000000000005f82015250565b5f61175e60138361157f565b91506117698261172a565b602082019050919050565b5f6020820190508181035f83015261178b81611752565b9050919050565b5f602082840312156117a7576117a66110f5565b5b5f6117b4848285016112df565b91505092915050565b7f4e6567617469766520736c697070616765206c696d69740000000000000000005f82015250565b5f6117f160178361157f565b91506117fc826117bd565b602082019050919050565b5f6020820190508181035f83015261181e816117e5565b9050919050565b7f536c697070616765206c696d697420746f6f206c6172676500000000000000005f82015250565b5f61185960188361157f565b915061186482611825565b602082019050919050565b5f6020820190508181035f8301526118868161184d565b9050919050565b5f602082840312156118a2576118a16110f5565b5b5f6118af8482850161120f565b91505092915050565b7f417262697472616765206e6f7420737570706f727465640000000000000000005f82015250565b5f6118ec60178361157f565b91506118f7826118b8565b602082019050919050565b5f6020820190508181035f830152611919816118e0565b9050919050565b7f4e6567617469766520726f7574696e67206665650000000000000000000000005f82015250565b5f61195460148361157f565b915061195f82611920565b602082019050919050565b5f6020820190508181035f83015261198181611948565b9050919050565b7f4e6567617469766520706172746e6572206665650000000000000000000000005f82015250565b5f6119bc60148361157f565b91506119c782611988565b602082019050919050565b5f6020820190508181035f8301526119e9816119b0565b9050919050565b5f6119fa8261134d565b9150611a058361134d565b9250828201905080821115611a1d57611a1c6115f7565b5b92915050565b5f611a2d8261134d565b9150611a388361134d565b9250828203905081811115611a5057611a4f6115f7565b5b92915050565b7f496e76616c696420616d6f756e747300000000000000000000000000000000005f82015250565b5f611a8a600f8361157f565b9150611a9582611a56565b602082019050919050565b5f6020820190508181035f830152611ab781611a7e565b9050919050565b7f496e76616c6964206e617469766520746f6b656e20696e70757420616d6f756e5f8201527f7400000000000000000000000000000000000000000000000000000000000000602082015250565b5f611b1860218361157f565b9150611b2382611abe565b604082019050919050565b5f6020820190508181035f830152611b4581611b0c565b9050919050565b5f8115159050919050565b611b6081611b4c565b8114611b6a575f80fd5b50565b5f81359050611b7b81611b57565b92915050565b5f60208284031215611b9657611b956110f5565b5b5f611ba384828501611b6d565b91505092915050565b5f82825260208201905092915050565b5f819050919050565b611bce816111d7565b8114611bd8575f80fd5b50565b5f81359050611be981611bc5565b92915050565b5f611bfd6020840184611bdb565b905092915050565b611c0e816111d7565b82525050565b5f611c226020840184611394565b905092915050565b611c338161134d565b82525050565b5f80fd5b5f80fd5b5f80fd5b5f8083356001602003843603038112611c6157611c60611c41565b5b83810192508235915060208301925067ffffffffffffffff821115611c8957611c88611c39565b5b600182023603831315611c9f57611c9e611c3d565b5b509250929050565b5f82825260208201905092915050565b828183375f83830152505050565b5f611cd08385611ca7565b9350611cdd838584611cb7565b611ce683611101565b840190509392505050565b5f60608301611d025f840184611bef565b611d0e5f860182611c05565b50611d1c6020840184611c14565b611d296020860182611c2a565b50611d376040840184611c45565b8583036040870152611d4a838284611cc5565b925050508091505092915050565b5f611d638383611cf1565b905092915050565b5f82356001606003833603038112611d8657611d85611c41565b5b82810191505092915050565b5f602082019050919050565b5f611da98385611bac565b935083602084028501611dbb84611bbc565b805f5b87811015611dfe578484038952611dd58284611d6b565b611ddf8582611d58565b9450611dea83611d92565b925060208a01995050600181019050611dbe565b50829750879450505050509392505050565b5f819050919050565b5f611e33611e2e611e29846111b8565b611e10565b6111b8565b9050919050565b5f611e4482611e19565b9050919050565b5f611e5582611e3a565b9050919050565b611e6581611e4b565b82525050565b5f6040820190508181035f830152611e84818587611d9e565b9050611e936020830184611e5c565b949350505050565b7f4e6567617469766520736c69707061676520746f6f206c6172676500000000005f82015250565b5f611ecf601b8361157f565b9150611eda82611e9b565b602082019050919050565b5f6020820190508181035f830152611efc81611ec3565b9050919050565b5f80fd5b5f80fd5b5f80fd5b5f8083356001602003843603038112611f2b57611f2a611f03565b5b80840192508235915067ffffffffffffffff821115611f4d57611f4c611f07565b5b602083019250600182023603831315611f6957611f68611f0b565b5b509250929050565b5f81905092915050565b5f611f868385611f71565b9350611f93838584611cb7565b82840190509392505050565b5f611fab828486611f7b565b91508190509392505050565b5f60e082019050611fca5f83018a611e5c565b611fd76020830189611356565b611fe46040830188611e5c565b611ff16060830187611356565b611ffe6080830186611356565b61200b60a0830185611356565b61201860c0830184611356565b98975050505050505050565b5f815190506120328161137e565b92915050565b5f6020828403121561204d5761204c6110f5565b5b5f61205a84828501612024565b91505092915050565b50565b5f6120715f83611f71565b915061207c82612063565b5f82019050919050565b5f61209082612066565b915081905091905056fea26469706673582212202c7846407edb4c16c2203fff72222ae94af9d7a5047862daf83d3a6cbfe7034064736f6c634300081a0033

Verified Source Code Full Match

Compiler: v0.8.26+commit.8a97fa7a EVM: cancun Optimization: No
GluexRouter.sol 326 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {EthReceiver} from "./utils/EthReceiver.sol";
import {Interaction} from "./base/RouterStructs.sol";
import {IExecutor} from "./interfaces/IExecutor.sol";
import {IERC20} from "./interfaces/IERC20.sol";
import {SafeERC20} from "./lib/SafeERC20.sol";

/**
 * @title GluexRouter
 * @notice A versatile router contract that enables the execution of on-chain interactions using the `IExecutor` interface.
 * @dev This contract provides functionality for routing tokens through various interactions, collecting routing fees,
 *      and enforcing strict slippage and routing rules for optimal security and usability.
 */
contract GluexRouter is EthReceiver {
    using SafeERC20 for IERC20;

    // Errors
    error InsufficientBalance();
    error NativeTransferFailed();
    error OnlyGlueTreasury();
    error ZeroAddress();

    // Events
    /**
     * @notice Emitted when a routing operation is completed.
     * @param unique_pid The unique identifier for the partner.
     * @param inputToken The ERC20 token used as input.
     * @param inputAmount The amount of input token used for routing.
     * @param outputToken The ERC20 token received as output.
     * @param outputAmount The expected output amount from the route.
     * @param partnerFee The fee charged for the partner.
     * @param routingFee The fee charged for the routing operation.
     * @param finalOutputAmount The actual output amount received after routing.
     */
    event Routed(
        bytes indexed unique_pid,
        IERC20 inputToken,
        uint256 inputAmount,
        IERC20 outputToken,
        uint256 outputAmount,
        uint256 partnerFee,
        uint256 routingFee,
        uint256 finalOutputAmount
    );

    // DataTypes
    /**
     * @dev A generic structure defining the parameters for a route.
     */
    struct RouteDescription {
        IERC20 inputToken; // Token used as input for the route
        IERC20 outputToken; // Token received as output from the route
        address payable inputReceiver; // Address to receive the input token
        address payable outputReceiver; // Address to receive the output token
        uint256 inputAmount; // Amount of input token
        uint256 outputAmount; // Original output amount
        uint256 partnerFee; // Fee for the partner
        uint256 routingFee; // Fee for the routing operation
        uint256 effectiveOutputAmount; // Effective output after deducting the routing fee
        uint256 minOutputAmount; // Minimum acceptable output amount
        bool isPermit2; // Whether to use Permit2 for token transfers
        bytes unique_pid; // Unique identifier for the partner
    }

    // Constants
    uint256 public _RAW_CALL_GAS_LIMIT = 5000;
    uint256 public _MAX_FEE = 5; // 5bps (0.05%)
    uint256 public _MIN_FEE = 1; // 1bps (0.01%)
    uint256 public _TOLERANCE = 100;

    // State Variables
    address public immutable _nativeToken; // Address of the native token (e.g., Ether on Ethereum)
    address internal immutable _glueTreasury; // Address of the Glue treasury contract

    /**
     * @dev Initializes the contract with the treasury address and native token address.
     * @param glueTreasury The address of the Glue treasury contract.
     * @param nativeToken The address of the native token.
     */
    constructor(address glueTreasury, address nativeToken) {
        // Ensure the addresses are not zero
        checkZeroAddress(glueTreasury);
        checkZeroAddress(nativeToken);

        _glueTreasury = glueTreasury;
        _nativeToken = nativeToken;
    }

    /**
     * @dev Modifier to restrict access to treasury-only functions.
     */
    modifier onlyTreasury() {
        checkTreasury();
        _;
    }

    /**
     * @notice Verifies the caller is the Glue treasury.
     * @dev Reverts with `OnlyGlueTreasury` if the caller is not the treasury.
     */
    function checkTreasury() internal view {
        if (msg.sender != _glueTreasury) revert OnlyGlueTreasury();
    }

    /**
     * @notice Verifies the given address is not zero.
     * @param addr The address to verify.
     * @dev Reverts with `ZeroAddress` if the address is zero.
     */
    function checkZeroAddress(address addr) internal pure {
        if (addr == address(0)) revert ZeroAddress();
    }

    /**
     * @notice Collects routing fees from specified tokens and transfers them to the given receiver.
     * @param feeTokens An array of ERC20 tokens from which fees will be collected.
     * @param receiver The address where collected fees will be transferred.
     */
    function collectFees(IERC20[] memory feeTokens, address payable receiver)
        external
        onlyTreasury
    {
        // Ensure the receiver address is valid
        checkZeroAddress(receiver);

        // Collect fees for each token
        uint256 len = feeTokens.length;
        for (uint256 i = 0; i < len; ) {
            uint256 feeBalance = uniBalanceOf(feeTokens[i], address(this));
            uniTransfer(feeTokens[i], receiver, feeBalance);

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Executes a route using the specified executor and interactions.
     * @param executor The executor contract that performs the interactions.
     * @param desc The route description containing input, output, and fee details.
     * @param interactions The interactions encoded for execution by the executor.
     * @return finalOutputAmount The final amount of output token received.
     * @dev Ensures strict validation of slippage, routing fees, and input/output parameters.
     */
    function swap(
        IExecutor executor,
        RouteDescription calldata desc,
        Interaction[] calldata interactions,
        uint256 deadline
    ) external payable returns (uint256 finalOutputAmount) {
        // Deadline check
        if (block.timestamp > deadline) revert("Deadline passed");

        // Validate routing fee
        if (desc.routingFee > (desc.outputAmount * _MAX_FEE) / 10000) revert("Routing fee too high");
        if (desc.routingFee < (desc.outputAmount * _MIN_FEE) / 10000) revert("Routing fee too low");

        // Validate non-zero addresses
        checkZeroAddress(desc.inputReceiver);
        checkZeroAddress(desc.outputReceiver);

        // Validate route parameters
        if (desc.minOutputAmount <= 0) revert("Negative slippage limit");
        if (desc.minOutputAmount > desc.outputAmount) revert("Slippage limit too large");
        if (desc.inputToken == desc.outputToken) revert("Arbitrage not supported");
        if (desc.routingFee <= 0) revert("Negative routing fee");
        if (desc.partnerFee < 0) revert("Negative partner fee");
        if (
            desc.outputAmount <
            desc.effectiveOutputAmount +
                desc.partnerFee +
                desc.routingFee -
                _TOLERANCE ||
            desc.outputAmount >
            desc.effectiveOutputAmount +
                desc.partnerFee +
                desc.routingFee +
                _TOLERANCE
        ) revert("Invalid amounts");

        // Handle native token input validation
        if (address(desc.inputToken) == _nativeToken) {
            if (msg.value != desc.inputAmount) revert("Invalid native token input amount");
        } else {
            if (msg.value != 0) revert("Invalid native token input amount");
            desc.inputToken.safeTransferFromUniversal(
                msg.sender,
                desc.inputReceiver,
                desc.inputAmount,
                desc.isPermit2
            );
        }

        // Execute interactions through the executor
        IERC20 outputToken = desc.outputToken;
        uint256 outputBalanceBefore = uniBalanceOf(outputToken, address(this));
        IExecutor(executor).executeRoute{value: msg.value}(
            interactions,
            desc.outputToken
        );
        uint256 outputBalanceAfter = uniBalanceOf(outputToken, address(this));

        // Calculate final output amount
        uint256 routeOutputAmount = outputBalanceAfter - outputBalanceBefore;
        uint256 routingFee = 0;
        if (routeOutputAmount > desc.effectiveOutputAmount + desc.routingFee) {
            finalOutputAmount = routeOutputAmount - desc.routingFee;
            routingFee = desc.routingFee;
        } else if (routeOutputAmount > desc.effectiveOutputAmount) {
            finalOutputAmount = desc.effectiveOutputAmount;
            routingFee = routeOutputAmount - desc.effectiveOutputAmount;
        } else {
            finalOutputAmount = routeOutputAmount;
        }

        // Ensure final output amount meets the minimum required
        if (finalOutputAmount < desc.minOutputAmount) revert("Negative slippage too large");

        // Transfer the final output amount to the receiver
        uniTransfer(outputToken, desc.outputReceiver, finalOutputAmount);

        emit Routed(
            desc.unique_pid,
            desc.inputToken,
            desc.inputAmount,
            desc.outputToken,
            desc.outputAmount,
            desc.partnerFee,
            routingFee,
            finalOutputAmount
        );
    }

    /**
     * @notice Retrieves the balance of a specified token for a given account.
     * @param token The ERC20 token to check.
     * @param account The account address to query the balance for.
     * @return The balance of the token for the account.
     */
    function uniBalanceOf(IERC20 token, address account)
        internal
        view
        returns (uint256)
    {
        if (address(token) == _nativeToken) {
            uint256 contractBalance;
            assembly {
                contractBalance := balance(account)
            }
            return contractBalance;
        } else {
            return token.balanceOf(account);
        }
    }

    /**
     * @notice Transfers a specified amount of a token to a given address.
     * @param token The ERC20 token to transfer.
     * @param to The address to transfer the token to.
     * @param amount The amount of the token to transfer.
     * @dev Handles both native token and ERC20 transfers.
     */
    function uniTransfer(
        IERC20 token,
        address payable to,
        uint256 amount
    ) internal {
        if (amount > 0) {
            if (address(token) == _nativeToken) {
                uint256 contractBalance;
                assembly {
                    contractBalance := selfbalance()
                }
                if (contractBalance < amount) revert InsufficientBalance();
                (bool success, ) = to.call{
                    value: amount,
                    gas: _RAW_CALL_GAS_LIMIT
                }("");
                if (!success) revert NativeTransferFailed();
            } else {
                token.safeTransfer(to, amount);
            }
        } else {
            revert InsufficientBalance();
        }
    }

    /**
     * @notice Updates the gas limit for raw calls made by the contract.
     * @param gasLimit The new gas limit to be set.
     * @dev This function is restricted to the treasury.
     */
    function setGasLimit(uint256 gasLimit) external onlyTreasury {
        _RAW_CALL_GAS_LIMIT = gasLimit;
    }

    /**
     * @notice Updates the maximum fee that can be charged by the contract.
     * @param maxFee The new maximum fee to be set.
     * @dev This function is restricted to the treasury.
     */
    function setMaxFee(uint256 maxFee) external onlyTreasury {
        _MAX_FEE = maxFee;
    }

    /**
     * @notice Updates the minimum fee that can be charged by the contract.
     * @param minFee The new minimum fee to be set.
     * @dev This function is restricted to the treasury.
     */
    function setMinFee(uint256 minFee) external onlyTreasury {
        _MIN_FEE = minFee;
    }

    /**
     * @notice Updates the tolerance.
     * @param tolerance The new tolerance to be set.
     * @dev This function is restricted to the treasury.
     */
    function setTolerance(uint256 tolerance) external onlyTreasury {
        _TOLERANCE = tolerance;
    }
}
RouterStructs.sol 9 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

/// @dev generic smart contract interaction
struct Interaction {
    address target;
    uint256 value;
    bytes callData;
}
IDaiLikePermit.sol 17 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

// File @1inch/solidity-utils/contracts/interfaces/[email protected]

interface IDaiLikePermit {
    function permit(
        address holder,
        address spender,
        uint256 nonce,
        uint256 expiry,
        bool allowed,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;
}
IERC20.sol 88 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(
        address indexed owner,
        address indexed spender,
        uint256 value
    );

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(
        address owner,
        address spender
    ) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 value
    ) external returns (bool);
}
IERC20Permit.sol 92 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

// File @openzeppelin/contracts/token/ERC20/extensions/[email protected]

// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/IERC20Permit.sol)

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 *
 * ==== Security Considerations
 *
 * There are two important considerations concerning the use of `permit`. The first is that a valid permit signature
 * expresses an allowance, and it should not be assumed to convey additional meaning. In particular, it should not be
 * considered as an intention to spend the allowance in any specific way. The second is that because permits have
 * built-in replay protection and can be submitted by anyone, they can be frontrun. A protocol that uses permits should
 * take this into consideration and allow a `permit` call to fail. Combining these two aspects, a pattern that may be
 * generally recommended is:
 *
 * ```solidity
 * function doThingWithPermit(..., uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public {
 *     try token.permit(msg.sender, address(this), value, deadline, v, r, s) {} catch {}
 *     doThing(..., value);
 * }
 *
 * function doThing(..., uint256 value) public {
 *     token.safeTransferFrom(msg.sender, address(this), value);
 *     ...
 * }
 * ```
 *
 * Observe that: 1) `msg.sender` is used as the owner, leaving no ambiguity as to the signer intent, and 2) the use of
 * `try/catch` allows the permit to fail and makes the code tolerant to frontrunning. (See also
 * {SafeERC20-safeTransferFrom}).
 *
 * Additionally, note that smart contract wallets (such as Argent or Safe) are not able to produce permit signatures, so
 * contracts should have entry points that don't rely on permit.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     *
     * CAUTION: See Security Considerations above.
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}
IExecutor.sol 14 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {Interaction} from "../base/RouterStructs.sol";
import {IERC20} from "./IERC20.sol";

/// @title Interface for making arbitrary calls
interface IExecutor {
    /// @notice propagates information about original msg.sender and executes arbitrary data
    function executeRoute(
        Interaction[] calldata interactions,
        IERC20 outputToken
    ) external payable;
}
IPermit2.sol 54 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

// File @1inch/solidity-utils/contracts/interfaces/[email protected]

interface IPermit2 {
    struct PermitDetails {
        // ERC20 token address
        address token;
        // the maximum amount allowed to spend
        uint160 amount;
        // timestamp at which a spender's token allowances become invalid
        uint48 expiration;
        // an incrementing value indexed per owner,token,and spender for each signature
        uint48 nonce;
    }
    /// @notice The permit message signed for a single token allownce
    struct PermitSingle {
        // the permit data for a single token alownce
        PermitDetails details;
        // address permissioned on the allowed tokens
        address spender;
        // deadline on the permit signature
        uint256 sigDeadline;
    }
    /// @notice Packed allowance
    struct PackedAllowance {
        // amount allowed
        uint160 amount;
        // permission expiry
        uint48 expiration;
        // an incrementing value indexed per owner,token,and spender for each signature
        uint48 nonce;
    }

    function transferFrom(
        address user,
        address spender,
        uint160 amount,
        address token
    ) external;

    function permit(
        address owner,
        PermitSingle memory permitSingle,
        bytes calldata signature
    ) external;

    function allowance(
        address user,
        address token,
        address spender
    ) external view returns (PackedAllowance memory);
}
RevertReasonForwarder.sol 30 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

// File @1inch/solidity-utils/contracts/libraries/[email protected]

/// @title Revert reason forwarder.
library RevertReasonForwarder {
    /// @dev Forwards latest externall call revert.
    function reRevert() internal pure {
        // bubble up revert reason from latest external call
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            let ptr := mload(0x40)
            returndatacopy(ptr, 0, returndatasize())
            revert(ptr, returndatasize())
        }
    }

    /// @dev Returns latest external call revert reason.
    function reReason() internal pure returns (bytes memory reason) {
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            reason := mload(0x40)
            let length := returndatasize()
            mstore(reason, length)
            returndatacopy(add(reason, 0x20), 0, length)
            mstore(0x40, add(reason, add(0x20, length)))
        }
    }
}
SafeERC20.sol 412 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

import {IERC20} from "../interfaces/IERC20.sol";
import {IPermit2} from "../interfaces/IPermit2.sol";
import {IERC20Permit} from "../interfaces/IERC20Permit.sol";
import {IDaiLikePermit} from "../interfaces/IDaiLikePermit.sol";
import {RevertReasonForwarder} from "../lib/RevertReasonForwarder.sol";

// File @1inch/solidity-utils/contracts/libraries/[email protected]

/**
 * @title Implements efficient safe methods for ERC20 interface.
 * @notice Compared to the standard ERC20, this implementation offers several enhancements:
 * 1. more gas-efficient, providing significant savings in transaction costs.
 * 2. support for different permit implementations
 * 3. forceApprove functionality
 * 4. support for WETH deposit and withdraw
 */
library SafeERC20 {
    error SafeTransferFailed();
    error SafeTransferFromFailed();
    error ForceApproveFailed();
    error SafeIncreaseAllowanceFailed();
    error SafeDecreaseAllowanceFailed();
    error SafePermitBadLength();
    error Permit2TransferAmountTooHigh();

    // Uniswap Permit2 address
    address private constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
    bytes4 private constant _PERMIT_LENGTH_ERROR = 0x68275857; // SafePermitBadLength.selector
    uint256 private constant _RAW_CALL_GAS_LIMIT = 5000;

    /**
     * @notice Fetches the balance of a specific ERC20 token held by an account.
     * Consumes less gas then regular `ERC20.balanceOf`.
     * @dev Note that the implementation does not perform dirty bits cleaning, so it is the
     * responsibility of the caller to make sure that the higher 96 bits of the `account` parameter are clean.
     * @param token The IERC20 token contract for which the balance will be fetched.
     * @param account The address of the account whose token balance will be fetched.
     * @return tokenBalance The balance of the specified ERC20 token held by the account.
     */
    function safeBalanceOf(
        IERC20 token,
        address account
    ) internal view returns (uint256 tokenBalance) {
        bytes4 selector = IERC20.balanceOf.selector;
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            mstore(0x00, selector)
            mstore(0x04, account)
            let success := staticcall(gas(), token, 0x00, 0x24, 0x00, 0x20)
            tokenBalance := mload(0)

            if or(iszero(success), lt(returndatasize(), 0x20)) {
                let ptr := mload(0x40)
                returndatacopy(ptr, 0, returndatasize())
                revert(ptr, returndatasize())
            }
        }
    }

    /**
     * @notice Attempts to safely transfer tokens from one address to another.
     * @dev If permit2 is true, uses the Permit2 standard; otherwise uses the standard ERC20 transferFrom.
     * Either requires `true` in return data, or requires target to be smart-contract and empty return data.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `from` and `to` parameters are clean.
     * @param token The IERC20 token contract from which the tokens will be transferred.
     * @param from The address from which the tokens will be transferred.
     * @param to The address to which the tokens will be transferred.
     * @param amount The amount of tokens to transfer.
     * @param permit2 If true, uses the Permit2 standard for the transfer; otherwise uses the standard ERC20 transferFrom.
     */
    function safeTransferFromUniversal(
        IERC20 token,
        address from,
        address to,
        uint256 amount,
        bool permit2
    ) internal {
        if (permit2) {
            safeTransferFromPermit2(token, from, to, amount);
        } else {
            safeTransferFrom(token, from, to, amount);
        }
    }

    /**
     * @notice Attempts to safely transfer tokens from one address to another using the ERC20 standard.
     * @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `from` and `to` parameters are clean.
     * @param token The IERC20 token contract from which the tokens will be transferred.
     * @param from The address from which the tokens will be transferred.
     * @param to The address to which the tokens will be transferred.
     * @param amount The amount of tokens to transfer.
     */
    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 amount
    ) internal {
        bytes4 selector = token.transferFrom.selector;
        bool success;
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            let data := mload(0x40)

            mstore(data, selector)
            mstore(add(data, 0x04), from)
            mstore(add(data, 0x24), to)
            mstore(add(data, 0x44), amount)
            success := call(gas(), token, 0, data, 100, 0x0, 0x20)
            if success {
                switch returndatasize()
                case 0 {
                    success := gt(extcodesize(token), 0)
                }
                default {
                    success := and(gt(returndatasize(), 31), eq(mload(0), 1))
                }
            }
        }
        if (!success) revert SafeTransferFromFailed();
    }

    /**
     * @notice Attempts to safely transfer tokens from one address to another using the Permit2 standard.
     * @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `from` and `to` parameters are clean.
     * @param token The IERC20 token contract from which the tokens will be transferred.
     * @param from The address from which the tokens will be transferred.
     * @param to The address to which the tokens will be transferred.
     * @param amount The amount of tokens to transfer.
     */
    function safeTransferFromPermit2(
        IERC20 token,
        address from,
        address to,
        uint256 amount
    ) internal {
        if (amount > type(uint160).max) revert Permit2TransferAmountTooHigh();
        bytes4 selector = IPermit2.transferFrom.selector;
        bool success;
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            let data := mload(0x40)

            mstore(data, selector)
            mstore(add(data, 0x04), from)
            mstore(add(data, 0x24), to)
            mstore(add(data, 0x44), amount)
            mstore(add(data, 0x64), token)
            success := call(gas(), _PERMIT2, 0, data, 0x84, 0x0, 0x0)
            if success {
                success := gt(extcodesize(_PERMIT2), 0)
            }
        }
        if (!success) revert SafeTransferFromFailed();
    }

    /**
     * @notice Attempts to safely transfer tokens to another address.
     * @dev Either requires `true` in return data, or requires target to be smart-contract and empty return data.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `to` parameter are clean.
     * @param token The IERC20 token contract from which the tokens will be transferred.
     * @param to The address to which the tokens will be transferred.
     * @param value The amount of tokens to transfer.
     */
    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        if (!_makeCall(token, token.transfer.selector, to, value)) {
            revert SafeTransferFailed();
        }
    }

    /**
     * @notice Attempts to execute the `permit` function on the provided token with the sender and contract as parameters.
     * Permit type is determined automatically based on permit calldata (IERC20Permit, IDaiLikePermit, and IPermit2).
     * @dev Wraps `tryPermit` function and forwards revert reason if permit fails.
     * @param token The IERC20 token to execute the permit function on.
     * @param permit The permit data to be used in the function call.
     */
    function safePermit(IERC20 token, bytes calldata permit) internal {
        if (!tryPermit(token, msg.sender, address(this), permit))
            RevertReasonForwarder.reRevert();
    }

    /**
     * @notice Attempts to execute the `permit` function on the provided token with custom owner and spender parameters.
     * Permit type is determined automatically based on permit calldata (IERC20Permit, IDaiLikePermit, and IPermit2).
     * @dev Wraps `tryPermit` function and forwards revert reason if permit fails.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `owner` and `spender` parameters are clean.
     * @param token The IERC20 token to execute the permit function on.
     * @param owner The owner of the tokens for which the permit is made.
     * @param spender The spender allowed to spend the tokens by the permit.
     * @param permit The permit data to be used in the function call.
     */
    function safePermit(
        IERC20 token,
        address owner,
        address spender,
        bytes calldata permit
    ) internal {
        if (!tryPermit(token, owner, spender, permit))
            RevertReasonForwarder.reRevert();
    }

    /**
     * @notice Attempts to execute the `permit` function on the provided token with the sender and contract as parameters.
     * @dev Invokes `tryPermit` with sender as owner and contract as spender.
     * @param token The IERC20 token to execute the permit function on.
     * @param permit The permit data to be used in the function call.
     * @return success Returns true if the permit function was successfully executed, false otherwise.
     */
    function tryPermit(
        IERC20 token,
        bytes calldata permit
    ) internal returns (bool success) {
        return tryPermit(token, msg.sender, address(this), permit);
    }

    /**
     * @notice The function attempts to call the permit function on a given ERC20 token.
     * @dev The function is designed to support a variety of permit functions, namely: IERC20Permit, IDaiLikePermit, and IPermit2.
     * It accommodates both Compact and Full formats of these permit types.
     * Please note, it is expected that the `expiration` parameter for the compact Permit2 and the `deadline` parameter
     * for the compact Permit are to be incremented by one before invoking this function. This approach is motivated by
     * gas efficiency considerations; as the unlimited expiration period is likely to be the most common scenario, and
     * zeros are cheaper to pass in terms of gas cost. Thus, callers should increment the expiration or deadline by one
     * before invocation for optimized performance.
     * Note that the implementation does not perform dirty bits cleaning, so it is the responsibility of
     * the caller to make sure that the higher 96 bits of the `owner` and `spender` parameters are clean.
     * @param token The address of the ERC20 token on which to call the permit function.
     * @param owner The owner of the tokens. This address should have signed the off-chain permit.
     * @param spender The address which will be approved for transfer of tokens.
     * @param permit The off-chain permit data, containing different fields depending on the type of permit function.
     * @return success A boolean indicating whether the permit call was successful.
     */
    function tryPermit(
        IERC20 token,
        address owner,
        address spender,
        bytes calldata permit
    ) internal returns (bool success) {
        // load function selectors for different permit standards
        bytes4 permitSelector = IERC20Permit.permit.selector;
        bytes4 daiPermitSelector = IDaiLikePermit.permit.selector;
        bytes4 permit2Selector = IPermit2.permit.selector;
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            let ptr := mload(0x40)

            // Switch case for different permit lengths, indicating different permit standards
            switch permit.length
            // Compact IERC20Permit
            case 100 {
                mstore(ptr, permitSelector) // store selector
                mstore(add(ptr, 0x04), owner) // store owner
                mstore(add(ptr, 0x24), spender) // store spender

                // Compact IERC20Permit.permit(uint256 value, uint32 deadline, uint256 r, uint256 vs)
                {
                    // stack too deep
                    let deadline := shr(
                        224,
                        calldataload(add(permit.offset, 0x20))
                    ) // loads permit.offset 0x20..0x23
                    let vs := calldataload(add(permit.offset, 0x44)) // loads permit.offset 0x44..0x63

                    calldatacopy(add(ptr, 0x44), permit.offset, 0x20) // store value     = copy permit.offset 0x00..0x19
                    mstore(add(ptr, 0x64), sub(deadline, 1)) // store deadline  = deadline - 1
                    mstore(add(ptr, 0x84), add(27, shr(255, vs))) // store v         = most significant bit of vs + 27 (27 or 28)
                    calldatacopy(add(ptr, 0xa4), add(permit.offset, 0x24), 0x20) // store r         = copy permit.offset 0x24..0x43
                    mstore(add(ptr, 0xc4), shr(1, shl(1, vs))) // store s         = vs without most significant bit
                }
                // IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
                success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
            }
            // Compact IDaiLikePermit
            case 72 {
                mstore(ptr, daiPermitSelector) // store selector
                mstore(add(ptr, 0x04), owner) // store owner
                mstore(add(ptr, 0x24), spender) // store spender

                // Compact IDaiLikePermit.permit(uint32 nonce, uint32 expiry, uint256 r, uint256 vs)
                {
                    // stack too deep
                    let expiry := shr(
                        224,
                        calldataload(add(permit.offset, 0x04))
                    ) // loads permit.offset 0x04..0x07
                    let vs := calldataload(add(permit.offset, 0x28)) // loads permit.offset 0x28..0x47

                    mstore(
                        add(ptr, 0x44),
                        shr(224, calldataload(permit.offset))
                    ) // store nonce   = copy permit.offset 0x00..0x03
                    mstore(add(ptr, 0x64), sub(expiry, 1)) // store expiry  = expiry - 1
                    mstore(add(ptr, 0x84), true) // store allowed = true
                    mstore(add(ptr, 0xa4), add(27, shr(255, vs))) // store v       = most significant bit of vs + 27 (27 or 28)
                    calldatacopy(add(ptr, 0xc4), add(permit.offset, 0x08), 0x20) // store r       = copy permit.offset 0x08..0x27
                    mstore(add(ptr, 0xe4), shr(1, shl(1, vs))) // store s       = vs without most significant bit
                }
                // IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
                success := call(gas(), token, 0, ptr, 0x104, 0, 0)
            }
            // IERC20Permit
            case 224 {
                mstore(ptr, permitSelector)
                calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
                // IERC20Permit.permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
                success := call(gas(), token, 0, ptr, 0xe4, 0, 0)
            }
            // IDaiLikePermit
            case 256 {
                mstore(ptr, daiPermitSelector)
                calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
                // IDaiLikePermit.permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s)
                success := call(gas(), token, 0, ptr, 0x104, 0, 0)
            }
            // Compact IPermit2
            case 96 {
                // Compact IPermit2.permit(uint160 amount, uint32 expiration, uint32 nonce, uint32 sigDeadline, uint256 r, uint256 vs)
                mstore(ptr, permit2Selector) // store selector
                mstore(add(ptr, 0x04), owner) // store owner
                mstore(add(ptr, 0x24), token) // store token

                calldatacopy(add(ptr, 0x50), permit.offset, 0x14) // store amount = copy permit.offset 0x00..0x13
                // and(0xffffffffffff, ...) - conversion to uint48
                mstore(
                    add(ptr, 0x64),
                    and(
                        0xffffffffffff,
                        sub(shr(224, calldataload(add(permit.offset, 0x14))), 1)
                    )
                ) // store expiration = ((permit.offset 0x14..0x17 - 1) & 0xffffffffffff)
                mstore(
                    add(ptr, 0x84),
                    shr(224, calldataload(add(permit.offset, 0x18)))
                ) // store nonce = copy permit.offset 0x18..0x1b
                mstore(add(ptr, 0xa4), spender) // store spender
                // and(0xffffffffffff, ...) - conversion to uint48
                mstore(
                    add(ptr, 0xc4),
                    and(
                        0xffffffffffff,
                        sub(shr(224, calldataload(add(permit.offset, 0x1c))), 1)
                    )
                ) // store sigDeadline = ((permit.offset 0x1c..0x1f - 1) & 0xffffffffffff)
                mstore(add(ptr, 0xe4), 0x100) // store offset = 256
                mstore(add(ptr, 0x104), 0x40) // store length = 64
                calldatacopy(add(ptr, 0x124), add(permit.offset, 0x20), 0x20) // store r      = copy permit.offset 0x20..0x3f
                calldatacopy(add(ptr, 0x144), add(permit.offset, 0x40), 0x20) // store vs     = copy permit.offset 0x40..0x5f
                // IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
                success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
            }
            // IPermit2
            case 352 {
                mstore(ptr, permit2Selector)
                calldatacopy(add(ptr, 0x04), permit.offset, permit.length) // copy permit calldata
                // IPermit2.permit(address owner, PermitSingle calldata permitSingle, bytes calldata signature)
                success := call(gas(), _PERMIT2, 0, ptr, 0x164, 0, 0)
            }
            // Unknown
            default {
                mstore(ptr, _PERMIT_LENGTH_ERROR)
                revert(ptr, 4)
            }
        }
    }

    /**
     * @dev Executes a low level call to a token contract, making it resistant to reversion and erroneous boolean returns.
     * @param token The IERC20 token contract on which the call will be made.
     * @param selector The function signature that is to be called on the token contract.
     * @param to The address to which the token amount will be transferred.
     * @param amount The token amount to be transferred.
     * @return success A boolean indicating if the call was successful. Returns 'true' on success and 'false' on failure.
     * In case of success but no returned data, validates that the contract code exists.
     * In case of returned data, ensures that it's a boolean `true`.
     */
    function _makeCall(
        IERC20 token,
        bytes4 selector,
        address to,
        uint256 amount
    ) private returns (bool success) {
        assembly ("memory-safe") {
            // solhint-disable-line no-inline-assembly
            let data := mload(0x40)

            mstore(data, selector)
            mstore(add(data, 0x04), to)
            mstore(add(data, 0x24), amount)
            success := call(gas(), token, 0, data, 0x44, 0x0, 0x20)
            if success {
                switch returndatasize()
                case 0 {
                    success := gt(extcodesize(token), 0)
                }
                default {
                    success := and(gt(returndatasize(), 31), eq(mload(0), 1))
                }
            }
        }
    }
}
EthReceiver.sol 15 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

abstract contract EthReceiver {
    error EthDepositRejected();

    receive() external payable {
        _receive();
    }

    function _receive() internal virtual {
        // solhint-disable-next-line avoid-tx-origin
        if (msg.sender == tx.origin) revert EthDepositRejected();
    }
}

Read Contract

_MAX_FEE 0xdeab3ebd → uint256
_MIN_FEE 0xaf48bcb9 → uint256
_RAW_CALL_GAS_LIMIT 0x249bf7e5 → uint256
_TOLERANCE 0x827f6e5c → uint256
_nativeToken 0xd81a0553 → address

Write Contract 6 functions

These functions modify contract state and require a wallet transaction to execute.

collectFees 0x06af20da
address[] feeTokens
address receiver
setGasLimit 0xee7d72b4
uint256 gasLimit
setMaxFee 0x710e5d2f
uint256 maxFee
setMinFee 0x31ac9920
uint256 minFee
setTolerance 0x3c5fd982
uint256 tolerance
swap 0xd92a1599
address executor
tuple desc
tuple[] interactions
uint256 deadline
returns: uint256

Recent Transactions

No transactions found for this address