1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../util/playgrounds';1920describe('Integration Test: Unnesting', () => {21 let alice: IKeyringPair;2223 before(async () => {24 await usingPlaygrounds(async (helper, privateKey) => {25 const donor = privateKey('//Alice');26 [alice] = await helper.arrange.createAccounts([50n], donor);27 });28 });2930 itSub('NFT: allows the owner to successfully unnest a token', async ({helper}) => {31 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});32 const targetToken = await collection.mintToken(alice);33 34 35 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());3637 38 await expect(nestedToken.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}), 'while unnesting').to.be.fulfilled;39 expect(await nestedToken.getOwner()).to.be.deep.equal({Substrate: alice.address});4041 42 await nestedToken.nest(alice, targetToken);43 await expect(nestedToken.burnFrom(alice, targetToken.nestingAccount()), 'while burning').to.be.fulfilled;44 await expect(nestedToken.getOwner()).to.be.rejected;45 });4647 itSub('Fungible: allows the owner to successfully unnest a token', async ({helper}) => {48 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});49 const targetToken = await collection.mintToken(alice);5051 const collectionFT = await helper.ft.mintCollection(alice);52 53 54 await collectionFT.mint(alice, 10n, targetToken.nestingAccount());55 await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n), 'while unnesting').to.be.fulfilled;56 expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(9n);57 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(1n);5859 60 await collectionFT.transfer(alice, targetToken.nestingAccount(), 5n);61 await expect(collectionFT.burnTokensFrom(alice, targetToken.nestingAccount(), 6n), 'while burning').to.be.fulfilled;62 expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(4n);63 expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);64 expect(await targetToken.getChildren()).to.be.length(0);65 });6667 itSub.ifWithPallets('ReFungible: allows the owner to successfully unnest a token', [Pallets.ReFungible], async ({helper}) => {68 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});69 const targetToken = await collection.mintToken(alice);7071 const collectionRFT = await helper.rft.mintCollection(alice);72 73 74 const token = await collectionRFT.mintToken(alice, 10n, targetToken.nestingAccount());75 await expect(token.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n), 'while unnesting').to.be.fulfilled;76 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(9n);77 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(1n);7879 80 await token.transfer(alice, targetToken.nestingAccount(), 5n);81 await expect(token.burnFrom(alice, targetToken.nestingAccount(), 6n), 'while burning').to.be.fulfilled;82 expect(await token.getBalance({Substrate: alice.address})).to.be.equal(4n);83 expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);84 expect(await targetToken.getChildren()).to.be.length(0);85 });86});8788describe('Negative Test: Unnesting', () => {89 let alice: IKeyringPair;90 let bob: IKeyringPair;9192 before(async () => {93 await usingPlaygrounds(async (helper, privateKey) => {94 const donor = privateKey('//Alice');95 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);96 });97 });9899 itSub('Disallows a non-owner to unnest/burn a token', async ({helper}) => {100 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});101 const targetToken = await collection.mintToken(alice);102103 104 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());105106 107 await expect(nestedToken.unnest(bob, targetToken, {Substrate: alice.address})).to.be.rejectedWith(/common\.ApprovedValueTooLow/);108 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());109110 111 await expect(nestedToken.burnFrom(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.ApprovedValueTooLow/);112 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());113 });114115 116117 118 itSub('Prevents Ouroboros creation', async ({helper}) => {119 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});120 const targetToken = await collection.mintToken(alice);121122 123 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());124 await expect(targetToken.nest(alice, nestedToken)).to.be.rejectedWith(/^structure\.OuroborosDetected$/);125 });126});