ERC1155PresetMinterPauser.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC1155.sol";
  4. import "../extensions/ERC1155Burnable.sol";
  5. import "../extensions/ERC1155Pausable.sol";
  6. import "../../../access/AccessControlEnumerable.sol";
  7. import "../../../utils/Context.sol";
  8. /**
  9. * @dev {ERC1155} token, including:
  10. *
  11. * - ability for holders to burn (destroy) their tokens
  12. * - a minter role that allows for token minting (creation)
  13. * - a pauser role that allows to stop all token transfers
  14. *
  15. * This contract uses {AccessControl} to lock permissioned functions using the
  16. * different roles - head to its documentation for details.
  17. *
  18. * The account that deploys the contract will be granted the minter and pauser
  19. * roles, as well as the default admin role, which will let it grant both minter
  20. * and pauser roles to other accounts.
  21. */
  22. contract ERC1155PresetMinterPauser is Context, AccessControlEnumerable, ERC1155Burnable, ERC1155Pausable {
  23. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  24. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  25. /**
  26. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE`, and `PAUSER_ROLE` to the account that
  27. * deploys the contract.
  28. */
  29. constructor(string memory uri) ERC1155(uri) {
  30. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  31. _setupRole(MINTER_ROLE, _msgSender());
  32. _setupRole(PAUSER_ROLE, _msgSender());
  33. }
  34. /**
  35. * @dev Creates `amount` new tokens for `to`, of token type `id`.
  36. *
  37. * See {ERC1155-_mint}.
  38. *
  39. * Requirements:
  40. *
  41. * - the caller must have the `MINTER_ROLE`.
  42. */
  43. function mint(
  44. address to,
  45. uint256 id,
  46. uint256 amount,
  47. bytes memory data
  48. ) public virtual {
  49. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  50. _mint(to, id, amount, data);
  51. }
  52. /**
  53. * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] variant of {mint}.
  54. */
  55. function mintBatch(
  56. address to,
  57. uint256[] memory ids,
  58. uint256[] memory amounts,
  59. bytes memory data
  60. ) public virtual {
  61. require(hasRole(MINTER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have minter role to mint");
  62. _mintBatch(to, ids, amounts, data);
  63. }
  64. /**
  65. * @dev Pauses all token transfers.
  66. *
  67. * See {ERC1155Pausable} and {Pausable-_pause}.
  68. *
  69. * Requirements:
  70. *
  71. * - the caller must have the `PAUSER_ROLE`.
  72. */
  73. function pause() public virtual {
  74. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to pause");
  75. _pause();
  76. }
  77. /**
  78. * @dev Unpauses all token transfers.
  79. *
  80. * See {ERC1155Pausable} and {Pausable-_unpause}.
  81. *
  82. * Requirements:
  83. *
  84. * - the caller must have the `PAUSER_ROLE`.
  85. */
  86. function unpause() public virtual {
  87. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC1155PresetMinterPauser: must have pauser role to unpause");
  88. _unpause();
  89. }
  90. /**
  91. * @dev See {IERC165-supportsInterface}.
  92. */
  93. function supportsInterface(bytes4 interfaceId)
  94. public
  95. view
  96. virtual
  97. override(AccessControlEnumerable, ERC1155)
  98. returns (bool)
  99. {
  100. return super.supportsInterface(interfaceId);
  101. }
  102. function _beforeTokenTransfer(
  103. address operator,
  104. address from,
  105. address to,
  106. uint256[] memory ids,
  107. uint256[] memory amounts,
  108. bytes memory data
  109. ) internal virtual override(ERC1155, ERC1155Pausable) {
  110. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  111. }
  112. }