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}130131type CollectionMode = Nft | Fungible | ReFungible | Invalid;132133export type CreateCollectionParams = {134 mode: CollectionMode,135 name: string,136 description: string,137 tokenPrefix: string,138};139140const defaultCreateCollectionParams: CreateCollectionParams = {141 description: 'description',142 mode: { type: 'NFT' },143 name: 'name',144 tokenPrefix: 'prefix',145}146147export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {148 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};149150 let collectionId: number = 0;151 await usingApi(async (api) => {152 153 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);154155 156 const alicePrivateKey = privateKey('//Alice');157158 let modeprm = {};159 if (mode.type === 'NFT') {160 modeprm = {nft: null};161 } else if (mode.type === 'Fungible') {162 modeprm = {fungible: mode.decimalPoints};163 } else if (mode.type === 'ReFungible') {164 modeprm = {refungible: null};165 } else if (mode.type === 'Invalid') {166 modeprm = {invalid: null};167 }168169 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);170 const events = await submitTransactionAsync(alicePrivateKey, tx);171 const result = getCreateCollectionResult(events);172173 174 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);175176 177 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();178179 180 181 expect(result.success).to.be.true;182 expect(result.collectionId).to.be.equal(BcollectionCount);183 184 expect(collection).to.be.not.null;185 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');186 expect(collection.Owner).to.be.equal(alicesPublicKey);187 expect(utf16ToStr(collection.Name)).to.be.equal(name);188 expect(utf16ToStr(collection.Description)).to.be.equal(description);189 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);190191 collectionId = result.collectionId;192 });193194 return collectionId;195}196197export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {198 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};199200 let modeprm = {};201 if (mode.type === 'NFT') {202 modeprm = {nft: null};203 } else if (mode.type === 'Fungible') {204 modeprm = {fungible: mode.decimalPoints};205 } else if (mode.type === 'ReFungible') {206 modeprm = {refungible: null};207 } else if (mode.type === 'Invalid') {208 modeprm = {invalid: null};209 }210211 await usingApi(async (api) => {212 213 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());214215 216 const alicePrivateKey = privateKey('//Alice');217 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);218 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;219 const result = getCreateCollectionResult(events);220221 222 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());223224 225 226 expect(result.success).to.be.false;227 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');228 });229}230231export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {232 let bal = new BigNumber(0);233 let unused;234 do {235 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));236 const keyring = new Keyring({ type: 'sr25519' });237 unused = keyring.addFromUri(`//${randomSeed}`);238 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());239 } while (bal.toFixed() != '0');240 return unused;241}242243export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {244 return await usingApi(async (api) => {245 const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;246 return BigInt(bn.toString());247 });248}249250export async function findNotExistingCollection(api: ApiPromise): Promise<number> {251 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;252 const newCollection: number = totalNumber + 1;253 return newCollection;254}255256function getDestroyResult(events: EventRecord[]): boolean {257 let success: boolean = false;258 events.forEach(({ phase, event: { data, method, section } }) => {259 260 if (method == 'ExtrinsicSuccess') {261 success = true;262 }263 });264 return success;265}266267export async function destroyCollectionExpectFailure(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 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;273 });274}275276export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {277 await usingApi(async (api) => {278 279 const alicePrivateKey = privateKey(senderSeed);280 const tx = api.tx.nft.destroyCollection(collectionId);281 const events = await submitTransactionAsync(alicePrivateKey, tx);282 const result = getDestroyResult(events);283284 285 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();286287 288 expect(result).to.be.true;289 expect(collection).to.be.not.null;290 expect(collection.Owner).to.be.equal(nullPublicKey);291 });292}293294export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {295 await usingApi(async (api) => {296297 298 const alicePrivateKey = privateKey('//Alice');299 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);300 const events = await submitTransactionAsync(alicePrivateKey, tx);301 const result = getGenericResult(events);302303 304 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();305306 307 expect(result.success).to.be.true;308 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());309 expect(collection.SponsorConfirmed).to.be.false;310 });311}312313export async function removeCollectionSponsorExpectSuccess(collectionId: number) {314 await usingApi(async (api) => {315316 317 const alicePrivateKey = privateKey('//Alice');318 const tx = api.tx.nft.removeCollectionSponsor(collectionId);319 const events = await submitTransactionAsync(alicePrivateKey, tx);320 const result = getGenericResult(events);321322 323 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();324325 326 expect(result.success).to.be.true;327 expect(collection.Sponsor).to.be.equal(nullPublicKey);328 expect(collection.SponsorConfirmed).to.be.false;329 });330}331332export async function removeCollectionSponsorExpectFailure(collectionId: number) {333 await usingApi(async (api) => {334335 336 const alicePrivateKey = privateKey('//Alice');337 const tx = api.tx.nft.removeCollectionSponsor(collectionId);338 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;339 });340}341342export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {343 await usingApi(async (api) => {344345 346 const alicePrivateKey = privateKey(senderSeed);347 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);348 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;349 });350}351352export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {353 await usingApi(async (api) => {354355 356 const sender = privateKey(senderSeed);357 const tx = api.tx.nft.confirmSponsorship(collectionId);358 const events = await submitTransactionAsync(sender, tx);359 const result = getGenericResult(events);360361 362 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();363364 365 expect(result.success).to.be.true;366 expect(collection.Sponsor).to.be.equal(sender.address);367 expect(collection.SponsorConfirmed).to.be.true;368 });369}370371372export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {373 await usingApi(async (api) => {374375 376 const sender = privateKey(senderSeed);377 const tx = api.tx.nft.confirmSponsorship(collectionId);378 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;379 });380}381382export async function enableContractSponsoringExpectSuccess(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 submitTransactionAsync(sender, tx);386 const result = getGenericResult(events);387388 expect(result.success).to.be.true;389 });390}391392export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {393 await usingApi(async (api) => {394 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);395 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;396 const result = getGenericResult(events);397398 expect(result.success).to.be.false;399 });400}401402export async function setContractSponsoringRateLimitExpectSuccess(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 submitTransactionAsync(sender, tx);406 const result = getGenericResult(events);407408 expect(result.success).to.be.true;409 });410}411412export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {413 await usingApi(async (api) => {414 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);415 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;416 const result = getGenericResult(events);417418 expect(result.success).to.be.false;419 });420}421422export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {423 await usingApi(async (api) => {424 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);425 const events = await submitTransactionAsync(sender, tx);426 const result = getGenericResult(events);427428 expect(result.success).to.be.true;429 });430}431432export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {433 let whitelisted: boolean = false;434 await usingApi(async (api) => {435 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;436 });437 return whitelisted;438}439440export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {441 await usingApi(async (api) => {442 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);443 const events = await submitTransactionAsync(sender, tx);444 const result = getGenericResult(events);445446 expect(result.success).to.be.true;447 });448}449450export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {451 await usingApi(async (api) => {452 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);453 const events = await submitTransactionAsync(sender, tx);454 const result = getGenericResult(events);455456 expect(result.success).to.be.true;457 });458}459460export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {461 await usingApi(async (api) => {462 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);463 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;464 const result = getGenericResult(events);465466 expect(result.success).to.be.false;467 });468}469470export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {471 await usingApi(async (api) => {472 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));473 const events = await submitTransactionAsync(sender, tx);474 const result = getGenericResult(events);475476 expect(result.success).to.be.true;477 });478}479480export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {481 await usingApi(async (api) => {482 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));483 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;484 });485}486487export interface CreateFungibleData {488 readonly Value: bigint;489}490491export interface CreateReFungibleData { }492export interface CreateNftData { }493494export type CreateItemData = {495 NFT: CreateNftData;496} | {497 Fungible: CreateFungibleData;498} | {499 ReFungible: CreateReFungibleData;500};501502export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {503 await usingApi(async (api) => {504 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);505 const events = await submitTransactionAsync(owner, tx);506 const result = getGenericResult(events);507 508 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();509 510 511 expect(result.success).to.be.true;512 513 expect(item).to.be.not.null;514 expect(item.Owner).to.be.equal(nullPublicKey);515 });516}517518export async function519approveExpectSuccess(collectionId: number,520 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) { 521 await usingApi(async (api: ApiPromise) => {522 const allowanceBefore =523 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;524 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);525 const events = await submitTransactionAsync(owner, approveNftTx);526 const result = getCreateItemResult(events);527 528 expect(result.success).to.be.true;529 const allowanceAfter =530 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;531 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());532 });533}534535export async function536transferFromExpectSuccess(collectionId: number,537 tokenId: number,538 accountApproved: IKeyringPair, 539 accountFrom: IKeyringPair, 540 accountTo: IKeyringPair, 541 value: number | bigint = 1,542 type: string = 'NFT') {543 await usingApi(async (api: ApiPromise) => {544 let balanceBefore = new BN(0);545 if (type === 'Fungible') {546 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;547 }548 const transferFromTx = await api.tx.nft.transferFrom(549 accountFrom.address, accountTo.address, collectionId, tokenId, value);550 const events = await submitTransactionAsync(accountApproved, transferFromTx);551 const result = getCreateItemResult(events);552 553 expect(result.success).to.be.true;554 if (type === 'NFT') {555 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;556 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);557 }558 if (type === 'Fungible') {559 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;560 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());561 }562 if (type === 'ReFungible') {563 const nftItemData =564 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;565 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);566 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);567 }568 });569}570571export async function572transferFromExpectFail(collectionId: number,573 tokenId: number,574 accountApproved: IKeyringPair,575 accountFrom: IKeyringPair,576 accountTo: IKeyringPair,577 value: number | bigint = 1) {578 await usingApi(async (api: ApiPromise) => {579 const transferFromTx = await api.tx.nft.transferFrom(580 accountFrom.address, accountTo.address, collectionId, tokenId, value);581 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;582 const result = getCreateCollectionResult(events);583 584 expect(result.success).to.be.false;585 });586}587588export async function589transferExpectSuccess(collectionId: number,590 tokenId: number,591 sender: IKeyringPair,592 recipient: IKeyringPair,593 value: number | bigint = 1,594 type: string = 'NFT') {595 await usingApi(async (api: ApiPromise) => {596 let balanceBefore = new BN(0);597 if (type === 'Fungible') {598 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;599 }600 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);601 const events = await submitTransactionAsync(sender, transferTx);602 const result = getCreateItemResult(events);603 604 expect(result.success).to.be.true;605 if (type === 'NFT') {606 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;607 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);608 }609 if (type === 'Fungible') {610 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;611 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());612 }613 if (type === 'ReFungible') {614 const nftItemData =615 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;616 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);617 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);618 }619 });620}621622export async function623transferExpectFail(collectionId: number,624 tokenId: number,625 sender: IKeyringPair,626 recipient: IKeyringPair,627 value: number | bigint = 1,628 type: string = 'NFT') {629 await usingApi(async (api: ApiPromise) => {630 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);631 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;632 if (events && Array.isArray(events)) {633 const result = getCreateCollectionResult(events);634 635 expect(result.success).to.be.false;636 }637 });638}639640export async function641approveExpectFail(collectionId: number,642 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {643 await usingApi(async (api: ApiPromise) => {644 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);645 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;646 const result = getCreateCollectionResult(events);647 648 expect(result.success).to.be.false;649 });650}651652export async function getFungibleBalance(653 collectionId: number,654 owner: string,655) {656 return await usingApi(async (api) => {657 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};658 return BigInt(response.Value);659 });660}661662export async function createFungibleItemExpectSuccess(663 sender: IKeyringPair,664 collectionId: number,665 data: CreateFungibleData,666 owner: string = sender.address,667) {668 return await usingApi(async (api) => {669 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });670671 const events = await submitTransactionAsync(sender, tx);672 const result = getCreateItemResult(events);673674 expect(result.success).to.be.true;675 return result.itemId;676 });677}678679export async function createItemExpectSuccess(680 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {681 let newItemId: number = 0;682 await usingApi(async (api) => {683 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);684 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();685 const AItemBalance = new BigNumber(Aitem.Value);686687 if (owner === '') {688 owner = sender.address;689 }690691 let tx;692 if (createMode === 'Fungible') {693 const createData = {fungible: {value: 10}};694 tx = api.tx.nft.createItem(collectionId, owner, createData);695 } else if (createMode === 'ReFungible') {696 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};697 tx = api.tx.nft.createItem(collectionId, owner, createData);698 } else {699 tx = api.tx.nft.createItem(collectionId, owner, createMode);700 }701 const events = await submitTransactionAsync(sender, tx);702 const result = getCreateItemResult(events);703704 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);705 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();706 const BItemBalance = new BigNumber(Bitem.Value);707708 709 710 expect(result.success).to.be.true;711 if (createMode === 'Fungible') {712 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);713 } else {714 expect(BItemCount).to.be.equal(AItemCount + 1);715 }716 expect(collectionId).to.be.equal(result.collectionId);717 expect(BItemCount).to.be.equal(result.itemId);718 newItemId = result.itemId;719 });720 return newItemId;721}722723export async function createItemExpectFailure(724 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {725 await usingApi(async (api) => {726 const tx = api.tx.nft.createItem(collectionId, owner, createMode);727 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;728 const result = getCreateItemResult(events);729730 expect(result.success).to.be.false;731 });732}733734export async function setPublicAccessModeExpectSuccess(735 sender: IKeyringPair, collectionId: number,736 accessMode: 'Normal' | 'WhiteList',737) {738 await usingApi(async (api) => {739740 741 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);742 const events = await submitTransactionAsync(sender, tx);743 const result = getGenericResult(events);744745 746 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();747748 749 750 expect(result.success).to.be.true;751 expect(collection.Access).to.be.equal(accessMode);752 });753}754755export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {756 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');757}758759export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {760 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');761}762763export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {764 await usingApi(async (api) => {765766 767 const tx = api.tx.nft.setMintPermission(collectionId, enabled);768 const events = await submitTransactionAsync(sender, tx);769 const result = getGenericResult(events);770771 772 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();773774 775 776 expect(result.success).to.be.true;777 expect(collection.MintMode).to.be.equal(enabled);778 });779}780781export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {782 await setMintPermissionExpectSuccess(sender, collectionId, true);783}784785export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {786 await usingApi(async (api) => {787 788 const tx = api.tx.nft.setMintPermission(collectionId, enabled);789 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;790 const result = getCreateCollectionResult(events);791 792 expect(result.success).to.be.false;793 });794}795796export async function isWhitelisted(collectionId: number, address: string) {797 let whitelisted: boolean = false;798 await usingApi(async (api) => {799 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;800 });801 return whitelisted;802}803804export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {805 await usingApi(async (api) => {806807 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();808809 810 const tx = api.tx.nft.addToWhiteList(collectionId, address);811 const events = await submitTransactionAsync(sender, tx);812 const result = getGenericResult(events);813814 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();815816 817 818 expect(result.success).to.be.true;819 820 expect(whiteListedBefore).to.be.false;821 822 expect(whiteListedAfter).to.be.true;823 });824}825826export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {827 await usingApi(async (api) => {828 829 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);830 const events = await submitTransactionAsync(sender, tx);831 const result = getGenericResult(events);832833 834 835 expect(result.success).to.be.true;836 });837}838839export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {840 await usingApi(async (api) => {841 842 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);843 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;844 const result = getGenericResult(events);845846 847 848 expect(result.success).to.be.false;849 });850}851852export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)853 : Promise<ICollectionInterface | null> => {854 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;855};856857export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {858 859 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();860};