Pausable.sol 2.6 KB

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