Crowdsale.sol 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. pragma solidity ^0.5.7;
  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 conforms
  13. * the base architecture for crowdsales. It is *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 payable 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 payable wallet, IERC20 token) public {
  49. require(rate > 0);
  50. require(wallet != address(0));
  51. require(address(token) != address(0));
  52. _rate = rate;
  53. _wallet = wallet;
  54. _token = token;
  55. }
  56. /**
  57. * @dev fallback function ***DO NOT OVERRIDE***
  58. * Note that other contracts will transfer funds with a base gas stipend
  59. * of 2300, which is not enough to call buyTokens. Consider calling
  60. * buyTokens directly when purchasing tokens from a contract.
  61. */
  62. function () external payable {
  63. buyTokens(msg.sender);
  64. }
  65. /**
  66. * @return the token being sold.
  67. */
  68. function token() public view returns (IERC20) {
  69. return _token;
  70. }
  71. /**
  72. * @return the address where funds are collected.
  73. */
  74. function wallet() public view returns (address payable) {
  75. return _wallet;
  76. }
  77. /**
  78. * @return the number of token units a buyer gets per wei.
  79. */
  80. function rate() public view returns (uint256) {
  81. return _rate;
  82. }
  83. /**
  84. * @return the amount of wei raised.
  85. */
  86. function weiRaised() public view returns (uint256) {
  87. return _weiRaised;
  88. }
  89. /**
  90. * @dev low level token purchase ***DO NOT OVERRIDE***
  91. * This function has a non-reentrancy guard, so it shouldn't be called by
  92. * another `nonReentrant` function.
  93. * @param beneficiary Recipient of the token purchase
  94. */
  95. function buyTokens(address beneficiary) public nonReentrant payable {
  96. uint256 weiAmount = msg.value;
  97. _preValidatePurchase(beneficiary, weiAmount);
  98. // calculate token amount to be created
  99. uint256 tokens = _getTokenAmount(weiAmount);
  100. // update state
  101. _weiRaised = _weiRaised.add(weiAmount);
  102. _processPurchase(beneficiary, tokens);
  103. emit TokensPurchased(msg.sender, beneficiary, weiAmount, tokens);
  104. _updatePurchasingState(beneficiary, weiAmount);
  105. _forwardFunds();
  106. _postValidatePurchase(beneficiary, weiAmount);
  107. }
  108. /**
  109. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met.
  110. * Use `super` in contracts that inherit from Crowdsale to extend their validations.
  111. * Example from CappedCrowdsale.sol's _preValidatePurchase method:
  112. * super._preValidatePurchase(beneficiary, weiAmount);
  113. * require(weiRaised().add(weiAmount) <= cap);
  114. * @param beneficiary Address performing the token purchase
  115. * @param weiAmount Value in wei involved in the purchase
  116. */
  117. function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
  118. require(beneficiary != address(0));
  119. require(weiAmount != 0);
  120. }
  121. /**
  122. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid
  123. * conditions are not met.
  124. * @param beneficiary Address performing the token purchase
  125. * @param weiAmount Value in wei involved in the purchase
  126. */
  127. function _postValidatePurchase(address beneficiary, uint256 weiAmount) internal view {
  128. // solhint-disable-previous-line no-empty-blocks
  129. }
  130. /**
  131. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends
  132. * its tokens.
  133. * @param beneficiary Address performing the token purchase
  134. * @param tokenAmount Number of tokens to be emitted
  135. */
  136. function _deliverTokens(address beneficiary, uint256 tokenAmount) internal {
  137. _token.safeTransfer(beneficiary, tokenAmount);
  138. }
  139. /**
  140. * @dev Executed when a purchase has been validated and is ready to be executed. Doesn't necessarily emit/send
  141. * tokens.
  142. * @param beneficiary Address receiving the tokens
  143. * @param tokenAmount Number of tokens to be purchased
  144. */
  145. function _processPurchase(address beneficiary, uint256 tokenAmount) internal {
  146. _deliverTokens(beneficiary, tokenAmount);
  147. }
  148. /**
  149. * @dev Override for extensions that require an internal state to check for validity (current user contributions,
  150. * etc.)
  151. * @param beneficiary Address receiving the tokens
  152. * @param weiAmount Value in wei involved in the purchase
  153. */
  154. function _updatePurchasingState(address beneficiary, uint256 weiAmount) internal {
  155. // solhint-disable-previous-line no-empty-blocks
  156. }
  157. /**
  158. * @dev Override to extend the way in which ether is converted to tokens.
  159. * @param weiAmount Value in wei to be converted into tokens
  160. * @return Number of tokens that can be purchased with the specified _weiAmount
  161. */
  162. function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
  163. return weiAmount.mul(_rate);
  164. }
  165. /**
  166. * @dev Determines how ETH is stored/forwarded on purchases.
  167. */
  168. function _forwardFunds() internal {
  169. _wallet.transfer(msg.value);
  170. }
  171. }