TimedCrowdsale.sol 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. pragma solidity ^0.5.7;
  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. * Event for crowdsale extending
  14. * @param newClosingTime new closing time
  15. * @param prevClosingTime old closing time
  16. */
  17. event TimedCrowdsaleExtended(uint256 prevClosingTime, uint256 newClosingTime);
  18. /**
  19. * @dev Reverts if not in crowdsale time range.
  20. */
  21. modifier onlyWhileOpen {
  22. require(isOpen());
  23. _;
  24. }
  25. /**
  26. * @dev Constructor, takes crowdsale opening and closing times.
  27. * @param openingTime Crowdsale opening time
  28. * @param closingTime Crowdsale closing time
  29. */
  30. constructor (uint256 openingTime, uint256 closingTime) public {
  31. // solhint-disable-next-line not-rely-on-time
  32. require(openingTime >= block.timestamp);
  33. require(closingTime > openingTime);
  34. _openingTime = openingTime;
  35. _closingTime = closingTime;
  36. }
  37. /**
  38. * @return the crowdsale opening time.
  39. */
  40. function openingTime() public view returns (uint256) {
  41. return _openingTime;
  42. }
  43. /**
  44. * @return the crowdsale closing time.
  45. */
  46. function closingTime() public view returns (uint256) {
  47. return _closingTime;
  48. }
  49. /**
  50. * @return true if the crowdsale is open, false otherwise.
  51. */
  52. function isOpen() public view returns (bool) {
  53. // solhint-disable-next-line not-rely-on-time
  54. return block.timestamp >= _openingTime && block.timestamp <= _closingTime;
  55. }
  56. /**
  57. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  58. * @return Whether crowdsale period has elapsed
  59. */
  60. function hasClosed() public view returns (bool) {
  61. // solhint-disable-next-line not-rely-on-time
  62. return block.timestamp > _closingTime;
  63. }
  64. /**
  65. * @dev Extend parent behavior requiring to be within contributing period.
  66. * @param beneficiary Token purchaser
  67. * @param weiAmount Amount of wei contributed
  68. */
  69. function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal onlyWhileOpen view {
  70. super._preValidatePurchase(beneficiary, weiAmount);
  71. }
  72. /**
  73. * @dev Extend crowdsale.
  74. * @param newClosingTime Crowdsale closing time
  75. */
  76. function _extendTime(uint256 newClosingTime) internal {
  77. require(!hasClosed());
  78. require(newClosingTime > _closingTime);
  79. emit TimedCrowdsaleExtended(_closingTime, newClosingTime);
  80. _closingTime = newClosingTime;
  81. }
  82. }