Crowdsale.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. pragma solidity ^0.4.11;
  2. import '../token/MintableToken.sol';
  3. import '../math/SafeMath.sol';
  4. /**
  5. * @title Crowdsale
  6. * @dev Crowdsale is a base contract for managing a token crowdsale.
  7. * Crowdsales have a start and end timestamps, where investors can make
  8. * token purchases and the crowdsale will assign them tokens based
  9. * on a token per ETH rate. Funds collected are forwarded to a wallet
  10. * as they arrive.
  11. */
  12. contract Crowdsale {
  13. using SafeMath for uint256;
  14. // The token being sold
  15. MintableToken public token;
  16. // start and end timestamps where investments are allowed (both inclusive)
  17. uint256 public startTime;
  18. uint256 public endTime;
  19. // address where funds are collected
  20. address public wallet;
  21. // how many token units a buyer gets per wei
  22. uint256 public rate;
  23. // amount of raised money in wei
  24. uint256 public weiRaised;
  25. /**
  26. * event for token purchase logging
  27. * @param purchaser who paid for the tokens
  28. * @param beneficiary who got the tokens
  29. * @param value weis paid for purchase
  30. * @param amount amount of tokens purchased
  31. */
  32. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  33. function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet) {
  34. require(_startTime >= now);
  35. require(_endTime >= _startTime);
  36. require(_rate > 0);
  37. require(_wallet != 0x0);
  38. token = createTokenContract();
  39. startTime = _startTime;
  40. endTime = _endTime;
  41. rate = _rate;
  42. wallet = _wallet;
  43. }
  44. // creates the token to be sold.
  45. // override this method to have crowdsale of a specific mintable token.
  46. function createTokenContract() internal returns (MintableToken) {
  47. return new MintableToken();
  48. }
  49. // fallback function can be used to buy tokens
  50. function () payable {
  51. buyTokens(msg.sender);
  52. }
  53. // low level token purchase function
  54. function buyTokens(address beneficiary) payable {
  55. require(beneficiary != 0x0);
  56. require(validPurchase());
  57. uint256 weiAmount = msg.value;
  58. // calculate token amount to be created
  59. uint256 tokens = weiAmount.mul(rate);
  60. // update state
  61. weiRaised = weiRaised.add(weiAmount);
  62. token.mint(beneficiary, tokens);
  63. TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
  64. forwardFunds();
  65. }
  66. // send ether to the fund collection wallet
  67. // override to create custom fund forwarding mechanisms
  68. function forwardFunds() internal {
  69. wallet.transfer(msg.value);
  70. }
  71. // @return true if the transaction can buy tokens
  72. function validPurchase() internal constant returns (bool) {
  73. bool withinPeriod = now >= startTime && now <= endTime;
  74. bool nonZeroPurchase = msg.value != 0;
  75. return withinPeriod && nonZeroPurchase;
  76. }
  77. // @return true if crowdsale event has ended
  78. function hasEnded() public constant returns (bool) {
  79. return now > endTime;
  80. }
  81. }