Skip to content
Verified contractExactMIT

ProxyBuyer

Address0x77d0efcedd879df398be72e4344ae6c551e75302
Balance

0 ETH

 

Transactions

57

 

Compiler

solc 0.8.8

 

Same code

1 more contract

 

Details

Compiler
solc 0.8.8+commit.dddeac2f
Language
solidity
Optimization
Yes, 200 runs
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
22,755,285
Creation transaction
0xd511e6c445…c63469494b

contracts/ProxyBuyer.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;

/**
 * @title ProxyBuyer (stealth micro-router)
 *
 * A minimal router that performs a single-hop swap (WETH -> TOKEN) directly
 * through a Uniswap-V2 pair, but exposes only one external entry-point – the
 * fallback function with a "zero" selector (0x00000000).  Bundle checkers that
 * rely on popular router selectors therefore fail to classify the call as a
 * swap-buy.
 *
 * Calldata layout after the first 4 zero bytes (total length = 0x64 = 100 bytes):
 *   0x04   ‑ 0x23   address  token         – the token address (paired with WETH)
 *   0x24   ‑ 0x43   address  recipient     – where bought tokens will be sent
 *   0x44   ‑ 0x63   uint256  minAmountOut  – slippage guard (tokens)
 *
 * The ETH sent with the tx is wrapped into WETH and used as the input amount.
 */

interface IWETH {
    function deposit() external payable;
    function withdraw(uint256) external;
    function transfer(address to, uint256 value) external returns (bool);
}

interface IUniswapV2Pair {
    function swap(
        uint256 amount0Out,
        uint256 amount1Out,
        address to,
        bytes calldata data
    ) external;

    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112, uint112, uint32);
}

/// Minimal interface for common V2 routers (Uniswap / PancakeSwap)
interface IDexRouter {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);
}

/// Minimal interface for Uniswap / PancakeSwap V2 factories
interface IUniswapV2Factory {
    function getPair(address tokenA, address tokenB) external view returns (address pair);
}

contract ProxyBuyer {
    address public owner;
    IWETH  public immutable WETH;
    address public immutable uniswapV2Router;
    address public immutable uniswapV2Factory;

    modifier onlyOwner() {
        require(msg.sender == owner, "PB:not owner");
        _;
    }

    constructor() {
        owner = msg.sender;

        // Determine router address based on current chain ID (same mapping as tokens)
        address _router = _getDEXRouterAddress();
        uniswapV2Router   = _router;
        uniswapV2Factory  = IDexRouter(_router).factory();

        WETH = IWETH(IDexRouter(_router).WETH());
    }

    /* ------------- admin helpers ------------- */

    function transferOwnership(address newOwner) external onlyOwner {
        require(newOwner != address(0), "PB:zero addr");
        owner = newOwner;
    }

    /**
     * Allow the owner to rescue accidentally sent tokens or ETH.
     */
    function rescue(address token, uint256 amount) external onlyOwner {
        if (token == address(0)) {
            (bool ok, ) = owner.call{value: amount}("");
            require(ok, "PB:ETH xfer fail");
        } else {
            (bool ok) = IERC20(token).transfer(owner, amount);
            require(ok, "PB:erc20 xfer fail");
        }
    }

    receive() external payable {}

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

    function _getDEXRouterAddress() internal view returns (address) {
        if (block.chainid == 1) {
            return 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D; // Uniswap V2 (Ethereum)
        } else if (block.chainid == 56) {
            return 0x10ED43C718714eb63d5aA57B78B54704E256024E; // PancakeSwap V2 (BSC)
        } else {
            revert("Chain ID not supported");
        }
    }

    // Calculate the expected output amount for the swap using the pair reserves
    function _estimateAmountOut(address pair, bool wethIs0, uint amountIn)
        internal view returns (uint amountOut)
    {
        (uint112 r0, uint112 r1, ) = IUniswapV2Pair(pair).getReserves();
        uint reserveIn  = wethIs0 ? uint(r0) : uint(r1);
        uint reserveOut = wethIs0 ? uint(r1) : uint(r0);
        require(reserveIn > 0 && reserveOut > 0, "PB:bad reserves");

        uint amountInWithFee = amountIn * 997;
        uint numerator   = amountInWithFee * reserveOut;
        uint denominator = reserveIn * 1000 + amountInWithFee;
        amountOut = numerator / denominator;
    }

    // Execute the actual swap on the pair (WETH -> token)
    function _swap(address pair, bool wethIs0, uint amountOut, address recipient) internal {
        IUniswapV2Pair(pair).swap(
            wethIs0 ? uint256(0) : amountOut,
            wethIs0 ? amountOut : uint256(0),
            recipient,
            ""
        );
    }

    /* ------------- stealth swap entry ‑ point ------------- */

    fallback() external payable {
        /* ------------------------------------------------------------------
           1) sanity-check selector & calldata length
              (use readable Solidity `require` so we get clear revert strings)
           ---------------------------------------------------------------- */

        // extract the first 4 bytes (function selector)
        bytes4 selector;
        assembly {
            selector := shr(224, calldataload(0))
        }

        require(selector == 0x00000000, "PB: bad selector");
        require(msg.data.length == 0x64, "PB: bad calldata len");

        // 2) decode parameters – expect TOKEN, not pair
        (address tokenOut, address recipient, uint256 minOut) = abi.decode(
            msg.data[4:], (address, address, uint256)
        );

        uint256 amountIn = msg.value;
        require(amountIn > 0, "PB: no ETH");
        require(recipient != address(0), "PB: bad rcpt");

        // sanity
        require(tokenOut != address(0), "PB: token zero");

        // 3) find the TOKEN/WETH pair via the factory
        address pair = IUniswapV2Factory(uniswapV2Factory).getPair(tokenOut, address(WETH));
        require(pair != address(0), "PB:no pair");

        // 4) wrap ETH into WETH
        WETH.deposit{value: amountIn}();

        // 5) verify pair composition & position of WETH
        address t0 = IUniswapV2Pair(pair).token0();
        address t1 = IUniswapV2Pair(pair).token1();
        bool wethIs0 = (t0 == address(WETH));
        require(wethIs0 ? t1 == tokenOut : t0 == tokenOut, "PB:pair mismatch");

        // 6) transfer WETH to pair
        require(WETH.transfer(pair, amountIn), "PB:WETH xfer fail");

        // 7) Estimate output via reserves (for informational slippage check)
        uint amountOutExpected = _estimateAmountOut(pair, wethIs0, amountIn);

        // 8) balance-based guard for fee-on-transfer tokens
        uint balanceBefore = IERC20(tokenOut).balanceOf(recipient);

        // perform the swap (WETH -> token)
        _swap(pair, wethIs0, amountOutExpected, recipient);

        uint received = IERC20(tokenOut).balanceOf(recipient) - balanceBefore;
        require(received >= minOut, "PB: INSUFFICIENT_OUTPUT");
    }
}

interface IERC20 {
    function transfer(address to, uint256 value) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
} 
Compiler settingsShow
{
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  }
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.