Pausable.sol 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)
  3. pragma solidity ^0.8.0;
  4. import "../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. /**
  16. * @dev Emitted when the pause is triggered by `account`.
  17. */
  18. event Paused(address account);
  19. /**
  20. * @dev Emitted when the pause is lifted by `account`.
  21. */
  22. event Unpaused(address account);
  23. bool private _paused;
  24. /**
  25. * @dev Initializes the contract in unpaused state.
  26. */
  27. constructor() {
  28. _paused = false;
  29. }
  30. /**
  31. * @dev Modifier to make a function callable only when the contract is not paused.
  32. *
  33. * Requirements:
  34. *
  35. * - The contract must not be paused.
  36. */
  37. modifier whenNotPaused() {
  38. _requireNotPaused();
  39. _;
  40. }
  41. /**
  42. * @dev Modifier to make a function callable only when the contract is paused.
  43. *
  44. * Requirements:
  45. *
  46. * - The contract must be paused.
  47. */
  48. modifier whenPaused() {
  49. _requirePaused();
  50. _;
  51. }
  52. /**
  53. * @dev Returns true if the contract is paused, and false otherwise.
  54. */
  55. function paused() public view virtual returns (bool) {
  56. return _paused;
  57. }
  58. /**
  59. * @dev Throws if the contract is paused.
  60. */
  61. function _requireNotPaused() internal view virtual {
  62. require(!paused(), "Pausable: paused");
  63. }
  64. /**
  65. * @dev Throws if the contract is not paused.
  66. */
  67. function _requirePaused() internal view virtual {
  68. require(paused(), "Pausable: not paused");
  69. }
  70. /**
  71. * @dev Triggers stopped state.
  72. *
  73. * Requirements:
  74. *
  75. * - The contract must not be paused.
  76. */
  77. function _pause() internal virtual whenNotPaused {
  78. _paused = true;
  79. emit Paused(_msgSender());
  80. }
  81. /**
  82. * @dev Returns to normal state.
  83. *
  84. * Requirements:
  85. *
  86. * - The contract must be paused.
  87. */
  88. function _unpause() internal virtual whenPaused {
  89. _paused = false;
  90. emit Unpaused(_msgSender());
  91. }
  92. }