Crowdsale.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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) public {
  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. {
  132. require(beneficiary != address(0));
  133. require(weiAmount != 0);
  134. }
  135. /**
  136. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  137. * @param beneficiary Address performing the token purchase
  138. * @param weiAmount Value in wei involved in the purchase
  139. */
  140. function _postValidatePurchase(
  141. address beneficiary,
  142. uint256 weiAmount
  143. )
  144. internal
  145. {
  146. // optional override
  147. }
  148. /**
  149. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  150. * @param beneficiary Address performing the token purchase
  151. * @param tokenAmount Number of tokens to be emitted
  152. */
  153. function _deliverTokens(
  154. address beneficiary,
  155. uint256 tokenAmount
  156. )
  157. internal
  158. {
  159. _token.safeTransfer(beneficiary, tokenAmount);
  160. }
  161. /**
  162. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  163. * @param beneficiary Address receiving the tokens
  164. * @param tokenAmount Number of tokens to be purchased
  165. */
  166. function _processPurchase(
  167. address beneficiary,
  168. uint256 tokenAmount
  169. )
  170. internal
  171. {
  172. _deliverTokens(beneficiary, tokenAmount);
  173. }
  174. /**
  175. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  176. * @param beneficiary Address receiving the tokens
  177. * @param weiAmount Value in wei involved in the purchase
  178. */
  179. function _updatePurchasingState(
  180. address beneficiary,
  181. uint256 weiAmount
  182. )
  183. internal
  184. {
  185. // optional override
  186. }
  187. /**
  188. * @dev Override to extend the way in which ether is converted to tokens.
  189. * @param weiAmount Value in wei to be converted into tokens
  190. * @return Number of tokens that can be purchased with the specified _weiAmount
  191. */
  192. function _getTokenAmount(uint256 weiAmount)
  193. internal view returns (uint256)
  194. {
  195. return weiAmount.mul(_rate);
  196. }
  197. /**
  198. * @dev Determines how ETH is stored/forwarded on purchases.
  199. */
  200. function _forwardFunds() internal {
  201. _wallet.transfer(msg.value);
  202. }
  203. }