Pausable.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. pragma solidity ^0.5.0;
  2. import "../access/roles/PauserRole.sol";
  3. /**
  4. * @dev Contract module which allows children to implement an emergency stop
  5. * mechanism that can be triggered by an authorized account.
  6. *
  7. * This module is used through inheritance. It will make available the
  8. * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
  9. * the functions of your contract. Note that they will not be pausable by
  10. * simply including this module, only once the modifiers are put in place.
  11. */
  12. contract Pausable is PauserRole {
  13. /**
  14. * @dev Emitted when the pause is triggered by a pauser (`account`).
  15. */
  16. event Paused(address account);
  17. /**
  18. * @dev Emitted when the pause is lifted by a pauser (`account`).
  19. */
  20. event Unpaused(address account);
  21. bool private _paused;
  22. /**
  23. * @dev Initializes the contract in unpaused state. Assigns the Pauser role
  24. * to the deployer.
  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 returns (bool) {
  33. return _paused;
  34. }
  35. /**
  36. * @dev Modifier to make a function callable only when the contract is not paused.
  37. */
  38. modifier whenNotPaused() {
  39. require(!_paused, "Pausable: paused");
  40. _;
  41. }
  42. /**
  43. * @dev Modifier to make a function callable only when the contract is paused.
  44. */
  45. modifier whenPaused() {
  46. require(_paused, "Pausable: not paused");
  47. _;
  48. }
  49. /**
  50. * @dev Called by a pauser to pause, triggers stopped state.
  51. */
  52. function pause() public onlyPauser whenNotPaused {
  53. _paused = true;
  54. emit Paused(msg.sender);
  55. }
  56. /**
  57. * @dev Called by a pauser to unpause, returns to normal state.
  58. */
  59. function unpause() public onlyPauser whenPaused {
  60. _paused = false;
  61. emit Unpaused(msg.sender);
  62. }
  63. }