contract Crowdsale is Ownable {
  using SafeMath for uint256;

  // The token being sold
  token myToken;
  
  // address where funds are collected
  address public wallet;
  
  // rate => tokens per ether
  uint256 public rate = 750000 ; 

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

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


  constructor(address tokenContractAddress, address _walletAddress) public{
    wallet = _walletAddress;
    myToken = token(tokenContractAddress);
  }

  // fallback function can be used to buy tokens
  function () payable public{
    buyTokens(msg.sender);
  }

  function getBalance() public constant returns(uint256){
      return myToken.balanceOf(this);
  }    

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(msg.value >= 10000000000000000);// min contribution 0.01ETH
    require(msg.value <= 1000000000000000000);// max contribution 1ETH

    uint256 weiAmount = msg.value;

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

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

    myToken.transfer(beneficiary, tokens);

    emit TokenPurchase(beneficiary, weiAmount, tokens);
  }

  // to change rate
  function updateRate(uint256 new_rate) onlyOwner public{
    rate = new_rate;
  }


  // send ether to the fund collection wallet
  // override to create custom fund forwarding mechanisms
  function forwardFunds() onlyOwner public {
    wallet.transfer(address(this).balance);
  }

  function transferBackTo(uint256 tokens, address beneficiary) onlyOwner public returns (bool){
    myToken.transfer(beneficiary, tokens);
    return true;
  }

}
/**
 * @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(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(msg.value >= 10000000000000000);// min contribution 0.01ETH
    require(msg.value <= 1000000000000000000);// max contribution 1ETH

    uint256 weiAmount = msg.value;

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

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

    myToken.transfer(beneficiary, tokens);

    emit TokenPurchase(beneficiary, weiAmount, tokens);
  }
// low level token purchase function