ERC721PresetMinterPauserAutoId.sol 3.3 KB

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