ERC20MinterPauser.sol 2.6 KB

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