ERC1155PresetMinterPauserUpgradeable.sol 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "../ERC1155Upgradeable.sol";
  5. import "../extensions/ERC1155BurnableUpgradeable.sol";
  6. import "../extensions/ERC1155PausableUpgradeable.sol";
  7. import "../../../access/AccessControlEnumerableUpgradeable.sol";
  8. import "../../../utils/ContextUpgradeable.sol";
  9. import "../../../proxy/utils/Initializable.sol";
  10. /**
  11. * @dev {ERC1155} 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 ERC1155PresetMinterPauserUpgradeable is Initializable, ContextUpgradeable, AccessControlEnumerableUpgradeable, ERC1155BurnableUpgradeable, ERC1155PausableUpgradeable {
  27. function initialize(string memory uri) public virtual initializer {
  28. __ERC1155PresetMinterPauser_init(uri);
  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 account that
  34. * deploys the contract.
  35. */
  36. function __ERC1155PresetMinterPauser_init(string memory uri) internal onlyInitializing {
  37. __ERC1155_init_unchained(uri);
  38. __Pausable_init_unchained();
  39. __ERC1155PresetMinterPauser_init_unchained(uri);
  40. }
  41. function __ERC1155PresetMinterPauser_init_unchained(string memory) internal onlyInitializing {
  42. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  43. _setupRole(MINTER_ROLE, _msgSender());
  44. _setupRole(PAUSER_ROLE, _msgSender());
  45. }
  46. /**
  47. * @dev Creates `amount` new tokens for `to`, of token type `id`.
  48. *
  49. * See {ERC1155-_mint}.
  50. *
  51. * Requirements:
  52. *
  53. * - the caller must have the `MINTER_ROLE`.
  54. */
  55. function mint(
  56. address to,
  57. uint256 id,
  58. uint256 amount,
  59. bytes memory data
  60. ) public virtual {
  61. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  62. _mint(to, id, amount, data);
  63. }
  64. /**
  65. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}.
  66. */
  67. function mintBatch(
  68. address to,
  69. uint256[] memory ids,
  70. uint256[] memory amounts,
  71. bytes memory data
  72. ) public virtual {
  73. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  74. _mintBatch(to, ids, amounts, data);
  75. }
  76. /**
  77. * @dev Pauses all token transfers.
  78. *
  79. * See {ERC1155Pausable} and {Pausable-_pause}.
  80. *
  81. * Requirements:
  82. *
  83. * - the caller must have the `PAUSER_ROLE`.
  84. */
  85. function pause() public virtual {
  86. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause");
  87. _pause();
  88. }
  89. /**
  90. * @dev Unpauses all token transfers.
  91. *
  92. * See {ERC1155Pausable} and {Pausable-_unpause}.
  93. *
  94. * Requirements:
  95. *
  96. * - the caller must have the `PAUSER_ROLE`.
  97. */
  98. function unpause() public virtual {
  99. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause");
  100. _unpause();
  101. }
  102. /**
  103. * @dev See {IERC165-supportsInterface}.
  104. */
  105. function supportsInterface(bytes4 interfaceId)
  106. public
  107. view
  108. virtual
  109. override(AccessControlEnumerableUpgradeable, ERC1155Upgradeable)
  110. returns (bool)
  111. {
  112. return super.supportsInterface(interfaceId);
  113. }
  114. function _beforeTokenTransfer(
  115. address operator,
  116. address from,
  117. address to,
  118. uint256[] memory ids,
  119. uint256[] memory amounts,
  120. bytes memory data
  121. ) internal virtual override(ERC1155Upgradeable, ERC1155PausableUpgradeable) {
  122. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  123. }
  124. /**
  125. * This empty reserved space is put in place to allow future versions to add new
  126. * variables without shifting down storage in the inheritance chain.
  127. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  128. */
  129. uint256[50] private __gap;
  130. }