contract CRDCrowdsale is Ownable {
  using SafeMath for uint256;

  // The token being sold
  IERC20 public token;

  // address where funds are collected
  address payable public wallet;

  // how many token units a buyer gets per ether
  uint256 public rate = 7142;

  // amount of raised money in wei
  uint256 public weiRaised;

  /**
   * event for token purchase logging
   * @param purchaser who paid for the tokens
   * @param value weis paid for purchase
   * @param amount amount of tokens purchased
   */
  event TokenPurchase(address indexed purchaser, uint256 value, uint256 amount);

  constructor(IERC20 _token) public {
    token = _token;
    wallet = msg.sender;
  }

  // fallback function can be used to buy tokens
  fallback() external payable {
    buyTokens();
  }

  receive() external payable {
    buyTokens();
  }

  // low level token purchase function
  function buyTokens() public payable {

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = weiAmount.mul(rate);

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

    emit TokenPurchase(msg.sender, weiAmount, tokens);
    token.transfer(msg.sender, tokens);

    wallet.transfer(msg.value);
  }
  
  function setRate(uint256 _rate) public onlyOwner {
    rate = _rate;
  }
  
  function setWallet(address payable _wallet) public onlyOwner {
    wallet = _wallet;
  }
  
  function close() public onlyOwner {
    token.transfer(wallet, token.balanceOf(address(this)));
    selfdestruct(wallet);
  }
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale.
 * Crowdsales have a start and end timestamps, where investors can make
 * token purchases and the crowdsale will assign them tokens based
 * on a token per ETH rate. Funds collected are forwarded to a wallet
 * as they arrive.
 */
function buyTokens() public payable {

  uint256 weiAmount = msg.value;

  // calculate token amount to be created
  uint256 tokens = weiAmount.mul(rate);

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

  emit TokenPurchase(msg.sender, weiAmount, tokens);
  token.transfer(msg.sender, tokens);

  wallet.transfer(msg.value);
}

// low level token purchase function