git.delta.rocks / unique-network / refs/commits / 92f89cb770f9

difftreelog

source

tests/src/util/helpers.ts29.0 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;2324type GenericResult = {25  success: boolean,26};2728interface CreateCollectionResult {29  success: boolean;30  collectionId: number;31}3233interface CreateItemResult {34  success: boolean;35  collectionId: number;36  itemId: number;37}3839interface IReFungibleOwner {40  Fraction: BN;41  Owner: number[];42}4344interface ITokenDataType {45  Owner: number[];46  ConstData: number[];47  VariableData: number[];48}4950interface IFungibleTokenDataType {51  Value: BN;52}5354export interface IReFungibleTokenDataType {55  Owner: IReFungibleOwner[];56  ConstData: number[];57  VariableData: number[];58}5960export function getGenericResult(events: EventRecord[]): GenericResult {61  const result: GenericResult = {62    success: false,63  };64  events.forEach(({ phase, event: { data, method, section } }) => {65    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);66    if (method === 'ExtrinsicSuccess') {67      result.success = true;68    }69  });70  return result;71}7273export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {74  let success = false;75  let collectionId: number = 0;76  events.forEach(({ phase, event: { data, method, section } }) => {77    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);78    if (method == 'ExtrinsicSuccess') {79      success = true;80    } else if ((section == 'nft') && (method == 'Created')) {81      collectionId = parseInt(data[0].toString());82    }83  });84  const result: CreateCollectionResult = {85    success,86    collectionId,87  };88  return result;89}9091export function getCreateItemResult(events: EventRecord[]): CreateItemResult {92  let success = false;93  let collectionId: number = 0;94  let itemId: number = 0;95  events.forEach(({ phase, event: { data, method, section } }) => {96    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);97    if (method == 'ExtrinsicSuccess') {98      success = true;99    } else if ((section == 'nft') && (method == 'ItemCreated')) {100      collectionId = parseInt(data[0].toString());101      itemId = parseInt(data[1].toString());102    }103  });104  const result: CreateItemResult = {105    success,106    collectionId,107    itemId,108  };109  return result;110}111112interface Invalid {113  type: 'Invalid';114}115116interface Nft {117  type: 'NFT';118}119120interface Fungible {121  type: 'Fungible';122  decimalPoints: number;123}124125interface ReFungible {126  type: 'ReFungible';127  decimalPoints: number;128}129130interface Nft {131  type: 'NFT'132}133134interface Fungible {135  type: 'Fungible',136  decimalPoints: number137}138139interface ReFungible {140  type: 'ReFungible',141  decimalPoints: number142}143144type CollectionMode = Nft | Fungible | ReFungible | Invalid;145146export type CreateCollectionParams = {147  mode: CollectionMode,148  name: string,149  description: string,150  tokenPrefix: string,151};152153const defaultCreateCollectionParams: CreateCollectionParams = {154  description: 'description',155  mode: { type: 'NFT' },156  name: 'name',157  tokenPrefix: 'prefix',158}159160export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {161  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};162163  let collectionId: number = 0;164  await usingApi(async (api) => {165    // Get number of collections before the transaction166    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);167168    // Run the CreateCollection transaction169    const alicePrivateKey = privateKey('//Alice');170171    let modeprm = {};172    if (mode.type === 'NFT') {173      modeprm = {nft: null};174    } else if (mode.type === 'Fungible') {175      modeprm = {fungible: mode.decimalPoints};176    } else if (mode.type === 'ReFungible') {177      modeprm = {refungible: mode.decimalPoints};178    } else if (mode.type === 'Invalid') {179      modeprm = {invalid: null};180    }181182    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);183    const events = await submitTransactionAsync(alicePrivateKey, tx);184    const result = getCreateCollectionResult(events);185186    // Get number of collections after the transaction187    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);188189    // Get the collection190    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();191192    // What to expect193    // tslint:disable-next-line:no-unused-expression194    expect(result.success).to.be.true;195    expect(result.collectionId).to.be.equal(BcollectionCount);196    // tslint:disable-next-line:no-unused-expression197    expect(collection).to.be.not.null;198    expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');199    expect(collection.Owner).to.be.equal(alicesPublicKey);200    expect(utf16ToStr(collection.Name)).to.be.equal(name);201    expect(utf16ToStr(collection.Description)).to.be.equal(description);202    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);203204    collectionId = result.collectionId;205  });206207  return collectionId;208}209210export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {211  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};212213  let modeprm = {};214  if (mode.type === 'NFT') {215    modeprm = {nft: null};216  } else if (mode.type === 'Fungible') {217    modeprm = {fungible: mode.decimalPoints};218  } else if (mode.type === 'ReFungible') {219    modeprm = {refungible: mode.decimalPoints};220  } else if (mode.type === 'Invalid') {221    modeprm = {invalid: null};222  }223224  await usingApi(async (api) => {225    // Get number of collections before the transaction226    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());227228    // Run the CreateCollection transaction229    const alicePrivateKey = privateKey('//Alice');230    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);231    const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;232    const result = getCreateCollectionResult(events);233234    // Get number of collections after the transaction235    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());236237    // What to expect238    // tslint:disable-next-line:no-unused-expression239    expect(result.success).to.be.false;240    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');241  });242}243244export async function findUnusedAddress(api: ApiPromise, seedAddition = ''): Promise<IKeyringPair> {245  let bal = new BigNumber(0);246  let unused;247  do {248    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000)) + seedAddition;249    const keyring = new Keyring({ type: 'sr25519' });250    unused = keyring.addFromUri(`//${randomSeed}`);251    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());252  } while (bal.toFixed() != '0');253  return unused;254}255256export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {257  return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));258}259260export async function findNotExistingCollection(api: ApiPromise): Promise<number> {261  const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;262  const newCollection: number = totalNumber + 1;263  return newCollection;264}265266function getDestroyResult(events: EventRecord[]): boolean {267  let success: boolean = false;268  events.forEach(({ phase, event: { data, method, section } }) => {269    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);270    if (method == 'ExtrinsicSuccess') {271      success = true;272    }273  });274  return success;275}276277export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {278  await usingApi(async (api) => {279    // Run the DestroyCollection transaction280    const alicePrivateKey = privateKey(senderSeed);281    const tx = api.tx.nft.destroyCollection(collectionId);282    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;283  });284}285286export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {287  await usingApi(async (api) => {288    // Run the DestroyCollection transaction289    const alicePrivateKey = privateKey(senderSeed);290    const tx = api.tx.nft.destroyCollection(collectionId);291    const events = await submitTransactionAsync(alicePrivateKey, tx);292    const result = getDestroyResult(events);293294    // Get the collection295    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();296297    // What to expect298    expect(result).to.be.true;299    expect(collection).to.be.not.null;300    expect(collection.Owner).to.be.equal(nullPublicKey);301  });302}303304export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {305  await usingApi(async (api) => {306307    // Run the transaction308    const alicePrivateKey = privateKey('//Alice');309    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);310    const events = await submitTransactionAsync(alicePrivateKey, tx);311    const result = getGenericResult(events);312313    // Get the collection314    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();315316    // What to expect317    expect(result.success).to.be.true;318    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());319    expect(collection.SponsorConfirmed).to.be.false;320  });321}322323export async function removeCollectionSponsorExpectSuccess(collectionId: number) {324  await usingApi(async (api) => {325326    // Run the transaction327    const alicePrivateKey = privateKey('//Alice');328    const tx = api.tx.nft.removeCollectionSponsor(collectionId);329    const events = await submitTransactionAsync(alicePrivateKey, tx);330    const result = getGenericResult(events);331332    // Get the collection333    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();334335    // What to expect336    expect(result.success).to.be.true;337    expect(collection.Sponsor).to.be.equal(nullPublicKey);338    expect(collection.SponsorConfirmed).to.be.false;339  });340}341342export async function removeCollectionSponsorExpectFailure(collectionId: number) {343  await usingApi(async (api) => {344345    // Run the transaction346    const alicePrivateKey = privateKey('//Alice');347    const tx = api.tx.nft.removeCollectionSponsor(collectionId);348    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;349  });350}351352export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {353  await usingApi(async (api) => {354355    // Run the transaction356    const alicePrivateKey = privateKey(senderSeed);357    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);358    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;359  });360}361362export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {363  await usingApi(async (api) => {364365    // Run the transaction366    const sender = privateKey(senderSeed);367    const tx = api.tx.nft.confirmSponsorship(collectionId);368    const events = await submitTransactionAsync(sender, tx);369    const result = getGenericResult(events);370371    // Get the collection372    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();373374    // What to expect375    expect(result.success).to.be.true;376    expect(collection.Sponsor).to.be.equal(sender.address);377    expect(collection.SponsorConfirmed).to.be.true;378  });379}380381export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {382  await usingApi(async (api) => {383384    // Run the transaction385    const sender = privateKey(senderSeed);386    const tx = api.tx.nft.confirmSponsorship(collectionId);387    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;388  });389}390391export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {392  await usingApi(async (api) => {393    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);394    const events = await submitTransactionAsync(sender, tx);395    const result = getGenericResult(events);396397    expect(result.success).to.be.true;398  });399}400401export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {402  await usingApi(async (api) => {403    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);404    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;405    const result = getGenericResult(events);406407    expect(result.success).to.be.false;408  });409}410411export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {412  await usingApi(async (api) => {413    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);414    const events = await submitTransactionAsync(sender, tx);415    const result = getGenericResult(events);416417    expect(result.success).to.be.true;418  });419}420421export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {422  await usingApi(async (api) => {423    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);424    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;425    const result = getGenericResult(events);426427    expect(result.success).to.be.false;428  });429}430431export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {432  await usingApi(async (api) => {433    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));434    const events = await submitTransactionAsync(sender, tx);435    const result = getGenericResult(events);436437    expect(result.success).to.be.true;438  });439}440441export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {442  await usingApi(async (api) => {443    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));444    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;445  });446}447448export interface CreateFungibleData extends Struct {449  readonly value: u128;450}451452export interface CreateReFungibleData extends Struct {}453export interface CreateNftData extends Struct {}454455export interface CreateItemData extends Enum {456  NFT: CreateNftData;457  Fungible: CreateFungibleData;458  ReFungible: CreateReFungibleData;459}460461export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {462  await usingApi(async (api) => {463    const tx = api.tx.nft.burnItem(collectionId, tokenId, value);464    const events = await submitTransactionAsync(owner, tx);465    const result = getGenericResult(events);466    // Get the item467    const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();468    // What to expect469    // tslint:disable-next-line:no-unused-expression470    expect(result.success).to.be.true;471    // tslint:disable-next-line:no-unused-expression472    expect(item).to.be.not.null;473    expect(item.Owner).to.be.equal(nullPublicKey);474  });475}476477export async function478approveExpectSuccess(collectionId: number,479                     tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {480  await usingApi(async (api: ApiPromise) => {481    const allowanceBefore =482      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;483    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);484    const events = await submitTransactionAsync(owner, approveNftTx);485    const result = getCreateItemResult(events);486    // tslint:disable-next-line:no-unused-expression487    expect(result.success).to.be.true;488    const allowanceAfter =489      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;490    expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);491  });492}493494export async function495transferFromExpectSuccess(collectionId: number,496                          tokenId: number,497                          accountApproved: IKeyringPair,498                          accountFrom: IKeyringPair,499                          accountTo: IKeyringPair,500                          value: number = 1,501                          type: string = 'NFT') {502  await usingApi(async (api: ApiPromise) => {503    let balanceBefore = new BN(0);504    if (type === 'Fungible') {505      balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;506    }507    const transferFromTx = await api.tx.nft.transferFrom(508      accountFrom.address, accountTo.address, collectionId, tokenId, value);509    const events = await submitTransactionAsync(accountApproved, transferFromTx);510    const result = getCreateItemResult(events);511    // tslint:disable-next-line:no-unused-expression512    expect(result.success).to.be.true;513    if (type === 'NFT') {514      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;515      expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);516    }517    if (type === 'Fungible') {518      const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;519      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);520    }521    if (type === 'ReFungible') {522      const nftItemData =523        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;524      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);525      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);526    }527  });528}529530export async function531transferFromExpectFail(collectionId: number,532                       tokenId: number,533                       accountApproved: IKeyringPair,534                       accountFrom: IKeyringPair,535                       accountTo: IKeyringPair,536                       value: number = 1) {537  await usingApi(async (api: ApiPromise) => {538    const transferFromTx = await api.tx.nft.transferFrom(539      accountFrom.address, accountTo.address, collectionId, tokenId, value);540    const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;541    const result = getCreateCollectionResult(events);542    // tslint:disable-next-line:no-unused-expression543    expect(result.success).to.be.false;544  });545}546547export async function548transferExpectSuccess(collectionId: number,549                      tokenId: number,550                      sender: IKeyringPair,551                      recipient: IKeyringPair,552                      value: number = 1,553                      type: string = 'NFT') {554  await usingApi(async (api: ApiPromise) => {555    let balanceBefore = new BN(0);556    if (type === 'Fungible') {557      balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;558    }559    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);560    const events = await submitTransactionAsync(sender, transferTx);561    const result = getCreateItemResult(events);562    // tslint:disable-next-line:no-unused-expression563    expect(result.success).to.be.true;564    if (type === 'NFT') {565      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;566      expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);567    }568    if (type === 'Fungible') {569      const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;570      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);571    }572    if (type === 'ReFungible') {573      const nftItemData =574        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;575      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);576      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);577    }578  });579}580581export async function582transferExpectFail(collectionId: number,583                   tokenId: number,584                   sender: IKeyringPair,585                   recipient: IKeyringPair,586                   value: number = 1,587                   type: string = 'NFT') {588  await usingApi(async (api: ApiPromise) => {589    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);590    const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;591    if (events && Array.isArray(events)) {592      const result = getCreateCollectionResult(events);593      // tslint:disable-next-line:no-unused-expression594      expect(result.success).to.be.false;595    }596  });597}598599export async function600approveExpectFail(collectionId: number,601                  tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {602  await usingApi(async (api: ApiPromise) => {603    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);604    const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;605    const result = getCreateCollectionResult(events);606    // tslint:disable-next-line:no-unused-expression607    expect(result.success).to.be.false;608  });609}610611export async function createItemExpectSuccess(612  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {613  let newItemId: number = 0;614  await usingApi(async (api) => {615    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);616    const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();617    const AItemBalance = new BigNumber(Aitem.Value);618619    if (owner === '') {620      owner = sender.address;621    }622623    let tx;624    if (createMode === 'Fungible') {625      const createData = {fungible: {value: 10}};626      tx = api.tx.nft.createItem(collectionId, owner, createData);627    } else {628      tx = api.tx.nft.createItem(collectionId, owner, createMode);629    }630    const events = await submitTransactionAsync(sender, tx);631    const result = getCreateItemResult(events);632633    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);634    const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();635    const BItemBalance = new BigNumber(Bitem.Value);636637    // What to expect638    // tslint:disable-next-line:no-unused-expression639    expect(result.success).to.be.true;640    if (createMode === 'Fungible') {641      expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);642    } else {643      expect(BItemCount).to.be.equal(AItemCount + 1);644    }645    expect(collectionId).to.be.equal(result.collectionId);646    expect(BItemCount).to.be.equal(result.itemId);647    newItemId = result.itemId;648  });649  return newItemId;650}651652export async function createItemExpectFailure(653  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {654  await usingApi(async (api) => {655    const tx = api.tx.nft.createItem(collectionId, owner, createMode);656    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;657    const result = getCreateItemResult(events);658659    expect(result.success).to.be.false;660  });661}662663export async function setPublicAccessModeExpectSuccess(664  sender: IKeyringPair, collectionId: number,665  accessMode: 'Normal' | 'WhiteList',666) {667  await usingApi(async (api) => {668669    // Run the transaction670    const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);671    const events = await submitTransactionAsync(sender, tx);672    const result = getGenericResult(events);673674    // Get the collection675    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();676677    // What to expect678    // tslint:disable-next-line:no-unused-expression679    expect(result.success).to.be.true;680    expect(collection.Access).to.be.equal(accessMode);681  });682}683684export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {685  await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');686}687688export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {689  await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');690}691692export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {693  await usingApi(async (api) => {694695    // Run the transaction696    const tx = api.tx.nft.setMintPermission(collectionId, enabled);697    const events = await submitTransactionAsync(sender, tx);698    const result = getGenericResult(events);699700    // Get the collection701    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();702703    // What to expect704    // tslint:disable-next-line:no-unused-expression705    expect(result.success).to.be.true;706    expect(collection.MintMode).to.be.equal(enabled);707  });708}709710export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {711  await setMintPermissionExpectSuccess(sender, collectionId, true);712}713714export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {715  await usingApi(async (api) => {716    // Run the transaction717    const tx = api.tx.nft.setMintPermission(collectionId, enabled);718    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;719    const result = getCreateCollectionResult(events);720    // tslint:disable-next-line:no-unused-expression721    expect(result.success).to.be.false;722  });723}724725export async function isWhitelisted(collectionId: number, address: string) {726  let whitelisted: boolean = false;727  await usingApi(async (api) => {728    whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;729  });730  return whitelisted;731}732733export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {734  await usingApi(async (api) => {735736    const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();737738    // Run the transaction739    const tx = api.tx.nft.addToWhiteList(collectionId, address);740    const events = await submitTransactionAsync(sender, tx);741    const result = getGenericResult(events);742743    const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();744745    // What to expect746    // tslint:disable-next-line:no-unused-expression747    expect(result.success).to.be.true;748    // tslint:disable-next-line: no-unused-expression749    expect(whiteListedBefore).to.be.false;750    // tslint:disable-next-line: no-unused-expression751    expect(whiteListedAfter).to.be.true;752  });753}754755export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {756  await usingApi(async (api) => {757    // Run the transaction758    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);759    const events = await submitTransactionAsync(sender, tx);760    const result = getGenericResult(events);761762    // What to expect763    // tslint:disable-next-line:no-unused-expression764    expect(result.success).to.be.true;765  });766}767768export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {769  await usingApi(async (api) => {770    // Run the transaction771    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);772    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;773    const result = getGenericResult(events);774775    // What to expect776    // tslint:disable-next-line:no-unused-expression777    expect(result.success).to.be.false;778  });779}780781export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)782  : Promise<ICollectionInterface | null> => {783  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;784};785786export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {787  // set global object - collectionsCount788  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();789};