ERC1155PresetMinterPauser.sol 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.0;
  3. import "../access/AccessControl.sol";
  4. import "../GSN/Context.sol";
  5. import "../token/ERC1155/ERC1155.sol";
  6. import "../token/ERC1155/ERC1155Burnable.sol";
  7. import "../token/ERC1155/ERC1155Pausable.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, AccessControl, 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) public 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. function _beforeTokenTransfer(
  81. address operator,
  82. address from,
  83. address to,
  84. uint256[] memory ids,
  85. uint256[] memory amounts,
  86. bytes memory data
  87. )
  88. internal virtual override(ERC1155, ERC1155Pausable)
  89. {
  90. super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
  91. }
  92. }