Crowdsale.sol 7.5 KB

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