Skip to content
Verified contractExactMIT

SellAndSplit

Address0xa060f4495edd8f9da6d98ad46e01ff9cdf07beea
Balance

0 ETH

 

Transactions

74

 

Compiler

solc 0.8.28

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.28+commit.7893614a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,444,235
Creation transaction
0xb3c120806d…3aab7b2698

src/SellAndSplit.sol

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

/// Sells a token for ETH in one transaction and splits the ETH:
/// the service fee goes to feeRecipient, the rest goes to the seller.
/// The seller approves this contract for the token, then calls sellV2 or sellV3.
/// There is no owner and no admin function. Nothing is held between calls.

interface IERC20Min {
    function balanceOf(address) external view returns (uint256);
}

interface IWETH {
    function withdraw(uint256) external;
}

interface IUniswapV2Router {
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;
}

interface ISwapRouter02 {
    struct ExactInputParams {
        bytes path;
        address recipient;
        uint256 amountIn;
        uint256 amountOutMinimum;
    }

    function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
}

contract SellAndSplit {
    uint16 public constant MIN_FEE_BPS = 250; // 2.5 percent
    uint16 public constant MAX_FEE_BPS = 450; // 4.5 percent

    address public immutable feeRecipient;
    address public immutable weth;
    address public immutable v2RouterA; // Uniswap V2 router
    address public immutable v2RouterB; // SushiSwap router
    address public immutable v3Router; // Uniswap SwapRouter02

    uint256 private locked = 1;

    event Sold(address indexed user, address indexed token, uint256 amountIn, uint256 ethOut, uint256 fee);

    error ZeroAddress();
    error FeeOutOfRange();
    error Expired();
    error RouterNotAllowed();
    error NothingReceived();
    error TooLittleEth();
    error TokenCallFailed();
    error EthSendFailed();
    error Reentered();
    error EthNotAccepted();
    error BadPath();

    modifier nonReentrant() {
        if (locked != 1) revert Reentered();
        locked = 2;
        _;
        locked = 1;
    }

    constructor(address _feeRecipient, address _weth, address _v2RouterA, address _v2RouterB, address _v3Router) {
        if (
            _feeRecipient == address(0) || _weth == address(0) || _v2RouterA == address(0)
                || _v2RouterB == address(0) || _v3Router == address(0)
        ) revert ZeroAddress();
        feeRecipient = _feeRecipient;
        weth = _weth;
        v2RouterA = _v2RouterA;
        v2RouterB = _v2RouterB;
        v3Router = _v3Router;
    }

    /// ETH is only accepted from WETH (unwrapping) and from the allowed routers (swap payouts).
    receive() external payable {
        if (msg.sender != weth && msg.sender != v2RouterA && msg.sender != v2RouterB && msg.sender != v3Router) {
            revert EthNotAccepted();
        }
    }

    /// Sell through a Uniswap V2 style router. The path starts at the token and ends at WETH
    /// (2 to 4 addresses, so it may go through one or two tokens in between).
    /// Works with tokens that take a cut on transfer.
    function sellV2(
        address token,
        uint256 amountIn,
        uint256 minUserEth,
        uint16 feeBps,
        address v2Router,
        address[] calldata path,
        uint256 deadline
    ) external nonReentrant {
        _checkCommon(feeBps, deadline);
        if (v2Router != v2RouterA && v2Router != v2RouterB) revert RouterNotAllowed();
        if (path.length < 2 || path.length > 4 || path[0] != token || path[path.length - 1] != weth) revert BadPath();

        uint256 received = _pullToken(token, amountIn);

        uint256 ethBefore = address(this).balance;
        _approve(token, v2Router, received);
        IUniswapV2Router(v2Router).swapExactTokensForETHSupportingFeeOnTransferTokens(
            received, 0, path, address(this), deadline
        );
        _approve(token, v2Router, 0);
        uint256 ethOut = address(this).balance - ethBefore;

        _split(token, received, ethOut, minUserEth, feeBps);
    }

    /// Sell through Uniswap V3 SwapRouter02, then unwrap to ETH. `path` is the router's packed path:
    /// token, pool fee, token, ... It starts at the token and ends at WETH (one to three pools).
    function sellV3(
        address token,
        bytes calldata path,
        uint256 amountIn,
        uint256 minUserEth,
        uint16 feeBps,
        uint256 deadline
    ) external nonReentrant {
        _checkCommon(feeBps, deadline);
        uint256 n = path.length;
        if (
            n < 43 || n > 89 || (n - 20) % 23 != 0 || address(bytes20(path[0:20])) != token
                || address(bytes20(path[n - 20:n])) != weth
        ) revert BadPath();

        uint256 received = _pullToken(token, amountIn);

        uint256 wethBefore = IERC20Min(weth).balanceOf(address(this));
        _approve(token, v3Router, received);
        ISwapRouter02(v3Router).exactInput(
            ISwapRouter02.ExactInputParams({
                path: path,
                recipient: address(this),
                amountIn: received,
                amountOutMinimum: 0 // the real minimum is checked on the seller's share below
            })
        );
        _approve(token, v3Router, 0);
        uint256 wethOut = IERC20Min(weth).balanceOf(address(this)) - wethBefore;

        uint256 ethBefore = address(this).balance;
        IWETH(weth).withdraw(wethOut);
        uint256 ethOut = address(this).balance - ethBefore;

        _split(token, received, ethOut, minUserEth, feeBps);
    }

    // ---------- internal helpers ----------

    function _checkCommon(uint16 feeBps, uint256 deadline) private view {
        if (feeBps < MIN_FEE_BPS || feeBps > MAX_FEE_BPS) revert FeeOutOfRange();
        if (block.timestamp > deadline) revert Expired();
    }

    /// Pull the seller's tokens and return how many actually arrived.
    function _pullToken(address token, uint256 amountIn) private returns (uint256 received) {
        uint256 before = IERC20Min(token).balanceOf(address(this));
        _call(token, abi.encodeWithSelector(0x23b872dd, msg.sender, address(this), amountIn)); // transferFrom
        received = IERC20Min(token).balanceOf(address(this)) - before;
        if (received == 0) revert NothingReceived();
    }

    function _approve(address token, address spender, uint256 amount) private {
        _call(token, abi.encodeWithSelector(0x095ea7b3, spender, amount)); // approve
    }

    /// Calls a token and accepts either no return data (USDT style) or a true bool.
    /// If the token reverts, its own reason is passed on.
    function _call(address token, bytes memory data) private {
        if (token.code.length == 0) revert TokenCallFailed();
        (bool ok, bytes memory ret) = token.call(data);
        if (!ok) {
            // Pass the token's own revert reason through, so the caller can see why it refused.
            if (ret.length == 0) revert TokenCallFailed();
            assembly {
                revert(add(ret, 32), mload(ret))
            }
        }
        if (ret.length != 0 && !abi.decode(ret, (bool))) revert TokenCallFailed();
    }

    /// Pay the fee and the seller's share. All ETH from this sale leaves in this call.
    function _split(address token, uint256 received, uint256 ethOut, uint256 minUserEth, uint16 feeBps) private {
        uint256 fee = ethOut * feeBps / 10_000;
        uint256 userEth = ethOut - fee;
        if (userEth < minUserEth) revert TooLittleEth();

        _sendEth(feeRecipient, fee);
        _sendEth(msg.sender, userEth);

        emit Sold(msg.sender, token, received, ethOut, fee);
    }

    function _sendEth(address to, uint256 amount) private {
        if (amount == 0) return;
        (bool ok,) = to.call{value: amount}("");
        if (!ok) revert EthSendFailed();
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun"
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.