TimedCrowdsale.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. pragma solidity ^0.4.24;
  2. import "../../math/SafeMath.sol";
  3. import "../Crowdsale.sol";
  4. /**
  5. * @title TimedCrowdsale
  6. * @dev Crowdsale accepting contributions only within a time frame.
  7. */
  8. contract TimedCrowdsale is Crowdsale {
  9. using SafeMath for uint256;
  10. uint256 public openingTime;
  11. uint256 public closingTime;
  12. /**
  13. * @dev Reverts if not in crowdsale time range.
  14. */
  15. modifier onlyWhileOpen {
  16. require(isOpen());
  17. _;
  18. }
  19. /**
  20. * @dev Constructor, takes crowdsale opening and closing times.
  21. * @param _openingTime Crowdsale opening time
  22. * @param _closingTime Crowdsale closing time
  23. */
  24. constructor(uint256 _openingTime, uint256 _closingTime) public {
  25. // solium-disable-next-line security/no-block-members
  26. require(_openingTime >= block.timestamp);
  27. require(_closingTime >= _openingTime);
  28. openingTime = _openingTime;
  29. closingTime = _closingTime;
  30. }
  31. /**
  32. * @return true if the crowdsale is open, false otherwise.
  33. */
  34. function isOpen() public view returns (bool) {
  35. // solium-disable-next-line security/no-block-members
  36. return block.timestamp >= openingTime && block.timestamp <= closingTime;
  37. }
  38. /**
  39. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  40. * @return Whether crowdsale period has elapsed
  41. */
  42. function hasClosed() public view returns (bool) {
  43. // solium-disable-next-line security/no-block-members
  44. return block.timestamp > closingTime;
  45. }
  46. /**
  47. * @dev Extend parent behavior requiring to be within contributing period
  48. * @param _beneficiary Token purchaser
  49. * @param _weiAmount Amount of wei contributed
  50. */
  51. function _preValidatePurchase(
  52. address _beneficiary,
  53. uint256 _weiAmount
  54. )
  55. internal
  56. onlyWhileOpen
  57. {
  58. super._preValidatePurchase(_beneficiary, _weiAmount);
  59. }
  60. }