git.delta.rocks / unique-network / refs/commits / 017086424d43

difftreelog

source

tests/src/nesting/nest.test.ts42.2 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, Pallets, usingPlaygrounds} from '../util';19import {UniqueNFToken, UniqueRFToken} from '../util/playgrounds/unique';2021describe('Integration Test: Composite nesting tests', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;2425  before(async () => {26    await usingPlaygrounds(async (helper, privateKey) => {27      const donor = await privateKey({filename: __filename});28      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29    });30  });3132  itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {33    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});34    const targetToken = await collection.mintToken(alice);3536    // Create an immediately nested token37    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());38    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});39    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4041    // Create a token to be nested42    const newToken = await collection.mintToken(alice);4344    // Nest45    await newToken.nest(alice, targetToken);46    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});47    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4849    // Move bundle to different user50    await targetToken.transfer(alice, {Substrate: bob.address});51    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});52    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());53    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});54    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());5556    // Unnest57    await newToken.unnest(bob, targetToken, {Substrate: bob.address});58    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});59    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});60  });6162  itSub('Transfers an already bundled token', async ({helper}) => {63    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});64    const tokenA = await collection.mintToken(alice);65    const tokenB = await collection.mintToken(alice);6667    // Create a nested token68    const tokenC = await collection.mintToken(alice, tokenA.nestingAccount());69    expect(await tokenC.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());7071    // Transfer the nested token to another token72    await expect(tokenC.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount())).to.be.fulfilled;73    expect(await tokenC.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});74    expect(await tokenC.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());75  });7677  itSub('Checks token children', async ({helper}) => {78    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});79    const collectionB = await helper.ft.mintCollection(alice);8081    const targetToken = await collectionA.mintToken(alice);82    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');8384    // Create a nested NFT token85    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());86    expect(await targetToken.getChildren()).to.have.deep.members([87      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},88    ], 'Children contents check at nesting #1').and.be.length(1, 'Children length check at nesting #1');8990    // Create then nest91    const tokenB = await collectionA.mintToken(alice);92    await tokenB.nest(alice, targetToken);93    expect(await targetToken.getChildren()).to.have.deep.members([94      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},95      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},96    ], 'Children contents check at nesting #2').and.be.length(2, 'Children length check at nesting #2');9798    // Move token B to a different user outside the nesting tree99    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});100    expect(await targetToken.getChildren()).to.be.have.deep.members([101      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},102    ], 'Children contents check at nesting #3 (unnesting)').and.be.length(1, 'Children length check at nesting #3 (unnesting)');103104    // Create a fungible token in another collection and then nest105    await collectionB.mint(alice, 10n);106    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);107    expect(await targetToken.getChildren()).to.be.have.deep.members([108      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},109      {tokenId: 0, collectionId: collectionB.collectionId},110    ], 'Children contents check at nesting #4 (from another collection)')111      .and.be.length(2, 'Children length check at nesting #4 (from another collection)');112113    // Move part of the fungible token inside token A deeper in the nesting tree114    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);115    expect(await targetToken.getChildren()).to.be.have.deep.members([116      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},117      {tokenId: 0, collectionId: collectionB.collectionId},118    ], 'Children contents check at nesting #5 (deeper)').and.be.length(2, 'Children length check at nesting #5 (deeper)');119    expect(await tokenA.getChildren()).to.be.have.deep.members([120      {tokenId: 0, collectionId: collectionB.collectionId},121    ], 'Children contents check at nesting #5.5 (deeper)').and.be.length(1, 'Children length check at nesting #5.5 (deeper)');122123    // Move the remaining part of the fungible token inside token A deeper in the nesting tree124    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);125    expect(await targetToken.getChildren()).to.be.have.deep.members([126      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},127    ], 'Children contents check at nesting #6 (deeper)').and.be.length(1, 'Children length check at nesting #6 (deeper)');128    expect(await tokenA.getChildren()).to.be.have.deep.members([129      {tokenId: 0, collectionId: collectionB.collectionId},130    ], 'Children contents check at nesting #6.5 (deeper)').and.be.length(1, 'Children length check at nesting #6.5 (deeper)');131  });132});133134describe('Integration Test: Various token type nesting', () => {135  let alice: IKeyringPair;136  let bob: IKeyringPair;137  let charlie: IKeyringPair;138139  before(async () => {140    await usingPlaygrounds(async (helper, privateKey) => {141      const donor = await privateKey({filename: __filename});142      [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 20n, 20n], donor);143    });144  });145146  itSub('Admin (NFT): allows an Admin to nest a token', async ({helper}) => {147    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true}}});148    await collection.addAdmin(alice, {Substrate: bob.address});149    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});150151    // Create an immediately nested token152    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());153    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});154    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());155156    // Create a token to be nested and nest157    const newToken = await collection.mintToken(bob);158    await newToken.nest(bob, targetToken);159    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});160    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());161  });162163  itSub('Admin (NFT): Admin and Token Owner can operate together', async ({helper}) => {164    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});165    await collection.addAdmin(alice, {Substrate: bob.address});166    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});167168    // Create an immediately nested token by an administrator169    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());170    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});171    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());172173    // Create a token to be nested and nest174    const newToken = await collection.mintToken(alice, {Substrate: charlie.address});175    await newToken.nest(charlie, targetToken);176    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});177    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());178  });179180  itSub('Admin (NFT): allows an Admin to nest a token (Restricted nesting)', async ({helper}) => {181    const collectionA = await helper.nft.mintCollection(alice);182    await collectionA.addAdmin(alice, {Substrate: bob.address});183    const collectionB = await helper.nft.mintCollection(alice);184    await collectionB.addAdmin(alice, {Substrate: bob.address});185    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted:[collectionB.collectionId]}});186    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});187188    // Create an immediately nested token189    const nestedToken = await collectionB.mintToken(bob, targetToken.nestingAccount());190    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});191    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());192193    // Create a token to be nested and nest194    const newToken = await collectionB.mintToken(bob);195    await newToken.nest(bob, targetToken);196    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});197    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());198  });199200  // ---------- Non-Fungible ----------201202  itSub('NFT: allows an Owner to nest/unnest their token', async ({helper}) => {203    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});204    await collection.addToAllowList(alice, {Substrate: charlie.address});205    const targetToken = await collection.mintToken(charlie);206    await collection.addToAllowList(alice, targetToken.nestingAccount());207208    // Create an immediately nested token209    const nestedToken = await collection.mintToken(charlie, targetToken.nestingAccount());210    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});211    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());212213    // Create a token to be nested and nest214    const newToken = await collection.mintToken(charlie);215    await newToken.nest(charlie, targetToken);216    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});217    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());218  });219220  itSub('NFT: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {221    const collectionA = await helper.nft.mintCollection(alice);222    const collectionB = await helper.nft.mintCollection(alice);223    //await collectionB.addAdmin(alice, {Substrate: bob.address});224    const targetToken = await collectionA.mintToken(alice, {Substrate: charlie.address});225226    await collectionA.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionB.collectionId]}});227    await collectionA.addToAllowList(alice, {Substrate: charlie.address});228    await collectionA.addToAllowList(alice, targetToken.nestingAccount());229230    await collectionB.setPermissions(alice, {access: 'AllowList', mintMode: true});231    await collectionB.addToAllowList(alice, {Substrate: charlie.address});232    await collectionB.addToAllowList(alice, targetToken.nestingAccount());233234    // Create an immediately nested token235    const nestedToken = await collectionB.mintToken(charlie, targetToken.nestingAccount());236    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});237    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());238239    // Create a token to be nested and nest240    const newToken = await collectionB.mintToken(charlie);241    await newToken.nest(charlie, targetToken);242    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});243    expect(await newToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());244  });245246  // ---------- Fungible ----------247248  itSub('Fungible: allows an Owner to nest/unnest their token', async ({helper}) => {249    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});250    const collectionFT = await helper.ft.mintCollection(alice);251    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});252253    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});254    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());255256    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});257    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});258    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());259260    // Create an immediately nested token261    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());262    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);263264    // Create a token to be nested and nest265    await collectionFT.mint(charlie, 5n);266    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);267    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);268  });269270  itSub('Fungible: allows an Owner to nest/unnest their token (Restricted nesting)', async ({helper}) => {271    const collectionNFT = await helper.nft.mintCollection(alice);272    const collectionFT = await helper.ft.mintCollection(alice);273    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});274275    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionFT.collectionId]}});276    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});277    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());278279    await collectionFT.setPermissions(alice, {access: 'AllowList', mintMode: true});280    await collectionFT.addToAllowList(alice, {Substrate: charlie.address});281    await collectionFT.addToAllowList(alice, targetToken.nestingAccount());282283    // Create an immediately nested token284    await collectionFT.mint(charlie, 5n, targetToken.nestingAccount());285    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);286287    // Create a token to be nested and nest288    await collectionFT.mint(charlie, 5n);289    await collectionFT.transfer(charlie, targetToken.nestingAccount(), 2n);290    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(7n);291  });292293  // ---------- Re-Fungible ----------294295  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token', [Pallets.ReFungible], async ({helper}) => {296    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}}});297    const collectionRFT = await helper.rft.mintCollection(alice);298    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});299300    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});301    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());302303    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});304    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});305    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());306307    // Create an immediately nested token308    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());309    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);310311    // Create a token to be nested and nest312    const newToken = await collectionRFT.mintToken(charlie, 5n);313    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);314    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);315  });316317  itSub.ifWithPallets('ReFungible: allows an Owner to nest/unnest their token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {318    const collectionNFT = await helper.nft.mintCollection(alice);319    const collectionRFT = await helper.rft.mintCollection(alice);320    const targetToken = await collectionNFT.mintToken(alice, {Substrate: charlie.address});321322    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted:[collectionRFT.collectionId]}});323    await collectionNFT.addToAllowList(alice, {Substrate: charlie.address});324    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());325326    await collectionRFT.setPermissions(alice, {access: 'AllowList', mintMode: true});327    await collectionRFT.addToAllowList(alice, {Substrate: charlie.address});328    await collectionRFT.addToAllowList(alice, targetToken.nestingAccount());329330    // Create an immediately nested token331    const nestedToken = await collectionRFT.mintToken(charlie, 5n, targetToken.nestingAccount());332    expect(await nestedToken.getBalance(targetToken.nestingAccount())).to.be.equal(5n);333334    // Create a token to be nested and nest335    const newToken = await collectionRFT.mintToken(charlie, 5n);336    await newToken.transfer(charlie, targetToken.nestingAccount(), 2n);337    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(2n);338  });339340  async function checkNestedRft({341    expectedBalance,342    childrenShouldPresent,343    nestedRft,344    targetNft,345  }: {346    expectedBalance: bigint,347    childrenShouldPresent: boolean,348    nestedRft: UniqueRFToken,349    targetNft: UniqueNFToken,350  }) {351    const balance = await nestedRft.getBalance(targetNft.nestingAccount());352    expect(balance).to.be.equal(expectedBalance);353354    const children = await targetNft.getChildren();355356    if (childrenShouldPresent) {357      expect(children[0]).to.be.deep.equal({358        collectionId: nestedRft.collectionId,359        tokenId: nestedRft.tokenId,360      });361    } else {362      expect(children.length).to.be.equal(0);363    }364  }365366  itSub.ifWithPallets('ReFungible: allows a collection owner to transfer nested token', [Pallets.ReFungible], async ({helper}) => {367    const collectionNFT = await helper.nft.mintCollection(alice);368    const collectionRFT = await helper.rft.mintCollection(alice, {369      limits: {370        ownerCanTransfer: true,371      },372    });373374    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});375376    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});377    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});378379    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);380    await checkNestedRft({381      expectedBalance: 5n,382      childrenShouldPresent: true,383      nestedRft,384      targetNft,385    });386387    await nestedRft.transferFrom(alice, targetNft.nestingAccount(), {Substrate: bob.address}, 2n);388    await checkNestedRft({389      expectedBalance: 3n,390      childrenShouldPresent: true,391      nestedRft,392      targetNft,393    });394    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(2n);395396    await nestedRft.transferFrom(alice, targetNft.nestingAccount(), {Substrate: bob.address}, 3n);397    await checkNestedRft({398      expectedBalance: 0n,399      childrenShouldPresent: false,400      nestedRft,401      targetNft,402    });403    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(5n);404  });405406  itSub.ifWithPallets('ReFungible: allows a collection admin to transfer nested token', [Pallets.ReFungible], async ({helper}) => {407    const collectionNFT = await helper.nft.mintCollection(alice);408    const collectionRFT = await helper.rft.mintCollection(alice, {409      limits: {410        ownerCanTransfer: true,411      },412    });413    await collectionRFT.addAdmin(alice, {Substrate: bob.address});414415    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});416417    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});418    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});419420    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);421    await checkNestedRft({422      expectedBalance: 5n,423      childrenShouldPresent: true,424      nestedRft,425      targetNft,426    });427428    await nestedRft.transferFrom(bob, targetNft.nestingAccount(), {Substrate: bob.address}, 2n);429    await checkNestedRft({430      expectedBalance: 3n,431      childrenShouldPresent: true,432      nestedRft,433      targetNft,434    });435    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(2n);436437    await nestedRft.transferFrom(bob, targetNft.nestingAccount(), {Substrate: bob.address}, 3n);438    await checkNestedRft({439      expectedBalance: 0n,440      childrenShouldPresent: false,441      nestedRft,442      targetNft,443    });444    expect(await nestedRft.getBalance({Substrate: bob.address})).to.be.equal(5n);445  });446447  itSub.ifWithPallets('ReFungible: allows a collection owner to burn nested token', [Pallets.ReFungible], async ({helper}) => {448    const collectionNFT = await helper.nft.mintCollection(alice, {449      limits: {450        ownerCanTransfer: true,451      },452    });453    const collectionRFT = await helper.rft.mintCollection(alice, {454      limits: {455        ownerCanTransfer: true,456      },457    });458459    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});460461    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});462    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});463464    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);465    await checkNestedRft({466      expectedBalance: 5n,467      childrenShouldPresent: true,468      nestedRft,469      targetNft,470    });471472    await nestedRft.burnFrom(alice, targetNft.nestingAccount(), 2n);473    await checkNestedRft({474      expectedBalance: 3n,475      childrenShouldPresent: true,476      nestedRft,477      targetNft,478    });479480    await nestedRft.burnFrom(alice, targetNft.nestingAccount(), 3n);481    await checkNestedRft({482      expectedBalance: 0n,483      childrenShouldPresent: false,484      nestedRft,485      targetNft,486    });487488    // Check if we can burn target NFT when all nested RFTs are gone489    await targetNft.burnFrom(alice, {Substrate: charlie.address});490  });491492  itSub.ifWithPallets('ReFungible: allows a collection admin to burn nested token', [Pallets.ReFungible], async ({helper}) => {493    const collectionNFT = await helper.nft.mintCollection(alice, {494      limits: {495        ownerCanTransfer: true,496      },497    });498    const collectionRFT = await helper.rft.mintCollection(alice, {499      limits: {500        ownerCanTransfer: true,501      },502    });503    await collectionNFT.addAdmin(alice, {Substrate: bob.address});504    await collectionRFT.addAdmin(alice, {Substrate: bob.address});505506    await collectionNFT.setPermissions(alice, {nesting: {tokenOwner: true}});507508    const targetNft = await collectionNFT.mintToken(alice, {Substrate: charlie.address});509    const nestedRft = await collectionRFT.mintToken(alice, 5n, {Substrate: charlie.address});510511    await nestedRft.transfer(charlie, targetNft.nestingAccount(), 5n);512    await checkNestedRft({513      expectedBalance: 5n,514      childrenShouldPresent: true,515      nestedRft,516      targetNft,517    });518519    await nestedRft.burnFrom(bob, targetNft.nestingAccount(), 2n);520    await checkNestedRft({521      expectedBalance: 3n,522      childrenShouldPresent: true,523      nestedRft,524      targetNft,525    });526527    await nestedRft.burnFrom(bob, targetNft.nestingAccount(), 3n);528    await checkNestedRft({529      expectedBalance: 0n,530      childrenShouldPresent: false,531      nestedRft,532      targetNft,533    });534535    // Check if we can burn target NFT when all nested RFTs are gone536    await targetNft.burnFrom(bob, {Substrate: charlie.address});537  });538});539540describe('Negative Test: Nesting', () => {541  let alice: IKeyringPair;542  let bob: IKeyringPair;543544  before(async () => {545    await usingPlaygrounds(async (helper, privateKey) => {546      const donor = await privateKey({filename: __filename});547      [alice, bob] = await helper.arrange.createAccounts([100n, 50n], donor);548    });549  });550551  itSub('Disallows excessive token nesting', async ({helper}) => {552    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});553    let token = await collection.mintToken(alice);554555    const maxNestingLevel = 5;556557    // Create a nested-token matryoshka558    for (let i = 0; i < maxNestingLevel; i++) {559      token = await collection.mintToken(alice, token.nestingAccount());560    }561562    // The nesting depth is limited by `maxNestingLevel`563    await expect(collection.mintToken(alice, token.nestingAccount()))564      .to.be.rejectedWith(/structure\.DepthLimit/);565    expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});566    expect(await token.getChildren()).to.be.length(0);567  });568569  // ---------- Admin ------------570571  itSub('Admin (NFT): disallows an Admin to operate nesting when only TokenOwner is allowed', async ({helper}) => {572    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});573    await collection.addAdmin(alice, {Substrate: bob.address});574    const targetToken = await collection.mintToken(alice);575576    // Try to create an immediately nested token as collection admin when it's disallowed577    await expect(collection.mintToken(bob, targetToken.nestingAccount()))578      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);579580    // Try to create a token to be nested and nest581    const newToken = await collection.mintToken(bob);582    await expect(newToken.nest(bob, targetToken))583      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);584585    expect(await targetToken.getChildren()).to.be.length(0);586    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});587  });588589  itSub('Admin (NFT): disallows a Token Owner to operate nesting when only Admin is allowed', async ({helper}) => {590    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});591    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});592    await collection.addToAllowList(alice, {Substrate: bob.address});593    await collection.addToAllowList(alice, targetToken.nestingAccount());594595    // Try to create a nested token as token owner when it's disallowed596    await expect(collection.mintToken(bob, targetToken.nestingAccount()))597      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);598599    // Try to create a token to be nested and nest600    const newToken = await collection.mintToken(bob);601    await expect(newToken.nest(bob, targetToken))602      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);603604    expect(await targetToken.getChildren()).to.be.length(0);605    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});606  });607608  itSub('Admin (NFT): disallows an Admin to unnest someone else\'s token', async ({helper}) => {609    const collection = await helper.nft.mintCollection(alice, {limits: {ownerCanTransfer: true}, permissions: {access: 'AllowList', mintMode: true, nesting: {collectionAdmin: true}}});610    //await collection.addAdmin(alice, {Substrate: bob.address});611    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});612    await collection.addToAllowList(alice, {Substrate: bob.address});613    await collection.addToAllowList(alice, targetToken.nestingAccount());614615    // Try to nest somebody else's token616    const newToken = await collection.mintToken(bob);617    await expect(newToken.nest(alice, targetToken))618      .to.be.rejectedWith(/common\.NoPermission/);619620    // Try to unnest a token belonging to someone else as collection admin621    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());622    await expect(nestedToken.unnest(alice, targetToken, {Substrate: bob.address}))623      .to.be.rejectedWith(/common\.AddressNotInAllowlist/);624625    expect(await targetToken.getChildren()).to.be.length(1);626    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});627    expect(await nestedToken.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());628  });629630  itSub('Admin (NFT): disallows an Admin to nest a token from an unlisted collection (Restricted nesting)', async ({helper}) => {631    const collectionA = await helper.nft.mintCollection(alice);632    const collectionB = await helper.nft.mintCollection(alice);633    await collectionA.setPermissions(alice, {nesting: {collectionAdmin: true, restricted: [collectionA.collectionId]}});634    const targetToken = await collectionA.mintToken(alice);635636    // Try to create a nested token from another collection637    await expect(collectionB.mintToken(alice, targetToken.nestingAccount()))638      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);639640    // Create a token in another collection yet to be nested and try to nest641    const newToken = await collectionB.mintToken(alice);642    await expect(newToken.nest(alice, targetToken))643      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);644645    expect(await targetToken.getChildren()).to.be.length(0);646    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});647  });648649  // ---------- Non-Fungible ----------650651  itSub('NFT: disallows to nest token if nesting is disabled', async ({helper}) => {652    // Collection is implicitly not allowed nesting at creation653    const collection = await helper.nft.mintCollection(alice);654    const targetToken = await collection.mintToken(alice);655656    // Try to create a nested token as token owner when it's disallowed657    await expect(collection.mintToken(alice, targetToken.nestingAccount()))658      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);659660    // Try to create a token to be nested and nest661    const newToken = await collection.mintToken(alice);662    await expect(newToken.nest(alice, targetToken))663      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);664665    expect(await targetToken.getChildren()).to.be.length(0);666    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});667  });668669  itSub('NFT: disallows a non-Owner to nest someone else\'s token', async ({helper}) => {670    const collection = await helper.nft.mintCollection(alice);671    const targetToken = await collection.mintToken(alice);672673    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});674    await collection.addToAllowList(alice, {Substrate: bob.address});675    await collection.addToAllowList(alice, targetToken.nestingAccount());676677    // Try to create a token to be nested and nest678    const newToken = await collection.mintToken(alice);679    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);680681    expect(await targetToken.getChildren()).to.be.length(0);682    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});683  });684685  itSub('NFT: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', async ({helper}) => {686    const collection = await helper.nft.mintCollection(alice);687    const targetToken = await collection.mintToken(alice);688689    await collection.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});690    await collection.addToAllowList(alice, {Substrate: bob.address});691    await collection.addToAllowList(alice, targetToken.nestingAccount());692693    const collectionB = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true}});694    await collectionB.addToAllowList(alice, {Substrate: bob.address});695    await collectionB.addToAllowList(alice, targetToken.nestingAccount());696697    // Try to create a token to be nested and nest698    const newToken = await collectionB.mintToken(alice);699    await expect(newToken.nest(bob, targetToken)).to.be.rejectedWith(/common\.NoPermission/);700701    expect(await targetToken.getChildren()).to.be.length(0);702    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: alice.address});703  });704705  itSub('NFT: disallows to nest token in an unlisted collection', async ({helper}) => {706    // Create collection with restricted nesting -- even self is not allowed707    const collection = await helper.nft.mintCollection(alice, {permissions: {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: []}}});708    const targetToken = await collection.mintToken(alice, {Substrate: bob.address});709710    await collection.addToAllowList(alice, {Substrate: bob.address});711    await collection.addToAllowList(alice, targetToken.nestingAccount());712713    // Try to mint in own collection after allowlisting the accounts714    await expect(collection.mintToken(bob, targetToken.nestingAccount()))715      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);716  });717718  // ---------- Fungible ----------719720  itSub('Fungible: disallows to nest token if nesting is disabled', async ({helper}) => {721    const collectionNFT = await helper.nft.mintCollection(alice);722    const collectionFT = await helper.ft.mintCollection(alice);723    const targetToken = await collectionNFT.mintToken(alice);724725    // Try to create an immediately nested token726    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))727      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);728729    // Try to create a token to be nested and nest730    await collectionFT.mint(alice, 5n);731    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 2n))732      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);733    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);734  });735736  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token', async ({helper}) => {737    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});738    const collectionFT = await helper.ft.mintCollection(alice);739    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});740741    // Nest some tokens as Alice into Bob's token742    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());743744    // Try to pull it out745    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))746      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);747    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);748  });749750  itSub('Fungible: disallows a non-Owner to unnest someone else\'s token (Restricted nesting)', async ({helper}) => {751    const collectionNFT = await helper.nft.mintCollection(alice);752    const collectionFT = await helper.ft.mintCollection(alice);753    const targetToken = await collectionNFT.mintToken(alice, {Substrate: bob.address});754755    await collectionNFT.setPermissions(alice, {nesting: {collectionAdmin: true, tokenOwner: true, restricted: [collectionFT.collectionId]}});756757    // Nest some tokens as Alice into Bob's token758    await collectionFT.mint(alice, 5n, targetToken.nestingAccount());759760    // Try to pull it out as Alice still761    await expect(collectionFT.transferFrom(alice, targetToken.nestingAccount(), {Substrate: bob.address}, 1n))762      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);763    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(5n);764  });765766  itSub('Fungible: disallows to nest token in an unlisted collection', async ({helper}) => {767    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true, restricted: []}}});768    const collectionFT = await helper.ft.mintCollection(alice);769    const targetToken = await collectionNFT.mintToken(alice);770771    // Try to mint an immediately nested token772    await expect(collectionFT.mint(alice, 5n, targetToken.nestingAccount()))773      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);774775    // Mint a token and try to nest it776    await collectionFT.mint(alice, 5n);777    await expect(collectionFT.transfer(alice, targetToken.nestingAccount(), 1n))778      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);779780    expect(await collectionFT.getBalance(targetToken.nestingAccount())).to.be.equal(0n);781    expect(await collectionFT.getBalance({Substrate: alice.address})).to.be.equal(5n);782  });783784  // ---------- Re-Fungible ----------785786  itSub.ifWithPallets('ReFungible: disallows to nest token if nesting is disabled', [Pallets.ReFungible], async ({helper}) => {787    const collectionNFT = await helper.nft.mintCollection(alice);788    const collectionRFT = await helper.rft.mintCollection(alice);789    const targetToken = await collectionNFT.mintToken(alice);790791    // Try to create an immediately nested token792    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))793      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);794795    // Try to create a token to be nested and nest796    const token = await collectionRFT.mintToken(alice, 5n);797    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))798      .to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);799    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);800  });801802  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token', [Pallets.ReFungible], async ({helper}) => {803    const collectionNFT = await helper.nft.mintCollection(alice);804    const collectionRFT = await helper.rft.mintCollection(alice);805    const targetToken = await collectionNFT.mintToken(alice);806807    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true}});808    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});809    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());810811    // Try to create a token to be nested and nest812    const newToken = await collectionRFT.mintToken(alice);813    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);814815    expect(await targetToken.getChildren()).to.be.length(0);816    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);817818    // Nest some tokens as Alice into Bob's token819    await newToken.transfer(alice, targetToken.nestingAccount());820821    // Try to pull it out822    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))823      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);824    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);825  });826827  itSub.ifWithPallets('ReFungible: disallows a non-Owner to nest someone else\'s token (Restricted nesting)', [Pallets.ReFungible], async ({helper}) => {828    const collectionNFT = await helper.nft.mintCollection(alice);829    const collectionRFT = await helper.rft.mintCollection(alice);830    const targetToken = await collectionNFT.mintToken(alice);831832    await collectionNFT.setPermissions(alice, {access: 'AllowList', mintMode: true, nesting: {tokenOwner: true, restricted: [collectionRFT.collectionId]}});833    await collectionNFT.addToAllowList(alice, {Substrate: bob.address});834    await collectionNFT.addToAllowList(alice, targetToken.nestingAccount());835836    // Try to create a token to be nested and nest837    const newToken = await collectionRFT.mintToken(alice);838    await expect(newToken.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith(/common\.TokenValueTooLow/);839840    expect(await targetToken.getChildren()).to.be.length(0);841    expect(await newToken.getBalance({Substrate: alice.address})).to.be.equal(1n);842843    // Nest some tokens as Alice into Bob's token844    await newToken.transfer(alice, targetToken.nestingAccount());845846    // Try to pull it out847    await expect(newToken.transferFrom(bob, targetToken.nestingAccount(), {Substrate: alice.address}, 1n))848      .to.be.rejectedWith(/common\.ApprovedValueTooLow/);849    expect(await newToken.getBalance(targetToken.nestingAccount())).to.be.equal(1n);850  });851852  itSub.ifWithPallets('ReFungible: disallows to nest token to an unlisted collection', [Pallets.ReFungible], async ({helper}) => {853    const collectionNFT = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true, restricted: []}}});854    const collectionRFT = await helper.rft.mintCollection(alice);855    const targetToken = await collectionNFT.mintToken(alice);856857    // Try to create an immediately nested token858    await expect(collectionRFT.mintToken(alice, 5n, targetToken.nestingAccount()))859      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);860861    // Try to create a token to be nested and nest862    const token = await collectionRFT.mintToken(alice, 5n);863    await expect(token.transfer(alice, targetToken.nestingAccount(), 2n))864      .to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);865    expect(await token.getBalance({Substrate: alice.address})).to.be.equal(5n);866  });867});