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): Promise<number> {61 let collectionId: number = 0;62 await usingApi(async (api) => {63 64 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());6566 67 const alicePrivateKey = privateKey('//Alice');68 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);69 const events = await submitTransactionAsync(alicePrivateKey, tx);70 const result = getCreateCollectionResult(events);7172 73 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());7475 76 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();7778 79 expect(result.success).to.be.true;80 expect(result.collectionId).to.be.equal(BcollectionCount);81 expect(collection).to.be.not.null;82 expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');83 expect(collection.Owner).to.be.equal(alicesPublicKey);84 expect(utf16ToStr(collection.Name)).to.be.equal(name);85 expect(utf16ToStr(collection.Description)).to.be.equal(description);86 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);8788 collectionId = result.collectionId;89 });9091 return collectionId;92}93 94export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {95 await usingApi(async (api) => {96 97 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());9899 100 const alicePrivateKey = privateKey('//Alice');101 const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);102 const events = await submitTransactionAsync(alicePrivateKey, tx);103 const result = getCreateCollectionResult(events);104105 106 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());107108 109 expect(result.success).to.be.false;110 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');111 });112}113 114export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {115 let bal = new BigNumber(0);116 let unused;117 do {118 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));119 const keyring = new Keyring({ type: 'sr25519' });120 unused = keyring.addFromUri(`//${randomSeed}`);121 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());122 } while (bal.toFixed() != '0');123 return unused; 124}125126export async function createItemExpectSuccess(collectionId: number, createMode: string, senderSeed: string = '//Alice') {127 await usingApi(async (api) => {128129 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());130131 const sender = privateKey(senderSeed);132 const tx = api.tx.nft.createItem(collectionId, sender.address, createMode);133 const events = await submitTransactionAsync(sender, tx);134 const result = getGenericResult(events);135 136 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString());137138 139 expect(result.success).to.be.true;140 expect(BItemCount).to.be.equal(AItemCount+1);141 });142}