contract pandasharkup is Context, IERC20, Ownable {
    
    using SafeMath for uint256;
    using Address for address;

    mapping (address => uint256) _balances;
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping (address => bool) public exceptfee;
    mapping (address => bool) public exceptlimit;
    mapping (address => bool) public blacklistbot;

    string private _name = "PANDA SHARK UP";
    string private _symbol = "$PSUP";
    uint8 private _decimals = 9;
   
    uint256 private _totalSupply = 1 * 10**9 * 10**9; 
    uint256 public limitamount = 2 * 10**7 * 10**9; 
    uint256 public limitwallet = 3 * 12**10 * 10**9; 
    uint256 public _liquidity = 2;
    uint256 public _marketing = 5;
    uint256 public _buyback = 5;  


    address payable public marketingaddress = payable(0x118e1ed10C36FA732DEa81F2A2618fBa1F957da6); 
    address payable public buybackaddress = payable(0xCf53DC675836D5aa4D1982997C2d6508C9e40E9f); 
    address public immutable zeroaddress = 0x000000000000000000000000000000000000dEaD;
    uint256 public _limitsell = 0;  
    uint256 public _antibot = 0;
    uint256 public _totaltax = 0;   
    uint256 private minbeforeswap = 1000 * 10**9;   

    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;
    
    bool inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = true;
    bool public swapAndLiquifyByLimitOnly = false;
    bool public checkWalletLimit = true;

    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );
    
    event SwapETHForTokens(
        uint256 amountIn,
        address[] path
    );
    
    event SwapTokensForETH(
        uint256 amountIn,
        address[] path
    );
    
    modifier lockTheSwap {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }
    
    constructor () {
        
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D); 

        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        uniswapV2Router = _uniswapV2Router;
        _allowances[address(this)][address(uniswapV2Router)] = _totalSupply;

        exceptfee[owner()] = true;
        exceptfee[address(this)] = true;
        
        _totaltax = _liquidity.add(_marketing).add(_buyback);
        _antibot = _totaltax.add(_limitsell);

        exceptlimit[owner()] = true;
        exceptlimit[address(uniswapV2Pair)] = true;

        _balances[_msgSender()] = _totalSupply;
        emit Transfer(address(0), _msgSender(), _totalSupply);
    }

    function name() public view returns (string memory) {
        return _name;
    }

    function symbol() public view returns (string memory) {
        return _symbol;
    }

    function decimals() public view returns (uint8) {
        return _decimals;
    }

    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }
    

    function allowance(address owner, address spender) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    function Minbeforeswap() public view returns (uint256) {
        return minbeforeswap;
    }

    function approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function _approve(address owner, address spender, uint256 amount) private {
        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 changeexceptfeewallet(address account, bool newValue) public onlyOwner {
        exceptfee[account] = newValue;
    }

    function changetax(uint256 newLiquidityTax, uint256 newMarketingTax, uint256 newRewardsTax, uint256 newExtraFeeOnSell) external onlyOwner() {
        _liquidity = newLiquidityTax;
        _marketing = newMarketingTax;
        _buyback = newRewardsTax;
        _limitsell = newExtraFeeOnSell;

        _totaltax = _liquidity.add(_marketing).add(_buyback);
        _antibot = _totaltax.add(_limitsell);
    }

    function setMaxTxAmount(uint256 maxTxAmount) external onlyOwner() {
        limitamount = maxTxAmount;
    }

    function enableDisableWalletLimit(bool newValue) external onlyOwner {
       checkWalletLimit = newValue;
    }

    function changeexceptlimitaddress(address holder, bool exempt) external onlyOwner {
        exceptlimit[holder] = exempt;
    }

    function setWalletLimit(uint256 newLimit) external onlyOwner {
        limitwallet  = newLimit;
    }

    function setNumTokensBeforeSwap(uint256 newLimit) external onlyOwner() {
        minbeforeswap = newLimit;
    }

    function changemarketingaddress(address newAddress) external onlyOwner() {
        marketingaddress = payable(newAddress);
    }

    function changebuybackaddress(address newAddress) external onlyOwner() {
        buybackaddress = payable(newAddress);
    }


    function transferToAddressETH(address payable recipient, uint256 amount) private {
        recipient.transfer(amount);
    }
    
    function changeRouterVersion(address newRouterAddress) public onlyOwner returns(address newPairAddress) {

        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(newRouterAddress); 

        newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory()).getPair(address(this), _uniswapV2Router.WETH());

        if(newPairAddress == address(0)) //Create If Doesnt exist
        {
            newPairAddress = IUniswapV2Factory(_uniswapV2Router.factory())
                .createPair(address(this), _uniswapV2Router.WETH());
        }

        uniswapV2Pair = newPairAddress; //Set new pair address
        uniswapV2Router = _uniswapV2Router; //Set new router address
    }

     //to recieve ETH from uniswapV2Router when swaping
    receive() external payable {}

    function transfer(address recipient, uint256 amount) public override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    function _transfer(address sender, address recipient, uint256 amount) private returns (bool) {

        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        require(!blacklistbot[sender] && !blacklistbot[recipient], "To/from address is blacklisted!");
        require(amount > 0, "Transfer amount must be greater than zero");
        
        if(inSwapAndLiquify)
        { 
            return _basicTransfer(sender, recipient, amount); 
        }
        else
        {
            if(sender != owner() && recipient != owner()) {
                require(amount <= limitamount, "Transfer amount exceeds the maxTxAmount.");
            }
            
            uint256 contractTokenBalance = balanceOf(address(this));
            bool overMinimumTokenBalance = contractTokenBalance >= minbeforeswap;
            
            if (overMinimumTokenBalance && !inSwapAndLiquify && sender != uniswapV2Pair && swapAndLiquifyEnabled) 
            {
                if(swapAndLiquifyByLimitOnly)
                    contractTokenBalance = minbeforeswap;
                swapAndLiquify(contractTokenBalance);    
            }

            _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");

            uint256 finalAmount = (exceptfee[sender] || exceptfee[recipient]) ? 
                                         amount : takeFee(sender, recipient, amount);

            if(checkWalletLimit && !exceptlimit[recipient])
                require(balanceOf(recipient).add(finalAmount) <= limitwallet);

            _balances[recipient] = _balances[recipient].add(finalAmount);

            emit Transfer(sender, recipient, finalAmount);
            return true;
        }
    }

    function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
        _balances[sender] = _balances[sender].sub(amount, "Insufficient Balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
        return true;
    }

    function swapAndLiquify(uint256 tAmount) private lockTheSwap {
       
        uint256 tokensForLP = tAmount.div(_totaltax).mul(_liquidity).div(2);
        uint256 tokensForSwap = tAmount.sub(tokensForLP);

        swapTokensForEth(tokensForSwap);
        uint256 amountReceived = address(this).balance;

        uint256 totalFee = _totaltax.sub(_liquidity.div(2));
        
        uint256 amountLiquidity = amountReceived.mul(_liquidity).div(totalFee).div(2);
        uint256 amountbuyback = amountReceived.mul(_buyback).div(totalFee);
        uint256 amountMarketing = amountReceived.sub(amountLiquidity).sub(amountbuyback);

        transferToAddressETH(marketingaddress, amountMarketing);
        transferToAddressETH(buybackaddress, amountbuyback);
        addLiquidity(tokensForLP, amountLiquidity);
    }
    
    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), // The contract
            block.timestamp
        );
        
        emit SwapTokensForETH(tokenAmount, path);
    }

    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
            owner(),
            block.timestamp
        );
    }

    function takeFee(address sender, address recipient, uint256 amount) internal returns (uint256) {
        
        uint256 feeAmount = recipient == uniswapV2Pair ? amount.mul(_antibot).div(100) 
                                : amount.mul(_totaltax).div(100);

        _balances[address(this)] = _balances[address(this)].add(feeAmount);
        emit Transfer(sender, address(this), feeAmount);

        return amount.sub(feeAmount);
    }

    function setSwapAndLiquifyEnabled(bool _enabled) public onlyOwner {
        swapAndLiquifyEnabled = _enabled;
        emit SwapAndLiquifyEnabledUpdated(_enabled);
    }

    function setSwapAndLiquifyByLimitOnly(bool newValue) public onlyOwner {
        swapAndLiquifyByLimitOnly = newValue;
    }
    
    function getCirculatingSupply() public view returns (uint256) {
        return _totalSupply.sub(balanceOf(zeroaddress));
    }

    function blacklistAddress(address account, bool newValue) public onlyOwner {
        blacklistbot[account] = newValue;
    }
}
//to recieve ETH from uniswapV2Router when swaping