contract Crowdsale {
  using SafeMath for uint256;
  using SafeIRC20 for IRC20;

  // The token being sold
  IRC20 public token;
  

  // Address where funds are collected
  address public wallet;

  // How many token units a buyer gets per wei.
  // The rate is the conversion between wei and the smallest and indivisible token unit.
  // So, if you are using a rate of 1 with a  token with 3 decimals called TOK
  // 1 wei will give you 1 unit, or 0.001 TOK.
  uint256 public rate;
    uint256 public maxpurchase;


  // Amount of wei raised
  uint256 public weiRaised;
  bool public isFunding;



  event TokenPurchase(
    address indexed purchaser,
    address indexed beneficiary,
    uint256 value,
    uint256 amount
  );


  constructor(uint256 _rate, uint256 _maxpurchase, address _wallet, IRC20 _token) public {
    require(_rate > 0);
    require(_wallet != address(0));
    require(_token != address(0));
    
    
    rate = _rate;
    wallet = _wallet;
    token = _token;
    isFunding = true;
    maxpurchase= _maxpurchase;
    
  }

  
  function () external payable {
    buyTokens(msg.sender);
  }


//Change the rate


    // update the ETH/COIN rate
    function updateRate(uint256 Newrate) external {
        require(msg.sender==wallet);
        require(isFunding);
        rate = Newrate;
    }


    function updateMaxPurchase (uint256 _newMaxPurchase) external {
        require(msg.sender==wallet);
        require(isFunding);
        maxpurchase = _newMaxPurchase;
    }
    
    
function closeSale() external {
      require(msg.sender==wallet);
     isFunding= false;
     
    }



//Close the Sale



  function buyTokens(address _beneficiary) public payable {

    require(isFunding==true);


//Anti Whale mechamism

require(token.balanceOf(msg.sender)<= maxpurchase*1000000000000000000);



//New

    uint256 weiAmount = msg.value;
    
    require(weiAmount <= maxpurchase*1000000000000000000);

    
    _preValidatePurchase(_beneficiary, weiAmount);

    // calculate token amount to be created
    uint256 tokens = _getTokenAmount(weiAmount);

    // update state
    weiRaised = weiRaised.add(weiAmount);

    _processPurchase(_beneficiary, tokens);
    emit TokenPurchase(
      msg.sender,
      _beneficiary,
      weiAmount,
      tokens
    );

  //  _updatePurchasingState(_beneficiary, weiAmount);

    _forwardFunds();
//    _postValidatePurchase(_beneficiary, weiAmount);
  }




  function _preValidatePurchase (
    address _beneficiary,
    uint256 _weiAmount
    
  ) pure
    internal
  {
      

    require(_beneficiary != address(0));
    require(_weiAmount != 0);
    

  }




  function _deliverTokens(
    address _beneficiary,
    uint256 _tokenAmount
  )
    internal
  {
    token.safeTransfer(_beneficiary, _tokenAmount);
  }

  function _processPurchase(
    address _beneficiary,
    uint256 _tokenAmount
  )
    internal
  {
    _deliverTokens(_beneficiary, _tokenAmount);
  }


  function _getTokenAmount(uint256 _weiAmount)
    internal view returns (uint256)
  {
    return _weiAmount.mul(rate);
  }

  /**
   * @dev Determines how ETH is stored/forwarded on purchases.
   */
  function _forwardFunds() internal {
    wallet.transfer(msg.value);
  }
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale,
 * allowing investors to purchase tokens with ether. This contract implements
 * such functionality in its most fundamental form and can be extended to provide additional
 * functionality and/or custom behavior.
 * The external interface represents the basic interface for purchasing tokens, and conform
 * the base architecture for crowdsales. They are *not* intended to be modified / overridden.
 * The internal interface conforms the extensible and modifiable surface of crowdsales. Override
 * the methods to add functionality. Consider using 'super' where appropriate to concatenate
 * behavior.
 */
function updateRate(uint256 Newrate) external {
    require(msg.sender==wallet);
    require(isFunding);
    rate = Newrate;
}

//Change the rate
// update the ETH/COIN rate