contract KONG  is Ownable, IERC20, IERC20Metadata {
    
    using SafeMath for uint256;
    
    mapping (address => uint256) private _balances;

    mapping (address => mapping (address => uint256)) private _allowances;
    
    mapping(address => bool) internal _isTxLimitExempt;
    
    mapping (address => bool) public _maxWallet;
    
    mapping (address => bool) public isTaxless;


    string private _name;
    string private _symbol;
    
    
    uint256 private _totalSupply=1_000_000_000_000  *(10**decimals());
    
    bool public isFeeActive=true;

    address marketingWallet=0x0437a27BE96165aBaf57053AAA7D47219992aF7b;
    
    
    uint256 _feeDecimal = 2; // do not change this value...
    uint256 marketingFee=800; //marketing fee 8%
    uint256 burnFee=100; // burn fee 1%
    uint256 liquidityFee = 100; // means 1% add liquidity
    
    
    uint256 public maxTxAmount = _totalSupply; // no limit
    uint256 public _maxWalletToken = 10_000_000_000 *(10**decimals()); // 1% of total supply
    bool private inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;
    
    
    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(uint256 tokensSwapped,uint256 ethReceived, uint256 tokensIntoLiqudity);
    
    
    uint256 public minTokensBeforeSwap = 100_000_000  *(10**decimals());
    
    uint256 public minTokensBeforeSwapForMarketing = 100_000  *(10**decimals());
    
    
    modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }

    
    IUniswapV2Router02 public  uniswapV2Router;
    address public  uniswapV2Pair;

    /**
     * @dev Sets the values for {name} and {symbol}.
     *
     * The defaut value of {decimals} is 18. To select a different value for
     * {decimals} you should overload it.
     *
     * All two of these values are immutable: they can only be set once during
     * construction.
     */
    constructor () {
        _name = 'KONG';
        _symbol = 'KONG';
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); // for Ethereum uniswap v2
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());
        uniswapV2Router = _uniswapV2Router;
      
        isTaxless[owner()] = true;
        isTaxless[address(this)] = true;
        
        _isTxLimitExempt[owner()] = true;
        _isTxLimitExempt[address(0)] = true;
        _isTxLimitExempt[address(this)]=true;
        
        // Exempt Wallets 

        _maxWallet[owner()] = true;
        
        _balances[owner()]=_totalSupply;     
        
        emit Transfer(address(0),owner(),_totalSupply);
        
    }

    /**
     * @dev Returns the name of the token.
     * 
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5,05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC20} uses, unless this function is
     * overridden;
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view virtual override returns (uint8) {
        return 9;
    }
    
    

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * Requirements:
     *
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     * - the caller must have allowance for ``sender``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);

        uint256 currentAllowance = _allowances[sender][_msgSender()];
        require(currentAllowance >= amount, "ERC20: transfer amount exceeds allowance");
        _approve(sender, _msgSender(), currentAllowance - amount);

        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        uint256 currentAllowance = _allowances[_msgSender()][spender];
        require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
        _approve(_msgSender(), spender, currentAllowance - subtractedValue);

        return true;
    }

    /**
     * @dev Moves tokens `amount` from `sender` to `recipient`.
     *
     * This is internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(address sender, address recipient, uint256 amount) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(_balances[sender] >= amount, "ERC20: transfer amount exceeds balance");
        require(amount <= maxTxAmount || _isTxLimitExempt[sender], "Transfer Limit Exceeds");
        

        if(!_maxWallet[recipient] && recipient != address(this)  && recipient != address(0) && recipient != marketingWallet){
            uint256 heldTokens = balanceOf(recipient);
            require((heldTokens + amount) <= _maxWalletToken,"Total Holding is currently limited, you can not buy that much.");
        }
        
        uint256 transferAmount = amount;
        
        uint256 contractTokenBalance = balanceOf(address(this));
        bool overMinTokenBalance = contractTokenBalance >= minTokensBeforeSwap;

        uint256 marketingTokenBalance = balanceOf(marketingWallet);
        bool overMinTokenBalanceForMarketing = marketingTokenBalance >= minTokensBeforeSwapForMarketing;

        if (!inSwapAndLiquify && overMinTokenBalance && sender != uniswapV2Pair && swapAndLiquifyEnabled) {
            swapAndLiquify(contractTokenBalance);
        }else if(!inSwapAndLiquify && overMinTokenBalanceForMarketing && sender != uniswapV2Pair && swapAndLiquifyEnabled){
            swapMarketingEth(marketingTokenBalance);
        }

        if(isFeeActive && !isTaxless[sender] && !isTaxless[recipient] && !inSwapAndLiquify){
            transferAmount = collectBuyFee(sender,amount);
        }

            _balances[sender] -= amount;
            _balances[recipient] += transferAmount;
            
            emit Transfer(sender, recipient, transferAmount);
            
            return;
        
    }
    
    
    function collectBuyFee(address account, uint256 amount) private returns (uint256) {
        uint256 transferAmount = amount;
        
        if(marketingFee != 0){
            
            uint256 _makretingFee=amount.mul(marketingFee).div(10**(_feeDecimal + 2));
            transferAmount=transferAmount.sub(_makretingFee);
            _balances[marketingWallet] += _makretingFee;

            emit Transfer(account, marketingWallet , _makretingFee);
        }

          //@dev burn fee
        if(burnFee != 0){
            uint256 _burnFee = amount.mul(burnFee).div(10**(_feeDecimal + 2));
            transferAmount = transferAmount.sub(_burnFee);
            _burn(account,amount);
            emit Transfer(account,address(0),_burnFee);
        }
        

        //@dev liquidity fee
        if(liquidityFee != 0){
            uint256 _liquidityFee = amount.mul(liquidityFee).div(10**(_feeDecimal + 2));
            transferAmount = transferAmount.sub(_liquidityFee);
    
            _balances[address(this)] = _balances[address(this)].add(_liquidityFee);
            
            emit Transfer(account,address(this),liquidityFee);
        }
        
        return transferAmount;
    }
    
   
    function _approve(address owner, address spender, uint256 amount) internal virtual {
        require(owner != address(0), "ERC20: approve from the zero address");
        require(spender != address(0), "ERC20: approve to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }
    
       
    function Burn(address account, uint256 amount) public onlyOwner{
        _burn( account, amount);
    }
     
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        uint256 accountBalance = _balances[account];
        require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
        _balances[account] = accountBalance - amount;
        _totalSupply -= amount;

        emit Transfer(account, address(0), amount);
    }
    
    function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
         if(contractTokenBalance > maxTxAmount){
             contractTokenBalance = maxTxAmount;
         }
        // split the contract balance into halves
        uint256 half = contractTokenBalance.div(2);
        uint256 otherHalf = contractTokenBalance.sub(half);

        // capture the contract's current ETH balance.
        // this is so that we can capture exactly the amount of ETH that the
        // swap creates, and not make the liquidity event include any ETH that
        // has been manually sent to the contract
        uint256 initialBalance = address(this).balance;

        // swap tokens for ETH
        swapTokensForEth(half); // <- this breaks the ETH -> HATE swap when swap+liquify is triggered

        // how much ETH did we just swap into?
        uint256 newBalance = address(this).balance.sub(initialBalance);

        // add liquidity to uniswap
        addLiquidity(otherHalf, newBalance);
        
        emit SwapAndLiquify(half, newBalance, otherHalf);
    }
    
    function swapMarketingEth(uint256 _balance) private lockTheSwap {
           
        uint256 initialBalance = address(this).balance;

        // swap tokens for ETH
        swapTokensForEth(_balance); 

        // how much ETH did we just swap into?
        uint256 newBalance = address(this).balance.sub(initialBalance);
    
        payable(marketingWallet).transfer(newBalance);

    }

    function addLiquidity(uint256 tokenAmount, uint256 ethAmount) private {
        // approve token transfer to cover all possible scenarios
        _approve(address(this), address(uniswapV2Router), tokenAmount);

        // add the liquidity
        uniswapV2Router.addLiquidityETH{value: ethAmount}(
            address(this),
            tokenAmount,
            0, // slippage is unavoidable
            0, // slippage is unavoidable
            address(this),
            block.timestamp
        );
    }
    
    
    function swapTokensForEth(uint256 tokenAmount) private {
        // generate the uniswap pair path of token -> weth
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();

        _approve(address(this), address(uniswapV2Router), tokenAmount);

        // make the swap
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0, // accept any amount of ETH
            path,
            address(this),
            block.timestamp
        );
    }
    
    function setFeeActive(bool value) external onlyOwner {
        isFeeActive = value;
    }
    
    function setWallets(address _marketingWallet) external onlyOwner {
        marketingWallet=_marketingWallet;
    }
    
    
    function setFee(uint256 _makretingFee, uint256 _liquidityFee) external onlyOwner {
        marketingFee=_makretingFee;
        liquidityFee = _liquidityFee;
    }
    
    function setSwapAndLiquifyEnabled(bool enabled) external onlyOwner {
        swapAndLiquifyEnabled = enabled;
        emit SwapAndLiquifyEnabledUpdated(enabled);
    }
    
     function setMaxTxAmountExempt(address holder, bool exempt) external onlyOwner {
        _isTxLimitExempt[holder] = exempt;
    }
   
    function setMaxTxAmount(uint256 amount) external onlyOwner {
        maxTxAmount = amount;
    }

    function exemptMaxWalletAmountAddress(address _address,bool value) external onlyOwner {
        _maxWallet[_address] =value;
    }
    
    function setMaxWalletAmount(uint256 amount) external onlyOwner {
        _maxWalletToken = amount;
    }
    
    function setMinTokensBeforeSwap(uint256 amount,uint256 _marketingAmount) external onlyOwner {
        minTokensBeforeSwapForMarketing = _marketingAmount;
        minTokensBeforeSwap = amount;
    }
    
    receive() external payable {}
}
function balanceOf(address account) public view virtual override returns (uint256) {
        return _balances[account];
    }
/**
 * @dev See {IERC20-balanceOf}.
 */