ERC721PresetMinterPauserAutoId.sol 3.4 KB

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