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, seedAddition = ''): Promise<IKeyringPair> {232 let bal = new BigNumber(0);233 let unused;234 do {235 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;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 function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {251 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));252}253254export async function findNotExistingCollection(api: ApiPromise): Promise<number> {255 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;256 const newCollection: number = totalNumber + 1;257 return newCollection;258}259260function getDestroyResult(events: EventRecord[]): boolean {261 let success: boolean = false;262 events.forEach(({ phase, event: { data, method, section } }) => {263 264 if (method == 'ExtrinsicSuccess') {265 success = true;266 }267 });268 return success;269}270271export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {272 await usingApi(async (api) => {273 274 const alicePrivateKey = privateKey(senderSeed);275 const tx = api.tx.nft.destroyCollection(collectionId);276 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;277 });278}279280export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {281 await usingApi(async (api) => {282 283 const alicePrivateKey = privateKey(senderSeed);284 const tx = api.tx.nft.destroyCollection(collectionId);285 const events = await submitTransactionAsync(alicePrivateKey, tx);286 const result = getDestroyResult(events);287288 289 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();290291 292 expect(result).to.be.true;293 expect(collection).to.be.not.null;294 expect(collection.Owner).to.be.equal(nullPublicKey);295 });296}297298export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {299 await usingApi(async (api) => {300301 302 const alicePrivateKey = privateKey('//Alice');303 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);304 const events = await submitTransactionAsync(alicePrivateKey, tx);305 const result = getGenericResult(events);306307 308 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();309310 311 expect(result.success).to.be.true;312 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());313 expect(collection.SponsorConfirmed).to.be.false;314 });315}316317export async function removeCollectionSponsorExpectSuccess(collectionId: number) {318 await usingApi(async (api) => {319320 321 const alicePrivateKey = privateKey('//Alice');322 const tx = api.tx.nft.removeCollectionSponsor(collectionId);323 const events = await submitTransactionAsync(alicePrivateKey, tx);324 const result = getGenericResult(events);325326 327 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();328329 330 expect(result.success).to.be.true;331 expect(collection.Sponsor).to.be.equal(nullPublicKey);332 expect(collection.SponsorConfirmed).to.be.false;333 });334}335336export async function removeCollectionSponsorExpectFailure(collectionId: number) {337 await usingApi(async (api) => {338339 340 const alicePrivateKey = privateKey('//Alice');341 const tx = api.tx.nft.removeCollectionSponsor(collectionId);342 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;343 });344}345346export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {347 await usingApi(async (api) => {348349 350 const alicePrivateKey = privateKey(senderSeed);351 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);352 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;353 });354}355356export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {357 await usingApi(async (api) => {358359 360 const sender = privateKey(senderSeed);361 const tx = api.tx.nft.confirmSponsorship(collectionId);362 const events = await submitTransactionAsync(sender, tx);363 const result = getGenericResult(events);364365 366 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();367368 369 expect(result.success).to.be.true;370 expect(collection.Sponsor).to.be.equal(sender.address);371 expect(collection.SponsorConfirmed).to.be.true;372 });373}374375376export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {377 await usingApi(async (api) => {378379 380 const sender = privateKey(senderSeed);381 const tx = api.tx.nft.confirmSponsorship(collectionId);382 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;383 });384}385386export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {387 await usingApi(async (api) => {388 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);389 const events = await submitTransactionAsync(sender, tx);390 const result = getGenericResult(events);391392 expect(result.success).to.be.true;393 });394}395396export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {397 await usingApi(async (api) => {398 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);399 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;400 const result = getGenericResult(events);401402 expect(result.success).to.be.false;403 });404}405406export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {407 await usingApi(async (api) => {408 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);409 const events = await submitTransactionAsync(sender, tx);410 const result = getGenericResult(events);411412 expect(result.success).to.be.true;413 });414}415416export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {417 await usingApi(async (api) => {418 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);419 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;420 const result = getGenericResult(events);421422 expect(result.success).to.be.false;423 });424}425426export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {427 await usingApi(async (api) => {428 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);429 const events = await submitTransactionAsync(sender, tx);430 const result = getGenericResult(events);431432 expect(result.success).to.be.true;433 });434}435436export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {437 let whitelisted: boolean = false;438 await usingApi(async (api) => {439 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;440 });441 return whitelisted;442}443444export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {445 await usingApi(async (api) => {446 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);447 const events = await submitTransactionAsync(sender, tx);448 const result = getGenericResult(events);449450 expect(result.success).to.be.true;451 });452}453454export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {455 await usingApi(async (api) => {456 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);457 const events = await submitTransactionAsync(sender, tx);458 const result = getGenericResult(events);459460 expect(result.success).to.be.true;461 });462}463464export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {465 await usingApi(async (api) => {466 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);467 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;468 const result = getGenericResult(events);469470 expect(result.success).to.be.false;471 });472}473474export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {475 await usingApi(async (api) => {476 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));477 const events = await submitTransactionAsync(sender, tx);478 const result = getGenericResult(events);479480 expect(result.success).to.be.true;481 });482}483484export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {485 await usingApi(async (api) => {486 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));487 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;488 });489}490491export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {492 await usingApi(async (api) => {493 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));494 const events = await submitTransactionAsync(sender, tx);495 const result = getGenericResult(events);496497 expect(result.success).to.be.true;498 });499}500501export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {502 await usingApi(async (api) => {503 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));504 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;505 });506}507508export interface CreateFungibleData {509 readonly Value: bigint;510}511512export interface CreateReFungibleData { }513export interface CreateNftData { }514515export type CreateItemData = {516 NFT: CreateNftData;517} | {518 Fungible: CreateFungibleData;519} | {520 ReFungible: CreateReFungibleData;521};522523export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {524 await usingApi(async (api) => {525 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);526 const events = await submitTransactionAsync(owner, tx);527 const result = getGenericResult(events);528 529 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();530 531 532 expect(result.success).to.be.true;533 534 expect(item).to.be.not.null;535 expect(item.Owner).to.be.equal(nullPublicKey);536 });537}538539export async function540approveExpectSuccess(collectionId: number,541 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {542 await usingApi(async (api: ApiPromise) => {543 const allowanceBefore =544 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;545 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);546 const events = await submitTransactionAsync(owner, approveNftTx);547 const result = getCreateItemResult(events);548 549 expect(result.success).to.be.true;550 const allowanceAfter =551 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;552 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());553 });554}555556export async function557transferFromExpectSuccess(collectionId: number,558 tokenId: number,559 accountApproved: IKeyringPair,560 accountFrom: IKeyringPair,561 accountTo: IKeyringPair,562 value: number | bigint = 1,563 type: string = 'NFT') {564 await usingApi(async (api: ApiPromise) => {565 let balanceBefore = new BN(0);566 if (type === 'Fungible') {567 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;568 }569 const transferFromTx = await api.tx.nft.transferFrom(570 accountFrom.address, accountTo.address, collectionId, tokenId, value);571 const events = await submitTransactionAsync(accountApproved, transferFromTx);572 const result = getCreateItemResult(events);573 574 expect(result.success).to.be.true;575 if (type === 'NFT') {576 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;577 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);578 }579 if (type === 'Fungible') {580 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;581 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());582 }583 if (type === 'ReFungible') {584 const nftItemData =585 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;586 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);587 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);588 }589 });590}591592export async function593transferFromExpectFail(collectionId: number,594 tokenId: number,595 accountApproved: IKeyringPair,596 accountFrom: IKeyringPair,597 accountTo: IKeyringPair,598 value: number | bigint = 1) {599 await usingApi(async (api: ApiPromise) => {600 const transferFromTx = await api.tx.nft.transferFrom(601 accountFrom.address, accountTo.address, collectionId, tokenId, value);602 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;603 const result = getCreateCollectionResult(events);604 605 expect(result.success).to.be.false;606 });607}608609export async function610transferExpectSuccess(collectionId: number,611 tokenId: number,612 sender: IKeyringPair,613 recipient: IKeyringPair,614 value: number | bigint = 1,615 type: string = 'NFT') {616 await usingApi(async (api: ApiPromise) => {617 let balanceBefore = new BN(0);618 if (type === 'Fungible') {619 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;620 }621 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);622 const events = await submitTransactionAsync(sender, transferTx);623 const result = getCreateItemResult(events);624 625 expect(result.success).to.be.true;626 if (type === 'NFT') {627 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;628 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);629 }630 if (type === 'Fungible') {631 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;632 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());633 }634 if (type === 'ReFungible') {635 const nftItemData =636 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;637 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);638 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);639 }640 });641}642643export async function644transferExpectFail(collectionId: number,645 tokenId: number,646 sender: IKeyringPair,647 recipient: IKeyringPair,648 value: number | bigint = 1,649 type: string = 'NFT') {650 await usingApi(async (api: ApiPromise) => {651 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);652 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;653 if (events && Array.isArray(events)) {654 const result = getCreateCollectionResult(events);655 656 expect(result.success).to.be.false;657 }658 });659}660661export async function662approveExpectFail(collectionId: number,663 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {664 await usingApi(async (api: ApiPromise) => {665 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);666 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;667 const result = getCreateCollectionResult(events);668 669 expect(result.success).to.be.false;670 });671}672673export async function getFungibleBalance(674 collectionId: number,675 owner: string,676) {677 return await usingApi(async (api) => {678 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};679 return BigInt(response.Value);680 });681}682683export async function createFungibleItemExpectSuccess(684 sender: IKeyringPair,685 collectionId: number,686 data: CreateFungibleData,687 owner: string = sender.address,688) {689 return await usingApi(async (api) => {690 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });691692 const events = await submitTransactionAsync(sender, tx);693 const result = getCreateItemResult(events);694695 expect(result.success).to.be.true;696 return result.itemId;697 });698}699700export async function createItemExpectSuccess(701 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {702 let newItemId: number = 0;703 await usingApi(async (api) => {704 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);705 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();706 const AItemBalance = new BigNumber(Aitem.Value);707708 if (owner === '') {709 owner = sender.address;710 }711712 let tx;713 if (createMode === 'Fungible') {714 const createData = {fungible: {value: 10}};715 tx = api.tx.nft.createItem(collectionId, owner, createData);716 } else if (createMode === 'ReFungible') {717 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};718 tx = api.tx.nft.createItem(collectionId, owner, createData);719 } else {720 tx = api.tx.nft.createItem(collectionId, owner, createMode);721 }722 const events = await submitTransactionAsync(sender, tx);723 const result = getCreateItemResult(events);724725 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);726 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();727 const BItemBalance = new BigNumber(Bitem.Value);728729 730 731 expect(result.success).to.be.true;732 if (createMode === 'Fungible') {733 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);734 } else {735 expect(BItemCount).to.be.equal(AItemCount + 1);736 }737 expect(collectionId).to.be.equal(result.collectionId);738 expect(BItemCount).to.be.equal(result.itemId);739 newItemId = result.itemId;740 });741 return newItemId;742}743744export async function createItemExpectFailure(745 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {746 await usingApi(async (api) => {747 const tx = api.tx.nft.createItem(collectionId, owner, createMode);748 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;749 const result = getCreateItemResult(events);750751 expect(result.success).to.be.false;752 });753}754755export async function setPublicAccessModeExpectSuccess(756 sender: IKeyringPair, collectionId: number,757 accessMode: 'Normal' | 'WhiteList',758) {759 await usingApi(async (api) => {760761 762 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);763 const events = await submitTransactionAsync(sender, tx);764 const result = getGenericResult(events);765766 767 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();768769 770 771 expect(result.success).to.be.true;772 expect(collection.Access).to.be.equal(accessMode);773 });774}775776export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {777 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');778}779780export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {781 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');782}783784export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {785 await usingApi(async (api) => {786787 788 const tx = api.tx.nft.setMintPermission(collectionId, enabled);789 const events = await submitTransactionAsync(sender, tx);790 const result = getGenericResult(events);791792 793 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();794795 796 797 expect(result.success).to.be.true;798 expect(collection.MintMode).to.be.equal(enabled);799 });800}801802export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {803 await setMintPermissionExpectSuccess(sender, collectionId, true);804}805806export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {807 await usingApi(async (api) => {808 809 const tx = api.tx.nft.setMintPermission(collectionId, enabled);810 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;811 const result = getCreateCollectionResult(events);812 813 expect(result.success).to.be.false;814 });815}816817export async function isWhitelisted(collectionId: number, address: string) {818 let whitelisted: boolean = false;819 await usingApi(async (api) => {820 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;821 });822 return whitelisted;823}824825export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {826 await usingApi(async (api) => {827828 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();829830 831 const tx = api.tx.nft.addToWhiteList(collectionId, address);832 const events = await submitTransactionAsync(sender, tx);833 const result = getGenericResult(events);834835 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();836837 838 839 expect(result.success).to.be.true;840 841 expect(whiteListedBefore).to.be.false;842 843 expect(whiteListedAfter).to.be.true;844 });845}846847export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {848 await usingApi(async (api) => {849 850 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);851 const events = await submitTransactionAsync(sender, tx);852 const result = getGenericResult(events);853854 855 856 expect(result.success).to.be.true;857 });858}859860export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {861 await usingApi(async (api) => {862 863 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);864 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;865 const result = getGenericResult(events);866867 868 869 expect(result.success).to.be.false;870 });871}872873export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)874 : Promise<ICollectionInterface | null> => {875 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;876};877878export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {879 880 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();881};882883export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {884 return await usingApi(async (api) => {885 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;886 });887}