Pausable.sol 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
  3. pragma solidity ^0.8.19;
  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 Initializes the contract in unpaused state.
  34. */
  35. constructor() {
  36. _paused = false;
  37. }
  38. /**
  39. * @dev Modifier to make a function callable only when the contract is not paused.
  40. *
  41. * Requirements:
  42. *
  43. * - The contract must not be paused.
  44. */
  45. modifier whenNotPaused() {
  46. _requireNotPaused();
  47. _;
  48. }
  49. /**
  50. * @dev Modifier to make a function callable only when the contract is paused.
  51. *
  52. * Requirements:
  53. *
  54. * - The contract must be paused.
  55. */
  56. modifier whenPaused() {
  57. _requirePaused();
  58. _;
  59. }
  60. /**
  61. * @dev Returns true if the contract is paused, and false otherwise.
  62. */
  63. function paused() public view virtual returns (bool) {
  64. return _paused;
  65. }
  66. /**
  67. * @dev Throws if the contract is paused.
  68. */
  69. function _requireNotPaused() internal view virtual {
  70. if (paused()) {
  71. revert EnforcedPause();
  72. }
  73. }
  74. /**
  75. * @dev Throws if the contract is not paused.
  76. */
  77. function _requirePaused() internal view virtual {
  78. if (!paused()) {
  79. revert ExpectedPause();
  80. }
  81. }
  82. /**
  83. * @dev Triggers stopped state.
  84. *
  85. * Requirements:
  86. *
  87. * - The contract must not be paused.
  88. */
  89. function _pause() internal virtual whenNotPaused {
  90. _paused = true;
  91. emit Paused(_msgSender());
  92. }
  93. /**
  94. * @dev Returns to normal state.
  95. *
  96. * Requirements:
  97. *
  98. * - The contract must be paused.
  99. */
  100. function _unpause() internal virtual whenPaused {
  101. _paused = false;
  102. emit Unpaused(_msgSender());
  103. }
  104. }