Crowdsale.sol 6.9 KB

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