ERC1155PresetMinterPauser.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/presets/ERC1155PresetMinterPauser.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC1155.sol";
  5. import "../extensions/ERC1155Burnable.sol";
  6. import "../extensions/ERC1155Pausable.sol";
  7. import "../../../access/AccessControlEnumerable.sol";
  8. import "../../../utils/Context.sol";
  9. /**
  10. * @dev {ERC1155} 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 ERC1155PresetMinterPauser is Context, AccessControlEnumerable, ERC1155Burnable, ERC1155Pausable {
  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 account that
  30. * deploys the contract.
  31. */
  32. constructor(string memory uri) ERC1155(uri) {
  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`, of token type `id`.
  39. *
  40. * See {ERC1155-_mint}.
  41. *
  42. * Requirements:
  43. *
  44. * - the caller must have the `MINTER_ROLE`.
  45. */
  46. function mint(
  47. address to,
  48. uint256 id,
  49. uint256 amount,
  50. bytes memory data
  51. ) public virtual {
  52. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  53. _mint(to, id, amount, data);
  54. }
  55. /**
  56. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}.
  57. */
  58. function mintBatch(
  59. address to,
  60. uint256[] memory ids,
  61. uint256[] memory amounts,
  62. bytes memory data
  63. ) public virtual {
  64. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  65. _mintBatch(to, ids, amounts, data);
  66. }
  67. /**
  68. * @dev Pauses all token transfers.
  69. *
  70. * See {ERC1155Pausable} and {Pausable-_pause}.
  71. *
  72. * Requirements:
  73. *
  74. * - the caller must have the `PAUSER_ROLE`.
  75. */
  76. function pause() public virtual {
  77. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause");
  78. _pause();
  79. }
  80. /**
  81. * @dev Unpauses all token transfers.
  82. *
  83. * See {ERC1155Pausable} and {Pausable-_unpause}.
  84. *
  85. * Requirements:
  86. *
  87. * - the caller must have the `PAUSER_ROLE`.
  88. */
  89. function unpause() public virtual {
  90. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause");
  91. _unpause();
  92. }
  93. /**
  94. * @dev See {IERC165-supportsInterface}.
  95. */
  96. function supportsInterface(bytes4 interfaceId)
  97. public
  98. view
  99. virtual
  100. override(AccessControlEnumerable, ERC1155)
  101. returns (bool)
  102. {
  103. return super.supportsInterface(interfaceId);
  104. }
  105. function _beforeTokenTransfer(
  106. address operator,
  107. address from,
  108. address to,
  109. uint256[] memory ids,
  110. uint256[] memory amounts,
  111. bytes memory data
  112. ) internal virtual override(ERC1155, ERC1155Pausable) {
  113. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  114. }
  115. }