ERC721PresetMinterPauserAutoId.sol 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.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(string memory name, string memory symbol, string memory baseTokenURI) ERC721(name, symbol) {
  48. _baseTokenURI = baseTokenURI;
  49. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  50. _setupRole(MINTER_ROLE, _msgSender());
  51. _setupRole(PAUSER_ROLE, _msgSender());
  52. }
  53. function _baseURI() internal view virtual override returns (string memory) {
  54. return _baseTokenURI;
  55. }
  56. /**
  57. * @dev Creates a new token for `to`. Its token ID will be automatically
  58. * assigned (and available on the emitted {IERC721-Transfer} event), and the token
  59. * URI autogenerated based on the base URI passed at construction.
  60. *
  61. * See {ERC721-_mint}.
  62. *
  63. * Requirements:
  64. *
  65. * - the caller must have the `MINTER_ROLE`.
  66. */
  67. function mint(address to) public virtual {
  68. require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
  69. // We cannot just use balanceOf to create the new tokenId because tokens
  70. // can be burned (destroyed), so we need a separate counter.
  71. _mint(to, _tokenIdTracker.current());
  72. _tokenIdTracker.increment();
  73. }
  74. /**
  75. * @dev Pauses all token transfers.
  76. *
  77. * See {ERC721Pausable} and {Pausable-_pause}.
  78. *
  79. * Requirements:
  80. *
  81. * - the caller must have the `PAUSER_ROLE`.
  82. */
  83. function pause() public virtual {
  84. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
  85. _pause();
  86. }
  87. /**
  88. * @dev Unpauses all token transfers.
  89. *
  90. * See {ERC721Pausable} and {Pausable-_unpause}.
  91. *
  92. * Requirements:
  93. *
  94. * - the caller must have the `PAUSER_ROLE`.
  95. */
  96. function unpause() public virtual {
  97. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause");
  98. _unpause();
  99. }
  100. function _beforeTokenTransfer(
  101. address from,
  102. address to,
  103. uint256 firstTokenId,
  104. uint256 batchSize
  105. ) internal virtual override(ERC721, ERC721Enumerable, ERC721Pausable) {
  106. super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
  107. }
  108. /**
  109. * @dev See {IERC165-supportsInterface}.
  110. */
  111. function supportsInterface(
  112. bytes4 interfaceId
  113. ) public view virtual override(AccessControlEnumerable, ERC721, ERC721Enumerable) returns (bool) {
  114. return super.supportsInterface(interfaceId);
  115. }
  116. }