123456import { 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 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 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 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 168 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);169170 171 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 189 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);190191 192 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();193194 195 196 expect(result.success).to.be.true;197 expect(result.collectionId).to.be.equal(BcollectionCount);198 199 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 228 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());229230 231 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 237 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());238239 240 241 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 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 278 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 287 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 293 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();294295 296 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 306 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 312 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();313314 315 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 325 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 331 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();332333 334 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 344 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 354 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 364 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 370 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();371372 373 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 383 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 {447 readonly Value: bigint;448}449450export interface CreateReFungibleData { }451export interface CreateNftData { }452453export type CreateItemData = {454 NFT: CreateNftData;455} | {456 Fungible: CreateFungibleData;457} | {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 467 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();468 469 470 expect(result.success).to.be.true;471 472 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 | bigint = 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 487 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.sub(allowanceBefore).toString()).to.be.equal(amount.toString());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 512 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 543 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 563 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 594 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 607 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 638 639 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 670 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);671 const events = await submitTransactionAsync(sender, tx);672 const result = getGenericResult(events);673674 675 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();676677 678 679 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 696 const tx = api.tx.nft.setMintPermission(collectionId, enabled);697 const events = await submitTransactionAsync(sender, tx);698 const result = getGenericResult(events);699700 701 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();702703 704 705 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 717 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 721 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 739 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 746 747 expect(result.success).to.be.true;748 749 expect(whiteListedBefore).to.be.false;750 751 expect(whiteListedAfter).to.be.true;752 });753}754755export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {756 await usingApi(async (api) => {757 758 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);759 const events = await submitTransactionAsync(sender, tx);760 const result = getGenericResult(events);761762 763 764 expect(result.success).to.be.true;765 });766}767768export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {769 await usingApi(async (api) => {770 771 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 776 777 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 788 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();789};