Crowdsale.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. pragma solidity ^0.4.18;
  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. TokenPurchase(msg.sender, _beneficiary, weiAmount, tokens);
  69. _updatePurchasingState(_beneficiary, weiAmount);
  70. _forwardFunds();
  71. _postValidatePurchase(_beneficiary, weiAmount);
  72. }
  73. // -----------------------------------------
  74. // Internal interface (extensible)
  75. // -----------------------------------------
  76. /**
  77. * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
  78. * @param _beneficiary Address performing the token purchase
  79. * @param _weiAmount Value in wei involved in the purchase
  80. */
  81. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  82. require(_beneficiary != address(0));
  83. require(_weiAmount != 0);
  84. }
  85. /**
  86. * @dev Validation of an executed purchase. Observe state and use revert statements to undo rollback when valid conditions are not met.
  87. * @param _beneficiary Address performing the token purchase
  88. * @param _weiAmount Value in wei involved in the purchase
  89. */
  90. function _postValidatePurchase(address _beneficiary, uint256 _weiAmount) internal {
  91. // optional override
  92. }
  93. /**
  94. * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
  95. * @param _beneficiary Address performing the token purchase
  96. * @param _tokenAmount Number of tokens to be emitted
  97. */
  98. function _deliverTokens(address _beneficiary, uint256 _tokenAmount) internal {
  99. token.transfer(_beneficiary, _tokenAmount);
  100. }
  101. /**
  102. * @dev Executed when a purchase has been validated and is ready to be executed. Not necessarily emits/sends tokens.
  103. * @param _beneficiary Address receiving the tokens
  104. * @param _tokenAmount Number of tokens to be purchased
  105. */
  106. function _processPurchase(address _beneficiary, uint256 _tokenAmount) internal {
  107. _deliverTokens(_beneficiary, _tokenAmount);
  108. }
  109. /**
  110. * @dev Override for extensions that require an internal state to check for validity (current user contributions, etc.)
  111. * @param _beneficiary Address receiving the tokens
  112. * @param _weiAmount Value in wei involved in the purchase
  113. */
  114. function _updatePurchasingState(address _beneficiary, uint256 _weiAmount) internal {
  115. // optional override
  116. }
  117. /**
  118. * @dev Override to extend the way in which ether is converted to tokens.
  119. * @param _weiAmount Value in wei to be converted into tokens
  120. * @return Number of tokens that can be purchased with the specified _weiAmount
  121. */
  122. function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
  123. return _weiAmount.mul(rate);
  124. }
  125. /**
  126. * @dev Determines how ETH is stored/forwarded on purchases.
  127. */
  128. function _forwardFunds() internal {
  129. wallet.transfer(msg.value);
  130. }
  131. }