TimedCrowdsale.sol 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. require(block.timestamp >= openingTime && block.timestamp <= closingTime);
  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. function TimedCrowdsale(uint256 _openingTime, uint256 _closingTime) public {
  25. require(_openingTime >= block.timestamp);
  26. require(_closingTime >= _openingTime);
  27. openingTime = _openingTime;
  28. closingTime = _closingTime;
  29. }
  30. /**
  31. * @dev Checks whether the period in which the crowdsale is open has already elapsed.
  32. * @return Whether crowdsale period has elapsed
  33. */
  34. function hasClosed() public view returns (bool) {
  35. return block.timestamp > closingTime;
  36. }
  37. /**
  38. * @dev Extend parent behavior requiring to be within contributing period
  39. * @param _beneficiary Token purchaser
  40. * @param _weiAmount Amount of wei contributed
  41. */
  42. function _preValidatePurchase(address _beneficiary, uint256 _weiAmount) internal onlyWhileOpen {
  43. super._preValidatePurchase(_beneficiary, _weiAmount);
  44. }
  45. }