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

difftreelog

source

tests/src/sub/nesting/common.test.ts6.5 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 {UniqueNFTCollection, UniqueRFTCollection} from '../../util/playgrounds/unique';2021let alice: IKeyringPair;22let bob: IKeyringPair;2324before(async () => {25  await usingPlaygrounds(async (helper, privateKey) => {26    const donor = await privateKey({url: import.meta.url});27    [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);28  });29});3031[32  {mode: 'nft' as const, restrictedMode: true, requiredPallets: []},33  {mode: 'nft' as const, restrictedMode: false, requiredPallets: []},34  {mode: 'rft' as const, restrictedMode: true, requiredPallets: [Pallets.ReFungible]},35  {mode: 'rft' as const, restrictedMode: false, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37  itSub.ifWithPallets(`Token owner can nest ${testCase.mode.toUpperCase()} in NFT if "tokenOwner" permission set ${testCase.restrictedMode ? 'in restricted mode': ''}`, testCase.requiredPallets, async ({helper}) => {38    // Only NFT can be target for nesting in39    const targetNFTCollection = await helper.nft.mintCollection(alice);40    const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});4142    const collectionForNesting = await helper[testCase.mode].mintCollection(bob);43    // permissions should be set:44    await targetNFTCollection.setPermissions(alice, {45      nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},46    });4748    // 1. Bob can immediately create nested token:49    const nestedToken1 = testCase.mode === 'nft'50      ? await (collectionForNesting as UniqueNFTCollection).mintToken(bob, targetTokenBob.nestingAccount())51      : await (collectionForNesting as UniqueRFTCollection).mintToken(bob, 10n, targetTokenBob.nestingAccount());52    expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});53    expect(await nestedToken1.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());5455    // 2. Bob can mint and nest token:56    const nestedToken2 = await collectionForNesting.mintToken(bob);57    await nestedToken2.nest(bob, targetTokenBob);58    expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: bob.address});59    expect(await nestedToken2.getOwner()).to.be.deep.equal(targetTokenBob.nestingAccount().toLowerCase());60  });61});626364[65  {restrictedMode: true},66  {restrictedMode: false},67].map(testCase => {68  itSub(`Token owner can nest FT in NFT if "tokenOwner" permission set  ${testCase.restrictedMode ? 'in restricted mode': ''}`, async ({helper}) => {69    // Only NFT allows nesting, permissions should be set:70    const targetNFTCollection = await helper.nft.mintCollection(alice);71    const targetTokenBob = await targetNFTCollection.mintToken(alice, {Substrate: bob.address});7273    const collectionForNesting = await helper.ft.mintCollection(bob);74    // permissions should be set:75    await targetNFTCollection.setPermissions(alice, {76      nesting: {tokenOwner: true, restricted: testCase.restrictedMode ? [collectionForNesting.collectionId] : null},77    });7879    // 1. Alice can immediately create nested tokens:80    await collectionForNesting.mint(bob, 100n, targetTokenBob.nestingAccount());81    expect(await collectionForNesting.getTop10Owners()).deep.eq([targetTokenBob.nestingAccount().toLowerCase()]);82    expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(100n);8384    // 2. Alice can mint and nest token:85    await collectionForNesting.mint(bob, 100n);86    await collectionForNesting.transfer(bob, targetTokenBob.nestingAccount(), 50n);87    expect(await collectionForNesting.getBalance(targetTokenBob.nestingAccount())).eq(150n);88  });89});909192itSub.ifWithPallets('Owner can unnest tokens using transferFrom', [Pallets.ReFungible], async ({helper}) => {93  const collectionToNest = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});94  const tokenA = await collectionToNest.mintToken(alice);95  const tokenB = await collectionToNest.mintToken(alice);9697  // Create a nested token98  const nftCollectionToBeNested = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});99  const rftCollectionToBeNested = await helper.rft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});100  const ftCollectionToBeNested = await helper.ft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});101102  const nestedNFT = await nftCollectionToBeNested.mintToken(alice, tokenA.nestingAccount());103  const nestedRFT = await rftCollectionToBeNested.mintToken(alice, 100n, tokenA.nestingAccount());104  const _nestedFT = await ftCollectionToBeNested.mint(alice, 100n, tokenA.nestingAccount());105  expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());106  expect(await nestedRFT.getOwner()).to.be.deep.equal(tokenA.nestingAccount().toLowerCase());107  expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(100n);108109  // Transfer the nested token to another token110  await nestedNFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount());111  await nestedRFT.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);112  await ftCollectionToBeNested.transferFrom(alice, tokenA.nestingAccount(), tokenB.nestingAccount(), 25n);113114  expect(await nestedNFT.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});115  expect(await nestedNFT.getOwner()).to.be.deep.equal(tokenB.nestingAccount().toLowerCase());116117  expect(await nestedRFT.getBalance(tokenB.nestingAccount())).to.equal(25n);118  expect(await nestedRFT.getBalance(tokenA.nestingAccount())).to.equal(75n);119120  expect(await ftCollectionToBeNested.getBalance(tokenB.nestingAccount())).to.equal(25n);121  expect(await ftCollectionToBeNested.getBalance(tokenA.nestingAccount())).to.equal(75n);122});