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;2324type GenericResult = {25 success: boolean,26};2728interface CreateCollectionResult {29 success: boolean;30 collectionId: number;31}3233interface CreateItemResult {34 success: boolean;35 collectionId: number;36 itemId: number;37}3839interface IReFungibleOwner {40 Fraction: BN;41 Owner: number[];42}4344interface ITokenDataType {45 Owner: number[];46 ConstData: number[];47 VariableData: number[];48}4950interface IFungibleTokenDataType {51 Value: BN;52}5354export interface IReFungibleTokenDataType {55 Owner: IReFungibleOwner[];56 ConstData: number[];57 VariableData: number[];58}5960export function getGenericResult(events: EventRecord[]): GenericResult {61 const result: GenericResult = {62 success: false,63 };64 events.forEach(({ phase, event: { data, method, section } }) => {65 66 if (method === 'ExtrinsicSuccess') {67 result.success = true;68 }69 });70 return result;71}7273export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {74 let success = false;75 let collectionId: number = 0;76 events.forEach(({ phase, event: { data, method, section } }) => {77 78 if (method == 'ExtrinsicSuccess') {79 success = true;80 } else if ((section == 'nft') && (method == 'Created')) {81 collectionId = parseInt(data[0].toString());82 }83 });84 const result: CreateCollectionResult = {85 success,86 collectionId,87 };88 return result;89}9091export function getCreateItemResult(events: EventRecord[]): CreateItemResult {92 let success = false;93 let collectionId: number = 0;94 let itemId: number = 0;95 events.forEach(({ phase, event: { data, method, section } }) => {96 97 if (method == 'ExtrinsicSuccess') {98 success = true;99 } else if ((section == 'nft') && (method == 'ItemCreated')) {100 collectionId = parseInt(data[0].toString());101 itemId = parseInt(data[1].toString());102 }103 });104 const result: CreateItemResult = {105 success,106 collectionId,107 itemId,108 };109 return result;110}111112interface Invalid {113 type: 'Invalid';114}115116interface Nft {117 type: 'NFT';118}119120interface Fungible {121 type: 'Fungible';122 decimalPoints: number;123}124125interface ReFungible {126 type: 'ReFungible';127 decimalPoints: number;128}129130interface Nft {131 type: 'NFT'132}133134interface Fungible {135 type: 'Fungible',136 decimalPoints: number137}138139interface ReFungible {140 type: 'ReFungible',141 decimalPoints: number142}143144type CollectionMode = Nft | Fungible | ReFungible | Invalid;145146export type CreateCollectionParams = {147 mode: CollectionMode,148 name: string,149 description: string,150 tokenPrefix: string,151};152153const defaultCreateCollectionParams: CreateCollectionParams = {154 description: 'description',155 mode: { type: 'NFT' },156 name: 'name',157 tokenPrefix: 'prefix',158}159160export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {161 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};162163 let collectionId: number = 0;164 await usingApi(async (api) => {165 166 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);167168 169 const alicePrivateKey = privateKey('//Alice');170171 let modeprm = {};172 if (mode.type === 'NFT') {173 modeprm = {nft: null};174 } else if (mode.type === 'Fungible') {175 modeprm = {fungible: mode.decimalPoints};176 } else if (mode.type === 'ReFungible') {177 modeprm = {refungible: mode.decimalPoints};178 } else if (mode.type === 'Invalid') {179 modeprm = {invalid: null};180 }181182 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);183 const events = await submitTransactionAsync(alicePrivateKey, tx);184 const result = getCreateCollectionResult(events);185186 187 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10);188189 190 const collection: any = (await api.query.nft.collection(result.collectionId)).toJSON();191192 193 194 expect(result.success).to.be.true;195 expect(result.collectionId).to.be.equal(BcollectionCount);196 197 expect(collection).to.be.not.null;198 expect(BcollectionCount).to.be.equal(AcollectionCount + 1, 'Error: NFT collection NOT created.');199 expect(collection.Owner).to.be.equal(alicesPublicKey);200 expect(utf16ToStr(collection.Name)).to.be.equal(name);201 expect(utf16ToStr(collection.Description)).to.be.equal(description);202 expect(hexToStr(collection.TokenPrefix)).to.be.equal(tokenPrefix);203204 collectionId = result.collectionId;205 });206207 return collectionId;208}209210export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {211 const {name, description, mode, tokenPrefix } = {...defaultCreateCollectionParams, ...params};212213 let modeprm = {};214 if (mode.type === 'NFT') {215 modeprm = {nft: null};216 } else if (mode.type === 'Fungible') {217 modeprm = {fungible: mode.decimalPoints};218 } else if (mode.type === 'ReFungible') {219 modeprm = {refungible: mode.decimalPoints};220 } else if (mode.type === 'Invalid') {221 modeprm = {invalid: null};222 }223224 await usingApi(async (api) => {225 226 const AcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());227228 229 const alicePrivateKey = privateKey('//Alice');230 const tx = api.tx.nft.createCollection(strToUTF16(name), strToUTF16(description), strToUTF16(tokenPrefix), modeprm);231 const events = await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;232 const result = getCreateCollectionResult(events);233234 235 const BcollectionCount = parseInt((await api.query.nft.createdCollectionCount()).toString());236237 238 239 expect(result.success).to.be.false;240 expect(BcollectionCount).to.be.equal(AcollectionCount, 'Error: Collection with incorrect data created.');241 });242}243244export async function findUnusedAddress(api: ApiPromise): Promise<IKeyringPair> {245 let bal = new BigNumber(0);246 let unused;247 do {248 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000));249 const keyring = new Keyring({ type: 'sr25519' });250 unused = keyring.addFromUri(`//${randomSeed}`);251 bal = new BigNumber((await api.query.system.account(unused.address)).data.free.toString());252 } while (bal.toFixed() != '0');253 return unused;254}255256export async function findNotExistingCollection(api: ApiPromise): Promise<number> {257 const totalNumber = parseInt((await api.query.nft.createdCollectionCount()).toString(), 10) as unknown as number;258 const newCollection: number = totalNumber + 1;259 return newCollection;260}261262function getDestroyResult(events: EventRecord[]): boolean {263 let success: boolean = false;264 events.forEach(({ phase, event: { data, method, section } }) => {265 266 if (method == 'ExtrinsicSuccess') {267 success = true;268 }269 });270 return success;271}272273export async function destroyCollectionExpectFailure(collectionId: number, senderSeed: string = '//Alice') {274 await usingApi(async (api) => {275 276 const alicePrivateKey = privateKey(senderSeed);277 const tx = api.tx.nft.destroyCollection(collectionId);278 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;279 });280}281282export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {283 await usingApi(async (api) => {284 285 const alicePrivateKey = privateKey(senderSeed);286 const tx = api.tx.nft.destroyCollection(collectionId);287 const events = await submitTransactionAsync(alicePrivateKey, tx);288 const result = getDestroyResult(events);289290 291 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();292293 294 expect(result).to.be.true;295 expect(collection).to.be.not.null;296 expect(collection.Owner).to.be.equal(nullPublicKey);297 });298}299300export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string) {301 await usingApi(async (api) => {302303 304 const alicePrivateKey = privateKey('//Alice');305 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);306 const events = await submitTransactionAsync(alicePrivateKey, tx);307 const result = getGenericResult(events);308309 310 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();311312 313 expect(result.success).to.be.true;314 expect(collection.Sponsor.toString()).to.be.equal(sponsor.toString());315 expect(collection.SponsorConfirmed).to.be.false;316 });317}318319export async function removeCollectionSponsorExpectSuccess(collectionId: number) {320 await usingApi(async (api) => {321322 323 const alicePrivateKey = privateKey('//Alice');324 const tx = api.tx.nft.removeCollectionSponsor(collectionId);325 const events = await submitTransactionAsync(alicePrivateKey, tx);326 const result = getGenericResult(events);327328 329 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();330331 332 expect(result.success).to.be.true;333 expect(collection.Sponsor).to.be.equal(nullPublicKey);334 expect(collection.SponsorConfirmed).to.be.false;335 });336}337338export async function removeCollectionSponsorExpectFailure(collectionId: number) {339 await usingApi(async (api) => {340341 342 const alicePrivateKey = privateKey('//Alice');343 const tx = api.tx.nft.removeCollectionSponsor(collectionId);344 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;345 });346}347348export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed: string = '//Alice') {349 await usingApi(async (api) => {350351 352 const alicePrivateKey = privateKey(senderSeed);353 const tx = api.tx.nft.setCollectionSponsor(collectionId, sponsor);354 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;355 });356}357358export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed: string = '//Alice') {359 await usingApi(async (api) => {360361 362 const sender = privateKey(senderSeed);363 const tx = api.tx.nft.confirmSponsorship(collectionId);364 const events = await submitTransactionAsync(sender, tx);365 const result = getGenericResult(events);366367 368 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();369370 371 expect(result.success).to.be.true;372 expect(collection.Sponsor).to.be.equal(sender.address);373 expect(collection.SponsorConfirmed).to.be.true;374 });375}376377export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed: string = '//Alice') {378 await usingApi(async (api) => {379380 381 const sender = privateKey(senderSeed);382 const tx = api.tx.nft.confirmSponsorship(collectionId);383 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;384 });385}386387export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {388 await usingApi(async (api) => {389 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);390 const events = await submitTransactionAsync(sender, tx);391 const result = getGenericResult(events);392393 expect(result.success).to.be.true;394 });395}396397export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {398 await usingApi(async (api) => {399 const tx = api.tx.nft.enableContractSponsoring(contractAddress, enable);400 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;401 const result = getGenericResult(events);402403 expect(result.success).to.be.false;404 });405}406407export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {408 await usingApi(async (api) => {409 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);410 const events = await submitTransactionAsync(sender, tx);411 const result = getGenericResult(events);412413 expect(result.success).to.be.true;414 });415}416417export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {418 await usingApi(async (api) => {419 const tx = api.tx.nft.setContractSponsoringRateLimit(contractAddress, rateLimit);420 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;421 const result = getGenericResult(events);422423 expect(result.success).to.be.false;424 });425}426427export async function toggleContractWhitelistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enabled: boolean) {428 await usingApi(async (api) => {429 const tx = api.tx.nft.toggleContractWhiteList(contractAddress, true);430 const events = await submitTransactionAsync(sender, tx);431 const result = getGenericResult(events);432433 expect(result.success).to.be.true;434 });435}436437export async function isWhitelistedInContract(contractAddress: AccountId | string, user: string) {438 let whitelisted: boolean = false;439 await usingApi(async (api) => {440 whitelisted = (await api.query.nft.contractWhiteList(contractAddress, user)).toJSON() as boolean;441 });442 return whitelisted;443}444445export async function addToContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {446 await usingApi(async (api) => {447 const tx = api.tx.nft.addToContractWhiteList(contractAddress, user);448 const events = await submitTransactionAsync(sender, tx);449 const result = getGenericResult(events);450451 expect(result.success).to.be.true;452 });453}454455export async function removeFromContractWhiteListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {456 await usingApi(async (api) => {457 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);458 const events = await submitTransactionAsync(sender, tx);459 const result = getGenericResult(events);460461 expect(result.success).to.be.true;462 });463}464465export async function removeFromContractWhiteListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: string) {466 await usingApi(async (api) => {467 const tx = api.tx.nft.removeFromContractWhiteList(contractAddress, user);468 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;469 const result = getGenericResult(events);470471 expect(result.success).to.be.false;472 });473}474475export async function setVariableMetaDataExpectSuccess(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {476 await usingApi(async (api) => {477 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));478 const events = await submitTransactionAsync(sender, tx);479 const result = getGenericResult(events);480481 expect(result.success).to.be.true;482 });483}484485export async function setVariableMetaDataExpectFailure(sender: IKeyringPair, collectionId: number, itemId: number, data: number[]) {486 await usingApi(async (api) => {487 const tx = api.tx.nft.setVariableMetaData(collectionId, itemId, '0x' + Buffer.from(data).toString('hex'));488 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;489 });490}491492export interface CreateFungibleData extends Struct {493 readonly value: u128;494}495496export interface CreateReFungibleData extends Struct {}497export interface CreateNftData extends Struct {}498499export interface CreateItemData extends Enum {500 NFT: CreateNftData;501 Fungible: CreateFungibleData;502 ReFungible: CreateReFungibleData;503}504505export async function burnItemExpectSuccess(owner: IKeyringPair, collectionId: number, tokenId: number, value = 0) {506 await usingApi(async (api) => {507 const tx = api.tx.nft.burnItem(collectionId, tokenId, value);508 const events = await submitTransactionAsync(owner, tx);509 const result = getGenericResult(events);510 511 const item: any = (await api.query.nft.nftItemList(collectionId, tokenId)).toJSON();512 513 514 expect(result.success).to.be.true;515 516 expect(item).to.be.not.null;517 expect(item.Owner).to.be.equal(nullPublicKey);518 });519}520521export async function522approveExpectSuccess(collectionId: number,523 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {524 await usingApi(async (api: ApiPromise) => {525 const allowanceBefore =526 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;527 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);528 const events = await submitTransactionAsync(owner, approveNftTx);529 const result = getCreateItemResult(events);530 531 expect(result.success).to.be.true;532 const allowanceAfter =533 await api.query.nft.allowances(collectionId, [tokenId, owner.address, approved.address]) as unknown as BN;534 expect(allowanceAfter.toNumber() - allowanceBefore.toNumber()).to.be.equal(amount);535 });536}537538export async function539transferFromExpectSuccess(collectionId: number,540 tokenId: number,541 accountApproved: IKeyringPair,542 accountFrom: IKeyringPair,543 accountTo: IKeyringPair,544 value: number = 1,545 type: string = 'NFT') {546 await usingApi(async (api: ApiPromise) => {547 let balanceBefore = new BN(0);548 if (type === 'Fungible') {549 balanceBefore = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;550 }551 const transferFromTx = await api.tx.nft.transferFrom(552 accountFrom.address, accountTo.address, collectionId, tokenId, value);553 const events = await submitTransactionAsync(accountApproved, transferFromTx);554 const result = getCreateItemResult(events);555 556 expect(result.success).to.be.true;557 if (type === 'NFT') {558 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;559 expect(nftItemData.Owner.toString()).to.be.equal(accountTo.address);560 }561 if (type === 'Fungible') {562 const balanceAfter = await api.query.nft.balance(collectionId, accountTo.address) as unknown as BN;563 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);564 }565 if (type === 'ReFungible') {566 const nftItemData =567 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;568 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(accountTo.address);569 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);570 }571 });572}573574export async function575transferFromExpectFail(collectionId: number,576 tokenId: number,577 accountApproved: IKeyringPair,578 accountFrom: IKeyringPair,579 accountTo: IKeyringPair,580 value: number = 1) {581 await usingApi(async (api: ApiPromise) => {582 const transferFromTx = await api.tx.nft.transferFrom(583 accountFrom.address, accountTo.address, collectionId, tokenId, value);584 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;585 const result = getCreateCollectionResult(events);586 587 expect(result.success).to.be.false;588 });589}590591export async function592transferExpectSuccess(collectionId: number,593 tokenId: number,594 sender: IKeyringPair,595 recipient: IKeyringPair,596 value: number = 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, recipient.address) as unknown as BN;602 }603 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);604 const events = await submitTransactionAsync(sender, transferTx);605 const result = getCreateItemResult(events);606 607 expect(result.success).to.be.true;608 if (type === 'NFT') {609 const nftItemData = await api.query.nft.nftItemList(collectionId, tokenId) as unknown as ITokenDataType;610 expect(nftItemData.Owner.toString()).to.be.equal(recipient.address);611 }612 if (type === 'Fungible') {613 const balanceAfter = await api.query.nft.balance(collectionId, recipient.address) as unknown as BN;614 expect(balanceAfter.sub(balanceBefore).toNumber()).to.be.equal(value);615 }616 if (type === 'ReFungible') {617 const nftItemData =618 await api.query.nft.reFungibleItemList(collectionId, tokenId) as unknown as IReFungibleTokenDataType;619 expect(nftItemData.Owner[0].Owner.toString()).to.be.equal(recipient.address);620 expect(nftItemData.Owner[0].Fraction.toNumber()).to.be.equal(value);621 }622 });623}624625export async function626transferExpectFail(collectionId: number,627 tokenId: number,628 sender: IKeyringPair,629 recipient: IKeyringPair,630 value: number = 1,631 type: string = 'NFT') {632 await usingApi(async (api: ApiPromise) => {633 const transferTx = await api.tx.nft.transfer(recipient.address, collectionId, tokenId, value);634 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;635 if (events && Array.isArray(events)) {636 const result = getCreateCollectionResult(events);637 638 expect(result.success).to.be.false;639 }640 });641}642643export async function644approveExpectFail(collectionId: number,645 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number = 1) {646 await usingApi(async (api: ApiPromise) => {647 const approveNftTx = await api.tx.nft.approve(approved.address, collectionId, tokenId, amount);648 const events = await expect(submitTransactionExpectFailAsync(owner, approveNftTx)).to.be.rejected;649 const result = getCreateCollectionResult(events);650 651 expect(result.success).to.be.false;652 });653}654655export async function createItemExpectSuccess(656 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = '') {657 let newItemId: number = 0;658 await usingApi(async (api) => {659 const AItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);660 const Aitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();661 const AItemBalance = new BigNumber(Aitem.Value);662663 if (owner === '') {664 owner = sender.address;665 }666667 let tx;668 if (createMode === 'Fungible') {669 const createData = {fungible: {value: 10}};670 tx = api.tx.nft.createItem(collectionId, owner, createData);671 } else {672 tx = api.tx.nft.createItem(collectionId, owner, createMode);673 }674 const events = await submitTransactionAsync(sender, tx);675 const result = getCreateItemResult(events);676677 const BItemCount = parseInt((await api.query.nft.itemListIndex(collectionId)).toString(), 10);678 const Bitem: any = (await api.query.nft.fungibleItemList(collectionId, owner)).toJSON();679 const BItemBalance = new BigNumber(Bitem.Value);680681 682 683 expect(result.success).to.be.true;684 if (createMode === 'Fungible') {685 expect(BItemBalance.minus(AItemBalance).toNumber()).to.be.equal(10);686 } else {687 expect(BItemCount).to.be.equal(AItemCount + 1);688 }689 expect(collectionId).to.be.equal(result.collectionId);690 expect(BItemCount).to.be.equal(result.itemId);691 newItemId = result.itemId;692 });693 return newItemId;694}695696export async function createItemExpectFailure(697 sender: IKeyringPair, collectionId: number, createMode: string, owner: string = sender.address) {698 await usingApi(async (api) => {699 const tx = api.tx.nft.createItem(collectionId, owner, createMode);700 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;701 const result = getCreateItemResult(events);702703 expect(result.success).to.be.false;704 });705}706707export async function setPublicAccessModeExpectSuccess(708 sender: IKeyringPair, collectionId: number,709 accessMode: 'Normal' | 'WhiteList',710) {711 await usingApi(async (api) => {712713 714 const tx = api.tx.nft.setPublicAccessMode(collectionId, accessMode);715 const events = await submitTransactionAsync(sender, tx);716 const result = getGenericResult(events);717718 719 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();720721 722 723 expect(result.success).to.be.true;724 expect(collection.Access).to.be.equal(accessMode);725 });726}727728export async function enableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {729 await setPublicAccessModeExpectSuccess(sender, collectionId, 'WhiteList');730}731732export async function disableWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number) {733 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');734}735736export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {737 await usingApi(async (api) => {738739 740 const tx = api.tx.nft.setMintPermission(collectionId, enabled);741 const events = await submitTransactionAsync(sender, tx);742 const result = getGenericResult(events);743744 745 const collection: any = (await api.query.nft.collection(collectionId)).toJSON();746747 748 749 expect(result.success).to.be.true;750 expect(collection.MintMode).to.be.equal(enabled);751 });752}753754export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {755 await setMintPermissionExpectSuccess(sender, collectionId, true);756}757758export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {759 await usingApi(async (api) => {760 761 const tx = api.tx.nft.setMintPermission(collectionId, enabled);762 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;763 const result = getCreateCollectionResult(events);764 765 expect(result.success).to.be.false;766 });767}768769export async function isWhitelisted(collectionId: number, address: string) {770 let whitelisted: boolean = false;771 await usingApi(async (api) => {772 whitelisted = (await api.query.nft.whiteList(collectionId, address)).toJSON() as unknown as boolean;773 });774 return whitelisted;775}776777export async function addToWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {778 await usingApi(async (api) => {779780 const whiteListedBefore = (await api.query.nft.whiteList(collectionId, address)).toJSON();781782 783 const tx = api.tx.nft.addToWhiteList(collectionId, address);784 const events = await submitTransactionAsync(sender, tx);785 const result = getGenericResult(events);786787 const whiteListedAfter = (await api.query.nft.whiteList(collectionId, address)).toJSON();788789 790 791 expect(result.success).to.be.true;792 793 expect(whiteListedBefore).to.be.false;794 795 expect(whiteListedAfter).to.be.true;796 });797}798799export async function removeFromWhiteListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string) {800 await usingApi(async (api) => {801 802 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);803 const events = await submitTransactionAsync(sender, tx);804 const result = getGenericResult(events);805806 807 808 expect(result.success).to.be.true;809 });810}811812export async function removeFromWhiteListExpectFailure(sender: IKeyringPair, collectionId: number, address: string) {813 await usingApi(async (api) => {814 815 const tx = api.tx.nft.removeFromWhiteList(collectionId, address);816 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;817 const result = getGenericResult(events);818819 820 821 expect(result.success).to.be.false;822 });823}824825export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)826 : Promise<ICollectionInterface | null> => {827 return await api.query.nft.collection(collectionId) as unknown as ICollectionInterface;828};829830export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {831 832 return (await api.query.nft.createdCollectionCount() as unknown as BN).toNumber();833};