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

difftreelog

source

js-packages/tests/createCollection.test.ts9.0 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 {usingPlaygrounds, expect, itSub, Pallets} from './util/index.js';19import {CollectionFlag} from '@unique/playgrounds/types.js';20import type {ICollectionCreationOptions, IProperty} from '@unique/playgrounds/types.js';21import {UniqueHelper} from '@unique/playgrounds/unique.js';2223async function mintCollectionHelper(helper: UniqueHelper, signer: IKeyringPair, options: ICollectionCreationOptions, type?: 'nft' | 'fungible' | 'refungible') {24  let collection;25  if(type === 'nft') {26    collection = await helper.nft.mintCollection(signer, options);27  } else if(type === 'fungible') {28    collection = await helper.ft.mintCollection(signer, options, 0);29  } else {30    collection = await helper.rft.mintCollection(signer, options);31  }32  const data = await collection.getData();33  expect(data?.normalizedOwner).to.be.equal(helper.address.normalizeSubstrate(signer.address));34  expect(data?.name).to.be.equal(options.name);35  expect(data?.description).to.be.equal(options.description);36  expect(data?.raw.tokenPrefix).to.be.equal(options.tokenPrefix);37  if(options.properties) {38    expect(data?.raw.properties).to.be.deep.equal(options.properties);39  }40  if(options.adminList) {41    expect(data?.admins).to.be.deep.equal(options.adminList);42  }4344  if(options.flags) {45    if((options.flags[0] & 64) != 0)46      expect(data?.raw.flags.erc721metadata).to.be.true;47    if((options.flags[0] & 128) != 0)48      expect(data?.raw.flags.foreign).to.be.false;49  }5051  if(options.tokenPropertyPermissions) {52    expect(data?.raw.tokenPropertyPermissions).to.be.deep.equal(options.tokenPropertyPermissions);53  }5455  return collection;56}5758describe('integration test: ext. createCollection():', () => {59  let alice: IKeyringPair;60  let bob: IKeyringPair;6162  before(async () => {63    await usingPlaygrounds(async (helper, privateKey) => {64      const donor = await privateKey({url: import.meta.url});65      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);66    });67  });68  itSub('Create new NFT collection', async ({helper}) => {69    await mintCollectionHelper(helper, alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 'nft');70  });71  itSub('Create new NFT collection whith collection_name of maximum length (64 bytes)', async ({helper}) => {72    await mintCollectionHelper(helper, alice, {name: 'A'.repeat(64), description: 'descr', tokenPrefix: 'COL'}, 'nft');73  });74  itSub('Create new NFT collection whith collection_description of maximum length (256 bytes)', async ({helper}) => {75    await mintCollectionHelper(helper, alice, {name: 'name', description: 'A'.repeat(256), tokenPrefix: 'COL'}, 'nft');76  });77  itSub('Create new NFT collection whith token_prefix of maximum length (16 bytes)', async ({helper}) => {78    await mintCollectionHelper(helper, alice, {name: 'name', description: 'descr', tokenPrefix: 'A'.repeat(16)}, 'nft');79  });8081  itSub('Create new Fungible collection', async ({helper}) => {82    await mintCollectionHelper(helper, alice, {name: 'name', description: 'descr', tokenPrefix: 'COL'}, 'fungible');83  });8485  itSub.ifWithPallets('Create new ReFungible collection', [Pallets.ReFungible], async ({helper}) => {86    await mintCollectionHelper(helper, alice, {name: 'name', description: 'descr', tokenPrefix: 'COL'}, 'refungible');87  });8889  itSub('create new collection with properties', async ({helper}) => {90    await mintCollectionHelper(helper, alice, {91      name: 'name', description: 'descr', tokenPrefix: 'COL',92      properties: [{key: 'key1', value: 'val1'}],93      tokenPropertyPermissions: [{key: 'key1', permission: {tokenOwner: true, mutable: false, collectionAdmin: true}}],94    }, 'nft');95  });9697  itSub('create new collection with admin', async ({helper}) => {98    await mintCollectionHelper(helper, alice, {99      name: 'name', description: 'descr', tokenPrefix: 'COL',100      adminList: [{Substrate: bob.address}],101    }, 'nft');102  });103104  itSub('create new collection with flags', async ({helper}) => {105    await mintCollectionHelper(helper, alice, {106      name: 'name', description: 'descr', tokenPrefix: 'COL',107      flags: [CollectionFlag.Erc721metadata],108    }, 'nft');109110    // User can not set Foreign flag itself111112    await expect(mintCollectionHelper(helper, alice, {113      name: 'name', description: 'descr', tokenPrefix: 'COL',114      flags: [CollectionFlag.Foreign],115    }, 'nft')).to.be.rejectedWith(/common.NoPermission/);116117    await expect(mintCollectionHelper(helper, alice, {118      name: 'name', description: 'descr', tokenPrefix: 'COL',119      flags: [CollectionFlag.Erc721metadata, CollectionFlag.Foreign],120    }, 'nft')).to.be.rejectedWith(/common.NoPermission/);121  });122123  itSub('Create new collection with extra fields', async ({helper}) => {124    const collection = await mintCollectionHelper(helper, alice, {name: 'name', description: 'descr', tokenPrefix: 'COL'}, 'fungible');125    await collection.setPermissions(alice, {access: 'AllowList'});126    await collection.setLimits(alice, {accountTokenOwnershipLimit: 3});127    const data = await collection.getData();128    const limits = await collection.getEffectiveLimits();129    const raw = data?.raw;130131    expect(data?.normalizedOwner).to.be.equal(helper.address.normalizeSubstrate(alice.address));132    expect(data?.name).to.be.equal('name');133    expect(data?.description).to.be.equal('descr');134    expect(raw.permissions.access).to.be.equal('AllowList');135    expect(raw.mode).to.be.deep.equal({Fungible: '0'});136    expect(limits.accountTokenOwnershipLimit).to.be.equal(3);137  });138139  itSub('New collection is not external', async ({helper}) => {140    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL'});141    const data = await collection.getData();142    expect(data?.raw.readOnly).to.be.false;143  });144});145146describe('(!negative test!) integration test: ext. createCollection():', () => {147  let alice: IKeyringPair;148149  before(async () => {150    await usingPlaygrounds(async (helper, privateKey) => {151      const donor = await privateKey({url: import.meta.url});152      [alice] = await helper.arrange.createAccounts([100n], donor);153    });154  });155156  itSub('(!negative test!) create new NFT collection whith incorrect data (collection_name)', async ({helper}) => {157    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'A'.repeat(65), description: 'descr', tokenPrefix: 'COL'});158    await expect(mintCollectionTx()).to.be.rejectedWith('Verification Error');159  });160  itSub('(!negative test!) create new NFT collection whith incorrect data (collection_description)', async ({helper}) => {161    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'name', description: 'A'.repeat(257), tokenPrefix: 'COL'});162    await expect(mintCollectionTx()).to.be.rejectedWith('Verification Error');163  });164  itSub('(!negative test!) create new NFT collection whith incorrect data (token_prefix)', async ({helper}) => {165    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'A'.repeat(17)});166    await expect(mintCollectionTx()).to.be.rejectedWith('Verification Error');167  });168169  itSub('(!negative test!) fails when bad limits are set', async ({helper}) => {170    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL', limits: {tokenLimit: 0}});171    await expect(mintCollectionTx()).to.be.rejectedWith(/common\.CollectionTokenLimitExceeded/);172  });173174  itSub('(!negative test!) create collection with incorrect property limit (64 elements)', async ({helper}) => {175    const props: IProperty[] = [];176177    for(let i = 0; i < 65; i++) {178      props.push({key: `key${i}`, value: `value${i}`});179    }180    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL', properties: props});181    await expect(mintCollectionTx()).to.be.rejectedWith('Verification Error');182  });183184  itSub('(!negative test!) create collection with incorrect property limit (40 kb)', async ({helper}) => {185    const props: IProperty[] = [];186187    for(let i = 0; i < 32; i++) {188      props.push({key: `key${i}`.repeat(80), value: `value${i}`.repeat(80)});189    }190191    const mintCollectionTx = () => helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL', properties: props});192    await expect(mintCollectionTx()).to.be.rejectedWith('Verification Error');193  });194});