Crowdsale.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. pragma solidity ^0.4.18;
  2. import "../token/ERC20/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. The contract requires a MintableToken that will be
  11. * minted as contributions arrive, note that the crowdsale contract
  12. * must be owner of the token in order to be able to mint it.
  13. */
  14. contract Crowdsale {
  15. using SafeMath for uint256;
  16. // The token being sold
  17. MintableToken public token;
  18. // start and end timestamps where investments are allowed (both inclusive)
  19. uint256 public startTime;
  20. uint256 public endTime;
  21. // address where funds are collected
  22. address public wallet;
  23. // how many token units a buyer gets per wei
  24. uint256 public rate;
  25. // amount of raised money in wei
  26. uint256 public weiRaised;
  27. /**
  28. * event for token purchase logging
  29. * @param purchaser who paid for the tokens
  30. * @param beneficiary who got the tokens
  31. * @param value weis paid for purchase
  32. * @param amount amount of tokens purchased
  33. */
  34. event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  35. function Crowdsale(uint256 _startTime, uint256 _endTime, uint256 _rate, address _wallet, MintableToken _token) public {
  36. require(_startTime >= now);
  37. require(_endTime >= _startTime);
  38. require(_rate > 0);
  39. require(_wallet != address(0));
  40. require(_token != address(0));
  41. startTime = _startTime;
  42. endTime = _endTime;
  43. rate = _rate;
  44. wallet = _wallet;
  45. token = _token;
  46. }
  47. // fallback function can be used to buy tokens
  48. function () external payable {
  49. buyTokens(msg.sender);
  50. }
  51. // low level token purchase function
  52. function buyTokens(address beneficiary) public payable {
  53. require(beneficiary != address(0));
  54. require(validPurchase());
  55. uint256 weiAmount = msg.value;
  56. // calculate token amount to be created
  57. uint256 tokens = getTokenAmount(weiAmount);
  58. // update state
  59. weiRaised = weiRaised.add(weiAmount);
  60. token.mint(beneficiary, tokens);
  61. TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
  62. forwardFunds();
  63. }
  64. // @return true if crowdsale event has ended
  65. function hasEnded() public view returns (bool) {
  66. return now > endTime;
  67. }
  68. // Override this method to have a way to add business logic to your crowdsale when buying
  69. function getTokenAmount(uint256 weiAmount) internal view returns(uint256) {
  70. return weiAmount.mul(rate);
  71. }
  72. // send ether to the fund collection wallet
  73. // override to create custom fund forwarding mechanisms
  74. function forwardFunds() internal {
  75. wallet.transfer(msg.value);
  76. }
  77. // @return true if the transaction can buy tokens
  78. function validPurchase() internal view returns (bool) {
  79. bool withinPeriod = now >= startTime && now <= endTime;
  80. bool nonZeroPurchase = msg.value != 0;
  81. return withinPeriod && nonZeroPurchase;
  82. }
  83. }