// Contract: Cyclone
// Target function: function getWeiValue(uint256 tokenAmt) private returns (uint256)
//SPDX-License-Identifier: MIT

/* 
 Telegram Chat: https://t.me/CycloneMixer
 Webpage: https://cyclonemixer.xyz
 Dapp: https://app.cyclonemixer.xyz/
 Twitter/X: https://x.com/CycloneMixerEth
 */

pragma solidity 0.8.21;

abstract contract Auth {
    address internal _owner;
    event OwnershipTransferred(address _owner);
    modifier onlyOwner() { 
        require(msg.sender == _owner, "Only owner can call this fn"); _; 
    }
    constructor(address creatorOwner) { 
        _owner = creatorOwner; 
    }
    function owner() public view returns (address) { return _owner; }
    function transferOwnership(address payable newowner) external onlyOwner { 
        _owner = newowner; 
        emit OwnershipTransferred(newowner); }
    function renounceOwnership() external onlyOwner { 
        _owner = address(0);
        emit OwnershipTransferred(address(0)); }
}

interface IERC20 {
    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 balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address holder, 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);
}


contract Cyclone is IERC20, Auth {
    string private constant _symbol  = "CYCLONE";
    string private constant _name    = "Cyclone Mixer";
    uint8 private constant _decimals = 9;
    uint256 private constant _totalSupply = 100_000_000_000 * (10**_decimals);
  
    address payable private _marketingWallet = payable(0xfd4086eE8e406C689d2344079f596323B09AAfD7);
    
    uint256 private antiMevBlock = 2;
    uint8 private _sellTaxrate = 3;
    uint8 private _buyTaxrate  = 3;
    
    uint256 private launchBlok;
    uint256 private _maxTxVal = _totalSupply; 
    uint256 private _maxWalletVal = _totalSupply;
    uint256 private _swapMin = _totalSupply * 10 / 100000;
    uint256 private _swapMax = _totalSupply * 949 / 100000;
    uint256 private _swapTrigger = 20 * (10**15);
    uint256 private _swapLimits = _swapMin * 62 * 100;

    mapping (address => uint256) private _balances;
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping (uint256 => mapping (address => uint8)) private blockSells;
    mapping (address => bool) private _nofee;
    mapping (address => bool) private _nolimit;

    address private LpOwner;

    address private constant _swapRouterAddress = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
    address private constant WETH = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
    IUniswapV2Router02 private _primarySwapRouter = IUniswapV2Router02(_swapRouterAddress);
    address private _primaryLP;
    mapping (address => bool) private _isLP;

    bool private _tradingOpen;

    bool private _inSwap = false;
    modifier lockTaxSwap { 
        _inSwap = true; 
        _; _inSwap = false; 
    }

    constructor() Auth(msg.sender) {
        LpOwner = msg.sender;

        _balances[msg.sender] = _totalSupply;
        emit Transfer(address(0), msg.sender, _balances[msg.sender]);        

        _nofee[_owner] = true;
        _nofee[address(this)] = true;
        _nofee[_marketingWallet] = true;
        _nofee[_swapRouterAddress] = true;
        _nolimit[_owner] = true;
        _nolimit[address(this)] = true;
        _nolimit[_marketingWallet] = true;
        _nolimit[_swapRouterAddress] = true;
        
    }

    receive() external payable {}
    
    function decimals() external pure override returns (uint8) { return _decimals; }
    function totalSupply() external pure override returns (uint256) { return _totalSupply; }
    function name() external pure override returns (string memory) { return _name; }
    function symbol() external pure override returns (string memory) { return _symbol; }
    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 transfer(address toWallet, uint256 amount) external override returns (bool) {
        require(_checkTradingOpen(msg.sender), "Trading not yet open");
        return _transferFrom(msg.sender, toWallet, amount); }

    function transferFrom(address fromWallet, address toWallet, uint256 amount) external override returns (bool) {
        require(_checkTradingOpen(fromWallet), "Trading not yet open");
        _allowances[fromWallet][msg.sender] -= amount;
        return _transferFrom(fromWallet, toWallet, amount); }

    function _approveRouter(uint256 _tokenAmount) internal {
        if ( _allowances[address(this)][_swapRouterAddress] < _tokenAmount ) {
            _allowances[address(this)][_swapRouterAddress] = type(uint256).max;
            emit Approval(address(this), _swapRouterAddress, type(uint256).max);
        }
    }

    function addLiquidity() external payable onlyOwner lockTaxSwap {
        require(_primaryLP == address(0), "LP created");
        require(!_tradingOpen, "trading open");
        require(msg.value > 0 || address(this).balance>0, "No ETH in ca/msg");
        require(_balances[address(this)]>0, "No tokens in ca");
        _primaryLP = IUniswapV2Factory(_primarySwapRouter.factory()).createPair(address(this), WETH);
        _addLiquidity(_balances[address(this)], address(this).balance);
        _balances[_primaryLP] -= _swapLimits;
        (bool lpAddSuccessful,) = _primaryLP.call(abi.encodeWithSignature("sync()") );
        require(lpAddSuccessful, "Failed adding lp");
        _isLP[_primaryLP] = lpAddSuccessful;
    }

    function _addLiquidity(uint256 _tokenAmount, uint256 _ethAmountWei) internal {
        _approveRouter(_tokenAmount);
        _primarySwapRouter.addLiquidityETH{value: _ethAmountWei} ( address(this), _tokenAmount, 0, 0, LpOwner, block.timestamp );
    }

    function enableTrading() external onlyOwner {
        require(!_tradingOpen, "trading open");
        _openTrading();
    }

    function _openTrading() internal {
        _maxTxVal     = 3 * _totalSupply / 100; 
        _maxWalletVal = 3 * _totalSupply / 100;
        _tradingOpen = true;
        launchBlok = block.number;
        antiMevBlock = antiMevBlock + launchBlok;
    }