git.delta.rocks / unique-network / refs/commits / 9e6c0bc65705

difftreelog

source

tests/src/util/helpers.ts28.8 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}128129type CollectionMode = Nft | Fungible | ReFungible | Invalid;130131export type CreateCollectionParams = {132  mode: CollectionMode,133  name: string,134  description: string,135  tokenPrefix: string,136};137138const defaultCreateCollectionParams: CreateCollectionParams = {139  description: 'description',140  mode: { type: 'NFT' },141  name: 'name',142  tokenPrefix: 'prefix',143}144145export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {146  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};147148  let collectionId: number = 0;149  await usingApi(async (api) => {150    // Get number of collections before the transaction151    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);152153    // Run the CreateCollection transaction154    const alicePrivateKey = privateKey('//Alice');155156    let modeprm = {};157    if (mode.type === 'NFT') {158      modeprm = {nft: null};159    } else if (mode.type === 'Fungible') {160      modeprm = {fungible: mode.decimalPoints};161    } else if (mode.type === 'ReFungible') {162      modeprm = {refungible: null};163    } else if (mode.type === 'Invalid') {164      modeprm = {invalid: null};165    }166167    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);168    const events = await submitTransactionAsync(alicePrivateKey, tx);169    const result = getCreateCollectionResult(events);170171    // Get number of collections after the transaction172    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);173174    // Get the collection175    const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();176177    // What to expect178    // tslint:disable-next-line:no-unused-expression179    expect(result.success).to.be.true;180    expect(result.collectionId).to.be.equal(BcollectionCount);181    // tslint:disable-next-line:no-unused-expression182    expect(collection).to.be.not.null;183    expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');184    expect(collection.Owner).to.be.equal(alicesPublicKey);185    expect(utf16ToStr(collection.Name)).to.be.equal(name);186    expect(utf16ToStr(collection.Description)).to.be.equal(description);187    expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);188189    collectionId = result.collectionId;190  });191192  return collectionId;193}194195export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {196  const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};197198  let modeprm = {};199  if (mode.type === 'NFT') {200    modeprm = {nft: null};201  } else if (mode.type === 'Fungible') {202    modeprm = {fungible: mode.decimalPoints};203  } else if (mode.type === 'ReFungible') {204    modeprm = {refungible: null};205  } else if (mode.type === 'Invalid') {206    modeprm = {invalid: null};207  }208209  await usingApi(async (api) => {210    // Get number of collections before the transaction211    const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());212213    // Run the CreateCollection transaction214    const alicePrivateKey = privateKey('//Alice');215    const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);216    const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;217    const result = getCreateCollectionResult(events);218219    // Get number of collections after the transaction220    const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());221222    // What to expect223    // tslint:disable-next-line:no-unused-expression224    expect(result.success).to.be.false;225    expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');226  });227}228229export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {230  let bal = new BigNumber(0);231  let unused;232  do {233    const randomSeed = 'seed' +  Math.floor(Math.random() * Math.floor(10000));234    const keyring = new Keyring({ type: 'sr25519' });235    unused = keyring.addFromUri(`//${randomSeed}`);236    bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());237  } while (bal.toFixed() != '0');238  return unused;239}240241export async function findNotExistingCollection(api: ApiPromise): Promise<number> {242  const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;243  const newCollection: number = totalNumber + 1;244  return newCollection;245}246247function getDestroyResult(events: EventRecord[]): boolean {248  let success: boolean = false;249  events.forEach(({ phase, event: { data, method, section } }) => {250    // console.log(`    ${phase}: ${section}.${method}:: ${data}`);251    if (method == 'ExtrinsicSuccess') {252      success = true;253    }254  });255  return success;256}257258export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {259  await usingApi(async (api) => {260    // Run the DestroyCollection transaction261    const alicePrivateKey = privateKey(senderSeed);262    const tx = api.tx.nft.destroyCollection(collectionId);263    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;264  });265}266267export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {268  await usingApi(async (api) => {269    // Run the DestroyCollection transaction270    const alicePrivateKey = privateKey(senderSeed);271    const tx = api.tx.nft.destroyCollection(collectionId);272    const events = await submitTransactionAsync(alicePrivateKey, tx);273    const result = getDestroyResult(events);274275    // Get the collection276    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();277278    // What to expect279    expect(result).to.be.true;280    expect(collection).to.be.not.null;281    expect(collection.Owner).to.be.equal(nullPublicKey);282  });283}284285export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {286  await usingApi(async (api) => {287288    // Run the transaction289    const alicePrivateKey = privateKey('//Alice');290    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);291    const events = await submitTransactionAsync(alicePrivateKey, tx);292    const result = getGenericResult(events);293294    // Get the collection295    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();296297    // What to expect298    expect(result.success).to.be.true;299    expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());300    expect(collection.SponsorConfirmed).to.be.false;301  });302}303304export async function removeCollectionSponsorExpectSuccess(collectionId: number) {305  await usingApi(async (api) => {306307    // Run the transaction308    const alicePrivateKey = privateKey('//Alice');309    const tx = api.tx.nft.removeCollectionSponsor(collectionId);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).to.be.equal(nullPublicKey);319    expect(collection.SponsorConfirmed).to.be.false;320  });321}322323export async function removeCollectionSponsorExpectFailure(collectionId: number) {324  await usingApi(async (api) => {325326    // Run the transaction327    const alicePrivateKey = privateKey('//Alice');328    const tx = api.tx.nft.removeCollectionSponsor(collectionId);329    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;330  });331}332333export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {334  await usingApi(async (api) => {335336    // Run the transaction337    const alicePrivateKey = privateKey(senderSeed);338    const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);339    await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;340  });341}342343export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {344  await usingApi(async (api) => {345346    // Run the transaction347    const sender = privateKey(senderSeed);348    const tx = api.tx.nft.confirmSponsorship(collectionId);349    const events = await submitTransactionAsync(sender, tx);350    const result = getGenericResult(events);351352    // Get the collection353    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();354355    // What to expect356    expect(result.success).to.be.true;357    expect(collection.Sponsor).to.be.equal(sender.address);358    expect(collection.SponsorConfirmed).to.be.true;359  });360}361362export async function confirmSponsorshipExpectFailure(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    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;369  });370}371372export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {373  await usingApi(async (api) => {374    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);375    const events = await submitTransactionAsync(sender, tx);376    const result = getGenericResult(events);377378    expect(result.success).to.be.true;379  });380}381382export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {383  await usingApi(async (api) => {384    const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);385    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;386    const result = getGenericResult(events);387388    expect(result.success).to.be.false;389  });390}391392export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {393  await usingApi(async (api) => {394    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);395    const events = await submitTransactionAsync(sender, tx);396    const result = getGenericResult(events);397398    expect(result.success).to.be.true;399  });400}401402export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {403  await usingApi(async (api) => {404    const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);405    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;406    const result = getGenericResult(events);407408    expect(result.success).to.be.false;409  });410}411412export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {413  await usingApi(async (api) => {414    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));415    const events = await submitTransactionAsync(sender, tx);416    const result = getGenericResult(events);417418    expect(result.success).to.be.true;419  });420}421422export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {423  await usingApi(async (api) => {424    const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));425    await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;426  });427}428429export interface CreateFungibleData extends Struct {430  readonly value: u128;431}432433export interface CreateReFungibleData extends Struct {}434export interface CreateNftData extends Struct {}435436export interface CreateItemData extends Enum {437  NFT: CreateNftData;438  Fungible: CreateFungibleData;439  ReFungible: CreateReFungibleData;440}441442export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {443  await usingApi(async (api) => {444    const tx = api.tx.nft.burnItem(collectionId, tokenId, value);445    const events = await submitTransactionAsync(owner, tx);446    const result = getGenericResult(events);447    // Get the item448    const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();449    // What to expect450    // tslint:disable-next-line:no-unused-expression451    expect(result.success).to.be.true;452    // tslint:disable-next-line:no-unused-expression453    expect(item).to.be.not.null;454    expect(item.Owner).to.be.equal(nullPublicKey);455  });456}457458export async function459approveExpectSuccess(collectionId: number,460                     tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {461  await usingApi(async (api: ApiPromise) => {462    const allowanceBefore =463      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;464    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);465    const events = await submitTransactionAsync(owner, approveNftTx);466    const result = getCreateItemResult(events);467    // tslint:disable-next-line:no-unused-expression468    expect(result.success).to.be.true;469    const allowanceAfter =470      await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;471    expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);472  });473}474475export async function476transferFromExpectSuccess(collectionId: number,477                          tokenId: number,478                          accountApproved: IKeyringPair,479                          accountFrom: IKeyringPair,480                          accountTo: IKeyringPair,481                          value: number = 1,482                          type: string = 'NFT') {483  await usingApi(async (api: ApiPromise) => {484    let balanceBefore = new BN(0);485    if (type === 'Fungible') {486      balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;487    }488    const transferFromTx = await api.tx.nft.transferFrom(489      accountFrom.address, accountTo.address, collectionId, tokenId, value);490    const events = await submitTransactionAsync(accountApproved, transferFromTx);491    const result = getCreateItemResult(events);492    // tslint:disable-next-line:no-unused-expression493    expect(result.success).to.be.true;494    if (type === 'NFT') {495      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;496      expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);497    }498    if (type === 'Fungible') {499      const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;500      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);501    }502    if (type === 'ReFungible') {503      const nftItemData =504        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;505      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);506      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);507    }508  });509}510511export async function512transferFromExpectFail(collectionId: number,513                       tokenId: number,514                       accountApproved: IKeyringPair,515                       accountFrom: IKeyringPair,516                       accountTo: IKeyringPair,517                       value: number = 1) {518  await usingApi(async (api: ApiPromise) => {519    const transferFromTx = await api.tx.nft.transferFrom(520      accountFrom.address, accountTo.address, collectionId, tokenId, value);521    const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;522    const result = getCreateCollectionResult(events);523    // tslint:disable-next-line:no-unused-expression524    expect(result.success).to.be.false;525  });526}527528export async function529transferExpectSuccess(collectionId: number,530                      tokenId: number,531                      sender: IKeyringPair,532                      recipient: IKeyringPair,533                      value: number = 1,534                      type: string = 'NFT') {535  await usingApi(async (api: ApiPromise) => {536    let balanceBefore = new BN(0);537    if (type === 'Fungible') {538      balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;539    }540    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);541    const events = await submitTransactionAsync(sender, transferTx);542    const result = getCreateItemResult(events);543    // tslint:disable-next-line:no-unused-expression544    expect(result.success).to.be.true;545    if (type === 'NFT') {546      const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;547      expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);548    }549    if (type === 'Fungible') {550      const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;551      expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);552    }553    if (type === 'ReFungible') {554      const nftItemData =555        await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;556      expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);557      expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);558    }559  });560}561562export async function563transferExpectFail(collectionId: number,564                   tokenId: number,565                   sender: IKeyringPair,566                   recipient: IKeyringPair,567                   value: number = 1,568                   type: string = 'NFT') {569  await usingApi(async (api: ApiPromise) => {570    const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);571    const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;572    if (events && Array.isArray(events)) {573      const result = getCreateCollectionResult(events);574      // tslint:disable-next-line:no-unused-expression575      expect(result.success).to.be.false;576    }577  });578}579580export async function581approveExpectFail(collectionId: number,582                  tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {583  await usingApi(async (api: ApiPromise) => {584    const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);585    const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;586    const result = getCreateCollectionResult(events);587    // tslint:disable-next-line:no-unused-expression588    expect(result.success).to.be.false;589  });590}591592export async function createItemExpectSuccess(593  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {594  let newItemId: number = 0;595  await usingApi(async (api) => {596    const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);597    const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();598    const AItemBalance = new BigNumber(Aitem.Value);599600    if (owner === '') {601      owner = sender.address;602    }603604    let tx;605    if (createMode === 'Fungible') {606      const createData = {fungible: {value: 10}};607      tx = api.tx.nft.createItem(collectionId, owner, createData);608    } else if (createMode === 'ReFungible') {609      const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};610      tx = api.tx.nft.createItem(collectionId, owner, createData);611    } else {612      tx = api.tx.nft.createItem(collectionId, owner, createMode);613    }614    const events = await submitTransactionAsync(sender, tx);615    const result = getCreateItemResult(events);616617    const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);618    const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();619    const BItemBalance = new BigNumber(Bitem.Value);620621    // What to expect622    // tslint:disable-next-line:no-unused-expression623    expect(result.success).to.be.true;624    if (createMode === 'Fungible') {625      expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);626    } else {627      expect(BItemCount).to.be.equal(AItemCount + 1);628    }629    expect(collectionId).to.be.equal(result.collectionId);630    expect(BItemCount).to.be.equal(result.itemId);631    newItemId = result.itemId;632  });633  return newItemId;634}635636export async function createItemExpectFailure(637  sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {638  await usingApi(async (api) => {639    const tx = api.tx.nft.createItem(collectionId, owner, createMode);640    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;641    const result = getCreateItemResult(events);642643    expect(result.success).to.be.false;644  });645}646647export async function setPublicAccessModeExpectSuccess(648  sender: IKeyringPair, collectionId: number,649  accessMode: 'Normal' | 'WhiteList',650) {651  await usingApi(async (api) => {652653    // Run the transaction654    const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);655    const events = await submitTransactionAsync(sender, tx);656    const result = getGenericResult(events);657658    // Get the collection659    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();660661    // What to expect662    // tslint:disable-next-line:no-unused-expression663    expect(result.success).to.be.true;664    expect(collection.Access).to.be.equal(accessMode);665  });666}667668export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {669  await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');670}671672export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {673  await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');674}675676export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {677  await usingApi(async (api) => {678679    // Run the transaction680    const tx = api.tx.nft.setMintPermission(collectionId, enabled);681    const events = await submitTransactionAsync(sender, tx);682    const result = getGenericResult(events);683684    // Get the collection685    const collection: any = (await api.query.nft.collection(collectionId)).toJSON();686687    // What to expect688    // tslint:disable-next-line:no-unused-expression689    expect(result.success).to.be.true;690    expect(collection.MintMode).to.be.equal(enabled);691  });692}693694export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {695  await setMintPermissionExpectSuccess(sender, collectionId, true);696}697698export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {699  await usingApi(async (api) => {700    // Run the transaction701    const tx = api.tx.nft.setMintPermission(collectionId, enabled);702    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;703    const result = getCreateCollectionResult(events);704    // tslint:disable-next-line:no-unused-expression705    expect(result.success).to.be.false;706  });707}708709export async function isWhitelisted(collectionId: number, address: string) {710  let whitelisted: boolean = false;711  await usingApi(async (api) => {712    whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;713  });714  return whitelisted;715}716717export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {718  await usingApi(async (api) => {719720    const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();721722    // Run the transaction723    const tx = api.tx.nft.addToWhiteList(collectionId, address);724    const events = await submitTransactionAsync(sender, tx);725    const result = getGenericResult(events);726727    const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();728729    // What to expect730    // tslint:disable-next-line:no-unused-expression731    expect(result.success).to.be.true;732    // tslint:disable-next-line: no-unused-expression733    expect(whiteListedBefore).to.be.false;734    // tslint:disable-next-line: no-unused-expression735    expect(whiteListedAfter).to.be.true;736  });737}738739export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {740  await usingApi(async (api) => {741    // Run the transaction742    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);743    const events = await submitTransactionAsync(sender, tx);744    const result = getGenericResult(events);745746    // What to expect747    // tslint:disable-next-line:no-unused-expression748    expect(result.success).to.be.true;749  });750}751752export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {753  await usingApi(async (api) => {754    // Run the transaction755    const tx = api.tx.nft.removeFromWhiteList(collectionId, address);756    const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;757    const result = getGenericResult(events);758759    // What to expect760    // tslint:disable-next-line:no-unused-expression761    expect(result.success).to.be.false;762  });763}764765export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)766  : Promise<ICollectionInterface | null> => {767  return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;768};769770export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {771  // set global object - collectionsCount772  return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();773};