git.delta.rocks / unique-network / refs/commits / 2dd0a6af978d

difftreelog

add sub tests for nesting

Trubnikov Sergey2023-05-05parent: #bea0737.patch.diff
in: master

8 files changed

deletedtests/src/NativeFungible.test.tsdiffbeforeafterboth
--- a/tests/src/NativeFungible.test.ts
+++ /dev/null
@@ -1,210 +0,0 @@
-// Copyright 2019-2023 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, requirePalletsOrSkip, usingPlaygrounds} from './util';
-import {ICollectionCreationOptions} from './util/playgrounds/types';
-
-describe('Native fungible', () => {
-  let alice: IKeyringPair;
-  let bob: IKeyringPair;
-
-  before(async () => {
-    await usingPlaygrounds(async (helper, privateKey) => {
-      const donor = await privateKey({url: import.meta.url});
-      [alice, bob] = await helper.arrange.createAccounts([10n, 10n], donor);
-    });
-  });
-
-  itSub('destroy_collection()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.burn(alice)).to.be.rejectedWith('common.UnsupportedOperation');
-    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.destroyCollection', [0])).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('add_to_allow_list()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.addToAllowList(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('remove_from_allow_list()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.removeFromAllowList(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('change_collection_owner()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.changeOwner(alice, bob.address)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('add_collection_admin()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.addAdmin(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('remove_collection_admin()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.removeAdmin(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_collection_sponsor()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.setSponsor(alice, bob.address)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('confirm_sponsorship()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.confirmSponsorship(alice)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('remove_collection_sponsor()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.removeSponsor(alice)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('create_item()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.mint(alice, 100n, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_collection_properties()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.setProperties(alice, [{key: 'value'}])).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('delete_collection_properties()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.deleteProperties(alice, ['key'])).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_token_properties()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.setTokenProperties',
-      [0, 0, [{key: 'value'}]],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('delete_token_properties()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.deleteTokenProperties',
-      [0, 0, ['key']],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_transfers_enabled_flag()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.setTransfersEnabledFlag',
-      [0, true],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('burn_item()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.burnItem',
-      [0, 0, 100n],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('burn_from()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.burnTokens(alice, 100n)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('transfer()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    const balanceAliceBefore = await helper.balance.getSubstrate(alice.address);
-    const balanceBobBefore = await helper.balance.getSubstrate(bob.address);
-    await collection.transfer(alice, {Substrate: bob.address}, 100n);
-    const balanceAliceAfter = await helper.balance.getSubstrate(alice.address);
-    const balanceBobAfter = await helper.balance.getSubstrate(bob.address);
-    expect(balanceAliceBefore - balanceAliceAfter > 100n).to.be.true;
-    expect(balanceBobAfter - balanceBobBefore === 100n).to.be.true;
-  });
-
-  itSub('transfer_from()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    const balanceAliceBefore = await helper.balance.getSubstrate(alice.address);
-    const balanceBobBefore = await helper.balance.getSubstrate(bob.address);
-
-    await collection.transferFrom(alice, {Substrate: alice.address}, {Substrate: bob.address}, 100n);
-
-    const balanceAliceAfter = await helper.balance.getSubstrate(alice.address);
-    const balanceBobAfter = await helper.balance.getSubstrate(bob.address);
-    expect(balanceAliceBefore - balanceAliceAfter > 100n).to.be.true;
-    expect(balanceBobAfter - balanceBobBefore === 100n).to.be.true;
-
-    await expect(collection.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address}, 100n)).to.be.rejectedWith('common.NoPermission');
-  });
-
-  itSub('approve()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.approveTokens(alice, {Substrate: bob.address}, 100n)).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('approve_from()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.approveFrom',
-      [{Substrate: alice.address}, {Substrate: bob.address}, 0, 0, 100n],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_collection_limits()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.setLimits(alice, {accountTokenOwnershipLimit: 1})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('set_collection_permissions()', async ({helper}) => {
-    const collection = helper.ft.getCollectionObject(0);
-    await expect(collection.setPermissions(alice, {access: 'AllowList'})).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('repartition()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.repartition',
-      [0, 0, 100n],
-      true,
-    )).to.be.rejectedWith('unique.RepartitionCalledOnNonRefungibleCollection');
-  });
-
-  itSub('force_repair_collection()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.forceRepairCollection',
-      [0],
-      true,
-    )).to.be.rejectedWith('common.UnsupportedOperation');
-  });
-
-  itSub('force_repair_item()', async ({helper}) => {
-    await expect(helper.executeExtrinsic(
-      alice,
-      'api.tx.unique.forceRepairItem',
-      [0, 0],
-      true,
-    )).to.be.rejectedWith('BadOrigin');
-  });
-});
\ No newline at end of file
modifiedtests/src/eth/nativeFungible.test.tsdiffbeforeafterboth
--- a/tests/src/eth/nativeFungible.test.ts
+++ b/tests/src/eth/nativeFungible.test.ts
@@ -117,7 +117,7 @@
     expect(balanceOwnerBefore - 50n > balanceOwnerAfter).to.be.true;
     expect(balanceReceiverBefore === balanceReceiverAfter - 50n).to.be.true;
 
-    await expect(contract.methods.transferFrom(receiver, receiver, 50).call({from: owner})).to.be.rejectedWith('no permission');
+    await expect(contract.methods.transferFrom(receiver, receiver, 50).call({from: owner})).to.be.rejectedWith('ApprovedValueTooLow');
   });
 });
 
addedtests/src/nativeFungible.test.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/nativeFungible.test.ts
@@ -0,0 +1,219 @@
+// Copyright 2019-2023 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('Native fungible', () => {
+  let alice: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingPlaygrounds(async (helper, privateKey) => {
+      const donor = await privateKey({url: import.meta.url});
+      [alice, bob] = await helper.arrange.createAccounts([10n, 10n], donor);
+    });
+  });
+
+  itSub('destroy_collection()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.burn(alice)).to.be.rejectedWith('common.UnsupportedOperation');
+    await expect(helper.executeExtrinsic(alice, 'api.tx.unique.destroyCollection', [0])).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('add_to_allow_list()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.addToAllowList(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('remove_from_allow_list()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.removeFromAllowList(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('change_collection_owner()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.changeOwner(alice, bob.address)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('add_collection_admin()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.addAdmin(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('remove_collection_admin()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.removeAdmin(alice, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_collection_sponsor()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.setSponsor(alice, bob.address)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('confirm_sponsorship()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.confirmSponsorship(alice)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('remove_collection_sponsor()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.removeSponsor(alice)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('create_item()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.mint(alice, 100n, {Substrate: bob.address})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_collection_properties()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.setProperties(alice, [{key: 'value'}])).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('delete_collection_properties()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.deleteProperties(alice, ['key'])).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_token_properties()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.setTokenProperties',
+      [0, 0, [{key: 'value'}]],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('delete_token_properties()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.deleteTokenProperties',
+      [0, 0, ['key']],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_transfers_enabled_flag()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.setTransfersEnabledFlag',
+      [0, true],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('burn_item()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.burnItem',
+      [0, 0, 100n],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('burn_from()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.burnTokens(alice, 100n)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('transfer()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    const balanceAliceBefore = await helper.balance.getSubstrate(alice.address);
+    const balanceBobBefore = await helper.balance.getSubstrate(bob.address);
+    await collection.transfer(alice, {Substrate: bob.address}, 100n);
+    const balanceAliceAfter = await helper.balance.getSubstrate(alice.address);
+    const balanceBobAfter = await helper.balance.getSubstrate(bob.address);
+    expect(balanceAliceBefore - balanceAliceAfter > 100n).to.be.true;
+    expect(balanceBobAfter - balanceBobBefore === 100n).to.be.true;
+  });
+
+  itSub('transfer_from()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    const balanceAliceBefore = await helper.balance.getSubstrate(alice.address);
+    const balanceBobBefore = await helper.balance.getSubstrate(bob.address);
+
+    await collection.transferFrom(alice, {Substrate: alice.address}, {Substrate: bob.address}, 100n);
+
+    const balanceAliceAfter = await helper.balance.getSubstrate(alice.address);
+    const balanceBobAfter = await helper.balance.getSubstrate(bob.address);
+    expect(balanceAliceBefore - balanceAliceAfter > 100n).to.be.true;
+    expect(balanceBobAfter - balanceBobBefore === 100n).to.be.true;
+
+    await expect(collection.transferFrom(alice, {Substrate: bob.address}, {Substrate: alice.address}, 100n)).to.be.rejectedWith('common.ApprovedValueTooLow');
+  });
+
+  itSub('approve()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.approveTokens(alice, {Substrate: bob.address}, 100n)).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('approve_from()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.approveFrom',
+      [{Substrate: alice.address}, {Substrate: bob.address}, 0, 0, 100n],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_collection_limits()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.setLimits(alice, {accountTokenOwnershipLimit: 1})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('set_collection_permissions()', async ({helper}) => {
+    const collection = helper.ft.getCollectionObject(0);
+    await expect(collection.setPermissions(alice, {access: 'AllowList'})).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('repartition()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.repartition',
+      [0, 0, 100n],
+      true,
+    )).to.be.rejectedWith('unique.RepartitionCalledOnNonRefungibleCollection');
+  });
+
+  itSub('force_repair_collection()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.forceRepairCollection',
+      [0],
+      true,
+    )).to.be.rejectedWith('common.UnsupportedOperation');
+  });
+
+  itSub('force_repair_item()', async ({helper}) => {
+    await expect(helper.executeExtrinsic(
+      alice,
+      'api.tx.unique.forceRepairItem',
+      [0, 0],
+      true,
+    )).to.be.rejectedWith('BadOrigin');
+  });
+
+  itSub.only('Nest into NFT token()', async ({helper}) => {
+    const nftCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+    const targetToken = await nftCollection.mintToken(alice);
+
+    const collection = helper.ft.getCollectionObject(0);
+    await collection.transfer(alice, targetToken.nestingAccount(), 100n);
+
+    await collection.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 100n);
+  });
+});
\ No newline at end of file
modifiedtests/src/nesting/unnest.test.tsdiffbeforeafterboth
--- a/tests/src/nesting/unnest.test.ts
+++ b/tests/src/nesting/unnest.test.ts
@@ -47,6 +47,20 @@
     await expect(nestedToken.getOwner()).to.be.rejected;
   });
 
+  itSub('NativeFungible: allows the owner to successfully unnest a token', async ({helper}) => {
+    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
+    const targetToken = await collection.mintToken(alice);
+
+    const collectionFT = helper.ft.getCollectionObject(0);
+
+    // Nest
+    await collectionFT.transfer(alice, targetToken.nestingAccount(), 10n);
+    // Unnest
+    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: alice.address}, 9n), 'while unnesting').to.be.fulfilled;
+
+    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(1n);
+  });
+
   itSub('Fungible: allows the owner to successfully unnest a token', async ({helper}) => {
     const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
     const targetToken = await collection.mintToken(alice);
modifiedtests/src/sub/nesting/common.test.tsdiffbeforeafterboth
--- a/tests/src/sub/nesting/common.test.ts
+++ b/tests/src/sub/nesting/common.test.ts
@@ -88,6 +88,27 @@
   });
 });
 
+[
+  {restrictedMode: true},
+  {restrictedMode: false},
+].map(testCase => {
+  itSub(`Token owner can nest Native FT in NFT if "tokenOwner" permission set  ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {
+    // Only NFT allows nesting, permissions should be set:
+    const targetNFTCollection = await helper.nft.mintCollection(alice);
+    const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});
+
+    const collectionForNesting = helper.ft.getCollectionObject(0);
+    // permissions should be set:
+    await targetNFTCollection.setPermissions(alice, {
+      nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},
+    });
+
+    // Alice can mint and nest token:
+    await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);
+    expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(50n);
+  });
+});
+
 
 itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {
   const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});
@@ -98,18 +119,22 @@
   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}}});
+  const nativeFtCollectionToBeNested = helper.ft.getCollectionObject(0);
 
   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());
+  await nativeFtCollectionToBeNested.transfer(alice, tokenA.nestingAccount(), 100n);
   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);
+  expect(await nativeFtCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);
 
   // 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);
+  await nativeFtCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);
 
   expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});
   expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());
@@ -119,4 +144,7 @@
 
   expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);
   expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);
+
+  expect(await nativeFtCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);
+  expect(await nativeFtCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);
 });
modifiedtests/src/sub/nesting/e2e.test.tsdiffbeforeafterboth
--- a/tests/src/sub/nesting/e2e.test.ts
+++ b/tests/src/sub/nesting/e2e.test.ts
@@ -32,6 +32,7 @@
     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 collectionD = helper.ft.getCollectionObject(0);
 
     const targetToken = await collectionA.mintToken(alice);
     expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');
@@ -73,20 +74,31 @@
       {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},
     ]).and.has.length(3);
 
+    // Nest native fungible token into another collection
+    await collectionD.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},
+      {tokenId: 0, collectionId: collectionD.collectionId},
+    ]).and.has.length(4);
+
     // 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},
+      {tokenId: 0, collectionId: collectionD.collectionId},
     ])
-      .and.has.length(2);
+      .and.has.length(3);
 
     // 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);
+      {tokenId: 0, collectionId: collectionD.collectionId},
+    ]).and.has.length(3);
     // Nested token also has children now:
     expect(await tokenA.getChildren()).to.have.deep.members([
       {tokenId: 0, collectionId: collectionB.collectionId},
@@ -96,7 +108,8 @@
     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);
+      {tokenId: 0, collectionId: collectionD.collectionId},
+    ]).and.has.length(2);
     expect(await tokenA.getChildren()).to.have.deep.members([
       {tokenId: 0, collectionId: collectionB.collectionId},
     ]).and.has.length(1);
modifiedtests/src/sub/nesting/nesting.negative.test.tsdiffbeforeafterboth
before · tests/src/sub/nesting/nesting.negative.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 {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '../../util/playgrounds/unique';20import {itEth} from '../../eth/util';2122let alice: IKeyringPair;23let bob: IKeyringPair;24let charlie: IKeyringPair;2526before(async () => {27  await usingPlaygrounds(async (helper, privateKey) => {28    const donor = await privateKey({url: import.meta.url});29    [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30  });31});3233[34  {mode: 'nft' as const, requiredPallets: []},35  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37  itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {38    // Create default collection, permissions are not set:39    const aliceNFTCollection = await helper.nft.mintCollection(alice);40    const targetToken = await aliceNFTCollection.mintToken(alice);4142    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4344    // 1. Alice cannot create immediately nested tokens:45    const nestingTx = testCase.mode === 'nft'46      ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())47      : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());48    await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');4950    // 2. Alice cannot mint and nest token:51    const nestedToken2 = await collectionForNesting.mintToken(alice);52    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');53  });54});5556itSub('Owner cannot nest FT if nesting is disabled', async ({helper}) => {57  // Create default collection, permissions are not set:58  const aliceNFTCollection = await helper.nft.mintCollection(alice);59  const targetToken = await aliceNFTCollection.mintToken(alice);6061  const collectionForNesting = await helper.ft.mintCollection(alice);6263  // 1. Alice cannot create immediately nested tokens:64  await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');6566  // 2. Alice can mint and nest token:67  await collectionForNesting.mint(alice, 100n);68  await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');69});7071[72  {mode: 'nft' as const},73  {mode: 'rft' as const},74  {mode: 'ft' as const},75].map(testCase => {76  itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {77    const targetCollection = await helper.nft.mintCollection(alice, {permissions:78      {nesting: {tokenOwner: true, collectionAdmin: true}},79    });80    const targetToken = await targetCollection.mintToken(alice);8182    const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);8384    let nestedTokenBob: UniqueNFToken | UniqueRFToken;85    switch (testCase.mode) {86      case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;87      case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;88      case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;89    }9091    // Bob non-owner of targetToken and non admin of targetCollection, so92    // 1. cannot mint nested token:93    switch (testCase.mode) {94      case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;95      case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;96      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;97    }9899    // 2. cannot nest existing token:100    switch (testCase.mode) {101      case 'nft':102      case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;103      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;104    }105  });106});107108[109  {mode: 'nft' as const, nesting: {tokenOwner: true,  collectionAdmin: false}},110  {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},111].map(testCase => {112  itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {113    // Create collection with tokenOwner or create collection with collectionAdmin permission:114    const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});115    const targetTokenCharlie = await targetCollection.mintToken(alice, {Substrate: charlie.address});116    await targetCollection.addAdmin(alice, {Substrate: bob.address});117118    const nestedCollectionCharlie = await helper[testCase.mode].mintCollection(charlie);119    const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);120    // if nesting permissions restricted for token owner – minter is bob (admin),121    // if collectionAdmin – charlie (owner)122    testCase.nesting.tokenOwner123      ? await expect(nestedCollectionBob.mintToken(bob, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)124      : await expect(nestedCollectionCharlie.mintToken(charlie, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);125  });126});127128itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {129  const collection = await helper.nft.mintCollection(alice);130  // To avoid UserIsNotAllowedToNest error131  await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});132133  // The list of non-existing tokens:134  const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);135  const tokenBurnt = await collection.mintToken(alice);136  await tokenBurnt.burn(alice);137  const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);138139  // The list of collections to nest tokens from:140  const nftCollectionForNesting = await helper.nft.mintCollection(alice);141  const rftCollectionForNesting = await helper.rft.mintCollection(alice);142  const ftCollectionForNesting = await helper.ft.mintCollection(alice);143144  const testCases = [145    {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},146    {token: tokenBurnt, error: 'TokenNotFound'},147    {token: tokenNotMintedYet, error: 'TokenNotFound'},148  ];149150  for(const testCase of testCases) {151    // 1. Alice cannot create nested token to non-existing token152    await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);153    await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);154    await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);155156    // 2. Alice cannot mint and nest token:157    const nft = await nftCollectionForNesting.mintToken(alice);158    const rft = await rftCollectionForNesting.mintToken(alice, 100n);159    const _ft = await ftCollectionForNesting.mint(alice, 100n);160    await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);161    await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);162    await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);163  }164});165166itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {167  const existingCollection = await helper.nft.mintCollection(alice);168  const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);169  const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);170171  const nftCollectionForNesting = await helper.nft.mintCollection(alice);172173  // 1. Alice cannot create nested token to collection address174  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');175  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');176177  // 2. Alice cannot mint and nest token to collection address:178  const nft = await nftCollectionForNesting.mintToken(alice);179  await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');180  await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');181});182183itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {184  // Create default collection, permissions are not set:185  const rftCollection = await helper.rft.mintCollection(alice);186  const ftCollection = await helper.ft.mintCollection(alice);187188  const rftToken = await rftCollection.mintToken(alice);189  const _ftToken = await ftCollection.mint(alice, 100n);190191  const collectionForNesting = await helper.nft.mintCollection(alice);192193  // 1. Alice cannot create immediately nested tokens:194  await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');195  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');196197  // 2. Alice cannot mint and nest token:198  const nestedToken2 = await collectionForNesting.mintToken(alice);199  await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');200  await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');201});202203itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {204  const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);205  const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);206  const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);207  const notAllowedCollectionFT = await helper.ft.mintCollection(alice);208209  // Collection restricted to allowedCollectionId210  const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:211    {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},212  });213  // Create collection with restricted nesting -- even self is not allowed214  const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:215    {nesting: {tokenOwner: true, restricted: []}},216  });217218  const targetTokenA = await restrictedCollectionA.mintToken(alice);219  const targetTokenB = await restrictedCollectionB.mintToken(alice);220221  // 1. Cannot mint in own collection after allowlisting the accounts:222  await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);223  await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);224225  // 2. Cannot mint from notAllowedCollection:226  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);227  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);228  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);229  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);230  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);231  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);232});233234itSub('Cannot create nesting chains greater than 5', 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  // 1. Cannot mint:247  await expect(collection.mintToken(alice, token.nestingAccount()))248    .to.be.rejectedWith(/structure\.DepthLimit/);249  // 2. Cannot transfer:250  const anotherToken = await collection.mintToken(alice);251  await expect(anotherToken.transfer(alice, token.nestingAccount()))252    .to.be.rejectedWith(/structure\.DepthLimit/);253  // 3. Cannot nest FT pieces:254  const ftCollection = await helper.ft.mintCollection(alice);255  await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))256    .to.be.rejectedWith(/structure\.DepthLimit/);257258  expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});259  expect(await token.getChildren()).to.has.length(0);260});
after · tests/src/sub/nesting/nesting.negative.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 {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '../../util/playgrounds/unique';20import {itEth} from '../../eth/util';2122let alice: IKeyringPair;23let bob: IKeyringPair;24let charlie: IKeyringPair;2526before(async () => {27  await usingPlaygrounds(async (helper, privateKey) => {28    const donor = await privateKey({url: import.meta.url});29    [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30  });31});3233[34  {mode: 'nft' as const, requiredPallets: []},35  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37  itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {38    // Create default collection, permissions are not set:39    const aliceNFTCollection = await helper.nft.mintCollection(alice);40    const targetToken = await aliceNFTCollection.mintToken(alice);4142    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4344    // 1. Alice cannot create immediately nested tokens:45    const nestingTx = testCase.mode === 'nft'46      ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())47      : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());48    await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');4950    // 2. Alice cannot mint and nest token:51    const nestedToken2 = await collectionForNesting.mintToken(alice);52    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');53  });54});5556[57  {mode: 'ft'},58  {mode: 'nativeFt'},59].map(testCase => {60  itSub(`Owner cannot nest [${testCase.mode}] if nesting is disabled`, async ({helper}) => {61  // Create default collection, permissions are not set:62    const aliceNFTCollection = await helper.nft.mintCollection(alice);63    const targetToken = await aliceNFTCollection.mintToken(alice);6465    const collectionForNesting = testCase.mode === 'ft' ? await helper.ft.mintCollection(alice) : helper.ft.getCollectionObject(0);6667    // Alice cannot create immediately nested tokens:68    await expect(testCase.mode === 'ft'69      ? collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())70      : collectionForNesting.transfer(alice, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');7172    // Alice can mint and nest token:73    if (testCase.mode === 'ft') {74      await collectionForNesting.mint(alice, 100n);75    }76    await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');77  });78});7980[81  {mode: 'nft' as const},82  {mode: 'rft' as const},83  {mode: 'ft' as const},84  {mode: 'native ft' as const},85].map(testCase => {86  itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {87    const targetCollection = await helper.nft.mintCollection(alice, {permissions:88      {nesting: {tokenOwner: true, collectionAdmin: true}},89    });90    const targetToken = await targetCollection.mintToken(alice);9192    const nestedCollectionBob = await (93      testCase.mode === 'native ft'94        ? helper.ft.getCollectionObject(0)95        : helper[testCase.mode].mintCollection(bob)96    );9798    let nestedTokenBob: UniqueNFToken | UniqueRFToken;99    switch (testCase.mode) {100      case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;101      case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;102      case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;103      case 'native ft': break;104    }105106    // Bob non-owner of targetToken and non admin of targetCollection, so107    // 1. cannot mint nested token:108    switch (testCase.mode) {109      case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;110      case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;111      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;112      case 'native ft': break;113    }114115    // 2. cannot nest existing token:116    switch (testCase.mode) {117      case 'nft':118      case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;119      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;120      case 'native ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;121    }122  });123});124125[126  {mode: 'nft' as const, nesting: {tokenOwner: true,  collectionAdmin: false}},127  {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},128].map(testCase => {129  itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {130    // Create collection with tokenOwner or create collection with collectionAdmin permission:131    const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});132    const targetTokenCharlie = await targetCollection.mintToken(alice, {Substrate: charlie.address});133    await targetCollection.addAdmin(alice, {Substrate: bob.address});134135    const nestedCollectionCharlie = await helper[testCase.mode].mintCollection(charlie);136    const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);137    // if nesting permissions restricted for token owner – minter is bob (admin),138    // if collectionAdmin – charlie (owner)139    testCase.nesting.tokenOwner140      ? await expect(nestedCollectionBob.mintToken(bob, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)141      : await expect(nestedCollectionCharlie.mintToken(charlie, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);142  });143});144145itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {146  const collection = await helper.nft.mintCollection(alice);147  // To avoid UserIsNotAllowedToNest error148  await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});149150  // The list of non-existing tokens:151  const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);152  const tokenBurnt = await collection.mintToken(alice);153  await tokenBurnt.burn(alice);154  const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);155156  // The list of collections to nest tokens from:157  const nftCollectionForNesting = await helper.nft.mintCollection(alice);158  const rftCollectionForNesting = await helper.rft.mintCollection(alice);159  const ftCollectionForNesting = await helper.ft.mintCollection(alice);160  const nativeFtCollectionForNesting = helper.ft.getCollectionObject(0);161162  const testCases = [163    {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},164    {token: tokenBurnt, error: 'TokenNotFound'},165    {token: tokenNotMintedYet, error: 'TokenNotFound'},166  ];167168  for(const testCase of testCases) {169    // 1. Alice cannot create nested token to non-existing token170    await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);171    await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);172    await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);173174    // 2. Alice cannot mint and nest token:175    const nft = await nftCollectionForNesting.mintToken(alice);176    const rft = await rftCollectionForNesting.mintToken(alice, 100n);177    const _ft = await ftCollectionForNesting.mint(alice, 100n);178    await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);179    await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);180    await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);181    await expect(nativeFtCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);182  }183});184185itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {186  const existingCollection = await helper.nft.mintCollection(alice);187  const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);188  const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);189190  const nftCollectionForNesting = await helper.nft.mintCollection(alice);191192  // 1. Alice cannot create nested token to collection address193  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');194  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');195196  // 2. Alice cannot mint and nest token to collection address:197  const nft = await nftCollectionForNesting.mintToken(alice);198  await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');199  await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');200});201202itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {203  // Create default collection, permissions are not set:204  const rftCollection = await helper.rft.mintCollection(alice);205  const ftCollection = await helper.ft.mintCollection(alice);206  const nativeFtCollection = helper.ft.getCollectionObject(0);207208  const rftToken = await rftCollection.mintToken(alice);209  const _ftToken = await ftCollection.mint(alice, 100n);210211  const collectionForNesting = await helper.nft.mintCollection(alice);212213  // 1. Alice cannot create immediately nested tokens:214  await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');215  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');216  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');217218  // 2. Alice cannot mint and nest token:219  const nestedToken2 = await collectionForNesting.mintToken(alice);220  await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');221  await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');222  await expect(nativeFtCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(nativeFtCollection.collectionId, 0)})).to.be.rejectedWith('common.UnsupportedOperation');223});224225itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {226  const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);227  const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);228  const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);229  const notAllowedCollectionFT = await helper.ft.mintCollection(alice);230  const notAllowedCollectionNativeFT = helper.ft.getCollectionObject(0);231232  // Collection restricted to allowedCollectionId233  const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:234    {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},235  });236  // Create collection with restricted nesting -- even self is not allowed237  const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:238    {nesting: {tokenOwner: true, restricted: []}},239  });240241  const targetTokenA = await restrictedCollectionA.mintToken(alice);242  const targetTokenB = await restrictedCollectionB.mintToken(alice);243244  // 1. Cannot mint in own collection after allowlisting the accounts:245  await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);246  await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);247248  // 2. Cannot mint from notAllowedCollection:249  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);250  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);251  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);252  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);253  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);254  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);255  await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenA.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);256  await expect(notAllowedCollectionNativeFT.transfer(alice, targetTokenB.nestingAccount(), 100n)).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);257});258259itSub('Cannot create nesting chains greater than 5', async ({helper}) => {260  const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});261  let token = await collection.mintToken(alice);262263  const maxNestingLevel = 5;264265  // Create a nested-token matryoshka266  for (let i = 0; i < maxNestingLevel; i++) {267    token = await collection.mintToken(alice, token.nestingAccount());268  }269270  // The nesting depth is limited by `maxNestingLevel`271  // 1. Cannot mint:272  await expect(collection.mintToken(alice, token.nestingAccount()))273    .to.be.rejectedWith(/structure\.DepthLimit/);274  // 2. Cannot transfer:275  const anotherToken = await collection.mintToken(alice);276  await expect(anotherToken.transfer(alice, token.nestingAccount()))277    .to.be.rejectedWith(/structure\.DepthLimit/);278  // 3. Cannot nest FT pieces:279  const ftCollection = await helper.ft.mintCollection(alice);280  await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))281    .to.be.rejectedWith(/structure\.DepthLimit/);282283  expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});284  expect(await token.getChildren()).to.has.length(0);285});
modifiedtests/src/sub/nesting/unnesting.negative.test.tsdiffbeforeafterboth
--- a/tests/src/sub/nesting/unnesting.negative.test.ts
+++ b/tests/src/sub/nesting/unnesting.negative.test.ts
@@ -52,12 +52,19 @@
   });
 
   [
-    {restrictedMode: true},
-    {restrictedMode: false},
+    {mode: 'ft' as const},
+    {mode: 'native ft' as const},
+  ].map(md => [
+    {mode: md.mode, restrictedMode: true},
+    {mode: md.mode, restrictedMode: false},
   ].map(testCase => {
-    itSub(`Fungible: disallows a non-Owner to unnest someone else's token ${testCase.restrictedMode ? '(Restricted nesting)' : ''}`, async ({helper}) => {
+    itSub.only(`Fungible: disallows a non-Owner to unnest someone else's token [${testCase.mode}${testCase.restrictedMode ? ' (Restricted nesting)' : ''}]`, async ({helper}) => {
       const collectionNFT = await helper.nft.mintCollection(alice);
-      const collectionFT = await helper.ft.mintCollection(alice);
+      const collectionFT = await (
+        testCase.mode === 'ft'
+          ? helper.ft.mintCollection(alice)
+          : helper.ft.getCollectionObject(0)
+      );
       const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});
 
       await collectionNFT.setPermissions(alice, {nesting: {
@@ -65,12 +72,16 @@
       }});
 
       // Nest some tokens as Alice into Bob's token
-      await collectionFT.mint(alice, 5n, targetToken.nestingAccount());
+      await (
+        testCase.mode === 'ft'
+          ? collectionFT.mint(alice, 5n, targetToken.nestingAccount())
+          : collectionFT.transfer(alice, targetToken.nestingAccount(), 5n)
+      );
 
       // Try to pull it out as Alice still
       await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))
         .to.be.rejectedWith(/common\.ApprovedValueTooLow/);
       expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);
     });
-  });
+  }));
 });