ERC721PresetMinterPauserAutoIdUpgradeable.sol 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0) (token/ERC721/presets/ERC721PresetMinterPauserAutoId.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC721Upgradeable.sol";
  5. import "../extensions/ERC721EnumerableUpgradeable.sol";
  6. import "../extensions/ERC721BurnableUpgradeable.sol";
  7. import "../extensions/ERC721PausableUpgradeable.sol";
  8. import "../../../access/AccessControlEnumerableUpgradeable.sol";
  9. import "../../../utils/ContextUpgradeable.sol";
  10. import "../../../utils/CountersUpgradeable.sol";
  11. import "../../../proxy/utils/Initializable.sol";
  12. /**
  13. * @dev {ERC721} token, including:
  14. *
  15. * - ability for holders to burn (destroy) their tokens
  16. * - a minter role that allows for token minting (creation)
  17. * - a pauser role that allows to stop all token transfers
  18. * - token ID and URI autogeneration
  19. *
  20. * This contract uses {AccessControl} to lock permissioned functions using the
  21. * different roles - head to its documentation for details.
  22. *
  23. * The account that deploys the contract will be granted the minter and pauser
  24. * roles, as well as the default admin role, which will let it grant both minter
  25. * and pauser roles to other accounts.
  26. *
  27. * _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
  28. */
  29. contract ERC721PresetMinterPauserAutoIdUpgradeable is
  30. Initializable, ContextUpgradeable,
  31. AccessControlEnumerableUpgradeable,
  32. ERC721EnumerableUpgradeable,
  33. ERC721BurnableUpgradeable,
  34. ERC721PausableUpgradeable
  35. {
  36. function initialize(
  37. string memory name,
  38. string memory symbol,
  39. string memory baseTokenURI
  40. ) public virtual initializer {
  41. __ERC721PresetMinterPauserAutoId_init(name, symbol, baseTokenURI);
  42. }
  43. using CountersUpgradeable for CountersUpgradeable.Counter;
  44. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  45. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  46. CountersUpgradeable.Counter private _tokenIdTracker;
  47. string private _baseTokenURI;
  48. /**
  49. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  50. * account that deploys the contract.
  51. *
  52. * Token URIs will be autogenerated based on `baseURI` and their token IDs.
  53. * See {ERC721-tokenURI}.
  54. */
  55. function __ERC721PresetMinterPauserAutoId_init(
  56. string memory name,
  57. string memory symbol,
  58. string memory baseTokenURI
  59. ) internal onlyInitializing {
  60. __ERC721_init_unchained(name, symbol);
  61. __Pausable_init_unchained();
  62. __ERC721PresetMinterPauserAutoId_init_unchained(name, symbol, baseTokenURI);
  63. }
  64. function __ERC721PresetMinterPauserAutoId_init_unchained(
  65. string memory,
  66. string memory,
  67. string memory baseTokenURI
  68. ) internal onlyInitializing {
  69. _baseTokenURI = baseTokenURI;
  70. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  71. _setupRole(MINTER_ROLE, _msgSender());
  72. _setupRole(PAUSER_ROLE, _msgSender());
  73. }
  74. function _baseURI() internal view virtual override returns (string memory) {
  75. return _baseTokenURI;
  76. }
  77. /**
  78. * @dev Creates a new token for `to`. Its token ID will be automatically
  79. * assigned (and available on the emitted {IERC721-Transfer} event), and the token
  80. * URI autogenerated based on the base URI passed at construction.
  81. *
  82. * See {ERC721-_mint}.
  83. *
  84. * Requirements:
  85. *
  86. * - the caller must have the `MINTER_ROLE`.
  87. */
  88. function mint(address to) public virtual {
  89. require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
  90. // We cannot just use balanceOf to create the new tokenId because tokens
  91. // can be burned (destroyed), so we need a separate counter.
  92. _mint(to, _tokenIdTracker.current());
  93. _tokenIdTracker.increment();
  94. }
  95. /**
  96. * @dev Pauses all token transfers.
  97. *
  98. * See {ERC721Pausable} and {Pausable-_pause}.
  99. *
  100. * Requirements:
  101. *
  102. * - the caller must have the `PAUSER_ROLE`.
  103. */
  104. function pause() public virtual {
  105. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
  106. _pause();
  107. }
  108. /**
  109. * @dev Unpauses all token transfers.
  110. *
  111. * See {ERC721Pausable} and {Pausable-_unpause}.
  112. *
  113. * Requirements:
  114. *
  115. * - the caller must have the `PAUSER_ROLE`.
  116. */
  117. function unpause() public virtual {
  118. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause");
  119. _unpause();
  120. }
  121. function _beforeTokenTransfer(
  122. address from,
  123. address to,
  124. uint256 tokenId
  125. ) internal virtual override(ERC721Upgradeable, ERC721EnumerableUpgradeable, ERC721PausableUpgradeable) {
  126. super._beforeTokenTransfer(from, to, tokenId);
  127. }
  128. /**
  129. * @dev See {IERC165-supportsInterface}.
  130. */
  131. function supportsInterface(bytes4 interfaceId)
  132. public
  133. view
  134. virtual
  135. override(AccessControlEnumerableUpgradeable, ERC721Upgradeable, ERC721EnumerableUpgradeable)
  136. returns (bool)
  137. {
  138. return super.supportsInterface(interfaceId);
  139. }
  140. /**
  141. * This empty reserved space is put in place to allow future versions to add new
  142. * variables without shifting down storage in the inheritance chain.
  143. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  144. */
  145. uint256[48] private __gap;
  146. }