123456789101112131415161718import {readFile} from 'fs/promises';1920import {IKeyringPair} from '@polkadot/types/types';21import {evmToAddress} from '@polkadot/util-crypto';2223import {Contract} from 'web3-eth-contract';2425import {usingEthPlaygrounds, expect, itEth, EthUniqueHelper} from '../util';26import {CompiledContract} from '../util/playgrounds/types';27import {requirePalletsOrSkip, Pallets, makeNames} from '../../util';2829const {dirname} = makeNames(import.meta.url);3031let compiledFractionalizer: CompiledContract;3233const compileContract = async (helper: EthUniqueHelper): Promise<CompiledContract> => {34 if(!compiledFractionalizer) {35 compiledFractionalizer = await helper.ethContract.compile('Fractionalizer', (await readFile(`${dirname}/Fractionalizer.sol`)).toString(), [36 {solPath: 'api/CollectionHelpers.sol', fsPath: `${dirname}/../api/CollectionHelpers.sol`},37 {solPath: 'api/ContractHelpers.sol', fsPath: `${dirname}/../api/ContractHelpers.sol`},38 {solPath: 'api/UniqueRefungibleToken.sol', fsPath: `${dirname}/../api/UniqueRefungibleToken.sol`},39 {solPath: 'api/UniqueRefungible.sol', fsPath: `${dirname}/../api/UniqueRefungible.sol`},40 {solPath: 'api/UniqueNFT.sol', fsPath: `${dirname}/../api/UniqueNFT.sol`},41 ]);42 }43 return compiledFractionalizer;44};454647const deployContract = async (helper: EthUniqueHelper, owner: string): Promise<Contract> => {48 const compiled = await compileContract(helper);49 return await helper.ethContract.deployByAbi(owner, compiled.abi, compiled.object);50};515253const initContract = async (helper: EthUniqueHelper, owner: string): Promise<{contract: Contract, rftCollectionAddress: string}> => {54 const fractionalizer = await deployContract(helper, owner);55 const amount = 10n * helper.balance.getOneTokenNominal();56 const web3 = helper.getWeb3();57 await web3.eth.sendTransaction({from: owner, to: fractionalizer.options.address, value: `${amount}`, gas: helper.eth.DEFAULT_GAS});58 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({value: Number(2n * helper.balance.getOneTokenNominal())});59 const rftCollectionAddress = result.events.RFTCollectionSet.returnValues._collection;60 return {contract: fractionalizer, rftCollectionAddress};61};6263const mintRFTToken = async (helper: EthUniqueHelper, owner: string, fractionalizer: Contract, amount: bigint): Promise<{64 nftCollectionAddress: string, nftTokenId: number, rftTokenAddress: string65}> => {66 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');67 const nftContract = await helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);68 const mintResult = await nftContract.methods.mint(owner).send({from: owner});69 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;7071 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});72 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});73 const result = await fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, amount).send({from: owner});74 const {_collection, _tokenId, _rftToken} = result.events.Fractionalized.returnValues;75 return {76 nftCollectionAddress: _collection,77 nftTokenId: _tokenId,78 rftTokenAddress: _rftToken,79 };80};818283describe('Fractionalizer contract usage', () => {84 let donor: IKeyringPair;8586 before(async function() {87 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {88 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);89 donor = await privateKey({url: import.meta.url});90 });91 });9293 itEth('Set RFT collection', async ({helper}) => {94 const owner = await helper.eth.createAccountWithBalance(donor);95 const fractionalizer = await deployContract(helper, owner);96 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');97 const rftContract = await helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);9899 const fractionalizerAddressCross = helper.ethCrossAccount.fromAddress(fractionalizer.options.address);100 await rftContract.methods.addCollectionAdminCross(fractionalizerAddressCross).send({from: owner});101 const result = await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});102 expect(result.events).to.be.like({103 RFTCollectionSet: {104 returnValues: {105 _collection: rftCollection.collectionAddress,106 },107 },108 });109 });110111 itEth('Mint RFT collection', async ({helper}) => {112 const owner = await helper.eth.createAccountWithBalance(donor);113 const fractionalizer = await deployContract(helper, owner);114 await helper.balance.transferToSubstrate(donor, evmToAddress(fractionalizer.options.address), 10n * helper.balance.getOneTokenNominal());115116 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner, value: Number(2n * helper.balance.getOneTokenNominal())});117 expect(result.events).to.be.like({118 RFTCollectionSet: {},119 });120 expect(result.events.RFTCollectionSet.returnValues._collection).to.be.ok;121 });122123 itEth('Set Allowlist', async ({helper}) => {124 const owner = await helper.eth.createAccountWithBalance(donor);125 const {contract: fractionalizer} = await initContract(helper, owner);126 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');127128 const result1 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});129 expect(result1.events).to.be.like({130 AllowListSet: {131 returnValues: {132 _collection: nftCollection.collectionAddress,133 _status: true,134 },135 },136 });137 const result2 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, false).send({from: owner});138 expect(result2.events).to.be.like({139 AllowListSet: {140 returnValues: {141 _collection: nftCollection.collectionAddress,142 _status: false,143 },144 },145 });146 });147148 itEth('NFT to RFT', async ({helper}) => {149 const owner = await helper.eth.createAccountWithBalance(donor);150151 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');152 const nftContract = await helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);153 const mintResult = await nftContract.methods.mint(owner).send({from: owner});154 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;155156 const {contract: fractionalizer} = await initContract(helper, owner);157158 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});159 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});160 const result = await fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).send({from: owner});161 expect(result.events).to.be.like({162 Fractionalized: {163 returnValues: {164 _collection: nftCollection.collectionAddress,165 _tokenId: nftTokenId,166 _amount: '100',167 },168 },169 });170 const rftTokenAddress = result.events.Fractionalized.returnValues._rftToken;171172 173 const rftTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress);174 expect(await rftTokenContract.methods.balanceOf(owner).call()).to.equal('100');175 });176177 itEth('RFT to NFT', async ({helper}) => {178 const owner = await helper.eth.createAccountWithBalance(donor);179180 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);181 const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await mintRFTToken(helper, owner, fractionalizer, 100n);182183 const {collectionId, tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);184 const refungibleAddress = helper.ethAddress.fromCollectionId(collectionId);185 expect(rftCollectionAddress).to.be.equal(refungibleAddress);186 const refungibleTokenContract = await helper.ethNativeContract.rftToken(rftTokenAddress, owner);187 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send({from: owner});188 const result = await fractionalizer.methods.rft2nft(refungibleAddress, tokenId).send({from: owner});189 expect(result.events).to.be.like({190 Defractionalized: {191 returnValues: {192 _rftToken: rftTokenAddress,193 _nftCollection: nftCollectionAddress,194 _nftTokenId: nftTokenId,195 },196 },197 });198 });199200 itEth('Test fractionalizer NFT <-> RFT mapping ', async ({helper}) => {201 const owner = await helper.eth.createAccountWithBalance(donor);202203 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);204 const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await mintRFTToken(helper, owner, fractionalizer, 100n);205206 const {collectionId, tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);207 const refungibleAddress = helper.ethAddress.fromCollectionId(collectionId);208 expect(rftCollectionAddress).to.be.equal(refungibleAddress);209 const refungibleTokenContract = await helper.ethNativeContract.rftToken(rftTokenAddress, owner);210 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send({from: owner});211212 const rft2nft = await fractionalizer.methods.rft2nftMapping(rftTokenAddress).call();213 expect(rft2nft).to.be.like({214 _collection: nftCollectionAddress,215 _tokenId: nftTokenId,216 });217218 const nft2rft = await fractionalizer.methods.nft2rftMapping(nftCollectionAddress, nftTokenId).call();219 expect(nft2rft).to.be.eq(tokenId.toString());220 });221});222223224225describe('Negative Integration Tests for fractionalizer', () => {226 let donor: IKeyringPair;227228 before(async function() {229 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {230 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);231 donor = await privateKey({url: import.meta.url});232 });233 });234235 itEth('call setRFTCollection twice', async ({helper}) => {236 const owner = await helper.eth.createAccountWithBalance(donor);237 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');238 const refungibleContract = await helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);239240 const fractionalizer = await deployContract(helper, owner);241 const fractionalizerAddressCross = helper.ethCrossAccount.fromAddress(fractionalizer.options.address);242 await refungibleContract.methods.addCollectionAdminCross(fractionalizerAddressCross).send({from: owner});243 await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});244245 await expect(fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).call())246 .to.be.rejectedWith(/RFT collection is already set$/g);247 });248249 itEth('call setRFTCollection with NFT collection', async ({helper}) => {250 const owner = await helper.eth.createAccountWithBalance(donor);251 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');252 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);253254 const fractionalizer = await deployContract(helper, owner);255 const fractionalizerAddressCross = helper.ethCrossAccount.fromAddress(fractionalizer.options.address);256 await nftContract.methods.addCollectionAdminCross(fractionalizerAddressCross).send({from: owner});257258 await expect(fractionalizer.methods.setRFTCollection(nftCollection.collectionAddress).call())259 .to.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g);260 });261262 itEth('call setRFTCollection while not collection admin', async ({helper}) => {263 const owner = await helper.eth.createAccountWithBalance(donor);264 const fractionalizer = await deployContract(helper, owner);265 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');266267 await expect(fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).call())268 .to.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g);269 });270271 itEth('call setRFTCollection after createAndSetRFTCollection', async ({helper}) => {272 const owner = await helper.eth.createAccountWithBalance(donor);273 const fractionalizer = await deployContract(helper, owner);274 await helper.balance.transferToSubstrate(donor, evmToAddress(fractionalizer.options.address), 10n * helper.balance.getOneTokenNominal());275276 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner, value: Number(2n * helper.balance.getOneTokenNominal())});277 const collectionIdAddress = result.events.RFTCollectionSet.returnValues._collection;278279 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())280 .to.be.rejectedWith(/RFT collection is already set$/g);281 });282283 itEth('call nft2rft without setting RFT collection for contract', async ({helper}) => {284 const owner = await helper.eth.createAccountWithBalance(donor);285286 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');287 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);288 const mintResult = await nftContract.methods.mint(owner).send({from: owner});289 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;290291 const fractionalizer = await deployContract(helper, owner);292293 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())294 .to.be.rejectedWith(/RFT collection is not set$/g);295 });296297 itEth('call nft2rft while not owner of NFT token', async ({helper}) => {298 const owner = await helper.eth.createAccountWithBalance(donor);299 const nftOwner = await helper.eth.createAccountWithBalance(donor);300301 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');302 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);303 const mintResult = await nftContract.methods.mint(owner).send({from: owner});304 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;305 await nftContract.methods.transfer(nftOwner, 1).send({from: owner});306307308 const {contract: fractionalizer} = await initContract(helper, owner);309 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});310311 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call({from: owner}))312 .to.be.rejectedWith(/Only token owner could fractionalize it$/g);313 });314315 itEth('call nft2rft while not in list of allowed accounts', async ({helper}) => {316 const owner = await helper.eth.createAccountWithBalance(donor);317318 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');319 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);320 const mintResult = await nftContract.methods.mint(owner).send({from: owner});321 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;322323 const {contract: fractionalizer} = await initContract(helper, owner);324325 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});326 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())327 .to.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g);328 });329330 itEth('call nft2rft while fractionalizer doesnt have approval for nft token', async ({helper}) => {331 const owner = await helper.eth.createAccountWithBalance(donor);332333 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');334 const nftContract = await helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);335 const mintResult = await nftContract.methods.mint(owner).send({from: owner});336 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;337338 const {contract: fractionalizer} = await initContract(helper, owner);339340 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});341 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())342 .to.be.rejectedWith(/ApprovedValueTooLow$/g);343 });344345 itEth('call rft2nft without setting RFT collection for contract', async ({helper}) => {346 const owner = await helper.eth.createAccountWithBalance(donor);347348 const fractionalizer = await deployContract(helper, owner);349 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');350 const refungibleContract = await helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);351 const mintResult = await refungibleContract.methods.mint(owner).send({from: owner});352 const rftTokenId = mintResult.events.Transfer.returnValues.tokenId;353354 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call({from: owner}))355 .to.be.rejectedWith(/RFT collection is not set$/g);356 });357358 itEth('call rft2nft for RFT token that is not from configured RFT collection', async ({helper}) => {359 const owner = await helper.eth.createAccountWithBalance(donor);360361 const {contract: fractionalizer} = await initContract(helper, owner);362 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');363 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);364 const mintResult = await refungibleContract.methods.mint(owner).send({from: owner});365 const rftTokenId = mintResult.events.Transfer.returnValues.tokenId;366367 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call())368 .to.be.rejectedWith(/Wrong RFT collection$/g);369 });370371 itEth('call rft2nft for RFT token that was not minted by fractionalizer contract', async ({helper}) => {372 const owner = await helper.eth.createAccountWithBalance(donor);373 const rftCollection = await helper.eth.createRFTCollection(owner, 'rft', 'RFT collection', 'RFT');374 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);375376 const fractionalizer = await deployContract(helper, owner);377378 const fractionalizerAddressCross = helper.ethCrossAccount.fromAddress(fractionalizer.options.address);379 await refungibleContract.methods.addCollectionAdminCross(fractionalizerAddressCross).send({from: owner});380 await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});381382 const mintResult = await refungibleContract.methods.mint(owner).send({from: owner});383 const rftTokenId = mintResult.events.Transfer.returnValues.tokenId;384385 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call())386 .to.be.rejectedWith(/No corresponding NFT token found$/g);387 });388389 itEth('call rft2nft without owning all RFT pieces', async ({helper}) => {390 const owner = await helper.eth.createAccountWithBalance(donor);391 const receiver = await helper.eth.createAccountWithBalance(donor);392393 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);394 const {rftTokenAddress} = await mintRFTToken(helper, owner, fractionalizer, 100n);395396 const {tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);397 const refungibleTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress, owner);398 await refungibleTokenContract.methods.transfer(receiver, 50).send({from: owner});399 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 50).send({from: receiver});400 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, tokenId).call({from: receiver}))401 .to.be.rejectedWith(/Not all pieces are owned by the caller$/g);402 });403404 itEth('send QTZ/UNQ to contract from non owner', async ({helper}) => {405 const owner = await helper.eth.createAccountWithBalance(donor);406 const payer = await helper.eth.createAccountWithBalance(donor);407408 const fractionalizer = await deployContract(helper, owner);409 const amount = 10n * helper.balance.getOneTokenNominal();410 const web3 = helper.getWeb3();411 await expect(web3.eth.sendTransaction({from: payer, to: fractionalizer.options.address, value: `${amount}`, gas: helper.eth.DEFAULT_GAS})).to.be.rejected;412 });413414 itEth('fractionalize NFT with NFT transfers disallowed', async ({helper}) => {415 const nftCollection = await helper.nft.mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});416417 const owner = await helper.eth.createAccountWithBalance(donor);418 const nftToken = await nftCollection.mintToken(donor, {Ethereum: owner});419 await helper.executeExtrinsic(donor, 'api.tx.unique.setTransfersEnabledFlag', [nftCollection.collectionId, false], true);420 const nftCollectionAddress = helper.ethAddress.fromCollectionId(nftCollection.collectionId);421 const {contract: fractionalizer} = await initContract(helper, owner);422 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send({from: owner});423424 const nftContract = await helper.ethNativeContract.collection(nftCollectionAddress, 'nft', owner);425 await nftContract.methods.approve(fractionalizer.options.address, nftToken.tokenId).send({from: owner});426 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftToken.tokenId, 100).call())427 .to.be.rejectedWith(/TransferNotAllowed$/g);428 });429430 itEth('fractionalize NFT with RFT transfers disallowed', async ({helper}) => {431 const owner = await helper.eth.createAccountWithBalance(donor);432433 const rftCollection = await helper.rft.mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});434 const rftCollectionAddress = helper.ethAddress.fromCollectionId(rftCollection.collectionId);435 const fractionalizer = await deployContract(helper, owner);436 await rftCollection.addAdmin(donor, {Ethereum: fractionalizer.options.address});437438 await fractionalizer.methods.setRFTCollection(rftCollectionAddress).send({from: owner});439 await helper.executeExtrinsic(donor, 'api.tx.unique.setTransfersEnabledFlag', [rftCollection.collectionId, false], true);440441 const nftCollection = await helper.eth.createNFTCollection(owner, 'nft', 'NFT collection', 'NFT');442 const nftContract = await helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);443 const mintResult = await nftContract.methods.mint(owner).send({from: owner});444 const nftTokenId = mintResult.events.Transfer.returnValues.tokenId;445446 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});447 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});448449 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100n).call())450 .to.be.rejectedWith(/TransferNotAllowed$/g);451 });452});