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 //TODO: add nonPayable modifier after Solidity updates to 0.9.24 receive() external payable onlyOwner {}2526 /// @dev Method modifier to only allow contract owner to call it.27 modifier onlyOwner() {28 address contracthelpersAddress = 0x842899ECF380553E8a4de75bF534cdf6fBF64049;29 ContractHelpers contractHelpers = ContractHelpers(contracthelpersAddress);30 address contractOwner = contractHelpers.contractOwner(address(this));31 require(msg.sender == contractOwner, "Only owner can");32 _;33 }3435 /// @dev This emits when RFT collection setting is changed.36 event RFTCollectionSet(address _collection);3738 /// @dev This emits when NFT collection is allowed or disallowed.39 event AllowListSet(address _collection, bool _status);4041 /// @dev This emits when NFT token is fractionalized by contract.42 event Fractionalized(address _collection, uint256 _tokenId, address _rftToken, uint128 _amount);4344 /// @dev This emits when NFT token is defractionalized by contract.45 event Defractionalized(address _rftToken, address _nftCollection, uint256 _nftTokenId);4647 /// Set RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens48 /// would be created in this collection.49 /// @dev Throws if RFT collection is already configured for this contract.50 /// Throws if collection of wrong type (NFT, Fungible) is provided instead51 /// of RFT collection.52 /// Throws if `msg.sender` is not owner or admin of provided RFT collection.53 /// Can only be called by contract owner.54 /// @param _collection address of RFT collection.55 function setRFTCollection(address _collection) public onlyOwner {56 require(57 rftCollection == address(0),58 "RFT collection is already set"59 );60 UniqueRefungible refungibleContract = UniqueRefungible(_collection);61 string memory collectionType = refungibleContract.uniqueCollectionType();62 63 require(64 keccak256(bytes(collectionType)) == refungibleCollectionType,65 "Wrong collection type. Collection is not refungible."66 );67 require(68 refungibleContract.verifyOwnerOrAdmin(),69 "Fractionalizer contract should be an admin of the collection"70 );71 rftCollection = _collection;72 emit RFTCollectionSet(rftCollection);73 }7475 /// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens76 /// would be created in this collection.77 /// @dev Throws if RFT collection is already configured for this contract.78 /// Can only be called by contract owner.79 /// @param _name name for created RFT collection.80 /// @param _description description for created RFT collection.81 /// @param _tokenPrefix token prefix for created RFT collection.82 function createAndSetRFTCollection(string calldata _name, string calldata _description, string calldata _tokenPrefix) public onlyOwner {83 require(84 rftCollection == address(0),85 "RFT collection is already set"86 );87 address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;88 rftCollection = CollectionHelpers(collectionHelpers).createRefungibleCollection(_name, _description, _tokenPrefix);89 emit RFTCollectionSet(rftCollection);90 }9192 /// Allow or disallow NFT collection tokens from being fractionalized by this contract.93 /// @dev Can only be called by contract owner.94 /// @param collection NFT token address.95 /// @param status `true` to allow and `false` to disallow NFT token.96 function setNftCollectionIsAllowed(address collection, bool status) public onlyOwner {97 nftCollectionAllowList[collection] = status;98 emit AllowListSet(collection, status);99 }100101 /// Fractionilize NFT token.102 /// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender`103 /// instead. Creates new RFT token if provided NFT token never was fractionalized104 /// by this contract or existing RFT token if it was.105 /// Throws if RFT collection isn't configured for this contract.106 /// Throws if fractionalization of provided NFT token is not allowed107 /// Throws if `msg.sender` is not owner of provided NFT token108 /// @param _collection NFT collection address109 /// @param _token id of NFT token to be fractionalized110 /// @param _pieces number of pieces new RFT token would have111 function nft2rft(address _collection, uint256 _token, uint128 _pieces) public {112 require(113 rftCollection != address(0),114 "RFT collection is not set"115 );116 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);117 require(118 nftCollectionAllowList[_collection] == true,119 "Fractionalization of this collection is not allowed by admin"120 );121 require(122 UniqueNFT(_collection).ownerOf(_token) == msg.sender,123 "Only token owner could fractionalize it"124 );125 UniqueNFT(_collection).transferFrom(126 msg.sender,127 address(this),128 _token129 );130 uint256 rftTokenId;131 address rftTokenAddress;132 UniqueRefungibleToken rftTokenContract;133 if (nft2rftMapping[_collection][_token] == 0) {134 rftTokenId = rftCollectionContract.nextTokenId();135 rftCollectionContract.mint(address(this), rftTokenId);136 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);137 nft2rftMapping[_collection][_token] = rftTokenId;138 rft2nftMapping[rftTokenAddress] = Token(_collection, _token);139140 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);141 rftTokenContract.setParentNFT(_collection, _token);142 } else {143 rftTokenId = nft2rftMapping[_collection][_token];144 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);145 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);146 }147 rftTokenContract.repartition(_pieces);148 rftTokenContract.transfer(msg.sender, _pieces);149 emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);150 }151152 /// Defrationalize NFT token.153 /// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token154 /// to `msg.sender` instead.155 /// Throws if RFT collection isn't configured for this contract.156 /// Throws if provided RFT token is no from configured RFT collection.157 /// Throws if RFT token was not created by this contract.158 /// Throws if `msg.sender` isn't owner of all RFT token pieces.159 /// @param _collection RFT collection address160 /// @param _token id of RFT token161 function rft2nft(address _collection, uint256 _token) public {162 require(163 rftCollection != address(0),164 "RFT collection is not set"165 );166 require(167 rftCollection == _collection,168 "Wrong RFT collection"169 );170 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);171 address rftTokenAddress = rftCollectionContract.tokenContractAddress(_token);172 Token memory nftToken = rft2nftMapping[rftTokenAddress];173 require(174 nftToken._collection != address(0),175 "No corresponding NFT token found"176 );177 UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(rftTokenAddress);178 require(179 rftTokenContract.balanceOf(msg.sender) == rftTokenContract.totalSupply(),180 "Not all pieces are owned by the caller"181 );182 rftCollectionContract.transferFrom(msg.sender, address(this), _token);183 UniqueNFT(nftToken._collection).transferFrom(184 address(this),185 msg.sender,186 nftToken._tokenId187 );188 emit Defractionalized(rftTokenAddress, nftToken._collection, nftToken._tokenId);189 }190}