Pausable.sol 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. import "./Context.sol";
  4. /**
  5. * @dev Contract module which allows children to implement an emergency stop
  6. * mechanism that can be triggered by an authorized account.
  7. *
  8. * This module is used through inheritance. It will make available the
  9. * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
  10. * the functions of your contract. Note that they will not be pausable by
  11. * simply including this module, only once the modifiers are put in place.
  12. */
  13. abstract contract Pausable is Context {
  14. /**
  15. * @dev Emitted when the pause is triggered by `account`.
  16. */
  17. event Paused(address account);
  18. /**
  19. * @dev Emitted when the pause is lifted by `account`.
  20. */
  21. event Unpaused(address account);
  22. bool private _paused;
  23. /**
  24. * @dev Initializes the contract in unpaused state.
  25. */
  26. constructor () internal {
  27. _paused = false;
  28. }
  29. /**
  30. * @dev Returns true if the contract is paused, and false otherwise.
  31. */
  32. function paused() public view virtual returns (bool) {
  33. return _paused;
  34. }
  35. /**
  36. * @dev Modifier to make a function callable only when the contract is not paused.
  37. *
  38. * Requirements:
  39. *
  40. * - The contract must not be paused.
  41. */
  42. modifier whenNotPaused() {
  43. require(!paused(), "Pausable: paused");
  44. _;
  45. }
  46. /**
  47. * @dev Modifier to make a function callable only when the contract is paused.
  48. *
  49. * Requirements:
  50. *
  51. * - The contract must be paused.
  52. */
  53. modifier whenPaused() {
  54. require(paused(), "Pausable: not paused");
  55. _;
  56. }
  57. /**
  58. * @dev Triggers stopped state.
  59. *
  60. * Requirements:
  61. *
  62. * - The contract must not be paused.
  63. */
  64. function _pause() internal virtual whenNotPaused {
  65. _paused = true;
  66. emit Paused(_msgSender());
  67. }
  68. /**
  69. * @dev Returns to normal state.
  70. *
  71. * Requirements:
  72. *
  73. * - The contract must be paused.
  74. */
  75. function _unpause() internal virtual whenPaused {
  76. _paused = false;
  77. emit Unpaused(_msgSender());
  78. }
  79. }