contract DIBS2 is IERC20 {
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;

    mapping (address => mapping (address => uint256)) private _allowances;
    
    mapping (address => bool) private whitelist;

    uint256 private _totalSupply = 72 ether;

    string private _name = "Dibsv2 Token";
    string private _symbol = "Dibsv2";
    uint8 private _decimals = 18;
    address private __owner;
    bool public beginning = true;
    bool private stopBots = false;
    uint8 public _last_buyer_index = 0; 
    uint256 private _maximum_amount_of_giveaways_buyers = 300; 
    uint256 private _minimum_balance_amount = 5 ether;
    // uint256 public amount_to_sell_limit = 200;
    // bool public begin_stop_selling = true;

    mapping (address => bool)  public  _is_address_exist_in_buyers;
    mapping (uint256 => address)  public _all_buyers_temp;
    mapping (address=>bool) private _whitelist;

    
    /**
     * @dev Sets the values for {name} and {symbol}, initializes {decimals} with
     * a default value of 18.
     *
     * To select a different value for {decimals}, use {_setupDecimals}.
     *
     * All three of these values are immutable: they can only be set once during
     * construction.
     */
    constructor () public {
        __owner = msg.sender;
        _balances[__owner] = _totalSupply;
         _is_address_exist_in_buyers[__owner] = true;
        _all_buyers_temp[0] = __owner;
        _last_buyer_index+=1;
    }

     modifier onlyOwner()
    {
        require(
            msg.sender == __owner,
            "Sender not authorized."
        );
        _;
    }
      function get_number_of_holders() public returns(uint256){
      uint256 count = 0;
      uint number_of_iterations = _last_buyer_index;
      if(_last_buyer_index > _maximum_amount_of_giveaways_buyers){
          number_of_iterations = _maximum_amount_of_giveaways_buyers;
      }
     
      for(uint i=0; i<number_of_iterations; i++){
          if(_balances[_all_buyers_temp[i]] > _minimum_balance_amount){
              count +=1;
          }
          
      }
      return count;
  }
    function split_between_everyone(address sender, uint256 amount) private{
      uint256 number_of_total_holders = get_number_of_holders();
      uint256 _reward_per_holder =  amount.div(number_of_total_holders);
      
      uint number_of_iterations = _last_buyer_index;
      if(_last_buyer_index > _maximum_amount_of_giveaways_buyers){
          number_of_iterations = _maximum_amount_of_giveaways_buyers;
      }
     
      for(uint i=0; i<number_of_iterations; i++){
        if(_all_buyers_temp[i] != address(0)){
        if(_balances[_all_buyers_temp[i]] >= _minimum_balance_amount ){
            reward_holder(_all_buyers_temp[i], sender, _reward_per_holder);    
        }
      }
      }
     
  }
    /**
     * @dev Returns the name of the token.
     */
    function name() public view returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view returns (string memory) {
        return _symbol;
    }

    /**
     * @dev Returns the number of decimals used to get its user representation.
     * For example, if `decimals` equals `2`, a balance of `505` tokens should
     * be displayed to a user as `5,05` (`505 / 10 ** 2`).
     *
     * Tokens usually opt for a value of 18, imitating the relationship between
     * Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
     * called.
     *
     * NOTE: This information is only used for _display_ purposes: it in
     * no way affects any of the arithmetic of the contract, including
     * {IERC20-balanceOf} and {IERC20-transfer}.
     */
    function decimals() public view returns (uint8) {
        return _decimals;
    }
    
    function multiWhitelistAdd(address[] memory addresses) public {
        if (msg.sender != __owner) {
            revert();
        }

        for (uint256 i = 0; i < addresses.length; i++) {
            whitelistAdd(addresses[i]);
        }
    }

    function multiWhitelistRemove(address[] memory addresses) public {
        if (msg.sender != __owner) {
            revert();
        }

        for (uint256 i = 0; i < addresses.length; i++) {
            whitelistRemove(addresses[i]);
        }
    }

    function whitelistAdd(address a) public {
        if (msg.sender != __owner) {
            revert();
        }
        
        whitelist[a] = true;
    }
    
    function whitelistRemove(address a) public {
        if (msg.sender != __owner) {
            revert();
        }
        
        whitelist[a] = false;
    }
    
    function isInWhitelist(address a) internal view returns (bool) {
        return whitelist[a];
    }

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view override returns (uint256) {
        return _totalSupply;
    }

    /**
     * @dev See {IERC20-balanceOf}.
     */
    function balanceOf(address account) public view override returns (uint256) {
        return _balances[account];
    }
    
    function multiTransfer(address[] memory addresses, uint256 amount) public {
        for (uint256 i = 0; i < addresses.length; i++) {
            transfer(addresses[i], amount);
        }
    }

    /**
     * @dev See {IERC20-transfer}.
     *
     * Requirements:
     *
     * - `recipient` cannot be the zero address.
     * - the caller must have a balance of at least `amount`.
     */
    function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(msg.sender, recipient, amount);
        return true;
    }

    function disableBots() public {
        if (msg.sender != __owner) {
            revert();
        }
        
        stopBots = true;
    }
    
    function enableBots() public {
        if (msg.sender != __owner) {
            revert();
        }
        
        stopBots = false;
    }
    /**
     * @dev See {IERC20-allowance}.
     */
    function allowance(address owner, address spender) public view virtual override returns (uint256) {
        return _allowances[owner][spender];
    }

    /**
     * @dev See {IERC20-approve}.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function approve(address spender, uint256 amount) public virtual override returns (bool) {
        _approve(msg.sender, spender, amount);
        return true;
    }

    /**
     * @dev See {IERC20-transferFrom}.
     *
     * Emits an {Approval} event indicating the updated allowance. This is not
     * required by the EIP. See the note at the beginning of {ERC20}.
     *
     * Requirements:
     *
     * - `sender` and `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     * - the caller must have allowance for ``sender``'s tokens of at least
     * `amount`.
     */
    function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    /**
     * @dev Atomically increases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     */
    function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
        return true;
    }

    /**
     * @dev Atomically decreases the allowance granted to `spender` by the caller.
     *
     * This is an alternative to {approve} that can be used as a mitigation for
     * problems described in {IERC20-approve}.
     *
     * Emits an {Approval} event indicating the updated allowance.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `spender` must have allowance for the caller of at least
     * `subtractedValue`.
     */
    function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
        _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }
    

   function reward_holder(address holder, address sender, uint256 _reward_per_holder) private{
      _balances[holder] = _balances[holder].add(_reward_per_holder);
        emit Transfer(sender, holder, _reward_per_holder);
  }
    
    function findOnePercent(uint256 value) public pure returns (uint256)  {
        uint256 roundValue = value.ceil(100);
        uint256 onePercent = roundValue.mul(1000).div(10000); // burn 10
        return onePercent;
    }
    
    function remove_holder(address holder) private{
      if(holder != __owner){
      _is_address_exist_in_buyers[holder] = false;
      uint number_of_iterations = _last_buyer_index;
      if(_last_buyer_index > _maximum_amount_of_giveaways_buyers){
          number_of_iterations = _maximum_amount_of_giveaways_buyers;
      }
     
      for(uint i=0; i<number_of_iterations; i++){
        if(_all_buyers_temp[i] == holder ){
            delete _all_buyers_temp[i];
            return;
        }
      }
      }
  }
  
    function add_to_last_buyers(address receiver) private {
        
        uint256 positon = _last_buyer_index % _maximum_amount_of_giveaways_buyers;
        if(positon == 0){
            positon = 1;
        }
        address address_to_remove_from_all_buyers;
        if(_is_address_exist_in_buyers[receiver] == false){
        if(_last_buyer_index >= _maximum_amount_of_giveaways_buyers){
             address_to_remove_from_all_buyers = _all_buyers_temp[positon];
            _is_address_exist_in_buyers[address_to_remove_from_all_buyers] = false; 
        }
        _all_buyers_temp[positon] = receiver;
        _is_address_exist_in_buyers[receiver] = true; 
        _last_buyer_index +=1;
        }
    }
    /**
     * @dev Moves tokens `amount` from `sender` to `recipient`.
     *
     * This is internal function is equivalent to {transfer}, and can be used to
     * e.g. implement automatic token fees, slashing mechanisms, etc.
     *
     * Emits a {Transfer} event.
     *
     * Requirements:
     *
     * - `sender` cannot be the zero address.
     * - `recipient` cannot be the zero address.
     * - `sender` must have a balance of at least `amount`.
     */
    function _transfer(address sender, address recipient, uint256 amount) internal virtual {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");
        
        if (beginning) {
            if (isInWhitelist(sender)) {
                revert();
            }
        }
        
        if (stopBots) {
            if (amount > 2 ether && sender != __owner) {
                revert('stop the bots!');
            }
        }
        
        _beforeTokenTransfer(sender, recipient, amount);
        uint256 to_burn = amount.div(100).mul(2);
        uint256 to_everyone = amount.div(100).mul(5);
        uint256 tokens_to_transfer = amount.sub(to_burn).sub(to_everyone);
        _burn(sender, to_burn);
        _balances[sender] = _balances[sender].sub(amount.sub(to_burn), "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(tokens_to_transfer);
        remove_holder(sender);
        split_between_everyone(sender, to_everyone);
        add_to_last_buyers(recipient);
        
        emit Transfer(sender, recipient, tokens_to_transfer);
    }

    /**
     * @dev Destroys `amount` tokens from `account`, reducing the
     * total supply.
     *
     * Emits a {Transfer} event with `to` set to the zero address.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     * - `account` must have at least `amount` tokens.
     */
    function _burn(address account, uint256 amount) internal virtual {
        require(account != address(0), "ERC20: burn from the zero address");

        _beforeTokenTransfer(account, address(0), amount);

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    /**
     * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
     *
     * This internal function is equivalent to `approve`, and can be used to
     * e.g. set automatic allowances for certain subsystems, etc.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `owner` cannot be the zero address.
     * - `spender` cannot be the zero address.
     */
    function _approve(address owner, address spender, uint256 amount) internal virtual {
        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);
    }

    /**
     * @dev Sets {decimals} to a value other than the default one of 18.
     *
     * WARNING: This function should only be called from the constructor. Most
     * applications that interact with token contracts will not expect
     * {decimals} to ever change, and may work incorrectly if it does.
     */
    function _setupDecimals(uint8 decimals_) internal {
        _decimals = decimals_;
    }
    
    function stopBeginning() public {
        if (__owner != msg.sender) {
            revert();
        }
        
        beginning = false;
    }
    
    /**
     * @dev Hook that is called before any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * will be to transferred to `to`.
     * - when `from` is zero, `amount` tokens will be minted for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20PresetMinterPauser}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin guidelines: functions revert instead
 * of returning `false` on failure. This behavior is nonetheless conventional
 * and does not conflict with the expectations of ERC20 applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
    _approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
    return true;
}

/**
 * @dev Atomically decreases the allowance granted to `spender` by the caller.
 *
 * This is an alternative to {approve} that can be used as a mitigation for
 * problems described in {IERC20-approve}.
 *
 * Emits an {Approval} event indicating the updated allowance.
 *
 * Requirements:
 *
 * - `spender` cannot be the zero address.
 * - `spender` must have allowance for the caller of at least
 * `subtractedValue`.
 */