MetaRareCollectionFactory.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../contracts/access/AccessControlEnumerable.sol";
  4. import "../contracts/utils/Context.sol";
  5. import "../contracts/token/ERC721/extensions/ERC721Enumerable.sol";
  6. import "../contracts/token/ERC721/extensions/ERC721URIStorage.sol";
  7. import "../contracts/access/Ownable.sol";
  8. import "../contracts/token/ERC1155/ERC1155.sol";
  9. import "../contracts/utils/math/SafeMath.sol";
  10. import "../contracts/utils/Strings.sol";
  11. //import "./ERC721TransferRole.sol";
  12. interface IMetaRareERC721Collection {
  13. function initialize(string memory, string memory, string memory, address, address) external;
  14. }
  15. contract MetaRareERC721Collection is
  16. Context,
  17. AccessControlEnumerable,
  18. ERC721Enumerable,
  19. ERC721URIStorage
  20. {
  21. address public collectionFactory;
  22. address public MetaRareOperator;
  23. bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
  24. string private _internalBaseURI;
  25. string private _customName;
  26. string private _customSymbol;
  27. constructor() public ERC721("initName", "initSymbol") {
  28. collectionFactory = msg.sender;
  29. }
  30. function initialize(
  31. string memory initName,
  32. string memory initSymbol,
  33. string memory baseURI,
  34. address collectionOwner,
  35. address operatorContract
  36. ) external {
  37. require(msg.sender == collectionFactory, 'ERC721: INITIALIZE FORBIDDEN');
  38. _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
  39. _setupRole(MINTER_ROLE, _msgSender());
  40. _setupRole(MINTER_ROLE, collectionOwner);
  41. _setupRole(MINTER_ROLE, operatorContract);
  42. _internalBaseURI = baseURI;
  43. _customName = initName;
  44. _customSymbol = initSymbol;
  45. MetaRareOperator = operatorContract;
  46. }
  47. function burn(uint256 tokenId) public virtual {
  48. require(
  49. _isApprovedOrOwner(_msgSender(), tokenId),
  50. "ERC721Burnable: caller is not owner nor approved"
  51. );
  52. _burn(tokenId);
  53. }
  54. function transferOnlyOperator(
  55. address from,
  56. address to,
  57. uint256 tokenId
  58. ) public virtual {
  59. require(msg.sender == MetaRareOperator, "Operatron only can use this function");
  60. _transferOnlyOperator(from, to, tokenId);
  61. }
  62. function supportsInterface(bytes4 interfaceId)
  63. public
  64. view
  65. virtual
  66. override(ERC721Enumerable, AccessControlEnumerable, ERC721)
  67. returns (bool)
  68. {
  69. return
  70. interfaceId == type(IERC721Enumerable).interfaceId ||
  71. super.supportsInterface(interfaceId);
  72. }
  73. function setBaseURI(string memory newBaseUri) public {
  74. require(
  75. hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
  76. "ERC721: must have admin role to change baseUri"
  77. );
  78. _internalBaseURI = newBaseUri;
  79. }
  80. function mint(address to, uint256 tokenId) public virtual {
  81. require(
  82. hasRole(MINTER_ROLE, _msgSender()),
  83. "ERC721: must have minter role to mint"
  84. );
  85. _mint(to, tokenId);
  86. }
  87. function tokenURI(uint256 tokenId)
  88. public
  89. view
  90. virtual
  91. override(ERC721URIStorage, ERC721)
  92. returns (string memory)
  93. {
  94. return super.tokenURI(tokenId);
  95. }
  96. function _transferOnlyOperator(
  97. address from,
  98. address to,
  99. uint256 tokenId
  100. ) internal virtual {
  101. super._safeTransfer(from, to, tokenId, "");
  102. }
  103. function _beforeTokenTransfer(
  104. address from,
  105. address to,
  106. uint256 tokenId
  107. ) internal virtual override(ERC721, ERC721Enumerable) {
  108. super._beforeTokenTransfer(from, to, tokenId);
  109. }
  110. function _burn(uint256 tokenId)
  111. internal
  112. virtual
  113. override(ERC721, ERC721URIStorage)
  114. {
  115. super._burn(tokenId);
  116. }
  117. function _baseURI() internal view override returns (string memory) {
  118. return _internalBaseURI;
  119. }
  120. function setTokenURI(uint256 tokenId, string memory _tokenURI) public {
  121. require(
  122. hasRole(DEFAULT_ADMIN_ROLE, _msgSender()),
  123. "ERC721: must have admin role to set Token URIs"
  124. );
  125. super._setTokenURI(tokenId, _tokenURI);
  126. }
  127. function setName(string memory newName) public {
  128. require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "Must have Admin role");
  129. _customName = newName;
  130. }
  131. function setSymbol(string memory newSymbol) public {
  132. require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "Must have Admin role");
  133. _customSymbol = newSymbol;
  134. }
  135. function name() public view override returns (string memory) {
  136. return _customName;
  137. }
  138. function symbol() public view override returns (string memory) {
  139. return _customSymbol;
  140. }
  141. }
  142. interface IMetaRareERC1155Collection {
  143. function initialize(string memory, string memory, string memory, address, address) external;
  144. }
  145. contract MetaRareERC1155Collection is ERC1155, Ownable {
  146. using Strings for string;
  147. using SafeMath for uint256;
  148. address public collectionFactory;
  149. address public MetaRareOperator;
  150. mapping (uint256 => address) public creators;
  151. mapping (uint256 => uint256) public tokenSupply;
  152. mapping (uint256 => string) public customUri;
  153. string public name;
  154. string public symbol;
  155. modifier creatorOnly(uint256 _id) {
  156. require(creators[_id] == _msgSender(), "ERC1155Tradable#creatorOnly: ONLY_CREATOR_ALLOWED");
  157. _;
  158. }
  159. modifier ownersOnly(uint256 _id) {
  160. require(balanceOf(_msgSender(), _id) > 0, "ERC1155Tradable#ownersOnly: ONLY_OWNERS_ALLOWED");
  161. _;
  162. }
  163. constructor() public ERC1155("_uri") {
  164. collectionFactory = msg.sender;
  165. }
  166. function initialize(
  167. string memory _name,
  168. string memory _symbol,
  169. string memory _uri,
  170. address collectionOwner,
  171. address operatorContract
  172. ) external {
  173. require(msg.sender == collectionFactory, 'ERC1155: INITIALIZE FORBIDDEN');
  174. name = _name;
  175. symbol = _symbol;
  176. _setURI(_uri);
  177. MetaRareOperator = operatorContract;
  178. transferOwnership(collectionOwner);
  179. ERC1155.setApprovalForAll(operatorContract, true);
  180. }
  181. function transferOnlyOperator(
  182. address from,
  183. address to,
  184. uint256 tokenId,
  185. uint256 amount
  186. ) public virtual {
  187. require(msg.sender == MetaRareOperator, "Operatron only can use this function");
  188. _transferOnlyOperator(from, to, tokenId, amount);
  189. }
  190. function uri(
  191. uint256 _id
  192. ) override public view returns (string memory) {
  193. require(_exists(_id), "ERC1155Tradable#uri: NONEXISTENT_TOKEN");
  194. bytes memory customUriBytes = bytes(customUri[_id]);
  195. if (customUriBytes.length > 0) {
  196. return customUri[_id];
  197. } else {
  198. return super.uri(_id);
  199. }
  200. }
  201. function totalSupply(
  202. uint256 _id
  203. ) public view returns (uint256) {
  204. return tokenSupply[_id];
  205. }
  206. function setURI(
  207. string memory _newURI
  208. ) public onlyOwner {
  209. _setURI(_newURI);
  210. }
  211. function setCustomURI(
  212. uint256 _tokenId,
  213. string memory _newURI
  214. ) public creatorOnly(_tokenId) {
  215. customUri[_tokenId] = _newURI;
  216. emit URI(_newURI, _tokenId);
  217. }
  218. function create(
  219. address _initialOwner,
  220. uint256 _id,
  221. uint256 _initialSupply,
  222. string memory _uri,
  223. bytes memory _data
  224. ) public onlyOwner returns (uint256) {
  225. require(!_exists(_id), "token _id already exists");
  226. creators[_id] = _msgSender();
  227. if (bytes(_uri).length > 0) {
  228. customUri[_id] = _uri;
  229. emit URI(_uri, _id);
  230. }
  231. _mint(_initialOwner, _id, _initialSupply, _data);
  232. tokenSupply[_id] = _initialSupply;
  233. return _id;
  234. }
  235. function createByOperator(
  236. address _initialOwner,
  237. uint256 _id,
  238. uint256 _initialSupply,
  239. string memory _uri,
  240. bytes memory _data
  241. ) public returns (uint256) {
  242. require(msg.sender == MetaRareOperator, "Cannot called by not operator");
  243. require(!_exists(_id), "token _id already exists");
  244. creators[_id] = _initialOwner;
  245. if (bytes(_uri).length > 0) {
  246. customUri[_id] = _uri;
  247. emit URI(_uri, _id);
  248. }
  249. _mint(_initialOwner, _id, _initialSupply, _data);
  250. tokenSupply[_id] = _initialSupply;
  251. return _id;
  252. }
  253. function mint(
  254. address _to,
  255. uint256 _id,
  256. uint256 _quantity,
  257. bytes memory _data
  258. ) virtual public creatorOnly(_id) {
  259. _mint(_to, _id, _quantity, _data);
  260. tokenSupply[_id] = tokenSupply[_id].add(_quantity);
  261. }
  262. function batchMint(
  263. address _to,
  264. uint256[] memory _ids,
  265. uint256[] memory _quantities,
  266. bytes memory _data
  267. ) public {
  268. for (uint256 i = 0; i < _ids.length; i++) {
  269. uint256 _id = _ids[i];
  270. require(creators[_id] == _msgSender(), "ERC1155Tradable#batchMint: ONLY_CREATOR_ALLOWED");
  271. uint256 quantity = _quantities[i];
  272. tokenSupply[_id] = tokenSupply[_id].add(quantity);
  273. }
  274. _mintBatch(_to, _ids, _quantities, _data);
  275. }
  276. function setCreator(
  277. address _to,
  278. uint256[] memory _ids
  279. ) public {
  280. require(_to != address(0), "ERC1155Tradable#setCreator: INVALID_ADDRESS.");
  281. for (uint256 i = 0; i < _ids.length; i++) {
  282. uint256 id = _ids[i];
  283. _setCreator(_to, id);
  284. }
  285. }
  286. function isApprovedForAll(
  287. address _owner,
  288. address _operator
  289. ) override public view returns (bool isOperator) {
  290. return ERC1155.isApprovedForAll(_owner, _operator);
  291. }
  292. function _setCreator(address _to, uint256 _id) internal creatorOnly(_id)
  293. {
  294. creators[_id] = _to;
  295. }
  296. function _transferOnlyOperator(
  297. address from,
  298. address to,
  299. uint256 tokenId,
  300. uint256 amount
  301. ) internal virtual {
  302. super._safeTransferFrom(from, to, tokenId, amount, "");
  303. }
  304. function _exists(
  305. uint256 _id
  306. ) internal view returns (bool) {
  307. return creators[_id] != address(0);
  308. }
  309. function exists(
  310. uint256 _id
  311. ) external view returns (bool) {
  312. return _exists(_id);
  313. }
  314. function _msgSender()
  315. internal
  316. override
  317. view
  318. returns (address sender)
  319. {
  320. return msg.sender;
  321. }
  322. }
  323. contract MetaRareCollectionFactory {
  324. uint256 private nonce;
  325. event CreateERC721Collection(address collection);
  326. event CreateERC1155Collection(address collection);
  327. constructor() {
  328. nonce = 0;
  329. }
  330. function CreateMetaRareERC721Collection(string memory name, string memory symbol, string memory baseURI, address OperatorContract) public {
  331. address addr = createERC721Contract(name, symbol, baseURI);
  332. IMetaRareERC721Collection(addr).initialize(name, symbol, baseURI, msg.sender, OperatorContract);
  333. emit CreateERC721Collection(addr);
  334. }
  335. function createERC721Contract(string memory name, string memory symbol, string memory baseURI) internal returns(address newAddr) {
  336. bytes memory bytecode = type(MetaRareERC721Collection).creationCode;
  337. bytes32 salt = keccak256(abi.encodePacked(name, symbol, baseURI, nonce));
  338. nonce += 1;
  339. assembly {
  340. newAddr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
  341. if iszero(extcodesize(newAddr)) {
  342. revert(0, 0)
  343. }
  344. }
  345. }
  346. function CreateMetaRareERC1155Collection(string memory name, string memory symbol, string memory uri, address OperatorContract) public {
  347. address addr = createERC1155Contract(name, symbol, uri);
  348. IMetaRareERC1155Collection(addr).initialize(name, symbol, uri, msg.sender, OperatorContract);
  349. emit CreateERC1155Collection(addr);
  350. }
  351. function createERC1155Contract(string memory name, string memory symbol, string memory uri) internal returns(address newAddr) {
  352. bytes memory bytecode = type(MetaRareERC1155Collection).creationCode;
  353. bytes32 salt = keccak256(abi.encodePacked(name, symbol, uri, nonce));
  354. nonce += 1;
  355. assembly {
  356. newAddr := create2(0, add(bytecode, 0x20), mload(bytecode), salt)
  357. if iszero(extcodesize(newAddr)) {
  358. revert(0, 0)
  359. }
  360. }
  361. }
  362. }