//SPDX-License-Identifier: MIT
/*
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                          & &                                                                                         
                                                        &GPGGBB#                                                                                      
                                                        #GBBBBBBGB&                                                                                   
                                                       &#&   &GPBBGGPJYP#        #&                                                                   
                                                      &#       &BBGGPP7?J5#     &55B                                                                  
                                                   &&#&        #BBGGGGPJ?77&   &B&&                                                                   
                                            &#BBGBBGB##BB&    &#BGBBBBBBGPG   &B&                                                                     
                                           BPPG55YPYY?YGGPB# #BBBBBBGGBBG5GG##B&                                                                      
                                          GPPGGP5PPPPJ?P#BBBGP55YJJJ?77??JJJ5PGG&                                                                     
                                         #PGBBGGGG5PGGPPBBGP5PP5YJJY555J7777?JPPPPBBGB#BB   &                                                         
                                         #GG#BGGBBBBBBGPGGGBPY?7!~^::^7PG???~^~~~777JJ?7?Y5YJ?JY5B                                                    
                                         &PPGGPB######BGGBB5YY5PGGP?^:::YB?YJ!!~^!?7!????7!!~^:::!7G                                                  
                                          #P5PB######BGGG#J5Y5B##BBGY777J#PYPY????J5Y?~:::^^!?!^~!~?P                                                 
                                    &##&   &GB#&#&&#BGGPBB75JP#####GP5PGB#GGBGPPGGPYY5J!:::..^7?~!?^.^?G                                              
                                    #BB#   &&#BB###GGGG5G#7555PGGPPPPPBB#BB#BBGGGGGGG55YJ?!~:::7J!!7^..:Y&                                            
                                     &BB  #GBGGB##BPPPGPPBG5GPPPPPGGBB######BBGGGGGGBBGP5?7~^:::!YJ777?JJ5                                            
                                       GPBGGG5GGB#GP5GBGGPB###BB############BBBGGGGGY?7~~^^::::.:^77!JY5?J                                            
                                      G5JJGBGPPGGBGGG#BBBBGGBBB###########BGBGGGGPP5YJ?JYYYYYYJ???7!^!PP?5                                            
                                     #YYJJGBBGGBBBBBGGBB##BBB###########BGPPPPPPPGGGGGGGPPPYYYYY?7?YY~7PG                                             
                                     PJJJJGBBGB##GGBGGGBBBBBB#######BBGGPPPPPGGBBBBBBGGGGBGGBBBBGP5J?J!B                                              
                                     YYYJP##BB#&&#PPGBGGGGBGBGGBBBBBGGGGGGGBBBBBBBBBBBBBBBBBBBBBBBBBP?JG                                              
                                     Y5P5##BG#&&&#GGPGBGGGGGGGGGGGGGGGBBBBBBBBBBBBBBBBBBBBBBBBBBBBBGG5J5#                                             
                                     #Y5BBGGB&&&&#PPGGGGGGGGGPPPGGBBBBBBBB#####BBBBBBBBBBBBBBBBBBGGGPP55G                                             
                                      &5Y5PB###&&#BPPBGBGGB#BBGGGGBB#########B##BBBBBBBBBBBBBBGGGGGGGGPG&                                             
                                     &&#BGG###&&#BGPGBBBBB###BB####&########BBBB#BBBBBBBBBGGPPGGGGBBGGB                                               
                                &&&&&&  &B##&###BGGGB#########&&&&&&###&#######BBBBBBBBBBGGGGBBBBBBB#&                                                
                           &###&&&         &G5Y5PGBB#&&&&&&&#B########&#########BBBBBBBBB##BB#BBBB&                                                   
                           BBB#B#&&&       #PPPPGBGGB#&&&&&&&&&&&&##&###B####&&&#######&&#BBGG#                                                       
                       &  #GGPGGBBGB####BBBGYJPGPPG####&&&&&&&&&&&&#########&&&&&&&&&&&&##BGPG&                                                       
                       B #GBGBB######BBGPGGGB###GGBB#####BB###&&&&&##B#####&&#BBBBBBBBGGGGBG5PBP&                                                     
                       BG#G##B#######BBBBBBBBB###GB#######BBBB##&&&&&&####&#BBBBBBBBBBGGGGGP5PBB&                                                     
                        BG#  B#&    &&BBBB##B##&&###&&##&&&BGB&#B#####&&&&BGBBBBBBBBBGGGGGGP5GB &&                                                    
                        #G#  BG&     &GB#BBB#&&&&&&&&&&###&#B&&#B######&&#BBBBBBBBBBBBBGGGPPPP&                                                       
                         BB&  #B#& &BPGBGGB##&&&&&#&&&&######&&&#&####&&&#BBBBBBBBBBB#BGGPPPPG#BG  #GGG&                                              
                          &BBB#&###GPB&BGB###&&&&###########&&&&&&&&&&&&&BBBGGGGGGGGP5PPPPP5GB#G!Y#B5YYJPB                                            
                 &##&       &#BB##G5P#BPBB##B#######B#########&&&& &#####BBGGGGBBGGP55PPPPP5G##B5YPBBBGY5P?YG&                                        
               #GPGGB#B&  &BB#&  #5Y5P5GB##BBBBBBB##############&& &GGB##BGBBGPBBGGP55PPPPGPB  &BGBBGGBBBBPYJ??5B&                                    
              #GBB#BB##&&#GGBBB#GPGPYP#&#BGBBBBBBB####BB###BB##&&&#BBGGB&BGGGBBBBGGP55PGGGGPB&&  &  #BGBBBGPGGPJ?JYG#&        #BPG&                   
               &#&     &GPGGBBGPY5GB##&#GPGGB###BBBBBBB######&###&BBGGGB#BGGGBBGGGGGGGGGGGPP#    &     &#BGGGBBGP5JYJJYPGG#&GPP5Y5P                   
                &       #GGB#GP5PPYP##BPPGB#####&&#BBBBBBB#&&&&##&&###B#BGBGGGGGGGPPGGGPPPPP#   &         &#BBGGGBGGPPGG5YJY55G##B&                   
                &B#&&    && &GG?5~JY5GGGB#BB##&&&&&&######&&&&&BB##&&&&&BB#BGGGPPPPPPPPPPPPPG                  &#BGGGGBGGB5??5B &                     
                #YY5B       PG&GYYPGYGGB##GGB###&&&&&&&&&&&&&&&#B#B#&&&&##BBBGGGGG55YPPPPPP55#GGG#                #BB5JPGGPPYG  &                     
                 &##&&     &P#& PYGBGPBGGB##########&&&&&&&&&&&&#####&&###BB#GGGGBGPPPPPGGPGBBGGPPPPPB#          &B5JY5GGGGPB#&&&                     
                     &     &G#&  GGPGGPBGGBB##&&&#BB##&&&&&&&#&&&####&&#BB###BBBBBBBBBGBB##&&#BBBBG5J7!!J5B#   #Y?5555PJYBGY5#&&&                     
                      &     #B####GG5PPGGP5PBB##&&&##&&&&&###&&&####&&#BBGB##BB#BBBBBBBBB#######BBBBBGPYJPPYJPBJ75####BP?PP5?5&&                      
                      &         & &G5P5GBGY?J5B######&&&&&###############B#####BBBBB#########&###BBBBBBBBBBGPGGPGG&   #Y!P&&G?J#                      
                      &&           &BGGB#&&PYYPB########&&&BBBB###############BBGBB#&&&#B##B#####BBGGGGBBBBBBPPBB55G &G??G   #5?#                     
                      &&             5GGB##BP5PGP5GBBB#&    &#B##BBBBBBBBBBBBBBB#&###B#######BBB####BGBBBB##BGBBGPYPPG55P&    &JG                     
                       &            BYGG!P#BGPPGGBBPYB           &#BBBBBBBB#&       &#BBBB####B&   &&&&&&&#BBBBBBBBGP5G&                              
                                    GYG5GG#BB#BG#&&#GPP#                                &&&                &BBBBBBBBBB&                               
                                    ##PG####BB#G#   &B5J5&                                             &&##BB##BBBBB#                                 
                                    & &PB###BPBBG&   &#BPG                                           &#BBBB###B##&&                                   
                                       #P&&BBBG#GPB   &#B5G                                        #GBBBB#####& &#&                                   
                                       &P# #BB#B##BP&   &BPB                                     &#BBBB####&                                          
                                        B###BB#####GPB&                                        #GBB#######                                            
                                        &BB###&&&##BGP&                                       &5BB##BBB##                                             
                                      #P5GBB#######BP5PG                                      B5B##BBBGGB###                                          
                                   &#GGGB#B###BGGGB#PPPG                                      &GBBBBBGPP5PP5JPB##B#B#BB#                              
                                 &BBG###&BBBB###BGPGB                            &#####BBBBBBGBBBGBBBG55P5PP5JJ555555YJJJG&                           
                               #GGBB#&&&#PGPG########BB####BBG#                 #GGGBBBBBBBBBBB##BB#BBGBGGBBG5YJ5PGBBBBGP555GBB&                      
                           &BPPPGB#&&&&&GPPB&&&&&&&&&&&####BBGPB##&             &BBBBBBBBB##&&&  &##&&#####&&#BPYJ?5B   &&#BG5Y5YYY5P#                
                       &B5JYY5B&&     B55JY& &&&          &&&##BG55PGGB#          &&&&&&#&                     #GP5J7?&       #BGGP5Y?5               
                      #555PBB&       P5J7YB                     &##GGP5?Y                                         &&B?B           &&#5?&              
                      &#&&&         &GBGG&                          &&&&B                                          #GB             &55#               
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
                                                                                                                                                      
http://t.me/AiPepeOriginal

https://twitter.com/AIPepe_lol

http://aipepe.lol/

https://dexscreener.com/ethereum/0x9ebeee744a18971ed999e13d992a876b4eb1d1a7

*/


pragma solidity ^0.8.17;

interface ERC20 {

    function totalSupply() external view returns (uint256);
    function decimals() external view returns (uint8);
    function symbol() external view returns (string memory);
    function name() external view returns (string memory);
    function getOwner() external view returns (address);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address _owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);

}

abstract contract Ownable {

    address internal owner;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    constructor(address _owner) {
        owner = _owner;
    }

    modifier onlyOwner() {
        require(msg.sender == owner, "not owner"); 
        _;
    }

    function isOwner(address account) public view returns (bool) {
        return account == owner;
    }

    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(owner, newOwner);
        owner = newOwner;
    }

}

interface IDEXFactory {
    function createPair(address tokenA, address tokenB) external returns (address pair);
    function getPair(address tokenA, address tokenB) external view returns (address pair);
}

interface IDEXRouter {

    function factory() external pure returns (address);
    function WETH() external pure returns (address);
    function addLiquidityETH(address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function swapExactTokensForETHSupportingFeeOnTransferTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external;
}

contract AIPEPE is ERC20, Ownable {

    // Events
    event SetMaxWallet(uint256 maxWalletToken);
    event SetSwapBackSettings(bool enabled, uint256 swapThreshold);
    event SetIsFeeExempt(address holder, bool enabled);
    event SetIsTxLimitExempt(address holder, bool enabled);
    event StuckBalanceSent(uint256 amountETH, address recipient);

    // Mappings
    mapping (address => uint256) _balances;
    mapping (address => mapping (address => uint256)) _allowances;
    mapping (address => bool) public isFeeExempt;
    mapping (address => bool) public isTxLimitExempt;

    // Token info
    string constant _name = "AIPEPE";
    string constant _symbol = "AIPEPE";
    uint8 constant _decimals = 18;
    uint256 _totalSupply = 1000000000 * (10 ** _decimals); 

    // Max wallet
    uint256 public _maxWalletSize = (_totalSupply * 1000) / 1000;
    uint256 public _maxTxSize = (_totalSupply * 1000) / 1000;

    // Tax amounts
    uint256 public TreasuryFee = 40;
    uint256 public LiquidityFee = 10;
    uint256 public TotalTax = TreasuryFee + LiquidityFee;

    // Tax wallets
    address DevWallet;
    address TreasuryWallet;
 
    // Contracts
    IDEXRouter public router;
    address public pair;
    address constant private DEAD = 0x000000000000000000000000000000000000dEaD;

    bool public swapEnabled = true;
    uint256 public swapThreshold = _totalSupply * 2 / 10000;

    bool public isTradingEnabled = false;
    uint256 public tradingTimestamp;
    uint256 public cooldown = 900;

    bool inSwap;
    modifier swapping() { inSwap = true; _; inSwap = false; }

    constructor(address _router, address _TreasuryWallet) Ownable(msg.sender) {

        router = IDEXRouter(_router);
        _allowances[address(this)][address(router)] = type(uint256).max;

        address _owner = owner;
        DevWallet = msg.sender;
        TreasuryWallet = _TreasuryWallet;

        isFeeExempt[_owner] = true;
        isTxLimitExempt[_owner] = true;

        _balances[msg.sender] = _totalSupply * 100 / 100;

        emit Transfer(address(0), msg.sender, _totalSupply * 100 / 100);

    }

    receive() external payable { }

// Basic Internal Functions

    function totalSupply() external view override returns (uint256) { return _totalSupply; }
    function decimals() external pure override returns (uint8) { return _decimals; }
    function symbol() external pure override returns (string memory) { return _symbol; }
    function name() external pure override returns (string memory) { return _name; }
    function getOwner() external view override returns (address) { return owner; }
    function balanceOf(address account) public view override returns (uint256) { return _balances[account]; }
    function allowance(address holder, address spender) external view override returns (uint256) { return _allowances[holder][spender]; }

    function approve(address spender, uint256 amount) public override returns (bool) {
        _allowances[msg.sender][spender] = amount;
        emit Approval(msg.sender, spender, amount);
        return true;
    }

    function approveMax(address spender) external returns (bool) {
        return approve(spender, type(uint256).max);
    }

    function transfer(address recipient, uint256 amount) external override returns (bool) {
        return _transferFrom(msg.sender, recipient, amount);
    }

    ////////////////////////////////////////////////
    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
        if(_allowances[sender][msg.sender] != type(uint256).max){
            _allowances[sender][msg.sender] = _allowances[sender][msg.sender] - (amount);
        }
        return _transferFrom(sender, recipient, amount);
    }

    function getPair() public onlyOwner {
        pair = IDEXFactory(router.factory()).getPair(address(this), router.WETH());
        if (pair == address(0)) {pair = IDEXFactory(router.factory()).createPair(address(this), router.WETH());}
    }

    function setIsTradingEnabled(bool _isTradingEnabled) public onlyOwner {
        isTradingEnabled = _isTradingEnabled;
        tradingTimestamp = block.timestamp;
    }

    function _transferFrom(address sender, address recipient, uint256 amount) internal returns (bool) {

        if(inSwap){ return _basicTransfer(sender, recipient, amount);}
        require(isFeeExempt[sender] || isFeeExempt[recipient] || isTradingEnabled, "trading not live");

        if (sender != owner && recipient != owner && recipient != DEAD && recipient != pair && sender != TreasuryWallet) {
            require(isTxLimitExempt[recipient] || (amount <= _maxTxSize && 
                _balances[recipient] + amount <= _maxWalletSize), "tx limit");
        }

        if(shouldSwapBack()){swapBack();}

        _balances[sender] = _balances[sender] - amount;
        uint256 amountReceived = (!shouldTakeFee(sender) || !shouldTakeFee(recipient)) ? amount : takeFee(sender, recipient, amount);
        _balances[recipient] = _balances[recipient] + (amountReceived);

        emit Transfer(sender, recipient, amountReceived);
        return true;
    }
    
    function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
        _balances[sender] = _balances[sender] - amount;
        _balances[recipient] = _balances[recipient] + amount;
        emit Transfer(sender, recipient, amount);
        return true;
    }

// Internal Functions

    function shouldTakeFee(address sender) internal view returns (bool) {
        return !isFeeExempt[sender];
    }

    function getMult() internal returns(uint256) {
        return block.timestamp <= tradingTimestamp + cooldown ? 9 : 1;
    }

    function takeFee(address sender, address recipient, uint256 amount) internal returns (uint256) {
   
        uint256 feeAmount = 0;
        
        if (sender != pair && recipient == pair) {
            feeAmount = amount * (TotalTax * getMult()) / 1000;    
        } else {
            feeAmount = amount * (TotalTax) / 1000;    
        }

        if (feeAmount > 0) {
            _balances[address(this)] = _balances[address(this)] + (feeAmount);
            emit Transfer(sender, address(this), feeAmount);            
        }

        return amount - (feeAmount);
    }

    function shouldSwapBack() internal view returns (bool) {
        return msg.sender != pair
        && !inSwap
        && swapEnabled
        && _balances[address(this)] >= swapThreshold;
    }

    function addLiquidity(uint256 _tokenBalance, uint256 _ETHBalance) private {
        if(_allowances[address(this)][address(router)] < _tokenBalance){_allowances[address(this)][address(router)] = _tokenBalance;}
        router.addLiquidityETH{value: _ETHBalance}(address(this), _tokenBalance, 0, 0, DevWallet, block.timestamp + 5 minutes);
    }

    function sendFees() internal {
        (bool success1,) = payable(TreasuryWallet).call{value: address(this).balance, gas: 30000}("");
        require(success1, 'failed!');
    }

    function swapBack() internal swapping {

        uint256 totalTax = TotalTax * getMult();
        uint256 amountToLiq = balanceOf(address(this)) * (LiquidityFee) / (2 * totalTax);
        uint256 amountToSwap = balanceOf(address(this)) - amountToLiq;

        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = router.WETH();

        router.swapExactTokensForETHSupportingFeeOnTransferTokens(amountToSwap, 0, path, address(this), block.timestamp);

        if (amountToLiq > 0) {
            addLiquidity(amountToLiq, address(this).balance * (LiquidityFee) / (2 * totalTax - LiquidityFee));
        }
    
        sendFees();
    
    }


// Tax and Tx functions
    function setMax(uint256 _maxWalletSize_, uint256 _maxTxSize_) external onlyOwner {
        require(_maxWalletSize_ >= _totalSupply / 1000 && _maxTxSize_ >= _totalSupply / 1000, "max");
        _maxWalletSize = _maxWalletSize_;
        _maxTxSize = _maxTxSize_;
        emit SetMaxWallet(_maxWalletSize);
    }

    function setTaxExempt(address holder, bool exempt) external onlyOwner {
        isFeeExempt[holder] = exempt;
        emit SetIsFeeExempt(holder, exempt);
    }

    function setTxExempt(address holder, bool exempt) external onlyOwner {
        isTxLimitExempt[holder] = exempt;
        emit SetIsTxLimitExempt(holder, exempt);
    }

    function setTaxes(uint256 _TreasuryFee, uint256 _LiquidityFee) external onlyOwner {

        uint256 TreasuryFee = _TreasuryFee;
        uint256 LiquidityFee = _LiquidityFee;
        uint256 TotalTax = TreasuryFee + LiquidityFee;
        require(TotalTax <= 495, 'tax too high');

    }

    function setTaxWallets(address _DevWallet, address _TreasuryWallet) external onlyOwner {

        DevWallet = _DevWallet;
        TreasuryWallet = _TreasuryWallet;

    }

    function getTaxWallets() view public returns(address,address) {
        return (DevWallet, TreasuryWallet);
    }

    function setSwapBackSettings(bool _enabled, uint256 _amount) external onlyOwner {
        require(_amount >= 1, "zero");
        swapEnabled = _enabled;
        swapThreshold = _amount;
        emit SetSwapBackSettings(swapEnabled, swapThreshold);
    }

    function initSwapBack() public onlyOwner {
        swapBack();
    }

    function clearContractETH() external {
        require(DevWallet == msg.sender, 'not dev');
        uint256 _ethBal = address(this).balance;
        if (_ethBal > 0) payable(DevWallet).transfer(_ethBal);
    }

    function clearContractTokens(address _token) external {
        require(DevWallet == msg.sender, 'dev');
        ERC20(_token).transfer(DevWallet, ERC20(_token).balanceOf(address(this)));
    }

    function getSelfAddress() public view returns(address) {
        return address(this);
    }

}