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

difftreelog

source

js-packages/tests/createItem.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 type {IKeyringPair} from '@polkadot/types/types';18import {usingPlaygrounds, expect, itSub, Pallets} from '@unique/test-utils/util.js';19import type {IProperty, ICrossAccountId} from '@unique-nft/playgrounds/types.js';20import {UniqueHelper} from '@unique-nft/playgrounds/unique.js';2122async function mintTokenHelper(helper: UniqueHelper, collection: any, signer: IKeyringPair, owner: ICrossAccountId, type: 'nft' | 'fungible' | 'refungible'='nft', properties?: IProperty[]) {23  let token;24  const itemCountBefore = await helper.collection.getLastTokenId(collection.collectionId);25  const itemBalanceBefore = (await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0])).toBigInt();26  if(type === 'nft') {27    token = await collection.mintToken(signer, owner, properties);28  } else if(type === 'fungible') {29    await collection.mint(signer, 10n, owner);30  } else {31    token = await collection.mintToken(signer, 100n, owner, properties);32  }3334  const itemCountAfter = await helper.collection.getLastTokenId(collection.collectionId);35  const itemBalanceAfter = (await helper.callRpc('api.rpc.unique.balance', [collection.collectionId, owner, 0])).toBigInt();3637  if(type === 'fungible') {38    expect(itemBalanceAfter - itemBalanceBefore).to.be.equal(10n);39  } else {40    expect(itemCountAfter).to.be.equal(itemCountBefore + 1);41  }4243  return token;44}454647describe('integration test: ext. ():', () => {48  let alice: IKeyringPair;49  let bob: IKeyringPair;5051  before(async () => {52    await usingPlaygrounds(async (helper, privateKey) => {53      const donor = await privateKey({url: import.meta.url});54      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);55    });56  });5758  itSub('Create new item in NFT collection', async ({helper}) => {59    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});60    await mintTokenHelper(helper, collection, alice, {Substrate: alice.address});61  });62  itSub('Create new item in Fungible collection', async ({helper}) => {63    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);64    await mintTokenHelper(helper, collection, alice, {Substrate: alice.address}, 'fungible');65  });66  itSub('Check events on create new item in Fungible collection', async ({helper}) => {67    const {collectionId} = await helper.ft.mintCollection(alice, {name: 'test', description: 'test', tokenPrefix: 'test'}, 0);68    const to = {Substrate: alice.address};69    {70      const createData = {fungible: {value: 100}};71      const events = await helper.executeExtrinsic(alice, 'api.tx.unique.createItem', [collectionId, to, createData as any]);72      const result = helper.util.extractTokensFromCreationResult(events);73      expect(result.tokens[0].amount).to.be.equal(100n);74      expect(result.tokens[0].collectionId).to.be.equal(collectionId);75      expect(result.tokens[0].owner).to.be.deep.equal(to);76    }77    {78      const createData = {fungible: {value: 50}};79      const events = await helper.executeExtrinsic(alice, 'api.tx.unique.createItem', [collectionId, to, createData as any]);80      const result = helper.util.extractTokensFromCreationResult(events);81      expect(result.tokens[0].amount).to.be.equal(50n);82      expect(result.tokens[0].collectionId).to.be.equal(collectionId);83      expect(result.tokens[0].owner).to.be.deep.equal(to);84    }85  });86  itSub.ifWithPallets('Create new item in ReFungible collection', [Pallets.ReFungible], async ({helper}) =>  {87    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});88    await mintTokenHelper(helper, collection, alice, {Substrate: alice.address}, 'refungible');89  });90  itSub('Create new item in NFT collection with collection admin permissions', async ({helper}) => {91    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});92    await collection.addAdmin(alice, {Substrate: bob.address});93    await mintTokenHelper(helper, collection, bob, {Substrate: alice.address});94  });95  itSub('Create new item in Fungible collection with collection admin permissions', async ({helper}) => {96    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);97    await collection.addAdmin(alice, {Substrate: bob.address});98    await mintTokenHelper(helper, collection, bob, {Substrate: alice.address}, 'fungible');99  });100  itSub.ifWithPallets('Create new item in ReFungible collection with collection admin permissions', [Pallets.ReFungible], async ({helper}) =>  {101    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});102    await collection.addAdmin(alice, {Substrate: bob.address});103    await mintTokenHelper(helper, collection, bob, {Substrate: alice.address}, 'refungible');104  });105106  itSub('Set property Admin', async ({helper}) => {107    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',108      properties: [{key: 'k', value: 'v'}],109      tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: false, mutable: true, collectionAdmin: true}}],110    });111    await mintTokenHelper(helper, collection, alice, {Substrate: bob.address}, 'nft', [{key: 'k', value: 'v'}]);112  });113114  itSub('Set property AdminConst', async ({helper}) => {115    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',116      properties: [{key: 'k', value: 'v'}],117      tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: false, mutable: false, collectionAdmin: true}}],118    });119    await mintTokenHelper(helper, collection, alice, {Substrate: bob.address}, 'nft', [{key: 'k', value: 'v'}]);120  });121122  itSub('Set property itemOwnerOrAdmin', async ({helper}) => {123    const collection = await helper.nft.mintCollection(alice, {name: 'name', description: 'descr', tokenPrefix: 'COL',124      properties: [{key: 'k', value: 'v'}],125      tokenPropertyPermissions: [{key: 'k', permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}],126    });127    await mintTokenHelper(helper, collection, alice, {Substrate: bob.address}, 'nft', [{key: 'k', value: 'v'}]);128  });129130  itSub('Check total pieces of Fungible token', async ({helper}) => {131    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);132    const amount = 10n;133    await mintTokenHelper(helper, collection, alice, {Substrate: bob.address}, 'fungible');134    {135      const totalPieces = await collection.getTotalPieces();136      expect(totalPieces).to.be.equal(amount);137    }138    await collection.transfer(bob, {Substrate: alice.address}, 1n);139    {140      const totalPieces = await collection.getTotalPieces();141      expect(totalPieces).to.be.equal(amount);142    }143  });144145  itSub('Check total pieces of NFT token', async ({helper}) => {146    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});147    const amount = 1n;148    const token = await mintTokenHelper(helper, collection, alice, {Substrate: bob.address});149    {150      const totalPieces = await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, token.tokenId]);151      expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);152    }153    await token.transfer(bob, {Substrate: alice.address});154    {155      const totalPieces = await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, token.tokenId]);156      expect(totalPieces?.unwrap().toBigInt()).to.be.equal(amount);157    }158  });159160  itSub.ifWithPallets('Check total pieces of ReFungible token', [Pallets.ReFungible], async ({helper}) => {161    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});162    const amount = 100n;163    const token = await mintTokenHelper(helper, collection, alice, {Substrate: bob.address}, 'refungible');164    {165      const totalPieces = await token.getTotalPieces();166      expect(totalPieces).to.be.equal(amount);167    }168    await token.transfer(bob, {Substrate: alice.address}, 60n);169    {170      const totalPieces = await token.getTotalPieces();171      expect(totalPieces).to.be.equal(amount);172    }173  });174});175176describe('Negative integration test: ext. createItem():', () => {177  let alice: IKeyringPair;178  let bob: IKeyringPair;179180  before(async () => {181    await usingPlaygrounds(async (helper, privateKey) => {182      const donor = await privateKey({url: import.meta.url});183      [alice, bob] = await helper.arrange.createAccounts([100n, 100n], donor);184    });185  });186187  itSub('Regular user cannot create new item in NFT collection', async ({helper}) => {188    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});189    const mintTx = () => collection.mintToken(bob, {Substrate: bob.address});190    await expect(mintTx()).to.be.rejectedWith(/common\.PublicMintingNotAllowed/);191  });192  itSub('Regular user cannot create new item in Fungible collection', async ({helper}) => {193    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);194    const mintTx = () => collection.mint(bob, 10n, {Substrate: bob.address});195    await expect(mintTx()).to.be.rejectedWith(/common\.PublicMintingNotAllowed/);196  });197  itSub.ifWithPallets('Regular user cannot create new item in ReFungible collection', [Pallets.ReFungible], async ({helper}) => {198    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});199    const mintTx = () => collection.mintToken(bob, 100n, {Substrate: bob.address});200    await expect(mintTx()).to.be.rejectedWith(/common\.PublicMintingNotAllowed/);201  });202203  itSub('No editing rights', async ({helper}) => {204    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',205      tokenPropertyPermissions: [{key: 'k', permission: {mutable: false, collectionAdmin: false, tokenOwner: false}}],206    });207    const mintTx = () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);208    await expect(mintTx()).to.be.rejectedWith(/common\.NoPermission/);209  });210211  itSub('User doesnt have editing rights', async ({helper}) => {212    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',213      tokenPropertyPermissions: [{key: 'k', permission: {mutable: true, collectionAdmin: false, tokenOwner: false}}],214    });215    const mintTx = () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);216    await expect(mintTx()).to.be.rejectedWith(/common\.NoPermission/);217  });218219  itSub('Adding property without access rights', async ({helper}) => {220    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});221    const mintTx = () => collection.mintToken(alice, {Substrate: bob.address}, [{key: 'k', value: 'v'}]);222    await expect(mintTx()).to.be.rejectedWith(/common\.NoPermission/);223  });224225  itSub('Adding more than 64 prps', async ({helper}) => {226    const props: IProperty[] = [];227228    for(let i = 0; i < 65; i++) {229      props.push({key: `key${i}`, value: `value${i}`});230    }231232233    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});234    const mintTx = () => collection.mintToken(alice, {Substrate: bob.address}, props);235    await expect(mintTx()).to.be.rejectedWith('Verification Error');236  });237238  itSub('Trying to add bigger property than allowed', async ({helper}) => {239    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL',240      tokenPropertyPermissions: [241        {key: 'k1', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},242        {key: 'k2', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},243      ],244    });245    const mintTx = () => collection.mintToken(alice, {Substrate: bob.address}, [246      {key: 'k1', value: 'vvvvvv'.repeat(5000)},247      {key: 'k2', value: 'vvv'.repeat(5000)},248    ]);249    await expect(mintTx()).to.be.rejectedWith(/common\.NoSpaceForProperty/);250  });251252  itSub('Check total pieces for invalid Fungible token', async ({helper}) => {253    const collection = await helper.ft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'}, 0);254    const invalidTokenId = 1_000_000;255    expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;256    expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);257  });258259  itSub('Check total pieces for invalid NFT token', async ({helper}) => {260    const collection = await helper.nft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});261    const invalidTokenId = 1_000_000;262    expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;263    expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);264  });265266  itSub.ifWithPallets('Check total pieces for invalid Refungible token', [Pallets.ReFungible], async ({helper}) => {267    const collection = await helper.rft.mintCollection(alice, {name: 'col', description: 'descr', tokenPrefix: 'COL'});268    const invalidTokenId = 1_000_000;269    expect((await helper.callRpc('api.rpc.unique.totalPieces', [collection.collectionId, invalidTokenId]))?.isNone).to.be.true;270    expect((await helper.callRpc('api.rpc.unique.tokenData', [collection.collectionId, invalidTokenId]))?.pieces.toBigInt()).to.be.equal(0n);271  });272});