ERC721PresetMinterPauserAutoId.sol 4.1 KB

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