Crowdsale.sol 5.6 KB

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