EasyTokenDocument.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: UNLICENSED
  2. pragma solidity ^0.8.20;
  3. import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
  4. import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
  5. import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Pausable.sol";
  6. import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
  7. import "@openzeppelin/contracts/access/AccessControl.sol";
  8. contract EasyTokenDocument is ERC721URIStorage, ERC721Burnable, ERC721Pausable, AccessControl {
  9. bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
  10. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  11. bytes32 public constant COMPLIANCE_ROLE = keccak256("COMPLIANCE_ROLE");
  12. bytes32 public constant METADATA_ROLE = keccak256("METADATA_ROLE");
  13. bytes32 public constant APPRAISER_ROLE = keccak256("APPRAISER_ROLE");
  14. mapping(address => bool) private _blacklist;
  15. mapping(uint256 => bytes32) private _documentHash;
  16. mapping(uint256 => uint256) private _appraisalValue;
  17. mapping(uint256 => string) private _tokenContent;
  18. uint256 private _tokenIdCounter;
  19. event BlacklistUpdated(address indexed account, bool isBlacklisted);
  20. event DocumentMinted(uint256 indexed tokenId, address indexed to, string uri, bytes32 documentHash, uint256 appraisalValue);
  21. event ContentMinted(uint256 indexed tokenId, address indexed to, string content);
  22. event AppraisalUpdated(uint256 indexed tokenId, uint256 oldValue, uint256 newValue);
  23. event TokenURIUpdated(uint256 indexed tokenId, string uri);
  24. constructor(address admin, string memory name_, string memory symbol_) ERC721(name_, symbol_) {
  25. _grantRole(DEFAULT_ADMIN_ROLE, admin);
  26. _grantRole(PAUSER_ROLE, admin);
  27. _grantRole(MINTER_ROLE, admin);
  28. _grantRole(METADATA_ROLE, admin);
  29. _grantRole(APPRAISER_ROLE, admin);
  30. _grantRole(COMPLIANCE_ROLE, admin);
  31. }
  32. function valueDecimals() public pure returns (uint8) {
  33. return 6;
  34. }
  35. function pause() external onlyRole(PAUSER_ROLE) {
  36. _pause();
  37. }
  38. function unpause() external onlyRole(PAUSER_ROLE) {
  39. _unpause();
  40. }
  41. function setBlacklist(address account, bool status) external onlyRole(COMPLIANCE_ROLE) {
  42. _blacklist[account] = status;
  43. emit BlacklistUpdated(account, status);
  44. }
  45. function isBlacklisted(address account) external view returns (bool) {
  46. return _blacklist[account];
  47. }
  48. function documentHashOf(uint256 tokenId) external view returns (bytes32) {
  49. return _documentHash[tokenId];
  50. }
  51. function appraisalOf(uint256 tokenId) external view returns (uint256) {
  52. return _appraisalValue[tokenId];
  53. }
  54. function contentOf(uint256 tokenId) external view returns (string memory) {
  55. require(_ownerOf(tokenId) != address(0), "Invalid token");
  56. return _tokenContent[tokenId];
  57. }
  58. function safeMint(address to, string memory uri, bytes32 documentHash, uint256 appraisalValue) external onlyRole(MINTER_ROLE) returns (uint256) {
  59. require(!_blacklist[to], "Blacklisted");
  60. uint256 tokenId = ++_tokenIdCounter;
  61. _safeMint(to, tokenId);
  62. _setTokenURI(tokenId, uri);
  63. _documentHash[tokenId] = documentHash;
  64. _appraisalValue[tokenId] = appraisalValue;
  65. emit DocumentMinted(tokenId, to, uri, documentHash, appraisalValue);
  66. return tokenId;
  67. }
  68. function mintContent(address to, string calldata content) external onlyRole(MINTER_ROLE) returns (uint256) {
  69. require(to != address(0), "Invalid recipient");
  70. require(!_blacklist[to], "Blacklisted");
  71. bytes memory raw = bytes(content);
  72. require(raw.length > 0, "Content required");
  73. require(raw.length <= 20, "Content too long");
  74. uint256 tokenId = ++_tokenIdCounter;
  75. _safeMint(to, tokenId);
  76. _tokenContent[tokenId] = content;
  77. emit ContentMinted(tokenId, to, content);
  78. return tokenId;
  79. }
  80. function setTokenURI(uint256 tokenId, string memory uri) external onlyRole(METADATA_ROLE) {
  81. require(_ownerOf(tokenId) != address(0), "Invalid token");
  82. _setTokenURI(tokenId, uri);
  83. emit TokenURIUpdated(tokenId, uri);
  84. }
  85. function setAppraisal(uint256 tokenId, uint256 newValue) external onlyRole(APPRAISER_ROLE) {
  86. require(_ownerOf(tokenId) != address(0), "Invalid token");
  87. uint256 old = _appraisalValue[tokenId];
  88. _appraisalValue[tokenId] = newValue;
  89. emit AppraisalUpdated(tokenId, old, newValue);
  90. }
  91. function approve(address to, uint256 tokenId) public override(ERC721, IERC721) {
  92. require(!_blacklist[_msgSender()], "Blacklisted");
  93. if (to != address(0)) {
  94. require(!_blacklist[to], "Blacklisted");
  95. }
  96. super.approve(to, tokenId);
  97. }
  98. function setApprovalForAll(address operator, bool approved) public override(ERC721, IERC721) {
  99. require(!_blacklist[_msgSender()], "Blacklisted");
  100. if (approved) {
  101. require(!_blacklist[operator], "Blacklisted");
  102. }
  103. super.setApprovalForAll(operator, approved);
  104. }
  105. function _update(address to, uint256 tokenId, address auth) internal override(ERC721, ERC721Pausable) returns (address) {
  106. address from = _ownerOf(tokenId);
  107. if (from != address(0)) {
  108. require(!_blacklist[from], "Blacklisted");
  109. }
  110. if (to != address(0)) {
  111. require(!_blacklist[to], "Blacklisted");
  112. }
  113. require(!_blacklist[auth], "Blacklisted");
  114. address previousOwner = super._update(to, tokenId, auth);
  115. if (to == address(0)) {
  116. delete _documentHash[tokenId];
  117. delete _appraisalValue[tokenId];
  118. delete _tokenContent[tokenId];
  119. }
  120. return previousOwner;
  121. }
  122. function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721URIStorage, AccessControl) returns (bool) {
  123. return super.supportsInterface(interfaceId);
  124. }
  125. function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
  126. return super.tokenURI(tokenId);
  127. }
  128. function grantComplianceRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
  129. _grantRole(COMPLIANCE_ROLE, account);
  130. }
  131. }