git.delta.rocks / unique-network / refs/commits / 0ceea0af66a9

difftreelog

source

js-packages/tests/sub/nesting/e2e.test.ts7.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 type {IKeyringPair} from '@polkadot/types/types';18import {expect, itSub, usingPlaygrounds} from '@unique/test-utils/util.js';19import {CrossAccountId} from '@unique-nft/playgrounds/unique.js';2021describe('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({url: import.meta.url});28      [alice, bob] = await helper.arrange.createAccounts([50n, 10n], donor);29    });30  });3132  itSub('Checks token children e2e', async ({helper}) => {33    const collectionA = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});34    const collectionB = await helper.ft.mintCollection(alice);35    const collectionC = await helper.rft.mintCollection(alice);36    const collectionNative = helper.ft.getCollectionObject(0);3738    const targetToken = await collectionA.mintToken(alice);39    expect((await targetToken.getChildren()).length).to.be.equal(0, 'Children length check at creation');4041    // Create a nested NFT token42    const tokenA = await collectionA.mintToken(alice, targetToken.nestingAccount());43    expect(await targetToken.getChildren()).to.have.deep.members([44      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},45    ]).and.has.length(1);4647    // Create then nest48    const tokenB = await collectionA.mintToken(alice);49    await tokenB.nest(alice, targetToken);50    expect(await targetToken.getChildren()).to.have.deep.members([51      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},52      {tokenId: tokenB.tokenId, collectionId: collectionA.collectionId},53    ]).and.has.length(2);5455    // Move token B to a different user outside the nesting tree56    await tokenB.unnest(alice, targetToken, {Substrate: bob.address});57    expect(await targetToken.getChildren()).to.have.deep.members([58      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},59    ]).and.has.length(1);6061    // Create a fungible token in another collection and then nest62    await collectionB.mint(alice, 10n);63    await collectionB.transfer(alice, targetToken.nestingAccount(), 2n);64    expect(await targetToken.getChildren()).to.have.deep.members([65      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},66      {tokenId: 0, collectionId: collectionB.collectionId},67    ]).and.has.length(2);6869    // Create a refungible token in another collection and then nest70    const tokenC = await collectionC.mintToken(alice, 10n);71    await tokenC.transfer(alice, targetToken.nestingAccount(), 2n);72    expect(await targetToken.getChildren()).to.have.deep.members([73      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},74      {tokenId: 0, collectionId: collectionB.collectionId},75      {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},76    ]).and.has.length(3);7778    // Nest native fungible token into another collection79    await collectionNative.transfer(alice, targetToken.nestingAccount(), 2n);80    expect(await targetToken.getChildren()).to.have.deep.members([81      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},82      {tokenId: 0, collectionId: collectionB.collectionId},83      {tokenId: tokenC.tokenId, collectionId: collectionC.collectionId},84    ]).and.has.length(3);8586    // Burn all nested pieces87    await tokenC.burnFrom(alice, targetToken.nestingAccount(), 2n);88    expect(await targetToken.getChildren()).to.have.deep.members([89      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},90      {tokenId: 0, collectionId: collectionB.collectionId},91    ])92      .and.has.length(2);9394    // Move part of the fungible token inside token A deeper in the nesting tree95    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);96    expect(await targetToken.getChildren()).to.be.have.deep.members([97      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},98      {tokenId: 0, collectionId: collectionB.collectionId},99    ]).and.has.length(2);100    // Nested token also has children now:101    expect(await tokenA.getChildren()).to.have.deep.members([102      {tokenId: 0, collectionId: collectionB.collectionId},103    ]).and.has.length(1);104105    // Move the remaining part of the fungible token inside token A deeper in the nesting tree106    await collectionB.transferFrom(alice, targetToken.nestingAccount(), tokenA.nestingAccount(), 1n);107    expect(await targetToken.getChildren()).to.have.deep.members([108      {tokenId: tokenA.tokenId, collectionId: collectionA.collectionId},109    ]).and.has.length(1);110    expect(await tokenA.getChildren()).to.have.deep.members([111      {tokenId: 0, collectionId: collectionB.collectionId},112    ]).and.has.length(1);113  });114115  /// TODO review this test116  itSub('Performs the full suite: bundles a token, transfers, and unnests', async ({helper}) => {117    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});118    const targetToken = await collection.mintToken(alice);119120    // Create an immediately nested token121    const nestedToken = await collection.mintToken(alice, targetToken.nestingAccount());122    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});123    expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));124125    // Create a token to be nested126    const newToken = await collection.mintToken(alice);127128    // Nest129    await newToken.nest(alice, targetToken);130    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});131    expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));132133    // Move bundle to different user134    await targetToken.transfer(alice, {Substrate: bob.address});135    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});136    expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));137    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});138    expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));139140    // Unnest141    await newToken.unnest(bob, targetToken, {Substrate: bob.address});142    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});143    expect(await newToken.getOwner()).to.be.deep.equal({Substrate: bob.address});144  });145});