TimedCrowdsale.sol 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. pragma solidity ^0.4.21;
  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. // solium-disable-next-line security/no-block-members
  17. require(block.timestamp >= openingTime && block.timestamp <= closingTime);
  18. _;
  19. }
  20. /**
  21. * @dev Constructor, takes crowdsale opening and closing times.
  22. * @param _openingTime Crowdsale opening time
  23. * @param _closingTime Crowdsale closing time
  24. */
  25. function TimedCrowdsale(uint256 _openingTime, uint256 _closingTime) public {
  26. // solium-disable-next-line security/no-block-members
  27. require(_openingTime >= block.timestamp);
  28. require(_closingTime >= _openingTime);
  29. openingTime = _openingTime;
  30. closingTime = _closingTime;
  31. }
  32. /**
  33. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  34. * @return Whether crowdsale period has elapsed
  35. */
  36. function hasClosed() public view returns (bool) {
  37. // solium-disable-next-line security/no-block-members
  38. return block.timestamp > closingTime;
  39. }
  40. /**
  41. * @dev Extend parent behavior requiring to be within contributing period
  42. * @param _beneficiary Token purchaser
  43. * @param _weiAmount Amount of wei contributed
  44. */
  45. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal onlyWhileOpen {
  46. super._preValidatePurchase(_beneficiary, _weiAmount);
  47. }
  48. }