Timers.sol 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/Timers.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Tooling for timepoints, timers and delays
  6. */
  7. library Timers {
  8. struct Timestamp {
  9. uint64 _deadline;
  10. }
  11. function getDeadline(Timestamp memory timer) internal pure returns (uint64) {
  12. return timer._deadline;
  13. }
  14. function setDeadline(Timestamp storage timer, uint64 timestamp) internal {
  15. timer._deadline = timestamp;
  16. }
  17. function reset(Timestamp storage timer) internal {
  18. timer._deadline = 0;
  19. }
  20. function isUnset(Timestamp memory timer) internal pure returns (bool) {
  21. return timer._deadline == 0;
  22. }
  23. function isStarted(Timestamp memory timer) internal pure returns (bool) {
  24. return timer._deadline > 0;
  25. }
  26. function isPending(Timestamp memory timer) internal view returns (bool) {
  27. return timer._deadline > block.timestamp;
  28. }
  29. function isExpired(Timestamp memory timer) internal view returns (bool) {
  30. return isStarted(timer) && timer._deadline <= block.timestamp;
  31. }
  32. struct BlockNumber {
  33. uint64 _deadline;
  34. }
  35. function getDeadline(BlockNumber memory timer) internal pure returns (uint64) {
  36. return timer._deadline;
  37. }
  38. function setDeadline(BlockNumber storage timer, uint64 timestamp) internal {
  39. timer._deadline = timestamp;
  40. }
  41. function reset(BlockNumber storage timer) internal {
  42. timer._deadline = 0;
  43. }
  44. function isUnset(BlockNumber memory timer) internal pure returns (bool) {
  45. return timer._deadline == 0;
  46. }
  47. function isStarted(BlockNumber memory timer) internal pure returns (bool) {
  48. return timer._deadline > 0;
  49. }
  50. function isPending(BlockNumber memory timer) internal view returns (bool) {
  51. return timer._deadline > block.number;
  52. }
  53. function isExpired(BlockNumber memory timer) internal view returns (bool) {
  54. return isStarted(timer) && timer._deadline <= block.number;
  55. }
  56. }