contract Crowdsale {
  //using SafeMath for uint256;

  // The token being sold
  TrueGoldCoinToken public token;

  // address where funds are collected
  address public wallet;

  // how many token units a buyer gets per wei
  uint256 public rate;

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

  // token's account
  address tokenStockAddress;

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




 function Crowdsale( ) public {
        wallet =   0xB8d4b5001946E6c46e39F1B85B69BbfDaB87EeBa;
        tokenStockAddress = wallet;
        rate = 100 ;
        token = TrueGoldCoinToken(0xE51601f59A610DAc06868aa711A05e9a4e291256);
  }


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

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != address(0));
    require(msg.value != 0);

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = weiAmount * rate;

    // update state
    weiRaised = weiRaised + weiAmount;

    // debug
    TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    //token.transfer(msg.sender, tokens);
    token.transferFrom(tokenStockAddress, msg.sender, tokens); 

    forwardFunds();
  }

  // send ether to the fund collection wallet
  // override to create custom fund forwarding mechanisms
  function forwardFunds() internal {
    wallet.transfer(msg.value);
  }
 
  function setRate(uint newRate) external payable {
	require(msg.sender == wallet);
	rate = newRate;
	}
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale.
 *  Funds collected are forwarded to a wallet
 * as they arrive.
 */
function forwardFunds() internal {
  wallet.transfer(msg.value);
}

// send ether to the fund collection wallet
// override to create custom fund forwarding mechanisms