ERC20PresetMinterPauserUpgradeable.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (token/ERC20/presets/ERC20PresetMinterPauser.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC20Upgradeable.sol";
  5. import "../extensions/ERC20BurnableUpgradeable.sol";
  6. import "../extensions/ERC20PausableUpgradeable.sol";
  7. import "../../../access/AccessControlEnumerableUpgradeable.sol";
  8. import "../../../utils/ContextUpgradeable.sol";
  9. import "../../../proxy/utils/Initializable.sol";
  10. /**
  11. * @dev {ERC20} token, including:
  12. *
  13. * - ability for holders to burn (destroy) their tokens
  14. * - a minter role that allows for token minting (creation)
  15. * - a pauser role that allows to stop all token transfers
  16. *
  17. * This contract uses {AccessControl} to lock permissioned functions using the
  18. * different roles - head to its documentation for details.
  19. *
  20. * The account that deploys the contract will be granted the minter and pauser
  21. * roles, as well as the default admin role, which will let it grant both minter
  22. * and pauser roles to other accounts.
  23. *
  24. * _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
  25. */
  26. contract ERC20PresetMinterPauserUpgradeable is Initializable, ContextUpgradeable, AccessControlEnumerableUpgradeable, ERC20BurnableUpgradeable, ERC20PausableUpgradeable {
  27. function initialize(string memory name, string memory symbol) public virtual initializer {
  28. __ERC20PresetMinterPauser_init(name, symbol);
  29. }
  30. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  31. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  32. /**
  33. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  34. * account that deploys the contract.
  35. *
  36. * See {ERC20-constructor}.
  37. */
  38. function __ERC20PresetMinterPauser_init(string memory name, string memory symbol) internal onlyInitializing {
  39. __ERC20_init_unchained(name, symbol);
  40. __Pausable_init_unchained();
  41. __ERC20PresetMinterPauser_init_unchained(name, symbol);
  42. }
  43. function __ERC20PresetMinterPauser_init_unchained(string memory, string memory) internal onlyInitializing {
  44. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  45. _setupRole(MINTER_ROLE, _msgSender());
  46. _setupRole(PAUSER_ROLE, _msgSender());
  47. }
  48. /**
  49. * @dev Creates `amount` new tokens for `to`.
  50. *
  51. * See {ERC20-_mint}.
  52. *
  53. * Requirements:
  54. *
  55. * - the caller must have the `MINTER_ROLE`.
  56. */
  57. function mint(address to, uint256 amount) public virtual {
  58. require(hasRole(MINTER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have minter role to mint");
  59. _mint(to, amount);
  60. }
  61. /**
  62. * @dev Pauses all token transfers.
  63. *
  64. * See {ERC20Pausable} and {Pausable-_pause}.
  65. *
  66. * Requirements:
  67. *
  68. * - the caller must have the `PAUSER_ROLE`.
  69. */
  70. function pause() public virtual {
  71. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to pause");
  72. _pause();
  73. }
  74. /**
  75. * @dev Unpauses all token transfers.
  76. *
  77. * See {ERC20Pausable} and {Pausable-_unpause}.
  78. *
  79. * Requirements:
  80. *
  81. * - the caller must have the `PAUSER_ROLE`.
  82. */
  83. function unpause() public virtual {
  84. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC20PresetMinterPauser: must have pauser role to unpause");
  85. _unpause();
  86. }
  87. function _beforeTokenTransfer(
  88. address from,
  89. address to,
  90. uint256 amount
  91. ) internal virtual override(ERC20Upgradeable, ERC20PausableUpgradeable) {
  92. super._beforeTokenTransfer(from, to, amount);
  93. }
  94. /**
  95. * This empty reserved space is put in place to allow future versions to add new
  96. * variables without shifting down storage in the inheritance chain.
  97. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  98. */
  99. uint256[50] private __gap;
  100. }