TimedCrowdsale.sol 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. pragma solidity ^0.5.0;
  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(), "TimedCrowdsale: not open");
  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, "TimedCrowdsale: opening time is before current time");
  33. // solhint-disable-next-line max-line-length
  34. require(closingTime > openingTime, "TimedCrowdsale: opening time is not before closing time");
  35. _openingTime = openingTime;
  36. _closingTime = closingTime;
  37. }
  38. /**
  39. * @return the crowdsale opening time.
  40. */
  41. function openingTime() public view returns (uint256) {
  42. return _openingTime;
  43. }
  44. /**
  45. * @return the crowdsale closing time.
  46. */
  47. function closingTime() public view returns (uint256) {
  48. return _closingTime;
  49. }
  50. /**
  51. * @return true if the crowdsale is open, false otherwise.
  52. */
  53. function isOpen() public view returns (bool) {
  54. // solhint-disable-next-line not-rely-on-time
  55. return block.timestamp >= _openingTime && block.timestamp <= _closingTime;
  56. }
  57. /**
  58. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  59. * @return Whether crowdsale period has elapsed
  60. */
  61. function hasClosed() public view returns (bool) {
  62. // solhint-disable-next-line not-rely-on-time
  63. return block.timestamp > _closingTime;
  64. }
  65. /**
  66. * @dev Extend parent behavior requiring to be within contributing period.
  67. * @param beneficiary Token purchaser
  68. * @param weiAmount Amount of wei contributed
  69. */
  70. function _preValidatePurchase(address beneficiary, uint256 weiAmount) internal onlyWhileOpen view {
  71. super._preValidatePurchase(beneficiary, weiAmount);
  72. }
  73. /**
  74. * @dev Extend crowdsale.
  75. * @param newClosingTime Crowdsale closing time
  76. */
  77. function _extendTime(uint256 newClosingTime) internal {
  78. require(!hasClosed(), "TimedCrowdsale: already closed");
  79. // solhint-disable-next-line max-line-length
  80. require(newClosingTime > _closingTime, "TimedCrowdsale: new closing time is before current closing time");
  81. emit TimedCrowdsaleExtended(_closingTime, newClosingTime);
  82. _closingTime = newClosingTime;
  83. }
  84. }