ERC20PresetMinterPauser.sol 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC20.sol";
  5. import "../extensions/ERC20Burnable.sol";
  6. import "../extensions/ERC20Pausable.sol";
  7. import "../../../access/AccessControlEnumerable.sol";
  8. import "../../../utils/Context.sol";
  9. /**
  10. * @dev {ERC20} token, including:
  11. *
  12. * - ability for holders to burn (destroy) their tokens
  13. * - a minter role that allows for token minting (creation)
  14. * - a pauser role that allows to stop all token transfers
  15. *
  16. * This contract uses {AccessControl} to lock permissioned functions using the
  17. * different roles - head to its documentation for details.
  18. *
  19. * The account that deploys the contract will be granted the minter and pauser
  20. * roles, as well as the default admin role, which will let it grant both minter
  21. * and pauser roles to other accounts.
  22. *
  23. * _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
  24. */
  25. contract ERC20PresetMinterPauser is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
  26. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  27. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  28. /**
  29. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  30. * account that deploys the contract.
  31. *
  32. * See {ERC20-constructor}.
  33. */
  34. constructor(string memory name, string memory symbol) ERC20(name, symbol) {
  35. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  36. _setupRole(MINTER_ROLE, _msgSender());
  37. _setupRole(PAUSER_ROLE, _msgSender());
  38. }
  39. /**
  40. * @dev Creates `amount` new tokens for `to`.
  41. *
  42. * See {ERC20-_mint}.
  43. *
  44. * Requirements:
  45. *
  46. * - the caller must have the `MINTER_ROLE`.
  47. */
  48. function mint(address to, uint256 amount) public virtual {
  49. require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
  50. _mint(to, amount);
  51. }
  52. /**
  53. * @dev Pauses all token transfers.
  54. *
  55. * See {ERC20Pausable} and {Pausable-_pause}.
  56. *
  57. * Requirements:
  58. *
  59. * - the caller must have the `PAUSER_ROLE`.
  60. */
  61. function pause() public virtual {
  62. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
  63. _pause();
  64. }
  65. /**
  66. * @dev Unpauses all token transfers.
  67. *
  68. * See {ERC20Pausable} and {Pausable-_unpause}.
  69. *
  70. * Requirements:
  71. *
  72. * - the caller must have the `PAUSER_ROLE`.
  73. */
  74. function unpause() public virtual {
  75. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
  76. _unpause();
  77. }
  78. function _beforeTokenTransfer(
  79. address from,
  80. address to,
  81. uint256 amount
  82. ) internal virtual override(ERC20, ERC20Pausable) {
  83. super._beforeTokenTransfer(from, to, amount);
  84. }
  85. }