Crowdsale.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. pragma solidity ^0.4.24;
  2. import "../token/ERC20/ERC20.sol";
  3. import "../math/SafeMath.sol";
  4. import "../token/ERC20/SafeERC20.sol";
  5. /**
  6. * @title Crowdsale
  7. * @dev Crowdsale is a base contract for managing a token crowdsale,
  8. * allowing investors to purchase tokens with ether. This contract implements
  9. * such functionality in its most fundamental form and can be extended to provide additional
  10. * functionality and/or custom behavior.
  11. * The external interface represents the basic interface for purchasing tokens, and conform
  12. * the base architecture for crowdsales. They are *not* intended to be modified / overridden.
  13. * The internal interface conforms the extensible and modifiable surface of crowdsales. Override
  14. * the methods to add functionality. Consider using 'super' where appropriate to concatenate
  15. * behavior.
  16. */
  17. contract Crowdsale {
  18. using SafeMath for uint256;
  19. using SafeERC20 for ERC20;
  20. // The token being sold
  21. ERC20 public token;
  22. // Address where funds are collected
  23. address public wallet;
  24. // How many token units a buyer gets per wei.
  25. // The rate is the conversion between wei and the smallest and indivisible token unit.
  26. // So, if you are using a rate of 1 with a DetailedERC20 token with 3 decimals called TOK
  27. // 1 wei will give you 1 unit, or 0.001 TOK.
  28. uint256 public rate;
  29. // Amount of wei raised
  30. uint256 public weiRaised;
  31. /**
  32. * Event for token purchase logging
  33. * @param purchaser who paid for the tokens
  34. * @param beneficiary who got the tokens
  35. * @param value weis paid for purchase
  36. * @param amount amount of tokens purchased
  37. */
  38. event TokenPurchase(
  39. address indexed purchaser,
  40. address indexed beneficiary,
  41. uint256 value,
  42. uint256 amount
  43. );
  44. /**
  45. * @param _rate Number of token units a buyer gets per wei
  46. * @param _wallet Address where collected funds will be forwarded to
  47. * @param _token Address of the token being sold
  48. */
  49. constructor(uint256 _rate, address _wallet, ERC20 _token) public {
  50. require(_rate > 0);
  51. require(_wallet != address(0));
  52. require(_token != address(0));
  53. rate = _rate;
  54. wallet = _wallet;
  55. token = _token;
  56. }
  57. // -----------------------------------------
  58. // Crowdsale external interface
  59. // -----------------------------------------
  60. /**
  61. * @dev fallback function ***DO NOT OVERRIDE***
  62. */
  63. function () external payable {
  64. buyTokens(msg.sender);
  65. }
  66. /**
  67. * @dev low level token purchase ***DO NOT OVERRIDE***
  68. * @param _beneficiary Address performing the token purchase
  69. */
  70. function buyTokens(address _beneficiary) public payable {
  71. uint256 weiAmount = msg.value;
  72. _preValidatePurchase(_beneficiary, weiAmount);
  73. // calculate token amount to be created
  74. uint256 tokens = _getTokenAmount(weiAmount);
  75. // update state
  76. weiRaised = weiRaised.add(weiAmount);
  77. _processPurchase(_beneficiary, tokens);
  78. emit TokenPurchase(
  79. msg.sender,
  80. _beneficiary,
  81. weiAmount,
  82. tokens
  83. );
  84. _updatePurchasingState(_beneficiary, weiAmount);
  85. _forwardFunds();
  86. _postValidatePurchase(_beneficiary, weiAmount);
  87. }
  88. // -----------------------------------------
  89. // Internal interface (extensible)
  90. // -----------------------------------------
  91. /**
  92. * @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.
  93. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  94. * super._preValidatePurchase(_beneficiary, _weiAmount);
  95. * require(weiRaised.add(_weiAmount) <= cap);
  96. * @param _beneficiary Address performing the token purchase
  97. * @param _weiAmount Value in wei involved in the purchase
  98. */
  99. function _preValidatePurchase(
  100. address _beneficiary,
  101. uint256 _weiAmount
  102. )
  103. internal
  104. {
  105. require(_beneficiary != address(0));
  106. require(_weiAmount != 0);
  107. }
  108. /**
  109. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  110. * @param _beneficiary Address performing the token purchase
  111. * @param _weiAmount Value in wei involved in the purchase
  112. */
  113. function _postValidatePurchase(
  114. address _beneficiary,
  115. uint256 _weiAmount
  116. )
  117. internal
  118. {
  119. // optional override
  120. }
  121. /**
  122. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  123. * @param _beneficiary Address performing the token purchase
  124. * @param _tokenAmount Number of tokens to be emitted
  125. */
  126. function _deliverTokens(
  127. address _beneficiary,
  128. uint256 _tokenAmount
  129. )
  130. internal
  131. {
  132. token.safeTransfer(_beneficiary, _tokenAmount);
  133. }
  134. /**
  135. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  136. * @param _beneficiary Address receiving the tokens
  137. * @param _tokenAmount Number of tokens to be purchased
  138. */
  139. function _processPurchase(
  140. address _beneficiary,
  141. uint256 _tokenAmount
  142. )
  143. internal
  144. {
  145. _deliverTokens(_beneficiary, _tokenAmount);
  146. }
  147. /**
  148. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  149. * @param _beneficiary Address receiving the tokens
  150. * @param _weiAmount Value in wei involved in the purchase
  151. */
  152. function _updatePurchasingState(
  153. address _beneficiary,
  154. uint256 _weiAmount
  155. )
  156. internal
  157. {
  158. // optional override
  159. }
  160. /**
  161. * @dev Override to extend the way in which ether is converted to tokens.
  162. * @param _weiAmount Value in wei to be converted into tokens
  163. * @return Number of tokens that can be purchased with the specified _weiAmount
  164. */
  165. function _getTokenAmount(uint256 _weiAmount)
  166. internal view returns (uint256)
  167. {
  168. return _weiAmount.mul(rate);
  169. }
  170. /**
  171. * @dev Determines how ETH is stored/forwarded on purchases.
  172. */
  173. function _forwardFunds() internal {
  174. wallet.transfer(msg.value);
  175. }
  176. }