Crowdsale.sol 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. pragma solidity ^0.4.24;
  2. import "../token/ERC20/IERC20.sol";
  3. import "../math/SafeMath.sol";
  4. import "../token/ERC20/SafeERC20.sol";
  5. import "../utils/ReentrancyGuard.sol";
  6. /**
  7. * @title Crowdsale
  8. * @dev Crowdsale is a base contract for managing a token crowdsale,
  9. * allowing investors to purchase tokens with ether. This contract implements
  10. * such functionality in its most fundamental form and can be extended to provide additional
  11. * functionality and/or custom behavior.
  12. * The external interface represents the basic interface for purchasing tokens, and conform
  13. * the base architecture for crowdsales. They are *not* intended to be modified / overridden.
  14. * The internal interface conforms the extensible and modifiable surface of crowdsales. Override
  15. * the methods to add functionality. Consider using 'super' where appropriate to concatenate
  16. * behavior.
  17. */
  18. contract Crowdsale is ReentrancyGuard {
  19. using SafeMath for uint256;
  20. using SafeERC20 for IERC20;
  21. // The token being sold
  22. IERC20 private _token;
  23. // Address where funds are collected
  24. address private _wallet;
  25. // How many token units a buyer gets per wei.
  26. // The rate is the conversion between wei and the smallest and indivisible token unit.
  27. // So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
  28. // 1 wei will give you 1 unit, or 0.001 TOK.
  29. uint256 private _rate;
  30. // Amount of wei raised
  31. uint256 private _weiRaised;
  32. /**
  33. * Event for token purchase logging
  34. * @param purchaser who paid for the tokens
  35. * @param beneficiary who got the tokens
  36. * @param value weis paid for purchase
  37. * @param amount amount of tokens purchased
  38. */
  39. event TokensPurchased(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  40. /**
  41. * @param rate Number of token units a buyer gets per wei
  42. * @dev The rate is the conversion between wei and the smallest and indivisible
  43. * token unit. So, if you are using a rate of 1 with a ERC20Detailed token
  44. * with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
  45. * @param wallet Address where collected funds will be forwarded to
  46. * @param token Address of the token being sold
  47. */
  48. constructor (uint256 rate, address wallet, IERC20 token) internal {
  49. require(rate > 0);
  50. require(wallet != address(0));
  51. require(token != address(0));
  52. _rate = rate;
  53. _wallet = wallet;
  54. _token = token;
  55. }
  56. // -----------------------------------------
  57. // Crowdsale external interface
  58. // -----------------------------------------
  59. /**
  60. * @dev fallback function ***DO NOT OVERRIDE***
  61. * Note that other contracts will transfer fund with a base gas stipend
  62. * of 2300, which is not enough to call buyTokens. Consider calling
  63. * buyTokens directly when purchasing tokens from a contract.
  64. */
  65. function () external payable {
  66. buyTokens(msg.sender);
  67. }
  68. /**
  69. * @return the token being sold.
  70. */
  71. function token() public view returns (IERC20) {
  72. return _token;
  73. }
  74. /**
  75. * @return the address where funds are collected.
  76. */
  77. function wallet() public view returns (address) {
  78. return _wallet;
  79. }
  80. /**
  81. * @return the number of token units a buyer gets per wei.
  82. */
  83. function rate() public view returns (uint256) {
  84. return _rate;
  85. }
  86. /**
  87. * @return the amount of wei raised.
  88. */
  89. function weiRaised() public view returns (uint256) {
  90. return _weiRaised;
  91. }
  92. /**
  93. * @dev low level token purchase ***DO NOT OVERRIDE***
  94. * This function has a non-reentrancy guard, so it shouldn't be called by
  95. * another `nonReentrant` function.
  96. * @param beneficiary Recipient of the token purchase
  97. */
  98. function buyTokens(address beneficiary) public nonReentrant payable {
  99. uint256 weiAmount = msg.value;
  100. _preValidatePurchase(beneficiary, weiAmount);
  101. // calculate token amount to be created
  102. uint256 tokens = _getTokenAmount(weiAmount);
  103. // update state
  104. _weiRaised = _weiRaised.add(weiAmount);
  105. _processPurchase(beneficiary, tokens);
  106. emit TokensPurchased(msg.sender, beneficiary, weiAmount, tokens);
  107. _updatePurchasingState(beneficiary, weiAmount);
  108. _forwardFunds();
  109. _postValidatePurchase(beneficiary, weiAmount);
  110. }
  111. // -----------------------------------------
  112. // Internal interface (extensible)
  113. // -----------------------------------------
  114. /**
  115. * @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.
  116. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  117. * super._preValidatePurchase(beneficiary, weiAmount);
  118. * require(weiRaised().add(weiAmount) <= cap);
  119. * @param beneficiary Address performing the token purchase
  120. * @param weiAmount Value in wei involved in the purchase
  121. */
  122. function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
  123. require(beneficiary != address(0));
  124. require(weiAmount != 0);
  125. }
  126. /**
  127. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  128. * @param beneficiary Address performing the token purchase
  129. * @param weiAmount Value in wei involved in the purchase
  130. */
  131. function _postValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
  132. // optional override
  133. }
  134. /**
  135. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  136. * @param beneficiary Address performing the token purchase
  137. * @param tokenAmount Number of tokens to be emitted
  138. */
  139. function _deliverTokens(address beneficiary, uint256 tokenAmount) internal {
  140. _token.safeTransfer(beneficiary, tokenAmount);
  141. }
  142. /**
  143. * @dev Executed when a purchase has been validated and is ready to be executed. Doesn't necessarily emit/send tokens.
  144. * @param beneficiary Address receiving the tokens
  145. * @param tokenAmount Number of tokens to be purchased
  146. */
  147. function _processPurchase(address beneficiary, uint256 tokenAmount) internal {
  148. _deliverTokens(beneficiary, tokenAmount);
  149. }
  150. /**
  151. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  152. * @param beneficiary Address receiving the tokens
  153. * @param weiAmount Value in wei involved in the purchase
  154. */
  155. function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
  156. // optional override
  157. }
  158. /**
  159. * @dev Override to extend the way in which ether is converted to tokens.
  160. * @param weiAmount Value in wei to be converted into tokens
  161. * @return Number of tokens that can be purchased with the specified _weiAmount
  162. */
  163. function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
  164. return weiAmount.mul(_rate);
  165. }
  166. /**
  167. * @dev Determines how ETH is stored/forwarded on purchases.
  168. */
  169. function _forwardFunds() internal {
  170. _wallet.transfer(msg.value);
  171. }
  172. }