MetaRareOperator.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0; // 몇으로 할까
  3. import "../contracts/utils/cryptography/ECDSA.sol";
  4. import "../contracts/utils/cryptography/draft-EIP712.sol";
  5. import "../contracts/utils/math/SafeMath.sol";
  6. interface IMetaRareERC721Collection {
  7. function mint(address, uint256) external;
  8. function hasRole(bytes32, address) external view returns (bool);
  9. function ownerOf(uint256 tokenId) external view returns (address);
  10. function transferFrom(address from, address to, uint256 tokenId) external;
  11. function transferOnlyOperator(address, address, uint256) external;
  12. }
  13. interface IMetaRareERC1155Collection {
  14. function owner() external view returns (address);
  15. function exists(uint256) external view returns (bool);
  16. function createByOperator(address, uint256, uint256, string memory, bytes memory) external returns (uint256);
  17. function safeTransferFrom(address, address, uint256, uint256, bytes memory) external;
  18. function balanceOf(address, uint256) external view returns (uint256);
  19. function transferOnlyOperator(address, address, uint256, uint256) external;
  20. }
  21. interface IERC20 {
  22. function transfer(address to, uint256 amount) external returns (bool);
  23. function transferFrom(address from, address to, uint256 value) external returns (bool);
  24. function approve(address spender, uint256 value) external returns (bool);
  25. function balanceOf(address owner) external view returns (uint256);
  26. }
  27. contract MetaRareOperator is EIP712 {
  28. using SafeMath for uint256;
  29. mapping (uint256 => address) public creators;
  30. mapping (address => mapping(uint256 => address)) public creatorsWithinCollection;
  31. mapping (address => mapping(uint256 => uint256)) public creationTaxRate;
  32. struct NFTVoucher {
  33. address signer;
  34. address tokenAddress;
  35. uint256 tokenType;
  36. uint256 tokenId;
  37. address assetAddress;
  38. uint256 price;
  39. uint256 balance;
  40. uint256 totalBalance;
  41. uint256 creationTax;
  42. uint256 treasuryTax;
  43. address treasuryAddress;
  44. string uri;
  45. }
  46. constructor() EIP712("METARARE","1"){
  47. }
  48. bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
  49. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  50. address payable private _treasury;
  51. function recoverAddress(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) public pure returns(address) {
  52. return ecrecover(msgHash, v, r, s);
  53. }
  54. function excuteMintingOrderWithERC721Collection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  55. address signer = recoverAddress(hash, v,r,s);
  56. require(signer == voucher.signer, "Invalid signature");
  57. creators[voucher.tokenId] = signer;
  58. creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId] = creators[voucher.tokenId];
  59. creationTaxRate[voucher.tokenAddress][voucher.tokenId] = voucher.creationTax;
  60. _treasury = payable(voucher.treasuryAddress);
  61. address ERC721TokenAddress = voucher.tokenAddress;
  62. IMetaRareERC721Collection _ERC721Collection = IMetaRareERC721Collection(ERC721TokenAddress);
  63. require(_ERC721Collection.hasRole(MINTER_ROLE, signer), "Signature invalid or unauthorized");
  64. if (voucher.assetAddress == address(0)) {
  65. require(msg.value >= voucher.price, "No money");
  66. address payable targetAddress;
  67. targetAddress = payable(signer);
  68. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  69. uint256 value_seller = voucher.price.sub(value_treasury);
  70. _treasury.transfer(value_treasury);
  71. targetAddress.transfer(value_seller);
  72. }
  73. else {
  74. IERC20 tokenContract = IERC20(voucher.assetAddress);
  75. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  76. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  77. uint256 value_seller = voucher.price.sub(value_treasury);
  78. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  79. tokenContract.transferFrom(msg.sender, signer, value_seller);
  80. }
  81. _ERC721Collection.mint(msg.sender, voucher.tokenId);
  82. }
  83. function excuteMintingOrderWithERC721MetaRareCollection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  84. address signer = recoverAddress(hash, v,r,s);
  85. require(signer == voucher.signer, "Invalid signature");
  86. creators[voucher.tokenId] = signer;
  87. creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId] = creators[voucher.tokenId];
  88. creationTaxRate[voucher.tokenAddress][voucher.tokenId] = voucher.creationTax;
  89. _treasury = payable(voucher.treasuryAddress);
  90. address ERC721TokenAddress = voucher.tokenAddress;
  91. IMetaRareERC721Collection _ERC721Collection = IMetaRareERC721Collection(ERC721TokenAddress);
  92. if (voucher.assetAddress == address(0)) {
  93. require(msg.value >= voucher.price, "No money");
  94. address payable targetAddress;
  95. targetAddress = payable(signer);
  96. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  97. uint256 value_seller = voucher.price.sub(value_treasury);
  98. _treasury.transfer(value_treasury);
  99. targetAddress.transfer(value_seller);
  100. }
  101. else {
  102. IERC20 tokenContract = IERC20(voucher.assetAddress);
  103. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  104. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  105. uint256 value_seller = voucher.price.sub(value_treasury);
  106. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  107. tokenContract.transferFrom(msg.sender, signer, value_seller);
  108. }
  109. _ERC721Collection.mint(msg.sender, voucher.tokenId);
  110. }
  111. function excuteTransferOrderWithERC721Collection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  112. address signer = recoverAddress(hash, v,r,s);
  113. require(signer == voucher.signer, "Invalid signature");
  114. _treasury = payable(voucher.treasuryAddress);
  115. address ERC721TokenAddress = voucher.tokenAddress;
  116. IMetaRareERC721Collection _ERC721Collection = IMetaRareERC721Collection(ERC721TokenAddress);
  117. require(_ERC721Collection.ownerOf(voucher.tokenId) == signer, "Signature invalid or unauthorized");
  118. if (voucher.assetAddress == address(0)) {
  119. require(msg.value >= voucher.price, "No money");
  120. address payable targetAddress;
  121. targetAddress = payable(signer);
  122. address payable creatorAddress;
  123. creatorAddress = payable(creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId]);
  124. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  125. uint256 value_creator = voucher.price.mul(voucher.creationTax).div(10000);
  126. uint256 value_seller = voucher.price;
  127. value_seller = value_seller.sub(value_treasury).sub(value_creator);
  128. _treasury.transfer(value_treasury);
  129. creatorAddress.transfer(value_creator);
  130. targetAddress.transfer(value_seller);
  131. }
  132. else {
  133. IERC20 tokenContract = IERC20(voucher.assetAddress);
  134. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  135. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  136. uint256 value_creator = voucher.price.mul(voucher.creationTax).div(10000);
  137. uint256 value_seller = voucher.price.sub(value_treasury).sub(value_creator);
  138. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  139. address _creator = creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId];
  140. tokenContract.transferFrom(msg.sender, _creator, value_creator);
  141. tokenContract.transferFrom(msg.sender, signer, value_seller);
  142. }
  143. _ERC721Collection.transferOnlyOperator(signer, msg.sender, voucher.tokenId);
  144. }
  145. function excuteMintingOrderWithERC1155Collection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  146. address signer = recoverAddress(hash, v,r,s);
  147. require(signer == voucher.signer, "Invalid signature");
  148. creators[voucher.tokenId] = signer;
  149. creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId] = creators[voucher.tokenId];
  150. creationTaxRate[voucher.tokenAddress][voucher.tokenId] = voucher.creationTax;
  151. _treasury = payable(voucher.treasuryAddress);
  152. address ERC1155TokenAddress = voucher.tokenAddress;
  153. IMetaRareERC1155Collection _ERC1155Collection = IMetaRareERC1155Collection(ERC1155TokenAddress);
  154. require(_ERC1155Collection.owner() == signer, "Signature invalid or unauthorized");
  155. if (voucher.assetAddress == address(0)) {
  156. require(msg.value >= voucher.price, "No money");
  157. address payable targetAddress;
  158. targetAddress = payable(signer);
  159. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  160. uint256 value_seller = voucher.price.sub(value_treasury);
  161. _treasury.transfer(value_treasury);
  162. targetAddress.transfer(value_seller);
  163. }
  164. else {
  165. IERC20 tokenContract = IERC20(voucher.assetAddress);
  166. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  167. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  168. uint256 value_seller = voucher.price.sub(value_treasury);
  169. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  170. tokenContract.transferFrom(msg.sender, signer, value_seller);
  171. }
  172. if(!_ERC1155Collection.exists(voucher.tokenId)) {
  173. _ERC1155Collection.createByOperator(signer, voucher.tokenId, voucher.totalBalance, voucher.uri, "");
  174. _ERC1155Collection.safeTransferFrom(signer, msg.sender, voucher.tokenId, voucher.balance, "");
  175. }
  176. else {
  177. _ERC1155Collection.safeTransferFrom(signer, msg.sender, voucher.tokenId, voucher.balance, "");
  178. }
  179. }
  180. function excuteMintingOrderWithERC1155MetaRareCollection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  181. address signer = recoverAddress(hash, v,r,s);
  182. require(signer == voucher.signer, "Invalid signature");
  183. creators[voucher.tokenId] = signer;
  184. creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId] = creators[voucher.tokenId];
  185. creationTaxRate[voucher.tokenAddress][voucher.tokenId] = voucher.creationTax;
  186. _treasury = payable(voucher.treasuryAddress);
  187. address ERC1155TokenAddress = voucher.tokenAddress;
  188. IMetaRareERC1155Collection _ERC1155Collection = IMetaRareERC1155Collection(ERC1155TokenAddress);
  189. if (voucher.assetAddress == address(0)) {
  190. require(msg.value >= voucher.price, "No money");
  191. address payable targetAddress;
  192. targetAddress = payable(signer);
  193. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  194. uint256 value_seller = voucher.price.sub(value_treasury);
  195. _treasury.transfer(value_treasury);
  196. targetAddress.transfer(value_seller);
  197. }
  198. else {
  199. IERC20 tokenContract = IERC20(voucher.assetAddress);
  200. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  201. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  202. uint256 value_seller = voucher.price.sub(value_treasury);
  203. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  204. tokenContract.transferFrom(msg.sender, signer, value_seller);
  205. }
  206. if(!_ERC1155Collection.exists(voucher.tokenId)) {
  207. _ERC1155Collection.createByOperator(signer, voucher.tokenId, voucher.totalBalance, voucher.uri, "");
  208. _ERC1155Collection.transferOnlyOperator(signer, msg.sender, voucher.tokenId, voucher.balance);
  209. }
  210. else {
  211. _ERC1155Collection.transferOnlyOperator(signer, msg.sender, voucher.tokenId, voucher.balance);
  212. }
  213. }
  214. function excuteTransferOrderWithERC1155Collection(NFTVoucher calldata voucher, bytes32 hash, uint8 v, bytes32 r, bytes32 s) public payable {
  215. address signer = recoverAddress(hash, v,r,s);
  216. require(signer == voucher.signer, "Invalid signature");
  217. _treasury = payable(voucher.treasuryAddress);
  218. address ERC1155TokenAddress = voucher.tokenAddress;
  219. IMetaRareERC1155Collection _ERC1155Collection = IMetaRareERC1155Collection(ERC1155TokenAddress);
  220. require(_ERC1155Collection.balanceOf(signer, voucher.tokenId) >= voucher.balance, "Signature invalid or unauthorized");
  221. if (voucher.assetAddress == address(0)) {
  222. require(msg.value >= voucher.price, "No money");
  223. address payable targetAddress;
  224. targetAddress = payable(signer);
  225. address payable creatorAddress;
  226. creatorAddress = payable(creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId]);
  227. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  228. uint256 value_creator = voucher.price;
  229. value_creator = value_creator.mul(voucher.creationTax).div(10000);
  230. uint256 value_seller = voucher.price;
  231. value_seller = value_seller.sub(value_treasury).sub(value_creator);
  232. _treasury.transfer(value_treasury);
  233. creatorAddress.transfer(value_creator);
  234. targetAddress.transfer(value_seller);
  235. }
  236. else {
  237. IERC20 tokenContract = IERC20(voucher.assetAddress);
  238. require(tokenContract.balanceOf(msg.sender) >= voucher.price, "No money");
  239. uint256 value_treasury = voucher.price.mul(voucher.treasuryTax).div(10000);
  240. uint256 value_creator = voucher.price.mul(voucher.creationTax).div(10000);
  241. uint256 value_seller = voucher.price.sub(value_treasury).sub(value_creator);
  242. address _creator = creatorsWithinCollection[voucher.tokenAddress][voucher.tokenId];
  243. tokenContract.transferFrom(msg.sender, _treasury, value_treasury);
  244. tokenContract.transferFrom(msg.sender, _creator, value_creator);
  245. tokenContract.transferFrom(msg.sender, signer, value_seller);
  246. }
  247. _ERC1155Collection.transferOnlyOperator(signer, msg.sender, voucher.tokenId, voucher.balance);
  248. }
  249. }