123456import chai from 'chai';7import chaiAsPromised from 'chai-as-promised';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { ApiPromise, Keyring } from "@polkadot/api";10import { default as usingApi, submitTransactionAsync } from "../substrate/substrate-api";11import privateKey from '../substrate/privateKey';12import { alicesPublicKey } from "../accounts";13import { strToUTF16, utf16ToStr, hexToStr } from '../util/util';14import { IKeyringPair } from "@polkadot/types/types";15import { BigNumber } from 'bignumber.js';1617chai.use(chaiAsPromised);18const expect = chai.expect;1920type GenericResult = {21 success: boolean,22};2324type CreateCollectionResult = {25 success: boolean,26 collectionId: number27};2829export function getGenericResult(events: EventRecord[]): GenericResult {30 let result: GenericResult = {31 success: false32 }33 events.forEach(({ phase, event: { data, method, section } }) => {34 35 if (method == 'ExtrinsicSuccess') {36 result.success = true;37 }38 });39 return result;40}4142function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {43 let success = false;44 let collectionId: number = 0;45 events.forEach(({ phase, event: { data, method, section } }) => {46 47 if (method == 'ExtrinsicSuccess') {48 success = true;49 } else if ((section == 'nft') && (method == 'Created')) {50 collectionId = parseInt(data[0].toString());51 }52 });53 let result: CreateCollectionResult = {54 success,55 collectionId56 }57 return result;58}5960export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string) {61 await usingApi(async (api) => {62 63 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6465 66 const alicePrivateKey = privateKey('//Alice');67 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);68 const events = await submitTransactionAsync(alicePrivateKey, tx);69 const result = getCreateCollectionResult(events);7071 72 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());7374 75 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();7677 78 expect(result.success).to.be.true;79 expect(result.collectionId).to.be.equal(BcollectionCount);80 expect(collection).to.be.not.null;81 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');82 expect(collection.Owner).to.be.equal(alicesPublicKey);83 expect(utf16ToStr(collection.Name)).to.be.equal(name);84 expect(utf16ToStr(collection.Description)).to.be.equal(description);85 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);86 });87}88 89export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {90 await usingApi(async (api) => {91 92 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9394 95 const alicePrivateKey = privateKey('//Alice');96 const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);97 const events = await submitTransactionAsync(alicePrivateKey, tx);98 const result = getCreateCollectionResult(events);99100 101 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());102103 104 expect(result.success).to.be.false;105 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');106 });107}108 109export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {110 let bal = new BigNumber(0);111 let unused;112 do {113 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));114 const keyring = new Keyring({ type: 'sr25519' });115 unused = keyring.addFromUri(`//${randomSeed}`);116 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());117 } while (bal.toFixed() != '0');118 return unused; 119}