ERC1155PresetMinterPauser.sol 3.7 KB

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