1import chai from 'chai';2import chaiAsPromised from 'chai-as-promised';3import type { EventRecord } from '@polkadot/types/interfaces';4import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";5import privateKey from '../substrate/privateKey';6import { alicesPublicKey } from "../accounts";7import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';89chai.use(chaiAsPromised);10const expect = chai.expect;1112type GenericResult = {13 success: boolean,14};1516type CreateCollectionResult = {17 success: boolean,18 collectionId: number19};2021export function getGenericResult(events: EventRecord[]): GenericResult {22 let result: GenericResult = {23 success: false24 }25 events.forEach(({ phase, event: { data, method, section } }) => {26 27 if (method == 'ExtrinsicSuccess') {28 result.success = true;29 }30 });31 return result;32}3334function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {35 let success = false;36 let collectionId: number = 0;37 events.forEach(({ phase, event: { data, method, section } }) => {38 39 if (method == 'ExtrinsicSuccess') {40 success = true;41 } else if ((section == 'nft') && (method == 'Created')) {42 collectionId = parseInt(data[0].toString());43 }44 });45 let result: CreateCollectionResult = {46 success,47 collectionId48 }49 return result;50}5152export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string) {53 await usingApi(async (api) => {54 55 const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());5657 58 const alicePrivateKey = privateKey('//Alice');59 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);60 const events = await submitTransactionAsync(alicePrivateKey, tx);61 const result = getCreateCollectionResult(events);6263 64 const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());6566 67 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();6869 70 expect(result.success).to.be.true;71 expect(result.collectionId).to.be.equal(BcollectionCount);72 expect(collection).to.be.not.null;73 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');74 expect(collection.Owner).to.be.equal(alicesPublicKey);75 expect(utf16ToStr(collection.Name)).to.be.equal(name);76 expect(utf16ToStr(collection.Description)).to.be.equal(description);77 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);78 });79}80 81export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {82 await usingApi(async (api) => {83 84 const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());8586 87 const alicePrivateKey = privateKey('//Alice');88 const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);89 const events = await submitTransactionAsync(alicePrivateKey, tx);90 const result = getCreateCollectionResult(events);9192 93 const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());9495 96 expect(result.success).to.be.false;97 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');98 });99}100