Crowdsale.sol 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. pragma solidity ^0.4.24;
  2. import "../token/ERC20/IERC20.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 IERC20;
  20. // The token being sold
  21. IERC20 private token_;
  22. // Address where funds are collected
  23. address private wallet_;
  24. // How many token units a buyer gets per wei.
  25. uint256 private rate_;
  26. // Amount of wei raised
  27. uint256 private weiRaised_;
  28. /**
  29. * Event for token purchase logging
  30. * @param purchaser who paid for the tokens
  31. * @param beneficiary who got the tokens
  32. * @param value weis paid for purchase
  33. * @param amount amount of tokens purchased
  34. */
  35. event TokensPurchased(
  36. address indexed purchaser,
  37. address indexed beneficiary,
  38. uint256 value,
  39. uint256 amount
  40. );
  41. /**
  42. * @param _rate Number of token units a buyer gets per wei
  43. * @dev The rate is the conversion between wei and the smallest and indivisible
  44. * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
  45. * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
  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, IERC20 _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. * @return the token being sold.
  68. */
  69. function token() public view returns(IERC20) {
  70. return token_;
  71. }
  72. /**
  73. * @return the address where funds are collected.
  74. */
  75. function wallet() public view returns(address) {
  76. return wallet_;
  77. }
  78. /**
  79. * @return the number of token units a buyer gets per wei.
  80. */
  81. function rate() public view returns(uint256) {
  82. return rate_;
  83. }
  84. /**
  85. * @return the mount of wei raised.
  86. */
  87. function weiRaised() public view returns (uint256) {
  88. return weiRaised_;
  89. }
  90. /**
  91. * @dev low level token purchase ***DO NOT OVERRIDE***
  92. * @param _beneficiary Address performing the token purchase
  93. */
  94. function buyTokens(address _beneficiary) public payable {
  95. uint256 weiAmount = msg.value;
  96. _preValidatePurchase(_beneficiary, weiAmount);
  97. // calculate token amount to be created
  98. uint256 tokens = _getTokenAmount(weiAmount);
  99. // update state
  100. weiRaised_ = weiRaised_.add(weiAmount);
  101. _processPurchase(_beneficiary, tokens);
  102. emit TokensPurchased(
  103. msg.sender,
  104. _beneficiary,
  105. weiAmount,
  106. tokens
  107. );
  108. _updatePurchasingState(_beneficiary, weiAmount);
  109. _forwardFunds();
  110. _postValidatePurchase(_beneficiary, weiAmount);
  111. }
  112. // -----------------------------------------
  113. // Internal interface (extensible)
  114. // -----------------------------------------
  115. /**
  116. * @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.
  117. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  118. * super._preValidatePurchase(_beneficiary, _weiAmount);
  119. * require(weiRaised().add(_weiAmount) <= cap);
  120. * @param _beneficiary Address performing the token purchase
  121. * @param _weiAmount Value in wei involved in the purchase
  122. */
  123. function _preValidatePurchase(
  124. address _beneficiary,
  125. uint256 _weiAmount
  126. )
  127. internal
  128. {
  129. require(_beneficiary != address(0));
  130. require(_weiAmount != 0);
  131. }
  132. /**
  133. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  134. * @param _beneficiary Address performing the token purchase
  135. * @param _weiAmount Value in wei involved in the purchase
  136. */
  137. function _postValidatePurchase(
  138. address _beneficiary,
  139. uint256 _weiAmount
  140. )
  141. internal
  142. {
  143. // optional override
  144. }
  145. /**
  146. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  147. * @param _beneficiary Address performing the token purchase
  148. * @param _tokenAmount Number of tokens to be emitted
  149. */
  150. function _deliverTokens(
  151. address _beneficiary,
  152. uint256 _tokenAmount
  153. )
  154. internal
  155. {
  156. token_.safeTransfer(_beneficiary, _tokenAmount);
  157. }
  158. /**
  159. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  160. * @param _beneficiary Address receiving the tokens
  161. * @param _tokenAmount Number of tokens to be purchased
  162. */
  163. function _processPurchase(
  164. address _beneficiary,
  165. uint256 _tokenAmount
  166. )
  167. internal
  168. {
  169. _deliverTokens(_beneficiary, _tokenAmount);
  170. }
  171. /**
  172. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  173. * @param _beneficiary Address receiving the tokens
  174. * @param _weiAmount Value in wei involved in the purchase
  175. */
  176. function _updatePurchasingState(
  177. address _beneficiary,
  178. uint256 _weiAmount
  179. )
  180. internal
  181. {
  182. // optional override
  183. }
  184. /**
  185. * @dev Override to extend the way in which ether is converted to tokens.
  186. * @param _weiAmount Value in wei to be converted into tokens
  187. * @return Number of tokens that can be purchased with the specified _weiAmount
  188. */
  189. function _getTokenAmount(uint256 _weiAmount)
  190. internal view returns (uint256)
  191. {
  192. return _weiAmount.mul(rate_);
  193. }
  194. /**
  195. * @dev Determines how ETH is stored/forwarded on purchases.
  196. */
  197. function _forwardFunds() internal {
  198. wallet_.transfer(msg.value);
  199. }
  200. }