ERC721PresetMinterPauserAutoId.sol 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "../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. * _Deprecated in favor of https://wizard.openzeppelin.com/[Contracts Wizard]._
  27. */
  28. contract ERC721PresetMinterPauserAutoId is
  29. Context,
  30. AccessControlEnumerable,
  31. ERC721Enumerable,
  32. ERC721Burnable,
  33. ERC721Pausable
  34. {
  35. using Counters for Counters.Counter;
  36. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  37. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  38. Counters.Counter private _tokenIdTracker;
  39. string private _baseTokenURI;
  40. /**
  41. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  42. * account that deploys the contract.
  43. *
  44. * Token URIs will be autogenerated based on `baseURI` and their token IDs.
  45. * See {ERC721-tokenURI}.
  46. */
  47. constructor(
  48. string memory name,
  49. string memory symbol,
  50. string memory baseTokenURI
  51. ) ERC721(name, symbol) {
  52. _baseTokenURI = baseTokenURI;
  53. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  54. _setupRole(MINTER_ROLE, _msgSender());
  55. _setupRole(PAUSER_ROLE, _msgSender());
  56. }
  57. function _baseURI() internal view virtual override returns (string memory) {
  58. return _baseTokenURI;
  59. }
  60. /**
  61. * @dev Creates a new token for `to`. Its token ID will be automatically
  62. * assigned (and available on the emitted {IERC721-Transfer} event), and the token
  63. * URI autogenerated based on the base URI passed at construction.
  64. *
  65. * See {ERC721-_mint}.
  66. *
  67. * Requirements:
  68. *
  69. * - the caller must have the `MINTER_ROLE`.
  70. */
  71. function mint(address to) public virtual {
  72. require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
  73. // We cannot just use balanceOf to create the new tokenId because tokens
  74. // can be burned (destroyed), so we need a separate counter.
  75. _mint(to, _tokenIdTracker.current());
  76. _tokenIdTracker.increment();
  77. }
  78. /**
  79. * @dev Pauses all token transfers.
  80. *
  81. * See {ERC721Pausable} and {Pausable-_pause}.
  82. *
  83. * Requirements:
  84. *
  85. * - the caller must have the `PAUSER_ROLE`.
  86. */
  87. function pause() public virtual {
  88. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
  89. _pause();
  90. }
  91. /**
  92. * @dev Unpauses all token transfers.
  93. *
  94. * See {ERC721Pausable} and {Pausable-_unpause}.
  95. *
  96. * Requirements:
  97. *
  98. * - the caller must have the `PAUSER_ROLE`.
  99. */
  100. function unpause() public virtual {
  101. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause");
  102. _unpause();
  103. }
  104. function _beforeTokenTransfer(
  105. address from,
  106. address to,
  107. uint256 tokenId
  108. ) internal virtual override(ERC721, ERC721Enumerable, ERC721Pausable) {
  109. super._beforeTokenTransfer(from, to, tokenId);
  110. }
  111. function _beforeConsecutiveTokenTransfer(
  112. address from,
  113. address to,
  114. uint256 first,
  115. uint96 size
  116. ) internal virtual override(ERC721, ERC721Enumerable, ERC721Pausable) {
  117. super._beforeConsecutiveTokenTransfer(from, to, first, size);
  118. }
  119. /**
  120. * @dev See {IERC165-supportsInterface}.
  121. */
  122. function supportsInterface(bytes4 interfaceId)
  123. public
  124. view
  125. virtual
  126. override(AccessControlEnumerable, ERC721, ERC721Enumerable)
  127. returns (bool)
  128. {
  129. return super.supportsInterface(interfaceId);
  130. }
  131. }