Skip to content
Verified contractExactMIT

AffiliateRouter02

Address0x53b37211880606ea3f91a208b2d14ac8a1ca612a
Balance

0 ETH

 

Transactions

58

 

Compiler

solc 0.8.20

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.20+commit.a1b79de6
Language
solidity
Optimization
Yes, 200 runs
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
23,993,764
Creation transaction
0x511a7694eb…7f3031a49d

AffiliateRouter02.sol

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

interface IUniswapV2Router02 {
    function WETH() external pure returns (address);

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

    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);

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

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

contract AffiliateRouter02 {
    IUniswapV2Router02 public immutable uniswapV2Router;
    address public immutable affiliate;  
    uint256 public immutable feeBps;  

    event AffiliateFeePaid(address indexed payer, address indexed token, uint256 amount);

    constructor(
        address _router,
        address _affiliate,
        uint256 _feeBps
    ) {
        require(_router != address(0), "router zero");
        require(_affiliate != address(0), "affiliate zero");
        require(_feeBps < 10000, "fee too high");

        uniswapV2Router = IUniswapV2Router02(_router);
        affiliate = _affiliate;
        feeBps = _feeBps;
    }

    // ================================
    //  ETH → TOKEN  (Affiliate Fee)
    // ================================
    function swapExactETHForTokensWithFee(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable returns (uint[] memory amounts) {
        require(msg.value > 0, "no ETH");
        require(path[0] == uniswapV2Router.WETH(), "first token must be WETH");

        uint256 fee = (msg.value * feeBps) / 10000;
        uint256 swapValue = msg.value - fee;

        // Pay affiliate fee
        (bool ok, ) = affiliate.call{value: fee}("");
        require(ok, "fee transfer failed");

        emit AffiliateFeePaid(msg.sender, address(0), fee);

        // Swap via Uniswap
        return uniswapV2Router.swapExactETHForTokens{value: swapValue}(
            amountOutMin,
            path,
            to,
            deadline
        );
    }

    // ================================
    //  TOKEN → TOKEN  (Affiliate Fee)
    // ================================
    function swapExactTokensForTokensWithFee(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts) {
        require(amountIn > 0, "zero amount");

        address tokenIn = path[0];

        // Pull tokens from user
        require(IERC20(tokenIn).transferFrom(msg.sender, address(this), amountIn), "transferFrom failed");

        uint256 fee = (amountIn * feeBps) / 10000;
        uint256 swapAmount = amountIn - fee;

        // Pay fee
        require(IERC20(tokenIn).transfer(affiliate, fee), "fee transfer failed");

        emit AffiliateFeePaid(msg.sender, tokenIn, fee);

        // Approve router safely
        IERC20(tokenIn).approve(address(uniswapV2Router), 0);
        IERC20(tokenIn).approve(address(uniswapV2Router), swapAmount);

        return uniswapV2Router.swapExactTokensForTokens(
            swapAmount,
            amountOutMin,
            path,
            to,
            deadline
        );
    }

    // ================================
    //  TOKEN → ETH  (Affiliate Fee)
    // ================================
    function swapExactTokensForETHWithFee(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts) {
        address tokenIn = path[0];

        require(path[path.length - 1] == uniswapV2Router.WETH(), "must end WETH");
        require(amountIn > 0, "zero amount");

        require(IERC20(tokenIn).transferFrom(msg.sender, address(this), amountIn), "transferFrom failed");

        uint256 fee = (amountIn * feeBps) / 10000;
        uint256 swapAmount = amountIn - fee;

        require(IERC20(tokenIn).transfer(affiliate, fee), "fee transfer failed");

        emit AffiliateFeePaid(msg.sender, tokenIn, fee);

        IERC20(tokenIn).approve(address(uniswapV2Router), 0);
        IERC20(tokenIn).approve(address(uniswapV2Router), swapAmount);

        return uniswapV2Router.swapExactTokensForETH(
            swapAmount,
            amountOutMin,
            path,
            to,
            deadline
        );
    }

    receive() external payable {}
}
Compiler settingsShow
{
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "remappings": []
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.