ERC20PresetMinterPauser.sol 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.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. contract ERC20PresetMinterPauser is Context, AccessControlEnumerable, ERC20Burnable, ERC20Pausable {
  24. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  25. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  26. /**
  27. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  28. * account that deploys the contract.
  29. *
  30. * See {ERC20-constructor}.
  31. */
  32. constructor(string memory name, string memory symbol) ERC20(name, symbol) {
  33. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  34. _setupRole(MINTER_ROLE, _msgSender());
  35. _setupRole(PAUSER_ROLE, _msgSender());
  36. }
  37. /**
  38. * @dev Creates `amount` new tokens for `to`.
  39. *
  40. * See {ERC20-_mint}.
  41. *
  42. * Requirements:
  43. *
  44. * - the caller must have the `MINTER_ROLE`.
  45. */
  46. function mint(address to, uint256 amount) public virtual {
  47. require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
  48. _mint(to, amount);
  49. }
  50. /**
  51. * @dev Pauses all token transfers.
  52. *
  53. * See {ERC20Pausable} and {Pausable-_pause}.
  54. *
  55. * Requirements:
  56. *
  57. * - the caller must have the `PAUSER_ROLE`.
  58. */
  59. function pause() public virtual {
  60. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
  61. _pause();
  62. }
  63. /**
  64. * @dev Unpauses all token transfers.
  65. *
  66. * See {ERC20Pausable} and {Pausable-_unpause}.
  67. *
  68. * Requirements:
  69. *
  70. * - the caller must have the `PAUSER_ROLE`.
  71. */
  72. function unpause() public virtual {
  73. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
  74. _unpause();
  75. }
  76. function _beforeTokenTransfer(
  77. address from,
  78. address to,
  79. uint256 amount
  80. ) internal virtual override(ERC20, ERC20Pausable) {
  81. super._beforeTokenTransfer(from, to, amount);
  82. }
  83. }