Timers.sol 2.1 KB

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