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 recipient: string;40}4142interface TransferResult {43 success: boolean;44 collectionId: number;45 itemId: number;46 sender: string;47 recipient: string;48 value: bigint;49}5051interface IReFungibleOwner {52 Fraction: BN;53 Owner: number[];54}5556interface ITokenDataType {57 Owner: number[];58 ConstData: number[];59 VariableData: number[];60}6162interface IFungibleTokenDataType {63 Value: BN;64}6566export interface IReFungibleTokenDataType {67 Owner: IReFungibleOwner[];68 ConstData: number[];69 VariableData: number[];70}7172export function getGenericResult(events: EventRecord[]): GenericResult {73 const result: GenericResult = {74 success: false,75 };76 events.forEach(({ phase, event: { data, method, section } }) => {77 78 if (method === 'ExtrinsicSuccess') {79 result.success = true;80 }81 });82 return result;83}8485export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {86 let success = false;87 let collectionId: number = 0;88 events.forEach(({ phase, event: { data, method, section } }) => {89 90 if (method == 'ExtrinsicSuccess') {91 success = true;92 } else if ((section == 'nft') && (method == 'Created')) {93 collectionId = parseInt(data[0].toString());94 }95 });96 const result: CreateCollectionResult = {97 success,98 collectionId,99 };100 return result;101}102103export function getCreateItemResult(events: EventRecord[]): CreateItemResult {104 let success = false;105 let collectionId: number = 0;106 let itemId: number = 0;107 let recipient: string = '';108 events.forEach(({ phase, event: { data, method, section } }) => {109 110 if (method == 'ExtrinsicSuccess') {111 success = true;112 } else if ((section == 'nft') && (method == 'ItemCreated')) {113 collectionId = parseInt(data[0].toString());114 itemId = parseInt(data[1].toString());115 recipient = data[2].toString();116 }117 });118 const result: CreateItemResult = {119 success,120 collectionId,121 itemId,122 recipient,123 };124 return result;125}126127export function getTransferResult(events: EventRecord[]): TransferResult {128 const result: TransferResult = {129 success: false,130 collectionId: 0,131 itemId: 0,132 sender: '',133 recipient: '',134 value: 0n,135 };136137 events.forEach(({event: {data, method, section}}) => {138 if (method === 'ExtrinsicSuccess') {139 result.success = true;140 } else if (section === 'nft' && method === 'Transfer') {141 result.collectionId = +data[0].toString();142 result.itemId = +data[1].toString();143 result.sender = data[2].toString();144 result.recipient = data[3].toString();145 result.value = BigInt(data[4].toString());146 }147 });148149 return result;150}151152interface Invalid {153 type: 'Invalid';154}155156interface Nft {157 type: 'NFT';158}159160interface Fungible {161 type: 'Fungible';162 decimalPoints: number;163}164165interface ReFungible {166 type: 'ReFungible';167}168169type CollectionMode = Nft | Fungible | ReFungible | Invalid;170171export type CreateCollectionParams = {172 mode: CollectionMode,173 name: string,174 description: string,175 tokenPrefix: string,176};177178const defaultCreateCollectionParams: CreateCollectionParams = {179 description: 'description',180 mode: { type: 'NFT' },181 name: 'name',182 tokenPrefix: 'prefix',183}184185export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {186 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};187188 let collectionId: number = 0;189 await usingApi(async (api) => {190 191 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);192193 194 const alicePrivateKey = privateKey('//Alice');195196 let modeprm = {};197 if (mode.type === 'NFT') {198 modeprm = {nft: null};199 } else if (mode.type === 'Fungible') {200 modeprm = {fungible: mode.decimalPoints};201 } else if (mode.type === 'ReFungible') {202 modeprm = {refungible: null};203 } else if (mode.type === 'Invalid') {204 modeprm = {invalid: null};205 }206207 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);208 const events = await submitTransactionAsync(alicePrivateKey, tx);209 const result = getCreateCollectionResult(events);210211 212 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);213214 215 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();216217 218 219 expect(result.success).to.be.true;220 expect(result.collectionId).to.be.equal(BcollectionCount);221 222 expect(collection).to.be.not.null;223 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');224 expect(collection.Owner).to.be.equal(alicesPublicKey);225 expect(utf16ToStr(collection.Name)).to.be.equal(name);226 expect(utf16ToStr(collection.Description)).to.be.equal(description);227 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);228229 collectionId = result.collectionId;230 });231232 return collectionId;233}234235export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {236 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};237238 let modeprm = {};239 if (mode.type === 'NFT') {240 modeprm = {nft: null};241 } else if (mode.type === 'Fungible') {242 modeprm = {fungible: mode.decimalPoints};243 } else if (mode.type === 'ReFungible') {244 modeprm = {refungible: null};245 } else if (mode.type === 'Invalid') {246 modeprm = {invalid: null};247 }248249 await usingApi(async (api) => {250 251 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());252253 254 const alicePrivateKey = privateKey('//Alice');255 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);256 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;257 const result = getCreateCollectionResult(events);258259 260 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());261262 263 264 expect(result.success).to.be.false;265 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');266 });267}268269export async function findUnusedAddress(api: ApiPromise, seedAddition = ''): Promise<IKeyringPair> {270 let bal = new BigNumber(0);271 let unused;272 do {273 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;274 const keyring = new Keyring({ type: 'sr25519' });275 unused = keyring.addFromUri(`//${randomSeed}`);276 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());277 } while (bal.toFixed() != '0');278 return unused;279}280281export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {282 return await usingApi(async (api) => {283 const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;284 return BigInt(bn.toString());285 });286}287288export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {289 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));290}291292export async function findNotExistingCollection(api: ApiPromise): Promise<number> {293 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;294 const newCollection: number = totalNumber + 1;295 return newCollection;296}297298function getDestroyResult(events: EventRecord[]): boolean {299 let success: boolean = false;300 events.forEach(({ phase, event: { data, method, section } }) => {301 302 if (method == 'ExtrinsicSuccess') {303 success = true;304 }305 });306 return success;307}308309export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {310 await usingApi(async (api) => {311 312 const alicePrivateKey = privateKey(senderSeed);313 const tx = api.tx.nft.destroyCollection(collectionId);314 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;315 });316}317318export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {319 await usingApi(async (api) => {320 321 const alicePrivateKey = privateKey(senderSeed);322 const tx = api.tx.nft.destroyCollection(collectionId);323 const events = await submitTransactionAsync(alicePrivateKey, tx);324 const result = getDestroyResult(events);325326 327 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();328329 330 expect(result).to.be.true;331 expect(collection).to.be.not.null;332 expect(collection.Owner).to.be.equal(nullPublicKey);333 });334}335336export async function queryCollectionLimits(collectionId: number) {337 return await usingApi(async (api) => {338 return ((await api.query.nft.collection(collectionId)).toJSON() as any).Limits;339 });340}341342export async function setCollectionLimitsExpectSuccess(sender: IKeyringPair, collectionId: number, limits: any) {343 await usingApi(async (api) => {344 const oldLimits = await queryCollectionLimits(collectionId);345 const newLimits = { ...oldLimits as any, ...limits };346 const tx = api.tx.nft.setCollectionLimits(collectionId, newLimits);347 const events = await submitTransactionAsync(sender, tx);348 const result = getGenericResult(events);349350 expect(result.success).to.be.true;351 });352}353354export async function setCollectionLimitsExpectFailure(sender: IKeyringPair, collectionId: number, limits: any) {355 await usingApi(async (api) => {356 const oldLimits = await queryCollectionLimits(collectionId);357 const newLimits = { ...oldLimits as any, ...limits };358 const tx = api.tx.nft.setCollectionLimits(collectionId, newLimits);359 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;360 const result = getGenericResult(events);361362 expect(result.success).to.be.false;363 });364}365366export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {367 await usingApi(async (api) => {368369 370 const alicePrivateKey = privateKey('//Alice');371 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);372 const events = await submitTransactionAsync(alicePrivateKey, tx);373 const result = getGenericResult(events);374375 376 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();377378 379 expect(result.success).to.be.true;380 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());381 expect(collection.SponsorConfirmed).to.be.false;382 });383}384385export async function removeCollectionSponsorExpectSuccess(collectionId: number) {386 await usingApi(async (api) => {387388 389 const alicePrivateKey = privateKey('//Alice');390 const tx = api.tx.nft.removeCollectionSponsor(collectionId);391 const events = await submitTransactionAsync(alicePrivateKey, tx);392 const result = getGenericResult(events);393394 395 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();396397 398 expect(result.success).to.be.true;399 expect(collection.Sponsor).to.be.equal(nullPublicKey);400 expect(collection.SponsorConfirmed).to.be.false;401 });402}403404export async function removeCollectionSponsorExpectFailure(collectionId: number) {405 await usingApi(async (api) => {406407 408 const alicePrivateKey = privateKey('//Alice');409 const tx = api.tx.nft.removeCollectionSponsor(collectionId);410 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;411 });412}413414export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {415 await usingApi(async (api) => {416417 418 const alicePrivateKey = privateKey(senderSeed);419 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);420 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;421 });422}423424export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {425 await usingApi(async (api) => {426427 428 const sender = privateKey(senderSeed);429 const tx = api.tx.nft.confirmSponsorship(collectionId);430 const events = await submitTransactionAsync(sender, tx);431 const result = getGenericResult(events);432433 434 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();435436 437 expect(result.success).to.be.true;438 expect(collection.Sponsor).to.be.equal(sender.address);439 expect(collection.SponsorConfirmed).to.be.true;440 });441}442443444export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {445 await usingApi(async (api) => {446447 448 const sender = privateKey(senderSeed);449 const tx = api.tx.nft.confirmSponsorship(collectionId);450 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;451 });452}453454export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {455 await usingApi(async (api) => {456 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);457 const events = await submitTransactionAsync(sender, tx);458 const result = getGenericResult(events);459460 expect(result.success).to.be.true;461 });462}463464export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {465 await usingApi(async (api) => {466 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);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 setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {475 await usingApi(async (api) => {476 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);477 const events = await submitTransactionAsync(sender, tx);478 const result = getGenericResult(events);479480 expect(result.success).to.be.true;481 });482}483484export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {485 await usingApi(async (api) => {486 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);487 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;488 const result = getGenericResult(events);489490 expect(result.success).to.be.false;491 });492}493494export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {495 await usingApi(async (api) => {496 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);497 const events = await submitTransactionAsync(sender, tx);498 const result = getGenericResult(events);499500 expect(result.success).to.be.true;501 });502}503504export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {505 let whitelisted: boolean = false;506 await usingApi(async (api) => {507 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;508 });509 return whitelisted;510}511512export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {513 await usingApi(async (api) => {514 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);515 const events = await submitTransactionAsync(sender, tx);516 const result = getGenericResult(events);517518 expect(result.success).to.be.true;519 });520}521522export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {523 await usingApi(async (api) => {524 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);525 const events = await submitTransactionAsync(sender, tx);526 const result = getGenericResult(events);527528 expect(result.success).to.be.true;529 });530}531532export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {533 await usingApi(async (api) => {534 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);535 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;536 const result = getGenericResult(events);537538 expect(result.success).to.be.false;539 });540}541542export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {543 await usingApi(async (api) => {544 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));545 const events = await submitTransactionAsync(sender, tx);546 const result = getGenericResult(events);547548 expect(result.success).to.be.true;549 });550}551552export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {553 await usingApi(async (api) => {554 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));555 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;556 });557}558559export async function setVariableMetaDataSponsoringRateLimitExpectSuccess(sender: IKeyringPair, collectionId: number, rateLimit: number) {560 await usingApi(async (api) => {561 const tx = api.tx.nft.setVariableMetaDataSponsoringRateLimit(collectionId, rateLimit);562 const events = await submitTransactionAsync(sender, tx);563 const result = getGenericResult(events);564565 expect(result.success).to.be.true;566 });567}568569export async function setVariableMetaDataSponsoringRateLimitExpectFailure(sender: IKeyringPair, collectionId: number, rateLimit: number) {570 await usingApi(async (api) => {571 const tx = api.tx.nft.setVariableMetaDataSponsoringRateLimit(collectionId, rateLimit);572 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;573 const result = getGenericResult(events);574575 expect(result.success).to.be.false;576 });577}578579export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {580 await usingApi(async (api) => {581 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));582 const events = await submitTransactionAsync(sender, tx);583 const result = getGenericResult(events);584585 expect(result.success).to.be.true;586 });587}588589export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {590 await usingApi(async (api) => {591 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));592 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;593 });594}595596export interface CreateFungibleData {597 readonly Value: bigint;598}599600export interface CreateReFungibleData { }601export interface CreateNftData { }602603export type CreateItemData = {604 NFT: CreateNftData;605} | {606 Fungible: CreateFungibleData;607} | {608 ReFungible: CreateReFungibleData;609};610611export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {612 await usingApi(async (api) => {613 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);614 const events = await submitTransactionAsync(owner, tx);615 const result = getGenericResult(events);616 617 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();618 619 620 expect(result.success).to.be.true;621 622 expect(item).to.be.not.null;623 expect(item.Owner).to.be.equal(nullPublicKey);624 });625}626627export async function628approveExpectSuccess(collectionId: number,629 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {630 await usingApi(async (api: ApiPromise) => {631 const allowanceBefore =632 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;633 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);634 const events = await submitTransactionAsync(owner, approveNftTx);635 const result = getCreateItemResult(events);636 637 expect(result.success).to.be.true;638 const allowanceAfter =639 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;640 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());641 });642}643644export async function645transferFromExpectSuccess(collectionId: number,646 tokenId: number,647 accountApproved: IKeyringPair,648 accountFrom: IKeyringPair,649 accountTo: IKeyringPair,650 value: number | bigint = 1,651 type: string = 'NFT') {652 await usingApi(async (api: ApiPromise) => {653 let balanceBefore = new BN(0);654 if (type === 'Fungible') {655 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;656 }657 const transferFromTx = await api.tx.nft.transferFrom(658 accountFrom.address, accountTo.address, collectionId, tokenId, value);659 const events = await submitTransactionAsync(accountApproved, transferFromTx);660 const result = getCreateItemResult(events);661 662 expect(result.success).to.be.true;663 if (type === 'NFT') {664 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;665 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);666 }667 if (type === 'Fungible') {668 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;669 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());670 }671 if (type === 'ReFungible') {672 const nftItemData =673 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;674 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);675 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);676 }677 });678}679680export async function681transferFromExpectFail(collectionId: number,682 tokenId: number,683 accountApproved: IKeyringPair,684 accountFrom: IKeyringPair,685 accountTo: IKeyringPair,686 value: number | bigint = 1) {687 await usingApi(async (api: ApiPromise) => {688 const transferFromTx = await api.tx.nft.transferFrom(689 accountFrom.address, accountTo.address, collectionId, tokenId, value);690 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;691 const result = getCreateCollectionResult(events);692 693 expect(result.success).to.be.false;694 });695}696697export async function698transferExpectSuccess(collectionId: number,699 tokenId: number,700 sender: IKeyringPair,701 recipient: IKeyringPair,702 value: number | bigint = 1,703 type: string = 'NFT') {704 await usingApi(async (api: ApiPromise) => {705 let balanceBefore = new BN(0);706 if (type === 'Fungible') {707 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;708 }709 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);710 const events = await submitTransactionAsync(sender, transferTx);711 const result = getTransferResult(events);712 713 expect(result.success).to.be.true;714 expect(result.collectionId).to.be.equal(collectionId);715 expect(result.itemId).to.be.equal(tokenId);716 expect(result.sender).to.be.equal(sender.address);717 expect(result.recipient).to.be.equal(recipient.address);718 expect(result.value.toString()).to.be.equal(value.toString());719 if (type === 'NFT') {720 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;721 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);722 }723 if (type === 'Fungible') {724 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;725 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());726 }727 if (type === 'ReFungible') {728 const nftItemData =729 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;730 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);731 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);732 }733 });734}735736export async function737transferExpectFail(collectionId: number,738 tokenId: number,739 sender: IKeyringPair,740 recipient: IKeyringPair,741 value: number | bigint = 1,742 type: string = 'NFT') {743 await usingApi(async (api: ApiPromise) => {744 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);745 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;746 if (events && Array.isArray(events)) {747 const result = getCreateCollectionResult(events);748 749 expect(result.success).to.be.false;750 }751 });752}753754export async function755approveExpectFail(collectionId: number,756 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {757 await usingApi(async (api: ApiPromise) => {758 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);759 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;760 const result = getCreateCollectionResult(events);761 762 expect(result.success).to.be.false;763 });764}765766export async function getFungibleBalance(767 collectionId: number,768 owner: string,769) {770 return await usingApi(async (api) => {771 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};772 return BigInt(response.Value);773 });774}775776export async function createFungibleItemExpectSuccess(777 sender: IKeyringPair,778 collectionId: number,779 data: CreateFungibleData,780 owner: string = sender.address,781) {782 return await usingApi(async (api) => {783 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });784785 const events = await submitTransactionAsync(sender, tx);786 const result = getCreateItemResult(events);787788 expect(result.success).to.be.true;789 return result.itemId;790 });791}792793export async function createItemExpectSuccess(794 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {795 let newItemId: number = 0;796 await usingApi(async (api) => {797 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);798 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();799 const AItemBalance = new BigNumber(Aitem.Value);800801 if (owner === '') {802 owner = sender.address;803 }804805 let tx;806 if (createMode === 'Fungible') {807 const createData = {fungible: {value: 10}};808 tx = api.tx.nft.createItem(collectionId, owner, createData);809 } else if (createMode === 'ReFungible') {810 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};811 tx = api.tx.nft.createItem(collectionId, owner, createData);812 } else {813 tx = api.tx.nft.createItem(collectionId, owner, createMode);814 }815 const events = await submitTransactionAsync(sender, tx);816 const result = getCreateItemResult(events);817818 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);819 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();820 const BItemBalance = new BigNumber(Bitem.Value);821822 823 824 expect(result.success).to.be.true;825 if (createMode === 'Fungible') {826 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);827 } else {828 expect(BItemCount).to.be.equal(AItemCount + 1);829 }830 expect(collectionId).to.be.equal(result.collectionId);831 expect(BItemCount).to.be.equal(result.itemId);832 expect(owner).to.be.equal(result.recipient);833 newItemId = result.itemId;834 });835 return newItemId;836}837838export async function createItemExpectFailure(839 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {840 await usingApi(async (api) => {841 const tx = api.tx.nft.createItem(collectionId, owner, createMode);842 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;843 const result = getCreateItemResult(events);844845 expect(result.success).to.be.false;846 });847}848849export async function setPublicAccessModeExpectSuccess(850 sender: IKeyringPair, collectionId: number,851 accessMode: 'Normal' | 'WhiteList',852) {853 await usingApi(async (api) => {854855 856 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);857 const events = await submitTransactionAsync(sender, tx);858 const result = getGenericResult(events);859860 861 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();862863 864 865 expect(result.success).to.be.true;866 expect(collection.Access).to.be.equal(accessMode);867 });868}869870export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {871 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');872}873874export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {875 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');876}877878export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {879 await usingApi(async (api) => {880881 882 const tx = api.tx.nft.setMintPermission(collectionId, enabled);883 const events = await submitTransactionAsync(sender, tx);884 const result = getGenericResult(events);885886 887 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();888889 890 891 expect(result.success).to.be.true;892 expect(collection.MintMode).to.be.equal(enabled);893 });894}895896export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {897 await setMintPermissionExpectSuccess(sender, collectionId, true);898}899900export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {901 await usingApi(async (api) => {902 903 const tx = api.tx.nft.setMintPermission(collectionId, enabled);904 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;905 const result = getCreateCollectionResult(events);906 907 expect(result.success).to.be.false;908 });909}910911export async function isWhitelisted(collectionId: number, address: string) {912 let whitelisted: boolean = false;913 await usingApi(async (api) => {914 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;915 });916 return whitelisted;917}918919export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {920 await usingApi(async (api) => {921922 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();923924 925 const tx = api.tx.nft.addToWhiteList(collectionId, address);926 const events = await submitTransactionAsync(sender, tx);927 const result = getGenericResult(events);928929 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();930931 932 933 expect(result.success).to.be.true;934 935 expect(whiteListedBefore).to.be.false;936 937 expect(whiteListedAfter).to.be.true;938 });939}940941export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {942 await usingApi(async (api) => {943 944 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);945 const events = await submitTransactionAsync(sender, tx);946 const result = getGenericResult(events);947948 949 950 expect(result.success).to.be.true;951 });952}953954export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {955 await usingApi(async (api) => {956 957 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);958 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;959 const result = getGenericResult(events);960961 962 963 expect(result.success).to.be.false;964 });965}966967export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)968 : Promise<ICollectionInterface | null> => {969 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;970};971972export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {973 974 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();975};976977export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {978 return await usingApi(async (api) => {979 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;980 });981}