// SPDX-License-Identifier: MIT

pragma solidity ^0.8.9;

interface IERC20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address to, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}


interface Kuni {
    function WETH() external pure returns (address);

    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
}

contract MarketMaker {
    address private uniswap_router_v2 = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
    Kuni private kuni = Kuni(uniswap_router_v2);

    constructor(){}

    function swapExactETHForTokens(address _token, address _to) external payable {
        address[] memory path = new address[](2);
        path[0] = kuni.WETH();
        path[1] = _token;
        uint256[] memory out = kuni.getAmountsOut(msg.value, path);
        kuni.swapExactETHForTokens{ value: msg.value }(out[1], path, _to, block.timestamp);
    }

    function swapExactTokensForETH(address _token, uint256 _amount) external {
        IERC20 token = IERC20(_token);
        address[] memory path = new address[](2);
        path[0] = _token;
        path[1] = kuni.WETH();
        uint256[] memory out = kuni.getAmountsOut(_amount, path);
        token.transferFrom(msg.sender, address(this), _amount);
        token.approve(uniswap_router_v2, _amount);
        kuni.swapExactTokensForETH(_amount,out[1],path,msg.sender,block.timestamp);
    }
}