1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '../../util/index.js';19import {CrossAccountId} from '@unique/playgrounds/unique.js';2021describe('Composite nesting tests', () => {22 let alice: IKeyringPair;23 let bob: IKeyringPair;2425 before(async () => {26 await usingPlaygrounds(async (helper, privateKey) => {27 const donor = await privateKey({url: import.meta.url});28 [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29 });30 });3132 itSub('Checks token children e2e', async ({helper}) => {33 const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});34 const collectionB = await helper.ft.mintCollection(alice);35 const collectionC = await helper.rft.mintCollection(alice);36 const collectionNative = helper.ft.getCollectionObject(0);3738 const targetToken = await collectionA.mintToken(alice);39 expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');4041 42 const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());43 expect(await targetToken.getChildren()).to.have.deep.members([44 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},45 ]).and.has.length(1);4647 48 const tokenB = await collectionA.mintToken(alice);49 await tokenB.nest(alice, targetToken);50 expect(await targetToken.getChildren()).to.have.deep.members([51 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},52 {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},53 ]).and.has.length(2);5455 56 await tokenB.unnest(alice, targetToken, {Substrate: bob.address});57 expect(await targetToken.getChildren()).to.have.deep.members([58 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},59 ]).and.has.length(1);6061 62 await collectionB.mint(alice, 10n);63 await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);64 expect(await targetToken.getChildren()).to.have.deep.members([65 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},66 {tokenId: 0, collectionId: collectionB.collectionId},67 ]).and.has.length(2);6869 70 const tokenC = await collectionC.mintToken(alice, 10n);71 await tokenC.transfer(alice, targetToken.nestingAccount(), 2n);72 expect(await targetToken.getChildren()).to.have.deep.members([73 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},74 {tokenId: 0, collectionId: collectionB.collectionId},75 {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},76 ]).and.has.length(3);7778 79 await collectionNative.transfer(alice, targetToken.nestingAccount(), 2n);80 expect(await targetToken.getChildren()).to.have.deep.members([81 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},82 {tokenId: 0, collectionId: collectionB.collectionId},83 {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},84 ]).and.has.length(3);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 ])92 .and.has.length(2);9394 95 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);96 expect(await targetToken.getChildren()).to.be.have.deep.members([97 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},98 {tokenId: 0, collectionId: collectionB.collectionId},99 ]).and.has.length(2);100 101 expect(await tokenA.getChildren()).to.have.deep.members([102 {tokenId: 0, collectionId: collectionB.collectionId},103 ]).and.has.length(1);104105 106 await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);107 expect(await targetToken.getChildren()).to.have.deep.members([108 {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},109 ]).and.has.length(1);110 expect(await tokenA.getChildren()).to.have.deep.members([111 {tokenId: 0, collectionId: collectionB.collectionId},112 ]).and.has.length(1);113 });114115 116 itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {117 const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});118 const targetToken = await collection.mintToken(alice);119120 121 const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());122 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});123 expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));124125 126 const newToken = await collection.mintToken(alice);127128 129 await newToken.nest(alice, targetToken);130 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});131 expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));132133 134 await targetToken.transfer(alice, {Substrate: bob.address});135 expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});136 expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));137 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});138 expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));139140 141 await newToken.unnest(bob, targetToken, {Substrate: bob.address});142 expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});143 expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});144 });145});