git.delta.rocks / unique-network / refs/commits / 004643cad6df

difftreelog

fix unnest NFT from parent

Daniel Shiposha2022-06-03parent: #4d135e3.patch.diff
in: master

3 files changed

modifiedpallets/nonfungible/src/lib.rsdiffbeforeafterboth
--- a/pallets/nonfungible/src/lib.rs
+++ b/pallets/nonfungible/src/lib.rs
@@ -604,7 +604,7 @@
 
 		// =========
 
-		<PalletStructure<T>>::unnest_if_nested(from, collection.id, token);
+		<PalletStructure<T>>::unnest_if_nested(&token_data.owner, collection.id, token);
 
 		<TokenData<T>>::insert(
 			(collection.id, token),
modifiedpallets/structure/src/lib.rsdiffbeforeafterboth
--- a/pallets/structure/src/lib.rs
+++ b/pallets/structure/src/lib.rs
@@ -191,8 +191,8 @@
 		token_id: TokenId,
 		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
-		Self::try_exec_if_owner_is_valid_nft(under, |d, parent_id| {
-			d.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)
+		Self::try_exec_if_owner_is_valid_nft(under, |collection, parent_id| {
+			collection.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)
 		})
 	}
 
@@ -203,10 +203,10 @@
 		token_id: TokenId,
 		nesting_budget: &dyn Budget,
 	) -> DispatchResult {
-		Self::try_exec_if_owner_is_valid_nft(under, |d, parent_id| {
-			d.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)?;
+		Self::try_exec_if_owner_is_valid_nft(under, |collection, parent_id| {
+			collection.check_nesting(from, (collection_id, token_id), parent_id, nesting_budget)?;
 
-			d.nest(parent_id, (collection_id, token_id));
+			collection.nest(parent_id, (collection_id, token_id));
 
 			Ok(())
 		})
@@ -217,8 +217,8 @@
 		collection_id: CollectionId,
 		token_id: TokenId,
 	) {
-		Self::exec_if_owner_is_valid_nft(owner, |d, parent_id| {
-			d.nest(parent_id, (collection_id, token_id))
+		Self::exec_if_owner_is_valid_nft(owner, |collection, parent_id| {
+			collection.nest(parent_id, (collection_id, token_id))
 		});
 	}
 
@@ -227,8 +227,8 @@
 		collection_id: CollectionId,
 		token_id: TokenId,
 	) {
-		Self::exec_if_owner_is_valid_nft(owner, |d, parent_id| {
-			d.unnest(parent_id, (collection_id, token_id))
+		Self::exec_if_owner_is_valid_nft(owner, |collection, parent_id| {
+			collection.unnest(parent_id, (collection_id, token_id))
 		});
 	}
 
@@ -236,8 +236,8 @@
 		account: &T::CrossAccountId,
 		action: impl FnOnce(&dyn CommonCollectionOperations<T>, TokenId),
 	) {
-		Self::try_exec_if_owner_is_valid_nft(account, |d, id| {
-			action(d, id);
+		Self::try_exec_if_owner_is_valid_nft(account, |collection, id| {
+			action(collection, id);
 			Ok(())
 		})
 		.unwrap();
modifiedtests/src/nesting/nest.test.tsdiffbeforeafterboth
before · tests/src/nesting/nest.test.ts
1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import privateKey from '../substrate/privateKey';4import usingApi, {executeTransaction} from '../substrate/substrate-api';5import {6  addToAllowListExpectSuccess,7  createCollectionExpectSuccess,8  createItemExpectSuccess,9  enableAllowListExpectSuccess,10  enablePublicMintingExpectSuccess,11  getTokenChildren,12  getTokenOwner,13  getTopmostTokenOwner,14  normalizeAccountId,15  setCollectionPermissionsExpectSuccess,16  transferExpectFailure,17  transferExpectSuccess,18  transferFromExpectSuccess,19} from '../util/helpers';20import {IKeyringPair} from '@polkadot/types/types';2122let alice: IKeyringPair;23let bob: IKeyringPair;2425describe('Integration Test: Nesting', () => {26  before(async () => {27    await usingApi(async () => {28      alice = privateKey('//Alice');29      bob = privateKey('//Bob');30    });31  });3233  it('Performs the full suite: bundles a token, transfers, and unnests', async () => {34    await usingApi(async api => {35      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});36      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});37      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');3839      // Create a nested token40      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});41      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});42      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});4344      // Create a token to be nested45      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');4647      // Nest48      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});49      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});50      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5152      // Move bundle to different user53      await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});54      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});55      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5657      // Unnest58      await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});59      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});60    });61  });6263  it('Transfers an already bundled token', async () => {64    await usingApi(async api => {65      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});66      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});6768      const tokenA = await createItemExpectSuccess(alice, collection, 'NFT');69      const tokenB = await createItemExpectSuccess(alice, collection, 'NFT');7071      // Create a nested token72      const tokenC = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, tokenA)});73      expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});74      expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenA).toLowerCase()});7576      // Transfer the nested token to another token77      await expect(executeTransaction(78        api,79        alice,80        api.tx.unique.transferFrom(81          normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenA)}), 82          normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenB)}), 83          collection,84          tokenC,85          1,86        ),87      )).to.not.be.rejected;88      expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});89      expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenB).toLowerCase()});90    });91  });9293  it('Checks token children', async () => {94    await usingApi(async api => {95      const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});96      await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: 'Owner'});97      const collectionB = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});9899      const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');100      const targetAddress = {Ethereum: tokenIdToAddress(collectionA, targetToken)};101      let children = await getTokenChildren(api, collectionA, targetToken);102      expect(children.length).to.be.equal(0, 'Children length check at creation');103104      // Create a nested NFT token105      const tokenA = await createItemExpectSuccess(alice, collectionA, 'NFT', targetAddress);106      children = await getTokenChildren(api, collectionA, targetToken);107      expect(children.length).to.be.equal(1, 'Children length check at nesting #1');108      expect(children).to.have.deep.members([109        {token: tokenA, collection: collectionA},110      ], 'Children contents check at nesting #1');111112      // Create then nest113      const tokenB = await createItemExpectSuccess(alice, collectionA, 'NFT');114      await transferExpectSuccess(collectionA, tokenB, alice, targetAddress);115      children = await getTokenChildren(api, collectionA, targetToken);116      expect(children.length).to.be.equal(2, 'Children length check at nesting #2');117      expect(children).to.have.deep.members([118        {token: tokenA, collection: collectionA},119        {token: tokenB, collection: collectionA},120      ], 'Children contents check at nesting #2');121122      // Move token B to a different user outside the nesting tree123      await transferFromExpectSuccess(collectionA, tokenB, alice, targetAddress, bob);124      children = await getTokenChildren(api, collectionA, targetToken);125      expect(children.length).to.be.equal(1, 'Children length check at unnesting');126      expect(children).to.be.have.deep.members([127        {token: tokenA, collection: collectionA},128      ], 'Children contents check at unnesting');129      130      // Create a fungible token in another collection and then nest131      const tokenC = await createItemExpectSuccess(alice, collectionB, 'Fungible');132      await transferExpectSuccess(collectionB, tokenC, alice, targetAddress, 1, 'Fungible');133      children = await getTokenChildren(api, collectionA, targetToken);134      expect(children.length).to.be.equal(2, 'Children length check at nesting #3 (from another collection)');135      expect(children).to.be.have.deep.members([136        {token: tokenA, collection: collectionA},137        {token: tokenC, collection: collectionB},138      ], 'Children contents check at nesting #3 (from another collection)');139140      // Move the fungible token inside token A deeper in the nesting tree141      await transferFromExpectSuccess(collectionB, tokenC, alice, targetAddress, {Ethereum: tokenIdToAddress(collectionA, tokenA)}, 1, 'Fungible');142      children = await getTokenChildren(api, collectionA, targetToken);143      expect(children.length).to.be.equal(1, 'Children length check at deeper nesting');144      expect(children).to.be.have.deep.members([145        {token: tokenA, collection: collectionA},146      ], 'Children contents check at deeper nesting');147    });148  });149150  // ---------- Non-Fungible ----------151152  it('NFT: allows an Owner to nest/unnest their token', async () => {153    await usingApi(async api => {154      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});155      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});156      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');157158      // Create a nested token159      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});160      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});161      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});162163      // Create a token to be nested and nest164      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');165      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});166      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});167      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});168    });169  });170171  it('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {172    await usingApi(async api => {173      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});174      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[collection]}});175      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');176177      // Create a nested token178      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});179      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});180      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});181182      // Create a token to be nested and nest183      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');184      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});185      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});186      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});187    });188  });189190  // ---------- Fungible ----------191192  it('Fungible: allows an Owner to nest/unnest their token', async () => {193    await usingApi(async api => {194      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});195      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});196      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});197      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};198199      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});200201      // Create a nested token202      await expect(executeTransaction(api, alice, api.tx.unique.createItem(203        collectionFT,204        targetAddress,205        {Fungible: {Value: 10}},206      ))).to.not.be.rejected;207208      // Nest a new token209      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');210      await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');211    });212  });213214  it('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {215    await usingApi(async api => {216      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});217      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});218      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};219220      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});221222      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted: [collectionFT]}});223224      // Create a nested token225      await expect(executeTransaction(api, alice, api.tx.unique.createItem(226        collectionFT,227        targetAddress,228        {Fungible: {Value: 10}},229      ))).to.not.be.rejected;230231      // Nest a new token232      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');233      await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');234    });235  });236237  // ---------- Re-Fungible ----------238239  it('ReFungible: allows an Owner to nest/unnest their token', async () => {240    await usingApi(async api => {241      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});242      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});243      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});244      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};245246      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});247248      // Create a nested token249      await expect(executeTransaction(api, alice, api.tx.unique.createItem(250        collectionRFT,251        targetAddress,252        {ReFungible: {const_data: [], pieces: 100}},253      ))).to.not.be.rejected;254255      // Nest a new token256      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');257      await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');258    });259  });260261  it('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {262    await usingApi(async api => {263      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});264      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});265      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};266267      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});268269      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionRFT]}});270271      // Create a nested token272      await expect(executeTransaction(api, alice, api.tx.unique.createItem(273        collectionRFT,274        targetAddress,275        {ReFungible: {const_data: [], pieces: 100}},276      ))).to.not.be.rejected;277278      // Nest a new token279      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');280      await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');281    });282  });283});284285describe('Negative Test: Nesting', async() => {286  before(async () => {287    await usingApi(async () => {288      alice = privateKey('//Alice');289      bob = privateKey('//Bob');290    });291  });292293  // TODO delete if this is actually wrong294  // TODO remake all other nesting tests if this is right295  it('Affirms that transfer is disallowed to transfer nested tokens', async () => {296    await usingApi(async () => {297      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});298      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});299300      const tokenA = await createItemExpectSuccess(alice, collection, 'NFT');301      const tokenB = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, tokenA)});302      303      await transferExpectFailure(collection, tokenB, alice, bob);304    });305  });306307  it('Disallows excessive token nesting', async () => {308    await usingApi(async api => {309      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});310      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});311      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');312313      const maxNestingLevel = 5;314      let prevToken = targetToken;315316      // Create a nested-token matryoshka317      for (let i = 0; i < maxNestingLevel; i++) {318        const nestedToken = await createItemExpectSuccess(319          alice,320          collection,321          'NFT',322          {Ethereum: tokenIdToAddress(collection, prevToken)},323        );324325        prevToken = nestedToken;326      }327328      // The nesting depth is limited by `maxNestingLevel`329      await expect(executeTransaction(api, alice, api.tx.unique.createItem(330        collection,331        {Ethereum: tokenIdToAddress(collection, prevToken)},332          {nft: {const_data: [], variable_data: []}} as any,333      )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);334335      expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});336    });337  });338339  // ---------- Non-Fungible ----------340341  it('NFT: disallows to nest token if nesting is disabled', async () => {342    await usingApi(async api => {343      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});344      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Disabled'});345      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');346347      // Try to create a nested token348      await expect(executeTransaction(api, alice, api.tx.unique.createItem(349        collection,350        {Ethereum: tokenIdToAddress(collection, targetToken)},351          {nft: {const_data: [], variable_data: []}} as any,352      )), 'while creating nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);353354      // Create a token to be nested355      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');356      // Try to nest357      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);358      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});359      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});360    });361  });362363  it('NFT: disallows a non-Owner to nest someone else\'s token', async () => {364    await usingApi(async api => {365      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});366      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});367368      await addToAllowListExpectSuccess(alice, collection, bob.address);369      await enableAllowListExpectSuccess(alice, collection);370      await enablePublicMintingExpectSuccess(alice, collection);371372      // Create a token to attempt to be nested into373      const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');374375      // Try to create a nested token in the wrong collection376      await expect(executeTransaction(api, alice, api.tx.unique.createItem(377        collection,378        {Ethereum: tokenIdToAddress(collection, targetToken)},379          {nft: {const_data: [], variable_data: []}} as any,380      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);381382      // Try to create and nest a token in the wrong collection383      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');384      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);385      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});386    });387  });388389  it('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {390    await usingApi(async api => {391      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});392      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[collection]}});393394      await addToAllowListExpectSuccess(alice, collection, bob.address);395      await enableAllowListExpectSuccess(alice, collection);396      await enablePublicMintingExpectSuccess(alice, collection);397398      // Create a token to attempt to be nested into399      const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');400401      // Try to create a nested token in the wrong collection402      await expect(executeTransaction(api, alice, api.tx.unique.createItem(403        collection,404        {Ethereum: tokenIdToAddress(collection, targetToken)},405          {nft: {const_data: [], variable_data: []}} as any,406      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);407408      // Try to create and nest a token in the wrong collection409      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');410      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);411      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});412    });413  });414415  it('NFT: disallows to nest token in an unlisted collection', async () => {416    await usingApi(async api => {417      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});418      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[]}});419420      // Create a token to attempt to be nested into421      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');422423      // Try to create a nested token in the wrong collection424      await expect(executeTransaction(api, alice, api.tx.unique.createItem(425        collection,426        {Ethereum: tokenIdToAddress(collection, targetToken)},427          {nft: {const_data: [], variable_data: []}} as any,428      )), 'while creating nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);429430      // Try to create and nest a token in the wrong collection431      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');432      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);433      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});434    });435  });436437  // ---------- Fungible ----------438439  it('Fungible: disallows to nest token if nesting is disabled', async () => {440    await usingApi(async api => {441      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});442      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Disabled'});443      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');444      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};445446      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});447448      // Try to create a nested token449      await expect(executeTransaction(api, alice, api.tx.unique.createItem(450        collectionFT,451        targetAddress,452        {Fungible: {Value: 10}},453      )), 'while creating nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);454455      // Create a token to be nested456      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');457      // Try to nest458      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);459460      // Create another token to be nested461      const newToken2 = await createItemExpectSuccess(alice, collectionFT, 'Fungible');462      // Try to nest inside a fungible token463      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionFT, newToken)}, collectionFT, newToken2, 1)), 'while nesting new token inside fungible').to.be.rejectedWith(/fungible\.FungibleDisallowsNesting/);464    });465  });466467  it('Fungible: disallows a non-Owner to nest someone else\'s token', async () => {468    await usingApi(async api => {469      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});470      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});471472      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);473      await enableAllowListExpectSuccess(alice, collectionNFT);474      await enablePublicMintingExpectSuccess(alice, collectionNFT);475476      // Create a token to attempt to be nested into477      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');478      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};479480      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});481482      // Try to create a nested token in the wrong collection483      await expect(executeTransaction(api, alice, api.tx.unique.createItem(484        collectionFT,485        targetAddress,486        {Fungible: {Value: 10}},487      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);488489      // Try to create and nest a token in the wrong collection490      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');491      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);492    });493  });494495  it('Fungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {496    await usingApi(async api => {497      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});498      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);499      await enableAllowListExpectSuccess(alice, collectionNFT);500      await enablePublicMintingExpectSuccess(alice, collectionNFT);501502      // Create a token to attempt to be nested into503      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');504      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};505506      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});507      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionFT]}});508509      // Try to create a nested token in the wrong collection510      await expect(executeTransaction(api, alice, api.tx.unique.createItem(511        collectionFT,512        targetAddress,513        {Fungible: {Value: 10}},514      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);515516      // Try to create and nest a token in the wrong collection517      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');518      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);519    });520  });521522  it('Fungible: disallows to nest token in an unlisted collection', async () => {523    await usingApi(async api => {524      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});525      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[]}});526527      // Create a token to attempt to be nested into528      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');529      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};530531      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});532533      // Try to create a nested token in the wrong collection534      await expect(executeTransaction(api, alice, api.tx.unique.createItem(535        collectionFT,536        targetAddress,537        {Fungible: {Value: 10}},538      )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);539540      // Try to create and nest a token in the wrong collection541      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');542      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);543    });544  });545546  // ---------- Re-Fungible ----------547548  it('ReFungible: disallows to nest token if nesting is disabled', async () => {549    await usingApi(async api => {550      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});551      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Disabled'});552      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');553      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};554555      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});556557      // Create a nested token558      await expect(executeTransaction(api, alice, api.tx.unique.createItem(559        collectionRFT,560        targetAddress,561        {ReFungible: {const_data: [], pieces: 100}},562      )), 'while creating a nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);563564      // Create a token to be nested565      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');566      // Try to nest567      await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);568      // Try to nest569      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);570571      // Create another token to be nested572      const newToken2 = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');573      // Try to nest inside a fungible token574      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionRFT, newToken)}, collectionRFT, newToken2, 1)), 'while nesting new token inside refungible').to.be.rejectedWith(/refungible\.RefungibleDisallowsNesting/);575    });576  });577578  it('ReFungible: disallows a non-Owner to nest someone else\'s token', async () => {579    await usingApi(async api => {580      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});581      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});582583      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);584      await enableAllowListExpectSuccess(alice, collectionNFT);585      await enablePublicMintingExpectSuccess(alice, collectionNFT);586587      // Create a token to attempt to be nested into588      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');589      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};590591      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});592593      // Try to create a nested token in the wrong collection594      await expect(executeTransaction(api, alice, api.tx.unique.createItem(595        collectionRFT,596        targetAddress,597        {ReFungible: {const_data: [], pieces: 100}},598      )), 'while creating a nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);599600      // Try to create and nest a token in the wrong collection601      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');602      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);603    });604  });605606  it('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {607    await usingApi(async api => {608      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});609      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);610      await enableAllowListExpectSuccess(alice, collectionNFT);611      await enablePublicMintingExpectSuccess(alice, collectionNFT);612613      // Create a token to attempt to be nested into614      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');615      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};616617      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});618      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionRFT]}});619620      // Try to create a nested token in the wrong collection621      await expect(executeTransaction(api, alice, api.tx.unique.createItem(622        collectionRFT,623        targetAddress,624        {ReFungible: {const_data: [], pieces: 100}},625      )), 'while creating a nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);626627      // Try to create and nest a token in the wrong collection628      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');629      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);630    });631  });632633  it('ReFungible: disallows to nest token to an unlisted collection', async () => {634    await usingApi(async api => {635      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});636      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[]}});637638      // Create a token to attempt to be nested into639      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');640      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};641642      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});643644      // Try to create a nested token in the wrong collection645      await expect(executeTransaction(api, alice, api.tx.unique.createItem(646        collectionRFT,647        targetAddress,648        {ReFungible: {const_data: [], pieces: 100}},649      )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);650651      // Try to create and nest a token in the wrong collection652      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');653      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);654    });655  });656});
after · tests/src/nesting/nest.test.ts
1import {expect} from 'chai';2import {tokenIdToAddress} from '../eth/util/helpers';3import privateKey from '../substrate/privateKey';4import usingApi, {executeTransaction} from '../substrate/substrate-api';5import {6  addToAllowListExpectSuccess,7  createCollectionExpectSuccess,8  createItemExpectSuccess,9  enableAllowListExpectSuccess,10  enablePublicMintingExpectSuccess,11  getTokenChildren,12  getTokenOwner,13  getTopmostTokenOwner,14  normalizeAccountId,15  setCollectionPermissionsExpectSuccess,16  transferExpectFailure,17  transferExpectSuccess,18  transferFromExpectSuccess,19} from '../util/helpers';20import {IKeyringPair} from '@polkadot/types/types';2122let alice: IKeyringPair;23let bob: IKeyringPair;2425describe('Integration Test: Nesting', () => {26  before(async () => {27    await usingApi(async () => {28      alice = privateKey('//Alice');29      bob = privateKey('//Bob');30    });31  });3233  it('Performs the full suite: bundles a token, transfers, and unnests', async () => {34    await usingApi(async api => {35      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});36      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});37      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');3839      // Create a nested token40      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});41      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});42      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});4344      // Create a token to be nested45      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');4647      // Nest48      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});49      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});50      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5152      // Move bundle to different user53      await transferExpectSuccess(collection, targetToken, alice, {Substrate: bob.address});54      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: bob.address});55      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});5657      // Unnest58      await transferFromExpectSuccess(collection, newToken, bob, {Ethereum: tokenIdToAddress(collection, targetToken)}, {Substrate: bob.address});59      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: bob.address});60    });61  });6263  it('Transfers an already bundled token', async () => {64    await usingApi(async api => {65      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});66      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});6768      const tokenA = await createItemExpectSuccess(alice, collection, 'NFT');69      const tokenB = await createItemExpectSuccess(alice, collection, 'NFT');7071      // Create a nested token72      const tokenC = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, tokenA)});73      expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});74      expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenA).toLowerCase()});7576      // Transfer the nested token to another token77      await expect(executeTransaction(78        api,79        alice,80        api.tx.unique.transferFrom(81          normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenA)}),82          normalizeAccountId({Ethereum: tokenIdToAddress(collection, tokenB)}),83          collection,84          tokenC,85          1,86        ),87      )).to.not.be.rejected;88      expect(await getTopmostTokenOwner(api, collection, tokenC)).to.be.deep.equal({Substrate: alice.address});89      expect(await getTokenOwner(api, collection, tokenC)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, tokenB).toLowerCase()});90    });91  });9293  it('Checks token children', async () => {94    await usingApi(async api => {95      const collectionA = await createCollectionExpectSuccess({mode: {type: 'NFT'}});96      await setCollectionPermissionsExpectSuccess(alice, collectionA, {nesting: 'Owner'});97      const collectionB = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});9899      const targetToken = await createItemExpectSuccess(alice, collectionA, 'NFT');100      const targetAddress = {Ethereum: tokenIdToAddress(collectionA, targetToken)};101      let children = await getTokenChildren(api, collectionA, targetToken);102      expect(children.length).to.be.equal(0, 'Children length check at creation');103104      // Create a nested NFT token105      const tokenA = await createItemExpectSuccess(alice, collectionA, 'NFT', targetAddress);106      children = await getTokenChildren(api, collectionA, targetToken);107      expect(children.length).to.be.equal(1, 'Children length check at nesting #1');108      expect(children).to.have.deep.members([109        {token: tokenA, collection: collectionA},110      ], 'Children contents check at nesting #1');111112      // Create then nest113      const tokenB = await createItemExpectSuccess(alice, collectionA, 'NFT');114      await transferExpectSuccess(collectionA, tokenB, alice, targetAddress);115      children = await getTokenChildren(api, collectionA, targetToken);116      expect(children.length).to.be.equal(2, 'Children length check at nesting #2');117      expect(children).to.have.deep.members([118        {token: tokenA, collection: collectionA},119        {token: tokenB, collection: collectionA},120      ], 'Children contents check at nesting #2');121122      // Move token B to a different user outside the nesting tree123      await transferExpectSuccess(collectionA, tokenB, alice, bob);124      children = await getTokenChildren(api, collectionA, targetToken);125      expect(children.length).to.be.equal(1, 'Children length check at unnesting');126      expect(children).to.be.have.deep.members([127        {token: tokenA, collection: collectionA},128      ], 'Children contents check at unnesting');129130      // Create a fungible token in another collection and then nest131      const tokenC = await createItemExpectSuccess(alice, collectionB, 'Fungible');132      await transferExpectSuccess(collectionB, tokenC, alice, targetAddress, 1, 'Fungible');133      children = await getTokenChildren(api, collectionA, targetToken);134      expect(children.length).to.be.equal(2, 'Children length check at nesting #3 (from another collection)');135      expect(children).to.be.have.deep.members([136        {token: tokenA, collection: collectionA},137        {token: tokenC, collection: collectionB},138      ], 'Children contents check at nesting #3 (from another collection)');139140      // Move the fungible token inside token A deeper in the nesting tree141      await transferFromExpectSuccess(collectionB, tokenC, alice, targetAddress, {Ethereum: tokenIdToAddress(collectionA, tokenA)}, 1, 'Fungible');142      children = await getTokenChildren(api, collectionA, targetToken);143      expect(children.length).to.be.equal(1, 'Children length check at deeper nesting');144      expect(children).to.be.have.deep.members([145        {token: tokenA, collection: collectionA},146      ], 'Children contents check at deeper nesting');147    });148  });149150  // ---------- Non-Fungible ----------151152  it('NFT: allows an Owner to nest/unnest their token', async () => {153    await usingApi(async api => {154      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});155      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});156      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');157158      // Create a nested token159      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});160      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});161      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});162163      // Create a token to be nested and nest164      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');165      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});166      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});167      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});168    });169  });170171  it('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {172    await usingApi(async api => {173      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});174      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[collection]}});175      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');176177      // Create a nested token178      const nestedToken = await createItemExpectSuccess(alice, collection, 'NFT', {Ethereum: tokenIdToAddress(collection, targetToken)});179      expect(await getTopmostTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Substrate: alice.address});180      expect(await getTokenOwner(api, collection, nestedToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});181182      // Create a token to be nested and nest183      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');184      await transferExpectSuccess(collection, newToken, alice, {Ethereum: tokenIdToAddress(collection, targetToken)});185      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});186      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Ethereum: tokenIdToAddress(collection, targetToken).toLowerCase()});187    });188  });189190  // ---------- Fungible ----------191192  it('Fungible: allows an Owner to nest/unnest their token', async () => {193    await usingApi(async api => {194      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});195      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});196      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});197      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};198199      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});200201      // Create a nested token202      await expect(executeTransaction(api, alice, api.tx.unique.createItem(203        collectionFT,204        targetAddress,205        {Fungible: {Value: 10}},206      ))).to.not.be.rejected;207208      // Nest a new token209      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');210      await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');211    });212  });213214  it('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {215    await usingApi(async api => {216      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});217      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});218      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};219220      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});221222      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted: [collectionFT]}});223224      // Create a nested token225      await expect(executeTransaction(api, alice, api.tx.unique.createItem(226        collectionFT,227        targetAddress,228        {Fungible: {Value: 10}},229      ))).to.not.be.rejected;230231      // Nest a new token232      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');233      await transferExpectSuccess(collectionFT, newToken, alice, targetAddress, 1, 'Fungible');234    });235  });236237  // ---------- Re-Fungible ----------238239  it('ReFungible: allows an Owner to nest/unnest their token', async () => {240    await usingApi(async api => {241      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});242      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});243      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});244      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};245246      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});247248      // Create a nested token249      await expect(executeTransaction(api, alice, api.tx.unique.createItem(250        collectionRFT,251        targetAddress,252        {ReFungible: {const_data: [], pieces: 100}},253      ))).to.not.be.rejected;254255      // Nest a new token256      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');257      await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');258    });259  });260261  it('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', async () => {262    await usingApi(async api => {263      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});264      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT', {Substrate: alice.address});265      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};266267      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});268269      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionRFT]}});270271      // Create a nested token272      await expect(executeTransaction(api, alice, api.tx.unique.createItem(273        collectionRFT,274        targetAddress,275        {ReFungible: {const_data: [], pieces: 100}},276      ))).to.not.be.rejected;277278      // Nest a new token279      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');280      await transferExpectSuccess(collectionRFT, newToken, alice, targetAddress, 100, 'ReFungible');281    });282  });283});284285describe('Negative Test: Nesting', async() => {286  before(async () => {287    await usingApi(async () => {288      alice = privateKey('//Alice');289      bob = privateKey('//Bob');290    });291  });292293  it('Disallows excessive token nesting', async () => {294    await usingApi(async api => {295      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});296      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});297      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');298299      const maxNestingLevel = 5;300      let prevToken = targetToken;301302      // Create a nested-token matryoshka303      for (let i = 0; i < maxNestingLevel; i++) {304        const nestedToken = await createItemExpectSuccess(305          alice,306          collection,307          'NFT',308          {Ethereum: tokenIdToAddress(collection, prevToken)},309        );310311        prevToken = nestedToken;312      }313314      // The nesting depth is limited by `maxNestingLevel`315      await expect(executeTransaction(api, alice, api.tx.unique.createItem(316        collection,317        {Ethereum: tokenIdToAddress(collection, prevToken)},318          {nft: {const_data: [], variable_data: []}} as any,319      )), 'while creating nested token').to.be.rejectedWith(/^structure\.DepthLimit$/);320321      expect(await getTopmostTokenOwner(api, collection, prevToken)).to.be.deep.equal({Substrate: alice.address});322    });323  });324325  // ---------- Non-Fungible ----------326327  it('NFT: disallows to nest token if nesting is disabled', async () => {328    await usingApi(async api => {329      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});330      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Disabled'});331      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');332333      // Try to create a nested token334      await expect(executeTransaction(api, alice, api.tx.unique.createItem(335        collection,336        {Ethereum: tokenIdToAddress(collection, targetToken)},337          {nft: {const_data: [], variable_data: []}} as any,338      )), 'while creating nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);339340      // Create a token to be nested341      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');342      // Try to nest343      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);344      expect(await getTopmostTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});345      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});346    });347  });348349  it('NFT: disallows a non-Owner to nest someone else\'s token', async () => {350    await usingApi(async api => {351      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});352      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: 'Owner'});353354      await addToAllowListExpectSuccess(alice, collection, bob.address);355      await enableAllowListExpectSuccess(alice, collection);356      await enablePublicMintingExpectSuccess(alice, collection);357358      // Create a token to attempt to be nested into359      const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');360361      // Try to create a nested token in the wrong collection362      await expect(executeTransaction(api, alice, api.tx.unique.createItem(363        collection,364        {Ethereum: tokenIdToAddress(collection, targetToken)},365          {nft: {const_data: [], variable_data: []}} as any,366      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);367368      // Try to create and nest a token in the wrong collection369      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');370      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);371      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});372    });373  });374375  it('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {376    await usingApi(async api => {377      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});378      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[collection]}});379380      await addToAllowListExpectSuccess(alice, collection, bob.address);381      await enableAllowListExpectSuccess(alice, collection);382      await enablePublicMintingExpectSuccess(alice, collection);383384      // Create a token to attempt to be nested into385      const targetToken = await createItemExpectSuccess(bob, collection, 'NFT');386387      // Try to create a nested token in the wrong collection388      await expect(executeTransaction(api, alice, api.tx.unique.createItem(389        collection,390        {Ethereum: tokenIdToAddress(collection, targetToken)},391          {nft: {const_data: [], variable_data: []}} as any,392      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);393394      // Try to create and nest a token in the wrong collection395      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');396      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.AddressNotInAllowlist/);397      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});398    });399  });400401  it('NFT: disallows to nest token in an unlisted collection', async () => {402    await usingApi(async api => {403      const collection = await createCollectionExpectSuccess({mode: {type: 'NFT'}});404      await setCollectionPermissionsExpectSuccess(alice, collection, {nesting: {OwnerRestricted:[]}});405406      // Create a token to attempt to be nested into407      const targetToken = await createItemExpectSuccess(alice, collection, 'NFT');408409      // Try to create a nested token in the wrong collection410      await expect(executeTransaction(api, alice, api.tx.unique.createItem(411        collection,412        {Ethereum: tokenIdToAddress(collection, targetToken)},413          {nft: {const_data: [], variable_data: []}} as any,414      )), 'while creating nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);415416      // Try to create and nest a token in the wrong collection417      const newToken = await createItemExpectSuccess(alice, collection, 'NFT');418      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collection, targetToken)}, collection, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);419      expect(await getTokenOwner(api, collection, newToken)).to.be.deep.equal({Substrate: alice.address});420    });421  });422423  // ---------- Fungible ----------424425  it('Fungible: disallows to nest token if nesting is disabled', async () => {426    await usingApi(async api => {427      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});428      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Disabled'});429      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');430      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};431432      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});433434      // Try to create a nested token435      await expect(executeTransaction(api, alice, api.tx.unique.createItem(436        collectionFT,437        targetAddress,438        {Fungible: {Value: 10}},439      )), 'while creating nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);440441      // Create a token to be nested442      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');443      // Try to nest444      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);445446      // Create another token to be nested447      const newToken2 = await createItemExpectSuccess(alice, collectionFT, 'Fungible');448      // Try to nest inside a fungible token449      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionFT, newToken)}, collectionFT, newToken2, 1)), 'while nesting new token inside fungible').to.be.rejectedWith(/fungible\.FungibleDisallowsNesting/);450    });451  });452453  it('Fungible: disallows a non-Owner to nest someone else\'s token', async () => {454    await usingApi(async api => {455      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});456      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});457458      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);459      await enableAllowListExpectSuccess(alice, collectionNFT);460      await enablePublicMintingExpectSuccess(alice, collectionNFT);461462      // Create a token to attempt to be nested into463      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');464      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};465466      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});467468      // Try to create a nested token in the wrong collection469      await expect(executeTransaction(api, alice, api.tx.unique.createItem(470        collectionFT,471        targetAddress,472        {Fungible: {Value: 10}},473      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);474475      // Try to create and nest a token in the wrong collection476      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');477      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);478    });479  });480481  it('Fungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {482    await usingApi(async api => {483      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});484      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);485      await enableAllowListExpectSuccess(alice, collectionNFT);486      await enablePublicMintingExpectSuccess(alice, collectionNFT);487488      // Create a token to attempt to be nested into489      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');490      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};491492      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});493      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionFT]}});494495      // Try to create a nested token in the wrong collection496      await expect(executeTransaction(api, alice, api.tx.unique.createItem(497        collectionFT,498        targetAddress,499        {Fungible: {Value: 10}},500      )), 'while creating nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);501502      // Try to create and nest a token in the wrong collection503      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');504      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);505    });506  });507508  it('Fungible: disallows to nest token in an unlisted collection', async () => {509    await usingApi(async api => {510      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});511      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[]}});512513      // Create a token to attempt to be nested into514      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');515      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};516517      const collectionFT = await createCollectionExpectSuccess({mode: {type: 'Fungible', decimalPoints: 0}});518519      // Try to create a nested token in the wrong collection520      await expect(executeTransaction(api, alice, api.tx.unique.createItem(521        collectionFT,522        targetAddress,523        {Fungible: {Value: 10}},524      )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);525526      // Try to create and nest a token in the wrong collection527      const newToken = await createItemExpectSuccess(alice, collectionFT, 'Fungible');528      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);529    });530  });531532  // ---------- Re-Fungible ----------533534  it('ReFungible: disallows to nest token if nesting is disabled', async () => {535    await usingApi(async api => {536      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});537      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Disabled'});538      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');539      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};540541      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});542543      // Create a nested token544      await expect(executeTransaction(api, alice, api.tx.unique.createItem(545        collectionRFT,546        targetAddress,547        {ReFungible: {const_data: [], pieces: 100}},548      )), 'while creating a nested token').to.be.rejectedWith(/^common\.NestingIsDisabled$/);549550      // Create a token to be nested551      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');552      // Try to nest553      await transferExpectFailure(collectionRFT, newToken, alice, targetAddress, 100);554      // Try to nest555      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.NestingIsDisabled/);556557      // Create another token to be nested558      const newToken2 = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');559      // Try to nest inside a fungible token560      await expect(executeTransaction(api, alice, api.tx.unique.transfer({Ethereum: tokenIdToAddress(collectionRFT, newToken)}, collectionRFT, newToken2, 1)), 'while nesting new token inside refungible').to.be.rejectedWith(/refungible\.RefungibleDisallowsNesting/);561    });562  });563564  it('ReFungible: disallows a non-Owner to nest someone else\'s token', async () => {565    await usingApi(async api => {566      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});567      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: 'Owner'});568569      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);570      await enableAllowListExpectSuccess(alice, collectionNFT);571      await enablePublicMintingExpectSuccess(alice, collectionNFT);572573      // Create a token to attempt to be nested into574      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');575      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};576577      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});578579      // Try to create a nested token in the wrong collection580      await expect(executeTransaction(api, alice, api.tx.unique.createItem(581        collectionRFT,582        targetAddress,583        {ReFungible: {const_data: [], pieces: 100}},584      )), 'while creating a nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);585586      // Try to create and nest a token in the wrong collection587      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');588      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);589    });590  });591592  it('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async () => {593    await usingApi(async api => {594      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});595      await addToAllowListExpectSuccess(alice, collectionNFT, bob.address);596      await enableAllowListExpectSuccess(alice, collectionNFT);597      await enablePublicMintingExpectSuccess(alice, collectionNFT);598599      // Create a token to attempt to be nested into600      const targetToken = await createItemExpectSuccess(bob, collectionNFT, 'NFT');601      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};602603      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});604      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[collectionRFT]}});605606      // Try to create a nested token in the wrong collection607      await expect(executeTransaction(api, alice, api.tx.unique.createItem(608        collectionRFT,609        targetAddress,610        {ReFungible: {const_data: [], pieces: 100}},611      )), 'while creating a nested token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);612613      // Try to create and nest a token in the wrong collection614      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');615      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.OnlyOwnerAllowedToNest/);616    });617  });618619  it('ReFungible: disallows to nest token to an unlisted collection', async () => {620    await usingApi(async api => {621      const collectionNFT = await createCollectionExpectSuccess({mode: {type: 'NFT'}});622      await setCollectionPermissionsExpectSuccess(alice, collectionNFT, {nesting: {OwnerRestricted:[]}});623624      // Create a token to attempt to be nested into625      const targetToken = await createItemExpectSuccess(alice, collectionNFT, 'NFT');626      const targetAddress = {Ethereum: tokenIdToAddress(collectionNFT, targetToken)};627628      const collectionRFT = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});629630      // Try to create a nested token in the wrong collection631      await expect(executeTransaction(api, alice, api.tx.unique.createItem(632        collectionRFT,633        targetAddress,634        {ReFungible: {const_data: [], pieces: 100}},635      )), 'while creating a nested token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);636637      // Try to create and nest a token in the wrong collection638      const newToken = await createItemExpectSuccess(alice, collectionRFT, 'ReFungible');639      await expect(executeTransaction(api, alice, api.tx.unique.transfer(targetAddress, collectionRFT, newToken, 1)), 'while nesting new token').to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);640    });641  });642});