Crowdsale.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // 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 ERC20Detailed token with 3 decimals called TOK
  27. // 1 wei will give you 1 unit, or 0.001 TOK.
  28. uint256 private _rate;
  29. // Amount of wei raised
  30. uint256 private _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 TokensPurchased(
  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. * @dev The rate is the conversion between wei and the smallest and indivisible
  47. * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
  48. * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
  49. * @param wallet Address where collected funds will be forwarded to
  50. * @param token Address of the token being sold
  51. */
  52. constructor(uint256 rate, address wallet, IERC20 token) internal {
  53. require(rate > 0);
  54. require(wallet != address(0));
  55. require(token != address(0));
  56. _rate = rate;
  57. _wallet = wallet;
  58. _token = token;
  59. }
  60. // -----------------------------------------
  61. // Crowdsale external interface
  62. // -----------------------------------------
  63. /**
  64. * @dev fallback function ***DO NOT OVERRIDE***
  65. */
  66. function () external payable {
  67. buyTokens(msg.sender);
  68. }
  69. /**
  70. * @return the token being sold.
  71. */
  72. function token() public view returns(IERC20) {
  73. return _token;
  74. }
  75. /**
  76. * @return the address where funds are collected.
  77. */
  78. function wallet() public view returns(address) {
  79. return _wallet;
  80. }
  81. /**
  82. * @return the number of token units a buyer gets per wei.
  83. */
  84. function rate() public view returns(uint256) {
  85. return _rate;
  86. }
  87. /**
  88. * @return the mount of wei raised.
  89. */
  90. function weiRaised() public view returns (uint256) {
  91. return _weiRaised;
  92. }
  93. /**
  94. * @dev low level token purchase ***DO NOT OVERRIDE***
  95. * @param beneficiary Address performing the token purchase
  96. */
  97. function buyTokens(address beneficiary) public payable {
  98. uint256 weiAmount = msg.value;
  99. _preValidatePurchase(beneficiary, weiAmount);
  100. // calculate token amount to be created
  101. uint256 tokens = _getTokenAmount(weiAmount);
  102. // update state
  103. _weiRaised = _weiRaised.add(weiAmount);
  104. _processPurchase(beneficiary, tokens);
  105. emit TokensPurchased(
  106. msg.sender,
  107. beneficiary,
  108. weiAmount,
  109. tokens
  110. );
  111. _updatePurchasingState(beneficiary, weiAmount);
  112. _forwardFunds();
  113. _postValidatePurchase(beneficiary, weiAmount);
  114. }
  115. // -----------------------------------------
  116. // Internal interface (extensible)
  117. // -----------------------------------------
  118. /**
  119. * @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.
  120. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  121. * super._preValidatePurchase(beneficiary, weiAmount);
  122. * require(weiRaised().add(weiAmount) <= cap);
  123. * @param beneficiary Address performing the token purchase
  124. * @param weiAmount Value in wei involved in the purchase
  125. */
  126. function _preValidatePurchase(
  127. address beneficiary,
  128. uint256 weiAmount
  129. )
  130. internal
  131. view
  132. {
  133. require(beneficiary != address(0));
  134. require(weiAmount != 0);
  135. }
  136. /**
  137. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  138. * @param beneficiary Address performing the token purchase
  139. * @param weiAmount Value in wei involved in the purchase
  140. */
  141. function _postValidatePurchase(
  142. address beneficiary,
  143. uint256 weiAmount
  144. )
  145. internal
  146. view
  147. {
  148. // optional override
  149. }
  150. /**
  151. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  152. * @param beneficiary Address performing the token purchase
  153. * @param tokenAmount Number of tokens to be emitted
  154. */
  155. function _deliverTokens(
  156. address beneficiary,
  157. uint256 tokenAmount
  158. )
  159. internal
  160. {
  161. _token.safeTransfer(beneficiary, tokenAmount);
  162. }
  163. /**
  164. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  165. * @param beneficiary Address receiving the tokens
  166. * @param tokenAmount Number of tokens to be purchased
  167. */
  168. function _processPurchase(
  169. address beneficiary,
  170. uint256 tokenAmount
  171. )
  172. internal
  173. {
  174. _deliverTokens(beneficiary, tokenAmount);
  175. }
  176. /**
  177. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  178. * @param beneficiary Address receiving the tokens
  179. * @param weiAmount Value in wei involved in the purchase
  180. */
  181. function _updatePurchasingState(
  182. address beneficiary,
  183. uint256 weiAmount
  184. )
  185. internal
  186. {
  187. // optional override
  188. }
  189. /**
  190. * @dev Override to extend the way in which ether is converted to tokens.
  191. * @param weiAmount Value in wei to be converted into tokens
  192. * @return Number of tokens that can be purchased with the specified _weiAmount
  193. */
  194. function _getTokenAmount(uint256 weiAmount)
  195. internal view returns (uint256)
  196. {
  197. return weiAmount.mul(_rate);
  198. }
  199. /**
  200. * @dev Determines how ETH is stored/forwarded on purchases.
  201. */
  202. function _forwardFunds() internal {
  203. _wallet.transfer(msg.value);
  204. }
  205. }