git.delta.rocks / unique-network / refs/commits / 8a4b6c870c52

difftreelog

source

tests/src/util/helpers.ts28.9 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 findNotExistingCollection(api: ApiPromise): Promise<number> {259  const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;260  const newCollection: number = totalNumber + 1;261  return newCollection;262}263264function getDestroyResult(events: EventRecord[]): boolean {265  let success: boolean = false;266  events.forEach(({ phase, event: { data, method, section } }) => {267    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);268    if (method == 'ExtrinsicSuccess') {269      success = true;270    }271  });272  return success;273}274275export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {276  await usingApi(async (api) => {277    // Run the DestroyCollection transaction278    const alicePrivateKey = privateKey(senderSeed);279    const tx = api.tx.nft.destroyCollection(collectionId);280    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;281  });282}283284export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {285  await usingApi(async (api) => {286    // Run the DestroyCollection transaction287    const alicePrivateKey = privateKey(senderSeed);288    const tx = api.tx.nft.destroyCollection(collectionId);289    const events = await submitTransactionAsync(alicePrivateKey, tx);290    const result = getDestroyResult(events);291292    // Get the collection293    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();294295    // What to expect296    expect(result).to.be.true;297    expect(collection).to.be.not.null;298    expect(collection.Owner).to.be.equal(nullPublicKey);299  });300}301302export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {303  await usingApi(async (api) => {304305    // Run the transaction306    const alicePrivateKey = privateKey('//Alice');307    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);308    const events = await submitTransactionAsync(alicePrivateKey, tx);309    const result = getGenericResult(events);310311    // Get the collection312    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();313314    // What to expect315    expect(result.success).to.be.true;316    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());317    expect(collection.SponsorConfirmed).to.be.false;318  });319}320321export async function removeCollectionSponsorExpectSuccess(collectionId: number) {322  await usingApi(async (api) => {323324    // Run the transaction325    const alicePrivateKey = privateKey('//Alice');326    const tx = api.tx.nft.removeCollectionSponsor(collectionId);327    const events = await submitTransactionAsync(alicePrivateKey, tx);328    const result = getGenericResult(events);329330    // Get the collection331    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();332333    // What to expect334    expect(result.success).to.be.true;335    expect(collection.Sponsor).to.be.equal(nullPublicKey);336    expect(collection.SponsorConfirmed).to.be.false;337  });338}339340export async function removeCollectionSponsorExpectFailure(collectionId: number) {341  await usingApi(async (api) => {342343    // Run the transaction344    const alicePrivateKey = privateKey('//Alice');345    const tx = api.tx.nft.removeCollectionSponsor(collectionId);346    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;347  });348}349350export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {351  await usingApi(async (api) => {352353    // Run the transaction354    const alicePrivateKey = privateKey(senderSeed);355    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);356    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;357  });358}359360export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {361  await usingApi(async (api) => {362363    // Run the transaction364    const sender = privateKey(senderSeed);365    const tx = api.tx.nft.confirmSponsorship(collectionId);366    const events = await submitTransactionAsync(sender, tx);367    const result = getGenericResult(events);368369    // Get the collection370    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();371372    // What to expect373    expect(result.success).to.be.true;374    expect(collection.Sponsor).to.be.equal(sender.address);375    expect(collection.SponsorConfirmed).to.be.true;376  });377}378379export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {380  await usingApi(async (api) => {381382    // Run the transaction383    const sender = privateKey(senderSeed);384    const tx = api.tx.nft.confirmSponsorship(collectionId);385    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;386  });387}388389export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {390  await usingApi(async (api) => {391    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);392    const events = await submitTransactionAsync(sender, tx);393    const result = getGenericResult(events);394395    expect(result.success).to.be.true;396  });397}398399export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {400  await usingApi(async (api) => {401    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);402    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;403    const result = getGenericResult(events);404405    expect(result.success).to.be.false;406  });407}408409export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {410  await usingApi(async (api) => {411    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);412    const events = await submitTransactionAsync(sender, tx);413    const result = getGenericResult(events);414415    expect(result.success).to.be.true;416  });417}418419export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {420  await usingApi(async (api) => {421    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);422    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;423    const result = getGenericResult(events);424425    expect(result.success).to.be.false;426  });427}428429export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {430  await usingApi(async (api) => {431    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));432    const events = await submitTransactionAsync(sender, tx);433    const result = getGenericResult(events);434435    expect(result.success).to.be.true;436  });437}438439export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {440  await usingApi(async (api) => {441    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));442    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;443  });444}445446export interface CreateFungibleData extends Struct {447  readonly value: u128;448}449450export interface CreateReFungibleData extends Struct {}451export interface CreateNftData extends Struct {}452453export interface CreateItemData extends Enum {454  NFT: CreateNftData;455  Fungible: CreateFungibleData;456  ReFungible: CreateReFungibleData;457}458459export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {460  await usingApi(async (api) => {461    const tx = api.tx.nft.burnItem(collectionId, tokenId, value);462    const events = await submitTransactionAsync(owner, tx);463    const result = getGenericResult(events);464    // Get the item465    const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();466    // What to expect467    // tslint:disable-next-line:no-unused-expression468    expect(result.success).to.be.true;469    // tslint:disable-next-line:no-unused-expression470    expect(item).to.be.not.null;471    expect(item.Owner).to.be.equal(nullPublicKey);472  });473}474475export async function476approveExpectSuccess(collectionId: number,477                     tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {478  await usingApi(async (api: ApiPromise) => {479    const allowanceBefore =480      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;481    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);482    const events = await submitTransactionAsync(owner, approveNftTx);483    const result = getCreateItemResult(events);484    // tslint:disable-next-line:no-unused-expression485    expect(result.success).to.be.true;486    const allowanceAfter =487      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;488    expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);489  });490}491492export async function493transferFromExpectSuccess(collectionId: number,494                          tokenId: number,495                          accountApproved: IKeyringPair,496                          accountFrom: IKeyringPair,497                          accountTo: IKeyringPair,498                          value: number = 1,499                          type: string = 'NFT') {500  await usingApi(async (api: ApiPromise) => {501    let balanceBefore = new BN(0);502    if (type === 'Fungible') {503      balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;504    }505    const transferFromTx = await api.tx.nft.transferFrom(506      accountFrom.address, accountTo.address, collectionId, tokenId, value);507    const events = await submitTransactionAsync(accountApproved, transferFromTx);508    const result = getCreateItemResult(events);509    // tslint:disable-next-line:no-unused-expression510    expect(result.success).to.be.true;511    if (type === 'NFT') {512      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;513      expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);514    }515    if (type === 'Fungible') {516      const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;517      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);518    }519    if (type === 'ReFungible') {520      const nftItemData =521        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;522      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);523      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);524    }525  });526}527528export async function529transferFromExpectFail(collectionId: number,530                       tokenId: number,531                       accountApproved: IKeyringPair,532                       accountFrom: IKeyringPair,533                       accountTo: IKeyringPair,534                       value: number = 1) {535  await usingApi(async (api: ApiPromise) => {536    const transferFromTx = await api.tx.nft.transferFrom(537      accountFrom.address, accountTo.address, collectionId, tokenId, value);538    const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;539    const result = getCreateCollectionResult(events);540    // tslint:disable-next-line:no-unused-expression541    expect(result.success).to.be.false;542  });543}544545export async function546transferExpectSuccess(collectionId: number,547                      tokenId: number,548                      sender: IKeyringPair,549                      recipient: IKeyringPair,550                      value: number = 1,551                      type: string = 'NFT') {552  await usingApi(async (api: ApiPromise) => {553    let balanceBefore = new BN(0);554    if (type === 'Fungible') {555      balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;556    }557    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);558    const events = await submitTransactionAsync(sender, transferTx);559    const result = getCreateItemResult(events);560    // tslint:disable-next-line:no-unused-expression561    expect(result.success).to.be.true;562    if (type === 'NFT') {563      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;564      expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);565    }566    if (type === 'Fungible') {567      const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;568      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);569    }570    if (type === 'ReFungible') {571      const nftItemData =572        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;573      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);574      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);575    }576  });577}578579export async function580transferExpectFail(collectionId: number,581                   tokenId: number,582                   sender: IKeyringPair,583                   recipient: IKeyringPair,584                   value: number = 1,585                   type: string = 'NFT') {586  await usingApi(async (api: ApiPromise) => {587    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);588    const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;589    if (events && Array.isArray(events)) {590      const result = getCreateCollectionResult(events);591      // tslint:disable-next-line:no-unused-expression592      expect(result.success).to.be.false;593    }594  });595}596597export async function598approveExpectFail(collectionId: number,599                  tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {600  await usingApi(async (api: ApiPromise) => {601    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);602    const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;603    const result = getCreateCollectionResult(events);604    // tslint:disable-next-line:no-unused-expression605    expect(result.success).to.be.false;606  });607}608609export async function createItemExpectSuccess(610  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {611  let newItemId: number = 0;612  await usingApi(async (api) => {613    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);614    const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();615    const AItemBalance = new BigNumber(Aitem.Value);616617    if (owner === '') {618      owner = sender.address;619    }620621    let tx;622    if (createMode === 'Fungible') {623      const createData = {fungible: {value: 10}};624      tx = api.tx.nft.createItem(collectionId, owner, createData);625    } else {626      tx = api.tx.nft.createItem(collectionId, owner, createMode);627    }628    const events = await submitTransactionAsync(sender, tx);629    const result = getCreateItemResult(events);630631    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);632    const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();633    const BItemBalance = new BigNumber(Bitem.Value);634635    // What to expect636    // tslint:disable-next-line:no-unused-expression637    expect(result.success).to.be.true;638    if (createMode === 'Fungible') {639      expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);640    } else {641      expect(BItemCount).to.be.equal(AItemCount + 1);642    }643    expect(collectionId).to.be.equal(result.collectionId);644    expect(BItemCount).to.be.equal(result.itemId);645    newItemId = result.itemId;646  });647  return newItemId;648}649650export async function createItemExpectFailure(651  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {652  await usingApi(async (api) => {653    const tx = api.tx.nft.createItem(collectionId, owner, createMode);654    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;655    const result = getCreateItemResult(events);656657    expect(result.success).to.be.false;658  });659}660661export async function setPublicAccessModeExpectSuccess(662  sender: IKeyringPair, collectionId: number,663  accessMode: 'Normal' | 'WhiteList',664) {665  await usingApi(async (api) => {666667    // Run the transaction668    const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);669    const events = await submitTransactionAsync(sender, tx);670    const result = getGenericResult(events);671672    // Get the collection673    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();674675    // What to expect676    // tslint:disable-next-line:no-unused-expression677    expect(result.success).to.be.true;678    expect(collection.Access).to.be.equal(accessMode);679  });680}681682export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {683  await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');684}685686export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {687  await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');688}689690export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {691  await usingApi(async (api) => {692693    // Run the transaction694    const tx = api.tx.nft.setMintPermission(collectionId, enabled);695    const events = await submitTransactionAsync(sender, tx);696    const result = getGenericResult(events);697698    // Get the collection699    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();700701    // What to expect702    // tslint:disable-next-line:no-unused-expression703    expect(result.success).to.be.true;704    expect(collection.MintMode).to.be.equal(enabled);705  });706}707708export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {709  await setMintPermissionExpectSuccess(sender, collectionId, true);710}711712export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {713  await usingApi(async (api) => {714    // Run the transaction715    const tx = api.tx.nft.setMintPermission(collectionId, enabled);716    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;717    const result = getCreateCollectionResult(events);718    // tslint:disable-next-line:no-unused-expression719    expect(result.success).to.be.false;720  });721}722723export async function isWhitelisted(collectionId: number, address: string) {724  let whitelisted: boolean = false;725  await usingApi(async (api) => {726    whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;727  });728  return whitelisted;729}730731export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {732  await usingApi(async (api) => {733734    const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();735736    // Run the transaction737    const tx = api.tx.nft.addToWhiteList(collectionId, address);738    const events = await submitTransactionAsync(sender, tx);739    const result = getGenericResult(events);740741    const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();742743    // What to expect744    // tslint:disable-next-line:no-unused-expression745    expect(result.success).to.be.true;746    // tslint:disable-next-line: no-unused-expression747    expect(whiteListedBefore).to.be.false;748    // tslint:disable-next-line: no-unused-expression749    expect(whiteListedAfter).to.be.true;750  });751}752753export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {754  await usingApi(async (api) => {755    // Run the transaction756    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);757    const events = await submitTransactionAsync(sender, tx);758    const result = getGenericResult(events);759760    // What to expect761    // tslint:disable-next-line:no-unused-expression762    expect(result.success).to.be.true;763  });764}765766export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {767  await usingApi(async (api) => {768    // Run the transaction769    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);770    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;771    const result = getGenericResult(events);772773    // What to expect774    // tslint:disable-next-line:no-unused-expression775    expect(result.success).to.be.false;776  });777}778779export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)780  : Promise<ICollectionInterface | null> => {781  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;782};783784export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {785  // set global object - collectionsCount786  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();787};