Crowdsale.sol 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. pragma solidity ^0.4.23;
  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. uint256 public rate;
  24. // Amount of wei raised
  25. uint256 public weiRaised;
  26. /**
  27. * Event for token purchase logging
  28. * @param purchaser who paid for the tokens
  29. * @param beneficiary who got the tokens
  30. * @param value weis paid for purchase
  31. * @param amount amount of tokens purchased
  32. */
  33. event TokenPurchase(
  34. address indexed purchaser,
  35. address indexed beneficiary,
  36. uint256 value,
  37. uint256 amount
  38. );
  39. /**
  40. * @param _rate Number of token units a buyer gets per wei
  41. * @param _wallet Address where collected funds will be forwarded to
  42. * @param _token Address of the token being sold
  43. */
  44. constructor(uint256 _rate, address _wallet, ERC20 _token) public {
  45. require(_rate > 0);
  46. require(_wallet != address(0));
  47. require(_token != address(0));
  48. rate = _rate;
  49. wallet = _wallet;
  50. token = _token;
  51. }
  52. // -----------------------------------------
  53. // Crowdsale external interface
  54. // -----------------------------------------
  55. /**
  56. * @dev fallback function ***DO NOT OVERRIDE***
  57. */
  58. function () external payable {
  59. buyTokens(msg.sender);
  60. }
  61. /**
  62. * @dev low level token purchase ***DO NOT OVERRIDE***
  63. * @param _beneficiary Address performing the token purchase
  64. */
  65. function buyTokens(address _beneficiary) public payable {
  66. uint256 weiAmount = msg.value;
  67. _preValidatePurchase(_beneficiary, weiAmount);
  68. // calculate token amount to be created
  69. uint256 tokens = _getTokenAmount(weiAmount);
  70. // update state
  71. weiRaised = weiRaised.add(weiAmount);
  72. _processPurchase(_beneficiary, tokens);
  73. emit TokenPurchase(
  74. msg.sender,
  75. _beneficiary,
  76. weiAmount,
  77. tokens
  78. );
  79. _updatePurchasingState(_beneficiary, weiAmount);
  80. _forwardFunds();
  81. _postValidatePurchase(_beneficiary, weiAmount);
  82. }
  83. // -----------------------------------------
  84. // Internal interface (extensible)
  85. // -----------------------------------------
  86. /**
  87. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
  88. * @param _beneficiary Address performing the token purchase
  89. * @param _weiAmount Value in wei involved in the purchase
  90. */
  91. function _preValidatePurchase(
  92. address _beneficiary,
  93. uint256 _weiAmount
  94. )
  95. internal
  96. {
  97. require(_beneficiary != address(0));
  98. require(_weiAmount != 0);
  99. }
  100. /**
  101. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  102. * @param _beneficiary Address performing the token purchase
  103. * @param _weiAmount Value in wei involved in the purchase
  104. */
  105. function _postValidatePurchase(
  106. address _beneficiary,
  107. uint256 _weiAmount
  108. )
  109. internal
  110. {
  111. // optional override
  112. }
  113. /**
  114. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  115. * @param _beneficiary Address performing the token purchase
  116. * @param _tokenAmount Number of tokens to be emitted
  117. */
  118. function _deliverTokens(
  119. address _beneficiary,
  120. uint256 _tokenAmount
  121. )
  122. internal
  123. {
  124. token.transfer(_beneficiary, _tokenAmount);
  125. }
  126. /**
  127. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  128. * @param _beneficiary Address receiving the tokens
  129. * @param _tokenAmount Number of tokens to be purchased
  130. */
  131. function _processPurchase(
  132. address _beneficiary,
  133. uint256 _tokenAmount
  134. )
  135. internal
  136. {
  137. _deliverTokens(_beneficiary, _tokenAmount);
  138. }
  139. /**
  140. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  141. * @param _beneficiary Address receiving the tokens
  142. * @param _weiAmount Value in wei involved in the purchase
  143. */
  144. function _updatePurchasingState(
  145. address _beneficiary,
  146. uint256 _weiAmount
  147. )
  148. internal
  149. {
  150. // optional override
  151. }
  152. /**
  153. * @dev Override to extend the way in which ether is converted to tokens.
  154. * @param _weiAmount Value in wei to be converted into tokens
  155. * @return Number of tokens that can be purchased with the specified _weiAmount
  156. */
  157. function _getTokenAmount(uint256 _weiAmount)
  158. internal view returns (uint256)
  159. {
  160. return _weiAmount.mul(rate);
  161. }
  162. /**
  163. * @dev Determines how ETH is stored/forwarded on purchases.
  164. */
  165. function _forwardFunds() internal {
  166. wallet.transfer(msg.value);
  167. }
  168. }