git.delta.rocks / unique-network / refs/commits / 876e077a7bb7

difftreelog

source

tests/src/util/helpers.ts3.4 KiBsourcehistory
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 CreateCollectionResult = {13  success: boolean,14  collectionId: number15};1617function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {18  let success = false;19  let collectionId: number = 0;20  events.forEach(({ phase, event: { data, method, section } }) => {21    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);22    if (method == 'ExtrinsicSuccess') {23      success = true;24    } else if ((section == 'nft') && (method == 'Created')) {25      collectionId = parseInt(data[0].toString());26    }27  });28  let result: CreateCollectionResult = {29    success,30    collectionId31  }32  return result;33}3435export async function createCollectionExpectSuccess(name: string, description: string, tokenPrefix: string, mode: string) {36  await usingApi(async (api) => {37    // Get number of collections before the transaction38    const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());3940    // Run the CreateCollection transaction41    const alicePrivateKey = privateKey('//Alice');42    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), mode);43    const events = await submitTransactionAsync(alicePrivateKey, tx);44    const result = getCreateCollectionResult(events);4546    // Get number of collections after the transaction47    const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());4849    // Get the collection 50    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();5152    // What to expect53    expect(result.success).to.be.true;54    expect(result.collectionId).to.be.equal(BcollectionCount);55    expect(collection).to.be.not.null;56    expect(BcollectionCount).to.be.equal(AcollectionCount+1, 'Error: NFT collection NOT created.');57    expect(collection.Owner).to.be.equal(alicesPublicKey);58    expect(utf16ToStr(collection.Name)).to.be.equal(name);59    expect(utf16ToStr(collection.Description)).to.be.equal(description);60    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);61  });62}63  64export async function createCollectionExpectFailure(name: string, description: string, tokenPrefix: string, mode: string) {65  await usingApi(async (api) => {66    // Get number of collections before the transaction67    const AcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());6869    // Run the CreateCollection transaction70    const alicePrivateKey = privateKey('//Alice');71    const tx = api.tx.nft.createCollection(name, description, tokenPrefix, mode);72    const events = await submitTransactionAsync(alicePrivateKey, tx);73    const result = getCreateCollectionResult(events);7475    // Get number of collections after the transaction76    const BcollectionCount = parseInt((await api.query.nft.collectionCount()).toString());7778    // What to expect79    expect(result.success).to.be.false;80    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');81  });82}83