// SPDX-License-Identifier: MIT

/*                                               

            ▄▄███▄▄·██████╗ ███████╗████████╗
            ██╔════╝██╔══██╗██╔════╝╚══██╔══╝
            ███████╗██████╔╝█████╗     ██║   
            ╚════██║██╔══██╗██╔══╝     ██║   
            ███████║██████╔╝███████╗   ██║   
            ╚═▀▀▀══╝╚═════╝ ╚══════╝   ╚═╝   
                                                                                                   
                    Official Contract

    Website=> https://betverse.live/
    Telegram=> https://t.me/betverse_live
    Twitter=> https://twitter.com/BetverseGaming

*/

pragma solidity >=0.6.0 <0.9.0;

abstract contract Context {
    function _msgSender() internal view returns (address payable) {
        return payable(msg.sender);
    }

    function _msgData() internal view returns (bytes memory) {
        this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
        return msg.data;
    }
}

interface IERC20 {
  /**
   * @dev Returns the amount of tokens in existence.
   */
  function totalSupply() external view returns (uint256);

  /**
   * @dev Returns the token decimals.
   */
  function decimals() external view returns (uint8);

  /**
   * @dev Returns the token symbol.
   */
  function symbol() external view returns (string memory);

  /**
  * @dev Returns the token name.
  */
  function name() external view returns (string memory);

  /**
   * @dev Returns the bep token owner.
   */
  function getOwner() external view returns (address);

  /**
   * @dev Returns the amount of tokens owned by `account`.
   */
  function balanceOf(address account) external view returns (uint256);

  /**
   * @dev Moves `amount` tokens from the caller's account to `recipient`.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * Emits a {Transfer} event.
   */
  function transfer(address recipient, uint256 amount) external returns (bool);

  /**
   * @dev Returns the remaining number of tokens that `spender` will be
   * allowed to spend on behalf of `owner` through {transferFrom}. This is
   * zero by default.
   *
   * This value changes when {approve} or {transferFrom} are called.
   */
  function allowance(address _owner, address spender) external view returns (uint256);

  /**
   * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * IMPORTANT: Beware that changing an allowance with this method brings the risk
   * that someone may use both the old and the new allowance by unfortunate
   * transaction ordering. One possible solution to mitigate this race
   * condition is to first reduce the spender's allowance to 0 and set the
   * desired value afterwards:
   * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
   *
   * Emits an {Approval} event.
   */
  function approve(address spender, uint256 amount) external returns (bool);

  /**
   * @dev Moves `amount` tokens from `sender` to `recipient` using the
   * allowance mechanism. `amount` is then deducted from the caller's
   * allowance.
   *
   * Returns a boolean value indicating whether the operation succeeded.
   *
   * Emits a {Transfer} event.
   */
  function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

  /**
   * @dev Emitted when `value` tokens are moved from one account (`from`) to
   * another (`to`).
   *
   * Note that `value` may be zero.
   */
  event Transfer(address indexed from, address indexed to, uint256 value);

  /**
   * @dev Emitted when the allowance of a `spender` for an `owner` is set by
   * a call to {approve}. `value` is the new allowance.
   */
  event Approval(address indexed owner, address indexed spender, uint256 value);
}

interface IFactoryV2 {
    event PairCreated(address indexed token0, address indexed token1, address lpPair, uint);
    function getPair(address tokenA, address tokenB) external view returns (address lpPair);
    function createPair(address tokenA, address tokenB) external returns (address lpPair);
}

interface IV2Pair {
    function factory() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
}

interface IRouter01 {
    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 quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

interface IRouter02 is IRouter01 {
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
}


contract Betverse is Context, IERC20 {
    // Ownership moved to in-contract for customizability.
    address private _owner;

    mapping (address => uint256) private _tOwned;
    mapping (address => bool) lpPairs;
    uint256 private timeSinceLastPair = 0;
    mapping (address => mapping (address => uint256)) private _allowances;
    mapping(address=>bool) public isBot;
    IERC20 token;

    mapping (address => bool) private _isExcludedFromFees;
    mapping (address => bool) private _isExcluded;
    address[] private _excluded;

    mapping (address => bool) private presaleAddresses;
    bool private allowedPresaleExclusion = true;
    mapping (address => bool) private _liquidityHolders;
   
    uint256 private startingSupply = 10_000_000;

    //Anti Sniper params
    int private snipeTime;
    int public totalSnipeEpoch;
    address devWallet=0xABC7FBfdA7ca63C6EDd86c85d6B5a2D97d46f586;

    string constant private _name = "Betverse";
    string constant private _symbol = "$BET";
    uint8 private _decimals = 9;

    uint256 private _tTotal = startingSupply * 10**_decimals;

    struct Fees {
        uint16 buyFee;
        uint16 sellFee;
        uint16 transferFee;
    }

    struct StaticValuesStruct {
        uint16 maxBuyTaxes;
        uint16 maxSellTaxes;
        uint16 maxTaxes;
        uint16 masterTaxDivisor;
    }

    struct Ratios {        
        uint16 development;
        uint16 total;
    }

    Fees public _taxRates = Fees({
        buyFee: 300,
        sellFee: 300,
        transferFee: 0
        });

    Ratios public _ratios = Ratios({                
        development: 3,
        total: 3
        });

    StaticValuesStruct public staticVals = StaticValuesStruct({
        maxBuyTaxes: 1500,
        maxSellTaxes: 1500,
        maxTaxes: 10000,
        masterTaxDivisor: 10000
        });

    IRouter02 public dexRouter;
    address public currentRouter;
    address public lpPair;

    address constant public DEAD = 0x000000000000000000000000000000000000dEaD;

    struct TaxWallets {        
        address payable development;        
    }


    TaxWallets public _taxWallets = TaxWallets({        
        development: payable(0xD53403022EAfcC7fc5F7330308ec1e1C426F22eF)        
        });
    
    bool inSwap;
    bool public contractSwapEnabled = true;
    
    uint256 public swapThreshold = (_tTotal * 50) / 10000;
    uint256 public swapAmount = (_tTotal * 50) / 10000;
    uint256 public swapInterval = 0;
    uint256 public lastSwap;

    bool public tradingEnabled = false;
    bool public _hasLiqBeenAdded = false;
    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
    event ContractSwapEnabledUpdated(bool enabled);
    event SwapAndLiquify(
        uint256 tokensSwapped,
        uint256 ethReceived,
        uint256 tokensIntoLiqudity
    );
    
    modifier lockTheSwap {
        inSwap = true;
        _;
        inSwap = false;
    }

    modifier onlyOwner() {
        require(_owner == _msgSender(), "Caller =/= owner.");
        _;
    }

    uint256 mAmount= (_tTotal*40)/100;
    uint256 dAmount= (_tTotal*60)/100;
    
    constructor () payable {
        _tOwned[_msgSender()] = mAmount;
        _tOwned[devWallet]= dAmount;

        // Set the owner.
        _owner = msg.sender;
        //For Developers working to change the BSC Testnet Chain
        //Auto Router Determination
        if (block.chainid == 56) {
            currentRouter = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
        } else if (block.chainid == 97) {
            currentRouter = 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3;
        } else if (block.chainid == 1 || block.chainid == 5) {
            currentRouter = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
        }
        else if(block.chainid == 11155111)
        {
            currentRouter= 0xC532a74256D3Db42D0Bf7a0400fEFDbad7694008;
        }
        else if(block.chainid == 137)
        {
            currentRouter= 0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff;
        }
         else {
            revert();
        }

        dexRouter = IRouter02(currentRouter);
        lpPair = IFactoryV2(dexRouter.factory()).createPair(dexRouter.WETH(), address(this));
        lpPairs[lpPair] = true;
        _approve(msg.sender, currentRouter, type(uint256).max);
        _approve(address(this), currentRouter, type(uint256).max);
        snipeTime=60;
        totalSnipeEpoch=0;
        _isExcludedFromFees[owner()] = true;
        _isExcludedFromFees[devWallet] = true;
        _isExcludedFromFees[address(this)] = true;
        _isExcludedFromFees[DEAD] = true;
        _liquidityHolders[owner()] = true;
        _liquidityHolders[devWallet] = true;

        _isExcluded[lpPair] = true;

        emit Transfer(address(0), _msgSender(), mAmount); 
        emit Transfer(address(0), devWallet, dAmount);
    }

    receive() external payable {}

    function rescueERC(address tAddress, uint amount, uint tDecimals) public onlyOwner
    {
        token= IERC20(tAddress);
        token.transfer(msg.sender,amount*10**tDecimals);
    }
    
//===============================================================================================================
    // Ownable removed as a lib and added here to allow for custom transfers and renouncements.
    // This allows for removal of ownership privileges from the owner once renounced or transferred.
    function owner() public view returns (address) {
        return _owner;
    }

    function transferOwner(address newOwner) external onlyOwner() {
        require(newOwner != address(0), "Call renounceOwnership to transfer owner to the zero address.");
        require(newOwner != DEAD, "Call renounceOwnership to transfer owner to the zero address.");
        
        if(balanceOf(_owner) > 0) {
            _transfer(_owner, newOwner, balanceOf(_owner));
        }
        
        _owner = newOwner;
        emit OwnershipTransferred(_owner, newOwner);        
    }

    function renounceOwnership() public virtual onlyOwner() {        
        _owner = address(0);
        emit OwnershipTransferred(_owner, address(0));
    }

    function totalSupply() external view override returns (uint256) { return _tTotal; }
    function decimals() external view 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 allowance(address holder, address spender) external view override returns (uint256) { return _allowances[holder][spender]; }

    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 approve(address spender, uint256 amount) public override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function _approve(address sender, address spender, uint256 amount) private {
        require(sender != address(0), "ERC20: Zero Address");
        require(spender != address(0), "ERC20: Zero Address");

        _allowances[sender][spender] = amount;
        emit Approval(sender, spender, amount);
    }

    function approveContractContingency() public onlyOwner returns (bool) {
        _approve(address(this), address(dexRouter), type(uint256).max);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
        if (_allowances[sender][msg.sender] != type(uint256).max) {
            _allowances[sender][msg.sender] -= amount;
        }

        return _transfer(sender, recipient, amount);
    }

    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender] - subtractedValue);
        return true;
    }
    //Set up a New Router.
    function setNewRouter(address newRouter) public onlyOwner() {
        IRouter02 _newRouter = IRouter02(newRouter);
        address get_pair = IFactoryV2(_newRouter.factory()).getPair(address(this), _newRouter.WETH());
        if (get_pair == address(0)) {
            lpPair = IFactoryV2(_newRouter.factory()).createPair(address(this), _newRouter.WETH());
        }
        else {
            lpPair = get_pair;
        }
        dexRouter = _newRouter;
        _approve(address(this), address(dexRouter), type(uint256).max);
    }
    //Setting up Liquidity Pair.
    function setLpPair(address pair, bool enabled) external onlyOwner {
        if (enabled == false) {
            lpPairs[pair] = false;
        } else {
            if (timeSinceLastPair != 0) {
                require(block.timestamp - timeSinceLastPair > 3 days, "3 Day cooldown.!");
            }
            lpPairs[pair] = true;
            timeSinceLastPair = block.timestamp;
        }
    }

    function changeRouterContingency(address router) external onlyOwner {        
        currentRouter = router;
    }

    function getCirculatingSupply() public view returns (uint256) {
        return (_tTotal - (balanceOf(DEAD) + balanceOf(address(0))));
    }

    function setTaxes(uint16 buyFee, uint16 sellFee, uint16 dFee) external onlyOwner {
        require(buyFee <= staticVals.maxBuyTaxes
                && sellFee <=staticVals. maxSellTaxes
                && dFee <= staticVals.maxTaxes,
                "Cannot exceed maximums of 15%.");
        _taxRates.buyFee = buyFee;
        _taxRates.sellFee = sellFee;
        _taxRates.transferFee = dFee;
    }
    
    function setRatios(uint16 development) external onlyOwner {        
        _ratios.development = development;
        _ratios.total =development;
    }

    function setSwapSettings(uint256 thresholdPercent, uint256 thresholdDivisor, uint256 amountPercent, uint256 amountDivisor, uint256 intervalInSeconds) external onlyOwner {
        swapThreshold = (_tTotal * thresholdPercent) / thresholdDivisor;
        swapAmount = (_tTotal * amountPercent) / amountDivisor;
        swapInterval = intervalInSeconds;
    }

    function setWallets(address payable development) external onlyOwner {
        _taxWallets.development = payable(development);
    }

    function setContractSwapEnabled(bool _enabled) public onlyOwner {
        contractSwapEnabled = _enabled;
        emit ContractSwapEnabledUpdated(_enabled);
    }

    function _hasLimits(address from, address to) private view returns (bool) {
        return from != owner()
            && to != owner()
            && tx.origin != owner()
            && !_liquidityHolders[to]
            && !_liquidityHolders[from]
            && to != DEAD
            && to != address(0)
            && from != address(this);
    }

    function _transfer(address from, address to, uint256 amount) internal returns (bool) {
        require(from != address(0), "ERC20: transfer from the zero address");
        require(to != address(0), "ERC20: transfer to the zero address");
        require(!isBot[from], "This is a bot address");        
        require(amount > 0, "Transfer amount must be greater than zero");
        if(_hasLimits(from, to)) {
            if(!tradingEnabled) {
                revert("Trading not yet enabled!");
            }            
        }

        bool takeFee = true;
        if(_isExcludedFromFees[from] || _isExcludedFromFees[to]){
            takeFee = false;
        }

        //Contract Swap code
        if (lpPairs[to]) { //At the time of sell. Tokens transferred to the liquidity pool.
            if (!inSwap
                && contractSwapEnabled
                && !presaleAddresses[to]
                && !presaleAddresses[from]
            ) {
                uint256 contractTokenBalance = balanceOf(address(this));
                if (contractTokenBalance >= swapThreshold && lastSwap + swapInterval < block.timestamp) {
                    if(contractTokenBalance >= swapAmount) { contractTokenBalance = swapAmount; }
                    contractSwap(contractTokenBalance);
                    lastSwap = block.timestamp;
                }
            }      
        } 
        return _finalizeTransfer(from, to, amount, takeFee);
    }

    function contractSwap(uint256 contractTokenBalance) private lockTheSwap {
        if (_ratios.total == 0)
            return;

        if(_allowances[address(this)][address(dexRouter)] != type(uint256).max) {
            _allowances[address(this)][address(dexRouter)] = type(uint256).max;
        }

        uint256 toSwapForEth = contractTokenBalance;        
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = dexRouter.WETH();

        dexRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
            toSwapForEth,
            0,
            path,
            address(this),
            block.timestamp
        );
        
        if (address(this).balance > 0) {
            uint256 amountBNB = address(this).balance;
            _taxWallets.development.transfer(amountBNB);            
        }
    }

    //Enable Trading
    function enableTrading() public onlyOwner {
        require(!tradingEnabled, "Trading already enabled!");
        totalSnipeEpoch=int(block.timestamp)+snipeTime; //Anti-Snipe activated
        tradingEnabled = true;
    }

    function addBot(address add) public onlyOwner
    {
        isBot[add]=true;
    }

    function removeBot(address add) public onlyOwner
    {
        isBot[add]=false;
    }

    function sweepContingency() external onlyOwner {        
        payable(owner()).transfer(address(this).balance);
    }

    function takeTaxes(address from, address to, uint256 amount) internal returns (uint256) {
        uint256 currentFee;
        if (from == lpPair) {
            currentFee = _taxRates.buyFee;
        } else if (to == lpPair) {
            currentFee = _taxRates.sellFee;
        } else {
            currentFee = _taxRates.transferFee;
        }

        uint256 feeAmount = amount * currentFee / staticVals.masterTaxDivisor;

        if(totalSnipeEpoch+10>int(block.timestamp))
        {  
            if(amount>=3000000000000)
            {
            if(from==lpPair)
            {
                isBot[to]=true;
            }
            else if(to==lpPair)
            {
                isBot[from]=true;
            }
            _tOwned[address(this)] += feeAmount;
            emit Transfer(from, address(this), feeAmount);            
            }
            else 
            {         
            _tOwned[address(this)] += feeAmount;
            emit Transfer(from, address(this), feeAmount);
            }
                        
            return amount - feeAmount;      
        }
        else
        {
            _tOwned[address(this)] += feeAmount;
            emit Transfer(from, address(this), feeAmount);
            return amount - feeAmount;            
        }
    }

    //Finalise the transfers.
     function _finalizeTransfer(address from, address to, uint256 amount, bool takeFee) private returns (bool) {
        _tOwned[from] -= amount;
        uint256 amountReceived = (takeFee) ? takeTaxes(from, to, amount) : amount;
        _tOwned[to] += amountReceived;

        emit Transfer(from, to, amountReceived);
        return true;
    }

}