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

difftreelog

Tests refactoring WIP

Max Andreev2023-01-20parent: #ba92b38.patch.diff
in: master

4 files changed

modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
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, 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  /// TODO move32  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  });61});6263describe('Integration Test: Various token type nesting', () => {64  let alice: IKeyringPair;65  let bob: IKeyringPair;66  let charlie: IKeyringPair;6768  before(async () => {69    await usingPlaygrounds(async (helper, privateKey) => {70      const donor = await privateKey({filename: __filename});71      [alice, bob, charlie] = await helper.arrange.createAccounts([50n, 10n, 10n], donor);72    });73  });7475  itSub('Admin (NFT): allows an Admin to nest a token', async ({helper}) => {76    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true}}});77    await collection.addAdmin(alice, {Substrate: bob.address});78    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});7980    // Create an immediately nested token81    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());82    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});83    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());8485    // Create a token to be nested and nest86    const newToken = await collection.mintToken(bob);87    await newToken.nest(bob, targetToken);88    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});89    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());90  });9192  itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {93    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});94    await collection.addAdmin(alice, {Substrate: bob.address});95    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});9697    // Create an immediately nested token by an administrator98    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());99    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});100    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());101102    // Create a token to be nested and nest103    const newToken = await collection.mintToken(alice, {Substrate: charlie.address});104    await newToken.nest(charlie, targetToken);105    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});106    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());107  });108109  itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {110    const collectionA = await helper.nft.mintCollection(alice);111    await collectionA.addAdmin(alice, {Substrate: bob.address});112    const collectionB = await helper.nft.mintCollection(alice);113    await collectionB.addAdmin(alice, {Substrate: bob.address});114    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted:[collectionB.collectionId]}});115    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});116117    // Create an immediately nested token118    const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());119    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});120    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());121122    // Create a token to be nested and nest123    const newToken = await collectionB.mintToken(bob);124    await newToken.nest(bob, targetToken);125    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});126    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());127  });128129  // ---------- Non-Fungible ----------130131  itSub('NFT: allows an Owner to nest/unnest their token', async ({helper}) => {132    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});133    await collection.addToAllowList(alice, {Substrate: charlie.address});134    const targetToken = await collection.mintToken(charlie);135    await collection.addToAllowList(alice, targetToken.nestingAccount());136137    // Create an immediately nested token138    const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());139    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});140    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());141142    // Create a token to be nested and nest143    const newToken = await collection.mintToken(charlie);144    await newToken.nest(charlie, targetToken);145    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});146    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());147  });148149  itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {150    const collectionA = await helper.nft.mintCollection(alice);151    const collectionB = await helper.nft.mintCollection(alice);152    //await collectionB.addAdmin(alice, {Substrate: bob.address});153    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});154155    await collectionA.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionB.collectionId]}});156    await collectionA.addToAllowList(alice, {Substrate: charlie.address});157    await collectionA.addToAllowList(alice, targetToken.nestingAccount());158159    await collectionB.setPermissions(alice, {access: 'AllowList', mintMode: true});160    await collectionB.addToAllowList(alice, {Substrate: charlie.address});161    await collectionB.addToAllowList(alice, targetToken.nestingAccount());162163    // Create an immediately nested token164    const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());165    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});166    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());167168    // Create a token to be nested and nest169    const newToken = await collectionB.mintToken(charlie);170    await newToken.nest(charlie, targetToken);171    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});172    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());173  });174175  // ---------- Fungible ----------176177  itSub('Fungible: allows an Owner to nest/unnest their token', async ({helper}) => {178    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});179    const collectionFT = await helper.ft.mintCollection(alice);180    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});181182    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});183    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());184185    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});186    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});187    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());188189    // Create an immediately nested token190    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());191    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);192193    // Create a token to be nested and nest194    await collectionFT.mint(charlie, 5n);195    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);196    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);197  });198199  itSub('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {200    const collectionNFT = await helper.nft.mintCollection(alice);201    const collectionFT = await helper.ft.mintCollection(alice);202    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});203204    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionFT.collectionId]}});205    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});206    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());207208    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});209    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});210    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());211212    // Create an immediately nested token213    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());214    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);215216    // Create a token to be nested and nest217    await collectionFT.mint(charlie, 5n);218    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);219    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);220  });221});222223describe('Negative Test: Nesting', () => {224  let alice: IKeyringPair;225  let bob: IKeyringPair;226227  before(async () => {228    await usingPlaygrounds(async (helper, privateKey) => {229      const donor = await privateKey({filename: __filename});230      [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);231    });232  });233234  itSub('Disallows excessive token nesting', async ({helper}) => {235    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});236    let token = await collection.mintToken(alice);237238    const maxNestingLevel = 5;239240    // Create a nested-token matryoshka241    for (let i = 0; i < maxNestingLevel; i++) {242      token = await collection.mintToken(alice, token.nestingAccount());243    }244245    // The nesting depth is limited by `maxNestingLevel`246    await expect(collection.mintToken(alice, token.nestingAccount()))247      .to.be.rejectedWith(/structure\.DepthLimit/);248    expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});249    expect(await token.getChildren()).to.be.length(0);250  });251252  // ---------- Admin ------------253254  itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {255    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});256    await collection.addAdmin(alice, {Substrate: bob.address});257    const targetToken = await collection.mintToken(alice);258259    // Try to create an immediately nested token as collection admin when it's disallowed260    await expect(collection.mintToken(bob, targetToken.nestingAccount()))261      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);262263    // Try to create a token to be nested and nest264    const newToken = await collection.mintToken(bob);265    await expect(newToken.nest(bob, targetToken))266      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);267268    expect(await targetToken.getChildren()).to.be.length(0);269    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});270  });271272  itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {273    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});274    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});275    await collection.addToAllowList(alice, {Substrate: bob.address});276    await collection.addToAllowList(alice, targetToken.nestingAccount());277278    // Try to create a nested token as token owner when it's disallowed279    await expect(collection.mintToken(bob, targetToken.nestingAccount()))280      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);281282    // Try to create a token to be nested and nest283    const newToken = await collection.mintToken(bob);284    await expect(newToken.nest(bob, targetToken))285      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);286287    expect(await targetToken.getChildren()).to.be.length(0);288    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});289  });290291  itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {292    const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});293    //await collection.addAdmin(alice, {Substrate: bob.address});294    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});295    await collection.addToAllowList(alice, {Substrate: bob.address});296    await collection.addToAllowList(alice, targetToken.nestingAccount());297298    // Try to nest somebody else's token299    const newToken = await collection.mintToken(bob);300    await expect(newToken.nest(alice, targetToken))301      .to.be.rejectedWith(/common\.NoPermission/);302303    // Try to unnest a token belonging to someone else as collection admin304    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());305    await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))306      .to.be.rejectedWith(/common\.AddressNotInAllowlist/);307308    expect(await targetToken.getChildren()).to.be.length(1);309    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});310    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());311  });312313  itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {314    const collectionA = await helper.nft.mintCollection(alice);315    const collectionB = await helper.nft.mintCollection(alice);316    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted: [collectionA.collectionId]}});317    const targetToken = await collectionA.mintToken(alice);318319    // Try to create a nested token from another collection320    await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))321      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);322323    // Create a token in another collection yet to be nested and try to nest324    const newToken = await collectionB.mintToken(alice);325    await expect(newToken.nest(alice, targetToken))326      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);327328    expect(await targetToken.getChildren()).to.be.length(0);329    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});330  });331332  // ---------- Non-Fungible ----------333334  itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {335    // Collection is implicitly not allowed nesting at creation336    const collection = await helper.nft.mintCollection(alice);337    const targetToken = await collection.mintToken(alice);338339    // Try to create a nested token as token owner when it's disallowed340    await expect(collection.mintToken(alice, targetToken.nestingAccount()))341      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);342343    // Try to create a token to be nested and nest344    const newToken = await collection.mintToken(alice);345    await expect(newToken.nest(alice, targetToken))346      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);347348    expect(await targetToken.getChildren()).to.be.length(0);349    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});350  });351352  itSub('NFT: disallows a non-Owner to nest someone else\'s token', async ({helper}) => {353    const collection = await helper.nft.mintCollection(alice);354    const targetToken = await collection.mintToken(alice);355356    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});357    await collection.addToAllowList(alice, {Substrate: bob.address});358    await collection.addToAllowList(alice, targetToken.nestingAccount());359360    // Try to create a token to be nested and nest361    const newToken = await collection.mintToken(alice);362    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);363364    expect(await targetToken.getChildren()).to.be.length(0);365    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});366  });367368  itSub('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({helper}) => {369    const collection = await helper.nft.mintCollection(alice);370    const targetToken = await collection.mintToken(alice);371372    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});373    await collection.addToAllowList(alice, {Substrate: bob.address});374    await collection.addToAllowList(alice, targetToken.nestingAccount());375376    const collectionB = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true}});377    await collectionB.addToAllowList(alice, {Substrate: bob.address});378    await collectionB.addToAllowList(alice, targetToken.nestingAccount());379380    // Try to create a token to be nested and nest381    const newToken = await collectionB.mintToken(alice);382    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);383384    expect(await targetToken.getChildren()).to.be.length(0);385    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});386  });387388  itSub('NFT: disallows to nest token in an unlisted collection', async ({helper}) => {389    // Create collection with restricted nesting -- even self is not allowed390    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: []}}});391    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});392393    await collection.addToAllowList(alice, {Substrate: bob.address});394    await collection.addToAllowList(alice, targetToken.nestingAccount());395396    // Try to mint in own collection after allowlisting the accounts397    await expect(collection.mintToken(bob, targetToken.nestingAccount()))398      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);399  });400401  // ---------- Fungible ----------402403  itSub('Fungible: disallows to nest token if nesting is disabled', async ({helper}) => {404    const collectionNFT = await helper.nft.mintCollection(alice);405    const collectionFT = await helper.ft.mintCollection(alice);406    const targetToken = await collectionNFT.mintToken(alice);407408    // Try to create an immediately nested token409    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))410      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);411412    // Try to create a token to be nested and nest413    await collectionFT.mint(alice, 5n);414    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 2n))415      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);416    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);417  });418419  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {420    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});421    const collectionFT = await helper.ft.mintCollection(alice);422    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});423424    // Nest some tokens as Alice into Bob's token425    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());426427    // Try to pull it out428    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))429      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);430    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);431  });432433  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {434    const collectionNFT = await helper.nft.mintCollection(alice);435    const collectionFT = await helper.ft.mintCollection(alice);436    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});437438    await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});439440    // Nest some tokens as Alice into Bob's token441    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());442443    // Try to pull it out as Alice still444    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))445      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);446    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);447  });448449  itSub('Fungible: disallows to nest token in an unlisted collection', async ({helper}) => {450    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true, restricted: []}}});451    const collectionFT = await helper.ft.mintCollection(alice);452    const targetToken = await collectionNFT.mintToken(alice);453454    // Try to mint an immediately nested token455    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))456      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);457458    // Mint a token and try to nest it459    await collectionFT.mint(alice, 5n);460    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 1n))461      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);462463    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);464    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);465  });466});
modifiedtests/src/sub/nesting/common.test.tsdiffbeforeafterboth
--- a/tests/src/sub/nesting/common.test.ts
+++ b/tests/src/sub/nesting/common.test.ts
@@ -17,191 +17,90 @@
 import {IKeyringPair} from '@polkadot/types/types';
 import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
 import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
-import {itEth} from '../../eth/util';
 
-describe('Nesting', () => {
-  let alice: IKeyringPair;
+let alice: IKeyringPair;
 
-  before(async () => {
-    await usingPlaygrounds(async (helper, privateKey) => {
-      const donor = await privateKey({filename: __filename});
-      [alice] = await helper.arrange.createAccounts([50n], donor);
-    });
+before(async () => {
+  await usingPlaygrounds(async (helper, privateKey) => {
+    const donor = await privateKey({filename: __filename});
+    [alice] = await helper.arrange.createAccounts([100n], donor);
   });
+});
 
-  [
-    {mode: 'nft' as const, requiredPallets: []},
-    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
-  ].map(testCase => {
-    itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()}`, testCase.requiredPallets, async ({helper}) => {
-      // Only NFT allows nesting, permissions should be set:
-      const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
-      const targetToken = await aliceNFTCollection.mintToken(alice);
-
-      const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
-
-      // 1. Alice can immediately create nested token:
-      const nestedToken1 = testCase.mode === 'nft'
-        ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
-        : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
-      expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
-      expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
-
-      // 2. Alice can mint and nest token:
-      const nestedToken2 = await collectionForNesting.mintToken(alice);
-      await nestedToken2.nest(alice, targetToken);
-      expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
-      expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
-    });
-  });
-
-  itSub('Owner can nest FT', async ({helper}) => {
+[
+  {mode: 'nft' as const, requiredPallets: []},
+  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+].map(testCase => {
+  itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()} in NFT`, testCase.requiredPallets, async ({helper}) => {
     // Only NFT allows nesting, permissions should be set:
     const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
     const targetToken = await aliceNFTCollection.mintToken(alice);
 
-    const collectionForNesting = await helper.ft.mintCollection(alice);
+    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
 
-    // 1. Alice can immediately create nested tokens:
-    await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());
-    expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);
-    expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);
+    // 1. Alice can immediately create nested token:
+    const nestedToken1 = testCase.mode === 'nft'
+      ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
+      : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
+    expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
+    expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
 
     // 2. Alice can mint and nest token:
-    await collectionForNesting.mint(alice, 100n);
-    await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);
-    expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);
+    const nestedToken2 = await collectionForNesting.mintToken(alice);
+    await nestedToken2.nest(alice, targetToken);
+    expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
+    expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());
   });
 });
-
-describe('Nesting negative', () => {
-  let alice: IKeyringPair;
-
-  before(async () => {
-    await usingPlaygrounds(async (helper, privateKey) => {
-      const donor = await privateKey({filename: __filename});
-      [alice] = await helper.arrange.createAccounts([50n], donor);
-    });
-  });
 
-  [
-    {mode: 'nft' as const, requiredPallets: []},
-    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
-  ].map(testCase => {
-    itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {
-    // Create default collection, permissions are not set:
-      const aliceNFTCollection = await helper.nft.mintCollection(alice);
-      const targetToken = await aliceNFTCollection.mintToken(alice);
 
-      const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
-
-      // 1. Alice cannot create immediately nested tokens:
-      const nestingTx = testCase.mode === 'nft'
-        ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
-        : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
-      await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');
-
-      // 2. Alice cannot mint and nest token:
-      const nestedToken2 = await collectionForNesting.mintToken(alice);
-      await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
-    });
-  });
-
-  itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {
-    // Create default collection, permissions are not set:
-    const aliceNFTCollection = await helper.nft.mintCollection(alice);
-    const targetToken = await aliceNFTCollection.mintToken(alice);
-
-    const collectionForNesting = await helper.ft.mintCollection(alice);
-
-    // 1. Alice cannot create immediately nested tokens:
-    await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');
-
-    // 2. Alice can mint and nest token:
-    await collectionForNesting.mint(alice, 100n);
-    await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
-  });
+itSub('Owner can nest FT in NFT', async ({helper}) => {
+  // Only NFT allows nesting, permissions should be set:
+  const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+  const targetToken = await aliceNFTCollection.mintToken(alice);
 
-  itSub.ifWithPallets('Cannot nest to a future collection', [Pallets.ReFungible], async ({helper}) => {
-    const nonExistingCollectionId = await helper.collection.getTotalCount() + 1000;
-    const futureToken = helper.nft.getTokenObject(nonExistingCollectionId, 1);
+  const collectionForNesting = await helper.ft.mintCollection(alice);
 
-    const nftCollectionForNesting = await helper.nft.mintCollection(alice);
-    const rftCollectionForNesting = await helper.rft.mintCollection(alice);
-    const ftCollectionForNesting = await helper.ft.mintCollection(alice);
+  // 1. Alice can immediately create nested tokens:
+  await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());
+  expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);
+  expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);
 
-    // 1. Alice cannot create nested token to future token
-    await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
-    await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
-    await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
+  // 2. Alice can mint and nest token:
+  await collectionForNesting.mint(alice, 100n);
+  await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);
+  expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);
+});
 
-    // 2. Alice cannot mint and nest token:
-    const nft = await nftCollectionForNesting.mintToken(alice);
-    const rft = await rftCollectionForNesting.mintToken(alice, 100n);
-    const _ft = await ftCollectionForNesting.mint(alice, 100n);
-    await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
-    await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');
-    await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('CollectionNotFound');
-  });
 
-  itSub.ifWithPallets('Cannot nest to a future token in a NFT collection', [Pallets.ReFungible], async ({helper}) => {
-    const {collectionId} = await helper.nft.mintCollection(alice);
-    // To avoid UserIsNotAllowedToNest error
-    await helper.collection.setPermissions(alice, collectionId, {nesting: {collectionAdmin: true}});
-    const futureToken = helper.nft.getTokenObject(collectionId, 1);
+itSub.ifWithPallets('Owner can transferFrom nested tokens', [Pallets.ReFungible], async ({helper}) => {
+  const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+  const tokenA = await collectionToNest.mintToken(alice);
+  const tokenB = await collectionToNest.mintToken(alice);
 
-    const nftCollectionForNesting = await helper.nft.mintCollection(alice);
-    const rftCollectionForNesting = await helper.rft.mintCollection(alice);
-    const ftCollectionForNesting = await helper.ft.mintCollection(alice);
+  // Create a nested token
+  const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+  const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+  const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
 
-    // 1. Alice cannot create nested token to future token
-    await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
-    await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
-    await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
-
-    // 2. Alice cannot mint and nest token:
-    const nft = await nftCollectionForNesting.mintToken(alice);
-    const rft = await rftCollectionForNesting.mintToken(alice, 100n);
-    const _ft = await ftCollectionForNesting.mint(alice, 100n);
-    await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
-    await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');
-    await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('TokenNotFound');
-  });
-
-  itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {
-    const existingCollection = await helper.nft.mintCollection(alice);
-    const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);
-    const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);
-
-    const nftCollectionForNesting = await helper.nft.mintCollection(alice);
-
-    // 1. Alice cannot create nested token to collection address
-    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
-    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+  const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());
+  const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());
+  const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());
+  expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
+  expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());
+  expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);
 
-    // 2. Alice cannot mint and nest token to collection address:
-    const nft = await nftCollectionForNesting.mintToken(alice);
-    await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
-    await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
-  });
+  // Transfer the nested token to another token
+  await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());
+  await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);
+  await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);
 
-  itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {
-  // Create default collection, permissions are not set:
-    const rftCollection = await helper.rft.mintCollection(alice);
-    const ftCollection = await helper.ft.mintCollection(alice);
-
-    const rftToken = await rftCollection.mintToken(alice);
-    const _ftToken = await ftCollection.mint(alice, 100n);
-
-    const collectionForNesting = await helper.nft.mintCollection(alice);
+  expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
+  expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());
 
-    // 1. Alice cannot create immediately nested tokens:
-    await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
-    await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
+  expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);
+  expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);
 
-    // 2. Alice cannot mint and nest token:
-    const nestedToken2 = await collectionForNesting.mintToken(alice);
-    await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
-    await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
-  });
+  expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);
+  expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);
 });
addedtests/src/sub/nesting/e2e.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/sub/nesting/e2e.test.ts
@@ -0,0 +1,104 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {expect, itSub, usingPlaygrounds} from '../../util';
+
+describe('Composite nesting tests', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingPlaygrounds(async (helper, privateKey) => {
+      const donor = await privateKey({filename: __filename});
+      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);
+    });
+  });
+
+  itSub('Checks token children e2e', async ({helper}) => {
+    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+    const collectionB = await helper.ft.mintCollection(alice);
+    const collectionC = await helper.rft.mintCollection(alice);
+
+    const targetToken = await collectionA.mintToken(alice);
+    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');
+
+    // Create a nested NFT token
+    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+
+    // Create then nest
+    const tokenB = await collectionA.mintToken(alice);
+    await tokenB.nest(alice, targetToken);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(2);
+
+    // Move token B to a different user outside the nesting tree
+    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+
+    // Create a fungible token in another collection and then nest
+    await collectionB.mint(alice, 10n);
+    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(2);
+
+    // Create a refungible token in another collection and then nest
+    const tokenC = await collectionC.mintToken(alice, 10n);
+    await tokenC.transfer(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+      {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},
+    ]).and.has.length(3);
+
+    // Burn all nested pieces
+    await tokenC.burnFrom(alice, targetToken.nestingAccount(), 2n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ])
+      .and.has.length(2);
+
+    // Move part of the fungible token inside token A deeper in the nesting tree
+    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
+    expect(await targetToken.getChildren()).to.be.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(2);
+    // Nested token also has children now:
+    expect(await tokenA.getChildren()).to.have.deep.members([
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(1);
+
+    // Move the remaining part of the fungible token inside token A deeper in the nesting tree
+    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);
+    expect(await targetToken.getChildren()).to.have.deep.members([
+      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},
+    ]).and.has.length(1);
+    expect(await tokenA.getChildren()).to.have.deep.members([
+      {tokenId: 0, collectionId: collectionB.collectionId},
+    ]).and.has.length(1);
+  });
+});
addedtests/src/sub/nesting/negative.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/sub/nesting/negative.test.ts
@@ -0,0 +1,142 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {expect, itSub, Pallets, usingPlaygrounds} from '../../util';
+import {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';
+import {itEth} from '../../eth/util';
+
+let alice: IKeyringPair;
+
+before(async () => {
+  await usingPlaygrounds(async (helper, privateKey) => {
+    const donor = await privateKey({filename: __filename});
+    [alice] = await helper.arrange.createAccounts([50n], donor);
+  });
+});
+
+[
+  {mode: 'nft' as const, requiredPallets: []},
+  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},
+].map(testCase => {
+  itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {
+    // Create default collection, permissions are not set:
+    const aliceNFTCollection = await helper.nft.mintCollection(alice);
+    const targetToken = await aliceNFTCollection.mintToken(alice);
+
+    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);
+
+    // 1. Alice cannot create immediately nested tokens:
+    const nestingTx = testCase.mode === 'nft'
+      ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())
+      : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());
+    await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+
+    // 2. Alice cannot mint and nest token:
+    const nestedToken2 = await collectionForNesting.mintToken(alice);
+    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+  });
+});
+
+itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {
+  // Create default collection, permissions are not set:
+  const aliceNFTCollection = await helper.nft.mintCollection(alice);
+  const targetToken = await aliceNFTCollection.mintToken(alice);
+
+  const collectionForNesting = await helper.ft.mintCollection(alice);
+
+  // 1. Alice cannot create immediately nested tokens:
+  await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+
+  // 2. Alice can mint and nest token:
+  await collectionForNesting.mint(alice, 100n);
+  await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');
+});
+
+itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {
+  const collection = await helper.nft.mintCollection(alice);
+  // To avoid UserIsNotAllowedToNest error
+  await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});
+
+  // The list of non-existing tokens:
+  const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);
+  const tokenBurnt = await collection.mintToken(alice);
+  await tokenBurnt.burn(alice);
+  const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);
+
+  // The list of collections to nest tokens from:
+  const nftCollectionForNesting = await helper.nft.mintCollection(alice);
+  const rftCollectionForNesting = await helper.rft.mintCollection(alice);
+  const ftCollectionForNesting = await helper.ft.mintCollection(alice);
+
+  const testCases = [
+    {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},
+    {token: tokenBurnt, error: 'TokenNotFound'},
+    {token: tokenNotMintedYet, error: 'TokenNotFound'},
+  ];
+
+  for(const testCase of testCases) {
+    // 1. Alice cannot create nested token to non-existing token
+    await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+
+    // 2. Alice cannot mint and nest token:
+    const nft = await nftCollectionForNesting.mintToken(alice);
+    const rft = await rftCollectionForNesting.mintToken(alice, 100n);
+    const _ft = await ftCollectionForNesting.mint(alice, 100n);
+    await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);
+    await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);
+  }
+});
+
+itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {
+  const existingCollection = await helper.nft.mintCollection(alice);
+  const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);
+  const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);
+
+  const nftCollectionForNesting = await helper.nft.mintCollection(alice);
+
+  // 1. Alice cannot create nested token to collection address
+  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+
+  // 2. Alice cannot mint and nest token to collection address:
+  const nft = await nftCollectionForNesting.mintToken(alice);
+  await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+  await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');
+});
+
+itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {
+  // Create default collection, permissions are not set:
+  const rftCollection = await helper.rft.mintCollection(alice);
+  const ftCollection = await helper.ft.mintCollection(alice);
+
+  const rftToken = await rftCollection.mintToken(alice);
+  const _ftToken = await ftCollection.mint(alice, 100n);
+
+  const collectionForNesting = await helper.nft.mintCollection(alice);
+
+  // 1. Alice cannot create immediately nested tokens:
+  await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
+  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
+
+  // 2. Alice cannot mint and nest token:
+  const nestedToken2 = await collectionForNesting.mintToken(alice);
+  await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');
+  await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');
+});