git.delta.rocks / unique-network / refs/commits / 105eff5daed6

difftreelog

source

tests/src/createMultipleItems.test.ts9.9 KiBsourcehistory
1//2// This file is subject to the terms and conditions defined in3// file 'LICENSE', which is part of this source code package.4//5import { ApiPromise } from '@polkadot/api';6import BN from 'bn.js';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import privateKey from './substrate/privateKey';10import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from './substrate/substrate-api';11import {12  createCollectionExpectSuccess,13  destroyCollectionExpectSuccess,14  getGenericResult,15  IReFungibleTokenDataType,16  setCollectionLimitsExpectSuccess,17} from './util/helpers';1819chai.use(chaiAsPromised);20const expect = chai.expect;2122interface ITokenDataType {23  Owner: number[];24  ConstData: number[];25  VariableData: number[];26}2728describe('Integration Test createMultipleItems(collection_id, owner, items_data):', () => {29  it('Create  0x31, 0x32, 0x33 items in active NFT collection and verify tokens data in chain', async () => {30    await usingApi(async (api: ApiPromise) => {31      const collectionId = await createCollectionExpectSuccess();32      const itemsListIndexBefore = await api.query.nft.itemListIndex(collectionId) as unknown as BN;33      expect(itemsListIndexBefore.toNumber()).to.be.equal(0);34      const Alice = privateKey('//Alice');35      const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }];36      const createMultipleItemsTx = await api.tx.nft37        .createMultipleItems(collectionId, Alice.address, args);38      await submitTransactionAsync(Alice, createMultipleItemsTx);39      const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;40      expect(itemsListIndexAfter.toNumber()).to.be.equal(3);41      const token1Data = await api.query.nft.nftItemList(collectionId, 1) as unknown as ITokenDataType;42      const token2Data = await api.query.nft.nftItemList(collectionId, 2) as unknown as ITokenDataType;43      const token3Data = await api.query.nft.nftItemList(collectionId, 3) as unknown as ITokenDataType;4445      expect(token1Data.Owner.toString()).to.be.equal(Alice.address);46      expect(token2Data.Owner.toString()).to.be.equal(Alice.address);47      expect(token3Data.Owner.toString()).to.be.equal(Alice.address);4849      expect(token1Data.ConstData.toString()).to.be.equal('0x31');50      expect(token2Data.ConstData.toString()).to.be.equal('0x32');51      expect(token3Data.ConstData.toString()).to.be.equal('0x33');5253      expect(token1Data.VariableData.toString()).to.be.equal('0x31');54      expect(token2Data.VariableData.toString()).to.be.equal('0x32');55      expect(token3Data.VariableData.toString()).to.be.equal('0x33');56    });57  });5859  it('Create  0x31, 0x32, 0x33 items in active ReFungible collection and verify tokens data in chain', async () => {60    await usingApi(async (api: ApiPromise) => {61      const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});62      const itemsListIndexBefore = await api.query.nft.itemListIndex(collectionId) as unknown as BN;63      expect(itemsListIndexBefore.toNumber()).to.be.equal(0);64      const Alice = privateKey('//Alice');65      const args = [66        {refungible: {const_data: [0x31], variable_data: [0x31], pieces: 1}},67        {refungible: {const_data: [0x32], variable_data: [0x32], pieces: 1}},68        {refungible: {const_data: [0x33], variable_data: [0x33], pieces: 1}},69      ];70      const createMultipleItemsTx = await api.tx.nft71        .createMultipleItems(collectionId, Alice.address, args);72      await submitTransactionAsync(Alice, createMultipleItemsTx);73      const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;74      expect(itemsListIndexAfter.toNumber()).to.be.equal(3);75      const token1Data = await api.query.nft.reFungibleItemList(collectionId, 1) as unknown as IReFungibleTokenDataType;76      const token2Data = await api.query.nft.reFungibleItemList(collectionId, 2) as unknown as IReFungibleTokenDataType;77      const token3Data = await api.query.nft.reFungibleItemList(collectionId, 3) as unknown as IReFungibleTokenDataType;7879      expect(token1Data.Owner[0].Owner.toString()).to.be.equal(Alice.address);80      expect(token1Data.Owner[0].Fraction.toNumber()).to.be.equal(1);8182      expect(token2Data.Owner[0].Owner.toString()).to.be.equal(Alice.address);83      expect(token2Data.Owner[0].Fraction.toNumber()).to.be.equal(1);8485      expect(token3Data.Owner[0].Owner.toString()).to.be.equal(Alice.address);86      expect(token3Data.Owner[0].Fraction.toNumber()).to.be.equal(1);8788      expect(token1Data.ConstData.toString()).to.be.equal('0x31');89      expect(token2Data.ConstData.toString()).to.be.equal('0x32');90      expect(token3Data.ConstData.toString()).to.be.equal('0x33');9192      expect(token1Data.VariableData.toString()).to.be.equal('0x31');93      expect(token2Data.VariableData.toString()).to.be.equal('0x32');94      expect(token3Data.VariableData.toString()).to.be.equal('0x33');95    });96  });9798  it('Can mint amount of items equals to collection limits', async () => {99    await usingApi(async (api) => {100      const alice = privateKey('//Alice');101102      const collectionId = await createCollectionExpectSuccess();103      await setCollectionLimitsExpectSuccess(alice, collectionId, {104        TokenLimit: 2,105      });106      const args = [107        { nft: ['A', 'A'] },108        { nft: ['B', 'B'] },109      ];110      const createMultipleItemsTx = api.tx.nft.createMultipleItems(collectionId, alice.address, args);111      const events = await submitTransactionAsync(alice, createMultipleItemsTx);112      const result = getGenericResult(events);113      expect(result.success).to.be.true;114    });115  });116});117118describe('Negative Integration Test createMultipleItems(collection_id, owner, items_data):', () => {119  it('Create token with not existing type', async () => {120    await usingApi(async (api: ApiPromise) => {121      const collectionId = await createCollectionExpectSuccess();122      const Alice = privateKey('//Alice');123      try {124        const args = [{ invalid: null }, { invalid: null }, { invalid: null }];125        const createMultipleItemsTx = await api.tx.nft126          .createMultipleItems(collectionId, Alice.address, args);127        await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;128      } catch (e) {129        // tslint:disable-next-line:no-unused-expression130        expect(e).to.be.exist;131      }132    });133  });134135  it('Create token in not existing collection', async () => {136    await usingApi(async (api: ApiPromise) => {137      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;138      const Alice = privateKey('//Alice');139      const createMultipleItemsTx = await api.tx.nft140        .createMultipleItems(collectionId, Alice.address, ['NFT', 'NFT', 'NFT']);141      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;142    });143  });144145  it('Create NFT and Re-fungible tokens that has reached the maximum data limit', async () => {146    await usingApi(async (api: ApiPromise) => {147      // NFT148      const collectionId = await createCollectionExpectSuccess();149      const Alice = privateKey('//Alice');150      const args = [151        { nft: ['A'.repeat(2049), 'A'.repeat(2049)] },152        { nft: ['B'.repeat(2049), 'B'.repeat(2049)] },153        { nft: ['C'.repeat(2049), 'C'.repeat(2049)] },154      ];155      const createMultipleItemsTx = await api.tx.nft156        .createMultipleItems(collectionId, Alice.address, args);157      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;158159      // ReFungible160      const collectionIdReFungible =161        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});162      const argsReFungible = [163        { ReFungible: ['1'.repeat(2049), '1'.repeat(2049)] },164        { ReFungible: ['2'.repeat(2049), '2'.repeat(2049)] },165        { ReFungible: ['3'.repeat(2049), '3'.repeat(2049)] },166      ];167      const createMultipleItemsTxFungible = await api.tx.nft168        .createMultipleItems(collectionIdReFungible, Alice.address, argsReFungible);169      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTxFungible)).to.be.rejected;170    });171  });172173  it('Create tokens with different types', async () => {174    await usingApi(async (api: ApiPromise) => {175      const collectionId = await createCollectionExpectSuccess();176      const Alice = privateKey('//Alice');177      const createMultipleItemsTx = await api.tx.nft178        .createMultipleItems(collectionId, Alice.address, ['NFT', 'Fungible', 'ReFungible']);179      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;180      // garbage collection :-D181      await destroyCollectionExpectSuccess(collectionId);182    });183  });184185  it('Create tokens with different data limits <> maximum data limit', async () => {186    await usingApi(async (api: ApiPromise) => {187      const collectionId = await createCollectionExpectSuccess();188      const Alice = privateKey('//Alice');189      const args = [190        { nft: ['A', 'A'] },191        { nft: ['B', 'B'.repeat(2049)] },192        { nft: ['C'.repeat(2049), 'C'] },193      ];194      const createMultipleItemsTx = await api.tx.nft195        .createMultipleItems(collectionId, Alice.address, args);196      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;197    });198  });199200  it('Fails when minting tokens exceeds collectionLimits amount', async () => {201    await usingApi(async (api) => {202      const alice = privateKey('//Alice');203204      const collectionId = await createCollectionExpectSuccess();205      await setCollectionLimitsExpectSuccess(alice, collectionId, {206        TokenLimit: 1,207      });208      const args = [209        { nft: ['A', 'A'] },210        { nft: ['B', 'B'] },211      ];212      const createMultipleItemsTx = api.tx.nft.createMultipleItems(collectionId, alice.address, args);213      await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;214    });215  });216});