TimedCrowdsale.sol 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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) 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 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(address beneficiary, uint256 weiAmount) internal onlyWhileOpen view {
  64. super._preValidatePurchase(beneficiary, weiAmount);
  65. }
  66. }