ERC1155PresetMinterPauser.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.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(address to, uint256 id, uint256 amount, bytes memory data) public virtual {
  47. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  48. _mint(to, id, amount, data);
  49. }
  50. /**
  51. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}.
  52. */
  53. function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public virtual {
  54. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  55. _mintBatch(to, ids, amounts, data);
  56. }
  57. /**
  58. * @dev Pauses all token transfers.
  59. *
  60. * See {ERC1155Pausable} and {Pausable-_pause}.
  61. *
  62. * Requirements:
  63. *
  64. * - the caller must have the `PAUSER_ROLE`.
  65. */
  66. function pause() public virtual {
  67. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause");
  68. _pause();
  69. }
  70. /**
  71. * @dev Unpauses all token transfers.
  72. *
  73. * See {ERC1155Pausable} and {Pausable-_unpause}.
  74. *
  75. * Requirements:
  76. *
  77. * - the caller must have the `PAUSER_ROLE`.
  78. */
  79. function unpause() public virtual {
  80. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause");
  81. _unpause();
  82. }
  83. /**
  84. * @dev See {IERC165-supportsInterface}.
  85. */
  86. function supportsInterface(
  87. bytes4 interfaceId
  88. ) public view virtual override(AccessControlEnumerable, ERC1155) returns (bool) {
  89. return super.supportsInterface(interfaceId);
  90. }
  91. function _beforeTokenTransfer(
  92. address operator,
  93. address from,
  94. address to,
  95. uint256[] memory ids,
  96. uint256[] memory amounts,
  97. bytes memory data
  98. ) internal virtual override(ERC1155, ERC1155Pausable) {
  99. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  100. }
  101. }