ERC20PresetMinterPauser.sol 2.7 KB

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