TimedCrowdsale.sol 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 private _openingTime;
  11. uint256 private _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) internal {
  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 the crowdsale opening time.
  33. */
  34. function openingTime() public view returns(uint256) {
  35. return _openingTime;
  36. }
  37. /**
  38. * @return the crowdsale closing time.
  39. */
  40. function closingTime() public view returns(uint256) {
  41. return _closingTime;
  42. }
  43. /**
  44. * @return true if the crowdsale is open, false otherwise.
  45. */
  46. function isOpen() public view returns (bool) {
  47. // solium-disable-next-line security/no-block-members
  48. return block.timestamp >= _openingTime && block.timestamp <= _closingTime;
  49. }
  50. /**
  51. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  52. * @return Whether crowdsale period has elapsed
  53. */
  54. function hasClosed() public view returns (bool) {
  55. // solium-disable-next-line security/no-block-members
  56. return block.timestamp > _closingTime;
  57. }
  58. /**
  59. * @dev Extend parent behavior requiring to be within contributing period
  60. * @param beneficiary Token purchaser
  61. * @param weiAmount Amount of wei contributed
  62. */
  63. function _preValidatePurchase(
  64. address beneficiary,
  65. uint256 weiAmount
  66. )
  67. internal
  68. onlyWhileOpen
  69. view
  70. {
  71. super._preValidatePurchase(beneficiary, weiAmount);
  72. }
  73. }