git.delta.rocks / unique-network / refs/commits / 6ad9467f3414

difftreelog

source

tests/src/sub/nesting/nesting.negative.test.ts14.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 {UniqueFTCollection, UniqueNFTCollection, UniqueNFToken, UniqueRFTCollection, UniqueRFToken} from '../../util/playgrounds/unique';20import {itEth} from '../../eth/util';2122let alice: IKeyringPair;23let bob: IKeyringPair;24let charlie: IKeyringPair;2526before(async () => {27  await usingPlaygrounds(async (helper, privateKey) => {28    const donor = await privateKey({url: import.meta.url});29    [alice, bob, charlie] = await helper.arrange.createAccounts([100n, 100n, 100n], donor);30  });31});3233[34  {mode: 'nft' as const, requiredPallets: []},35  {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36].map(testCase => {37  itSub.ifWithPallets(`Owner cannot nest ${testCase.mode.toUpperCase()} if nesting is disabled`, testCase.requiredPallets, async ({helper}) => {38    // Create default collection, permissions are not set:39    const aliceNFTCollection = await helper.nft.mintCollection(alice);40    const targetToken = await aliceNFTCollection.mintToken(alice);4142    const collectionForNesting = await helper[testCase.mode].mintCollection(alice);4344    // 1. Alice cannot create immediately nested tokens:45    const nestingTx = testCase.mode === 'nft'46      ? (collectionForNesting as UniqueNFTCollection).mintToken(alice, targetToken.nestingAccount())47      : (collectionForNesting as UniqueRFTCollection).mintToken(alice, 10n, targetToken.nestingAccount());48    await expect(nestingTx).to.be.rejectedWith('common.UserIsNotAllowedToNest');4950    // 2. Alice cannot mint and nest token:51    const nestedToken2 = await collectionForNesting.mintToken(alice);52    await expect(nestedToken2.nest(alice, targetToken)).to.be.rejectedWith('common.UserIsNotAllowedToNest');53  });54});5556itSub('Owner cannot nest FT if nesting is disabled', async ({helper}) => {57  // Create default collection, permissions are not set:58  const aliceNFTCollection = await helper.nft.mintCollection(alice);59  const targetToken = await aliceNFTCollection.mintToken(alice);6061  const collectionForNesting = await helper.ft.mintCollection(alice);6263  // 1. Alice cannot create immediately nested tokens:64  await expect(collectionForNesting.mint(alice, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest');6566  // 2. Alice can mint and nest token:67  await collectionForNesting.mint(alice, 100n);68  await expect(collectionForNesting.transfer(alice, targetToken.nestingAccount(), 50n)).to.be.rejectedWith('common.UserIsNotAllowedToNest');69});7071[72  {mode: 'nft' as const},73  {mode: 'rft' as const},74  {mode: 'ft' as const},75].map(testCase => {76  itSub(`Non-owner and non-admin cannot nest ${testCase.mode.toUpperCase()} in someone else's tokens`, async ({helper}) => {77    const targetCollection = await helper.nft.mintCollection(alice, {permissions:78      {nesting: {tokenOwner: true, collectionAdmin: true}},79    });80    const targetToken = await targetCollection.mintToken(alice);8182    const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);8384    let nestedTokenBob: UniqueNFToken | UniqueRFToken;85    switch (testCase.mode) {86      case 'nft': nestedTokenBob = await (nestedCollectionBob as UniqueNFTCollection).mintToken(bob); break;87      case 'rft': nestedTokenBob = await (nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n); break;88      case 'ft': await (nestedCollectionBob as UniqueFTCollection).mint(bob, 100n); break;89    }9091    // Bob non-owner of targetToken and non admin of targetCollection, so92    // 1. cannot mint nested token:93    switch (testCase.mode) {94      case 'nft': await expect((nestedCollectionBob as UniqueNFTCollection).mintToken(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;95      case 'rft': await expect((nestedCollectionBob as UniqueRFTCollection).mintToken(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;96      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).mint(bob, 100n, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;97    }9899    // 2. cannot nest existing token:100    switch (testCase.mode) {101      case 'nft':102      case 'rft': await expect(nestedTokenBob!.transfer(bob, targetToken.nestingAccount())).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;103      case 'ft': await expect((nestedCollectionBob as UniqueFTCollection).transfer(bob, targetToken.nestingAccount(), 100n)).to.be.rejectedWith('common.UserIsNotAllowedToNest'); break;104    }105  });106});107108[109  {mode: 'nft' as const, nesting: {tokenOwner: true,  collectionAdmin: false}},110  {mode: 'nft' as const, nesting: {tokenOwner: false, collectionAdmin: true}},111].map(testCase => {112  itSub(`${testCase.nesting.tokenOwner ? 'Admin' : 'Token owner'} cannot nest when only ${testCase.nesting.tokenOwner ? 'tokenOwner' : 'collectionAdmin'} is allowed`, async ({helper}) => {113    // Create collection with tokenOwner or create collection with collectionAdmin permission:114    const targetCollection = await helper.nft.mintCollection(alice, {permissions: {nesting: testCase.nesting}});115    const targetTokenCharlie = await targetCollection.mintToken(alice, {Substrate: charlie.address});116    await targetCollection.addAdmin(alice, {Substrate: bob.address});117118    const nestedCollectionCharlie = await helper[testCase.mode].mintCollection(charlie);119    const nestedCollectionBob = await helper[testCase.mode].mintCollection(bob);120    // if nesting permissions restricted for token owner – minter is bob (admin),121    // if collectionAdmin – charlie (owner)122    testCase.nesting.tokenOwner123      ? await expect(nestedCollectionBob.mintToken(bob, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/)124      : await expect(nestedCollectionCharlie.mintToken(charlie, targetTokenCharlie.nestingAccount())).to.be.rejectedWith(/common\.UserIsNotAllowedToNest/);125  });126});127128itSub.ifWithPallets('Cannot nest in non existing token', [Pallets.ReFungible], async ({helper}) => {129  const collection = await helper.nft.mintCollection(alice);130  // To avoid UserIsNotAllowedToNest error131  await helper.collection.setPermissions(alice, collection.collectionId, {nesting: {collectionAdmin: true}});132133  // The list of non-existing tokens:134  const tokenFromNonExistingCollection = helper.nft.getTokenObject(9999999, 1);135  const tokenBurnt = await collection.mintToken(alice);136  await tokenBurnt.burn(alice);137  const tokenNotMintedYet = helper.nft.getTokenObject(collection.collectionId, 2);138139  // The list of collections to nest tokens from:140  const nftCollectionForNesting = await helper.nft.mintCollection(alice);141  const rftCollectionForNesting = await helper.rft.mintCollection(alice);142  const ftCollectionForNesting = await helper.ft.mintCollection(alice);143144  const testCases = [145    {token: tokenFromNonExistingCollection, error: 'CollectionNotFound'},146    {token: tokenBurnt, error: 'TokenNotFound'},147    {token: tokenNotMintedYet, error: 'TokenNotFound'},148  ];149150  for(const testCase of testCases) {151    // 1. Alice cannot create nested token to non-existing token152    await expect(nftCollectionForNesting.mintToken(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);153    await expect(rftCollectionForNesting.mintToken(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);154    await expect(ftCollectionForNesting.mint(alice, 10n, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);155156    // 2. Alice cannot mint and nest token:157    const nft = await nftCollectionForNesting.mintToken(alice);158    const rft = await rftCollectionForNesting.mintToken(alice, 100n);159    const _ft = await ftCollectionForNesting.mint(alice, 100n);160    await expect(nft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);161    await expect(rft.transfer(alice, testCase.token.nestingAccount())).to.be.rejectedWith(testCase.error);162    await expect(ftCollectionForNesting.transfer(alice, testCase.token.nestingAccount(), 50n)).to.be.rejectedWith(testCase.error);163  }164});165166itEth.ifWithPallets('Cannot nest in collection address', [Pallets.ReFungible], async({helper}) => {167  const existingCollection = await helper.nft.mintCollection(alice);168  const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);169  const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);170171  const nftCollectionForNesting = await helper.nft.mintCollection(alice);172173  // 1. Alice cannot create nested token to collection address174  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');175  await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');176177  // 2. Alice cannot mint and nest token to collection address:178  const nft = await nftCollectionForNesting.mintToken(alice);179  await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');180  await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');181});182183itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {184  // Create default collection, permissions are not set:185  const rftCollection = await helper.rft.mintCollection(alice);186  const ftCollection = await helper.ft.mintCollection(alice);187188  const rftToken = await rftCollection.mintToken(alice);189  const _ftToken = await ftCollection.mint(alice, 100n);190191  const collectionForNesting = await helper.nft.mintCollection(alice);192193  // 1. Alice cannot create immediately nested tokens:194  await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');195  await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');196197  // 2. Alice cannot mint and nest token:198  const nestedToken2 = await collectionForNesting.mintToken(alice);199  await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');200  await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');201});202203itSub('Cannot nest in restricted collection if collection is not in the list', async ({helper}) => {204  const {collectionId: allowedCollectionId} = await helper.nft.mintCollection(alice);205  const notAllowedCollectionNFT = await helper.nft.mintCollection(alice);206  const notAllowedCollectionRFT = await helper.rft.mintCollection(alice);207  const notAllowedCollectionFT = await helper.ft.mintCollection(alice);208209  // Collection restricted to allowedCollectionId210  const restrictedCollectionA = await helper.nft.mintCollection(alice, {permissions:211    {nesting: {tokenOwner: true, restricted: [allowedCollectionId]}},212  });213  // Create collection with restricted nesting -- even self is not allowed214  const restrictedCollectionB = await helper.nft.mintCollection(alice, {permissions:215    {nesting: {tokenOwner: true, restricted: []}},216  });217218  const targetTokenA = await restrictedCollectionA.mintToken(alice);219  const targetTokenB = await restrictedCollectionB.mintToken(alice);220221  // 1. Cannot mint in own collection after allowlisting the accounts:222  await expect(restrictedCollectionA.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);223  await expect(restrictedCollectionB.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);224225  // 2. Cannot mint from notAllowedCollection:226  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);227  await expect(notAllowedCollectionNFT.mintToken(alice, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);228  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);229  await expect(notAllowedCollectionRFT.mintToken(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);230  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenA.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);231  await expect(notAllowedCollectionFT.mint(alice, 100n, targetTokenB.nestingAccount())).to.be.rejectedWith(/common\.SourceCollectionIsNotAllowedToNest/);232});233234itSub('Cannot create nesting chains greater than 5', async ({helper}) => {235  const collection = await helper.nft.mintCollection(alice, {permissions: {nesting: {tokenOwner: true}}});236  let token = await collection.mintToken(alice);237238  const maxNestingLevel = 5;239240  // Create a nested-token matryoshka241  for (let i = 0; i < maxNestingLevel; i++) {242    token = await collection.mintToken(alice, token.nestingAccount());243  }244245  // The nesting depth is limited by `maxNestingLevel`246  // 1. Cannot mint:247  await expect(collection.mintToken(alice, token.nestingAccount()))248    .to.be.rejectedWith(/structure\.DepthLimit/);249  // 2. Cannot transfer:250  const anotherToken = await collection.mintToken(alice);251  await expect(anotherToken.transfer(alice, token.nestingAccount()))252    .to.be.rejectedWith(/structure\.DepthLimit/);253  // 3. Cannot nest FT pieces:254  const ftCollection = await helper.ft.mintCollection(alice);255  await expect(ftCollection.mint(alice, 100n, token.nestingAccount()))256    .to.be.rejectedWith(/structure\.DepthLimit/);257258  expect(await token.getTopmostOwner()).to.be.deep.equal({Substrate: alice.address});259  expect(await token.getChildren()).to.has.length(0);260});