git.delta.rocks / unique-network / refs/commits / 81ec604a4d9b

difftreelog

test nesting to future token or collection address

Max Andreev2023-01-18parent: #cde495e.patch.diff
in: master

1 file changed

addedtests/src/sub/nesting/common.test.tsdiffbeforeafterboth
after · tests/src/sub/nesting/common.test.ts
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';20import {itEth} from '../../eth/util';2122describe('Nesting', () => {23  let alice: IKeyringPair;2425  before(async () => {26    await usingPlaygrounds(async (helper, privateKey) => {27      const donor = await privateKey({filename: __filename});28      [alice] = await helper.arrange.createAccounts([50n], donor);29    });30  });3132  [33    {mode: 'nft' as const, requiredPallets: []},34    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},35  ].map(testCase => {36    itSub.ifWithPallets(`Owner can nest ${testCase.mode.toUpperCase()}`, testCase.requiredPallets, async ({helper}) => {37      // Only NFT allows nesting, permissions should be set:38      const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});39      const targetToken = await aliceNFTCollection.mintToken(alice);4041      const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4243      // 1. Alice can immediately create nested token:44      const nestedToken1 = testCase.mode === 'nft'45        ? await (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())46        : await (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());47      expect(await nestedToken1.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});48      expect(await nestedToken1.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());4950      // 2. Alice can mint and nest token:51      const nestedToken2 = await collectionForNesting.mintToken(alice);52      await nestedToken2.nest(alice, targetToken);53      expect(await nestedToken2.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});54      expect(await nestedToken2.getOwner()).to.be.deep.equal(targetToken.nestingAccount().toLowerCase());55    });56  });5758  itSub('Owner can nest FT', async ({helper}) => {59    // Only NFT allows nesting, permissions should be set:60    const aliceNFTCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});61    const targetToken = await aliceNFTCollection.mintToken(alice);6263    const collectionForNesting = await helper.ft.mintCollection(alice);6465    // 1. Alice can immediately create nested tokens:66    await collectionForNesting.mint(alice, 100n, targetToken.nestingAccount());67    expect(await collectionForNesting.getTop10Owners()).deep.eq([targetToken.nestingAccount().toLowerCase()]);68    expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(100n);6970    // 2. Alice can mint and nest token:71    await collectionForNesting.mint(alice, 100n);72    await collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n);73    expect(await collectionForNesting.getBalance(targetToken.nestingAccount())).eq(150n);74  });75});7677describe('Nesting negative', () => {78  let alice: IKeyringPair;7980  before(async () => {81    await usingPlaygrounds(async (helper, privateKey) => {82      const donor = await privateKey({filename: __filename});83      [alice] = await helper.arrange.createAccounts([50n], donor);84    });85  });8687  [88    {mode: 'nft' as const, requiredPallets: []},89    {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},90  ].map(testCase => {91    itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting not allowed`, testCase.requiredPallets, async ({helper}) => {92    // Create default collection, permissions are not set:93      const aliceNFTCollection = await helper.nft.mintCollection(alice);94      const targetToken = await aliceNFTCollection.mintToken(alice);9596      const collectionForNesting = await helper[testCase.mode].mintCollection(alice);9798      // 1. Alice cannot create immediately nested tokens:99      const nestingTx = testCase.mode === 'nft'100        ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())101        : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());102      await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');103104      // 2. Alice cannot mint and nest token:105      const nestedToken2 = await collectionForNesting.mintToken(alice);106      await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');107    });108  });109110  itSub('Owner cannot nest FT if nesting not allowed', async ({helper}) => {111    // Create default collection, permissions are not set:112    const aliceNFTCollection = await helper.nft.mintCollection(alice);113    const targetToken = await aliceNFTCollection.mintToken(alice);114115    const collectionForNesting = await helper.ft.mintCollection(alice);116117    // 1. Alice cannot create immediately nested tokens:118    await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');119120    // 2. Alice can mint and nest token:121    await collectionForNesting.mint(alice, 100n);122    await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');123  });124125  itSub('Cannot nest to a future token', async ({helper}) => {126    const nonExistingCollectionId = await helper.collection.getTotalCount() + 1000;127    const futureToken = helper.nft.getTokenObject(nonExistingCollectionId, 1);128129    const nftCollectionForNesting = await helper.nft.mintCollection(alice);130    const rftCollectionForNesting = await helper.rft.mintCollection(alice);131    const ftCollectionForNesting = await helper.ft.mintCollection(alice);132133    // 1. Alice cannot create nested token to future token134    await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('TODO:add message');135    await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TODO:add message');136    await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TODO:add message');137138    // 2. Alice cannot mint and nest token:139    const nft = await nftCollectionForNesting.mintToken(alice);140    const rft = await rftCollectionForNesting.mintToken(alice, 100n);141    const _ft = await ftCollectionForNesting.mint(alice, 100n);142    await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TODO:add message');143    await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TODO:add message');144    await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('TODO:add message');145  });146147  itEth('Cannot nest to collection address', async({helper}) => {148    const existingCollection = await helper.nft.mintCollection(alice);149    const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);150    const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);151152    const nftCollectionForNesting = await helper.nft.mintCollection(alice);153154    // 1. Alice cannot create nested token to collection address155    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('TODO:add message');156    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('TODO:add message');157158    // 2. Alice cannot mint and nest token to collection address:159    const nft = await nftCollectionForNesting.mintToken(alice);160    await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('TODO:add message');161    await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('TODO:add message');162  });163164  itSub.ifWithPallets('Cannot nest in RFT', [Pallets.ReFungible], async ({helper}) => {165  // Create default collection, permissions are not set:166    const rftCollection = await helper.rft.mintCollection(alice);167    const targetToken = await rftCollection.mintToken(alice);168169    const collectionForNesting = await helper.nft.mintCollection(alice);170171    // 1. Alice cannot create immediately nested tokens:172    await expect(collectionForNesting.mintToken(alice, targetToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');173174    // 2. Alice cannot mint and nest token:175    const nestedToken2 = await collectionForNesting.mintToken(alice);176    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');177  });178});179