Skip to content
Verified contractPartialMIT

BatchSwapper

Address0xfe0701f63a755f0dd1551e6563809609b45c56ab
Balance

0 ETH

 

Transactions

51

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
No
EVM version
cancun
License
MIT
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,392,955
Creation transaction
0xb9d87fc910…d8d5be8410

src/BatchSwapper.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

interface IV2Router {
    function swapExactTokensForTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);
}

interface IERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
}

/// @title BatchSwapper
/// @notice Executes N sequential Uniswap-V2-style swaps through a single router
///         in one transaction, with owner-only rescue paths for tokens or ETH
///         that get stuck in the contract.
contract BatchSwapper {
    struct SwapStep {
        address tokenIn;
        address tokenOut;
        uint256 amountIn;
        uint256 amountOutMin;
        uint256 deadline;
    }

    address public owner;
    uint256 private _locked = 1;

    event BatchSwapExecuted(
        address indexed caller,
        address indexed router,
        uint256 steps,
        uint256 totalInFirstToken,
        uint256 totalOutLastToken
    );
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event TokenWithdrawn(address indexed token, address indexed to, uint256 amount);
    event ETHWithdrawn(address indexed to, uint256 amount);

    error EmptyBatch();
    error ZeroAddress();
    error ZeroAmount();
    error ExpiredDeadline(uint256 index);
    error TransferInFailed(uint256 index);
    error TransferOutFailed(uint256 index);
    error Reentrancy();
    error NotOwner();
    error ETHTransferFailed();

    modifier onlyOwner() {
        if (msg.sender != owner) revert NotOwner();
        _;
    }

    modifier nonReentrant() {
        if (_locked != 1) revert Reentrancy();
        _locked = 2;
        _;
        _locked = 1;
    }

    constructor(address initialOwner) {
        if (initialOwner == address(0)) revert ZeroAddress();
        owner = initialOwner;
        emit OwnershipTransferred(address(0), initialOwner);
    }

    receive() external payable {}

    function transferOwnership(address newOwner) external onlyOwner {
        if (newOwner == address(0)) revert ZeroAddress();
        address old = owner;
        owner = newOwner;
        emit OwnershipTransferred(old, newOwner);
    }

    function renounceOwnership() external onlyOwner {
        address old = owner;
        owner = address(0);
        emit OwnershipTransferred(old, address(0));
    }

    function batchSwap(address router, SwapStep[] calldata steps) external nonReentrant {
        if (router == address(0)) revert ZeroAddress();
        uint256 n = steps.length;
        if (n == 0) revert EmptyBatch();

        uint256 sumIn = 0;
        uint256 sumOut = 0;

        for (uint256 i = 0; i < n; ++i) {
            SwapStep calldata s = steps[i];
            if (s.tokenIn == address(0) || s.tokenOut == address(0)) revert ZeroAddress();
            if (s.amountIn == 0) revert ZeroAmount();
            if (s.deadline < block.timestamp) revert ExpiredDeadline(i);

            bool ok = IERC20(s.tokenIn).transferFrom(msg.sender, address(this), s.amountIn);
            if (!ok) revert TransferInFailed(i);

            address[] memory path = new address[](2);
            path[0] = s.tokenIn;
            path[1] = s.tokenOut;

            IERC20(s.tokenIn).approve(router, s.amountIn);

            uint256[] memory amounts = IV2Router(router).swapExactTokensForTokens(
                s.amountIn, s.amountOutMin, path, address(this), s.deadline
            );

            if (i == 0) sumIn = amounts[0];
            if (i == n - 1) sumOut = amounts[amounts.length - 1];
        }

        for (uint256 i = 0; i < n; ++i) {
            address tIn = steps[i].tokenIn;
            address tOut = steps[i].tokenOut;
            uint256 balIn = IERC20(tIn).balanceOf(address(this));
            if (balIn > 0) IERC20(tIn).transfer(msg.sender, balIn);
            uint256 balOut = IERC20(tOut).balanceOf(address(this));
            if (balOut > 0) {
                if (!IERC20(tOut).transfer(msg.sender, balOut)) revert TransferOutFailed(i);
            }
        }

        emit BatchSwapExecuted(msg.sender, router, n, sumIn, sumOut);
    }

    function withdrawToken(address token, address to, uint256 amount) external onlyOwner nonReentrant {
        if (token == address(0) || to == address(0)) revert ZeroAddress();
        uint256 bal = IERC20(token).balanceOf(address(this));
        uint256 amt = (amount == 0 || amount > bal) ? bal : amount;
        if (amt == 0) return;
        if (!IERC20(token).transfer(to, amt)) revert TransferOutFailed(0);
        emit TokenWithdrawn(token, to, amt);
    }

    function withdrawETH(address to, uint256 amount) external onlyOwner nonReentrant {
        if (to == address(0)) revert ZeroAddress();
        uint256 bal = address(this).balance;
        uint256 amt = (amount == 0 || amount > bal) ? bal : amount;
        if (amt == 0) return;
        (bool ok, ) = to.call{value: amt}("");
        if (!ok) revert ETHTransferFailed();
        emit ETHWithdrawn(to, amt);
    }

    function approveToken(address token, address spender, uint256 amount) external {
        if (token == address(0) || spender == address(0)) revert ZeroAddress();
        IERC20(token).approve(spender, amount);
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": false
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@uniswap/v4-core/=lib/v4-core/",
    "@uniswap/v4-periphery/=lib/v4-periphery/",
    "v4-core/=lib/v4-core/",
    "v4-periphery/=lib/v4-periphery/",
    "@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "hookmate/=lib/hookmate/src/",
    "permit2/=lib/v4-periphery/lib/permit2/",
    "solmate/=lib/v4-core/lib/solmate/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-std/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/solmate/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/solmate/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/:ds-test/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/:ds-test/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/v4-core/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-core/lib/solmate/:ds-test/=lib/uniswap-hooks/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/:forge-gas-snapshot/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/:forge-gas-snapshot/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/openzeppelin-contracts/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/permit2/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/lib/openzeppelin-contracts/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/v4-core/lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/forge-std/:ds-test/=lib/v4-periphery/lib/permit2/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/forge-std/:ds-test/=lib/v4-periphery/lib/permit2/lib/forge-std/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/solmate/:ds-test/=lib/v4-periphery/lib/permit2/lib/solmate/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/lib/solmate/:ds-test/=lib/v4-periphery/lib/permit2/lib/solmate/lib/ds-test/src/",
    "lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/v4-periphery/lib/v4-core/lib/solmate/:ds-test/=lib/v4-periphery/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/v4-periphery/lib/v4-core/lib/solmate/:ds-test/=lib/v4-periphery/lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/openzeppelin-contracts/lib/forge-std/:ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "lib/uniswap-hooks/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/openzeppelin-contracts/:erc4626-tests/=lib/uniswap-hooks/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/uniswap-hooks/lib/openzeppelin-contracts/:halmos-cheatcodes/=lib/uniswap-hooks/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "lib/uniswap-hooks/lib/openzeppelin-contracts/:halmos-cheatcodes/=lib/uniswap-hooks/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "lib/uniswap-hooks/lib/v4-core/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/uniswap-hooks/lib/v4-core/:openzeppelin-contracts/=lib/uniswap-hooks/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/v4-core/lib/openzeppelin-contracts/:erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/v4-core/lib/solmate/:ds-test/=lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/v4-core/lib/solmate/:ds-test/=lib/v4-core/lib/solmate/lib/ds-test/src/",
    "lib/v4-periphery/lib/permit2/:forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "lib/v4-periphery/lib/permit2/:forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "lib/v4-periphery/lib/permit2/:openzeppelin-contracts/=lib/v4-periphery/lib/permit2/lib/openzeppelin-contracts/",
    "lib/v4-periphery/lib/permit2/:openzeppelin-contracts/=lib/v4-periphery/lib/permit2/lib/openzeppelin-contracts/",
    "lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/v4-periphery/lib/v4-core/:openzeppelin-contracts/=lib/v4-periphery/lib/v4-core/lib/openzeppelin-contracts/",
    "lib/openzeppelin-contracts/:erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/openzeppelin-contracts/:erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "lib/openzeppelin-contracts/:halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "lib/openzeppelin-contracts/:halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "lib/uniswap-hooks/:openzeppelin-contracts/=lib/uniswap-hooks/lib/openzeppelin-contracts/",
    "lib/uniswap-hooks/:openzeppelin-contracts/=lib/uniswap-hooks/lib/openzeppelin-contracts/",
    "lib/v4-core/:openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/",
    "lib/v4-core/:openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/",
    "@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
    "@openzeppelin/=lib/v4-core/lib/openzeppelin-contracts/",
    "ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/v4-periphery/lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "openzeppelin/=lib/uniswap-hooks/lib/openzeppelin-contracts/contracts/",
    "uniswap-hooks/=lib/uniswap-hooks/src/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.