git.delta.rocks / unique-network / refs/commits / 4dbeda6769b2

difftreelog

source

tests/src/createMultipleItems.test.ts10.2 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  normalizeAccountId,17  setCollectionLimitsExpectSuccess,18} from './util/helpers';1920chai.use(chaiAsPromised);21const expect = chai.expect;2223interface ITokenDataType {24  Owner: number[];25  ConstData: number[];26  VariableData: number[];27}2829describe('Integration Test createMultipleItems(collection_id, owner, items_data):', () => {30  it('Create  0x31, 0x32, 0x33 items in active NFT collection and verify tokens data in chain', async () => {31    await usingApi(async (api: ApiPromise) => {32      const collectionId = await createCollectionExpectSuccess();33      const itemsListIndexBefore = await api.query.nft.itemListIndex(collectionId) as unknown as BN;34      expect(itemsListIndexBefore.toNumber()).to.be.equal(0);35      const Alice = privateKey('//Alice');36      const args = [{ nft: ['0x31', '0x31'] }, { nft: ['0x32', '0x32'] }, { nft: ['0x33', '0x33'] }];37      const createMultipleItemsTx = api.tx.nft38        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), args);39      await submitTransactionAsync(Alice, createMultipleItemsTx);40      const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;41      expect(itemsListIndexAfter.toNumber()).to.be.equal(3);42      const token1Data = (await api.query.nft.nftItemList(collectionId, 1)).toJSON() as unknown as ITokenDataType;43      const token2Data = (await api.query.nft.nftItemList(collectionId, 2)).toJSON() as unknown as ITokenDataType;44      const token3Data = (await api.query.nft.nftItemList(collectionId, 3)).toJSON() as unknown as ITokenDataType;4546      expect(token1Data.Owner).to.be.deep.equal(normalizeAccountId(Alice.address));47      expect(token2Data.Owner).to.be.deep.equal(normalizeAccountId(Alice.address));48      expect(token3Data.Owner).to.be.deep.equal(normalizeAccountId(Alice.address));4950      expect(token1Data.ConstData.toString()).to.be.equal('0x31');51      expect(token2Data.ConstData.toString()).to.be.equal('0x32');52      expect(token3Data.ConstData.toString()).to.be.equal('0x33');5354      expect(token1Data.VariableData.toString()).to.be.equal('0x31');55      expect(token2Data.VariableData.toString()).to.be.equal('0x32');56      expect(token3Data.VariableData.toString()).to.be.equal('0x33');57    });58  });5960  it('Create  0x31, 0x32, 0x33 items in active ReFungible collection and verify tokens data in chain', async () => {61    await usingApi(async (api: ApiPromise) => {62      const collectionId = await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});63      const itemsListIndexBefore = await api.query.nft.itemListIndex(collectionId) as unknown as BN;64      expect(itemsListIndexBefore.toNumber()).to.be.equal(0);65      const Alice = privateKey('//Alice');66      const args = [67        {refungible: {const_data: [0x31], variable_data: [0x31], pieces: 1}},68        {refungible: {const_data: [0x32], variable_data: [0x32], pieces: 1}},69        {refungible: {const_data: [0x33], variable_data: [0x33], pieces: 1}},70      ];71      const createMultipleItemsTx = api.tx.nft72        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), args);73      await submitTransactionAsync(Alice, createMultipleItemsTx);74      const itemsListIndexAfter = await api.query.nft.itemListIndex(collectionId) as unknown as BN;75      expect(itemsListIndexAfter.toNumber()).to.be.equal(3);76      const token1Data = (await api.query.nft.reFungibleItemList(collectionId, 1) as any).toJSON() as unknown as IReFungibleTokenDataType;77      const token2Data = (await api.query.nft.reFungibleItemList(collectionId, 2) as any).toJSON() as unknown as IReFungibleTokenDataType;78      const token3Data = (await api.query.nft.reFungibleItemList(collectionId, 3) as any).toJSON() as unknown as IReFungibleTokenDataType;7980      expect(token1Data.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(Alice.address));81      expect(token1Data.Owner[0].Fraction).to.be.equal(1);8283      expect(token2Data.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(Alice.address));84      expect(token2Data.Owner[0].Fraction).to.be.equal(1);8586      expect(token3Data.Owner[0].Owner).to.be.deep.equal(normalizeAccountId(Alice.address));87      expect(token3Data.Owner[0].Fraction).to.be.equal(1);8889      expect(token1Data.ConstData.toString()).to.be.equal('0x31');90      expect(token2Data.ConstData.toString()).to.be.equal('0x32');91      expect(token3Data.ConstData.toString()).to.be.equal('0x33');9293      expect(token1Data.VariableData.toString()).to.be.equal('0x31');94      expect(token2Data.VariableData.toString()).to.be.equal('0x32');95      expect(token3Data.VariableData.toString()).to.be.equal('0x33');96    });97  });9899  it('Can mint amount of items equals to collection limits', async () => {100    await usingApi(async (api) => {101      const alice = privateKey('//Alice');102103      const collectionId = await createCollectionExpectSuccess();104      await setCollectionLimitsExpectSuccess(alice, collectionId, {105        TokenLimit: 2,106      });107      const args = [108        { nft: ['A', 'A'] },109        { nft: ['B', 'B'] },110      ];111      const createMultipleItemsTx = api.tx.nft.createMultipleItems(collectionId, normalizeAccountId(alice.address), args);112      const events = await submitTransactionAsync(alice, createMultipleItemsTx);113      const result = getGenericResult(events);114      expect(result.success).to.be.true;115    });116  });117});118119describe('Negative Integration Test createMultipleItems(collection_id, owner, items_data):', () => {120  it('Create token with not existing type', async () => {121    await usingApi(async (api: ApiPromise) => {122      const collectionId = await createCollectionExpectSuccess();123      const Alice = privateKey('//Alice');124      try {125        const args = [{ invalid: null }, { invalid: null }, { invalid: null }];126        const createMultipleItemsTx = await api.tx.nft127          .createMultipleItems(collectionId, normalizeAccountId(Alice.address), args);128        await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;129      } catch (e) {130        // tslint:disable-next-line:no-unused-expression131        expect(e).to.be.exist;132      }133    });134  });135136  it('Create token in not existing collection', async () => {137    await usingApi(async (api: ApiPromise) => {138      const collectionId = parseInt((await api.query.nft.createdCollectionCount()).toString()) + 1;139      const Alice = privateKey('//Alice');140      const createMultipleItemsTx = api.tx.nft141        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), ['NFT', 'NFT', 'NFT']);142      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;143    });144  });145146  it('Create NFT and Re-fungible tokens that has reached the maximum data limit', async () => {147    await usingApi(async (api: ApiPromise) => {148      // NFT149      const collectionId = await createCollectionExpectSuccess();150      const Alice = privateKey('//Alice');151      const args = [152        { nft: ['A'.repeat(2049), 'A'.repeat(2049)] },153        { nft: ['B'.repeat(2049), 'B'.repeat(2049)] },154        { nft: ['C'.repeat(2049), 'C'.repeat(2049)] },155      ];156      const createMultipleItemsTx = api.tx.nft157        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), args);158      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;159160      // ReFungible161      const collectionIdReFungible =162        await createCollectionExpectSuccess({mode: {type: 'ReFungible'}});163      const argsReFungible = [164        { ReFungible: ['1'.repeat(2049), '1'.repeat(2049), 10] },165        { ReFungible: ['2'.repeat(2049), '2'.repeat(2049), 10] },166        { ReFungible: ['3'.repeat(2049), '3'.repeat(2049), 10] },167      ];168      const createMultipleItemsTxFungible = api.tx.nft169        .createMultipleItems(collectionIdReFungible, normalizeAccountId(Alice.address), argsReFungible);170      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTxFungible)).to.be.rejected;171    });172  });173174  it('Create tokens with different types', async () => {175    await usingApi(async (api: ApiPromise) => {176      const collectionId = await createCollectionExpectSuccess();177      const Alice = privateKey('//Alice');178      const createMultipleItemsTx = api.tx.nft179        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), ['NFT', 'Fungible', 'ReFungible']);180      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;181      // garbage collection :-D182      await destroyCollectionExpectSuccess(collectionId);183    });184  });185186  it('Create tokens with different data limits <> maximum data limit', async () => {187    await usingApi(async (api: ApiPromise) => {188      const collectionId = await createCollectionExpectSuccess();189      const Alice = privateKey('//Alice');190      const args = [191        { nft: ['A', 'A'] },192        { nft: ['B', 'B'.repeat(2049)] },193        { nft: ['C'.repeat(2049), 'C'] },194      ];195      const createMultipleItemsTx = await api.tx.nft196        .createMultipleItems(collectionId, normalizeAccountId(Alice.address), args);197      await expect(submitTransactionExpectFailAsync(Alice, createMultipleItemsTx)).to.be.rejected;198    });199  });200201  it('Fails when minting tokens exceeds collectionLimits amount', async () => {202    await usingApi(async (api) => {203      const alice = privateKey('//Alice');204205      const collectionId = await createCollectionExpectSuccess();206      await setCollectionLimitsExpectSuccess(alice, collectionId, {207        TokenLimit: 1,208      });209      const args = [210        { nft: ['A', 'A'] },211        { nft: ['B', 'B'] },212      ];213      const createMultipleItemsTx = api.tx.nft.createMultipleItems(collectionId, normalizeAccountId(alice.address), args);214      await expect(submitTransactionExpectFailAsync(alice, createMultipleItemsTx)).to.be.rejected;215    });216  });217});