ERC721PresetMinterPauserAutoId.sol 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC721.sol";
  5. import "../extensions/ERC721Enumerable.sol";
  6. import "../extensions/ERC721Burnable.sol";
  7. import "../extensions/ERC721Pausable.sol";
  8. import "../../../access/AccessControlEnumerable.sol";
  9. import "../../../utils/Context.sol";
  10. import "../../../utils/Counters.sol";
  11. /**
  12. * @dev {ERC721} token, including:
  13. *
  14. * - ability for holders to burn (destroy) their tokens
  15. * - a minter role that allows for token minting (creation)
  16. * - a pauser role that allows to stop all token transfers
  17. * - token ID and URI autogeneration
  18. *
  19. * This contract uses {AccessControl} to lock permissioned functions using the
  20. * different roles - head to its documentation for details.
  21. *
  22. * The account that deploys the contract will be granted the minter and pauser
  23. * roles, as well as the default admin role, which will let it grant both minter
  24. * and pauser roles to other accounts.
  25. */
  26. contract ERC721PresetMinterPauserAutoId is
  27. Context,
  28. AccessControlEnumerable,
  29. ERC721Enumerable,
  30. ERC721Burnable,
  31. ERC721Pausable
  32. {
  33. using Counters for Counters.Counter;
  34. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  35. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  36. Counters.Counter private _tokenIdTracker;
  37. string private _baseTokenURI;
  38. /**
  39. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  40. * account that deploys the contract.
  41. *
  42. * Token URIs will be autogenerated based on `baseURI` and their token IDs.
  43. * See {ERC721-tokenURI}.
  44. */
  45. constructor(
  46. string memory name,
  47. string memory symbol,
  48. string memory baseTokenURI
  49. ) ERC721(name, symbol) {
  50. _baseTokenURI = baseTokenURI;
  51. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  52. _setupRole(MINTER_ROLE, _msgSender());
  53. _setupRole(PAUSER_ROLE, _msgSender());
  54. }
  55. function _baseURI() internal view virtual override returns (string memory) {
  56. return _baseTokenURI;
  57. }
  58. /**
  59. * @dev Creates a new token for `to`. Its token ID will be automatically
  60. * assigned (and available on the emitted {IERC721-Transfer} event), and the token
  61. * URI autogenerated based on the base URI passed at construction.
  62. *
  63. * See {ERC721-_mint}.
  64. *
  65. * Requirements:
  66. *
  67. * - the caller must have the `MINTER_ROLE`.
  68. */
  69. function mint(address to) public virtual {
  70. require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
  71. // We cannot just use balanceOf to create the new tokenId because tokens
  72. // can be burned (destroyed), so we need a separate counter.
  73. _mint(to, _tokenIdTracker.current());
  74. _tokenIdTracker.increment();
  75. }
  76. /**
  77. * @dev Pauses all token transfers.
  78. *
  79. * See {ERC721Pausable} and {Pausable-_pause}.
  80. *
  81. * Requirements:
  82. *
  83. * - the caller must have the `PAUSER_ROLE`.
  84. */
  85. function pause() public virtual {
  86. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
  87. _pause();
  88. }
  89. /**
  90. * @dev Unpauses all token transfers.
  91. *
  92. * See {ERC721Pausable} and {Pausable-_unpause}.
  93. *
  94. * Requirements:
  95. *
  96. * - the caller must have the `PAUSER_ROLE`.
  97. */
  98. function unpause() public virtual {
  99. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause");
  100. _unpause();
  101. }
  102. function _beforeTokenTransfer(
  103. address from,
  104. address to,
  105. uint256 tokenId
  106. ) internal virtual override(ERC721, ERC721Enumerable, ERC721Pausable) {
  107. super._beforeTokenTransfer(from, to, tokenId);
  108. }
  109. /**
  110. * @dev See {IERC165-supportsInterface}.
  111. */
  112. function supportsInterface(bytes4 interfaceId)
  113. public
  114. view
  115. virtual
  116. override(AccessControlEnumerable, ERC721, ERC721Enumerable)
  117. returns (bool)
  118. {
  119. return super.supportsInterface(interfaceId);
  120. }
  121. }