Crowdsale.sol 7.1 KB

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