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 IReFungibleOwner {43 Fraction: BN;44 Owner: number[];45}4647interface ITokenDataType {48 Owner: number[];49 ConstData: number[];50 VariableData: number[];51}5253interface IFungibleTokenDataType {54 Value: BN;55}5657export interface IReFungibleTokenDataType {58 Owner: IReFungibleOwner[];59 ConstData: number[];60 VariableData: number[];61}6263export function getGenericResult(events: EventRecord[]): GenericResult {64 const result: GenericResult = {65 success: false,66 };67 events.forEach(({ phase, event: { data, method, section } }) => {68 69 if (method === 'ExtrinsicSuccess') {70 result.success = true;71 }72 });73 return result;74}7576export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {77 let success = false;78 let collectionId: number = 0;79 events.forEach(({ phase, event: { data, method, section } }) => {80 81 if (method == 'ExtrinsicSuccess') {82 success = true;83 } else if ((section == 'nft') && (method == 'Created')) {84 collectionId = parseInt(data[0].toString());85 }86 });87 const result: CreateCollectionResult = {88 success,89 collectionId,90 };91 return result;92}9394export function getCreateItemResult(events: EventRecord[]): CreateItemResult {95 let success = false;96 let collectionId: number = 0;97 let itemId: number = 0;98 let recipient: string = '';99 events.forEach(({ phase, event: { data, method, section } }) => {100 101 if (method == 'ExtrinsicSuccess') {102 success = true;103 } else if ((section == 'nft') && (method == 'ItemCreated')) {104 collectionId = parseInt(data[0].toString());105 itemId = parseInt(data[1].toString());106 recipient = data[2].toString();107 }108 });109 const result: CreateItemResult = {110 success,111 collectionId,112 itemId,113 recipient,114 };115 return result;116}117118interface Invalid {119 type: 'Invalid';120}121122interface Nft {123 type: 'NFT';124}125126interface Fungible {127 type: 'Fungible';128 decimalPoints: number;129}130131interface ReFungible {132 type: 'ReFungible';133}134135type CollectionMode = Nft | Fungible | ReFungible | Invalid;136137export type CreateCollectionParams = {138 mode: CollectionMode,139 name: string,140 description: string,141 tokenPrefix: string,142};143144const defaultCreateCollectionParams: CreateCollectionParams = {145 description: 'description',146 mode: { type: 'NFT' },147 name: 'name',148 tokenPrefix: 'prefix',149}150151export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {152 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};153154 let collectionId: number = 0;155 await usingApi(async (api) => {156 157 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);158159 160 const alicePrivateKey = privateKey('//Alice');161162 let modeprm = {};163 if (mode.type === 'NFT') {164 modeprm = {nft: null};165 } else if (mode.type === 'Fungible') {166 modeprm = {fungible: mode.decimalPoints};167 } else if (mode.type === 'ReFungible') {168 modeprm = {refungible: null};169 } else if (mode.type === 'Invalid') {170 modeprm = {invalid: null};171 }172173 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);174 const events = await submitTransactionAsync(alicePrivateKey, tx);175 const result = getCreateCollectionResult(events);176177 178 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);179180 181 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();182183 184 185 expect(result.success).to.be.true;186 expect(result.collectionId).to.be.equal(BcollectionCount);187 188 expect(collection).to.be.not.null;189 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');190 expect(collection.Owner).to.be.equal(alicesPublicKey);191 expect(utf16ToStr(collection.Name)).to.be.equal(name);192 expect(utf16ToStr(collection.Description)).to.be.equal(description);193 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);194195 collectionId = result.collectionId;196 });197198 return collectionId;199}200201export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {202 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};203204 let modeprm = {};205 if (mode.type === 'NFT') {206 modeprm = {nft: null};207 } else if (mode.type === 'Fungible') {208 modeprm = {fungible: mode.decimalPoints};209 } else if (mode.type === 'ReFungible') {210 modeprm = {refungible: null};211 } else if (mode.type === 'Invalid') {212 modeprm = {invalid: null};213 }214215 await usingApi(async (api) => {216 217 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());218219 220 const alicePrivateKey = privateKey('//Alice');221 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);222 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;223 const result = getCreateCollectionResult(events);224225 226 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());227228 229 230 expect(result.success).to.be.false;231 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');232 });233}234235export async function findUnusedAddress(api: ApiPromise, seedAddition = ''): Promise<IKeyringPair> {236 let bal = new BigNumber(0);237 let unused;238 do {239 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;240 const keyring = new Keyring({ type: 'sr25519' });241 unused = keyring.addFromUri(`//${randomSeed}`);242 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());243 } while (bal.toFixed() != '0');244 return unused;245}246247export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {248 return await usingApi(async (api) => {249 const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;250 return BigInt(bn.toString());251 });252}253254export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {255 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));256}257258export async function findNotExistingCollection(api: ApiPromise): Promise<number> {259 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;260 const newCollection: number = totalNumber + 1;261 return newCollection;262}263264function getDestroyResult(events: EventRecord[]): boolean {265 let success: boolean = false;266 events.forEach(({ phase, event: { data, method, section } }) => {267 268 if (method == 'ExtrinsicSuccess') {269 success = true;270 }271 });272 return success;273}274275export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {276 await usingApi(async (api) => {277 278 const alicePrivateKey = privateKey(senderSeed);279 const tx = api.tx.nft.destroyCollection(collectionId);280 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;281 });282}283284export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {285 await usingApi(async (api) => {286 287 const alicePrivateKey = privateKey(senderSeed);288 const tx = api.tx.nft.destroyCollection(collectionId);289 const events = await submitTransactionAsync(alicePrivateKey, tx);290 const result = getDestroyResult(events);291292 293 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();294295 296 expect(result).to.be.true;297 expect(collection).to.be.not.null;298 expect(collection.Owner).to.be.equal(nullPublicKey);299 });300}301302export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {303 await usingApi(async (api) => {304305 306 const alicePrivateKey = privateKey('//Alice');307 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);308 const events = await submitTransactionAsync(alicePrivateKey, tx);309 const result = getGenericResult(events);310311 312 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();313314 315 expect(result.success).to.be.true;316 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());317 expect(collection.SponsorConfirmed).to.be.false;318 });319}320321export async function removeCollectionSponsorExpectSuccess(collectionId: number) {322 await usingApi(async (api) => {323324 325 const alicePrivateKey = privateKey('//Alice');326 const tx = api.tx.nft.removeCollectionSponsor(collectionId);327 const events = await submitTransactionAsync(alicePrivateKey, tx);328 const result = getGenericResult(events);329330 331 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();332333 334 expect(result.success).to.be.true;335 expect(collection.Sponsor).to.be.equal(nullPublicKey);336 expect(collection.SponsorConfirmed).to.be.false;337 });338}339340export async function removeCollectionSponsorExpectFailure(collectionId: number) {341 await usingApi(async (api) => {342343 344 const alicePrivateKey = privateKey('//Alice');345 const tx = api.tx.nft.removeCollectionSponsor(collectionId);346 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;347 });348}349350export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {351 await usingApi(async (api) => {352353 354 const alicePrivateKey = privateKey(senderSeed);355 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);356 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;357 });358}359360export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {361 await usingApi(async (api) => {362363 364 const sender = privateKey(senderSeed);365 const tx = api.tx.nft.confirmSponsorship(collectionId);366 const events = await submitTransactionAsync(sender, tx);367 const result = getGenericResult(events);368369 370 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();371372 373 expect(result.success).to.be.true;374 expect(collection.Sponsor).to.be.equal(sender.address);375 expect(collection.SponsorConfirmed).to.be.true;376 });377}378379380export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {381 await usingApi(async (api) => {382383 384 const sender = privateKey(senderSeed);385 const tx = api.tx.nft.confirmSponsorship(collectionId);386 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;387 });388}389390export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {391 await usingApi(async (api) => {392 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);393 const events = await submitTransactionAsync(sender, tx);394 const result = getGenericResult(events);395396 expect(result.success).to.be.true;397 });398}399400export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {401 await usingApi(async (api) => {402 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);403 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;404 const result = getGenericResult(events);405406 expect(result.success).to.be.false;407 });408}409410export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {411 await usingApi(async (api) => {412 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);413 const events = await submitTransactionAsync(sender, tx);414 const result = getGenericResult(events);415416 expect(result.success).to.be.true;417 });418}419420export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {421 await usingApi(async (api) => {422 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);423 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;424 const result = getGenericResult(events);425426 expect(result.success).to.be.false;427 });428}429430export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {431 await usingApi(async (api) => {432 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);433 const events = await submitTransactionAsync(sender, tx);434 const result = getGenericResult(events);435436 expect(result.success).to.be.true;437 });438}439440export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {441 let whitelisted: boolean = false;442 await usingApi(async (api) => {443 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;444 });445 return whitelisted;446}447448export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {449 await usingApi(async (api) => {450 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);451 const events = await submitTransactionAsync(sender, tx);452 const result = getGenericResult(events);453454 expect(result.success).to.be.true;455 });456}457458export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {459 await usingApi(async (api) => {460 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);461 const events = await submitTransactionAsync(sender, tx);462 const result = getGenericResult(events);463464 expect(result.success).to.be.true;465 });466}467468export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {469 await usingApi(async (api) => {470 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);471 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;472 const result = getGenericResult(events);473474 expect(result.success).to.be.false;475 });476}477478export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {479 await usingApi(async (api) => {480 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));481 const events = await submitTransactionAsync(sender, tx);482 const result = getGenericResult(events);483484 expect(result.success).to.be.true;485 });486}487488export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {489 await usingApi(async (api) => {490 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));491 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;492 });493}494495export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {496 await usingApi(async (api) => {497 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));498 const events = await submitTransactionAsync(sender, tx);499 const result = getGenericResult(events);500501 expect(result.success).to.be.true;502 });503}504505export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {506 await usingApi(async (api) => {507 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));508 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;509 });510}511512export interface CreateFungibleData {513 readonly Value: bigint;514}515516export interface CreateReFungibleData { }517export interface CreateNftData { }518519export type CreateItemData = {520 NFT: CreateNftData;521} | {522 Fungible: CreateFungibleData;523} | {524 ReFungible: CreateReFungibleData;525};526527export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {528 await usingApi(async (api) => {529 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);530 const events = await submitTransactionAsync(owner, tx);531 const result = getGenericResult(events);532 533 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();534 535 536 expect(result.success).to.be.true;537 538 expect(item).to.be.not.null;539 expect(item.Owner).to.be.equal(nullPublicKey);540 });541}542543export async function544approveExpectSuccess(collectionId: number,545 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {546 await usingApi(async (api: ApiPromise) => {547 const allowanceBefore =548 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;549 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);550 const events = await submitTransactionAsync(owner, approveNftTx);551 const result = getCreateItemResult(events);552 553 expect(result.success).to.be.true;554 const allowanceAfter =555 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;556 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());557 });558}559560export async function561transferFromExpectSuccess(collectionId: number,562 tokenId: number,563 accountApproved: IKeyringPair,564 accountFrom: IKeyringPair,565 accountTo: IKeyringPair,566 value: number | bigint = 1,567 type: string = 'NFT') {568 await usingApi(async (api: ApiPromise) => {569 let balanceBefore = new BN(0);570 if (type === 'Fungible') {571 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;572 }573 const transferFromTx = await api.tx.nft.transferFrom(574 accountFrom.address, accountTo.address, collectionId, tokenId, value);575 const events = await submitTransactionAsync(accountApproved, transferFromTx);576 const result = getCreateItemResult(events);577 578 expect(result.success).to.be.true;579 if (type === 'NFT') {580 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;581 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);582 }583 if (type === 'Fungible') {584 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;585 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());586 }587 if (type === 'ReFungible') {588 const nftItemData =589 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;590 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);591 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);592 }593 });594}595596export async function597transferFromExpectFail(collectionId: number,598 tokenId: number,599 accountApproved: IKeyringPair,600 accountFrom: IKeyringPair,601 accountTo: IKeyringPair,602 value: number | bigint = 1) {603 await usingApi(async (api: ApiPromise) => {604 const transferFromTx = await api.tx.nft.transferFrom(605 accountFrom.address, accountTo.address, collectionId, tokenId, value);606 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;607 const result = getCreateCollectionResult(events);608 609 expect(result.success).to.be.false;610 });611}612613export async function614transferExpectSuccess(collectionId: number,615 tokenId: number,616 sender: IKeyringPair,617 recipient: IKeyringPair,618 value: number | bigint = 1,619 type: string = 'NFT') {620 await usingApi(async (api: ApiPromise) => {621 let balanceBefore = new BN(0);622 if (type === 'Fungible') {623 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;624 }625 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);626 const events = await submitTransactionAsync(sender, transferTx);627 const result = getCreateItemResult(events);628 629 expect(result.success).to.be.true;630 if (type === 'NFT') {631 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;632 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);633 }634 if (type === 'Fungible') {635 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;636 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());637 }638 if (type === 'ReFungible') {639 const nftItemData =640 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;641 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);642 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);643 }644 });645}646647export async function648transferExpectFail(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 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);656 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;657 if (events && Array.isArray(events)) {658 const result = getCreateCollectionResult(events);659 660 expect(result.success).to.be.false;661 }662 });663}664665export async function666approveExpectFail(collectionId: number,667 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {668 await usingApi(async (api: ApiPromise) => {669 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);670 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;671 const result = getCreateCollectionResult(events);672 673 expect(result.success).to.be.false;674 });675}676677export async function getFungibleBalance(678 collectionId: number,679 owner: string,680) {681 return await usingApi(async (api) => {682 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};683 return BigInt(response.Value);684 });685}686687export async function createFungibleItemExpectSuccess(688 sender: IKeyringPair,689 collectionId: number,690 data: CreateFungibleData,691 owner: string = sender.address,692) {693 return await usingApi(async (api) => {694 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });695696 const events = await submitTransactionAsync(sender, tx);697 const result = getCreateItemResult(events);698699 expect(result.success).to.be.true;700 return result.itemId;701 });702}703704export async function createItemExpectSuccess(705 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {706 let newItemId: number = 0;707 await usingApi(async (api) => {708 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);709 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();710 const AItemBalance = new BigNumber(Aitem.Value);711712 if (owner === '') {713 owner = sender.address;714 }715716 let tx;717 if (createMode === 'Fungible') {718 const createData = {fungible: {value: 10}};719 tx = api.tx.nft.createItem(collectionId, owner, createData);720 } else if (createMode === 'ReFungible') {721 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};722 tx = api.tx.nft.createItem(collectionId, owner, createData);723 } else {724 tx = api.tx.nft.createItem(collectionId, owner, createMode);725 }726 const events = await submitTransactionAsync(sender, tx);727 const result = getCreateItemResult(events);728729 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);730 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();731 const BItemBalance = new BigNumber(Bitem.Value);732733 734 735 expect(result.success).to.be.true;736 if (createMode === 'Fungible') {737 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);738 } else {739 expect(BItemCount).to.be.equal(AItemCount + 1);740 }741 expect(collectionId).to.be.equal(result.collectionId);742 expect(BItemCount).to.be.equal(result.itemId);743 expect(owner).to.be.equal(result.recipient);744 newItemId = result.itemId;745 });746 return newItemId;747}748749export async function createItemExpectFailure(750 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {751 await usingApi(async (api) => {752 const tx = api.tx.nft.createItem(collectionId, owner, createMode);753 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;754 const result = getCreateItemResult(events);755756 expect(result.success).to.be.false;757 });758}759760export async function setPublicAccessModeExpectSuccess(761 sender: IKeyringPair, collectionId: number,762 accessMode: 'Normal' | 'WhiteList',763) {764 await usingApi(async (api) => {765766 767 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);768 const events = await submitTransactionAsync(sender, tx);769 const result = getGenericResult(events);770771 772 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();773774 775 776 expect(result.success).to.be.true;777 expect(collection.Access).to.be.equal(accessMode);778 });779}780781export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {782 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');783}784785export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {786 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');787}788789export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {790 await usingApi(async (api) => {791792 793 const tx = api.tx.nft.setMintPermission(collectionId, enabled);794 const events = await submitTransactionAsync(sender, tx);795 const result = getGenericResult(events);796797 798 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();799800 801 802 expect(result.success).to.be.true;803 expect(collection.MintMode).to.be.equal(enabled);804 });805}806807export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {808 await setMintPermissionExpectSuccess(sender, collectionId, true);809}810811export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {812 await usingApi(async (api) => {813 814 const tx = api.tx.nft.setMintPermission(collectionId, enabled);815 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;816 const result = getCreateCollectionResult(events);817 818 expect(result.success).to.be.false;819 });820}821822export async function isWhitelisted(collectionId: number, address: string) {823 let whitelisted: boolean = false;824 await usingApi(async (api) => {825 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;826 });827 return whitelisted;828}829830export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {831 await usingApi(async (api) => {832833 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();834835 836 const tx = api.tx.nft.addToWhiteList(collectionId, address);837 const events = await submitTransactionAsync(sender, tx);838 const result = getGenericResult(events);839840 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();841842 843 844 expect(result.success).to.be.true;845 846 expect(whiteListedBefore).to.be.false;847 848 expect(whiteListedAfter).to.be.true;849 });850}851852export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {853 await usingApi(async (api) => {854 855 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);856 const events = await submitTransactionAsync(sender, tx);857 const result = getGenericResult(events);858859 860 861 expect(result.success).to.be.true;862 });863}864865export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {866 await usingApi(async (api) => {867 868 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);869 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;870 const result = getGenericResult(events);871872 873 874 expect(result.success).to.be.false;875 });876}877878export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)879 : Promise<ICollectionInterface | null> => {880 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;881};882883export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {884 885 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();886};887888export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {889 return await usingApi(async (api) => {890 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;891 });892}