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";89/// @dev Fractionalization contract. It stores mappings between NFT and RFT tokens,10/// stores allowlist of NFT tokens available for fractionalization, has methods11/// for fractionalization and defractionalization of NFT tokens.12contract Fractionalizer {13 struct Token {14 address _collection;15 uint256 _tokenId;16 }17 address rftCollection;18 mapping(address => bool) nftCollectionAllowList;19 mapping(address => mapping(uint256 => uint256)) nft2rftMapping;20 mapping(address => Token) rft2nftMapping;21 bytes32 refungibleCollectionType = keccak256(bytes("ReFungible"));2223 receive() external payable onlyOwner {}2425 /// @dev Method modifier to only allow contract owner to call it.26 modifier onlyOwner() {27 address contracthelpersAddress = 0x842899ECF380553E8a4de75bF534cdf6fBF64049;28 ContractHelpers contractHelpers = ContractHelpers(contracthelpersAddress);29 address contractOwner = contractHelpers.contractOwner(address(this));30 require(msg.sender == contractOwner, "Only owner can");31 _;32 }3334 /// @dev This emits when RFT collection setting is changed.35 event RFTCollectionSet(address _collection);3637 /// @dev This emits when NFT collection is allowed or disallowed.38 event AllowListSet(address _collection, bool _status);3940 /// @dev This emits when NFT token is fractionalized by contract.41 event Fractionalized(address _collection, uint256 _tokenId, address _rftToken, uint128 _amount);4243 /// @dev This emits when NFT token is defractionalized by contract.44 event Defractionalized(address _rftToken, address _nftCollection, uint256 _nftTokenId);4546 /// Set RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens47 /// would be created in this collection.48 /// @dev Throws if RFT collection is already configured for this contract.49 /// Throws if collection of wrong type (NFT, Fungible) is provided instead50 /// of RFT collection.51 /// Throws if `msg.sender` is not owner or admin of provided RFT collection.52 /// Can only be called by contract owner.53 /// @param _collection address of RFT collection.54 function setRFTCollection(address _collection) public onlyOwner {55 require(56 rftCollection == address(0),57 "RFT collection is already set"58 );59 UniqueRefungible refungibleContract = UniqueRefungible(_collection);60 string memory collectionType = refungibleContract.uniqueCollectionType();61 62 require(63 keccak256(bytes(collectionType)) == refungibleCollectionType,64 "Wrong collection type. Collection is not refungible."65 );66 require(67 refungibleContract.verifyOwnerOrAdmin(address(this)),68 "Fractionalizer contract should be an admin of the collection"69 );70 rftCollection = _collection;71 emit RFTCollectionSet(rftCollection);72 }7374 /// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens75 /// would be created in this collection.76 /// @dev Throws if RFT collection is already configured for this contract.77 /// Can only be called by contract owner.78 /// @param _name name for created RFT collection.79 /// @param _description description for created RFT collection.80 /// @param _tokenPrefix token prefix for created RFT collection.81 function createAndSetRFTCollection(string calldata _name, string calldata _description, string calldata _tokenPrefix) public onlyOwner {82 require(83 rftCollection == address(0),84 "RFT collection is already set"85 );86 address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;87 rftCollection = CollectionHelpers(collectionHelpers).createRefungibleCollection(_name, _description, _tokenPrefix);88 emit RFTCollectionSet(rftCollection);89 }9091 /// Allow or disallow NFT collection tokens from being fractionalized by this contract.92 /// @dev Can only be called by contract owner.93 /// @param collection NFT token address.94 /// @param status `true` to allow and `false` to disallow NFT token.95 function setNftCollectionIsAllowed(address collection, bool status) public onlyOwner {96 nftCollectionAllowList[collection] = status;97 emit AllowListSet(collection, status);98 }99100 /// Fractionilize NFT token.101 /// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender`102 /// instead. Creates new RFT token if provided NFT token never was fractionalized103 /// by this contract or existing RFT token if it was.104 /// Throws if RFT collection isn't configured for this contract.105 /// Throws if fractionalization of provided NFT token is not allowed106 /// Throws if `msg.sender` is not owner of provided NFT token107 /// @param _collection NFT collection address108 /// @param _token id of NFT token to be fractionalized109 /// @param _pieces number of pieces new RFT token would have110 function nft2rft(address _collection, uint256 _token, uint128 _pieces) public {111 require(112 rftCollection != address(0),113 "RFT collection is not set"114 );115 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);116 require(117 nftCollectionAllowList[_collection] == true,118 "Fractionalization of this collection is not allowed by admin"119 );120 require(121 UniqueNFT(_collection).ownerOf(_token) == msg.sender,122 "Only token owner could fractionalize it"123 );124 UniqueNFT(_collection).transferFrom(125 msg.sender,126 address(this),127 _token128 );129 uint256 rftTokenId;130 address rftTokenAddress;131 UniqueRefungibleToken rftTokenContract;132 if (nft2rftMapping[_collection][_token] == 0) {133 rftTokenId = rftCollectionContract.nextTokenId();134 rftCollectionContract.mint(address(this), rftTokenId);135 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);136 nft2rftMapping[_collection][_token] = rftTokenId;137 rft2nftMapping[rftTokenAddress] = Token(_collection, _token);138139 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);140 rftTokenContract.setParentNFT(_collection, _token);141 } else {142 rftTokenId = nft2rftMapping[_collection][_token];143 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);144 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);145 }146 rftTokenContract.repartition(_pieces);147 rftTokenContract.transfer(msg.sender, _pieces);148 emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);149 }150151 /// Defrationalize NFT token.152 /// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token153 /// to `msg.sender` instead.154 /// Throws if RFT collection isn't configured for this contract.155 /// Throws if provided RFT token is no from configured RFT collection.156 /// Throws if RFT token was not created by this contract.157 /// Throws if `msg.sender` isn't owner of all RFT token pieces.158 /// @param _collection RFT collection address159 /// @param _token id of RFT token160 function rft2nft(address _collection, uint256 _token) public {161 require(162 rftCollection != address(0),163 "RFT collection is not set"164 );165 require(166 rftCollection == _collection,167 "Wrong RFT collection"168 );169 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);170 address rftTokenAddress = rftCollectionContract.tokenContractAddress(_token);171 Token memory nftToken = rft2nftMapping[rftTokenAddress];172 require(173 nftToken._collection != address(0),174 "No corresponding NFT token found"175 );176 UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(rftTokenAddress);177 require(178 rftTokenContract.balanceOf(msg.sender) == rftTokenContract.totalSupply(),179 "Not all pieces are owned by the caller"180 );181 rftCollectionContract.transferFrom(msg.sender, address(this), _token);182 UniqueNFT(nftToken._collection).transferFrom(183 address(this),184 msg.sender,185 nftToken._tokenId186 );187 emit Defractionalized(rftTokenAddress, nftToken._collection, nftToken._tokenId);188 }189}