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 queryCollectionLimits(collectionId: number) {299 return await usingApi(async (api) => {300 return ((await api.query.nft.collection(collectionId)).toJSON() as any).Limits;301 });302}303304export async function setCollectionLimitsExpectSuccess(sender: IKeyringPair, collectionId: number, limits: any) {305 await usingApi(async (api) => {306 const oldLimits = await queryCollectionLimits(collectionId);307 const newLimits = { ...oldLimits as any, ...limits };308 const tx = api.tx.nft.setCollectionLimits(collectionId, newLimits);309 const events = await submitTransactionAsync(sender, tx);310 const result = getGenericResult(events);311312 expect(result.success).to.be.true;313 });314}315316export async function setCollectionLimitsExpectFailure(sender: IKeyringPair, collectionId: number, limits: any) {317 await usingApi(async (api) => {318 const oldLimits = await queryCollectionLimits(collectionId);319 const newLimits = { ...oldLimits as any, ...limits };320 const tx = api.tx.nft.setCollectionLimits(collectionId, newLimits);321 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;322 const result = getGenericResult(events);323324 expect(result.success).to.be.false;325 });326}327328export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {329 await usingApi(async (api) => {330331 332 const alicePrivateKey = privateKey('//Alice');333 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);334 const events = await submitTransactionAsync(alicePrivateKey, tx);335 const result = getGenericResult(events);336337 338 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();339340 341 expect(result.success).to.be.true;342 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());343 expect(collection.SponsorConfirmed).to.be.false;344 });345}346347export async function removeCollectionSponsorExpectSuccess(collectionId: number) {348 await usingApi(async (api) => {349350 351 const alicePrivateKey = privateKey('//Alice');352 const tx = api.tx.nft.removeCollectionSponsor(collectionId);353 const events = await submitTransactionAsync(alicePrivateKey, tx);354 const result = getGenericResult(events);355356 357 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();358359 360 expect(result.success).to.be.true;361 expect(collection.Sponsor).to.be.equal(nullPublicKey);362 expect(collection.SponsorConfirmed).to.be.false;363 });364}365366export async function removeCollectionSponsorExpectFailure(collectionId: number) {367 await usingApi(async (api) => {368369 370 const alicePrivateKey = privateKey('//Alice');371 const tx = api.tx.nft.removeCollectionSponsor(collectionId);372 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;373 });374}375376export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {377 await usingApi(async (api) => {378379 380 const alicePrivateKey = privateKey(senderSeed);381 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);382 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;383 });384}385386export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {387 await usingApi(async (api) => {388389 390 const sender = privateKey(senderSeed);391 const tx = api.tx.nft.confirmSponsorship(collectionId);392 const events = await submitTransactionAsync(sender, tx);393 const result = getGenericResult(events);394395 396 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();397398 399 expect(result.success).to.be.true;400 expect(collection.Sponsor).to.be.equal(sender.address);401 expect(collection.SponsorConfirmed).to.be.true;402 });403}404405406export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {407 await usingApi(async (api) => {408409 410 const sender = privateKey(senderSeed);411 const tx = api.tx.nft.confirmSponsorship(collectionId);412 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;413 });414}415416export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {417 await usingApi(async (api) => {418 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);419 const events = await submitTransactionAsync(sender, tx);420 const result = getGenericResult(events);421422 expect(result.success).to.be.true;423 });424}425426export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {427 await usingApi(async (api) => {428 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);429 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;430 const result = getGenericResult(events);431432 expect(result.success).to.be.false;433 });434}435436export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {437 await usingApi(async (api) => {438 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);439 const events = await submitTransactionAsync(sender, tx);440 const result = getGenericResult(events);441442 expect(result.success).to.be.true;443 });444}445446export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {447 await usingApi(async (api) => {448 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);449 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;450 const result = getGenericResult(events);451452 expect(result.success).to.be.false;453 });454}455456export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {457 await usingApi(async (api) => {458 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);459 const events = await submitTransactionAsync(sender, tx);460 const result = getGenericResult(events);461462 expect(result.success).to.be.true;463 });464}465466export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {467 let whitelisted: boolean = false;468 await usingApi(async (api) => {469 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;470 });471 return whitelisted;472}473474export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {475 await usingApi(async (api) => {476 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);477 const events = await submitTransactionAsync(sender, tx);478 const result = getGenericResult(events);479480 expect(result.success).to.be.true;481 });482}483484export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {485 await usingApi(async (api) => {486 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);487 const events = await submitTransactionAsync(sender, tx);488 const result = getGenericResult(events);489490 expect(result.success).to.be.true;491 });492}493494export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {495 await usingApi(async (api) => {496 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);497 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;498 const result = getGenericResult(events);499500 expect(result.success).to.be.false;501 });502}503504export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {505 await usingApi(async (api) => {506 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));507 const events = await submitTransactionAsync(sender, tx);508 const result = getGenericResult(events);509510 expect(result.success).to.be.true;511 });512}513514export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {515 await usingApi(async (api) => {516 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));517 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;518 });519}520521export async function setVariableMetaDataSponsoringRateLimitExpectSuccess(sender: IKeyringPair, collectionId: number, rateLimit: number) {522 await usingApi(async (api) => {523 const tx = api.tx.nft.setVariableMetaDataSponsoringRateLimit(collectionId, rateLimit);524 const events = await submitTransactionAsync(sender, tx);525 const result = getGenericResult(events);526527 expect(result.success).to.be.true;528 });529}530531export async function setVariableMetaDataSponsoringRateLimitExpectFailure(sender: IKeyringPair, collectionId: number, rateLimit: number) {532 await usingApi(async (api) => {533 const tx = api.tx.nft.setVariableMetaDataSponsoringRateLimit(collectionId, rateLimit);534 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;535 const result = getGenericResult(events);536537 expect(result.success).to.be.false;538 });539}540541export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {542 await usingApi(async (api) => {543 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));544 const events = await submitTransactionAsync(sender, tx);545 const result = getGenericResult(events);546547 expect(result.success).to.be.true;548 });549}550551export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {552 await usingApi(async (api) => {553 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));554 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;555 });556}557558export interface CreateFungibleData {559 readonly Value: bigint;560}561562export interface CreateReFungibleData { }563export interface CreateNftData { }564565export type CreateItemData = {566 NFT: CreateNftData;567} | {568 Fungible: CreateFungibleData;569} | {570 ReFungible: CreateReFungibleData;571};572573export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {574 await usingApi(async (api) => {575 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);576 const events = await submitTransactionAsync(owner, tx);577 const result = getGenericResult(events);578 579 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();580 581 582 expect(result.success).to.be.true;583 584 expect(item).to.be.not.null;585 expect(item.Owner).to.be.equal(nullPublicKey);586 });587}588589export async function590approveExpectSuccess(collectionId: number,591 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {592 await usingApi(async (api: ApiPromise) => {593 const allowanceBefore =594 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;595 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);596 const events = await submitTransactionAsync(owner, approveNftTx);597 const result = getCreateItemResult(events);598 599 expect(result.success).to.be.true;600 const allowanceAfter =601 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;602 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());603 });604}605606export async function607transferFromExpectSuccess(collectionId: number,608 tokenId: number,609 accountApproved: IKeyringPair,610 accountFrom: IKeyringPair,611 accountTo: IKeyringPair,612 value: number | bigint = 1,613 type: string = 'NFT') {614 await usingApi(async (api: ApiPromise) => {615 let balanceBefore = new BN(0);616 if (type === 'Fungible') {617 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;618 }619 const transferFromTx = await api.tx.nft.transferFrom(620 accountFrom.address, accountTo.address, collectionId, tokenId, value);621 const events = await submitTransactionAsync(accountApproved, transferFromTx);622 const result = getCreateItemResult(events);623 624 expect(result.success).to.be.true;625 if (type === 'NFT') {626 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;627 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);628 }629 if (type === 'Fungible') {630 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;631 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());632 }633 if (type === 'ReFungible') {634 const nftItemData =635 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;636 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);637 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);638 }639 });640}641642export async function643transferFromExpectFail(collectionId: number,644 tokenId: number,645 accountApproved: IKeyringPair,646 accountFrom: IKeyringPair,647 accountTo: IKeyringPair,648 value: number | bigint = 1) {649 await usingApi(async (api: ApiPromise) => {650 const transferFromTx = await api.tx.nft.transferFrom(651 accountFrom.address, accountTo.address, collectionId, tokenId, value);652 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;653 const result = getCreateCollectionResult(events);654 655 expect(result.success).to.be.false;656 });657}658659export async function660transferExpectSuccess(collectionId: number,661 tokenId: number,662 sender: IKeyringPair,663 recipient: IKeyringPair,664 value: number | bigint = 1,665 type: string = 'NFT') {666 await usingApi(async (api: ApiPromise) => {667 let balanceBefore = new BN(0);668 if (type === 'Fungible') {669 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;670 }671 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);672 const events = await submitTransactionAsync(sender, transferTx);673 const result = getCreateItemResult(events);674 675 expect(result.success).to.be.true;676 if (type === 'NFT') {677 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;678 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);679 }680 if (type === 'Fungible') {681 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;682 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());683 }684 if (type === 'ReFungible') {685 const nftItemData =686 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;687 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);688 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);689 }690 });691}692693export async function694transferExpectFail(collectionId: number,695 tokenId: number,696 sender: IKeyringPair,697 recipient: IKeyringPair,698 value: number | bigint = 1,699 type: string = 'NFT') {700 await usingApi(async (api: ApiPromise) => {701 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);702 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;703 if (events && Array.isArray(events)) {704 const result = getCreateCollectionResult(events);705 706 expect(result.success).to.be.false;707 }708 });709}710711export async function712approveExpectFail(collectionId: number,713 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {714 await usingApi(async (api: ApiPromise) => {715 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);716 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;717 const result = getCreateCollectionResult(events);718 719 expect(result.success).to.be.false;720 });721}722723export async function getFungibleBalance(724 collectionId: number,725 owner: string,726) {727 return await usingApi(async (api) => {728 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};729 return BigInt(response.Value);730 });731}732733export async function createFungibleItemExpectSuccess(734 sender: IKeyringPair,735 collectionId: number,736 data: CreateFungibleData,737 owner: string = sender.address,738) {739 return await usingApi(async (api) => {740 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });741742 const events = await submitTransactionAsync(sender, tx);743 const result = getCreateItemResult(events);744745 expect(result.success).to.be.true;746 return result.itemId;747 });748}749750export async function createItemExpectSuccess(751 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {752 let newItemId: number = 0;753 await usingApi(async (api) => {754 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);755 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();756 const AItemBalance = new BigNumber(Aitem.Value);757758 if (owner === '') {759 owner = sender.address;760 }761762 let tx;763 if (createMode === 'Fungible') {764 const createData = {fungible: {value: 10}};765 tx = api.tx.nft.createItem(collectionId, owner, createData);766 } else if (createMode === 'ReFungible') {767 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};768 tx = api.tx.nft.createItem(collectionId, owner, createData);769 } else {770 tx = api.tx.nft.createItem(collectionId, owner, createMode);771 }772 const events = await submitTransactionAsync(sender, tx);773 const result = getCreateItemResult(events);774775 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);776 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();777 const BItemBalance = new BigNumber(Bitem.Value);778779 780 781 expect(result.success).to.be.true;782 if (createMode === 'Fungible') {783 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);784 } else {785 expect(BItemCount).to.be.equal(AItemCount + 1);786 }787 expect(collectionId).to.be.equal(result.collectionId);788 expect(BItemCount).to.be.equal(result.itemId);789 newItemId = result.itemId;790 });791 return newItemId;792}793794export async function createItemExpectFailure(795 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {796 await usingApi(async (api) => {797 const tx = api.tx.nft.createItem(collectionId, owner, createMode);798 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;799 const result = getCreateItemResult(events);800801 expect(result.success).to.be.false;802 });803}804805export async function setPublicAccessModeExpectSuccess(806 sender: IKeyringPair, collectionId: number,807 accessMode: 'Normal' | 'WhiteList',808) {809 await usingApi(async (api) => {810811 812 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);813 const events = await submitTransactionAsync(sender, tx);814 const result = getGenericResult(events);815816 817 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();818819 820 821 expect(result.success).to.be.true;822 expect(collection.Access).to.be.equal(accessMode);823 });824}825826export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {827 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');828}829830export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {831 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');832}833834export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {835 await usingApi(async (api) => {836837 838 const tx = api.tx.nft.setMintPermission(collectionId, enabled);839 const events = await submitTransactionAsync(sender, tx);840 const result = getGenericResult(events);841842 843 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();844845 846 847 expect(result.success).to.be.true;848 expect(collection.MintMode).to.be.equal(enabled);849 });850}851852export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {853 await setMintPermissionExpectSuccess(sender, collectionId, true);854}855856export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {857 await usingApi(async (api) => {858 859 const tx = api.tx.nft.setMintPermission(collectionId, enabled);860 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;861 const result = getCreateCollectionResult(events);862 863 expect(result.success).to.be.false;864 });865}866867export async function isWhitelisted(collectionId: number, address: string) {868 let whitelisted: boolean = false;869 await usingApi(async (api) => {870 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;871 });872 return whitelisted;873}874875export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {876 await usingApi(async (api) => {877878 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();879880 881 const tx = api.tx.nft.addToWhiteList(collectionId, address);882 const events = await submitTransactionAsync(sender, tx);883 const result = getGenericResult(events);884885 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();886887 888 889 expect(result.success).to.be.true;890 891 expect(whiteListedBefore).to.be.false;892 893 expect(whiteListedAfter).to.be.true;894 });895}896897export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {898 await usingApi(async (api) => {899 900 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);901 const events = await submitTransactionAsync(sender, tx);902 const result = getGenericResult(events);903904 905 906 expect(result.success).to.be.true;907 });908}909910export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {911 await usingApi(async (api) => {912 913 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);914 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;915 const result = getGenericResult(events);916917 918 919 expect(result.success).to.be.false;920 });921}922923export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)924 : Promise<ICollectionInterface | null> => {925 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;926};927928export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {929 930 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();931};932933export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {934 return await usingApi(async (api) => {935 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;936 });937}