git.delta.rocks / unique-network / refs/commits / c7a326d2e299

difftreelog

test owner/admin unnest

Daniel Shiposha2023-01-16parent: #0170864.patch.diff
in: master

2 files changed

modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
before · tests/src/nesting/nest.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../util';19import {UniqueNFToken, UniqueRFToken} from '../util/playgrounds/unique';2021describe('Integration Test: 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({filename: __filename});28      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29    });30  });3132  itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {33    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});34    const targetToken = await collection.mintToken(alice);3536    // Create an immediately nested token37    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());38    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});39    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4041    // Create a token to be nested42    const newToken = await collection.mintToken(alice);4344    // Nest45    await newToken.nest(alice, targetToken);46    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});47    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4849    // Move bundle to different user50    await targetToken.transfer(alice, {Substrate: bob.address});51    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});52    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());53    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});54    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());5556    // Unnest57    await newToken.unnest(bob, targetToken, {Substrate: bob.address});58    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});59    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});60  });6162  itSub('Transfers an already bundled token', async ({helper}) => {63    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});64    const tokenA = await collection.mintToken(alice);65    const tokenB = await collection.mintToken(alice);6667    // Create a nested token68    const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());69    expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());7071    // Transfer the nested token to another token72    await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;73    expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});74    expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());75  });7677  itSub('Checks token children', async ({helper}) => {78    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});79    const collectionB = await helper.ft.mintCollection(alice);8081    const targetToken = await collectionA.mintToken(alice);82    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');8384    // Create a nested NFT token85    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());86    expect(await targetToken.getChildren()).to.have.deep.members([87      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},88    ], 'Children contents check at nesting #1').and.be.length(1, 'Children length check at nesting #1');8990    // Create then nest91    const tokenB = await collectionA.mintToken(alice);92    await tokenB.nest(alice, targetToken);93    expect(await targetToken.getChildren()).to.have.deep.members([94      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},95      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},96    ], 'Children contents check at nesting #2').and.be.length(2, 'Children length check at nesting #2');9798    // Move token B to a different user outside the nesting tree99    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});100    expect(await targetToken.getChildren()).to.be.have.deep.members([101      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},102    ], 'Children contents check at nesting #3 (unnesting)').and.be.length(1, 'Children length check at nesting #3 (unnesting)');103104    // Create a fungible token in another collection and then nest105    await collectionB.mint(alice, 10n);106    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);107    expect(await targetToken.getChildren()).to.be.have.deep.members([108      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},109      {tokenId: 0, collectionId: collectionB.collectionId},110    ], 'Children contents check at nesting #4 (from another collection)')111      .and.be.length(2, 'Children length check at nesting #4 (from another collection)');112113    // Move part of the fungible token inside token A deeper in the nesting tree114    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);115    expect(await targetToken.getChildren()).to.be.have.deep.members([116      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},117      {tokenId: 0, collectionId: collectionB.collectionId},118    ], 'Children contents check at nesting #5 (deeper)').and.be.length(2, 'Children length check at nesting #5 (deeper)');119    expect(await tokenA.getChildren()).to.be.have.deep.members([120      {tokenId: 0, collectionId: collectionB.collectionId},121    ], 'Children contents check at nesting #5.5 (deeper)').and.be.length(1, 'Children length check at nesting #5.5 (deeper)');122123    // Move the remaining part of the fungible token inside token A deeper in the nesting tree124    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);125    expect(await targetToken.getChildren()).to.be.have.deep.members([126      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},127    ], 'Children contents check at nesting #6 (deeper)').and.be.length(1, 'Children length check at nesting #6 (deeper)');128    expect(await tokenA.getChildren()).to.be.have.deep.members([129      {tokenId: 0, collectionId: collectionB.collectionId},130    ], 'Children contents check at nesting #6.5 (deeper)').and.be.length(1, 'Children length check at nesting #6.5 (deeper)');131  });132});133134describe('Integration Test: Various token type nesting', () => {135  let alice: IKeyringPair;136  let bob: IKeyringPair;137  let charlie: IKeyringPair;138139  before(async () => {140    await usingPlaygrounds(async (helper, privateKey) => {141      const donor = await privateKey({filename: __filename});142      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 20n, 20n], donor);143    });144  });145146  itSub('Admin (NFT): allows an Admin to nest a token', async ({helper}) => {147    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true}}});148    await collection.addAdmin(alice, {Substrate: bob.address});149    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});150151    // Create an immediately nested token152    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());153    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});154    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());155156    // Create a token to be nested and nest157    const newToken = await collection.mintToken(bob);158    await newToken.nest(bob, targetToken);159    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});160    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());161  });162163  itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {164    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});165    await collection.addAdmin(alice, {Substrate: bob.address});166    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});167168    // Create an immediately nested token by an administrator169    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());170    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});171    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());172173    // Create a token to be nested and nest174    const newToken = await collection.mintToken(alice, {Substrate: charlie.address});175    await newToken.nest(charlie, targetToken);176    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});177    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());178  });179180  itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {181    const collectionA = await helper.nft.mintCollection(alice);182    await collectionA.addAdmin(alice, {Substrate: bob.address});183    const collectionB = await helper.nft.mintCollection(alice);184    await collectionB.addAdmin(alice, {Substrate: bob.address});185    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted:[collectionB.collectionId]}});186    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});187188    // Create an immediately nested token189    const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());190    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});191    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());192193    // Create a token to be nested and nest194    const newToken = await collectionB.mintToken(bob);195    await newToken.nest(bob, targetToken);196    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});197    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());198  });199200  // ---------- Non-Fungible ----------201202  itSub('NFT: allows an Owner to nest/unnest their token', async ({helper}) => {203    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});204    await collection.addToAllowList(alice, {Substrate: charlie.address});205    const targetToken = await collection.mintToken(charlie);206    await collection.addToAllowList(alice, targetToken.nestingAccount());207208    // Create an immediately nested token209    const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());210    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});211    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());212213    // Create a token to be nested and nest214    const newToken = await collection.mintToken(charlie);215    await newToken.nest(charlie, targetToken);216    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});217    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());218  });219220  itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {221    const collectionA = await helper.nft.mintCollection(alice);222    const collectionB = await helper.nft.mintCollection(alice);223    //await collectionB.addAdmin(alice, {Substrate: bob.address});224    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});225226    await collectionA.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionB.collectionId]}});227    await collectionA.addToAllowList(alice, {Substrate: charlie.address});228    await collectionA.addToAllowList(alice, targetToken.nestingAccount());229230    await collectionB.setPermissions(alice, {access: 'AllowList', mintMode: true});231    await collectionB.addToAllowList(alice, {Substrate: charlie.address});232    await collectionB.addToAllowList(alice, targetToken.nestingAccount());233234    // Create an immediately nested token235    const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());236    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});237    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());238239    // Create a token to be nested and nest240    const newToken = await collectionB.mintToken(charlie);241    await newToken.nest(charlie, targetToken);242    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});243    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());244  });245246  // ---------- Fungible ----------247248  itSub('Fungible: allows an Owner to nest/unnest their token', async ({helper}) => {249    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});250    const collectionFT = await helper.ft.mintCollection(alice);251    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});252253    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});254    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());255256    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});257    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});258    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());259260    // Create an immediately nested token261    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());262    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);263264    // Create a token to be nested and nest265    await collectionFT.mint(charlie, 5n);266    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);267    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);268  });269270  itSub('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {271    const collectionNFT = await helper.nft.mintCollection(alice);272    const collectionFT = await helper.ft.mintCollection(alice);273    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});274275    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionFT.collectionId]}});276    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});277    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());278279    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});280    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});281    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());282283    // Create an immediately nested token284    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());285    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);286287    // Create a token to be nested and nest288    await collectionFT.mint(charlie, 5n);289    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);290    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);291  });292293  // ---------- Re-Fungible ----------294295  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token', [Pallets.ReFungible], async ({helper}) => {296    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});297    const collectionRFT = await helper.rft.mintCollection(alice);298    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});299300    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});301    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());302303    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});304    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});305    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());306307    // Create an immediately nested token308    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());309    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);310311    // Create a token to be nested and nest312    const newToken = await collectionRFT.mintToken(charlie, 5n);313    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);314    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);315  });316317  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {318    const collectionNFT = await helper.nft.mintCollection(alice);319    const collectionRFT = await helper.rft.mintCollection(alice);320    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});321322    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionRFT.collectionId]}});323    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});324    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());325326    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});327    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});328    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());329330    // Create an immediately nested token331    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());332    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);333334    // Create a token to be nested and nest335    const newToken = await collectionRFT.mintToken(charlie, 5n);336    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);337    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);338  });339340  async function checkNestedRft({341    expectedBalance,342    childrenShouldPresent,343    nestedRft,344    targetNft,345  }: {346    expectedBalance: bigint,347    childrenShouldPresent: boolean,348    nestedRft: UniqueRFToken,349    targetNft: UniqueNFToken,350  }) {351    const balance = await nestedRft.getBalance(targetNft.nestingAccount());352    expect(balance).to.be.equal(expectedBalance);353354    const children = await targetNft.getChildren();355356    if (childrenShouldPresent) {357      expect(children[0]).to.be.deep.equal({358        collectionId: nestedRft.collectionId,359        tokenId: nestedRft.tokenId,360      });361    } else {362      expect(children.length).to.be.equal(0);363    }364  }365366  itSub.ifWithPallets('ReFungible: allows a collection owner to transfer nested token', [Pallets.ReFungible], async ({helper}) => {367    const collectionNFT = await helper.nft.mintCollection(alice);368    const collectionRFT = await helper.rft.mintCollection(alice, {369      limits: {370        ownerCanTransfer: true,371      },372    });373374    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});375376    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});377    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});378379    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);380    await checkNestedRft({381      expectedBalance: 5n,382      childrenShouldPresent: true,383      nestedRft,384      targetNft,385    });386387    await nestedRft.transferFrom(alice, targetNft.nestingAccount(), {Substrate: bob.address}, 2n);388    await checkNestedRft({389      expectedBalance: 3n,390      childrenShouldPresent: true,391      nestedRft,392      targetNft,393    });394    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(2n);395396    await nestedRft.transferFrom(alice, targetNft.nestingAccount(), {Substrate: bob.address}, 3n);397    await checkNestedRft({398      expectedBalance: 0n,399      childrenShouldPresent: false,400      nestedRft,401      targetNft,402    });403    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(5n);404  });405406  itSub.ifWithPallets('ReFungible: allows a collection admin to transfer nested token', [Pallets.ReFungible], async ({helper}) => {407    const collectionNFT = await helper.nft.mintCollection(alice);408    const collectionRFT = await helper.rft.mintCollection(alice, {409      limits: {410        ownerCanTransfer: true,411      },412    });413    await collectionRFT.addAdmin(alice, {Substrate: bob.address});414415    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});416417    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});418    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});419420    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);421    await checkNestedRft({422      expectedBalance: 5n,423      childrenShouldPresent: true,424      nestedRft,425      targetNft,426    });427428    await nestedRft.transferFrom(bob, targetNft.nestingAccount(), {Substrate: bob.address}, 2n);429    await checkNestedRft({430      expectedBalance: 3n,431      childrenShouldPresent: true,432      nestedRft,433      targetNft,434    });435    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(2n);436437    await nestedRft.transferFrom(bob, targetNft.nestingAccount(), {Substrate: bob.address}, 3n);438    await checkNestedRft({439      expectedBalance: 0n,440      childrenShouldPresent: false,441      nestedRft,442      targetNft,443    });444    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(5n);445  });446447  itSub.ifWithPallets('ReFungible: allows a collection owner to burn nested token', [Pallets.ReFungible], async ({helper}) => {448    const collectionNFT = await helper.nft.mintCollection(alice, {449      limits: {450        ownerCanTransfer: true,451      },452    });453    const collectionRFT = await helper.rft.mintCollection(alice, {454      limits: {455        ownerCanTransfer: true,456      },457    });458459    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});460461    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});462    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});463464    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);465    await checkNestedRft({466      expectedBalance: 5n,467      childrenShouldPresent: true,468      nestedRft,469      targetNft,470    });471472    await nestedRft.burnFrom(alice, targetNft.nestingAccount(), 2n);473    await checkNestedRft({474      expectedBalance: 3n,475      childrenShouldPresent: true,476      nestedRft,477      targetNft,478    });479480    await nestedRft.burnFrom(alice, targetNft.nestingAccount(), 3n);481    await checkNestedRft({482      expectedBalance: 0n,483      childrenShouldPresent: false,484      nestedRft,485      targetNft,486    });487488    // Check if we can burn target NFT when all nested RFTs are gone489    await targetNft.burnFrom(alice, {Substrate: charlie.address});490  });491492  itSub.ifWithPallets('ReFungible: allows a collection admin to burn nested token', [Pallets.ReFungible], async ({helper}) => {493    const collectionNFT = await helper.nft.mintCollection(alice, {494      limits: {495        ownerCanTransfer: true,496      },497    });498    const collectionRFT = await helper.rft.mintCollection(alice, {499      limits: {500        ownerCanTransfer: true,501      },502    });503    await collectionNFT.addAdmin(alice, {Substrate: bob.address});504    await collectionRFT.addAdmin(alice, {Substrate: bob.address});505506    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});507508    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});509    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});510511    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);512    await checkNestedRft({513      expectedBalance: 5n,514      childrenShouldPresent: true,515      nestedRft,516      targetNft,517    });518519    await nestedRft.burnFrom(bob, targetNft.nestingAccount(), 2n);520    await checkNestedRft({521      expectedBalance: 3n,522      childrenShouldPresent: true,523      nestedRft,524      targetNft,525    });526527    await nestedRft.burnFrom(bob, targetNft.nestingAccount(), 3n);528    await checkNestedRft({529      expectedBalance: 0n,530      childrenShouldPresent: false,531      nestedRft,532      targetNft,533    });534535    // Check if we can burn target NFT when all nested RFTs are gone536    await targetNft.burnFrom(bob, {Substrate: charlie.address});537  });538});539540describe('Negative Test: Nesting', () => {541  let alice: IKeyringPair;542  let bob: IKeyringPair;543544  before(async () => {545    await usingPlaygrounds(async (helper, privateKey) => {546      const donor = await privateKey({filename: __filename});547      [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);548    });549  });550551  itSub('Disallows excessive token nesting', async ({helper}) => {552    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});553    let token = await collection.mintToken(alice);554555    const maxNestingLevel = 5;556557    // Create a nested-token matryoshka558    for (let i = 0; i < maxNestingLevel; i++) {559      token = await collection.mintToken(alice, token.nestingAccount());560    }561562    // The nesting depth is limited by `maxNestingLevel`563    await expect(collection.mintToken(alice, token.nestingAccount()))564      .to.be.rejectedWith(/structure\.DepthLimit/);565    expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});566    expect(await token.getChildren()).to.be.length(0);567  });568569  // ---------- Admin ------------570571  itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {572    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});573    await collection.addAdmin(alice, {Substrate: bob.address});574    const targetToken = await collection.mintToken(alice);575576    // Try to create an immediately nested token as collection admin when it's disallowed577    await expect(collection.mintToken(bob, targetToken.nestingAccount()))578      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);579580    // Try to create a token to be nested and nest581    const newToken = await collection.mintToken(bob);582    await expect(newToken.nest(bob, targetToken))583      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);584585    expect(await targetToken.getChildren()).to.be.length(0);586    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});587  });588589  itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {590    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});591    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});592    await collection.addToAllowList(alice, {Substrate: bob.address});593    await collection.addToAllowList(alice, targetToken.nestingAccount());594595    // Try to create a nested token as token owner when it's disallowed596    await expect(collection.mintToken(bob, targetToken.nestingAccount()))597      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);598599    // Try to create a token to be nested and nest600    const newToken = await collection.mintToken(bob);601    await expect(newToken.nest(bob, targetToken))602      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);603604    expect(await targetToken.getChildren()).to.be.length(0);605    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});606  });607608  itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {609    const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});610    //await collection.addAdmin(alice, {Substrate: bob.address});611    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});612    await collection.addToAllowList(alice, {Substrate: bob.address});613    await collection.addToAllowList(alice, targetToken.nestingAccount());614615    // Try to nest somebody else's token616    const newToken = await collection.mintToken(bob);617    await expect(newToken.nest(alice, targetToken))618      .to.be.rejectedWith(/common\.NoPermission/);619620    // Try to unnest a token belonging to someone else as collection admin621    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());622    await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))623      .to.be.rejectedWith(/common\.AddressNotInAllowlist/);624625    expect(await targetToken.getChildren()).to.be.length(1);626    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});627    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());628  });629630  itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {631    const collectionA = await helper.nft.mintCollection(alice);632    const collectionB = await helper.nft.mintCollection(alice);633    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted: [collectionA.collectionId]}});634    const targetToken = await collectionA.mintToken(alice);635636    // Try to create a nested token from another collection637    await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))638      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);639640    // Create a token in another collection yet to be nested and try to nest641    const newToken = await collectionB.mintToken(alice);642    await expect(newToken.nest(alice, targetToken))643      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);644645    expect(await targetToken.getChildren()).to.be.length(0);646    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});647  });648649  // ---------- Non-Fungible ----------650651  itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {652    // Collection is implicitly not allowed nesting at creation653    const collection = await helper.nft.mintCollection(alice);654    const targetToken = await collection.mintToken(alice);655656    // Try to create a nested token as token owner when it's disallowed657    await expect(collection.mintToken(alice, targetToken.nestingAccount()))658      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);659660    // Try to create a token to be nested and nest661    const newToken = await collection.mintToken(alice);662    await expect(newToken.nest(alice, targetToken))663      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);664665    expect(await targetToken.getChildren()).to.be.length(0);666    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});667  });668669  itSub('NFT: disallows a non-Owner to nest someone else\'s token', async ({helper}) => {670    const collection = await helper.nft.mintCollection(alice);671    const targetToken = await collection.mintToken(alice);672673    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});674    await collection.addToAllowList(alice, {Substrate: bob.address});675    await collection.addToAllowList(alice, targetToken.nestingAccount());676677    // Try to create a token to be nested and nest678    const newToken = await collection.mintToken(alice);679    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);680681    expect(await targetToken.getChildren()).to.be.length(0);682    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});683  });684685  itSub('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({helper}) => {686    const collection = await helper.nft.mintCollection(alice);687    const targetToken = await collection.mintToken(alice);688689    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});690    await collection.addToAllowList(alice, {Substrate: bob.address});691    await collection.addToAllowList(alice, targetToken.nestingAccount());692693    const collectionB = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true}});694    await collectionB.addToAllowList(alice, {Substrate: bob.address});695    await collectionB.addToAllowList(alice, targetToken.nestingAccount());696697    // Try to create a token to be nested and nest698    const newToken = await collectionB.mintToken(alice);699    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);700701    expect(await targetToken.getChildren()).to.be.length(0);702    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});703  });704705  itSub('NFT: disallows to nest token in an unlisted collection', async ({helper}) => {706    // Create collection with restricted nesting -- even self is not allowed707    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: []}}});708    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});709710    await collection.addToAllowList(alice, {Substrate: bob.address});711    await collection.addToAllowList(alice, targetToken.nestingAccount());712713    // Try to mint in own collection after allowlisting the accounts714    await expect(collection.mintToken(bob, targetToken.nestingAccount()))715      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);716  });717718  // ---------- Fungible ----------719720  itSub('Fungible: disallows to nest token if nesting is disabled', async ({helper}) => {721    const collectionNFT = await helper.nft.mintCollection(alice);722    const collectionFT = await helper.ft.mintCollection(alice);723    const targetToken = await collectionNFT.mintToken(alice);724725    // Try to create an immediately nested token726    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))727      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);728729    // Try to create a token to be nested and nest730    await collectionFT.mint(alice, 5n);731    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 2n))732      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);733    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);734  });735736  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {737    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});738    const collectionFT = await helper.ft.mintCollection(alice);739    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});740741    // Nest some tokens as Alice into Bob's token742    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());743744    // Try to pull it out745    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))746      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);747    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);748  });749750  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {751    const collectionNFT = await helper.nft.mintCollection(alice);752    const collectionFT = await helper.ft.mintCollection(alice);753    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});754755    await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});756757    // Nest some tokens as Alice into Bob's token758    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());759760    // Try to pull it out as Alice still761    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))762      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);763    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);764  });765766  itSub('Fungible: disallows to nest token in an unlisted collection', async ({helper}) => {767    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true, restricted: []}}});768    const collectionFT = await helper.ft.mintCollection(alice);769    const targetToken = await collectionNFT.mintToken(alice);770771    // Try to mint an immediately nested token772    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))773      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);774775    // Mint a token and try to nest it776    await collectionFT.mint(alice, 5n);777    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 1n))778      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);779780    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);781    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);782  });783784  // ---------- Re-Fungible ----------785786  itSub.ifWithPallets('ReFungible: disallows to nest token if nesting is disabled', [Pallets.ReFungible], async ({helper}) => {787    const collectionNFT = await helper.nft.mintCollection(alice);788    const collectionRFT = await helper.rft.mintCollection(alice);789    const targetToken = await collectionNFT.mintToken(alice);790791    // Try to create an immediately nested token792    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))793      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);794795    // Try to create a token to be nested and nest796    const token = await collectionRFT.mintToken(alice, 5n);797    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))798      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);799    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);800  });801802  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token', [Pallets.ReFungible], async ({helper}) => {803    const collectionNFT = await helper.nft.mintCollection(alice);804    const collectionRFT = await helper.rft.mintCollection(alice);805    const targetToken = await collectionNFT.mintToken(alice);806807    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});808    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});809    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());810811    // Try to create a token to be nested and nest812    const newToken = await collectionRFT.mintToken(alice);813    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);814815    expect(await targetToken.getChildren()).to.be.length(0);816    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);817818    // Nest some tokens as Alice into Bob's token819    await newToken.transfer(alice, targetToken.nestingAccount());820821    // Try to pull it out822    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))823      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);824    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);825  });826827  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {828    const collectionNFT = await helper.nft.mintCollection(alice);829    const collectionRFT = await helper.rft.mintCollection(alice);830    const targetToken = await collectionNFT.mintToken(alice);831832    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: [collectionRFT.collectionId]}});833    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});834    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());835836    // Try to create a token to be nested and nest837    const newToken = await collectionRFT.mintToken(alice);838    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);839840    expect(await targetToken.getChildren()).to.be.length(0);841    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);842843    // Nest some tokens as Alice into Bob's token844    await newToken.transfer(alice, targetToken.nestingAccount());845846    // Try to pull it out847    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))848      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);849    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);850  });851852  itSub.ifWithPallets('ReFungible: disallows to nest token to an unlisted collection', [Pallets.ReFungible], async ({helper}) => {853    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});854    const collectionRFT = await helper.rft.mintCollection(alice);855    const targetToken = await collectionNFT.mintToken(alice);856857    // Try to create an immediately nested token858    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))859      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);860861    // Try to create a token to be nested and nest862    const token = await collectionRFT.mintToken(alice, 5n);863    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))864      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);865    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);866  });867});
after · tests/src/nesting/nest.test.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../util';1920describe('Integration Test: 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({filename: __filename});27      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);28    });29  });3031  itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {32    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});33    const targetToken = await collection.mintToken(alice);3435    // Create an immediately nested token36    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());37    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});38    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());3940    // Create a token to be nested41    const newToken = await collection.mintToken(alice);4243    // Nest44    await newToken.nest(alice, targetToken);45    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});46    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4748    // Move bundle to different user49    await targetToken.transfer(alice, {Substrate: bob.address});50    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});51    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());52    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});53    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());5455    // Unnest56    await newToken.unnest(bob, targetToken, {Substrate: bob.address});57    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});58    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});59  });6061  itSub('Transfers an already bundled token', async ({helper}) => {62    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});63    const tokenA = await collection.mintToken(alice);64    const tokenB = await collection.mintToken(alice);6566    // Create a nested token67    const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());68    expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());6970    // Transfer the nested token to another token71    await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;72    expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});73    expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());74  });7576  itSub('Checks token children', async ({helper}) => {77    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});78    const collectionB = await helper.ft.mintCollection(alice);7980    const targetToken = await collectionA.mintToken(alice);81    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');8283    // Create a nested NFT token84    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());85    expect(await targetToken.getChildren()).to.have.deep.members([86      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},87    ], 'Children contents check at nesting #1').and.be.length(1, 'Children length check at nesting #1');8889    // Create then nest90    const tokenB = await collectionA.mintToken(alice);91    await tokenB.nest(alice, targetToken);92    expect(await targetToken.getChildren()).to.have.deep.members([93      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},94      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},95    ], 'Children contents check at nesting #2').and.be.length(2, 'Children length check at nesting #2');9697    // Move token B to a different user outside the nesting tree98    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});99    expect(await targetToken.getChildren()).to.be.have.deep.members([100      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},101    ], 'Children contents check at nesting #3 (unnesting)').and.be.length(1, 'Children length check at nesting #3 (unnesting)');102103    // Create a fungible token in another collection and then nest104    await collectionB.mint(alice, 10n);105    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);106    expect(await targetToken.getChildren()).to.be.have.deep.members([107      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},108      {tokenId: 0, collectionId: collectionB.collectionId},109    ], 'Children contents check at nesting #4 (from another collection)')110      .and.be.length(2, 'Children length check at nesting #4 (from another collection)');111112    // Move part of the fungible token inside token A deeper in the nesting tree113    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);114    expect(await targetToken.getChildren()).to.be.have.deep.members([115      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},116      {tokenId: 0, collectionId: collectionB.collectionId},117    ], 'Children contents check at nesting #5 (deeper)').and.be.length(2, 'Children length check at nesting #5 (deeper)');118    expect(await tokenA.getChildren()).to.be.have.deep.members([119      {tokenId: 0, collectionId: collectionB.collectionId},120    ], 'Children contents check at nesting #5.5 (deeper)').and.be.length(1, 'Children length check at nesting #5.5 (deeper)');121122    // Move the remaining part of the fungible token inside token A deeper in the nesting tree123    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);124    expect(await targetToken.getChildren()).to.be.have.deep.members([125      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},126    ], 'Children contents check at nesting #6 (deeper)').and.be.length(1, 'Children length check at nesting #6 (deeper)');127    expect(await tokenA.getChildren()).to.be.have.deep.members([128      {tokenId: 0, collectionId: collectionB.collectionId},129    ], 'Children contents check at nesting #6.5 (deeper)').and.be.length(1, 'Children length check at nesting #6.5 (deeper)');130  });131});132133describe('Integration Test: Various token type nesting', () => {134  let alice: IKeyringPair;135  let bob: IKeyringPair;136  let charlie: IKeyringPair;137138  before(async () => {139    await usingPlaygrounds(async (helper, privateKey) => {140      const donor = await privateKey({filename: __filename});141      [alice, bob, charlie] = await helper.arrange.createAccounts([50n, 10n, 10n], donor);142    });143  });144145  itSub('Admin (NFT): allows an Admin to nest a token', async ({helper}) => {146    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true}}});147    await collection.addAdmin(alice, {Substrate: bob.address});148    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});149150    // Create an immediately nested token151    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());152    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});153    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());154155    // Create a token to be nested and nest156    const newToken = await collection.mintToken(bob);157    await newToken.nest(bob, targetToken);158    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});159    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());160  });161162  itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {163    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});164    await collection.addAdmin(alice, {Substrate: bob.address});165    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});166167    // Create an immediately nested token by an administrator168    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());169    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});170    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());171172    // Create a token to be nested and nest173    const newToken = await collection.mintToken(alice, {Substrate: charlie.address});174    await newToken.nest(charlie, targetToken);175    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});176    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());177  });178179  itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {180    const collectionA = await helper.nft.mintCollection(alice);181    await collectionA.addAdmin(alice, {Substrate: bob.address});182    const collectionB = await helper.nft.mintCollection(alice);183    await collectionB.addAdmin(alice, {Substrate: bob.address});184    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted:[collectionB.collectionId]}});185    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});186187    // Create an immediately nested token188    const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());189    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});190    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());191192    // Create a token to be nested and nest193    const newToken = await collectionB.mintToken(bob);194    await newToken.nest(bob, targetToken);195    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});196    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());197  });198199  // ---------- Non-Fungible ----------200201  itSub('NFT: allows an Owner to nest/unnest their token', async ({helper}) => {202    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});203    await collection.addToAllowList(alice, {Substrate: charlie.address});204    const targetToken = await collection.mintToken(charlie);205    await collection.addToAllowList(alice, targetToken.nestingAccount());206207    // Create an immediately nested token208    const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());209    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});210    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());211212    // Create a token to be nested and nest213    const newToken = await collection.mintToken(charlie);214    await newToken.nest(charlie, targetToken);215    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});216    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());217  });218219  itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {220    const collectionA = await helper.nft.mintCollection(alice);221    const collectionB = await helper.nft.mintCollection(alice);222    //await collectionB.addAdmin(alice, {Substrate: bob.address});223    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});224225    await collectionA.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionB.collectionId]}});226    await collectionA.addToAllowList(alice, {Substrate: charlie.address});227    await collectionA.addToAllowList(alice, targetToken.nestingAccount());228229    await collectionB.setPermissions(alice, {access: 'AllowList', mintMode: true});230    await collectionB.addToAllowList(alice, {Substrate: charlie.address});231    await collectionB.addToAllowList(alice, targetToken.nestingAccount());232233    // Create an immediately nested token234    const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());235    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});236    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());237238    // Create a token to be nested and nest239    const newToken = await collectionB.mintToken(charlie);240    await newToken.nest(charlie, targetToken);241    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});242    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());243  });244245  // ---------- Fungible ----------246247  itSub('Fungible: allows an Owner to nest/unnest their token', async ({helper}) => {248    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});249    const collectionFT = await helper.ft.mintCollection(alice);250    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});251252    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});253    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());254255    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});256    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});257    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());258259    // Create an immediately nested token260    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());261    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);262263    // Create a token to be nested and nest264    await collectionFT.mint(charlie, 5n);265    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);266    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);267  });268269  itSub('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {270    const collectionNFT = await helper.nft.mintCollection(alice);271    const collectionFT = await helper.ft.mintCollection(alice);272    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});273274    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionFT.collectionId]}});275    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});276    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());277278    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});279    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});280    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());281282    // Create an immediately nested token283    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());284    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);285286    // Create a token to be nested and nest287    await collectionFT.mint(charlie, 5n);288    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);289    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);290  });291292  // ---------- Re-Fungible ----------293294  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token', [Pallets.ReFungible], async ({helper}) => {295    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});296    const collectionRFT = await helper.rft.mintCollection(alice);297    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});298299    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});300    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());301302    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});303    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});304    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());305306    // Create an immediately nested token307    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());308    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);309310    // Create a token to be nested and nest311    const newToken = await collectionRFT.mintToken(charlie, 5n);312    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);313    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);314  });315316  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {317    const collectionNFT = await helper.nft.mintCollection(alice);318    const collectionRFT = await helper.rft.mintCollection(alice);319    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});320321    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionRFT.collectionId]}});322    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});323    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());324325    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});326    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});327    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());328329    // Create an immediately nested token330    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());331    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);332333    // Create a token to be nested and nest334    const newToken = await collectionRFT.mintToken(charlie, 5n);335    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);336    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);337  });338});339340describe('Negative Test: Nesting', () => {341  let alice: IKeyringPair;342  let bob: IKeyringPair;343344  before(async () => {345    await usingPlaygrounds(async (helper, privateKey) => {346      const donor = await privateKey({filename: __filename});347      [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);348    });349  });350351  itSub('Disallows excessive token nesting', async ({helper}) => {352    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});353    let token = await collection.mintToken(alice);354355    const maxNestingLevel = 5;356357    // Create a nested-token matryoshka358    for (let i = 0; i < maxNestingLevel; i++) {359      token = await collection.mintToken(alice, token.nestingAccount());360    }361362    // The nesting depth is limited by `maxNestingLevel`363    await expect(collection.mintToken(alice, token.nestingAccount()))364      .to.be.rejectedWith(/structure\.DepthLimit/);365    expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});366    expect(await token.getChildren()).to.be.length(0);367  });368369  // ---------- Admin ------------370371  itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {372    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});373    await collection.addAdmin(alice, {Substrate: bob.address});374    const targetToken = await collection.mintToken(alice);375376    // Try to create an immediately nested token as collection admin when it's disallowed377    await expect(collection.mintToken(bob, targetToken.nestingAccount()))378      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);379380    // Try to create a token to be nested and nest381    const newToken = await collection.mintToken(bob);382    await expect(newToken.nest(bob, targetToken))383      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);384385    expect(await targetToken.getChildren()).to.be.length(0);386    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});387  });388389  itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {390    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});391    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});392    await collection.addToAllowList(alice, {Substrate: bob.address});393    await collection.addToAllowList(alice, targetToken.nestingAccount());394395    // Try to create a nested token as token owner when it's disallowed396    await expect(collection.mintToken(bob, targetToken.nestingAccount()))397      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);398399    // Try to create a token to be nested and nest400    const newToken = await collection.mintToken(bob);401    await expect(newToken.nest(bob, targetToken))402      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);403404    expect(await targetToken.getChildren()).to.be.length(0);405    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});406  });407408  itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {409    const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});410    //await collection.addAdmin(alice, {Substrate: bob.address});411    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});412    await collection.addToAllowList(alice, {Substrate: bob.address});413    await collection.addToAllowList(alice, targetToken.nestingAccount());414415    // Try to nest somebody else's token416    const newToken = await collection.mintToken(bob);417    await expect(newToken.nest(alice, targetToken))418      .to.be.rejectedWith(/common\.NoPermission/);419420    // Try to unnest a token belonging to someone else as collection admin421    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());422    await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))423      .to.be.rejectedWith(/common\.AddressNotInAllowlist/);424425    expect(await targetToken.getChildren()).to.be.length(1);426    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});427    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());428  });429430  itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {431    const collectionA = await helper.nft.mintCollection(alice);432    const collectionB = await helper.nft.mintCollection(alice);433    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted: [collectionA.collectionId]}});434    const targetToken = await collectionA.mintToken(alice);435436    // Try to create a nested token from another collection437    await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))438      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);439440    // Create a token in another collection yet to be nested and try to nest441    const newToken = await collectionB.mintToken(alice);442    await expect(newToken.nest(alice, targetToken))443      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);444445    expect(await targetToken.getChildren()).to.be.length(0);446    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});447  });448449  // ---------- Non-Fungible ----------450451  itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {452    // Collection is implicitly not allowed nesting at creation453    const collection = await helper.nft.mintCollection(alice);454    const targetToken = await collection.mintToken(alice);455456    // Try to create a nested token as token owner when it's disallowed457    await expect(collection.mintToken(alice, targetToken.nestingAccount()))458      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);459460    // Try to create a token to be nested and nest461    const newToken = await collection.mintToken(alice);462    await expect(newToken.nest(alice, targetToken))463      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);464465    expect(await targetToken.getChildren()).to.be.length(0);466    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});467  });468469  itSub('NFT: disallows a non-Owner to nest someone else\'s token', async ({helper}) => {470    const collection = await helper.nft.mintCollection(alice);471    const targetToken = await collection.mintToken(alice);472473    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});474    await collection.addToAllowList(alice, {Substrate: bob.address});475    await collection.addToAllowList(alice, targetToken.nestingAccount());476477    // Try to create a token to be nested and nest478    const newToken = await collection.mintToken(alice);479    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);480481    expect(await targetToken.getChildren()).to.be.length(0);482    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});483  });484485  itSub('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({helper}) => {486    const collection = await helper.nft.mintCollection(alice);487    const targetToken = await collection.mintToken(alice);488489    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});490    await collection.addToAllowList(alice, {Substrate: bob.address});491    await collection.addToAllowList(alice, targetToken.nestingAccount());492493    const collectionB = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true}});494    await collectionB.addToAllowList(alice, {Substrate: bob.address});495    await collectionB.addToAllowList(alice, targetToken.nestingAccount());496497    // Try to create a token to be nested and nest498    const newToken = await collectionB.mintToken(alice);499    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);500501    expect(await targetToken.getChildren()).to.be.length(0);502    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});503  });504505  itSub('NFT: disallows to nest token in an unlisted collection', async ({helper}) => {506    // Create collection with restricted nesting -- even self is not allowed507    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: []}}});508    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});509510    await collection.addToAllowList(alice, {Substrate: bob.address});511    await collection.addToAllowList(alice, targetToken.nestingAccount());512513    // Try to mint in own collection after allowlisting the accounts514    await expect(collection.mintToken(bob, targetToken.nestingAccount()))515      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);516  });517518  // ---------- Fungible ----------519520  itSub('Fungible: disallows to nest token if nesting is disabled', async ({helper}) => {521    const collectionNFT = await helper.nft.mintCollection(alice);522    const collectionFT = await helper.ft.mintCollection(alice);523    const targetToken = await collectionNFT.mintToken(alice);524525    // Try to create an immediately nested token526    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))527      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);528529    // Try to create a token to be nested and nest530    await collectionFT.mint(alice, 5n);531    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 2n))532      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);533    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);534  });535536  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {537    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});538    const collectionFT = await helper.ft.mintCollection(alice);539    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});540541    // Nest some tokens as Alice into Bob's token542    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());543544    // Try to pull it out545    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))546      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);547    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);548  });549550  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {551    const collectionNFT = await helper.nft.mintCollection(alice);552    const collectionFT = await helper.ft.mintCollection(alice);553    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});554555    await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});556557    // Nest some tokens as Alice into Bob's token558    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());559560    // Try to pull it out as Alice still561    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))562      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);563    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);564  });565566  itSub('Fungible: disallows to nest token in an unlisted collection', async ({helper}) => {567    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true, restricted: []}}});568    const collectionFT = await helper.ft.mintCollection(alice);569    const targetToken = await collectionNFT.mintToken(alice);570571    // Try to mint an immediately nested token572    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))573      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);574575    // Mint a token and try to nest it576    await collectionFT.mint(alice, 5n);577    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 1n))578      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);579580    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);581    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);582  });583584  // ---------- Re-Fungible ----------585586  itSub.ifWithPallets('ReFungible: disallows to nest token if nesting is disabled', [Pallets.ReFungible], async ({helper}) => {587    const collectionNFT = await helper.nft.mintCollection(alice);588    const collectionRFT = await helper.rft.mintCollection(alice);589    const targetToken = await collectionNFT.mintToken(alice);590591    // Try to create an immediately nested token592    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))593      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);594595    // Try to create a token to be nested and nest596    const token = await collectionRFT.mintToken(alice, 5n);597    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))598      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);599    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);600  });601602  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token', [Pallets.ReFungible], async ({helper}) => {603    const collectionNFT = await helper.nft.mintCollection(alice);604    const collectionRFT = await helper.rft.mintCollection(alice);605    const targetToken = await collectionNFT.mintToken(alice);606607    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});608    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});609    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());610611    // Try to create a token to be nested and nest612    const newToken = await collectionRFT.mintToken(alice);613    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);614615    expect(await targetToken.getChildren()).to.be.length(0);616    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);617618    // Nest some tokens as Alice into Bob's token619    await newToken.transfer(alice, targetToken.nestingAccount());620621    // Try to pull it out622    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))623      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);624    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);625  });626627  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {628    const collectionNFT = await helper.nft.mintCollection(alice);629    const collectionRFT = await helper.rft.mintCollection(alice);630    const targetToken = await collectionNFT.mintToken(alice);631632    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: [collectionRFT.collectionId]}});633    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});634    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());635636    // Try to create a token to be nested and nest637    const newToken = await collectionRFT.mintToken(alice);638    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);639640    expect(await targetToken.getChildren()).to.be.length(0);641    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);642643    // Nest some tokens as Alice into Bob's token644    await newToken.transfer(alice, targetToken.nestingAccount());645646    // Try to pull it out647    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))648      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);649    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);650  });651652  itSub.ifWithPallets('ReFungible: disallows to nest token to an unlisted collection', [Pallets.ReFungible], async ({helper}) => {653    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});654    const collectionRFT = await helper.rft.mintCollection(alice);655    const targetToken = await collectionNFT.mintToken(alice);656657    // Try to create an immediately nested token658    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))659      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);660661    // Try to create a token to be nested and nest662    const token = await collectionRFT.mintToken(alice, 5n);663    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))664      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);665    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);666  });667});
modifiedtests/src/nesting/unnest.test.tsdiffbeforeafterboth
--- a/tests/src/nesting/unnest.test.ts
+++ b/tests/src/nesting/unnest.test.ts
@@ -16,14 +16,17 @@
 
 import {IKeyringPair} from '@polkadot/types/types';
 import {expect, itSub, Pallets, usingPlaygrounds} from '../util';
+import {UniqueFTCollection, UniqueNFToken, UniqueRFToken} from '../util/playgrounds/unique';
 
 describe('Integration Test: Unnesting', () => {
   let alice: IKeyringPair;
+  let bob: IKeyringPair;
+  let charlie: IKeyringPair;
 
   before(async () => {
     await usingPlaygrounds(async (helper, privateKey) => {
       const donor = await privateKey({filename: __filename});
-      [alice] = await helper.arrange.createAccounts([50n], donor);
+      [alice, bob, charlie] = await helper.arrange.createAccounts([200n, 50n, 50n], donor);
     });
   });
 
@@ -83,6 +86,188 @@
     expect(await token.getBalance(targetToken.nestingAccount())).to.be.equal(0n);
     expect(await targetToken.getChildren()).to.be.length(0);
   });
+
+  async function checkNestedAmountState({
+    expectedBalance,
+    childrenShouldPresent,
+    nested,
+    targetNft,
+  }: {
+    expectedBalance: bigint,
+    childrenShouldPresent: boolean,
+    nested: UniqueFTCollection | UniqueRFToken,
+    targetNft: UniqueNFToken,
+  }) {
+    const balance = await nested.getBalance(targetNft.nestingAccount());
+    expect(balance).to.be.equal(expectedBalance);
+
+    const children = await targetNft.getChildren();
+
+    if (childrenShouldPresent) {
+      expect(children[0]).to.be.deep.equal({
+        collectionId: nested.collectionId,
+        tokenId: (nested instanceof UniqueFTCollection) ? 0 : nested.tokenId,
+      });
+    } else {
+      expect(children.length).to.be.equal(0);
+    }
+  }
+
+  function ownerOrAdminUnnestCases(modes: ('ft' | 'nft' | 'rft')[]): {
+    mode: 'ft' | 'nft' | 'rft',
+    sender: string,
+    op: 'transfer' | 'burn',
+    requiredPallets: Pallets[],
+  }[] {
+    const senders = ['owner', 'admin'];
+    const ops = ['transfer', 'burn'];
+
+    const cases = [];
+    for (const mode of modes) {
+      const requiredPallets = (mode === 'rft')
+        ? [Pallets.ReFungible]
+        : [];
+
+      for (const sender of senders) {
+        for (const op of ops) {
+          cases.push({
+            mode: mode as 'ft' | 'nft' | 'rft',
+            sender,
+            op: op as 'transfer' | 'burn',
+            requiredPallets,
+          });
+        }
+      }
+    }
+
+    return cases;
+  }
+
+  ownerOrAdminUnnestCases(['ft', 'rft']).map(testCase =>
+    itSub.ifWithPallets(`[${testCase.mode}]: allows a collection ${testCase.sender} to ${testCase.op} nested token`, testCase.requiredPallets, async({helper}) => {
+      const owner = alice;
+      const admin = bob;
+
+      const unnester = (testCase.sender === 'owner')
+        ? owner
+        : admin;
+
+      const collectionNFT = await helper.nft.mintCollection(owner);
+      await collectionNFT.setPermissions(owner, {nesting: {tokenOwner: true}});
+
+      const collectionNested = await helper[testCase.mode as 'ft' | 'rft'].mintCollection(owner, {
+        limits: {
+          ownerCanTransfer: true,
+        },
+      });
+      await collectionNested.addAdmin(owner, {Substrate: admin.address});
+
+      const targetNft = await collectionNFT.mintToken(owner, {Substrate: charlie.address});
+
+      let nested: UniqueFTCollection | UniqueRFToken;
+      const totalAmount = 5n;
+      const firstUnnestAmount = 2n;
+      const restUnnestAmount = totalAmount - firstUnnestAmount;
+
+      if (collectionNested instanceof UniqueFTCollection) {
+        await collectionNested.mint(owner, totalAmount, {Substrate: charlie.address});
+        nested = collectionNested;
+      } else {
+        nested = await collectionNested.mintToken(owner, totalAmount, {Substrate: charlie.address});
+      }
+
+      // transfer/burn `amount` of nested assets by `unnester`.
+      const doOperationAndCheck = async ({
+        amount,
+        shouldBeNestedAfterOp,
+      }: {
+        amount: bigint,
+        shouldBeNestedAfterOp: boolean,
+      }) => {
+        const nestedBalanceBeforeOp = await nested.getBalance(targetNft.nestingAccount());
+
+        if (testCase.op === 'transfer') {
+          const bobBalanceBeforeOp = await nested.getBalance({Substrate: bob.address});
+
+          await nested.transferFrom(unnester, targetNft.nestingAccount(), {Substrate: bob.address}, amount);
+          expect(await nested.getBalance({Substrate: bob.address})).to.be.equal(bobBalanceBeforeOp + amount);
+        } else {
+          if (nested instanceof UniqueFTCollection) {
+            await nested.burnTokensFrom(unnester, targetNft.nestingAccount(), amount);
+          } else {
+            await nested.burnFrom(unnester, targetNft.nestingAccount(), amount);
+          }
+        }
+
+        await checkNestedAmountState({
+          expectedBalance: nestedBalanceBeforeOp - amount,
+          childrenShouldPresent: shouldBeNestedAfterOp,
+          nested,
+          targetNft,
+        });
+      };
+
+      // Initial setup: nest (fungibles/rft parts).
+      // Check NFT's balance of nested assets and NFT's children.
+      await nested.transfer(charlie, targetNft.nestingAccount(), totalAmount);
+      await checkNestedAmountState({
+        expectedBalance: totalAmount,
+        childrenShouldPresent: true,
+        nested,
+        targetNft,
+      });
+
+      // Transfer/burn only a part of nested assets.
+      // Check that NFT's balance of the nested assets correctly decreased and NFT's children are not changed.
+      await doOperationAndCheck({
+        amount: firstUnnestAmount,
+        shouldBeNestedAfterOp: true,
+      });
+
+      // Transfer/burn all remaining nested assets.
+      // Check that NFT's balance of the nested assets is 0 and NFT has no more children.
+      await doOperationAndCheck({
+        amount: restUnnestAmount,
+        shouldBeNestedAfterOp: false,
+      });
+    }));
+
+  ownerOrAdminUnnestCases(['nft']).map(testCase =>
+    itSub(`[nft]: allows a collection ${testCase.sender} to ${testCase.op} nested token`, async ({helper}) => {
+      const owner = alice;
+      const admin = bob;
+
+      const unnester = (testCase.sender === 'owner')
+        ? owner
+        : admin;
+
+      const collectionNFT = await helper.nft.mintCollection(owner);
+      await collectionNFT.setPermissions(owner, {nesting: {tokenOwner: true}});
+
+      const collectionNested = await helper.nft.mintCollection(owner, {
+        limits: {
+          ownerCanTransfer: true,
+        },
+      });
+      await collectionNested.addAdmin(owner, {Substrate: admin.address});
+
+      const targetNft = await collectionNFT.mintToken(owner, {Substrate: charlie.address});
+      const nested = await collectionNested.mintToken(owner, {Substrate: charlie.address});
+
+      await nested.transfer(charlie, targetNft.nestingAccount());
+      expect(await targetNft.getChildren()).to.be.deep.equal([{
+        collectionId: nested.collectionId,
+        tokenId: nested.tokenId,
+      }]);
+
+      if (testCase.op === 'transfer') {
+        await nested.transferFrom(unnester, targetNft.nestingAccount(), {Substrate: bob.address});
+      } else {
+        await nested.burnFrom(unnester, targetNft.nestingAccount());
+      }
+
+      expect((await targetNft.getChildren()).length).to.be.equal(0);
+    }));
 });
 
 describe('Negative Test: Unnesting', () => {