UniTradeExecutorTool

Description:

Decentralized Finance (DeFi) protocol contract providing Swap, Liquidity, Factory functionality.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

{{
  "language": "Solidity",
  "sources": {
    "tradetool.sol": {
      "content": "// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
    function balanceOf(address) external view returns (uint256);
    function transfer(address,uint256) external returns (bool);
    function approve(address,uint256) external returns (bool);
}

interface IWETH is IERC20 {
    function deposit() external payable;
    function withdraw(uint256) external;
}

interface IUniswapV2Factory {
    function getPair(address tokenA, address tokenB) external view returns (address);
}

interface IUniswapV2Router02 {
    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);

    function swapExactETHForTokens(
        uint amountOutMin, address[] calldata path, address to, uint deadline
    ) external payable returns (uint[] memory amounts);
}

interface IUniswapV2Pair is IERC20 {
    function token0() external view returns (address);
    function token1() external view returns (address);
    function sync() external;
}

interface IUniTradeOrderBook {
    function executeOrder(uint256 orderId) external returns (uint256[] memory);
}

/* =========================================================
   ============== Simplified On-chain Executor ==============
   ========================================================= */
contract UniTradeExecutorTool {
    address public constant FACTORY = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
    address public constant ROUTER  = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    address public constant WETH    = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address public constant PICKLE_WETH_LP = 0xdc98556Ce24f007A5eF6dC1CE96322d65832A819;

    address public immutable owner;
    modifier onlyOwner() {
        require(msg.sender == owner, "not owner");
        _;
    }

    constructor() {
        owner = msg.sender;
    }

    event SeedDone(uint lp1Minted, uint usedWeth, uint usedPickle);
    event Executed(address orderbook, uint orderId);
    event WithdrawETH(uint amount);
    event WithdrawERC20(address token, uint amount);

    /* ========== Main entry ========== */
    function executeAndRefund(address orderbook, uint256 orderId) external payable {
        require(msg.value > 0, "no value");

        _seedAndExecute(orderbook, orderId);

        uint256 left = address(this).balance;
        if (left > 0) {
            (bool ok, ) = msg.sender.call{value: left}("");
            require(ok, "refund failed");
        }
    }

    function _seedAndExecute(address orderbook, uint256 orderId) internal {
        IUniswapV2Router02 router = IUniswapV2Router02(ROUTER);
        address lp2 = IUniswapV2Factory(FACTORY).getPair(WETH, PICKLE_WETH_LP);
        require(lp2 != address(0), "LP2 not found");

        IUniswapV2Pair pair1 = IUniswapV2Pair(PICKLE_WETH_LP);
        address t0 = pair1.token0();
        address PICKLE = (t0 == WETH) ? pair1.token1() : t0;

        // ✅ 1. 手动定义分配比例(例如 50% WETH + 50% 用于买 PICKLE)
        uint256 half = msg.value / 2;
        IWETH(WETH).deposit{value: half}();

        // ✅ 2. swap 一半 ETH -> PICKLE
        address[] memory path = new address[](2);
        path[0] = WETH;
        path[1] = PICKLE;
        router.swapExactETHForTokens{value: msg.value - half}(0, path, address(this), block.timestamp);

        // ✅ 3. addLiquidity (WETH, PICKLE)
        IERC20(WETH).approve(ROUTER, type(uint256).max);
        IERC20(PICKLE).approve(ROUTER, type(uint256).max);
        (uint usedWeth, uint usedPickle, uint lp1Minted) = router.addLiquidity(
            WETH, PICKLE,
            IERC20(WETH).balanceOf(address(this)),
            IERC20(PICKLE).balanceOf(address(this)),
            0, 0,
            address(this),
            block.timestamp
        );
        emit SeedDone(lp1Minted, usedWeth, usedPickle);

        // ✅ 4. seed LP2 pool
        IERC20(WETH).transfer(lp2, 1 gwei); // 少量 WETH 作为 seed
        IERC20(PICKLE_WETH_LP).transfer(lp2, lp1Minted);
        IUniswapV2Pair(lp2).sync();

        // ✅ 5. execute order
        IUniTradeOrderBook(orderbook).executeOrder(orderId);
        emit Executed(orderbook, orderId);
    }

    /* ========== Owner functions ========== */
    function withdrawETH(uint256 amount) external onlyOwner {
        require(amount <= address(this).balance, "insufficient");
        (bool ok, ) = payable(owner).call{value: amount}("");
        require(ok, "withdraw failed");
        emit WithdrawETH(amount);
    }

    function withdrawERC20(address token) external onlyOwner {
        uint bal = IERC20(token).balanceOf(address(this));
        require(bal > 0, "no balance");
        require(IERC20(token).transfer(owner, bal), "transfer failed");
        emit WithdrawERC20(token, bal);
    }

    receive() external payable {}
}
"
    }
  },
  "settings": {
    "optimizer": {
      "enabled": true,
      "runs": 200
    },
    "outputSelection": {
      "*": {
        "*": [
          "evm.bytecode",
          "evm.deployedBytecode",
          "devdoc",
          "userdoc",
          "metadata",
          "abi"
        ]
      }
    },
    "remappings": []
  }
}}

Tags:
DeFi, Swap, Liquidity, Factory|addr:0xa64f9b3404a77d5148b1d6c06ef6e3dfc5cedfb8|verified:true|block:23681469|tx:0x05b82804dba2af4c41fcd164e0d338e6d991681d6730d1f561cac3a239bc9efd|first_check:1761732359

Submitted on: 2025-10-29 11:05:59

Comments

Log in to comment.

No comments yet.