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 ]).and.has.length(3);8485 86 await tokenC.burnFrom(alice, targetToken.nestingAccount(), 2n);87 expect(await targetToken.getChildren()).to.have.deep.members([88 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},89 {tokenId: 0, collectionId: collectionB.collectionId},90 ])91 .and.has.length(2);9293 94 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);95 expect(await targetToken.getChildren()).to.be.have.deep.members([96 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},97 {tokenId: 0, collectionId: collectionB.collectionId},98 ]).and.has.length(2);99 100 expect(await tokenA.getChildren()).to.have.deep.members([101 {tokenId: 0, collectionId: collectionB.collectionId},102 ]).and.has.length(1);103104 105 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);106 expect(await targetToken.getChildren()).to.have.deep.members([107 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},108 ]).and.has.length(1);109 expect(await tokenA.getChildren()).to.have.deep.members([110 {tokenId: 0, collectionId: collectionB.collectionId},111 ]).and.has.length(1);112 });113114 115 itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {116 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});117 const targetToken = await collection.mintToken(alice);118119 120 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());121 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});122 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());123124 125 const newToken = await collection.mintToken(alice);126127 128 await newToken.nest(alice, targetToken);129 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});130 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());131132 133 await targetToken.transfer(alice, {Substrate: bob.address});134 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});135 expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());136 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});137 expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());138139 140 await newToken.unnest(bob, targetToken, {Substrate: bob.address});141 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});142 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});143 });144});