Timers.sol 1.9 KB

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