1// SPDX-License-Identifier: Apache License2pragma solidity >=0.8.0;3import {CollectionHelpers} from "../api/CollectionHelpers.sol";4import {ContractHelpers} from "../api/ContractHelpers.sol";5import {UniqueRefungibleToken} from "../api/UniqueRefungibleToken.sol";6import {UniqueRefungible} from "../api/UniqueRefungible.sol";7import {UniqueNFT} from "../api/UniqueNFT.sol";89contract Fractionalizer {10 struct Token {11 address _collection;12 uint256 _tokenId;13 }14 address rftCollection;15 mapping(address => bool) nftCollectionAllowList;16 mapping(address => mapping(uint256 => uint256)) nft2rftMapping;17 mapping(address => Token) rft2nftMapping;18 address owner;19 bytes32 refungibleCollectionType = keccak256(bytes("ReFungible"));2021 constructor() {22 owner = msg.sender;23 }2425 modifier onlyOwner() {26 require(msg.sender == owner, "Only owner can");27 _;28 }2930 event RFTCollectionSet(address _collection);31 event AllowListSet(address _collection, bool _status);32 event Fractionalized(address _collection, uint256 _tokenId, address _rftToken, uint128 _amount);33 event DeFractionalized(address _rftToken, address _nftCollection, uint256 _nftTokenId);3435 function setRFTCollection(address _collection) public onlyOwner {36 require(37 rftCollection == address(0),38 "RFT collection is already set"39 );40 UniqueRefungible refungibleContract = UniqueRefungible(_collection);41 string memory collectionType = refungibleContract.uniqueCollectionType();42 43 require(44 keccak256(bytes(collectionType)) == refungibleCollectionType,45 "Fractionalizer contract should be an admin of the collection"46 );47 require(48 refungibleContract.verifyOwnerOrAdmin(),49 "Fractionalizer contract should be an admin of the collection"50 );51 rftCollection = _collection;52 emit RFTCollectionSet(rftCollection);53 }5455 function mintRFTCollection(string calldata _name, string calldata _description, string calldata _tokenPrefix) public onlyOwner {56 require(57 rftCollection == address(0),58 "RFT collection is already set"59 );60 address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;61 rftCollection = CollectionHelpers(collectionHelpers).createRefungibleCollection(_name, _description, _tokenPrefix);62 emit RFTCollectionSet(rftCollection);63 }6465 function setAllowlist(address collection, bool status) public onlyOwner {66 nftCollectionAllowList[collection] = status;67 emit AllowListSet(collection, status);68 }6970 function nft2rft(address _collection, uint256 _token, uint128 _pieces) public {71 require(72 rftCollection != address(0),73 "RFT collection is not set"74 );75 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);76 require(77 UniqueNFT(_collection).ownerOf(_token) == msg.sender,78 "Only token owner could fractionalize it"79 );80 require(81 nftCollectionAllowList[_collection] == true,82 "Fractionalization of this collection is not allowed by admin"83 );84 require(85 UniqueNFT(_collection).ownerOf(_token) == msg.sender,86 "Only token owner could fractionalize it"87 );88 UniqueNFT(_collection).transferFrom(89 msg.sender,90 address(this),91 _token92 );93 uint256 rftTokenId;94 address rftTokenAddress;95 UniqueRefungibleToken rftTokenContract;96 if (nft2rftMapping[_collection][_token] == 0) {97 rftTokenId = rftCollectionContract.nextTokenId();98 rftCollectionContract.mint(address(this), rftTokenId);99 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);100 nft2rftMapping[_collection][_token] = rftTokenId;101 rft2nftMapping[rftTokenAddress] = Token(_collection, _token);102103 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);104 rftTokenContract.setParentNFT(_collection, _token);105 } else {106 rftTokenId = nft2rftMapping[_collection][_token];107 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);108 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);109 }110 rftTokenContract.repartition(_pieces);111 rftTokenContract.transfer(msg.sender, _pieces);112 emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);113 }114115 function rft2nft(address _collection, uint256 _token) public {116 require(117 rftCollection != address(0),118 "RFT collection is not set"119 );120 require(121 rftCollection == _collection,122 "Wrong RFT collection"123 );124 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);125 address rftTokenAddress = rftCollectionContract.tokenContractAddress(_token);126 Token memory nftToken = rft2nftMapping[rftTokenAddress];127 require(128 nftToken._collection != address(0),129 "No corresponding NFT token found"130 );131 UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(rftTokenAddress);132 require(133 rftTokenContract.balanceOf(msg.sender) == rftTokenContract.totalSupply(),134 "Not all pieces are owned by the caller"135 );136 rftCollectionContract.transferFrom(msg.sender, address(this), _token);137 UniqueNFT(nftToken._collection).transferFrom(138 address(this),139 msg.sender,140 nftToken._tokenId141 );142 emit DeFractionalized(rftTokenAddress, nftToken._collection, nftToken._tokenId);143 }144}