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;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 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 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 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 151 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);152153 154 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 172 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);173174 175 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();176177 178 179 expect(result.success).to.be.true;180 expect(result.collectionId).to.be.equal(BcollectionCount);181 182 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 211 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());212213 214 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 220 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());221222 223 224 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 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 261 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 270 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 276 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();277278 279 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 289 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 295 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();296297 298 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 308 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 314 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();315316 317 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 327 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 337 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 347 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 353 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();354355 356 expect(result.success).to.be.true;357 expect(collection.Sponsor).to.be.equal(sender.address);358 expect(collection.SponsorConfirmed).to.be.true;359 });360}361362363export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {364 await usingApi(async (api) => {365366 367 const sender = privateKey(senderSeed);368 const tx = api.tx.nft.confirmSponsorship(collectionId);369 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;370 });371}372373export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {374 await usingApi(async (api) => {375 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);376 const events = await submitTransactionAsync(sender, tx);377 const result = getGenericResult(events);378379 expect(result.success).to.be.true;380 });381}382383export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {384 await usingApi(async (api) => {385 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);386 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;387 const result = getGenericResult(events);388389 expect(result.success).to.be.false;390 });391}392393export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {394 await usingApi(async (api) => {395 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);396 const events = await submitTransactionAsync(sender, tx);397 const result = getGenericResult(events);398399 expect(result.success).to.be.true;400 });401}402403export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {404 await usingApi(async (api) => {405 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);406 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;407 const result = getGenericResult(events);408409 expect(result.success).to.be.false;410 });411}412413export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {414 await usingApi(async (api) => {415 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));416 const events = await submitTransactionAsync(sender, tx);417 const result = getGenericResult(events);418419 expect(result.success).to.be.true;420 });421}422423export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {424 await usingApi(async (api) => {425 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));426 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;427 });428}429430export interface CreateFungibleData extends Struct {431 readonly value: u128;432}433434export interface CreateReFungibleData extends Struct {}435export interface CreateNftData extends Struct {}436437export interface CreateItemData extends Enum {438 NFT: CreateNftData;439 Fungible: CreateFungibleData;440 ReFungible: CreateReFungibleData;441}442443export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {444 await usingApi(async (api) => {445 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);446 const events = await submitTransactionAsync(owner, tx);447 const result = getGenericResult(events);448 449 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();450 451 452 expect(result.success).to.be.true;453 454 expect(item).to.be.not.null;455 expect(item.Owner).to.be.equal(nullPublicKey);456 });457}458459export async function460approveExpectSuccess(collectionId: number,461 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) { 462 await usingApi(async (api: ApiPromise) => {463 const allowanceBefore =464 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;465 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);466 const events = await submitTransactionAsync(owner, approveNftTx);467 const result = getCreateItemResult(events);468 469 expect(result.success).to.be.true;470 const allowanceAfter =471 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;472 expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);473 });474}475476export async function477transferFromExpectSuccess(collectionId: number,478 tokenId: number,479 accountApproved: IKeyringPair, 480 accountFrom: IKeyringPair, 481 accountTo: IKeyringPair, 482 value: number = 1,483 type: string = 'NFT') {484 await usingApi(async (api: ApiPromise) => {485 let balanceBefore = new BN(0);486 if (type === 'Fungible') {487 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;488 }489 const transferFromTx = await api.tx.nft.transferFrom(490 accountFrom.address, accountTo.address, collectionId, tokenId, value);491 const events = await submitTransactionAsync(accountApproved, transferFromTx);492 const result = getCreateItemResult(events);493 494 expect(result.success).to.be.true;495 if (type === 'NFT') {496 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;497 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);498 }499 if (type === 'Fungible') {500 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;501 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);502 }503 if (type === 'ReFungible') {504 const nftItemData =505 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;506 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);507 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);508 }509 });510}511512export async function513transferFromExpectFail(collectionId: number,514 tokenId: number,515 accountApproved: IKeyringPair,516 accountFrom: IKeyringPair,517 accountTo: IKeyringPair,518 value: number = 1) {519 await usingApi(async (api: ApiPromise) => {520 const transferFromTx = await api.tx.nft.transferFrom(521 accountFrom.address, accountTo.address, collectionId, tokenId, value);522 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;523 const result = getCreateCollectionResult(events);524 525 expect(result.success).to.be.false;526 });527}528529export async function530transferExpectSuccess(collectionId: number,531 tokenId: number,532 sender: IKeyringPair,533 recipient: IKeyringPair,534 value: number = 1,535 type: string = 'NFT') {536 await usingApi(async (api: ApiPromise) => {537 let balanceBefore = new BN(0);538 if (type === 'Fungible') {539 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;540 }541 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);542 const events = await submitTransactionAsync(sender, transferTx);543 const result = getCreateItemResult(events);544 545 expect(result.success).to.be.true;546 if (type === 'NFT') {547 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;548 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);549 }550 if (type === 'Fungible') {551 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;552 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);553 }554 if (type === 'ReFungible') {555 const nftItemData =556 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;557 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);558 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);559 }560 });561}562563export async function564transferExpectFail(collectionId: number,565 tokenId: number,566 sender: IKeyringPair,567 recipient: IKeyringPair,568 value: number = 1,569 type: string = 'NFT') {570 await usingApi(async (api: ApiPromise) => {571 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);572 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;573 if (events && Array.isArray(events)) {574 const result = getCreateCollectionResult(events);575 576 expect(result.success).to.be.false;577 }578 });579}580581export async function582approveExpectFail(collectionId: number,583 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {584 await usingApi(async (api: ApiPromise) => {585 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);586 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;587 const result = getCreateCollectionResult(events);588 589 expect(result.success).to.be.false;590 });591}592593export async function createItemExpectSuccess(594 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {595 let newItemId: number = 0;596 await usingApi(async (api) => {597 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);598 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();599 const AItemBalance = new BigNumber(Aitem.Value);600601 if (owner === '') {602 owner = sender.address;603 }604605 let tx;606 if (createMode === 'Fungible') {607 const createData = {fungible: {value: 10}};608 tx = api.tx.nft.createItem(collectionId, owner, createData);609 } else if (createMode === 'ReFungible') {610 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};611 tx = api.tx.nft.createItem(collectionId, owner, createData);612 } else {613 tx = api.tx.nft.createItem(collectionId, owner, createMode);614 }615 const events = await submitTransactionAsync(sender, tx);616 const result = getCreateItemResult(events);617618 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);619 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();620 const BItemBalance = new BigNumber(Bitem.Value);621622 623 624 expect(result.success).to.be.true;625 if (createMode === 'Fungible') {626 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);627 } else {628 expect(BItemCount).to.be.equal(AItemCount + 1);629 }630 expect(collectionId).to.be.equal(result.collectionId);631 expect(BItemCount).to.be.equal(result.itemId);632 newItemId = result.itemId;633 });634 return newItemId;635}636637export async function createItemExpectFailure(638 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {639 await usingApi(async (api) => {640 const tx = api.tx.nft.createItem(collectionId, owner, createMode);641 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;642 const result = getCreateItemResult(events);643644 expect(result.success).to.be.false;645 });646}647648export async function setPublicAccessModeExpectSuccess(649 sender: IKeyringPair, collectionId: number,650 accessMode: 'Normal' | 'WhiteList',651) {652 await usingApi(async (api) => {653654 655 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);656 const events = await submitTransactionAsync(sender, tx);657 const result = getGenericResult(events);658659 660 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();661662 663 664 expect(result.success).to.be.true;665 expect(collection.Access).to.be.equal(accessMode);666 });667}668669export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {670 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');671}672673export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {674 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');675}676677export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {678 await usingApi(async (api) => {679680 681 const tx = api.tx.nft.setMintPermission(collectionId, enabled);682 const events = await submitTransactionAsync(sender, tx);683 const result = getGenericResult(events);684685 686 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();687688 689 690 expect(result.success).to.be.true;691 expect(collection.MintMode).to.be.equal(enabled);692 });693}694695export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {696 await setMintPermissionExpectSuccess(sender, collectionId, true);697}698699export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {700 await usingApi(async (api) => {701 702 const tx = api.tx.nft.setMintPermission(collectionId, enabled);703 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;704 const result = getCreateCollectionResult(events);705 706 expect(result.success).to.be.false;707 });708}709710export async function isWhitelisted(collectionId: number, address: string) {711 let whitelisted: boolean = false;712 await usingApi(async (api) => {713 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;714 });715 return whitelisted;716}717718export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {719 await usingApi(async (api) => {720721 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();722723 724 const tx = api.tx.nft.addToWhiteList(collectionId, address);725 const events = await submitTransactionAsync(sender, tx);726 const result = getGenericResult(events);727728 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();729730 731 732 expect(result.success).to.be.true;733 734 expect(whiteListedBefore).to.be.false;735 736 expect(whiteListedAfter).to.be.true;737 });738}739740export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {741 await usingApi(async (api) => {742 743 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);744 const events = await submitTransactionAsync(sender, tx);745 const result = getGenericResult(events);746747 748 749 expect(result.success).to.be.true;750 });751}752753export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {754 await usingApi(async (api) => {755 756 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);757 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;758 const result = getGenericResult(events);759760 761 762 expect(result.success).to.be.false;763 });764}765766export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)767 : Promise<ICollectionInterface | null> => {768 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;769};770771export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {772 773 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();774};