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

difftreelog

source

tests/src/util/helpers.ts29.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//56import { ApiPromise, Keyring } from '@polkadot/api';7import { Enum, Struct } from '@polkadot/types/codec';8import type { AccountId, EventRecord } from '@polkadot/types/interfaces';9import { u128 } from '@polkadot/types/primitive';10import { IKeyringPair } from '@polkadot/types/types';11import { BigNumber } from 'bignumber.js';12import BN from 'bn.js';13import chai from 'chai';14import chaiAsPromised from 'chai-as-promised';15import { alicesPublicKey, nullPublicKey } from '../accounts';16import privateKey from '../substrate/privateKey';17import { default as usingApi, submitTransactionAsync, submitTransactionExpectFailAsync } from '../substrate/substrate-api';18import { ICollectionInterface } from '../types';19import { hexToStr, strToUTF16, utf16ToStr } from './util';2021chai.use(chaiAsPromised);22const expect = chai.expect;2324export const U128_MAX = (1n << 128n) - 1n;2526type GenericResult = {27  success: boolean,28};2930interface CreateCollectionResult {31  success: boolean;32  collectionId: number;33}3435interface CreateItemResult {36  success: boolean;37  collectionId: number;38  itemId: number;39}4041interface IReFungibleOwner {42  Fraction: BN;43  Owner: number[];44}4546interface ITokenDataType {47  Owner: number[];48  ConstData: number[];49  VariableData: number[];50}5152interface IFungibleTokenDataType {53  Value: BN;54}5556export interface IReFungibleTokenDataType {57  Owner: IReFungibleOwner[];58  ConstData: number[];59  VariableData: number[];60}6162export function getGenericResult(events: EventRecord[]): GenericResult {63  const result: GenericResult = {64    success: false,65  };66  events.forEach(({ phase, event: { data, method, section } }) => {67    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);68    if (method === 'ExtrinsicSuccess') {69      result.success = true;70    }71  });72  return result;73}7475export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {76  let success = false;77  let collectionId: number = 0;78  events.forEach(({ phase, event: { data, method, section } }) => {79    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);80    if (method == 'ExtrinsicSuccess') {81      success = true;82    } else if ((section == 'nft') && (method == 'Created')) {83      collectionId = parseInt(data[0].toString());84    }85  });86  const result: CreateCollectionResult = {87    success,88    collectionId,89  };90  return result;91}9293export function getCreateItemResult(events: EventRecord[]): CreateItemResult {94  let success = false;95  let collectionId: number = 0;96  let itemId: number = 0;97  events.forEach(({ phase, event: { data, method, section } }) => {98    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);99    if (method == 'ExtrinsicSuccess') {100      success = true;101    } else if ((section == 'nft') && (method == 'ItemCreated')) {102      collectionId = parseInt(data[0].toString());103      itemId = parseInt(data[1].toString());104    }105  });106  const result: CreateItemResult = {107    success,108    collectionId,109    itemId,110  };111  return result;112}113114interface Invalid {115  type: 'Invalid';116}117118interface Nft {119  type: 'NFT';120}121122interface Fungible {123  type: 'Fungible';124  decimalPoints: number;125}126127interface ReFungible {128  type: 'ReFungible';129  decimalPoints: number;130}131132interface Nft {133  type: 'NFT'134}135136interface Fungible {137  type: 'Fungible',138  decimalPoints: number139}140141interface ReFungible {142  type: 'ReFungible',143  decimalPoints: number144}145146type CollectionMode = Nft | Fungible | ReFungible | Invalid;147148export type CreateCollectionParams = {149  mode: CollectionMode,150  name: string,151  description: string,152  tokenPrefix: string,153};154155const defaultCreateCollectionParams: CreateCollectionParams = {156  description: 'description',157  mode: { type: 'NFT' },158  name: 'name',159  tokenPrefix: 'prefix',160}161162export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {163  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};164165  let collectionId: number = 0;166  await usingApi(async (api) => {167    // Get number of collections before the transaction168    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);169170    // Run the CreateCollection transaction171    const alicePrivateKey = privateKey('//Alice');172173    let modeprm = {};174    if (mode.type === 'NFT') {175      modeprm = {nft: null};176    } else if (mode.type === 'Fungible') {177      modeprm = {fungible: mode.decimalPoints};178    } else if (mode.type === 'ReFungible') {179      modeprm = {refungible: mode.decimalPoints};180    } else if (mode.type === 'Invalid') {181      modeprm = {invalid: null};182    }183184    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);185    const events = await submitTransactionAsync(alicePrivateKey, tx);186    const result = getCreateCollectionResult(events);187188    // Get number of collections after the transaction189    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);190191    // Get the collection192    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();193194    // What to expect195    // tslint:disable-next-line:no-unused-expression196    expect(result.success).to.be.true;197    expect(result.collectionId).to.be.equal(BcollectionCount);198    // tslint:disable-next-line:no-unused-expression199    expect(collection).to.be.not.null;200    expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');201    expect(collection.Owner).to.be.equal(alicesPublicKey);202    expect(utf16ToStr(collection.Name)).to.be.equal(name);203    expect(utf16ToStr(collection.Description)).to.be.equal(description);204    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);205206    collectionId = result.collectionId;207  });208209  return collectionId;210}211212export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {213  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};214215  let modeprm = {};216  if (mode.type === 'NFT') {217    modeprm = {nft: null};218  } else if (mode.type === 'Fungible') {219    modeprm = {fungible: mode.decimalPoints};220  } else if (mode.type === 'ReFungible') {221    modeprm = {refungible: mode.decimalPoints};222  } else if (mode.type === 'Invalid') {223    modeprm = {invalid: null};224  }225226  await usingApi(async (api) => {227    // Get number of collections before the transaction228    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());229230    // Run the CreateCollection transaction231    const alicePrivateKey = privateKey('//Alice');232    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);233    const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;234    const result = getCreateCollectionResult(events);235236    // Get number of collections after the transaction237    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());238239    // What to expect240    // tslint:disable-next-line:no-unused-expression241    expect(result.success).to.be.false;242    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');243  });244}245246export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {247  let bal = new BigNumber(0);248  let unused;249  do {250    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));251    const keyring = new Keyring({ type: 'sr25519' });252    unused = keyring.addFromUri(`//${randomSeed}`);253    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());254  } while (bal.toFixed() != '0');255  return unused;256}257258export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {259  return await usingApi(async (api) => {260    const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;261    return BigInt(bn.toString());262  });263}264265export async function findNotExistingCollection(api: ApiPromise): Promise<number> {266  const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;267  const newCollection: number = totalNumber + 1;268  return newCollection;269}270271function getDestroyResult(events: EventRecord[]): boolean {272  let success: boolean = false;273  events.forEach(({ phase, event: { data, method, section } }) => {274    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);275    if (method == 'ExtrinsicSuccess') {276      success = true;277    }278  });279  return success;280}281282export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {283  await usingApi(async (api) => {284    // Run the DestroyCollection transaction285    const alicePrivateKey = privateKey(senderSeed);286    const tx = api.tx.nft.destroyCollection(collectionId);287    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;288  });289}290291export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {292  await usingApi(async (api) => {293    // Run the DestroyCollection transaction294    const alicePrivateKey = privateKey(senderSeed);295    const tx = api.tx.nft.destroyCollection(collectionId);296    const events = await submitTransactionAsync(alicePrivateKey, tx);297    const result = getDestroyResult(events);298299    // Get the collection300    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();301302    // What to expect303    expect(result).to.be.true;304    expect(collection).to.be.not.null;305    expect(collection.Owner).to.be.equal(nullPublicKey);306  });307}308309export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {310  await usingApi(async (api) => {311312    // Run the transaction313    const alicePrivateKey = privateKey('//Alice');314    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);315    const events = await submitTransactionAsync(alicePrivateKey, tx);316    const result = getGenericResult(events);317318    // Get the collection319    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();320321    // What to expect322    expect(result.success).to.be.true;323    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());324    expect(collection.SponsorConfirmed).to.be.false;325  });326}327328export async function removeCollectionSponsorExpectSuccess(collectionId: number) {329  await usingApi(async (api) => {330331    // Run the transaction332    const alicePrivateKey = privateKey('//Alice');333    const tx = api.tx.nft.removeCollectionSponsor(collectionId);334    const events = await submitTransactionAsync(alicePrivateKey, tx);335    const result = getGenericResult(events);336337    // Get the collection338    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();339340    // What to expect341    expect(result.success).to.be.true;342    expect(collection.Sponsor).to.be.equal(nullPublicKey);343    expect(collection.SponsorConfirmed).to.be.false;344  });345}346347export async function removeCollectionSponsorExpectFailure(collectionId: number) {348  await usingApi(async (api) => {349350    // Run the transaction351    const alicePrivateKey = privateKey('//Alice');352    const tx = api.tx.nft.removeCollectionSponsor(collectionId);353    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;354  });355}356357export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {358  await usingApi(async (api) => {359360    // Run the transaction361    const alicePrivateKey = privateKey(senderSeed);362    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);363    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;364  });365}366367export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {368  await usingApi(async (api) => {369370    // Run the transaction371    const sender = privateKey(senderSeed);372    const tx = api.tx.nft.confirmSponsorship(collectionId);373    const events = await submitTransactionAsync(sender, tx);374    const result = getGenericResult(events);375376    // Get the collection377    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();378379    // What to expect380    expect(result.success).to.be.true;381    expect(collection.Sponsor).to.be.equal(sender.address);382    expect(collection.SponsorConfirmed).to.be.true;383  });384}385386export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {387  await usingApi(async (api) => {388389    // Run the transaction390    const sender = privateKey(senderSeed);391    const tx = api.tx.nft.confirmSponsorship(collectionId);392    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;393  });394}395396export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {397  await usingApi(async (api) => {398    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);399    const events = await submitTransactionAsync(sender, tx);400    const result = getGenericResult(events);401402    expect(result.success).to.be.true;403  });404}405406export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {407  await usingApi(async (api) => {408    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);409    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;410    const result = getGenericResult(events);411412    expect(result.success).to.be.false;413  });414}415416export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {417  await usingApi(async (api) => {418    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);419    const events = await submitTransactionAsync(sender, tx);420    const result = getGenericResult(events);421422    expect(result.success).to.be.true;423  });424}425426export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {427  await usingApi(async (api) => {428    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);429    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;430    const result = getGenericResult(events);431432    expect(result.success).to.be.false;433  });434}435436export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {437  await usingApi(async (api) => {438    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));439    const events = await submitTransactionAsync(sender, tx);440    const result = getGenericResult(events);441442    expect(result.success).to.be.true;443  });444}445446export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {447  await usingApi(async (api) => {448    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));449    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;450  });451}452453export interface CreateFungibleData {454  readonly Value: bigint;455}456457export interface CreateReFungibleData { }458export interface CreateNftData { }459460export type CreateItemData = {461  NFT: CreateNftData;462} | {463  Fungible: CreateFungibleData;464} | {465  ReFungible: CreateReFungibleData;466};467468export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {469  await usingApi(async (api) => {470    const tx = api.tx.nft.burnItem(collectionId, tokenId, value);471    const events = await submitTransactionAsync(owner, tx);472    const result = getGenericResult(events);473    // Get the item474    const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();475    // What to expect476    // tslint:disable-next-line:no-unused-expression477    expect(result.success).to.be.true;478    // tslint:disable-next-line:no-unused-expression479    expect(item).to.be.not.null;480    expect(item.Owner).to.be.equal(nullPublicKey);481  });482}483484export async function485approveExpectSuccess(collectionId: number,486                     tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {487  await usingApi(async (api: ApiPromise) => {488    const allowanceBefore =489      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;490    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);491    const events = await submitTransactionAsync(owner, approveNftTx);492    const result = getCreateItemResult(events);493    // tslint:disable-next-line:no-unused-expression494    expect(result.success).to.be.true;495    const allowanceAfter =496      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;497    expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());498  });499}500501export async function502transferFromExpectSuccess(collectionId: number,503                          tokenId: number,504                          accountApproved: IKeyringPair,505                          accountFrom: IKeyringPair,506                          accountTo: IKeyringPair,507                          value: number | bigint = 1,508                          type: string = 'NFT') {509  await usingApi(async (api: ApiPromise) => {510    let balanceBefore = new BN(0);511    if (type === 'Fungible') {512      balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;513    }514    const transferFromTx = await api.tx.nft.transferFrom(515      accountFrom.address, accountTo.address, collectionId, tokenId, value);516    const events = await submitTransactionAsync(accountApproved, transferFromTx);517    const result = getCreateItemResult(events);518    // tslint:disable-next-line:no-unused-expression519    expect(result.success).to.be.true;520    if (type === 'NFT') {521      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;522      expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);523    }524    if (type === 'Fungible') {525      const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;526      expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());527    }528    if (type === 'ReFungible') {529      const nftItemData =530        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;531      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);532      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);533    }534  });535}536537export async function538transferFromExpectFail(collectionId: number,539                       tokenId: number,540                       accountApproved: IKeyringPair,541                       accountFrom: IKeyringPair,542                       accountTo: IKeyringPair,543                       value: number | bigint = 1) {544  await usingApi(async (api: ApiPromise) => {545    const transferFromTx = await api.tx.nft.transferFrom(546      accountFrom.address, accountTo.address, collectionId, tokenId, value);547    const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;548    const result = getCreateCollectionResult(events);549    // tslint:disable-next-line:no-unused-expression550    expect(result.success).to.be.false;551  });552}553554export async function555transferExpectSuccess(collectionId: number,556                      tokenId: number,557                      sender: IKeyringPair,558                      recipient: IKeyringPair,559                      value: number | bigint = 1,560                      type: string = 'NFT') {561  await usingApi(async (api: ApiPromise) => {562    let balanceBefore = new BN(0);563    if (type === 'Fungible') {564      balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;565    }566    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);567    const events = await submitTransactionAsync(sender, transferTx);568    const result = getCreateItemResult(events);569    // tslint:disable-next-line:no-unused-expression570    expect(result.success).to.be.true;571    if (type === 'NFT') {572      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;573      expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);574    }575    if (type === 'Fungible') {576      const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;577      expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());578    }579    if (type === 'ReFungible') {580      const nftItemData =581        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;582      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);583      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);584    }585  });586}587588export async function589transferExpectFail(collectionId: number,590                   tokenId: number,591                   sender: IKeyringPair,592                   recipient: IKeyringPair,593                   value: number = 1,594                   type: string = 'NFT') {595  await usingApi(async (api: ApiPromise) => {596    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);597    const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;598    if (events && Array.isArray(events)) {599      const result = getCreateCollectionResult(events);600      // tslint:disable-next-line:no-unused-expression601      expect(result.success).to.be.false;602    }603  });604}605606export async function607approveExpectFail(collectionId: number,608                  tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {609  await usingApi(async (api: ApiPromise) => {610    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);611    const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;612    const result = getCreateCollectionResult(events);613    // tslint:disable-next-line:no-unused-expression614    expect(result.success).to.be.false;615  });616}617618export async function createItemExpectSuccess(619  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {620  let newItemId: number = 0;621  await usingApi(async (api) => {622    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);623    const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();624    const AItemBalance = new BigNumber(Aitem.Value);625626    if (owner === '') {627      owner = sender.address;628    }629630    let tx;631    if (createMode === 'Fungible') {632      const createData = {fungible: {value: 10}};633      tx = api.tx.nft.createItem(collectionId, owner, createData);634    } else {635      tx = api.tx.nft.createItem(collectionId, owner, createMode);636    }637    const events = await submitTransactionAsync(sender, tx);638    const result = getCreateItemResult(events);639640    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);641    const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();642    const BItemBalance = new BigNumber(Bitem.Value);643644    // What to expect645    // tslint:disable-next-line:no-unused-expression646    expect(result.success).to.be.true;647    if (createMode === 'Fungible') {648      expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);649    } else {650      expect(BItemCount).to.be.equal(AItemCount + 1);651    }652    expect(collectionId).to.be.equal(result.collectionId);653    expect(BItemCount).to.be.equal(result.itemId);654    newItemId = result.itemId;655  });656  return newItemId;657}658659export async function createItemExpectFailure(660  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {661  await usingApi(async (api) => {662    const tx = api.tx.nft.createItem(collectionId, owner, createMode);663    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;664    const result = getCreateItemResult(events);665666    expect(result.success).to.be.false;667  });668}669670export async function setPublicAccessModeExpectSuccess(671  sender: IKeyringPair, collectionId: number,672  accessMode: 'Normal' | 'WhiteList',673) {674  await usingApi(async (api) => {675676    // Run the transaction677    const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);678    const events = await submitTransactionAsync(sender, tx);679    const result = getGenericResult(events);680681    // Get the collection682    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();683684    // What to expect685    // tslint:disable-next-line:no-unused-expression686    expect(result.success).to.be.true;687    expect(collection.Access).to.be.equal(accessMode);688  });689}690691export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {692  await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');693}694695export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {696  await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');697}698699export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {700  await usingApi(async (api) => {701702    // Run the transaction703    const tx = api.tx.nft.setMintPermission(collectionId, enabled);704    const events = await submitTransactionAsync(sender, tx);705    const result = getGenericResult(events);706707    // Get the collection708    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();709710    // What to expect711    // tslint:disable-next-line:no-unused-expression712    expect(result.success).to.be.true;713    expect(collection.MintMode).to.be.equal(enabled);714  });715}716717export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {718  await setMintPermissionExpectSuccess(sender, collectionId, true);719}720721export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {722  await usingApi(async (api) => {723    // Run the transaction724    const tx = api.tx.nft.setMintPermission(collectionId, enabled);725    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;726    const result = getCreateCollectionResult(events);727    // tslint:disable-next-line:no-unused-expression728    expect(result.success).to.be.false;729  });730}731732export async function isWhitelisted(collectionId: number, address: string) {733  let whitelisted: boolean = false;734  await usingApi(async (api) => {735    whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;736  });737  return whitelisted;738}739740export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {741  await usingApi(async (api) => {742743    const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();744745    // Run the transaction746    const tx = api.tx.nft.addToWhiteList(collectionId, address);747    const events = await submitTransactionAsync(sender, tx);748    const result = getGenericResult(events);749750    const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();751752    // What to expect753    // tslint:disable-next-line:no-unused-expression754    expect(result.success).to.be.true;755    // tslint:disable-next-line: no-unused-expression756    expect(whiteListedBefore).to.be.false;757    // tslint:disable-next-line: no-unused-expression758    expect(whiteListedAfter).to.be.true;759  });760}761762export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {763  await usingApi(async (api) => {764    // Run the transaction765    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);766    const events = await submitTransactionAsync(sender, tx);767    const result = getGenericResult(events);768769    // What to expect770    // tslint:disable-next-line:no-unused-expression771    expect(result.success).to.be.true;772  });773}774775export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {776  await usingApi(async (api) => {777    // Run the transaction778    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);779    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;780    const result = getGenericResult(events);781782    // What to expect783    // tslint:disable-next-line:no-unused-expression784    expect(result.success).to.be.false;785  });786}787788export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)789  : Promise<ICollectionInterface | null> => {790  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;791};792793export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {794  // set global object - collectionsCount795  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();796};