MonkleRouterAdapter02

Description:

Decentralized Finance (DeFi) protocol contract providing Liquidity functionality.

Blockchain: Ethereum

Source Code: View Code On The Blockchain

Solidity Source Code:

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

interface IERC20 {
    function transferFrom(address from, address to, uint amount) external returns (bool);
    function approve(address spender, uint amount) external returns (bool);
}
interface IMonkleRouter02 {
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bytes32 pairSalt,
        bytes calldata pairBytecode
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
}

contract MonkleRouterAdapter02 {
    address public monkleRouter;
    bytes32 public pairSalt;
    bytes   public pairBytecode; // can be empty; router/factory may ignore it
    address public owner;

    event OwnershipTransferred(address indexed prev, address indexed next);
    event MonkleRouterUpdated(address indexed router);
    event PairTemplateUpdated(bytes32 salt, bytes bytecode);

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

    constructor(address _monkleRouter, bytes32 _pairSalt, bytes memory _pairBytecode) {
        require(_monkleRouter != address(0), "router=0");
        owner = msg.sender;
        monkleRouter = _monkleRouter;
        pairSalt = _pairSalt;
        pairBytecode = _pairBytecode; // may be 0x
        emit OwnershipTransferred(address(0), msg.sender);
        emit MonkleRouterUpdated(_monkleRouter);
        emit PairTemplateUpdated(_pairSalt, _pairBytecode);
    }

    function transferOwnership(address n) external onlyOwner {
        require(n != address(0), "zero");
        emit OwnershipTransferred(owner, n);
        owner = n;
    }

    function setMonkleRouter(address r) external onlyOwner {
        require(r != address(0), "zero");
        monkleRouter = r;
        emit MonkleRouterUpdated(r);
    }

    function setPairTemplate(bytes32 salt, bytes calldata bytecode) external onlyOwner {
        pairSalt = salt;
        pairBytecode = bytecode; // can be empty 0x
        emit PairTemplateUpdated(salt, bytecode);
    }

    /// @notice UniswapV2-compatible entry. Adapter PULLS tokens from msg.sender,
    /// approves router to use them, then forwards the call.
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity) {
        // 1) Pull tokens from the caller into the adapter
        require(IERC20(token).transferFrom(msg.sender, address(this), amountTokenDesired), "pull fail");

        // 2) Approve the router to take them from the adapter
        require(IERC20(token).approve(monkleRouter, amountTokenDesired), "approve fail");

        // 3) Forward to Router (router will transferFrom(adapter -> pair))
        (bool ok, bytes memory data) = monkleRouter.call{value: msg.value}(
            abi.encodeWithSelector(
                IMonkleRouter02.addLiquidityETH.selector,
                token,
                amountTokenDesired,
                amountTokenMin,
                amountETHMin,
                to,
                deadline,
                pairSalt,
                pairBytecode
            )
        );
        require(ok, "router call failed");

        (amountToken, amountETH, liquidity) = abi.decode(data, (uint, uint, uint));
    }

    receive() external payable {}
}

Tags:
DeFi, Liquidity|addr:0x97e1397789735bb284aff4f74c4bd62a93e26e9b|verified:true|block:23471168|tx:0x8e6a8d54f26e4adf36345feb8439cbb5b8ea6f8d7714e0fab8cdd620bb615e01|first_check:1759241464

Submitted on: 2025-09-30 16:11:05

Comments

Log in to comment.

No comments yet.