Crowdsale.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. pragma solidity ^0.4.21;
  2. import "../token/ERC20/ERC20.sol";
  3. import "../math/SafeMath.sol";
  4. /**
  5. * @title Crowdsale
  6. * @dev Crowdsale is a base contract for managing a token crowdsale,
  7. * allowing investors to purchase tokens with ether. This contract implements
  8. * such functionality in its most fundamental form and can be extended to provide additional
  9. * functionality and/or custom behavior.
  10. * The external interface represents the basic interface for purchasing tokens, and conform
  11. * the base architecture for crowdsales. They are *not* intended to be modified / overriden.
  12. * The internal interface conforms the extensible and modifiable surface of crowdsales. Override
  13. * the methods to add functionality. Consider using 'super' where appropiate to concatenate
  14. * behavior.
  15. */
  16. contract Crowdsale {
  17. using SafeMath for uint256;
  18. // The token being sold
  19. ERC20 public token;
  20. // Address where funds are collected
  21. address public wallet;
  22. // How many token units a buyer gets per wei
  23. uint256 public rate;
  24. // Amount of wei raised
  25. uint256 public weiRaised;
  26. /**
  27. * Event for token purchase logging
  28. * @param purchaser who paid for the tokens
  29. * @param beneficiary who got the tokens
  30. * @param value weis paid for purchase
  31. * @param amount amount of tokens purchased
  32. */
  33. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  34. /**
  35. * @param _rate Number of token units a buyer gets per wei
  36. * @param _wallet Address where collected funds will be forwarded to
  37. * @param _token Address of the token being sold
  38. */
  39. function Crowdsale(uint256 _rate, address _wallet, ERC20 _token) public {
  40. require(_rate > 0);
  41. require(_wallet != address(0));
  42. require(_token != address(0));
  43. rate = _rate;
  44. wallet = _wallet;
  45. token = _token;
  46. }
  47. // -----------------------------------------
  48. // Crowdsale external interface
  49. // -----------------------------------------
  50. /**
  51. * @dev fallback function ***DO NOT OVERRIDE***
  52. */
  53. function () external payable {
  54. buyTokens(msg.sender);
  55. }
  56. /**
  57. * @dev low level token purchase ***DO NOT OVERRIDE***
  58. * @param _beneficiary Address performing the token purchase
  59. */
  60. function buyTokens(address _beneficiary) public payable {
  61. uint256 weiAmount = msg.value;
  62. _preValidatePurchase(_beneficiary, weiAmount);
  63. // calculate token amount to be created
  64. uint256 tokens = _getTokenAmount(weiAmount);
  65. // update state
  66. weiRaised = weiRaised.add(weiAmount);
  67. _processPurchase(_beneficiary, tokens);
  68. emit TokenPurchase(
  69. msg.sender,
  70. _beneficiary,
  71. weiAmount,
  72. tokens
  73. );
  74. _updatePurchasingState(_beneficiary, weiAmount);
  75. _forwardFunds();
  76. _postValidatePurchase(_beneficiary, weiAmount);
  77. }
  78. // -----------------------------------------
  79. // Internal interface (extensible)
  80. // -----------------------------------------
  81. /**
  82. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
  83. * @param _beneficiary Address performing the token purchase
  84. * @param _weiAmount Value in wei involved in the purchase
  85. */
  86. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  87. require(_beneficiary != address(0));
  88. require(_weiAmount != 0);
  89. }
  90. /**
  91. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  92. * @param _beneficiary Address performing the token purchase
  93. * @param _weiAmount Value in wei involved in the purchase
  94. */
  95. function _postValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  96. // optional override
  97. }
  98. /**
  99. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  100. * @param _beneficiary Address performing the token purchase
  101. * @param _tokenAmount Number of tokens to be emitted
  102. */
  103. function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {
  104. token.transfer(_beneficiary, _tokenAmount);
  105. }
  106. /**
  107. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  108. * @param _beneficiary Address receiving the tokens
  109. * @param _tokenAmount Number of tokens to be purchased
  110. */
  111. function _processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
  112. _deliverTokens(_beneficiary, _tokenAmount);
  113. }
  114. /**
  115. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  116. * @param _beneficiary Address receiving the tokens
  117. * @param _weiAmount Value in wei involved in the purchase
  118. */
  119. function _updatePurchasingState(address _beneficiary, uint256 _weiAmount) internal {
  120. // optional override
  121. }
  122. /**
  123. * @dev Override to extend the way in which ether is converted to tokens.
  124. * @param _weiAmount Value in wei to be converted into tokens
  125. * @return Number of tokens that can be purchased with the specified _weiAmount
  126. */
  127. function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
  128. return _weiAmount.mul(rate);
  129. }
  130. /**
  131. * @dev Determines how ETH is stored/forwarded on purchases.
  132. */
  133. function _forwardFunds() internal {
  134. wallet.transfer(msg.value);
  135. }
  136. }