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

difftreelog

source

js-packages/tests/sub/nesting/admin.test.ts4.6 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('Nesting by collection admin', () => {22  let alice: IKeyringPair;23  let bob: IKeyringPair;24  let charlie: IKeyringPair;2526  before(async () => {27    await usingPlaygrounds(async (helper, privateKey) => {28      const donor = await privateKey({url: import.meta.url});29      [alice, bob, charlie] = await helper.arrange.createAccounts([200n, 10n, 10n], donor);30    });31  });3233  [34    {restricted: true},35    {restricted: false},36  ].map(testCase => {37    itSub(`can nest tokens if "collectionAdmin" permission set ${testCase.restricted ? ', in restricted mode' : ''}`, async ({helper}) => {38      const collectionA = await helper.nft.mintCollection(alice);39      await collectionA.addAdmin(alice, {Substrate: bob.address});40      const collectionB = await helper.nft.mintCollection(alice);41      await collectionB.addAdmin(alice, {Substrate: bob.address});42      // Collection has permission for collectionAdmin to nest:43      await collectionA.setPermissions(alice, {nesting:44        {collectionAdmin: true, restricted: testCase.restricted ? [collectionA.collectionId, collectionB.collectionId] : null},45      });46      // Token for nesting in from collectionA:47      const targetTokenA = await collectionA.mintToken(alice, {Substrate: charlie.address});4849      // 1. Create an immediately nested tokens:50      // 1.1 From own collection:51      const nestedTokenA = await collectionA.mintToken(bob, targetTokenA.nestingAccount());52      // 1.2 From different collection:53      const nestedTokenB = await collectionB.mintToken(bob, targetTokenA.nestingAccount());54      expect(await nestedTokenA.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});55      expect(await nestedTokenA.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));56      expect(await nestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});57      expect(await nestedTokenB.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));5859      // 2. Create a token to be nested and nest:60      const newNestedTokenA = await collectionA.mintToken(bob);61      const newNestedTokenB = await collectionB.mintToken(bob);62      // 2.1 From own collection:63      await newNestedTokenA.nest(bob, targetTokenA);64      // 2.2 From different collection:65      await newNestedTokenB.nest(bob, targetTokenA);66      expect(await newNestedTokenB.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});67      expect(await newNestedTokenB.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetTokenA.nestingAccount()));68    });69  });7071  itSub('can operate together with token owner if "collectionAdmin" and "tokenOwner" permissions set', async ({helper}) => {72    const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {collectionAdmin: true, tokenOwner: true}}});73    await collection.addAdmin(alice, {Substrate: bob.address});74    const targetToken = await collection.mintToken(alice, {Substrate: charlie.address});7576    // Admin can create an immediately nested token:77    const nestedToken = await collection.mintToken(bob, targetToken.nestingAccount());78    expect(await nestedToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});79    expect(await nestedToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));8081    // Owner can create and and nest:82    const newToken = await collection.mintToken(alice, {Substrate: charlie.address});83    await newToken.nest(charlie, targetToken);84    expect(await newToken.getTopmostOwner()).to.be.deep.equal({Substrate: charlie.address});85    expect(await newToken.getOwner()).to.be.deep.equal(CrossAccountId.toLowerCase(targetToken.nestingAccount()));86  });87});