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 setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {337 await usingApi(async (api) => {338339 340 const alicePrivateKey = privateKey('//Alice');341 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);342 const events = await submitTransactionAsync(alicePrivateKey, tx);343 const result = getGenericResult(events);344345 346 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();347348 349 expect(result.success).to.be.true;350 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());351 expect(collection.SponsorConfirmed).to.be.false;352 });353}354355export async function removeCollectionSponsorExpectSuccess(collectionId: number) {356 await usingApi(async (api) => {357358 359 const alicePrivateKey = privateKey('//Alice');360 const tx = api.tx.nft.removeCollectionSponsor(collectionId);361 const events = await submitTransactionAsync(alicePrivateKey, tx);362 const result = getGenericResult(events);363364 365 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();366367 368 expect(result.success).to.be.true;369 expect(collection.Sponsor).to.be.equal(nullPublicKey);370 expect(collection.SponsorConfirmed).to.be.false;371 });372}373374export async function removeCollectionSponsorExpectFailure(collectionId: number) {375 await usingApi(async (api) => {376377 378 const alicePrivateKey = privateKey('//Alice');379 const tx = api.tx.nft.removeCollectionSponsor(collectionId);380 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;381 });382}383384export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {385 await usingApi(async (api) => {386387 388 const alicePrivateKey = privateKey(senderSeed);389 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);390 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;391 });392}393394export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {395 await usingApi(async (api) => {396397 398 const sender = privateKey(senderSeed);399 const tx = api.tx.nft.confirmSponsorship(collectionId);400 const events = await submitTransactionAsync(sender, tx);401 const result = getGenericResult(events);402403 404 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();405406 407 expect(result.success).to.be.true;408 expect(collection.Sponsor).to.be.equal(sender.address);409 expect(collection.SponsorConfirmed).to.be.true;410 });411}412413414export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {415 await usingApi(async (api) => {416417 418 const sender = privateKey(senderSeed);419 const tx = api.tx.nft.confirmSponsorship(collectionId);420 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;421 });422}423424export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {425 await usingApi(async (api) => {426 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);427 const events = await submitTransactionAsync(sender, tx);428 const result = getGenericResult(events);429430 expect(result.success).to.be.true;431 });432}433434export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {435 await usingApi(async (api) => {436 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);437 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;438 const result = getGenericResult(events);439440 expect(result.success).to.be.false;441 });442}443444export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {445 await usingApi(async (api) => {446 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);447 const events = await submitTransactionAsync(sender, tx);448 const result = getGenericResult(events);449450 expect(result.success).to.be.true;451 });452}453454export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {455 await usingApi(async (api) => {456 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);457 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;458 const result = getGenericResult(events);459460 expect(result.success).to.be.false;461 });462}463464export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {465 await usingApi(async (api) => {466 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);467 const events = await submitTransactionAsync(sender, tx);468 const result = getGenericResult(events);469470 expect(result.success).to.be.true;471 });472}473474export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {475 let whitelisted: boolean = false;476 await usingApi(async (api) => {477 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;478 });479 return whitelisted;480}481482export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {483 await usingApi(async (api) => {484 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);485 const events = await submitTransactionAsync(sender, tx);486 const result = getGenericResult(events);487488 expect(result.success).to.be.true;489 });490}491492export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {493 await usingApi(async (api) => {494 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);495 const events = await submitTransactionAsync(sender, tx);496 const result = getGenericResult(events);497498 expect(result.success).to.be.true;499 });500}501502export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {503 await usingApi(async (api) => {504 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);505 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;506 const result = getGenericResult(events);507508 expect(result.success).to.be.false;509 });510}511512export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {513 await usingApi(async (api) => {514 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));515 const events = await submitTransactionAsync(sender, tx);516 const result = getGenericResult(events);517518 expect(result.success).to.be.true;519 });520}521522export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {523 await usingApi(async (api) => {524 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));525 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;526 });527}528529export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {530 await usingApi(async (api) => {531 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));532 const events = await submitTransactionAsync(sender, tx);533 const result = getGenericResult(events);534535 expect(result.success).to.be.true;536 });537}538539export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {540 await usingApi(async (api) => {541 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));542 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;543 });544}545546export interface CreateFungibleData {547 readonly Value: bigint;548}549550export interface CreateReFungibleData { }551export interface CreateNftData { }552553export type CreateItemData = {554 NFT: CreateNftData;555} | {556 Fungible: CreateFungibleData;557} | {558 ReFungible: CreateReFungibleData;559};560561export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {562 await usingApi(async (api) => {563 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);564 const events = await submitTransactionAsync(owner, tx);565 const result = getGenericResult(events);566 567 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();568 569 570 expect(result.success).to.be.true;571 572 expect(item).to.be.not.null;573 expect(item.Owner).to.be.equal(nullPublicKey);574 });575}576577export async function578approveExpectSuccess(collectionId: number,579 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {580 await usingApi(async (api: ApiPromise) => {581 const allowanceBefore =582 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;583 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);584 const events = await submitTransactionAsync(owner, approveNftTx);585 const result = getCreateItemResult(events);586 587 expect(result.success).to.be.true;588 const allowanceAfter =589 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;590 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());591 });592}593594export async function595transferFromExpectSuccess(collectionId: number,596 tokenId: number,597 accountApproved: IKeyringPair,598 accountFrom: IKeyringPair,599 accountTo: IKeyringPair,600 value: number | bigint = 1,601 type: string = 'NFT') {602 await usingApi(async (api: ApiPromise) => {603 let balanceBefore = new BN(0);604 if (type === 'Fungible') {605 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;606 }607 const transferFromTx = await api.tx.nft.transferFrom(608 accountFrom.address, accountTo.address, collectionId, tokenId, value);609 const events = await submitTransactionAsync(accountApproved, transferFromTx);610 const result = getCreateItemResult(events);611 612 expect(result.success).to.be.true;613 if (type === 'NFT') {614 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;615 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);616 }617 if (type === 'Fungible') {618 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;619 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());620 }621 if (type === 'ReFungible') {622 const nftItemData =623 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;624 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);625 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);626 }627 });628}629630export async function631transferFromExpectFail(collectionId: number,632 tokenId: number,633 accountApproved: IKeyringPair,634 accountFrom: IKeyringPair,635 accountTo: IKeyringPair,636 value: number | bigint = 1) {637 await usingApi(async (api: ApiPromise) => {638 const transferFromTx = await api.tx.nft.transferFrom(639 accountFrom.address, accountTo.address, collectionId, tokenId, value);640 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;641 const result = getCreateCollectionResult(events);642 643 expect(result.success).to.be.false;644 });645}646647export async function648transferExpectSuccess(collectionId: number,649 tokenId: number,650 sender: IKeyringPair,651 recipient: IKeyringPair,652 value: number | bigint = 1,653 type: string = 'NFT') {654 await usingApi(async (api: ApiPromise) => {655 let balanceBefore = new BN(0);656 if (type === 'Fungible') {657 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;658 }659 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);660 const events = await submitTransactionAsync(sender, transferTx);661 const result = getTransferResult(events);662 663 expect(result.success).to.be.true;664 expect(result.collectionId).to.be.equal(collectionId);665 expect(result.itemId).to.be.equal(tokenId);666 expect(result.sender).to.be.equal(sender.address);667 expect(result.recipient).to.be.equal(recipient.address);668 expect(result.value.toString()).to.be.equal(value.toString());669 if (type === 'NFT') {670 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;671 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);672 }673 if (type === 'Fungible') {674 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;675 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());676 }677 if (type === 'ReFungible') {678 const nftItemData =679 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;680 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);681 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);682 }683 });684}685686export async function687transferExpectFail(collectionId: number,688 tokenId: number,689 sender: IKeyringPair,690 recipient: IKeyringPair,691 value: number | bigint = 1,692 type: string = 'NFT') {693 await usingApi(async (api: ApiPromise) => {694 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);695 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;696 if (events && Array.isArray(events)) {697 const result = getCreateCollectionResult(events);698 699 expect(result.success).to.be.false;700 }701 });702}703704export async function705approveExpectFail(collectionId: number,706 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {707 await usingApi(async (api: ApiPromise) => {708 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);709 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;710 const result = getCreateCollectionResult(events);711 712 expect(result.success).to.be.false;713 });714}715716export async function getFungibleBalance(717 collectionId: number,718 owner: string,719) {720 return await usingApi(async (api) => {721 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};722 return BigInt(response.Value);723 });724}725726export async function createFungibleItemExpectSuccess(727 sender: IKeyringPair,728 collectionId: number,729 data: CreateFungibleData,730 owner: string = sender.address,731) {732 return await usingApi(async (api) => {733 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });734735 const events = await submitTransactionAsync(sender, tx);736 const result = getCreateItemResult(events);737738 expect(result.success).to.be.true;739 return result.itemId;740 });741}742743export async function createItemExpectSuccess(744 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {745 let newItemId: number = 0;746 await usingApi(async (api) => {747 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);748 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();749 const AItemBalance = new BigNumber(Aitem.Value);750751 if (owner === '') {752 owner = sender.address;753 }754755 let tx;756 if (createMode === 'Fungible') {757 const createData = {fungible: {value: 10}};758 tx = api.tx.nft.createItem(collectionId, owner, createData);759 } else if (createMode === 'ReFungible') {760 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};761 tx = api.tx.nft.createItem(collectionId, owner, createData);762 } else {763 tx = api.tx.nft.createItem(collectionId, owner, createMode);764 }765 const events = await submitTransactionAsync(sender, tx);766 const result = getCreateItemResult(events);767768 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);769 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();770 const BItemBalance = new BigNumber(Bitem.Value);771772 773 774 expect(result.success).to.be.true;775 if (createMode === 'Fungible') {776 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);777 } else {778 expect(BItemCount).to.be.equal(AItemCount + 1);779 }780 expect(collectionId).to.be.equal(result.collectionId);781 expect(BItemCount).to.be.equal(result.itemId);782 expect(owner).to.be.equal(result.recipient);783 newItemId = result.itemId;784 });785 return newItemId;786}787788export async function createItemExpectFailure(789 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {790 await usingApi(async (api) => {791 const tx = api.tx.nft.createItem(collectionId, owner, createMode);792 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;793 const result = getCreateItemResult(events);794795 expect(result.success).to.be.false;796 });797}798799export async function setPublicAccessModeExpectSuccess(800 sender: IKeyringPair, collectionId: number,801 accessMode: 'Normal' | 'WhiteList',802) {803 await usingApi(async (api) => {804805 806 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);807 const events = await submitTransactionAsync(sender, tx);808 const result = getGenericResult(events);809810 811 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();812813 814 815 expect(result.success).to.be.true;816 expect(collection.Access).to.be.equal(accessMode);817 });818}819820export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {821 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');822}823824export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {825 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');826}827828export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {829 await usingApi(async (api) => {830831 832 const tx = api.tx.nft.setMintPermission(collectionId, enabled);833 const events = await submitTransactionAsync(sender, tx);834 const result = getGenericResult(events);835836 837 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();838839 840 841 expect(result.success).to.be.true;842 expect(collection.MintMode).to.be.equal(enabled);843 });844}845846export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {847 await setMintPermissionExpectSuccess(sender, collectionId, true);848}849850export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {851 await usingApi(async (api) => {852 853 const tx = api.tx.nft.setMintPermission(collectionId, enabled);854 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;855 const result = getCreateCollectionResult(events);856 857 expect(result.success).to.be.false;858 });859}860861export async function isWhitelisted(collectionId: number, address: string) {862 let whitelisted: boolean = false;863 await usingApi(async (api) => {864 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;865 });866 return whitelisted;867}868869export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {870 await usingApi(async (api) => {871872 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();873874 875 const tx = api.tx.nft.addToWhiteList(collectionId, address);876 const events = await submitTransactionAsync(sender, tx);877 const result = getGenericResult(events);878879 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();880881 882 883 expect(result.success).to.be.true;884 885 expect(whiteListedBefore).to.be.false;886 887 expect(whiteListedAfter).to.be.true;888 });889}890891export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {892 await usingApi(async (api) => {893 894 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);895 const events = await submitTransactionAsync(sender, tx);896 const result = getGenericResult(events);897898 899 900 expect(result.success).to.be.true;901 });902}903904export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {905 await usingApi(async (api) => {906 907 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);908 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;909 const result = getGenericResult(events);910911 912 913 expect(result.success).to.be.false;914 });915}916917export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)918 : Promise<ICollectionInterface | null> => {919 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;920};921922export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {923 924 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();925};926927export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {928 return await usingApi(async (api) => {929 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;930 });931}