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, CrossAddress} 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)) public nft2rftMapping;20 mapping(address => Token) public rft2nftMapping;21 //use constant to reduce gas cost22 bytes32 constant refungibleCollectionType = keccak256(bytes("ReFungible"));2324 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) external onlyOwner {56 require(rftCollection == address(0), "RFT collection is already set");57 UniqueRefungible refungibleContract = UniqueRefungible(_collection);58 string memory collectionType = refungibleContract.uniqueCollectionType();5960 // compare hashed to reduce gas cost61 require(62 keccak256(bytes(collectionType)) == refungibleCollectionType,63 "Wrong collection type. Collection is not refungible."64 );65 require(66 refungibleContract.isOwnerOrAdminCross(CrossAddress({eth: address(this), sub: uint256(0)})),67 "Fractionalizer contract should be an admin of the collection"68 );69 rftCollection = _collection;70 emit RFTCollectionSet(rftCollection);71 }7273 /// Creates and sets RFT collection that contract will work with. RFT tokens for fractionalized NFT tokens74 /// would be created in this collection.75 /// @dev Throws if RFT collection is already configured for this contract.76 /// Can only be called by contract owner.77 /// @param _name name for created RFT collection.78 /// @param _description description for created RFT collection.79 /// @param _tokenPrefix token prefix for created RFT collection.80 function createAndSetRFTCollection(81 string calldata _name,82 string calldata _description,83 string calldata _tokenPrefix84 ) external payable onlyOwner {85 require(rftCollection == address(0), "RFT collection is already set");86 address collectionHelpers = 0x6C4E9fE1AE37a41E93CEE429e8E1881aBdcbb54F;87 rftCollection = CollectionHelpers(collectionHelpers).createRFTCollection{value: msg.value}(88 _name,89 _description,90 _tokenPrefix91 );92 emit RFTCollectionSet(rftCollection);93 }9495 /// Allow or disallow NFT collection tokens from being fractionalized by this contract.96 /// @dev Can only be called by contract owner.97 /// @param collection NFT token address.98 /// @param status `true` to allow and `false` to disallow NFT token.99 function setNftCollectionIsAllowed(address collection, bool status) external onlyOwner {100 nftCollectionAllowList[collection] = status;101 emit AllowListSet(collection, status);102 }103104 /// Fractionilize NFT token.105 /// @dev Takes NFT token from `msg.sender` and transfers RFT token to `msg.sender`106 /// instead. Creates new RFT token if provided NFT token never was fractionalized107 /// by this contract or existing RFT token if it was.108 /// Throws if RFT collection isn't configured for this contract.109 /// Throws if fractionalization of provided NFT token is not allowed110 /// Throws if `msg.sender` is not owner of provided NFT token111 /// @param _collection NFT collection address112 /// @param _token id of NFT token to be fractionalized113 /// @param _pieces number of pieces new RFT token would have114 function nft2rft(115 address _collection,116 uint256 _token,117 uint128 _pieces118 ) external {119 require(rftCollection != address(0), "RFT collection is not set");120 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);121 require(122 nftCollectionAllowList[_collection] == true,123 "Fractionalization of this collection is not allowed by admin"124 );125 require(UniqueNFT(_collection).ownerOf(_token) == msg.sender, "Only token owner could fractionalize it");126 UniqueNFT(_collection).transferFrom(msg.sender, address(this), _token);127 uint256 rftTokenId;128 address rftTokenAddress;129 UniqueRefungibleToken rftTokenContract;130 if (nft2rftMapping[_collection][_token] == 0) {131 rftTokenId = rftCollectionContract.mint(address(this));132 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);133 nft2rftMapping[_collection][_token] = rftTokenId;134 rft2nftMapping[rftTokenAddress] = Token(_collection, _token);135136 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);137 } else {138 rftTokenId = nft2rftMapping[_collection][_token];139 rftTokenAddress = rftCollectionContract.tokenContractAddress(rftTokenId);140 rftTokenContract = UniqueRefungibleToken(rftTokenAddress);141 }142 rftTokenContract.repartition(_pieces);143 rftTokenContract.transfer(msg.sender, _pieces);144 emit Fractionalized(_collection, _token, rftTokenAddress, _pieces);145 }146147 /// Defrationalize NFT token.148 /// @dev Takes RFT token from `msg.sender` and transfers corresponding NFT token149 /// to `msg.sender` instead.150 /// Throws if RFT collection isn't configured for this contract.151 /// Throws if provided RFT token is no from configured RFT collection.152 /// Throws if RFT token was not created by this contract.153 /// Throws if `msg.sender` isn't owner of all RFT token pieces.154 /// @param _collection RFT collection address155 /// @param _token id of RFT token156 function rft2nft(address _collection, uint256 _token) external {157 require(rftCollection != address(0), "RFT collection is not set");158 require(rftCollection == _collection, "Wrong RFT collection");159 UniqueRefungible rftCollectionContract = UniqueRefungible(rftCollection);160 address rftTokenAddress = rftCollectionContract.tokenContractAddress(_token);161 Token memory nftToken = rft2nftMapping[rftTokenAddress];162 require(nftToken._collection != address(0), "No corresponding NFT token found");163 UniqueRefungibleToken rftTokenContract = UniqueRefungibleToken(rftTokenAddress);164 require(165 rftTokenContract.balanceOf(msg.sender) == rftTokenContract.totalSupply(),166 "Not all pieces are owned by the caller"167 );168 rftCollectionContract.transferFrom(msg.sender, address(this), _token);169 UniqueNFT(nftToken._collection).transferFrom(address(this), msg.sender, nftToken._tokenId);170 emit Defractionalized(rftTokenAddress, nftToken._collection, nftToken._tokenId);171 }172}