Pausable.sol 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
  3. pragma solidity ^0.8.19;
  4. import "../utils/Context.sol";
  5. /**
  6. * @dev Pausable Errors library. Kept separate from {Pausable} to avoid
  7. * an identifier clash with {Pausable}'s event identifiers.
  8. */
  9. library PausableErrors {
  10. /**
  11. * @dev The contract is paused.
  12. */
  13. error Paused();
  14. /**
  15. * @dev The contract is not paused.
  16. */
  17. error Unpaused();
  18. }
  19. /**
  20. * @dev Contract module which allows children to implement an emergency stop
  21. * mechanism that can be triggered by an authorized account.
  22. *
  23. * This module is used through inheritance. It will make available the
  24. * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
  25. * the functions of your contract. Note that they will not be pausable by
  26. * simply including this module, only once the modifiers are put in place.
  27. */
  28. abstract contract Pausable is Context {
  29. /**
  30. * @dev Emitted when the pause is triggered by `account`.
  31. */
  32. event Paused(address account);
  33. /**
  34. * @dev Emitted when the pause is lifted by `account`.
  35. */
  36. event Unpaused(address account);
  37. bool private _paused;
  38. /**
  39. * @dev Initializes the contract in unpaused state.
  40. */
  41. constructor() {
  42. _paused = false;
  43. }
  44. /**
  45. * @dev Modifier to make a function callable only when the contract is not paused.
  46. *
  47. * Requirements:
  48. *
  49. * - The contract must not be paused.
  50. */
  51. modifier whenNotPaused() {
  52. _requireNotPaused();
  53. _;
  54. }
  55. /**
  56. * @dev Modifier to make a function callable only when the contract is paused.
  57. *
  58. * Requirements:
  59. *
  60. * - The contract must be paused.
  61. */
  62. modifier whenPaused() {
  63. _requirePaused();
  64. _;
  65. }
  66. /**
  67. * @dev Returns true if the contract is paused, and false otherwise.
  68. */
  69. function paused() public view virtual returns (bool) {
  70. return _paused;
  71. }
  72. /**
  73. * @dev Throws if the contract is paused.
  74. */
  75. function _requireNotPaused() internal view virtual {
  76. if (paused()) {
  77. revert PausableErrors.Paused();
  78. }
  79. }
  80. /**
  81. * @dev Throws if the contract is not paused.
  82. */
  83. function _requirePaused() internal view virtual {
  84. if (!paused()) {
  85. revert PausableErrors.Unpaused();
  86. }
  87. }
  88. /**
  89. * @dev Triggers stopped state.
  90. *
  91. * Requirements:
  92. *
  93. * - The contract must not be paused.
  94. */
  95. function _pause() internal virtual whenNotPaused {
  96. _paused = true;
  97. emit Paused(_msgSender());
  98. }
  99. /**
  100. * @dev Returns to normal state.
  101. *
  102. * Requirements:
  103. *
  104. * - The contract must be paused.
  105. */
  106. function _unpause() internal virtual whenPaused {
  107. _paused = false;
  108. emit Unpaused(_msgSender());
  109. }
  110. }