contract Crowdsale is Ownable {
	using SafeMath for uint256;

	// The token being sold
	IERC20 private _token;

	// Address where funds are collected
	address payable private _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 ERC20Detailed token with 3 decimals called TOK
	// 1 wei will give you 1 unit, or 0.001 TOK.
	uint256 private _rate;

	// Amount of wei raised
	uint256 private _tokenRaised;

	mapping (address => uint256) private _beneficiariesTokenRaised;

	/**
	 * @param rate Number of token units a buyer gets per wei
	 * @dev 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 ERC20Detailed token
	 * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
	 * @param wallet Address where collected funds will be forwarded to
	 * @param token Address of the token being sold
	 */
	constructor(uint256 rate, address payable wallet, IERC20 token) public {
		require(rate > 0, "Crowdsale: rate is 0");
		require(wallet != address(0), "Crowdsale: wallet is the zero address");
		require(
			address(token) != address(0),
			"Crowdsale: token is the zero address"
		);

		_rate = rate;
		_wallet = wallet;
		_token = token;
	}

	/**
	 * @return the token being sold.
	 */
	function token() public view returns (IERC20) {
		return _token;
	}

	/**
	 * @return the address where funds are collected.
	 */
	function wallet() public view returns (address payable) {
		return _wallet;
	}

	/**
	 * @return the number of token units a buyer gets per wei.
	 */
	function rate() public view returns (uint256) {
		return _rate;
	}

	/**
	 * @return the amount of token raised.
	 */
	function tokenRaised() public view returns (uint256) {
		return _tokenRaised;
	}

	/**
	 * @return the amount of token raised of beneficiary address
	 */
	function tokenRaisedOfBeneficiary(address beneficiary) public view returns (uint256) {
		return _beneficiariesTokenRaised[beneficiary];
	}

	/**
	 * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met.
	 * Use `super` in contracts that inherit from Crowdsale to extend their validations.
	 * Example from CappedCrowdsale.sol's _preValidatePurchase method:
	 *     super._preValidatePurchase(beneficiary, tokenAmount);
	 *     require(tokenRaised().add(tokenAmount) <= cap);
	 * @param beneficiary Address performing the token purchase
	 * @param tokenAmount Value in token involved in the purchase
	 */
	function _preValidatePurchase(address beneficiary, uint256 tokenAmount)
		internal
		view
	{
		require(
			beneficiary != address(0),
			"Crowdsale: beneficiary is the zero address"
		);
		require(tokenAmount != 0, "Crowdsale: tokenAmount is 0");
		this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
	}

	/**
	 * @dev Override for extensions that require an internal state to check for validity (current user contributions,
	 * etc.)
	 * @param beneficiary Address receiving the tokens
	 * @param tokenAmount Value in token involved in the purchase
	 */
	function _updatePurchasingState(address beneficiary, uint256 tokenAmount)
		internal
	{
		// solhint-disable-previous-line no-empty-blocks
		_tokenRaised = _tokenRaised.add(tokenAmount);
		_beneficiariesTokenRaised[beneficiary] = _beneficiariesTokenRaised[beneficiary].add(tokenAmount);
	}

	/**
	 * @dev set exchange rate to new rate
	 */
	function _setRate(uint256 _newRate) internal {
		require(_newRate != 0, "Crowdsale: Rate is 0.");
		_rate = _newRate;
	}
}
/**
 * @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 conforms
 * the base architecture for crowdsales. It is *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 tokenRaised() public view returns (uint256) {
		return _tokenRaised;
	}
/**
 	 * @return the amount of token raised.
 	 */