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

difftreelog

source

tests/src/sub/nesting/common.test.ts11.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';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.ifWithPallets('Cannot nest to a future collection', [Pallets.ReFungible], 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('CollectionNotFound');135    await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');136    await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');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('CollectionNotFound');143    await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('CollectionNotFound');144    await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('CollectionNotFound');145  });146147  itSub.ifWithPallets('Cannot nest to a future token in a NFT collection', [Pallets.ReFungible], async ({helper}) => {148    const {collectionId} = await helper.nft.mintCollection(alice);149    // To avoid UserIsNotAllowedToNest error150    await helper.collection.setPermissions(alice, collectionId, {nesting: {collectionAdmin: true}});151    const futureToken = helper.nft.getTokenObject(collectionId, 1);152153    const nftCollectionForNesting = await helper.nft.mintCollection(alice);154    const rftCollectionForNesting = await helper.rft.mintCollection(alice);155    const ftCollectionForNesting = await helper.ft.mintCollection(alice);156157    // 1. Alice cannot create nested token to future token158    await expect(nftCollectionForNesting.mintToken(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');159    await expect(rftCollectionForNesting.mintToken(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');160    await expect(ftCollectionForNesting.mint(alice, 10n, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');161162    // 2. Alice cannot mint and nest token:163    const nft = await nftCollectionForNesting.mintToken(alice);164    const rft = await rftCollectionForNesting.mintToken(alice, 100n);165    const _ft = await ftCollectionForNesting.mint(alice, 100n);166    await expect(nft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');167    await expect(rft.transfer(alice, futureToken.nestingAccount())).to.be.rejectedWith('TokenNotFound');168    await expect(ftCollectionForNesting.transfer(alice, futureToken.nestingAccount(), 50n)).to.be.rejectedWith('TokenNotFound');169  });170171  itEth.ifWithPallets('Cannot nest to collection address', [Pallets.ReFungible], async({helper}) => {172    const existingCollection = await helper.nft.mintCollection(alice);173    const existingCollectionAddress = helper.ethAddress.fromCollectionId(existingCollection.collectionId);174    const futureCollectionAddress = helper.ethAddress.fromCollectionId(99999999);175176    const nftCollectionForNesting = await helper.nft.mintCollection(alice);177178    // 1. Alice cannot create nested token to collection address179    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');180    await expect(nftCollectionForNesting.mintToken(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');181182    // 2. Alice cannot mint and nest token to collection address:183    const nft = await nftCollectionForNesting.mintToken(alice);184    await expect(nft.transfer(alice, {Ethereum: existingCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');185    await expect(nft.transfer(alice, {Ethereum: futureCollectionAddress})).to.be.rejectedWith('CantNestTokenUnderCollection');186  });187188  itEth.ifWithPallets('Cannot nest in RFT or FT', [Pallets.ReFungible], async ({helper}) => {189  // Create default collection, permissions are not set:190    const rftCollection = await helper.rft.mintCollection(alice);191    const ftCollection = await helper.ft.mintCollection(alice);192193    const rftToken = await rftCollection.mintToken(alice);194    const _ftToken = await ftCollection.mint(alice, 100n);195196    const collectionForNesting = await helper.nft.mintCollection(alice);197198    // 1. Alice cannot create immediately nested tokens:199    await expect(collectionForNesting.mintToken(alice, rftToken.nestingAccount())).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');200    await expect(collectionForNesting.mintToken(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');201202    // 2. Alice cannot mint and nest token:203    const nestedToken2 = await collectionForNesting.mintToken(alice);204    await expect(nestedToken2.nest(alice, rftToken)).to.be.rejectedWith('refungible.RefungibleDisallowsNesting');205    await expect(ftCollection.transfer(alice, {Ethereum: helper.ethAddress.fromTokenId(ftCollection.collectionId, 0)})).to.be.rejectedWith('fungible.FungibleDisallowsNesting');206  });207});