|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.17; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/utils/Context.sol"; |
| 5 | +import "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol"; |
| 6 | +import "../accessmaster/interfaces/IAccessMaster.sol"; |
| 7 | + |
| 8 | +contract RewardToken is Context, ERC1155Supply { |
| 9 | + string public name; |
| 10 | + string public symbol; |
| 11 | + |
| 12 | + address public tradeHub; |
| 13 | + address public accessMasterAddress; |
| 14 | + |
| 15 | + uint256 public Counter; |
| 16 | + uint8 public constant version = 1; |
| 17 | + // Optional mapping for token URIs |
| 18 | + mapping(uint256 => string) private _tokenURIs; |
| 19 | + mapping(address => mapping(uint256 => uint256)) public phygitalToRewardId; |
| 20 | + |
| 21 | + //// phygitalTokenID => RewardToken -- >if it exists +++ |
| 22 | + |
| 23 | + IACCESSMASTER flowRoles; |
| 24 | + |
| 25 | + modifier onlyOperator() { |
| 26 | + require( |
| 27 | + flowRoles.isOperator(_msgSender()), |
| 28 | + "RewardToken: Unauthorized!" |
| 29 | + ); |
| 30 | + _; |
| 31 | + } |
| 32 | + |
| 33 | + modifier onlyCreator() { |
| 34 | + require( |
| 35 | + flowRoles.isCreator(_msgSender()), |
| 36 | + "RewardToken: Unauthorized!" |
| 37 | + ); |
| 38 | + _; |
| 39 | + } |
| 40 | + |
| 41 | + event RewardTokenCreated( |
| 42 | + uint256 indexed tokenID, |
| 43 | + address indexed creator, |
| 44 | + uint256 indexed amount, |
| 45 | + string metadataUri |
| 46 | + ); |
| 47 | + event RewardTokenDestroyed(uint indexed tokenId, address ownerOrApproved); |
| 48 | + |
| 49 | + // tradeHub should be there |
| 50 | + /** |
| 51 | + * @dev Grants `FLOW_ADMIN_ROLE`, `FLOW_CREATOR_ROLE` and `FLOW_OPERATOR_ROLE` to the |
| 52 | + * account that deploys the contract. |
| 53 | + * |
| 54 | + */ |
| 55 | + constructor( |
| 56 | + string memory baseURI, |
| 57 | + string memory _name, |
| 58 | + string memory _symbol, |
| 59 | + address tradeHubAddress, |
| 60 | + address flowContract |
| 61 | + ) ERC1155(baseURI) { |
| 62 | + name = _name; |
| 63 | + symbol = _symbol; |
| 64 | + tradeHub = tradeHubAddress; |
| 65 | + flowRoles = IACCESSMASTER(flowContract); |
| 66 | + accessMasterAddress = flowContract; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @dev Creates a new token for `to`. Its token ID will be automatically |
| 71 | + * assigned (and available on the emitted {IERC1155-Transfer} event), and the token |
| 72 | + * URI autogenerated based on the base URI passed at construction. |
| 73 | + * |
| 74 | + * |
| 75 | + * Requirements: |
| 76 | + * |
| 77 | + * - the caller must have the `FLOW_CREATOR_ROLE`. |
| 78 | + */ |
| 79 | + ///IssueRewardToken |
| 80 | + function createFanToken( |
| 81 | + address phygitalcontractAddr, |
| 82 | + uint256 amount, |
| 83 | + uint256 tokenId, |
| 84 | + bytes memory data, |
| 85 | + string memory _uri |
| 86 | + ) public returns (uint256) { |
| 87 | + uint256 currentTokenID; |
| 88 | + if (phygitalToRewardId[phygitalcontractAddr][tokenId] > 0) { |
| 89 | + currentTokenID = phygitalToRewardId[phygitalcontractAddr][tokenId]; |
| 90 | + } else { |
| 91 | + Counter++; |
| 92 | + currentTokenID = Counter; |
| 93 | + } |
| 94 | + _mint(_msgSender(), currentTokenID, amount, data); |
| 95 | + _tokenURIs[currentTokenID] = _uri; |
| 96 | + setApprovalForAll(tradeHub, true); |
| 97 | + emit RewardTokenCreated(currentTokenID, _msgSender(), amount, _uri); |
| 98 | + return currentTokenID; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * @dev Creates a new token for `to`. Its token ID will be automatically |
| 103 | + * assigned (and available on the emitted {IERC1155-Transfer} event), and the token |
| 104 | + * URI autogenerated based on the base URI passed at construction. |
| 105 | + * |
| 106 | + * Requirements: |
| 107 | + * |
| 108 | + * - the caller must have the `FLOW_CREATOR_ROLE`. |
| 109 | + */ |
| 110 | + function delegateAssetCreation( |
| 111 | + address phygitalcontractAddr, |
| 112 | + address creator, |
| 113 | + uint256 tokenId, |
| 114 | + uint256 amount, |
| 115 | + bytes memory data, |
| 116 | + string memory _uri |
| 117 | + ) public onlyOperator returns (uint256) { |
| 118 | + uint256 currentTokenID; |
| 119 | + // We cannot just use balanceOf to create the new tokenId because tokens |
| 120 | + // can be burned (destroyed), so we need a separate counter. |
| 121 | + if (phygitalToRewardId[phygitalcontractAddr][tokenId] > 0) { |
| 122 | + currentTokenID = phygitalToRewardId[phygitalcontractAddr][tokenId]; |
| 123 | + } else { |
| 124 | + Counter++; |
| 125 | + currentTokenID = Counter; |
| 126 | + } |
| 127 | + _mint(creator, currentTokenID, amount, data); |
| 128 | + _tokenURIs[currentTokenID] = _uri; |
| 129 | + setApprovalForAll(tradeHub, true); |
| 130 | + emit RewardTokenCreated(currentTokenID, _msgSender(), amount, _uri); |
| 131 | + return currentTokenID; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @notice Burns `tokenId`. See {ERC721-_burn}. |
| 136 | + * |
| 137 | + * @dev Requirements: |
| 138 | + * |
| 139 | + * - The caller must own `tokenId` or be an approved operator. |
| 140 | + */ |
| 141 | + function destroyAsset(uint256 tokenId, uint256 amount) public { |
| 142 | + require( |
| 143 | + balanceOf(_msgSender(), tokenId) == amount, |
| 144 | + "RewardToken: Caller is not token owner or approved" |
| 145 | + ); |
| 146 | + _burn(_msgSender(), tokenId, amount); |
| 147 | + emit RewardTokenDestroyed(tokenId, _msgSender()); |
| 148 | + } |
| 149 | + |
| 150 | + /// @dev ONLY Operator can set the Base URI |
| 151 | + function setURI(string memory newuri) external onlyOperator { |
| 152 | + _setURI(newuri); |
| 153 | + } |
| 154 | + |
| 155 | + /** Getter Functions **/ |
| 156 | + |
| 157 | + /// @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token. |
| 158 | + function uri( |
| 159 | + uint256 tokenId |
| 160 | + ) public view virtual override returns (string memory) { |
| 161 | + return _tokenURIs[tokenId]; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @dev See {IERC165-supportsInterface}. |
| 166 | + */ |
| 167 | + function supportsInterface( |
| 168 | + bytes4 interfaceId |
| 169 | + ) public view virtual override(ERC1155) returns (bool) { |
| 170 | + return super.supportsInterface(interfaceId); |
| 171 | + } |
| 172 | +} |
0 commit comments