Pausable.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0-rc.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 Returns true if the contract is paused, and false otherwise.
  32. */
  33. function paused() public view virtual returns (bool) {
  34. return _paused;
  35. }
  36. /**
  37. * @dev Modifier to make a function callable only when the contract is not paused.
  38. *
  39. * Requirements:
  40. *
  41. * - The contract must not be paused.
  42. */
  43. modifier whenNotPaused() {
  44. require(!paused(), "Pausable: paused");
  45. _;
  46. }
  47. /**
  48. * @dev Modifier to make a function callable only when the contract is paused.
  49. *
  50. * Requirements:
  51. *
  52. * - The contract must be paused.
  53. */
  54. modifier whenPaused() {
  55. require(paused(), "Pausable: not paused");
  56. _;
  57. }
  58. /**
  59. * @dev Triggers stopped state.
  60. *
  61. * Requirements:
  62. *
  63. * - The contract must not be paused.
  64. */
  65. function _pause() internal virtual whenNotPaused {
  66. _paused = true;
  67. emit Paused(_msgSender());
  68. }
  69. /**
  70. * @dev Returns to normal state.
  71. *
  72. * Requirements:
  73. *
  74. * - The contract must be paused.
  75. */
  76. function _unpause() internal virtual whenPaused {
  77. _paused = false;
  78. emit Unpaused(_msgSender());
  79. }
  80. }