ERC1155PresetMinterPauser.sol 3.5 KB

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