Crowdsale.sol 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 to concatenate validations.
  93. * @param _beneficiary Address performing the token purchase
  94. * @param _weiAmount Value in wei involved in the purchase
  95. */
  96. function _preValidatePurchase(
  97. address _beneficiary,
  98. uint256 _weiAmount
  99. )
  100. internal
  101. {
  102. require(_beneficiary != address(0));
  103. require(_weiAmount != 0);
  104. }
  105. /**
  106. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  107. * @param _beneficiary Address performing the token purchase
  108. * @param _weiAmount Value in wei involved in the purchase
  109. */
  110. function _postValidatePurchase(
  111. address _beneficiary,
  112. uint256 _weiAmount
  113. )
  114. internal
  115. {
  116. // optional override
  117. }
  118. /**
  119. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  120. * @param _beneficiary Address performing the token purchase
  121. * @param _tokenAmount Number of tokens to be emitted
  122. */
  123. function _deliverTokens(
  124. address _beneficiary,
  125. uint256 _tokenAmount
  126. )
  127. internal
  128. {
  129. token.safeTransfer(_beneficiary, _tokenAmount);
  130. }
  131. /**
  132. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  133. * @param _beneficiary Address receiving the tokens
  134. * @param _tokenAmount Number of tokens to be purchased
  135. */
  136. function _processPurchase(
  137. address _beneficiary,
  138. uint256 _tokenAmount
  139. )
  140. internal
  141. {
  142. _deliverTokens(_beneficiary, _tokenAmount);
  143. }
  144. /**
  145. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  146. * @param _beneficiary Address receiving the tokens
  147. * @param _weiAmount Value in wei involved in the purchase
  148. */
  149. function _updatePurchasingState(
  150. address _beneficiary,
  151. uint256 _weiAmount
  152. )
  153. internal
  154. {
  155. // optional override
  156. }
  157. /**
  158. * @dev Override to extend the way in which ether is converted to tokens.
  159. * @param _weiAmount Value in wei to be converted into tokens
  160. * @return Number of tokens that can be purchased with the specified _weiAmount
  161. */
  162. function _getTokenAmount(uint256 _weiAmount)
  163. internal view returns (uint256)
  164. {
  165. return _weiAmount.mul(rate);
  166. }
  167. /**
  168. * @dev Determines how ETH is stored/forwarded on purchases.
  169. */
  170. function _forwardFunds() internal {
  171. wallet.transfer(msg.value);
  172. }
  173. }