// File: contracts/Koko.sol



// SPDX-License-Identifier: MIT
// https://www.kokonuts.xyz/


pragma solidity ^0.8.0;

/*
⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⣠⡶⠟⠉⠉⠉⠙⠛⠶⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢰⣾⠏⠀⠀⠀⣿⣦⠀⠀⠀⠈⢷⣦⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⡆⣿⠃⠀⢰⣿⠀⠈⠉⠀⠀⠀⠀⠀⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⢀⣿⠇⠀⠀⠘⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⢉⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⢸⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⠴⠚⣛⣉⣉⣉⣉⣙⣿⡆⠀⠀⠀⠀⠀
⠀⠀⣾⠃⠀⠀⠀⠀⠀⠀⠀⢀⣴⢟⣥⣶⣿⣿⣿⣿⣿⣿⣿⠟⢁⣼⣇⠀⠀⠀
⠀⢰⡟⠀⠀⠀⠀⠀⠀⠀⠀⠘⠿⠿⣿⣿⣿⠿⠿⠟⠛⢉⣠⣴⡟⠁⢻⡄⠀⠀
⠀⣸⡇⠀⠀⠀⠀⠀⠀⠀⠈⣿⠶⢦⣤⣤⣤⣤⣶⣶⣿⠿⠟⠛⠀⠀⠸⣧⠀⠀                             
⠀⢹⡇⠀⠀⠀⠀⠀⠀⠀⠀⢿⡀⠀⠀⠀⠈⠉⠉⣿⡇⠀⠀⠀⠀⠀⠀⣿⡀⠀
⠀⢸⣇⠀⠀⠀⠀⠀⠀⠀⠀⢸⣧⠀⠀⠀⠀⠀⠀⣿⣇⠀⠀⠀⠀⠀⠀⣿⠇⠀
⠀⢦⣿⡄⠀⠀⠀⠀⠀⠀⠀⠈⣿⣆⠀⠀⠀⠀⠀⢿⣿⠇⠀⠀⠀⠀⢠⣿⠀⠀
⠀⠈⣿⣷⡄⠀⠀⠀⠀⠀⠀⠀⠈⠿⣧⡀⠀⠀⠀⠀⠁⠀⠀⠀⠀⢀⣾⠻⠀⠀
⠀⠀⠈⠙⠛⢷⣤⣄⣀⣀⣀⣀⣀⣤⣴⠿⠶⣤⣄⣀⣀⣀⣤⣤⡶⠟⠁⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀


 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 amount) external returns (bool);
}

interface IUniswapV2Router01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);

    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, 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);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

contract KokoPresale {
    mapping(address => uint256) public amountPurchased;
    uint256 public immutable maxPerWallet = 1 ether;
    uint256 public immutable presalePrice = 9000000000 * 1e18;
    uint256 public totalPurchased = 0;
    uint256 public presaleMax;

    address public KOKO;
    address public immutable UNISWAP_V2_ROUTER = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;

    address public immutable owner;
    uint256 public immutable startTime;
    uint256 public immutable endTime;

    constructor(uint256 _startTime, address _KOKO, uint256 _max) {
        owner = msg.sender;
        startTime = _startTime;
        endTime = _startTime + 1 days;
        KOKO = _KOKO;
        presaleMax = _max;
    }

    receive() external payable{
        Contribute();
    }

    fallback() external payable{
        Contribute();
    }

    modifier onlyOwner {
        require(owner == msg.sender);
        _;
    }

    function Contribute() public payable {
        require(block.timestamp >= startTime && block.timestamp <= endTime, "Not active");
        require(msg.sender == tx.origin, "No contracts");
        require(msg.value > 0, "Zero amount");
        require(amountPurchased[msg.sender] + msg.value <= maxPerWallet, "Over wallet limit");
        require(totalPurchased + msg.value <= presaleMax, "Amount over limit");
        amountPurchased[msg.sender] += msg.value;
        totalPurchased += msg.value;
    }

    function claim() external {
        require(block.timestamp > endTime + 1 hours, "Not claimable");
        require(amountPurchased[msg.sender] > 0, "No amount claimable");
        uint256 amount = amountPurchased[msg.sender] * presalePrice / 1e18;
        amountPurchased[msg.sender] = 0;
        IERC20(KOKO).transfer(msg.sender, amount);
    }

    function setMax(uint256 _max) external onlyOwner {
        presaleMax = _max;
    }

    function setKoko(address _koko) external onlyOwner{
        KOKO = _koko;
    }

    function addLiquidity() external onlyOwner {
        require(block.timestamp > endTime, "Not finished");
        IERC20(KOKO).approve(UNISWAP_V2_ROUTER, type(uint256).max);
        uint256 totalAmount = address(this).balance;
        (bool success,) = owner.call{value: totalAmount * 20 / 100}("");
        require(success);
        uint256 ethAmount = totalAmount - (totalAmount * 20 / 100);
        uint256 tokenAmount = (ethAmount * presalePrice / 1e18) * 70 / 100;
        IUniswapV2Router02(UNISWAP_V2_ROUTER).addLiquidityETH{value: ethAmount}(
            KOKO,
            tokenAmount,
            1,
            1,
            0x000000000000000000000000000000000000dEaD,
            type(uint256).max
        );
    }

    function Emergency_Withdraw() public onlyOwner{
        (bool callSuccess,) = payable(msg.sender).call{value: address(this).balance}("");
        require(callSuccess, "Send failed");
    }

}