1234567891011121314151617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util';1920describe('Composite nesting tests', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({url: import.meta.url});27 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);28 });29 });3031 itSub('Checks token children e2e', async ({helper}) => {32 const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});33 const collectionB = await helper.ft.mintCollection(alice);34 const collectionC = await helper.rft.mintCollection(alice);35 const collectionNative = helper.ft.getCollectionObject(0);3637 const targetToken = await collectionA.mintToken(alice);38 expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');3940 41 const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());42 expect(await targetToken.getChildren()).to.have.deep.members([43 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},44 ]).and.has.length(1);4546 47 const tokenB = await collectionA.mintToken(alice);48 await tokenB.nest(alice, targetToken);49 expect(await targetToken.getChildren()).to.have.deep.members([50 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},51 {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},52 ]).and.has.length(2);5354 55 await tokenB.unnest(alice, targetToken, {Substrate: bob.address});56 expect(await targetToken.getChildren()).to.have.deep.members([57 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},58 ]).and.has.length(1);5960 61 await collectionB.mint(alice, 10n);62 await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);63 expect(await targetToken.getChildren()).to.have.deep.members([64 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},65 {tokenId: 0, collectionId: collectionB.collectionId},66 ]).and.has.length(2);6768 69 const tokenC = await collectionC.mintToken(alice, 10n);70 await tokenC.transfer(alice, targetToken.nestingAccount(), 2n);71 expect(await targetToken.getChildren()).to.have.deep.members([72 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},73 {tokenId: 0, collectionId: collectionB.collectionId},74 {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},75 ]).and.has.length(3);7677 78 await collectionNative.transfer(alice, targetToken.nestingAccount(), 2n);79 expect(await targetToken.getChildren()).to.have.deep.members([80 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},81 {tokenId: 0, collectionId: collectionB.collectionId},82 {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},83 {tokenId: 0, collectionId: collectionNative.collectionId},84 ]).and.has.length(4);8586 87 await tokenC.burnFrom(alice, targetToken.nestingAccount(), 2n);88 expect(await targetToken.getChildren()).to.have.deep.members([89 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},90 {tokenId: 0, collectionId: collectionB.collectionId},91 {tokenId: 0, collectionId: collectionNative.collectionId},92 ])93 .and.has.length(3);9495 96 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);97 expect(await targetToken.getChildren()).to.be.have.deep.members([98 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},99 {tokenId: 0, collectionId: collectionB.collectionId},100 {tokenId: 0, collectionId: collectionNative.collectionId},101 ]).and.has.length(3);102 103 expect(await tokenA.getChildren()).to.have.deep.members([104 {tokenId: 0, collectionId: collectionB.collectionId},105 ]).and.has.length(1);106107 108 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);109 expect(await targetToken.getChildren()).to.have.deep.members([110 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},111 {tokenId: 0, collectionId: collectionNative.collectionId},112 ]).and.has.length(2);113 expect(await tokenA.getChildren()).to.have.deep.members([114 {tokenId: 0, collectionId: collectionB.collectionId},115 ]).and.has.length(1);116 });117118 119 itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {120 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});121 const targetToken = await collection.mintToken(alice);122123 124 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());125 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});126 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());127128 129 const newToken = await collection.mintToken(alice);130131 132 await newToken.nest(alice, targetToken);133 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});134 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());135136 137 await targetToken.transfer(alice, {Substrate: bob.address});138 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});139 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());140 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});141 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());142143 144 await newToken.unnest(bob, targetToken, {Substrate: bob.address});145 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});146 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});147 });148});