ERC721PresetMinterPauserAutoId.sol 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../access/AccessControlEnumerable.sol";
  4. import "../utils/Context.sol";
  5. import "../utils/Counters.sol";
  6. import "../token/ERC721/ERC721.sol";
  7. import "../token/ERC721/ERC721Enumerable.sol";
  8. import "../token/ERC721/ERC721Burnable.sol";
  9. import "../token/ERC721/ERC721Pausable.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 Context, AccessControlEnumerable, ERC721Enumerable, ERC721Burnable, ERC721Pausable {
  26. using Counters for Counters.Counter;
  27. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  28. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  29. Counters.Counter private _tokenIdTracker;
  30. string private _baseTokenURI;
  31. /**
  32. * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
  33. * account that deploys the contract.
  34. *
  35. * Token URIs will be autogenerated based on `baseURI` and their token IDs.
  36. * See {ERC721-tokenURI}.
  37. */
  38. constructor(string memory name, string memory symbol, string memory baseTokenURI) ERC721(name, symbol) {
  39. _baseTokenURI = baseTokenURI;
  40. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  41. _setupRole(MINTER_ROLE, _msgSender());
  42. _setupRole(PAUSER_ROLE, _msgSender());
  43. }
  44. function _baseURI() internal view virtual override returns (string memory) {
  45. return _baseTokenURI;
  46. }
  47. /**
  48. * @dev Creates a new token for `to`. Its token ID will be automatically
  49. * assigned (and available on the emitted {IERC721-Transfer} event), and the token
  50. * URI autogenerated based on the base URI passed at construction.
  51. *
  52. * See {ERC721-_mint}.
  53. *
  54. * Requirements:
  55. *
  56. * - the caller must have the `MINTER_ROLE`.
  57. */
  58. function mint(address to) public virtual {
  59. require(hasRole(MINTER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have minter role to mint");
  60. // We cannot just use balanceOf to create the new tokenId because tokens
  61. // can be burned (destroyed), so we need a separate counter.
  62. _mint(to, _tokenIdTracker.current());
  63. _tokenIdTracker.increment();
  64. }
  65. /**
  66. * @dev Pauses all token transfers.
  67. *
  68. * See {ERC721Pausable} and {Pausable-_pause}.
  69. *
  70. * Requirements:
  71. *
  72. * - the caller must have the `PAUSER_ROLE`.
  73. */
  74. function pause() public virtual {
  75. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to pause");
  76. _pause();
  77. }
  78. /**
  79. * @dev Unpauses all token transfers.
  80. *
  81. * See {ERC721Pausable} and {Pausable-_unpause}.
  82. *
  83. * Requirements:
  84. *
  85. * - the caller must have the `PAUSER_ROLE`.
  86. */
  87. function unpause() public virtual {
  88. require(hasRole(PAUSER_ROLE, _msgSender()), "ERC721PresetMinterPauserAutoId: must have pauser role to unpause");
  89. _unpause();
  90. }
  91. function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual override(ERC721, ERC721Enumerable, ERC721Pausable) {
  92. super._beforeTokenTransfer(from, to, tokenId);
  93. }
  94. /**
  95. * @dev See {IERC165-supportsInterface}.
  96. */
  97. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC721Enumerable) returns (bool) {
  98. return super.supportsInterface(interfaceId);
  99. }
  100. }