// Contract: HALO
// Target function: function swapAndLiquify(uint256 toSwap) private lockTheSwap
ForExactTokens(
        uint256 amountOut,
        uint256 amountInMax,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);

    function swapExactETHForTokens(
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable returns (uint256[] memory amounts);

    function swapTokensForExactETH(
        uint256 amountOut,
        uint256 amountInMax,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);

    function swapExactTokensForETH(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external returns (uint256[] memory amounts);

    function swapETHForExactTokens(
        uint256 amountOut,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable returns (uint256[] memory amounts);

    function quote(
        uint256 amountA,
        uint256 reserveA,
        uint256 reserveB
    ) external pure returns (uint256 amountB);

    function getAmountOut(
        uint256 amountIn,
        uint256 reserveIn,
        uint256 reserveOut
    ) external pure returns (uint256 amountOut);

    function getAmountIn(
        uint256 amountOut,
        uint256 reserveIn,
        uint256 reserveOut
    ) external pure returns (uint256 amountIn);

    function getAmountsOut(
        uint256 amountIn,
        address[] calldata path
    ) external view returns (uint256[] memory amounts);

    function getAmountsIn(
        uint256 amountOut,
        address[] calldata path
    ) external view returns (uint256[] memory amounts);
}

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint256 liquidity,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline
    ) external returns (uint256 amountETH);

    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint256 liquidity,
        uint256 amountTokenMin,
        uint256 amountETHMin,
        address to,
        uint256 deadline,
        bool approveMax,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external returns (uint256 amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;

    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable;

    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint256 amountIn,
        uint256 amountOutMin,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external;
}

contract HALO is Context, IERC20, Ownable {
    using SafeMath for uint256;
    using Address for address;

    mapping(address => uint256) private _tOwned;
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(address => bool) public _isExcludedFromFee;
    mapping(address => bool) public _isExcludedFromMaxTx;

    address payable private Wallet_Dev =
        payable(0x64e650A605A6906813035Bf11aBBC3cD69b91005);
    address payable private Wallet_Burn =
        payable(0x000000000000000000000000000000000000dEaD);
    address payable private Wallet_zero =
        payable(0x0000000000000000000000000000000000000000);

    string private _name = "Halo Liquidity";
    string private _symbol = "HALO";
    uint8 private _decimals = 9;
    uint256 private _tTotal = 100_000_000 * 10 ** 9;
    uint256 private _tFeeTotal;

    // This is the max fee that the contract will accept, it is hard-coded to protect buyers
    // This includes the buy AND the sell fee!
    uint256 private maxPossibleFee = 25;

    // Setting the initial fees
    uint256 private _TotalFee = 15;
    uint256 public _buyFee = 15;
    uint256 public _sellFee = 15;

    // 'Previous fees' are used to keep track of fee settings when removing and restoring fees
    uint256 private _previousTotalFee = _TotalFee;
    uint256 private _previousBuyFee = _buyFee;
    uint256 private _previousSellFee = _sellFee;

    /*

    WALLET LIMITS 
    
    */

    uint256 public _maxWalletToken = _tTotal.mul(2).div(100);
    uint256 private _previousMaxWalletToken = _maxWalletToken;
    uint256 public _maxTxAmount = _tTotal.mul(2).div(100);
    uint256 public _swapTxAmount = _tTotal.mul(1).div(100000);
    uint256 private _previousMaxTxAmount = _maxTxAmount;
    uint256 private _minSwapAmount = _tTotal.mul(2).div(1000);

    /* 

    UNISWAP SET UP

    */

    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;
    bool public inSwapAndLiquify;
    bool public swapAndLiquifyEnabled = false;
    bool public noFeeToTransfer = true;
    bool public tradingEnabled = false;

    event SwapAndLiquifyEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );

    // Prevent processing while already processing!
    modifier lockTheSwap() {
        inSwapAndLiquify = true;
        _;
        inSwapAndLiquify = false;
    }

    constructor() {
        _tOwned[owner()] = _tTotal;

        excludeFromFee(owner());
        excludeFromFee(address(this));
        excludeFromFee(Wallet_Dev);

        excludeFromTx(owner());
        excludeFromTx(address(this));
        excludeFromTx(Wallet_Dev);

        emit Transfer(address(0), owner(), _tTotal);
    }

    function initializeUniPair() external onlyOwner {
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(
            0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
        );

        // Create pair address for Uniswap
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());
        uniswapV2Router = _uniswapV2Router;

        _allowances[address(this)][address(uniswapV2Router)] = type(uint256).max;
        uniswapV2Router.addLiquidityETH{value: address(this).balance}(
            address(this),
            balanceOf(address(this)),
            0,
            0,
            owner(),
            block.timestamp
        );
    }

    /*

    STANDARD ERC20 COMPLIANCE FUNCTIONS

    */

    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 _tTotal;
    }

    function balanceOf(address account) public view override returns (uint256) {
        return _tOwned[account];
    }

    function transfer(
        address recipient,
        uint256 amount
    ) public override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function allowance(
        address owner,
        address spender
    ) public view override returns (uint256) {
        return _allowances[owner][spender];
    }

    function approve(
        address spender,
        uint256 amount
    ) public override returns (bool) {
        _approve(_msgSender(), spender, 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 enableTrading() external onlyOwner {
        tradingEnabled = true;
        swapAndLiquifyEnabled = true;
    }

    function removeLimits() external onlyOwner {
        _TotalFee = 2;
        _buyFee = 2;
        _sellFee = 2;

        _maxWalletToken = ~uint256(0);
        _maxTxAmount = ~uint256(0);
    }

    // Set a wallet address so that it does not have to pay transaction fees
    function excludeFromTx(address account) public onlyOwner {
        _isExcludedFromMaxTx[account] = true;
    }

    // Set a wallet address so that it has to pay transaction fees
    function includeInTx(address account) public onlyOwner {
        _isExcludedFromMaxTx[account] = false;
    }

    // Set a wallet address so that it does not have to pay transaction fees
    function excludeFromFee(address account) public onlyOwner {
        _isExcludedFromFee[account] = true;
    }

    // Set a wallet address so that it has to pay transaction fees
    function includeInFee(address account) public onlyOwner {
        _isExcludedFromFee[account] = false;
    }

    function _set_Fees(uint256 Buy_Fee, uint256 Sell_Fee) external onlyOwner {
        require((Buy_Fee + Sell_Fee) <= maxPossibleFee, "Fee is too high!");
        _sellFee = Sell_Fee;
        _buyFee = Buy_Fee;
    }

    // Update main wallet
    function Wallet_Update_Dev(address payable wallet) public onlyOwner {
        Wallet_Dev = wallet;
        _isExcludedFromFee[Wallet_Dev] = true;
    }

    /*

    PROCESSING TOKENS - SET UP

    */

    // Toggle on and off to auto process tokens to ETH wallet
    function set_Swap_And_Liquify_Enabled(bool true_or_false) public onlyOwner {
        swapAndLiquifyEnabled = true_or_false;
        emit SwapAndLiquifyEnabledUpdated(true_or_false);
    }

    // This function is required so that the contract can receive ETH from uniswap
    receive() external payable {}

    // Option to set fee or no fee for transfer (just in case the no fee transfer option is exploited in future!)
    // True = there will be no fees when moving tokens around or giving them to friends! (There will only be a fee to buy or sell)
    // False = there will be a fee when buying/selling/tranfering tokens
    // Default is true
    function set_Transfers_Without_Fees(bool true_or_false) external onlyOwner {
        noFeeToTransfer = true_or_false;
    }

    /*

    WALLET LIMITS

    Wallets are limited in two ways. The amount of tokens that can be purchased in one transaction
    and the total amount of tokens a wallet can buy. Limiting a wallet prevents one wallet from holding too
    many tokens, which can scare away potential buyers that worry that a whale might dump!

    IMPORTANT

    Solidity can not process decimals, so to increase flexibility, we multiple everything by 100.
    When entering the percent, you need to shift your decimal two steps to the right.

    eg: For 4% enter 400, for 1% enter 100, for 0.25% enter 25, for 0.2% enter 20 etc!

    */

    // Set the Max transaction amount (percent of total supply)

    function set_Swap_Percent(uint256 percent) external onlyOwner {
        _swapTxAmount = (_tTotal * percent) / 10000;
    }

    function set_Max_Transaction_Percent(
        uint256 maxTxPercent_x100
    ) external onlyOwner {
        _maxTxAmount = (_tTotal * maxTxPercent_x100) / 10000;
    }

    // Set the maximum wallet holding (percent of total supply)
    function set_Max_Wallet_Percent(
        uint256 maxWallPercent_x100
    ) external onlyOwner {
        _maxWalletToken = (_tTotal * maxWallPercent_x100) / 10000;
    }

    // Remove all fees
    function removeAllFee() private {
        if (_TotalFee == 0 && _buyFee == 0 && _sellFee == 0) return;

        _previousBuyFee = _buyFee;
        _previousSellFee = _sellFee;
        _previousTotalFee = _TotalFee;
        _buyFee = 0;
        _sellFee = 0;
        _TotalFee = 0;
    }

    // Restore all fees
    function restoreAllFee() private {
        _TotalFee = _previousTotalFee;
        _buyFee = _previousBuyFee;
        _sellFee = _previousSellFee;
    }

    // Approve a wallet to sell tokens
    function _approve(address owner, address spender, uint256 amount) private {
        require(
            owner != address(0) && spender != address(0),
            "ERR: zero address"
        );
        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    function _transfer(address from, address to, uint256 amount) private {
        /*

        TRANSACTION AND WALLET LIMITS

        */

        require(
            tradingEnabled ||
                _isExcludedFromFee[from] ||
                _isExcludedFromFee[to],
            "Trading not enabled"
        );

        if (!swapAndLiquifyEnabled || inSwapAndLiquify || amount == 0) {
            _basicTransfer(from, to, amount);
            return;
        }

        // Limit wallet total
        if (
            to != owner() &&
            to != Wallet_Dev &&
            to != address(this) &&
            to != uniswapV2Pair &&
            to != Wallet_Burn &&
            from != owner()
        ) {
            uint256 heldTokens = balanceOf(to);
            require(
                (heldTokens + amount) <= _maxWalletToken,
                "You are trying to buy too many tokens. You have reached the limit for one wallet."
            );
        }

        // Limit the maximum number of tokens that can be bought or sold in one transaction
        if (!_isExcludedFromMaxTx[from] && !_isExcludedFromMaxTx[to])
            require(
                amount <= _maxTxAmount,
                "You are trying to buy more than the max transaction limit."
            );

        require(
            from != address(0) && to != address(0),
            "ERR: Using 0 address!"
        );
        require(amount > 0, "Token value must be higher than zero.");

        /*

        PROCESSING

        */

        // SwapAndLiquify is triggered after every X transactions - this number can be adjusted using swapTrigger

        if (
            !inSwapAndLiquify &&
            from != uniswapV2Pair &&
            swapAndLiquifyEnabled &&
            balanceOf(address(this)) > _swapTxAmount &&
            amount > _swapTxAmount &&
            !_isExcludedFromFee[from] &&
            !_isExcludedFromFee[to]
        ) {
            uint256 caBalance = balanceOf(address(this));
            uint256 toSwap = min(amount, min(caBalance, _minSwapAmount));
            swapAndLiquify(toSwap);
        }

        /*

        REMOVE FEES IF REQUIRED

        Fee removed if the to or from address is excluded from fee.
        Fee removed if the transfer is NOT a buy or sell.
        Change fee amount for buy or sell.

        */

        bool takeFee = true;

        if (
            _isExcludedFromFee[from] ||
            _isExcludedFromFee[to] ||
            (noFeeToTransfer && from != uniswapV2Pair && to != uniswapV2Pair)
        ) {
            takeFee = false;
        } else if (from == uniswapV2Pair) {
            _TotalFee = _buyFee;
        } else if (to == uniswapV2Pair) {
            _TotalFee = _sellFee;
        }

        _tokenTransfer(from, to, amount, takeFee);
    }

    // Get minimum amount of tokens
    function min(uint256 a, uint256 b) private pure returns (uint256) {
        return (a > b) ? b : a;
    }

    // Send ETH to external wallet
    function sendToWallet(address payable wallet, uint256 amount) private {
        wallet.transfer(amount);
    }