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 TransferResult {42 success: boolean;43 collectionId: number;44 itemId: number;45 sender: string;46 recipient: string;47 value: bigint;48}4950interface IReFungibleOwner {51 Fraction: BN;52 Owner: number[];53}5455interface ITokenDataType {56 Owner: number[];57 ConstData: number[];58 VariableData: number[];59}6061interface IFungibleTokenDataType {62 Value: BN;63}6465export interface IReFungibleTokenDataType {66 Owner: IReFungibleOwner[];67 ConstData: number[];68 VariableData: number[];69}7071export function getGenericResult(events: EventRecord[]): GenericResult {72 const result: GenericResult = {73 success: false,74 };75 events.forEach(({ phase, event: { data, method, section } }) => {76 77 if (method === 'ExtrinsicSuccess') {78 result.success = true;79 }80 });81 return result;82}8384export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {85 let success = false;86 let collectionId: number = 0;87 events.forEach(({ phase, event: { data, method, section } }) => {88 89 if (method == 'ExtrinsicSuccess') {90 success = true;91 } else if ((section == 'nft') && (method == 'Created')) {92 collectionId = parseInt(data[0].toString());93 }94 });95 const result: CreateCollectionResult = {96 success,97 collectionId,98 };99 return result;100}101102export function getCreateItemResult(events: EventRecord[]): CreateItemResult {103 let success = false;104 let collectionId: number = 0;105 let itemId: number = 0;106 events.forEach(({ phase, event: { data, method, section } }) => {107 108 if (method == 'ExtrinsicSuccess') {109 success = true;110 } else if ((section == 'nft') && (method == 'ItemCreated')) {111 collectionId = parseInt(data[0].toString());112 itemId = parseInt(data[1].toString());113 }114 });115 const result: CreateItemResult = {116 success,117 collectionId,118 itemId,119 };120 return result;121}122123export function getTransferResult(events: EventRecord[]): TransferResult {124 const result: TransferResult = {125 success: false,126 collectionId: 0,127 itemId: 0,128 sender: '',129 recipient: '',130 value: 0n,131 };132133 events.forEach(({event: {data, method, section}}) => {134 if (method === 'ExtrinsicSuccess') {135 result.success = true;136 } else if (section === 'nft' && method === 'Transfer') {137 result.collectionId = +data[0].toString();138 result.itemId = +data[1].toString();139 result.sender = data[2].toString();140 result.recipient = data[3].toString();141 result.value = BigInt(data[4].toString());142 }143 });144145 return result;146}147148interface Invalid {149 type: 'Invalid';150}151152interface Nft {153 type: 'NFT';154}155156interface Fungible {157 type: 'Fungible';158 decimalPoints: number;159}160161interface ReFungible {162 type: 'ReFungible';163}164165type CollectionMode = Nft | Fungible | ReFungible | Invalid;166167export type CreateCollectionParams = {168 mode: CollectionMode,169 name: string,170 description: string,171 tokenPrefix: string,172};173174const defaultCreateCollectionParams: CreateCollectionParams = {175 description: 'description',176 mode: { type: 'NFT' },177 name: 'name',178 tokenPrefix: 'prefix',179}180181export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {182 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};183184 let collectionId: number = 0;185 await usingApi(async (api) => {186 187 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);188189 190 const alicePrivateKey = privateKey('//Alice');191192 let modeprm = {};193 if (mode.type === 'NFT') {194 modeprm = {nft: null};195 } else if (mode.type === 'Fungible') {196 modeprm = {fungible: mode.decimalPoints};197 } else if (mode.type === 'ReFungible') {198 modeprm = {refungible: null};199 } else if (mode.type === 'Invalid') {200 modeprm = {invalid: null};201 }202203 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);204 const events = await submitTransactionAsync(alicePrivateKey, tx);205 const result = getCreateCollectionResult(events);206207 208 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);209210 211 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();212213 214 215 expect(result.success).to.be.true;216 expect(result.collectionId).to.be.equal(BcollectionCount);217 218 expect(collection).to.be.not.null;219 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');220 expect(collection.Owner).to.be.equal(alicesPublicKey);221 expect(utf16ToStr(collection.Name)).to.be.equal(name);222 expect(utf16ToStr(collection.Description)).to.be.equal(description);223 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);224225 collectionId = result.collectionId;226 });227228 return collectionId;229}230231export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {232 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};233234 let modeprm = {};235 if (mode.type === 'NFT') {236 modeprm = {nft: null};237 } else if (mode.type === 'Fungible') {238 modeprm = {fungible: mode.decimalPoints};239 } else if (mode.type === 'ReFungible') {240 modeprm = {refungible: null};241 } else if (mode.type === 'Invalid') {242 modeprm = {invalid: null};243 }244245 await usingApi(async (api) => {246 247 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());248249 250 const alicePrivateKey = privateKey('//Alice');251 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);252 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;253 const result = getCreateCollectionResult(events);254255 256 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());257258 259 260 expect(result.success).to.be.false;261 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');262 });263}264265export async function findUnusedAddress(api: ApiPromise, seedAddition = ''): Promise<IKeyringPair> {266 let bal = new BigNumber(0);267 let unused;268 do {269 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;270 const keyring = new Keyring({ type: 'sr25519' });271 unused = keyring.addFromUri(`//${randomSeed}`);272 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());273 } while (bal.toFixed() != '0');274 return unused;275}276277export async function getAllowance(collectionId: number, tokenId: number, owner: string, approved: string) {278 return await usingApi(async (api) => {279 const bn = await api.query.nft.allowances(collectionId, [tokenId, owner, approved]) as unknown as BN;280 return BigInt(bn.toString());281 });282}283284export function findUnusedAddresses(api: ApiPromise, amount: number): Promise<IKeyringPair[]> {285 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, '_' + Date.now())));286}287288export async function findNotExistingCollection(api: ApiPromise): Promise<number> {289 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;290 const newCollection: number = totalNumber + 1;291 return newCollection;292}293294function getDestroyResult(events: EventRecord[]): boolean {295 let success: boolean = false;296 events.forEach(({ phase, event: { data, method, section } }) => {297 298 if (method == 'ExtrinsicSuccess') {299 success = true;300 }301 });302 return success;303}304305export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {306 await usingApi(async (api) => {307 308 const alicePrivateKey = privateKey(senderSeed);309 const tx = api.tx.nft.destroyCollection(collectionId);310 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;311 });312}313314export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {315 await usingApi(async (api) => {316 317 const alicePrivateKey = privateKey(senderSeed);318 const tx = api.tx.nft.destroyCollection(collectionId);319 const events = await submitTransactionAsync(alicePrivateKey, tx);320 const result = getDestroyResult(events);321322 323 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();324325 326 expect(result).to.be.true;327 expect(collection).to.be.not.null;328 expect(collection.Owner).to.be.equal(nullPublicKey);329 });330}331332export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {333 await usingApi(async (api) => {334335 336 const alicePrivateKey = privateKey('//Alice');337 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);338 const events = await submitTransactionAsync(alicePrivateKey, tx);339 const result = getGenericResult(events);340341 342 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();343344 345 expect(result.success).to.be.true;346 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());347 expect(collection.SponsorConfirmed).to.be.false;348 });349}350351export async function removeCollectionSponsorExpectSuccess(collectionId: number) {352 await usingApi(async (api) => {353354 355 const alicePrivateKey = privateKey('//Alice');356 const tx = api.tx.nft.removeCollectionSponsor(collectionId);357 const events = await submitTransactionAsync(alicePrivateKey, tx);358 const result = getGenericResult(events);359360 361 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();362363 364 expect(result.success).to.be.true;365 expect(collection.Sponsor).to.be.equal(nullPublicKey);366 expect(collection.SponsorConfirmed).to.be.false;367 });368}369370export async function removeCollectionSponsorExpectFailure(collectionId: number) {371 await usingApi(async (api) => {372373 374 const alicePrivateKey = privateKey('//Alice');375 const tx = api.tx.nft.removeCollectionSponsor(collectionId);376 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;377 });378}379380export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {381 await usingApi(async (api) => {382383 384 const alicePrivateKey = privateKey(senderSeed);385 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);386 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;387 });388}389390export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {391 await usingApi(async (api) => {392393 394 const sender = privateKey(senderSeed);395 const tx = api.tx.nft.confirmSponsorship(collectionId);396 const events = await submitTransactionAsync(sender, tx);397 const result = getGenericResult(events);398399 400 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();401402 403 expect(result.success).to.be.true;404 expect(collection.Sponsor).to.be.equal(sender.address);405 expect(collection.SponsorConfirmed).to.be.true;406 });407}408409410export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {411 await usingApi(async (api) => {412413 414 const sender = privateKey(senderSeed);415 const tx = api.tx.nft.confirmSponsorship(collectionId);416 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;417 });418}419420export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {421 await usingApi(async (api) => {422 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);423 const events = await submitTransactionAsync(sender, tx);424 const result = getGenericResult(events);425426 expect(result.success).to.be.true;427 });428}429430export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {431 await usingApi(async (api) => {432 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);433 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;434 const result = getGenericResult(events);435436 expect(result.success).to.be.false;437 });438}439440export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {441 await usingApi(async (api) => {442 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);443 const events = await submitTransactionAsync(sender, tx);444 const result = getGenericResult(events);445446 expect(result.success).to.be.true;447 });448}449450export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {451 await usingApi(async (api) => {452 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);453 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;454 const result = getGenericResult(events);455456 expect(result.success).to.be.false;457 });458}459460export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {461 await usingApi(async (api) => {462 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);463 const events = await submitTransactionAsync(sender, tx);464 const result = getGenericResult(events);465466 expect(result.success).to.be.true;467 });468}469470export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {471 let whitelisted: boolean = false;472 await usingApi(async (api) => {473 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;474 });475 return whitelisted;476}477478export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {479 await usingApi(async (api) => {480 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);481 const events = await submitTransactionAsync(sender, tx);482 const result = getGenericResult(events);483484 expect(result.success).to.be.true;485 });486}487488export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {489 await usingApi(async (api) => {490 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);491 const events = await submitTransactionAsync(sender, tx);492 const result = getGenericResult(events);493494 expect(result.success).to.be.true;495 });496}497498export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {499 await usingApi(async (api) => {500 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);501 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;502 const result = getGenericResult(events);503504 expect(result.success).to.be.false;505 });506}507508export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {509 await usingApi(async (api) => {510 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));511 const events = await submitTransactionAsync(sender, tx);512 const result = getGenericResult(events);513514 expect(result.success).to.be.true;515 });516}517518export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {519 await usingApi(async (api) => {520 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));521 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;522 });523}524525export async function setOffchainSchemaExpectSuccess(sender: IKeyringPair, collectionId: number, data: number[]) {526 await usingApi(async (api) => {527 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));528 const events = await submitTransactionAsync(sender, tx);529 const result = getGenericResult(events);530531 expect(result.success).to.be.true;532 });533}534535export async function setOffchainSchemaExpectFailure(sender: IKeyringPair, collectionId: number, data: number[]) {536 await usingApi(async (api) => {537 const tx = api.tx.nft.setOffchainSchema(collectionId, '0x' + Buffer.from(data).toString('hex'));538 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;539 });540}541542export interface CreateFungibleData {543 readonly Value: bigint;544}545546export interface CreateReFungibleData { }547export interface CreateNftData { }548549export type CreateItemData = {550 NFT: CreateNftData;551} | {552 Fungible: CreateFungibleData;553} | {554 ReFungible: CreateReFungibleData;555};556557export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {558 await usingApi(async (api) => {559 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);560 const events = await submitTransactionAsync(owner, tx);561 const result = getGenericResult(events);562 563 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();564 565 566 expect(result.success).to.be.true;567 568 expect(item).to.be.not.null;569 expect(item.Owner).to.be.equal(nullPublicKey);570 });571}572573export async function574approveExpectSuccess(collectionId: number,575 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {576 await usingApi(async (api: ApiPromise) => {577 const allowanceBefore =578 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;579 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);580 const events = await submitTransactionAsync(owner, approveNftTx);581 const result = getCreateItemResult(events);582 583 expect(result.success).to.be.true;584 const allowanceAfter =585 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;586 expect(allowanceAfter.sub(allowanceBefore).toString()).to.be.equal(amount.toString());587 });588}589590export async function591transferFromExpectSuccess(collectionId: number,592 tokenId: number,593 accountApproved: IKeyringPair,594 accountFrom: IKeyringPair,595 accountTo: IKeyringPair,596 value: number | bigint = 1,597 type: string = 'NFT') {598 await usingApi(async (api: ApiPromise) => {599 let balanceBefore = new BN(0);600 if (type === 'Fungible') {601 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;602 }603 const transferFromTx = await api.tx.nft.transferFrom(604 accountFrom.address, accountTo.address, collectionId, tokenId, value);605 const events = await submitTransactionAsync(accountApproved, transferFromTx);606 const result = getCreateItemResult(events);607 608 expect(result.success).to.be.true;609 if (type === 'NFT') {610 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;611 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);612 }613 if (type === 'Fungible') {614 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;615 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());616 }617 if (type === 'ReFungible') {618 const nftItemData =619 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;620 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);621 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);622 }623 });624}625626export async function627transferFromExpectFail(collectionId: number,628 tokenId: number,629 accountApproved: IKeyringPair,630 accountFrom: IKeyringPair,631 accountTo: IKeyringPair,632 value: number | bigint = 1) {633 await usingApi(async (api: ApiPromise) => {634 const transferFromTx = await api.tx.nft.transferFrom(635 accountFrom.address, accountTo.address, collectionId, tokenId, value);636 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;637 const result = getCreateCollectionResult(events);638 639 expect(result.success).to.be.false;640 });641}642643export async function644transferExpectSuccess(collectionId: number,645 tokenId: number,646 sender: IKeyringPair,647 recipient: IKeyringPair,648 value: number | bigint = 1,649 type: string = 'NFT') {650 await usingApi(async (api: ApiPromise) => {651 let balanceBefore = new BN(0);652 if (type === 'Fungible') {653 balanceBefore = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;654 }655 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);656 const events = await submitTransactionAsync(sender, transferTx);657 const result = getCreateItemResult(events);658 659 expect(result.success).to.be.true;660 if (type === 'NFT') {661 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;662 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);663 }664 if (type === 'Fungible') {665 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;666 expect(balanceAfter.sub(balanceBefore).toString()).to.be.equal(value.toString());667 }668 if (type === 'ReFungible') {669 const nftItemData =670 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;671 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);672 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);673 }674 });675}676677export async function678transferExpectFail(collectionId: number,679 tokenId: number,680 sender: IKeyringPair,681 recipient: IKeyringPair,682 value: number | bigint = 1,683 type: string = 'NFT') {684 await usingApi(async (api: ApiPromise) => {685 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);686 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;687 if (events && Array.isArray(events)) {688 const result = getCreateCollectionResult(events);689 690 expect(result.success).to.be.false;691 }692 });693}694695export async function696approveExpectFail(collectionId: number,697 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1) {698 await usingApi(async (api: ApiPromise) => {699 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);700 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;701 const result = getCreateCollectionResult(events);702 703 expect(result.success).to.be.false;704 });705}706707export async function getFungibleBalance(708 collectionId: number,709 owner: string,710) {711 return await usingApi(async (api) => {712 const response = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON() as unknown as {Value: string};713 return BigInt(response.Value);714 });715}716717export async function createFungibleItemExpectSuccess(718 sender: IKeyringPair,719 collectionId: number,720 data: CreateFungibleData,721 owner: string = sender.address,722) {723 return await usingApi(async (api) => {724 const tx = api.tx.nft.createItem(collectionId, owner, { Fungible: data });725726 const events = await submitTransactionAsync(sender, tx);727 const result = getCreateItemResult(events);728729 expect(result.success).to.be.true;730 return result.itemId;731 });732}733734export async function createItemExpectSuccess(735 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {736 let newItemId: number = 0;737 await usingApi(async (api) => {738 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);739 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();740 const AItemBalance = new BigNumber(Aitem.Value);741742 if (owner === '') {743 owner = sender.address;744 }745746 let tx;747 if (createMode === 'Fungible') {748 const createData = {fungible: {value: 10}};749 tx = api.tx.nft.createItem(collectionId, owner, createData);750 } else if (createMode === 'ReFungible') {751 const createData = {refungible: {const_data: [], variable_data: [], pieces: 100}};752 tx = api.tx.nft.createItem(collectionId, owner, createData);753 } else {754 tx = api.tx.nft.createItem(collectionId, owner, createMode);755 }756 const events = await submitTransactionAsync(sender, tx);757 const result = getCreateItemResult(events);758759 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);760 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();761 const BItemBalance = new BigNumber(Bitem.Value);762763 764 765 expect(result.success).to.be.true;766 if (createMode === 'Fungible') {767 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);768 } else {769 expect(BItemCount).to.be.equal(AItemCount + 1);770 }771 expect(collectionId).to.be.equal(result.collectionId);772 expect(BItemCount).to.be.equal(result.itemId);773 newItemId = result.itemId;774 });775 return newItemId;776}777778export async function createItemExpectFailure(779 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {780 await usingApi(async (api) => {781 const tx = api.tx.nft.createItem(collectionId, owner, createMode);782 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;783 const result = getCreateItemResult(events);784785 expect(result.success).to.be.false;786 });787}788789export async function setPublicAccessModeExpectSuccess(790 sender: IKeyringPair, collectionId: number,791 accessMode: 'Normal' | 'WhiteList',792) {793 await usingApi(async (api) => {794795 796 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);797 const events = await submitTransactionAsync(sender, tx);798 const result = getGenericResult(events);799800 801 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();802803 804 805 expect(result.success).to.be.true;806 expect(collection.Access).to.be.equal(accessMode);807 });808}809810export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {811 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');812}813814export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {815 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');816}817818export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {819 await usingApi(async (api) => {820821 822 const tx = api.tx.nft.setMintPermission(collectionId, enabled);823 const events = await submitTransactionAsync(sender, tx);824 const result = getGenericResult(events);825826 827 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();828829 830 831 expect(result.success).to.be.true;832 expect(collection.MintMode).to.be.equal(enabled);833 });834}835836export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {837 await setMintPermissionExpectSuccess(sender, collectionId, true);838}839840export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {841 await usingApi(async (api) => {842 843 const tx = api.tx.nft.setMintPermission(collectionId, enabled);844 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;845 const result = getCreateCollectionResult(events);846 847 expect(result.success).to.be.false;848 });849}850851export async function isWhitelisted(collectionId: number, address: string) {852 let whitelisted: boolean = false;853 await usingApi(async (api) => {854 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;855 });856 return whitelisted;857}858859export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {860 await usingApi(async (api) => {861862 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();863864 865 const tx = api.tx.nft.addToWhiteList(collectionId, address);866 const events = await submitTransactionAsync(sender, tx);867 const result = getGenericResult(events);868869 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();870871 872 873 expect(result.success).to.be.true;874 875 expect(whiteListedBefore).to.be.false;876 877 expect(whiteListedAfter).to.be.true;878 });879}880881export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {882 await usingApi(async (api) => {883 884 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);885 const events = await submitTransactionAsync(sender, tx);886 const result = getGenericResult(events);887888 889 890 expect(result.success).to.be.true;891 });892}893894export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {895 await usingApi(async (api) => {896 897 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);898 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;899 const result = getGenericResult(events);900901 902 903 expect(result.success).to.be.false;904 });905}906907export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)908 : Promise<ICollectionInterface | null> => {909 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;910};911912export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {913 914 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();915};916917export async function queryCollectionExpectSuccess(collectionId: number): Promise<ICollectionInterface> {918 return await usingApi(async (api) => {919 return (await api.query.nft.collection(collectionId)) as unknown as ICollectionInterface;920 });921}