1234567891011121314151617import '../interfaces/augment-api-rpc';18import '../interfaces/augment-api-query';19import {ApiPromise} from '@polkadot/api';20import type {AccountId, EventRecord, Event} from '@polkadot/types/interfaces';21import type {GenericEventData} from '@polkadot/types';22import {AnyTuple, IEvent, IKeyringPair} from '@polkadot/types/types';23import {evmToAddress} from '@polkadot/util-crypto';24import BN from 'bn.js';25import chai from 'chai';26import chaiAsPromised from 'chai-as-promised';27import {default as usingApi, executeTransaction, submitTransactionAsync, submitTransactionExpectFailAsync} from '../substrate/substrate-api';28import {hexToStr, strToUTF16, utf16ToStr} from './util';29import {UpDataStructsRpcCollection, UpDataStructsCreateItemData, UpDataStructsProperty} from '@polkadot/types/lookup';30import {UpDataStructsTokenChild} from '../interfaces';3132chai.use(chaiAsPromised);33const expect = chai.expect;3435export type CrossAccountId = {36 Substrate: string,37} | {38 Ethereum: string,39};4041export function normalizeAccountId(input: string | AccountId | CrossAccountId | IKeyringPair): CrossAccountId {42 if (typeof input === 'string') {43 if (input.length >= 47) {44 return {Substrate: input};45 } else if (input.length === 42 && input.startsWith('0x')) {46 return {Ethereum: input.toLowerCase()};47 } else if (input.length === 40 && !input.startsWith('0x')) {48 return {Ethereum: '0x' + input.toLowerCase()};49 } else {50 throw new Error(`Unknown address format: "${input}"`);51 }52 }53 if ('address' in input) {54 return {Substrate: input.address};55 }56 if ('Ethereum' in input) {57 return {58 Ethereum: input.Ethereum.toLowerCase(),59 };60 } else if ('ethereum' in input) {61 return {62 Ethereum: (input as any).ethereum.toLowerCase(),63 };64 } else if ('Substrate' in input) {65 return input;66 } else if ('substrate' in input) {67 return {68 Substrate: (input as any).substrate,69 };70 }7172 73 return {Substrate: input.toString()};74}75export function toSubstrateAddress(input: string | CrossAccountId | IKeyringPair): string {76 input = normalizeAccountId(input);77 if ('Substrate' in input) {78 return input.Substrate;79 } else {80 return evmToAddress(input.Ethereum);81 }82}8384export const U128_MAX = (1n << 128n) - 1n;8586const MICROUNIQUE = 1_000_000_000_000n;87const MILLIUNIQUE = 1_000n * MICROUNIQUE;88const CENTIUNIQUE = 10n * MILLIUNIQUE;89export const UNIQUE = 100n * CENTIUNIQUE;9091interface GenericResult<T> {92 success: boolean;93 data: T | null;94}9596interface CreateCollectionResult {97 success: boolean;98 collectionId: number;99}100101interface CreateItemResult {102 success: boolean;103 collectionId: number;104 itemId: number;105 recipient?: CrossAccountId;106}107108interface TransferResult {109 collectionId: number;110 itemId: number;111 sender?: CrossAccountId;112 recipient?: CrossAccountId;113 value: bigint;114}115116interface IReFungibleOwner {117 fraction: BN;118 owner: number[];119}120121interface IGetMessage {122 checkMsgUnqMethod: string;123 checkMsgTrsMethod: string;124 checkMsgSysMethod: string;125}126127export interface IFungibleTokenDataType {128 value: number;129}130131export interface IChainLimits {132 collectionNumbersLimit: number;133 accountTokenOwnershipLimit: number;134 collectionsAdminsLimit: number;135 customDataLimit: number;136 nftSponsorTransferTimeout: number;137 fungibleSponsorTransferTimeout: number;138 refungibleSponsorTransferTimeout: number;139 140 141}142143export interface IReFungibleTokenDataType {144 owner: IReFungibleOwner[];145}146147export function uniqueEventMessage(events: EventRecord[]): IGetMessage {148 let checkMsgUnqMethod = '';149 let checkMsgTrsMethod = '';150 let checkMsgSysMethod = '';151 events.forEach(({event: {method, section}}) => {152 if (section === 'common') {153 checkMsgUnqMethod = method;154 } else if (section === 'treasury') {155 checkMsgTrsMethod = method;156 } else if (section === 'system') {157 checkMsgSysMethod = method;158 } else { return null; }159 });160 const result: IGetMessage = {161 checkMsgUnqMethod,162 checkMsgTrsMethod,163 checkMsgSysMethod,164 };165 return result;166}167168export function getEvent<T extends Event>(events: EventRecord[], check: (event: IEvent<AnyTuple>) => event is T): T | undefined {169 const event = events.find(r => check(r.event));170 if (!event) return;171 return event.event as T;172}173174export function getGenericResult<T>(events: EventRecord[]): GenericResult<T>;175export function getGenericResult<T>(176 events: EventRecord[],177 expectSection: string,178 expectMethod: string,179 extractAction: (data: GenericEventData) => T180): GenericResult<T>;181182export function getGenericResult<T>(183 events: EventRecord[],184 expectSection?: string,185 expectMethod?: string,186 extractAction?: (data: GenericEventData) => T,187): GenericResult<T> {188 let success = false;189 let successData = null;190191 events.forEach(({event: {data, method, section}}) => {192 193 if (method === 'ExtrinsicSuccess') {194 success = true;195 } else if ((expectSection == section) && (expectMethod == method)) {196 successData = extractAction!(data as any);197 }198 });199200 const result: GenericResult<T> = {201 success,202 data: successData,203 };204 return result;205}206207export function getCreateCollectionResult(events: EventRecord[]): CreateCollectionResult {208 const genericResult = getGenericResult(events, 'common', 'CollectionCreated', (data) => parseInt(data[0].toString(), 10));209 const result: CreateCollectionResult = {210 success: genericResult.success,211 collectionId: genericResult.data ?? 0,212 };213 return result;214}215216export function getCreateItemsResult(events: EventRecord[]): CreateItemResult[] {217 const results: CreateItemResult[] = [];218 219 const genericResult = getGenericResult<CreateItemResult[]>(events, 'common', 'ItemCreated', (data) => {220 const collectionId = parseInt(data[0].toString(), 10);221 const itemId = parseInt(data[1].toString(), 10);222 const recipient = normalizeAccountId(data[2].toJSON() as any);223224 const itemRes: CreateItemResult = {225 success: true,226 collectionId,227 itemId,228 recipient,229 };230231 results.push(itemRes);232 return results;233 });234235 if (!genericResult.success) return [];236 return results;237}238239export function getCreateItemResult(events: EventRecord[]): CreateItemResult {240 const genericResult = getGenericResult<[number, number, CrossAccountId?]>(events, 'common', 'ItemCreated', (data) => [241 parseInt(data[0].toString(), 10),242 parseInt(data[1].toString(), 10),243 normalizeAccountId(data[2].toJSON() as any),244 ]);245246 if (genericResult.data == null) genericResult.data = [0, 0];247248 const result: CreateItemResult = {249 success: genericResult.success,250 collectionId: genericResult.data[0],251 itemId: genericResult.data[1],252 recipient: genericResult.data![2],253 };254 255 return result;256}257258export function getTransferResult(api: ApiPromise, events: EventRecord[]): TransferResult {259 for (const {event} of events) {260 if (api.events.common.Transfer.is(event)) {261 const [collection, token, sender, recipient, value] = event.data;262 return {263 collectionId: collection.toNumber(),264 itemId: token.toNumber(),265 sender: normalizeAccountId(sender.toJSON() as any),266 recipient: normalizeAccountId(recipient.toJSON() as any),267 value: value.toBigInt(),268 };269 }270 }271 throw new Error('no transfer event');272}273274interface Nft {275 type: 'NFT';276}277278interface Fungible {279 type: 'Fungible';280 decimalPoints: number;281}282283interface ReFungible {284 type: 'ReFungible';285}286287type CollectionMode = Nft | Fungible | ReFungible;288289export type Property = {290 key: any,291 value: any,292};293294type Permission = {295 mutable: boolean;296 collectionAdmin: boolean;297 tokenOwner: boolean;298}299300type PropertyPermission = {301 key: any;302 permission: Permission;303}304305export type CreateCollectionParams = {306 mode: CollectionMode,307 name: string,308 description: string,309 tokenPrefix: string,310 properties?: Array<Property>,311 propPerm?: Array<PropertyPermission>312};313314const defaultCreateCollectionParams: CreateCollectionParams = {315 description: 'description',316 mode: {type: 'NFT'},317 name: 'name',318 tokenPrefix: 'prefix',319};320321export async function createCollectionExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {322 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};323324 let collectionId = 0;325 await usingApi(async (api, privateKeyWrapper) => {326 327 const collectionCountBefore = await getCreatedCollectionCount(api);328329 330 const alicePrivateKey = privateKeyWrapper('//Alice');331332 let modeprm = {};333 if (mode.type === 'NFT') {334 modeprm = {nft: null};335 } else if (mode.type === 'Fungible') {336 modeprm = {fungible: mode.decimalPoints};337 } else if (mode.type === 'ReFungible') {338 modeprm = {refungible: null};339 }340341 const tx = api.tx.unique.createCollectionEx({342 name: strToUTF16(name),343 description: strToUTF16(description),344 tokenPrefix: strToUTF16(tokenPrefix),345 mode: modeprm as any,346 });347 const events = await submitTransactionAsync(alicePrivateKey, tx);348 const result = getCreateCollectionResult(events);349350 351 const collectionCountAfter = await getCreatedCollectionCount(api);352353 354 const collection = await queryCollectionExpectSuccess(api, result.collectionId);355356 357 358 expect(result.success).to.be.true;359 expect(result.collectionId).to.be.equal(collectionCountAfter);360 361 expect(collection).to.be.not.null;362 expect(collectionCountAfter).to.be.equal(collectionCountBefore + 1, 'Error: NFT collection NOT created.');363 expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicePrivateKey));364 expect(utf16ToStr(collection.name.toJSON() as any)).to.be.equal(name);365 expect(utf16ToStr(collection.description.toJSON() as any)).to.be.equal(description);366 expect(hexToStr(collection.tokenPrefix.toJSON())).to.be.equal(tokenPrefix);367368 collectionId = result.collectionId;369 });370371 return collectionId;372}373374export async function createCollectionWithPropsExpectSuccess(params: Partial<CreateCollectionParams> = {}): Promise<number> {375 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};376377 let collectionId = 0;378 await usingApi(async (api, privateKeyWrapper) => {379 380 const collectionCountBefore = await getCreatedCollectionCount(api);381382 383 const alicePrivateKey = privateKeyWrapper('//Alice');384385 let modeprm = {};386 if (mode.type === 'NFT') {387 modeprm = {nft: null};388 } else if (mode.type === 'Fungible') {389 modeprm = {fungible: mode.decimalPoints};390 } else if (mode.type === 'ReFungible') {391 modeprm = {refungible: null};392 }393394 const tx = api.tx.unique.createCollectionEx({name: strToUTF16(name), description: strToUTF16(description), tokenPrefix: strToUTF16(tokenPrefix), mode: modeprm as any, properties: params.properties, tokenPropertyPermissions: params.propPerm});395 const events = await submitTransactionAsync(alicePrivateKey, tx);396 const result = getCreateCollectionResult(events);397398 399 const collectionCountAfter = await getCreatedCollectionCount(api);400401 402 const collection = await queryCollectionExpectSuccess(api, result.collectionId);403404 405 406 expect(result.success).to.be.true;407 expect(result.collectionId).to.be.equal(collectionCountAfter);408 409 expect(collection).to.be.not.null;410 expect(collectionCountAfter).to.be.equal(collectionCountBefore + 1, 'Error: NFT collection NOT created.');411 expect(collection.owner.toString()).to.be.equal(toSubstrateAddress(alicePrivateKey));412 expect(utf16ToStr(collection.name.toJSON() as any)).to.be.equal(name);413 expect(utf16ToStr(collection.description.toJSON() as any)).to.be.equal(description);414 expect(hexToStr(collection.tokenPrefix.toJSON())).to.be.equal(tokenPrefix);415416417 collectionId = result.collectionId;418 });419420 return collectionId;421}422423export async function createCollectionWithPropsExpectFailure(params: Partial<CreateCollectionParams> = {}) {424 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};425426 await usingApi(async (api, privateKeyWrapper) => {427 428 const collectionCountBefore = await getCreatedCollectionCount(api);429430 431 const alicePrivateKey = privateKeyWrapper('//Alice');432433 let modeprm = {};434 if (mode.type === 'NFT') {435 modeprm = {nft: null};436 } else if (mode.type === 'Fungible') {437 modeprm = {fungible: mode.decimalPoints};438 } else if (mode.type === 'ReFungible') {439 modeprm = {refungible: null};440 }441442 const tx = api.tx.unique.createCollectionEx({name: strToUTF16(name), description: strToUTF16(description), tokenPrefix: strToUTF16(tokenPrefix), mode: modeprm as any, properties: params.properties, tokenPropertyPermissions: params.propPerm});443 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;444445446 447 const collectionCountAfter = await getCreatedCollectionCount(api);448449 expect(collectionCountAfter).to.be.equal(collectionCountBefore, 'Error: Collection with incorrect data created.');450 });451}452453export async function createCollectionExpectFailure(params: Partial<CreateCollectionParams> = {}) {454 const {name, description, mode, tokenPrefix} = {...defaultCreateCollectionParams, ...params};455456 let modeprm = {};457 if (mode.type === 'NFT') {458 modeprm = {nft: null};459 } else if (mode.type === 'Fungible') {460 modeprm = {fungible: mode.decimalPoints};461 } else if (mode.type === 'ReFungible') {462 modeprm = {refungible: null};463 }464465 await usingApi(async (api, privateKeyWrapper) => {466 467 const collectionCountBefore = await getCreatedCollectionCount(api);468469 470 const alicePrivateKey = privateKeyWrapper('//Alice');471 const tx = api.tx.unique.createCollectionEx({name: strToUTF16(name), description: strToUTF16(description), tokenPrefix: strToUTF16(tokenPrefix), mode: modeprm as any});472 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;473474 475 const collectionCountAfter = await getCreatedCollectionCount(api);476477 478 expect(collectionCountAfter).to.be.equal(collectionCountBefore, 'Error: Collection with incorrect data created.');479 });480}481482export async function findUnusedAddress(api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair, seedAddition = ''): Promise<IKeyringPair> {483 let bal = 0n;484 let unused;485 do {486 const randomSeed = 'seed' + Math.floor(Math.random() * Math.floor(10000)) + seedAddition;487 unused = privateKeyWrapper(`//${randomSeed}`);488 bal = (await api.query.system.account(unused.address)).data.free.toBigInt();489 } while (bal !== 0n);490 return unused;491}492493export async function getAllowance(api: ApiPromise, collectionId: number, owner: CrossAccountId | string, approved: CrossAccountId | string, tokenId: number) {494 return (await api.rpc.unique.allowance(collectionId, normalizeAccountId(owner), normalizeAccountId(approved), tokenId)).toBigInt();495}496497export function findUnusedAddresses(api: ApiPromise, privateKeyWrapper: (account: string) => IKeyringPair, amount: number): Promise<IKeyringPair[]> {498 return Promise.all(new Array(amount).fill(null).map(() => findUnusedAddress(api, privateKeyWrapper, '_' + Date.now())));499}500501export async function findNotExistingCollection(api: ApiPromise): Promise<number> {502 const totalNumber = await getCreatedCollectionCount(api);503 const newCollection: number = totalNumber + 1;504 return newCollection;505}506507function getDestroyResult(events: EventRecord[]): boolean {508 let success = false;509 events.forEach(({event: {method}}) => {510 if (method == 'ExtrinsicSuccess') {511 success = true;512 }513 });514 return success;515}516517export async function destroyCollectionExpectFailure(collectionId: number, senderSeed = '//Alice') {518 await usingApi(async (api, privateKeyWrapper) => {519 520 const alicePrivateKey = privateKeyWrapper(senderSeed);521 const tx = api.tx.unique.destroyCollection(collectionId);522 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;523 });524}525526export async function destroyCollectionExpectSuccess(collectionId: number, senderSeed = '//Alice') {527 await usingApi(async (api, privateKeyWrapper) => {528 529 const alicePrivateKey = privateKeyWrapper(senderSeed);530 const tx = api.tx.unique.destroyCollection(collectionId);531 const events = await submitTransactionAsync(alicePrivateKey, tx);532 const result = getDestroyResult(events);533 expect(result).to.be.true;534535 536 expect(await getDetailedCollectionInfo(api, collectionId)).to.be.null;537 });538}539540export async function setCollectionLimitsExpectSuccess(sender: IKeyringPair, collectionId: number, limits: any) {541 await usingApi(async (api) => {542 const tx = api.tx.unique.setCollectionLimits(collectionId, limits);543 const events = await submitTransactionAsync(sender, tx);544 const result = getGenericResult(events);545546 expect(result.success).to.be.true;547 });548}549550export const setCollectionPermissionsExpectSuccess = async (sender: IKeyringPair, collectionId: number, permissions: any) => {551 await usingApi(async(api) => {552 const tx = api.tx.unique.setCollectionPermissions(collectionId, permissions);553 const events = await submitTransactionAsync(sender, tx);554 const result = getGenericResult(events);555556 expect(result.success).to.be.true;557 });558};559560export async function setCollectionLimitsExpectFailure(sender: IKeyringPair, collectionId: number, limits: any) {561 await usingApi(async (api) => {562 const tx = api.tx.unique.setCollectionLimits(collectionId, limits);563 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;564 const result = getGenericResult(events);565566 expect(result.success).to.be.false;567 });568}569570export async function setCollectionSponsorExpectSuccess(collectionId: number, sponsor: string, sender = '//Alice') {571 await usingApi(async (api, privateKeyWrapper) => {572573 574 const senderPrivateKey = privateKeyWrapper(sender);575 const tx = api.tx.unique.setCollectionSponsor(collectionId, sponsor);576 const events = await submitTransactionAsync(senderPrivateKey, tx);577 const result = getGenericResult(events);578579 580 const collection = await queryCollectionExpectSuccess(api, collectionId);581582 583 expect(result.success).to.be.true;584 expect(collection.sponsorship.toJSON()).to.deep.equal({585 unconfirmed: sponsor,586 });587 });588}589590export async function removeCollectionSponsorExpectSuccess(collectionId: number, sender = '//Alice') {591 await usingApi(async (api, privateKeyWrapper) => {592593 594 const alicePrivateKey = privateKeyWrapper(sender);595 const tx = api.tx.unique.removeCollectionSponsor(collectionId);596 const events = await submitTransactionAsync(alicePrivateKey, tx);597 const result = getGenericResult(events);598599 600 const collection = await queryCollectionExpectSuccess(api, collectionId);601602 603 expect(result.success).to.be.true;604 expect(collection.sponsorship.toJSON()).to.be.deep.equal({disabled: null});605 });606}607608export async function removeCollectionSponsorExpectFailure(collectionId: number, senderSeed = '//Alice') {609 await usingApi(async (api, privateKeyWrapper) => {610611 612 const alicePrivateKey = privateKeyWrapper(senderSeed);613 const tx = api.tx.unique.removeCollectionSponsor(collectionId);614 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;615 });616}617618export async function setCollectionSponsorExpectFailure(collectionId: number, sponsor: string, senderSeed = '//Alice') {619 await usingApi(async (api, privateKeyWrapper) => {620621 622 const alicePrivateKey = privateKeyWrapper(senderSeed);623 const tx = api.tx.unique.setCollectionSponsor(collectionId, sponsor);624 await expect(submitTransactionExpectFailAsync(alicePrivateKey, tx)).to.be.rejected;625 });626}627628export async function confirmSponsorshipExpectSuccess(collectionId: number, senderSeed = '//Alice') {629 await usingApi(async (api, privateKeyWrapper) => {630631 632 const sender = privateKeyWrapper(senderSeed);633 await confirmSponsorshipByKeyExpectSuccess(collectionId, sender);634 });635}636637export async function confirmSponsorshipByKeyExpectSuccess(collectionId: number, sender: IKeyringPair) {638 await usingApi(async (api, privateKeyWrapper) => {639640 641 const tx = api.tx.unique.confirmSponsorship(collectionId);642 const events = await submitTransactionAsync(sender, tx);643 const result = getGenericResult(events);644645 646 const collection = await queryCollectionExpectSuccess(api, collectionId);647648 649 expect(result.success).to.be.true;650 expect(collection.sponsorship.toJSON()).to.be.deep.equal({651 confirmed: sender.address,652 });653 });654}655656657export async function confirmSponsorshipExpectFailure(collectionId: number, senderSeed = '//Alice') {658 await usingApi(async (api, privateKeyWrapper) => {659660 661 const sender = privateKeyWrapper(senderSeed);662 const tx = api.tx.unique.confirmSponsorship(collectionId);663 await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;664 });665}666667export async function enableContractSponsoringExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {668 await usingApi(async (api) => {669 const tx = api.tx.unique.enableContractSponsoring(contractAddress, enable);670 const events = await submitTransactionAsync(sender, tx);671 const result = getGenericResult(events);672673 expect(result.success).to.be.true;674 });675}676677export async function enableContractSponsoringExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, enable: boolean) {678 await usingApi(async (api) => {679 const tx = api.tx.unique.enableContractSponsoring(contractAddress, enable);680 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;681 const result = getGenericResult(events);682683 expect(result.success).to.be.false;684 });685}686687export async function setTransferFlagExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {688689 await usingApi(async (api) => {690691 const tx = api.tx.unique.setTransfersEnabledFlag(collectionId, enabled);692 const events = await submitTransactionAsync(sender, tx);693 const result = getGenericResult(events);694695 expect(result.success).to.be.true;696 });697}698699export async function setTransferFlagExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {700701 await usingApi(async (api) => {702703 const tx = api.tx.unique.setTransfersEnabledFlag(collectionId, enabled);704 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;705 const result = getGenericResult(events);706707 expect(result.success).to.be.false;708 });709}710711export async function setContractSponsoringRateLimitExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {712 await usingApi(async (api) => {713 const tx = api.tx.unique.setContractSponsoringRateLimit(contractAddress, rateLimit);714 const events = await submitTransactionAsync(sender, tx);715 const result = getGenericResult(events);716717 expect(result.success).to.be.true;718 });719}720721export async function setContractSponsoringRateLimitExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, rateLimit: number) {722 await usingApi(async (api) => {723 const tx = api.tx.unique.setContractSponsoringRateLimit(contractAddress, rateLimit);724 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;725 const result = getGenericResult(events);726727 expect(result.success).to.be.false;728 });729}730731export async function getNextSponsored(732 api: ApiPromise,733 collectionId: number,734 account: string | CrossAccountId,735 tokenId: number,736): Promise<number> {737 return Number((await api.rpc.unique.nextSponsored(collectionId, account, tokenId)).unwrapOr(-1));738}739740export async function toggleContractAllowlistExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, value = true) {741 await usingApi(async (api) => {742 const tx = api.tx.unique.toggleContractAllowList(contractAddress, value);743 const events = await submitTransactionAsync(sender, tx);744 const result = getGenericResult(events);745746 expect(result.success).to.be.true;747 });748}749750export async function isAllowlistedInContract(contractAddress: AccountId | string, user: string) {751 let allowlisted = false;752 await usingApi(async (api) => {753 allowlisted = (await api.query.unique.contractAllowList(contractAddress, user)).toJSON() as boolean;754 });755 return allowlisted;756}757758export async function addToContractAllowListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: AccountId | string) {759 await usingApi(async (api) => {760 const tx = api.tx.unique.addToContractAllowList(contractAddress.toString(), user.toString());761 const events = await submitTransactionAsync(sender, tx);762 const result = getGenericResult(events);763764 expect(result.success).to.be.true;765 });766}767768export async function removeFromContractAllowListExpectSuccess(sender: IKeyringPair, contractAddress: AccountId | string, user: AccountId | string) {769 await usingApi(async (api) => {770 const tx = api.tx.unique.removeFromContractAllowList(contractAddress.toString(), user.toString());771 const events = await submitTransactionAsync(sender, tx);772 const result = getGenericResult(events);773774 expect(result.success).to.be.true;775 });776}777778export async function removeFromContractAllowListExpectFailure(sender: IKeyringPair, contractAddress: AccountId | string, user: AccountId | string) {779 await usingApi(async (api) => {780 const tx = api.tx.unique.removeFromContractAllowList(contractAddress.toString(), user.toString());781 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;782 const result = getGenericResult(events);783784 expect(result.success).to.be.false;785 });786}787788export interface CreateFungibleData {789 readonly Value: bigint;790}791792export interface CreateReFungibleData { }793export interface CreateNftData { }794795export type CreateItemData = {796 NFT: CreateNftData;797} | {798 Fungible: CreateFungibleData;799} | {800 ReFungible: CreateReFungibleData;801};802803export async function burnItemExpectSuccess(sender: IKeyringPair, collectionId: number, tokenId: number, value: number | bigint = 1) {804 await usingApi(async (api) => {805 const balanceBefore = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);806 807 expect(balanceBefore >= BigInt(value)).to.be.true;808809 const tx = api.tx.unique.burnItem(collectionId, tokenId, value);810 const events = await submitTransactionAsync(sender, tx);811 const result = getGenericResult(events);812 expect(result.success).to.be.true;813814 const balanceAfter = await getBalance(api, collectionId, normalizeAccountId(sender), tokenId);815 expect(balanceAfter + BigInt(value)).to.be.equal(balanceBefore);816 });817}818819export async function820approveExpectSuccess(821 collectionId: number,822 tokenId: number, owner: IKeyringPair, approved: CrossAccountId | string, amount: number | bigint = 1,823) {824 await usingApi(async (api: ApiPromise) => {825 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);826 const events = await submitTransactionAsync(owner, approveUniqueTx);827 const result = getGenericResult(events);828 expect(result.success).to.be.true;829830 expect(await getAllowance(api, collectionId, owner.address, approved, tokenId)).to.be.equal(BigInt(amount));831 });832}833834export async function adminApproveFromExpectSuccess(835 collectionId: number,836 tokenId: number, admin: IKeyringPair, owner: CrossAccountId | string, approved: CrossAccountId | string, amount: number | bigint = 1,837) {838 await usingApi(async (api: ApiPromise) => {839 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);840 const events = await submitTransactionAsync(admin, approveUniqueTx);841 const result = getGenericResult(events);842 expect(result.success).to.be.true;843844 expect(await getAllowance(api, collectionId, owner, approved, tokenId)).to.be.equal(BigInt(amount));845 });846}847848export async function849transferFromExpectSuccess(850 collectionId: number,851 tokenId: number,852 accountApproved: IKeyringPair,853 accountFrom: IKeyringPair | CrossAccountId,854 accountTo: IKeyringPair | CrossAccountId,855 value: number | bigint = 1,856 type = 'NFT',857) {858 await usingApi(async (api: ApiPromise) => {859 const from = normalizeAccountId(accountFrom);860 const to = normalizeAccountId(accountTo);861 let balanceBefore = 0n;862 if (type === 'Fungible' || type === 'ReFungible') {863 balanceBefore = await getBalance(api, collectionId, to, tokenId);864 }865 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom), to, collectionId, tokenId, value);866 const events = await submitTransactionAsync(accountApproved, transferFromTx);867 const result = getGenericResult(events);868 869 expect(result.success).to.be.true;870 if (type === 'NFT') {871 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);872 }873 if (type === 'Fungible') {874 const balanceAfter = await getBalance(api, collectionId, to, tokenId);875 if (JSON.stringify(to) !== JSON.stringify(from)) {876 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));877 } else {878 expect(balanceAfter).to.be.equal(balanceBefore);879 }880 }881 if (type === 'ReFungible') {882 expect(await getBalance(api, collectionId, to, tokenId)).to.be.equal(balanceBefore + BigInt(value));883 }884 });885}886887export async function888transferFromExpectFail(889 collectionId: number,890 tokenId: number,891 accountApproved: IKeyringPair,892 accountFrom: IKeyringPair,893 accountTo: IKeyringPair,894 value: number | bigint = 1,895) {896 await usingApi(async (api: ApiPromise) => {897 const transferFromTx = api.tx.unique.transferFrom(normalizeAccountId(accountFrom.address), normalizeAccountId(accountTo.address), collectionId, tokenId, value);898 const events = await expect(submitTransactionExpectFailAsync(accountApproved, transferFromTx)).to.be.rejected;899 const result = getCreateCollectionResult(events);900 901 expect(result.success).to.be.false;902 });903}904905906export async function getBlockNumber(api: ApiPromise): Promise<number> {907 return new Promise<number>(async (resolve) => {908 const unsubscribe = await api.rpc.chain.subscribeNewHeads((head) => {909 unsubscribe();910 resolve(head.number.toNumber());911 });912 });913}914915export async function addCollectionAdminExpectSuccess(sender: IKeyringPair, collectionId: number, address: string | CrossAccountId) {916 await usingApi(async (api) => {917 const changeAdminTx = api.tx.unique.addCollectionAdmin(collectionId, normalizeAccountId(address));918 const events = await submitTransactionAsync(sender, changeAdminTx);919 const result = getCreateCollectionResult(events);920 expect(result.success).to.be.true;921 });922}923924export async function adminApproveFromExpectFail(925 collectionId: number,926 tokenId: number, admin: IKeyringPair, owner: CrossAccountId | string, approved: CrossAccountId | string, amount: number | bigint = 1,927) {928 await usingApi(async (api: ApiPromise) => {929 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved), collectionId, tokenId, amount);930 const events = await expect(submitTransactionAsync(admin, approveUniqueTx)).to.be.rejected;931 const result = getGenericResult(events);932 expect(result.success).to.be.false;933 });934}935936export async function937getFreeBalance(account: IKeyringPair): Promise<bigint> {938 let balance = 0n;939 await usingApi(async (api) => {940 balance = BigInt((await api.query.system.account(account.address)).data.free.toString());941 });942943 return balance;944}945946export async function transferBalanceTo(api: ApiPromise, source: IKeyringPair, target: string, amount = 1000n * UNIQUE) {947 const tx = api.tx.balances.transfer(target, amount);948 const events = await submitTransactionAsync(source, tx);949 const result = getGenericResult(events);950 expect(result.success).to.be.true;951}952953export async function954scheduleExpectSuccess(955 operationTx: any,956 sender: IKeyringPair,957 blockSchedule: number,958 scheduledId: string,959 period = 1,960 repetitions = 1,961) {962 await usingApi(async (api: ApiPromise) => {963 const blockNumber: number | undefined = await getBlockNumber(api);964 const expectedBlockNumber = blockNumber + blockSchedule;965966 expect(blockNumber).to.be.greaterThan(0);967 const scheduleTx = api.tx.scheduler.scheduleNamed( 968 scheduledId,969 expectedBlockNumber, 970 repetitions > 1 ? [period, repetitions] : null, 971 0, 972 {value: operationTx as any},973 );974975 const events = await submitTransactionAsync(sender, scheduleTx);976 expect(getGenericResult(events).success).to.be.true;977 });978}979980export async function981scheduleExpectFailure(982 operationTx: any,983 sender: IKeyringPair,984 blockSchedule: number,985 scheduledId: string,986 period = 1,987 repetitions = 1,988) {989 await usingApi(async (api: ApiPromise) => {990 const blockNumber: number | undefined = await getBlockNumber(api);991 const expectedBlockNumber = blockNumber + blockSchedule;992993 expect(blockNumber).to.be.greaterThan(0);994 const scheduleTx = api.tx.scheduler.scheduleNamed( 995 scheduledId,996 expectedBlockNumber, 997 repetitions <= 1 ? null : [period, repetitions], 998 0, 999 {value: operationTx as any},1000 );10011002 1003 await expect(submitTransactionExpectFailAsync(sender, scheduleTx)).to.be.rejected;1004 1005 });1006}10071008export async function1009scheduleTransferAndWaitExpectSuccess(1010 collectionId: number,1011 tokenId: number,1012 sender: IKeyringPair,1013 recipient: IKeyringPair,1014 value: number | bigint = 1,1015 blockSchedule: number,1016 scheduledId: string,1017) {1018 await usingApi(async (api: ApiPromise) => {1019 await scheduleTransferExpectSuccess(collectionId, tokenId, sender, recipient, value, blockSchedule, scheduledId);10201021 const recipientBalanceBefore = (await api.query.system.account(recipient.address)).data.free.toBigInt();10221023 1024 await waitNewBlocks(blockSchedule + 1);10251026 const recipientBalanceAfter = (await api.query.system.account(recipient.address)).data.free.toBigInt();10271028 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(recipient.address));1029 expect(recipientBalanceAfter).to.be.equal(recipientBalanceBefore);1030 });1031}10321033export async function1034scheduleTransferExpectSuccess(1035 collectionId: number,1036 tokenId: number,1037 sender: IKeyringPair,1038 recipient: IKeyringPair,1039 value: number | bigint = 1,1040 blockSchedule: number,1041 scheduledId: string,1042) {1043 await usingApi(async (api: ApiPromise) => {1044 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient.address), collectionId, tokenId, value);10451046 await scheduleExpectSuccess(transferTx, sender, blockSchedule, scheduledId);10471048 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(normalizeAccountId(sender.address));1049 });1050}10511052export async function1053scheduleTransferFundsPeriodicExpectSuccess(1054 amount: bigint,1055 sender: IKeyringPair,1056 recipient: IKeyringPair,1057 blockSchedule: number,1058 scheduledId: string,1059 period: number,1060 repetitions: number,1061) {1062 await usingApi(async (api: ApiPromise) => {1063 const transferTx = api.tx.balances.transfer(recipient.address, amount);10641065 const balanceBefore = await getFreeBalance(recipient);1066 1067 await scheduleExpectSuccess(transferTx, sender, blockSchedule, scheduledId, period, repetitions);10681069 expect(await getFreeBalance(recipient)).to.be.equal(balanceBefore);1070 });1071}10721073export async function1074transferExpectSuccess(1075 collectionId: number,1076 tokenId: number,1077 sender: IKeyringPair,1078 recipient: IKeyringPair | CrossAccountId,1079 value: number | bigint = 1,1080 type = 'NFT',1081) {1082 await usingApi(async (api: ApiPromise) => {1083 const from = normalizeAccountId(sender);1084 const to = normalizeAccountId(recipient);10851086 let balanceBefore = 0n;1087 if (type === 'Fungible' || type === 'ReFungible') {1088 balanceBefore = await getBalance(api, collectionId, to, tokenId);1089 }1090 const transferTx = api.tx.unique.transfer(to, collectionId, tokenId, value);1091 const events = await executeTransaction(api, sender, transferTx);10921093 const result = getTransferResult(api, events);1094 expect(result.collectionId).to.be.equal(collectionId);1095 expect(result.itemId).to.be.equal(tokenId);1096 expect(result.sender).to.be.deep.equal(normalizeAccountId(sender.address));1097 expect(result.recipient).to.be.deep.equal(to);1098 expect(result.value).to.be.equal(BigInt(value));10991100 if (type === 'NFT') {1101 expect(await getTokenOwner(api, collectionId, tokenId)).to.be.deep.equal(to);1102 }1103 if (type === 'Fungible' || type === 'ReFungible') {1104 const balanceAfter = await getBalance(api, collectionId, to, tokenId);1105 if (JSON.stringify(to) !== JSON.stringify(from)) {1106 expect(balanceAfter - balanceBefore).to.be.equal(BigInt(value));1107 } else {1108 expect(balanceAfter).to.be.equal(balanceBefore);1109 }1110 }1111 });1112}11131114export async function1115transferExpectFailure(1116 collectionId: number,1117 tokenId: number,1118 sender: IKeyringPair,1119 recipient: IKeyringPair | CrossAccountId,1120 value: number | bigint = 1,1121) {1122 await usingApi(async (api: ApiPromise) => {1123 const transferTx = api.tx.unique.transfer(normalizeAccountId(recipient), collectionId, tokenId, value);1124 const events = await expect(submitTransactionExpectFailAsync(sender, transferTx)).to.be.rejected;1125 const result = getGenericResult(events);1126 1127 1128 1129 expect(result.success).to.be.false;1130 1131 });1132}11331134export async function1135approveExpectFail(1136 collectionId: number,1137 tokenId: number, owner: IKeyringPair, approved: IKeyringPair, amount: number | bigint = 1,1138) {1139 await usingApi(async (api: ApiPromise) => {1140 const approveUniqueTx = api.tx.unique.approve(normalizeAccountId(approved.address), collectionId, tokenId, amount);1141 const events = await expect(submitTransactionExpectFailAsync(owner, approveUniqueTx)).to.be.rejected;1142 const result = getCreateCollectionResult(events);1143 1144 expect(result.success).to.be.false;1145 });1146}11471148export async function getBalance(1149 api: ApiPromise,1150 collectionId: number,1151 owner: string | CrossAccountId,1152 token: number,1153): Promise<bigint> {1154 return (await api.rpc.unique.balance(collectionId, normalizeAccountId(owner), token)).toBigInt();1155}1156export async function getTokenOwner(1157 api: ApiPromise,1158 collectionId: number,1159 token: number,1160): Promise<CrossAccountId> {1161 const owner = (await api.rpc.unique.tokenOwner(collectionId, token)).toJSON() as any;1162 if (owner == null) throw new Error('owner == null');1163 return normalizeAccountId(owner);1164}1165export async function getTopmostTokenOwner(1166 api: ApiPromise,1167 collectionId: number,1168 token: number,1169): Promise<CrossAccountId> {1170 const owner = (await api.rpc.unique.topmostTokenOwner(collectionId, token)).toJSON() as any;1171 if (owner == null) throw new Error('owner == null');1172 return normalizeAccountId(owner);1173}1174export async function getTokenChildren(1175 api: ApiPromise,1176 collectionId: number,1177 tokenId: number,1178): Promise<UpDataStructsTokenChild[]> {1179 return (await api.rpc.unique.tokenChildren(collectionId, tokenId)).toJSON() as any;1180}1181export async function isTokenExists(1182 api: ApiPromise,1183 collectionId: number,1184 token: number,1185): Promise<boolean> {1186 return (await api.rpc.unique.tokenExists(collectionId, token)).toJSON();1187}1188export async function getLastTokenId(1189 api: ApiPromise,1190 collectionId: number,1191): Promise<number> {1192 return (await api.rpc.unique.lastTokenId(collectionId)).toJSON();1193}1194export async function getAdminList(1195 api: ApiPromise,1196 collectionId: number,1197): Promise<string[]> {1198 return (await api.rpc.unique.adminlist(collectionId)).toHuman() as any;1199}1200export async function getTokenProperties(1201 api: ApiPromise,1202 collectionId: number,1203 tokenId: number,1204 propertyKeys: string[],1205): Promise<UpDataStructsProperty[]> {1206 return (await api.rpc.unique.tokenProperties(collectionId, tokenId, propertyKeys)).toHuman() as any;1207}12081209export async function createFungibleItemExpectSuccess(1210 sender: IKeyringPair,1211 collectionId: number,1212 data: CreateFungibleData,1213 owner: CrossAccountId | string = sender.address,1214) {1215 return await usingApi(async (api) => {1216 const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), {Fungible: data});12171218 const events = await submitTransactionAsync(sender, tx);1219 const result = getCreateItemResult(events);12201221 expect(result.success).to.be.true;1222 return result.itemId;1223 });1224}12251226export async function createMultipleItemsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1227 await usingApi(async (api) => {1228 const to = normalizeAccountId(owner);1229 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);12301231 const events = await submitTransactionAsync(sender, tx);1232 const result = getCreateItemsResult(events);1233 });1234}12351236export async function createMultipleItemsWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any, owner: CrossAccountId | string = sender.address) {1237 await usingApi(async (api) => {1238 const to = normalizeAccountId(owner);1239 const tx = api.tx.unique.createMultipleItems(collectionId, to, itemsData);12401241 const events = await submitTransactionAsync(sender, tx);1242 const result = getCreateItemsResult(events);12431244 for (const res of result) {1245 expect(await api.rpc.unique.tokenProperties(collectionId, res.itemId)).not.to.be.empty;1246 }1247 });1248}12491250export async function createMultipleItemsExWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, itemsData: any) {1251 await usingApi(async (api) => {1252 const tx = api.tx.unique.createMultipleItemsEx(collectionId, itemsData);12531254 const events = await submitTransactionAsync(sender, tx);1255 const result = getCreateItemsResult(events);12561257 for (const res of result) {1258 expect(await api.rpc.unique.tokenProperties(collectionId, res.itemId)).not.to.be.empty;1259 }1260 });1261}12621263export async function createItemWithPropsExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, props: Array<Property>, owner: CrossAccountId | string = sender.address) {1264 let newItemId = 0;1265 await usingApi(async (api) => {1266 const to = normalizeAccountId(owner);1267 const itemCountBefore = await getLastTokenId(api, collectionId);1268 const itemBalanceBefore = await getBalance(api, collectionId, to, newItemId);12691270 let tx;1271 if (createMode === 'Fungible') {1272 const createData = {fungible: {value: 10}};1273 tx = api.tx.unique.createItem(collectionId, to, createData as any);1274 } else if (createMode === 'ReFungible') {1275 const createData = {refungible: {pieces: 100}};1276 tx = api.tx.unique.createItem(collectionId, to, createData as any);1277 } else {1278 const data = api.createType('UpDataStructsCreateItemData', {NFT: {properties: props}});1279 tx = api.tx.unique.createItem(collectionId, to, data as UpDataStructsCreateItemData);1280 }12811282 const events = await submitTransactionAsync(sender, tx);1283 const result = getCreateItemResult(events);12841285 const itemCountAfter = await getLastTokenId(api, collectionId);1286 const itemBalanceAfter = await getBalance(api, collectionId, to, newItemId);12871288 if (createMode === 'NFT') {1289 expect(await api.rpc.unique.tokenProperties(collectionId, result.itemId)).not.to.be.empty;1290 }12911292 1293 1294 expect(result.success).to.be.true;1295 if (createMode === 'Fungible') {1296 expect(itemBalanceAfter - itemBalanceBefore).to.be.equal(10n);1297 } else {1298 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);1299 }1300 expect(collectionId).to.be.equal(result.collectionId);1301 expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());1302 expect(to).to.be.deep.equal(result.recipient);1303 newItemId = result.itemId;1304 });1305 return newItemId;1306}13071308export async function createItemWithPropsExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, props: Array<Property>, owner: CrossAccountId | string = sender.address) {1309 await usingApi(async (api) => {13101311 let tx;1312 if (createMode === 'NFT') {1313 const data = api.createType('UpDataStructsCreateItemData', {NFT: {properties: props}});1314 tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), data);1315 } else {1316 tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createMode);1317 }131813191320 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1321 if(events.message && events.message.toString().indexOf('1002: Verification Error') > -1) return;1322 const result = getCreateItemResult(events);13231324 expect(result.success).to.be.false;1325 });1326}13271328export async function createItemExpectSuccess(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {1329 let newItemId = 0;1330 await usingApi(async (api) => {1331 const to = normalizeAccountId(owner);1332 const itemCountBefore = await getLastTokenId(api, collectionId);1333 const itemBalanceBefore = await getBalance(api, collectionId, to, newItemId);13341335 let tx;1336 if (createMode === 'Fungible') {1337 const createData = {fungible: {value: 10}};1338 tx = api.tx.unique.createItem(collectionId, to, createData as any);1339 } else if (createMode === 'ReFungible') {1340 const createData = {refungible: {pieces: 100}};1341 tx = api.tx.unique.createItem(collectionId, to, createData as any);1342 } else {1343 const createData = {nft: {}};1344 tx = api.tx.unique.createItem(collectionId, to, createData as any);1345 }13461347 const events = await submitTransactionAsync(sender, tx);1348 const result = getCreateItemResult(events);13491350 const itemCountAfter = await getLastTokenId(api, collectionId);1351 const itemBalanceAfter = await getBalance(api, collectionId, to, newItemId);13521353 1354 1355 expect(result.success).to.be.true;1356 if (createMode === 'Fungible') {1357 expect(itemBalanceAfter - itemBalanceBefore).to.be.equal(10n);1358 } else {1359 expect(itemCountAfter).to.be.equal(itemCountBefore + 1);1360 }1361 expect(collectionId).to.be.equal(result.collectionId);1362 expect(itemCountAfter.toString()).to.be.equal(result.itemId.toString());1363 expect(to).to.be.deep.equal(result.recipient);1364 newItemId = result.itemId;1365 });1366 return newItemId;1367}13681369export async function createItemExpectFailure(sender: IKeyringPair, collectionId: number, createMode: string, owner: CrossAccountId | string = sender.address) {1370 await usingApi(async (api) => {1371 const tx = api.tx.unique.createItem(collectionId, normalizeAccountId(owner), createMode);13721373 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1374 const result = getCreateItemResult(events);13751376 expect(result.success).to.be.false;1377 });1378}13791380export async function setPublicAccessModeExpectSuccess(1381 sender: IKeyringPair, collectionId: number,1382 accessMode: 'Normal' | 'AllowList',1383) {1384 await usingApi(async (api) => {13851386 1387 const tx = api.tx.unique.setCollectionPermissions(collectionId, {access: accessMode});1388 const events = await submitTransactionAsync(sender, tx);1389 const result = getGenericResult(events);13901391 1392 const collection = await queryCollectionExpectSuccess(api, collectionId);13931394 1395 1396 expect(result.success).to.be.true;1397 expect(collection.permissions.access.toHuman()).to.be.equal(accessMode);1398 });1399}14001401export async function setPublicAccessModeExpectFail(1402 sender: IKeyringPair, collectionId: number,1403 accessMode: 'Normal' | 'AllowList',1404) {1405 await usingApi(async (api) => {14061407 1408 const tx = api.tx.unique.setCollectionPermissions(collectionId, {access: accessMode});1409 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1410 const result = getGenericResult(events);14111412 1413 1414 expect(result.success).to.be.false;1415 });1416}14171418export async function enableAllowListExpectSuccess(sender: IKeyringPair, collectionId: number) {1419 await setPublicAccessModeExpectSuccess(sender, collectionId, 'AllowList');1420}14211422export async function enableAllowListExpectFail(sender: IKeyringPair, collectionId: number) {1423 await setPublicAccessModeExpectFail(sender, collectionId, 'AllowList');1424}14251426export async function disableAllowListExpectSuccess(sender: IKeyringPair, collectionId: number) {1427 await setPublicAccessModeExpectSuccess(sender, collectionId, 'Normal');1428}14291430export async function setMintPermissionExpectSuccess(sender: IKeyringPair, collectionId: number, enabled: boolean) {1431 await usingApi(async (api) => {14321433 1434 const tx = api.tx.unique.setCollectionPermissions(collectionId, {mintMode: enabled});1435 const events = await submitTransactionAsync(sender, tx);1436 const result = getGenericResult(events);1437 expect(result.success).to.be.true;14381439 1440 const collection = await queryCollectionExpectSuccess(api, collectionId);14411442 expect(collection.permissions.mintMode.toHuman()).to.be.equal(enabled);1443 });1444}14451446export async function enablePublicMintingExpectSuccess(sender: IKeyringPair, collectionId: number) {1447 await setMintPermissionExpectSuccess(sender, collectionId, true);1448}14491450export async function setMintPermissionExpectFailure(sender: IKeyringPair, collectionId: number, enabled: boolean) {1451 await usingApi(async (api) => {1452 1453 const tx = api.tx.unique.setCollectionPermissions(collectionId, {mintMode: enabled});1454 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1455 const result = getCreateCollectionResult(events);1456 1457 expect(result.success).to.be.false;1458 });1459}14601461export async function setChainLimitsExpectFailure(sender: IKeyringPair, limits: IChainLimits) {1462 await usingApi(async (api) => {1463 1464 const tx = api.tx.unique.setChainLimits(limits);1465 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1466 const result = getCreateCollectionResult(events);1467 1468 expect(result.success).to.be.false;1469 });1470}14711472export async function isAllowlisted(api: ApiPromise, collectionId: number, address: string | CrossAccountId) {1473 return (await api.rpc.unique.allowed(collectionId, normalizeAccountId(address))).toJSON();1474}14751476export async function addToAllowListExpectSuccess(sender: IKeyringPair, collectionId: number, address: string | AccountId | CrossAccountId) {1477 await usingApi(async (api) => {1478 expect(await isAllowlisted(api, collectionId, normalizeAccountId(address))).to.be.false;14791480 1481 const tx = api.tx.unique.addToAllowList(collectionId, normalizeAccountId(address));1482 const events = await submitTransactionAsync(sender, tx);1483 const result = getGenericResult(events);1484 expect(result.success).to.be.true;14851486 expect(await isAllowlisted(api, collectionId, normalizeAccountId(address))).to.be.true;1487 });1488}14891490export async function addToAllowListAgainExpectSuccess(sender: IKeyringPair, collectionId: number, address: string | AccountId) {1491 await usingApi(async (api) => {14921493 expect(await isAllowlisted(api, collectionId, normalizeAccountId(address))).to.be.true;14941495 1496 const tx = api.tx.unique.addToAllowList(collectionId, normalizeAccountId(address));1497 const events = await submitTransactionAsync(sender, tx);1498 const result = getGenericResult(events);1499 expect(result.success).to.be.true;15001501 expect(await isAllowlisted(api, collectionId, normalizeAccountId(address))).to.be.true;1502 });1503}15041505export async function addToAllowListExpectFail(sender: IKeyringPair, collectionId: number, address: string | AccountId) {1506 await usingApi(async (api) => {15071508 1509 const tx = api.tx.unique.addToAllowList(collectionId, normalizeAccountId(address));1510 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1511 const result = getGenericResult(events);15121513 1514 1515 expect(result.success).to.be.false;1516 });1517}15181519export async function removeFromAllowListExpectSuccess(sender: IKeyringPair, collectionId: number, address: CrossAccountId) {1520 await usingApi(async (api) => {1521 1522 const tx = api.tx.unique.removeFromAllowList(collectionId, normalizeAccountId(address));1523 const events = await submitTransactionAsync(sender, tx);1524 const result = getGenericResult(events);15251526 1527 1528 expect(result.success).to.be.true;1529 });1530}15311532export async function removeFromAllowListExpectFailure(sender: IKeyringPair, collectionId: number, address: CrossAccountId) {1533 await usingApi(async (api) => {1534 1535 const tx = api.tx.unique.removeFromAllowList(collectionId, normalizeAccountId(address));1536 const events = await expect(submitTransactionExpectFailAsync(sender, tx)).to.be.rejected;1537 const result = getGenericResult(events);15381539 1540 1541 expect(result.success).to.be.false;1542 });1543}15441545export const getDetailedCollectionInfo = async (api: ApiPromise, collectionId: number)1546 : Promise<UpDataStructsRpcCollection | null> => {1547 return (await api.rpc.unique.collectionById(collectionId)).unwrapOr(null);1548};15491550export const getCreatedCollectionCount = async (api: ApiPromise): Promise<number> => {1551 1552 return (await api.rpc.unique.collectionStats()).created.toNumber();1553};15541555export async function queryCollectionExpectSuccess(api: ApiPromise, collectionId: number): Promise<UpDataStructsRpcCollection> {1556 return (await api.rpc.unique.collectionById(collectionId)).unwrap();1557}15581559export async function waitNewBlocks(blocksCount = 1): Promise<void> {1560 await usingApi(async (api) => {1561 const promise = new Promise<void>(async (resolve) => {1562 const unsubscribe = await api.rpc.chain.subscribeNewHeads(() => {1563 if (blocksCount > 0) {1564 blocksCount--;1565 } else {1566 unsubscribe();1567 resolve();1568 }1569 });1570 });1571 return promise;1572 });1573}