EasyTokenDocument.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. uint256 private _tokenIdCounter;
  18. event BlacklistUpdated(address indexed account, bool isBlacklisted);
  19. event DocumentMinted(uint256 indexed tokenId, address indexed to, string uri, bytes32 documentHash, uint256 appraisalValue);
  20. event AppraisalUpdated(uint256 indexed tokenId, uint256 oldValue, uint256 newValue);
  21. event TokenURIUpdated(uint256 indexed tokenId, string uri);
  22. constructor(address admin, string memory name_, string memory symbol_) ERC721(name_, symbol_) {
  23. _grantRole(DEFAULT_ADMIN_ROLE, admin);
  24. _grantRole(PAUSER_ROLE, admin);
  25. _grantRole(MINTER_ROLE, admin);
  26. _grantRole(METADATA_ROLE, admin);
  27. _grantRole(APPRAISER_ROLE, admin);
  28. _grantRole(COMPLIANCE_ROLE, admin);
  29. }
  30. function valueDecimals() public pure returns (uint8) {
  31. return 6;
  32. }
  33. function pause() external onlyRole(PAUSER_ROLE) {
  34. _pause();
  35. }
  36. function unpause() external onlyRole(PAUSER_ROLE) {
  37. _unpause();
  38. }
  39. function setBlacklist(address account, bool status) external onlyRole(COMPLIANCE_ROLE) {
  40. _blacklist[account] = status;
  41. emit BlacklistUpdated(account, status);
  42. }
  43. function isBlacklisted(address account) external view returns (bool) {
  44. return _blacklist[account];
  45. }
  46. function documentHashOf(uint256 tokenId) external view returns (bytes32) {
  47. return _documentHash[tokenId];
  48. }
  49. function appraisalOf(uint256 tokenId) external view returns (uint256) {
  50. return _appraisalValue[tokenId];
  51. }
  52. function safeMint(address to, string memory uri, bytes32 documentHash, uint256 appraisalValue) external onlyRole(MINTER_ROLE) returns (uint256) {
  53. require(!_blacklist[to], "Blacklisted");
  54. uint256 tokenId = ++_tokenIdCounter;
  55. _safeMint(to, tokenId);
  56. _setTokenURI(tokenId, uri);
  57. _documentHash[tokenId] = documentHash;
  58. _appraisalValue[tokenId] = appraisalValue;
  59. emit DocumentMinted(tokenId, to, uri, documentHash, appraisalValue);
  60. return tokenId;
  61. }
  62. function setTokenURI(uint256 tokenId, string memory uri) external onlyRole(METADATA_ROLE) {
  63. require(_ownerOf(tokenId) != address(0), "Invalid token");
  64. _setTokenURI(tokenId, uri);
  65. emit TokenURIUpdated(tokenId, uri);
  66. }
  67. function setAppraisal(uint256 tokenId, uint256 newValue) external onlyRole(APPRAISER_ROLE) {
  68. require(_ownerOf(tokenId) != address(0), "Invalid token");
  69. uint256 old = _appraisalValue[tokenId];
  70. _appraisalValue[tokenId] = newValue;
  71. emit AppraisalUpdated(tokenId, old, newValue);
  72. }
  73. function approve(address to, uint256 tokenId) public override(ERC721, IERC721) {
  74. require(!_blacklist[_msgSender()], "Blacklisted");
  75. if (to != address(0)) {
  76. require(!_blacklist[to], "Blacklisted");
  77. }
  78. super.approve(to, tokenId);
  79. }
  80. function setApprovalForAll(address operator, bool approved) public override(ERC721, IERC721) {
  81. require(!_blacklist[_msgSender()], "Blacklisted");
  82. if (approved) {
  83. require(!_blacklist[operator], "Blacklisted");
  84. }
  85. super.setApprovalForAll(operator, approved);
  86. }
  87. function _update(address to, uint256 tokenId, address auth) internal override(ERC721, ERC721Pausable) returns (address) {
  88. address from = _ownerOf(tokenId);
  89. if (from != address(0)) {
  90. require(!_blacklist[from], "Blacklisted");
  91. }
  92. if (to != address(0)) {
  93. require(!_blacklist[to], "Blacklisted");
  94. }
  95. require(!_blacklist[auth], "Blacklisted");
  96. address previousOwner = super._update(to, tokenId, auth);
  97. if (to == address(0)) {
  98. delete _documentHash[tokenId];
  99. delete _appraisalValue[tokenId];
  100. }
  101. return previousOwner;
  102. }
  103. function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721URIStorage, AccessControl) returns (bool) {
  104. return super.supportsInterface(interfaceId);
  105. }
  106. function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
  107. return super.tokenURI(tokenId);
  108. }
  109. function grantComplianceRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) {
  110. _grantRole(COMPLIANCE_ROLE, account);
  111. }
  112. }