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/playgrounds';26import {CompiledContract} from '../util/playgrounds/types';27import {requirePalletsOrSkip, Pallets} from '../../util/playgrounds';282930let compiledFractionalizer: CompiledContract;3132const compileContract = async (helper: EthUniqueHelper): Promise<CompiledContract> => {33 if(!compiledFractionalizer) {34 compiledFractionalizer = await helper.ethContract.compile('Fractionalizer', (await readFile(`${__dirname}/Fractionalizer.sol`)).toString(), [35 {solPath: 'api/CollectionHelpers.sol', fsPath: `${__dirname}/../api/CollectionHelpers.sol`},36 {solPath: 'api/ContractHelpers.sol', fsPath: `${__dirname}/../api/ContractHelpers.sol`},37 {solPath: 'api/UniqueRefungibleToken.sol', fsPath: `${__dirname}/../api/UniqueRefungibleToken.sol`},38 {solPath: 'api/UniqueRefungible.sol', fsPath: `${__dirname}/../api/UniqueRefungible.sol`},39 {solPath: 'api/UniqueNFT.sol', fsPath: `${__dirname}/../api/UniqueNFT.sol`},40 ]);41 }42 return compiledFractionalizer;43};444546const deployContract = async (helper: EthUniqueHelper, owner: string): Promise<Contract> => {47 const compiled = await compileContract(helper);48 return await helper.ethContract.deployByAbi(owner, compiled.abi, compiled.object);49};505152const initContract = async (helper: EthUniqueHelper, owner: string): Promise<{contract: Contract, rftCollectionAddress: string}> => {53 const fractionalizer = await deployContract(helper, owner);54 const amount = 10n * helper.balance.getOneTokenNominal();55 const web3 = helper.getWeb3();56 await web3.eth.sendTransaction({from: owner, to: fractionalizer.options.address, value: `${amount}`, gas: helper.eth.DEFAULT_GAS});57 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({value: Number(2n * helper.balance.getOneTokenNominal())});58 const rftCollectionAddress = result.events.RFTCollectionSet.returnValues._collection;59 return {contract: fractionalizer, rftCollectionAddress};60};6162const mintRFTToken = async (helper: EthUniqueHelper, owner: string, fractionalizer: Contract, amount: bigint): Promise<{63 nftCollectionAddress: string, nftTokenId: number, rftTokenAddress: string64}> => {65 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');66 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);67 const nftTokenId = await nftContract.methods.nextTokenId().call();68 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});6970 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});71 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});72 const result = await fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, amount).send({from: owner});73 const {_collection, _tokenId, _rftToken} = result.events.Fractionalized.returnValues;74 return {75 nftCollectionAddress: _collection,76 nftTokenId: _tokenId,77 rftTokenAddress: _rftToken,78 };79};808182describe('Fractionalizer contract usage', () => {83 let donor: IKeyringPair;8485 before(async function() {86 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {87 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);88 donor = privateKey('//Alice');89 });90 });9192 itEth('Set RFT collection', async ({helper}) => {93 const owner = await helper.eth.createAccountWithBalance(donor, 10n);94 const fractionalizer = await deployContract(helper, owner);95 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');96 const rftContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);9798 await rftContract.methods.addCollectionAdmin(fractionalizer.options.address).send({from: owner});99 const result = await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});100 expect(result.events).to.be.like({101 RFTCollectionSet: {102 returnValues: {103 _collection: rftCollection.collectionAddress,104 },105 },106 });107 });108109 itEth('Mint RFT collection', async ({helper}) => {110 const owner = await helper.eth.createAccountWithBalance(donor, 10n);111 const fractionalizer = await deployContract(helper, owner);112 await helper.balance.transferToSubstrate(donor, evmToAddress(fractionalizer.options.address), 10n * helper.balance.getOneTokenNominal());113114 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner, value: Number(2n * helper.balance.getOneTokenNominal())});115 expect(result.events).to.be.like({116 RFTCollectionSet: {},117 });118 expect(result.events.RFTCollectionSet.returnValues._collection).to.be.ok;119 });120121 itEth('Set Allowlist', async ({helper}) => {122 const owner = await helper.eth.createAccountWithBalance(donor, 20n);123 const {contract: fractionalizer} = await initContract(helper, owner);124 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');125126 const result1 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});127 expect(result1.events).to.be.like({128 AllowListSet: {129 returnValues: {130 _collection: nftCollection.collectionAddress,131 _status: true,132 },133 },134 });135 const result2 = await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, false).send({from: owner});136 expect(result2.events).to.be.like({137 AllowListSet: {138 returnValues: {139 _collection: nftCollection.collectionAddress,140 _status: false,141 },142 },143 });144 });145146 itEth('NFT to RFT', async ({helper}) => {147 const owner = await helper.eth.createAccountWithBalance(donor, 20n);148149 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');150 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);151 const nftTokenId = await nftContract.methods.nextTokenId().call();152 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});153154 const {contract: fractionalizer} = await initContract(helper, owner);155156 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});157 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});158 const result = await fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).send({from: owner});159 expect(result.events).to.be.like({160 Fractionalized: {161 returnValues: {162 _collection: nftCollection.collectionAddress,163 _tokenId: nftTokenId,164 _amount: '100',165 },166 },167 });168 const rftTokenAddress = result.events.Fractionalized.returnValues._rftToken;169170 const rftTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress);171 expect(await rftTokenContract.methods.balanceOf(owner).call()).to.equal('100');172 });173174 itEth('RFT to NFT', async ({helper}) => {175 const owner = await helper.eth.createAccountWithBalance(donor, 20n);176177 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);178 const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await mintRFTToken(helper, owner, fractionalizer, 100n);179180 const {collectionId, tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);181 const refungibleAddress = helper.ethAddress.fromCollectionId(collectionId);182 expect(rftCollectionAddress).to.be.equal(refungibleAddress);183 const refungibleTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress, owner);184 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send({from: owner});185 const result = await fractionalizer.methods.rft2nft(refungibleAddress, tokenId).send({from: owner});186 expect(result.events).to.be.like({187 Defractionalized: {188 returnValues: {189 _rftToken: rftTokenAddress,190 _nftCollection: nftCollectionAddress,191 _nftTokenId: nftTokenId,192 },193 },194 });195 });196197 itEth('Test fractionalizer NFT <-> RFT mapping ', async ({helper}) => {198 const owner = await helper.eth.createAccountWithBalance(donor, 20n);199200 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);201 const {rftTokenAddress, nftCollectionAddress, nftTokenId} = await mintRFTToken(helper, owner, fractionalizer, 100n);202203 const {collectionId, tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);204 const refungibleAddress = helper.ethAddress.fromCollectionId(collectionId);205 expect(rftCollectionAddress).to.be.equal(refungibleAddress);206 const refungibleTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress, owner);207 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 100).send({from: owner});208209 const rft2nft = await fractionalizer.methods.rft2nftMapping(rftTokenAddress).call();210 expect(rft2nft).to.be.like({211 _collection: nftCollectionAddress,212 _tokenId: nftTokenId,213 });214215 const nft2rft = await fractionalizer.methods.nft2rftMapping(nftCollectionAddress, nftTokenId).call();216 expect(nft2rft).to.be.eq(tokenId.toString());217 });218});219220221222describe('Negative Integration Tests for fractionalizer', () => {223 let donor: IKeyringPair;224225 before(async function() {226 await usingEthPlaygrounds(async (helper: EthUniqueHelper, privateKey) => {227 requirePalletsOrSkip(this, helper, [Pallets.ReFungible]);228 donor = privateKey('//Alice');229 });230 });231232 itEth('call setRFTCollection twice', async ({helper}) => {233 const owner = await helper.eth.createAccountWithBalance(donor, 20n);234 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');235 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);236237 const fractionalizer = await deployContract(helper, owner);238 await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send({from: owner});239 await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});240241 await expect(fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).call())242 .to.be.rejectedWith(/RFT collection is already set$/g);243 });244245 itEth('call setRFTCollection with NFT collection', async ({helper}) => {246 const owner = await helper.eth.createAccountWithBalance(donor, 20n);247 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');248 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);249250 const fractionalizer = await deployContract(helper, owner);251 await nftContract.methods.addCollectionAdmin(fractionalizer.options.address).send({from: owner});252253 await expect(fractionalizer.methods.setRFTCollection(nftCollection.collectionAddress).call())254 .to.be.rejectedWith(/Wrong collection type. Collection is not refungible.$/g);255 });256257 itEth('call setRFTCollection while not collection admin', async ({helper}) => {258 const owner = await helper.eth.createAccountWithBalance(donor, 20n);259 const fractionalizer = await deployContract(helper, owner);260 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');261262 await expect(fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).call())263 .to.be.rejectedWith(/Fractionalizer contract should be an admin of the collection$/g);264 });265266 itEth('call setRFTCollection after createAndSetRFTCollection', async ({helper}) => {267 const owner = await helper.eth.createAccountWithBalance(donor, 20n);268 const fractionalizer = await deployContract(helper, owner);269 await helper.balance.transferToSubstrate(donor, evmToAddress(fractionalizer.options.address), 10n * helper.balance.getOneTokenNominal());270271 const result = await fractionalizer.methods.createAndSetRFTCollection('A', 'B', 'C').send({from: owner, value: Number(2n * helper.balance.getOneTokenNominal())});272 const collectionIdAddress = result.events.RFTCollectionSet.returnValues._collection;273274 await expect(fractionalizer.methods.setRFTCollection(collectionIdAddress).call())275 .to.be.rejectedWith(/RFT collection is already set$/g);276 });277278 itEth('call nft2rft without setting RFT collection for contract', async ({helper}) => {279 const owner = await helper.eth.createAccountWithBalance(donor, 20n);280281 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');282 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);283 const nftTokenId = await nftContract.methods.nextTokenId().call();284 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});285286 const fractionalizer = await deployContract(helper, owner);287288 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())289 .to.be.rejectedWith(/RFT collection is not set$/g);290 });291292 itEth('call nft2rft while not owner of NFT token', async ({helper}) => {293 const owner = await helper.eth.createAccountWithBalance(donor, 20n);294 const nftOwner = await helper.eth.createAccountWithBalance(donor, 10n);295296 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');297 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);298 const nftTokenId = await nftContract.methods.nextTokenId().call();299 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});300 await nftContract.methods.transfer(nftOwner, 1).send({from: owner});301302303 const {contract: fractionalizer} = await initContract(helper, owner);304 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});305306 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call({from: owner}))307 .to.be.rejectedWith(/Only token owner could fractionalize it$/g);308 });309310 itEth('call nft2rft while not in list of allowed accounts', async ({helper}) => {311 const owner = await helper.eth.createAccountWithBalance(donor, 20n);312313 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');314 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);315 const nftTokenId = await nftContract.methods.nextTokenId().call();316 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});317318 const {contract: fractionalizer} = await initContract(helper, owner);319320 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});321 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())322 .to.be.rejectedWith(/Fractionalization of this collection is not allowed by admin$/g);323 });324325 itEth('call nft2rft while fractionalizer doesnt have approval for nft token', async ({helper}) => {326 const owner = await helper.eth.createAccountWithBalance(donor, 20n);327328 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');329 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);330 const nftTokenId = await nftContract.methods.nextTokenId().call();331 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});332333 const {contract: fractionalizer} = await initContract(helper, owner);334335 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});336 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100).call())337 .to.be.rejectedWith(/ApprovedValueTooLow$/g);338 });339340 itEth('call rft2nft without setting RFT collection for contract', async ({helper}) => {341 const owner = await helper.eth.createAccountWithBalance(donor, 20n);342343 const fractionalizer = await deployContract(helper, owner);344 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');345 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);346 const rftTokenId = await refungibleContract.methods.nextTokenId().call();347 await refungibleContract.methods.mint(owner, rftTokenId).send({from: owner});348 349 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call({from: owner}))350 .to.be.rejectedWith(/RFT collection is not set$/g);351 });352353 itEth('call rft2nft for RFT token that is not from configured RFT collection', async ({helper}) => {354 const owner = await helper.eth.createAccountWithBalance(donor, 20n);355356 const {contract: fractionalizer} = await initContract(helper, owner);357 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');358 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);359 const rftTokenId = await refungibleContract.methods.nextTokenId().call();360 await refungibleContract.methods.mint(owner, rftTokenId).send({from: owner});361 362 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call())363 .to.be.rejectedWith(/Wrong RFT collection$/g);364 });365366 itEth('call rft2nft for RFT token that was not minted by fractionalizer contract', async ({helper}) => {367 const owner = await helper.eth.createAccountWithBalance(donor, 20n);368 const rftCollection = await helper.eth.createRefungibleCollection(owner, 'rft', 'RFT collection', 'RFT');369 const refungibleContract = helper.ethNativeContract.collection(rftCollection.collectionAddress, 'rft', owner);370371 const fractionalizer = await deployContract(helper, owner);372373 await refungibleContract.methods.addCollectionAdmin(fractionalizer.options.address).send({from: owner});374 await fractionalizer.methods.setRFTCollection(rftCollection.collectionAddress).send({from: owner});375376 const rftTokenId = await refungibleContract.methods.nextTokenId().call();377 await refungibleContract.methods.mint(owner, rftTokenId).send({from: owner});378 379 await expect(fractionalizer.methods.rft2nft(rftCollection.collectionAddress, rftTokenId).call())380 .to.be.rejectedWith(/No corresponding NFT token found$/g);381 });382383 itEth('call rft2nft without owning all RFT pieces', async ({helper}) => {384 const owner = await helper.eth.createAccountWithBalance(donor, 20n);385 const receiver = await helper.eth.createAccountWithBalance(donor, 10n);386387 const {contract: fractionalizer, rftCollectionAddress} = await initContract(helper, owner);388 const {rftTokenAddress} = await mintRFTToken(helper, owner, fractionalizer, 100n);389 390 const {tokenId} = helper.ethAddress.extractTokenId(rftTokenAddress);391 const refungibleTokenContract = helper.ethNativeContract.rftToken(rftTokenAddress, owner);392 await refungibleTokenContract.methods.transfer(receiver, 50).send({from: owner});393 await refungibleTokenContract.methods.approve(fractionalizer.options.address, 50).send({from: receiver});394 await expect(fractionalizer.methods.rft2nft(rftCollectionAddress, tokenId).call({from: receiver}))395 .to.be.rejectedWith(/Not all pieces are owned by the caller$/g);396 });397398 itEth('send QTZ/UNQ to contract from non owner', async ({helper}) => {399 const owner = await helper.eth.createAccountWithBalance(donor, 20n);400 const payer = await helper.eth.createAccountWithBalance(donor, 10n);401402 const fractionalizer = await deployContract(helper, owner);403 const amount = 10n * helper.balance.getOneTokenNominal();404 const web3 = helper.getWeb3();405 await expect(web3.eth.sendTransaction({from: payer, to: fractionalizer.options.address, value: `${amount}`, gas: helper.eth.DEFAULT_GAS})).to.be.rejected;406 });407408 itEth('fractionalize NFT with NFT transfers disallowed', async ({helper}) => {409 const nftCollection = await helper.nft.mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});410411 const owner = await helper.eth.createAccountWithBalance(donor, 20n);412 const nftToken = await nftCollection.mintToken(donor, {Ethereum: owner});413 await helper.executeExtrinsic(donor, 'api.tx.unique.setTransfersEnabledFlag', [nftCollection.collectionId, false], true);414 const nftCollectionAddress = helper.ethAddress.fromCollectionId(nftCollection.collectionId);415 const {contract: fractionalizer} = await initContract(helper, owner);416 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollectionAddress, true).send({from: owner});417418 const nftContract = helper.ethNativeContract.collection(nftCollectionAddress, 'nft', owner);419 await nftContract.methods.approve(fractionalizer.options.address, nftToken.tokenId).send({from: owner});420 await expect(fractionalizer.methods.nft2rft(nftCollectionAddress, nftToken.tokenId, 100).call())421 .to.be.rejectedWith(/TransferNotAllowed$/g);422 });423 424 itEth('fractionalize NFT with RFT transfers disallowed', async ({helper}) => {425 const owner = await helper.eth.createAccountWithBalance(donor, 20n);426427 const rftCollection = await helper.rft.mintCollection(donor, {name: 'A', description: 'B', tokenPrefix: 'C'});428 const rftCollectionAddress = helper.ethAddress.fromCollectionId(rftCollection.collectionId);429 const fractionalizer = await deployContract(helper, owner);430 await rftCollection.addAdmin(donor, {Ethereum: fractionalizer.options.address});431432 await fractionalizer.methods.setRFTCollection(rftCollectionAddress).send({from: owner});433 await helper.executeExtrinsic(donor, 'api.tx.unique.setTransfersEnabledFlag', [rftCollection.collectionId, false], true);434435 const nftCollection = await helper.eth.createNonfungibleCollection(owner, 'nft', 'NFT collection', 'NFT');436 const nftContract = helper.ethNativeContract.collection(nftCollection.collectionAddress, 'nft', owner);437 const nftTokenId = await nftContract.methods.nextTokenId().call();438 await nftContract.methods.mint(owner, nftTokenId).send({from: owner});439440 await fractionalizer.methods.setNftCollectionIsAllowed(nftCollection.collectionAddress, true).send({from: owner});441 await nftContract.methods.approve(fractionalizer.options.address, nftTokenId).send({from: owner});442443 await expect(fractionalizer.methods.nft2rft(nftCollection.collectionAddress, nftTokenId, 100n).call())444 .to.be.rejectedWith(/TransferNotAllowed$/g);445 });446});