git.delta.rocks / unique-network / refs/commits / 85f3ef2e0166

difftreelog

move types and interfaces to types.ts

Max Andreev2022-08-24parent: #a36e310.patch.diff
in: master

3 files changed

addedtests/src/util/playgrounds/types.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/util/playgrounds/types.ts
@@ -0,0 +1,130 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// SPDX-License-Identifier: Apache-2.0
+
+import {IKeyringPair} from '@polkadot/types/types';
+
+export interface IChainEvent {
+  data: any;
+  method: string;
+  section: string;
+}
+
+export interface ITransactionResult {
+    status: 'Fail' | 'Success';
+    result: {
+        events: {
+          event: IChainEvent
+        }[];
+    },
+    moduleError?: string;
+}
+
+export interface ILogger {
+  log: (msg: any, level?: string) => void;
+  level: {
+    ERROR: 'ERROR';
+    WARNING: 'WARNING';
+    INFO: 'INFO';
+    [key: string]: string;
+  }
+}
+
+export interface IUniqueHelperLog {
+  executedAt: number;
+  executionTime: number;
+  type: 'extrinsic' | 'rpc';
+  status: 'Fail' | 'Success';
+  call: string;
+  params: any[];
+  moduleError?: string;
+  events?: any;
+}
+
+export interface IApiListeners {
+  connected?: (...args: any[]) => any;
+  disconnected?: (...args: any[]) => any;
+  error?: (...args: any[]) => any;
+  ready?: (...args: any[]) => any; 
+  decorated?: (...args: any[]) => any;
+}
+
+export interface ICrossAccountId {
+  Substrate?: TSubstrateAccount;
+  Ethereum?: TEthereumAccount;
+}
+
+export interface ICrossAccountIdLower {
+  substrate?: TSubstrateAccount;
+  ethereum?: TEthereumAccount;
+}
+
+export interface ICollectionLimits {
+  accountTokenOwnershipLimit?: number | null;
+  sponsoredDataSize?: number | null;
+  sponsoredDataRateLimit?: {blocks: number} | {sponsoringDisabled: null} | null;
+  tokenLimit?: number | null;
+  sponsorTransferTimeout?: number | null;
+  sponsorApproveTimeout?: number | null;
+  ownerCanTransfer?: boolean | null;
+  ownerCanDestroy?: boolean | null;
+  transfersEnabled?: boolean | null;
+}
+
+export interface INestingPermissions {
+  tokenOwner?: boolean;
+  collectionAdmin?: boolean;
+  restricted?: number[] | null;
+}
+
+export interface ICollectionPermissions {
+  access?: 'Normal' | 'AllowList';
+  mintMode?: boolean;
+  nesting?: INestingPermissions;
+}
+
+export interface IProperty {
+  key: string;
+  value: string;
+}
+
+export interface ITokenPropertyPermission {
+  key: string;
+  permission: {
+    mutable: boolean;
+    tokenOwner: boolean;
+    collectionAdmin: boolean;
+  }
+}
+
+export interface IToken {
+  collectionId: number;
+  tokenId: number;
+}
+
+export interface ICollectionCreationOptions {
+  name: string | number[];
+  description: string | number[];
+  tokenPrefix: string | number[];
+  mode?: {
+    nft?: null;
+    refungible?: null;
+    fungible?: number;
+  }
+  permissions?: ICollectionPermissions;
+  properties?: IProperty[];
+  tokenPropertyPermissions?: ITokenPropertyPermission[];
+  limits?: ICollectionLimits;
+  pendingSponsor?: TSubstrateAccount;
+}
+
+export interface IChainProperties {
+  ss58Format: number;
+  tokenDecimals: number[];
+  tokenSymbol: string[]
+}
+
+export type TSubstrateAccount = string;
+export type TEthereumAccount = string;
+export type TApiAllowedListeners = 'connected' | 'disconnected' | 'error' | 'ready' | 'decorated';
+export type TUniqueNetworks = 'opal' | 'quartz' | 'unique';
+export type TSigner = IKeyringPair; // | 'string'
\ No newline at end of file
modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.dev.ts
+++ b/tests/src/util/playgrounds/unique.dev.ts
@@ -3,9 +3,9 @@
 
 import {mnemonicGenerate} from '@polkadot/util-crypto';
 import {UniqueHelper} from './unique';
-import {IKeyringPair} from '@polkadot/types/types';
 import {ApiPromise, WsProvider} from '@polkadot/api';
 import * as defs from '../../interfaces/definitions';
+import { TSigner } from './types';
 
 
 export class DevUniqueHelper extends UniqueHelper {
@@ -69,7 +69,7 @@
    * @returns array of newly created accounts
    * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 
    */
-  creteAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {
+  creteAccounts = async (balances: bigint[], donor: TSigner): Promise<TSigner[]> => {
     let nonce = await this.helper.chain.getNonce(donor.address);
     const tokenNominal = this.helper.balance.getOneTokenNominal();
     const transactions = [];
modifiedtests/src/util/playgrounds/unique.tsdiffbeforeafterboth
before · tests/src/util/playgrounds/unique.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034/* eslint-disable @typescript-eslint/no-var-requires */5/* eslint-disable function-call-argument-newline */6/* eslint-disable no-prototype-builtins */78import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';9import {ApiInterfaceEvents} from '@polkadot/api/types';10import {IKeyringPair} from '@polkadot/types/types';11import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';121314const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {15  const address = {} as ICrossAccountId;16  if(lowerAddress.substrate) address.Substrate = lowerAddress.substrate;17  if(lowerAddress.ethereum) address.Ethereum = lowerAddress.ethereum;18  return address;19};202122const nesting = {23  toChecksumAddress(address: string): string {24    if (typeof address === 'undefined') return '';2526    if(!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);2728    address = address.toLowerCase().replace(/^0x/i,'');29    const addressHash = keccakAsHex(address).replace(/^0x/i,'');30    const checksumAddress = ['0x'];3132    for (let i = 0; i < address.length; i++) {33      // If ith character is 8 to f then make it uppercase34      if (parseInt(addressHash[i], 16) > 7) {35        checksumAddress.push(address[i].toUpperCase());36      } else {37        checksumAddress.push(address[i]);38      }39    }40    return checksumAddress.join('');41  },42  tokenIdToAddress(collectionId: number, tokenId: number) {43    return this.toChecksumAddress(`0xf8238ccfff8ed887463fd5e0${collectionId.toString(16).padStart(8, '0')}${tokenId.toString(16).padStart(8, '0')}`);44  },45};464748interface IChainEvent {49  data: any;50  method: string;51  section: string;52}5354interface ITransactionResult {55    status: 'Fail' | 'Success';56    result: {57        events: {58          event: IChainEvent59        }[];60    },61    moduleError?: string;62}6364interface ILogger {65  log: (msg: any, level?: string) => void;66  level: {67    ERROR: 'ERROR';68    WARNING: 'WARNING';69    INFO: 'INFO';70    [key: string]: string;71  }72}7374interface IUniqueHelperLog {75  executedAt: number;76  executionTime: number;77  type: 'extrinsic' | 'rpc';78  status: 'Fail' | 'Success';79  call: string;80  params: any[];81  moduleError?: string;82  events?: any;83}8485interface IApiListeners {86  connected?: (...args: any[]) => any;87  disconnected?: (...args: any[]) => any;88  error?: (...args: any[]) => any;89  ready?: (...args: any[]) => any; 90  decorated?: (...args: any[]) => any;91}9293interface ICrossAccountId {94  Substrate?: TSubstrateAccount;95  Ethereum?: TEthereumAccount;96}9798interface ICrossAccountIdLower {99  substrate?: TSubstrateAccount;100  ethereum?: TEthereumAccount;101}102103interface ICollectionLimits {104  accountTokenOwnershipLimit?: number | null;105  sponsoredDataSize?: number | null;106  sponsoredDataRateLimit?: {blocks: number} | {sponsoringDisabled: null} | null;107  tokenLimit?: number | null;108  sponsorTransferTimeout?: number | null;109  sponsorApproveTimeout?: number | null;110  ownerCanTransfer?: boolean | null;111  ownerCanDestroy?: boolean | null;112  transfersEnabled?: boolean | null;113}114115interface INestingPermissions {116  tokenOwner?: boolean;117  collectionAdmin?: boolean;118  restricted?: number[] | null;119}120121interface ICollectionPermissions {122  access?: 'Normal' | 'AllowList';123  mintMode?: boolean;124  nesting?: INestingPermissions;125}126127interface IProperty {128  key: string;129  value: string;130}131132interface ITokenPropertyPermission {133  key: string;134  permission: {135    mutable: boolean;136    tokenOwner: boolean;137    collectionAdmin: boolean;138  }139}140141interface IToken {142  collectionId: number;143  tokenId: number;144}145146interface ICollectionCreationOptions {147  name: string | number[];148  description: string | number[];149  tokenPrefix: string | number[];150  mode?: {151    nft?: null;152    refungible?: null;153    fungible?: number;154  }155  permissions?: ICollectionPermissions;156  properties?: IProperty[];157  tokenPropertyPermissions?: ITokenPropertyPermission[];158  limits?: ICollectionLimits;159  pendingSponsor?: TSubstrateAccount;160}161162interface IChainProperties {163  ss58Format: number;164  tokenDecimals: number[];165  tokenSymbol: string[]166}167168type TSubstrateAccount = string;169type TEthereumAccount = string;170type TApiAllowedListeners = 'connected' | 'disconnected' | 'error' | 'ready' | 'decorated';171type TUniqueNetworks = 'opal' | 'quartz' | 'unique';172type TSigner = IKeyringPair; // | 'string'173174class UniqueUtil {175  static transactionStatus = {176    NOT_READY: 'NotReady',177    FAIL: 'Fail',178    SUCCESS: 'Success',179  };180181  static chainLogType = {182    EXTRINSIC: 'extrinsic',183    RPC: 'rpc',184  };185186  static getNestingTokenAddress(collectionId: number, tokenId: number) {187    return nesting.tokenIdToAddress(collectionId, tokenId);188  }189190  static getDefaultLogger(): ILogger {191    return {192      log(msg: any, level = 'INFO') {193        console[level.toLocaleLowerCase() === 'error' ? 'error' : 'log'](...(Array.isArray(msg) ? msg : [msg]));194      },195      level: {196        ERROR: 'ERROR',197        WARNING: 'WARNING',198        INFO: 'INFO',199      },200    };201  }202203  static vec2str(arr: string[] | number[]) {204    return arr.map(x => String.fromCharCode(parseInt(x.toString()))).join('');205  }206207  static str2vec(string: string) {208    if (typeof string !== 'string') return string;209    return Array.from(string).map(x => x.charCodeAt(0));210  }211212  static fromSeed(seed: string, ss58Format = 42) {213    const keyring = new Keyring({type: 'sr25519', ss58Format});214    return keyring.addFromUri(seed);215  }216217  static normalizeSubstrateAddress(address: string, ss58Format = 42) {218    return encodeAddress(decodeAddress(address), ss58Format);219  }220221  static extractCollectionIdFromCreationResult(creationResult: ITransactionResult, label = 'new collection') {222    if (creationResult.status !== this.transactionStatus.SUCCESS) {223      throw Error(`Unable to create collection for ${label}`);224    }225226    let collectionId = null;227    creationResult.result.events.forEach(({event: {data, method, section}}) => {228      if ((section === 'common') && (method === 'CollectionCreated')) {229        collectionId = parseInt(data[0].toString(), 10);230      }231    });232233    if (collectionId === null) {234      throw Error(`No CollectionCreated event for ${label}`);235    }236237    return collectionId;238  }239240  static extractTokensFromCreationResult(creationResult: ITransactionResult, label = 'new tokens') {241    if (creationResult.status !== this.transactionStatus.SUCCESS) {242      throw Error(`Unable to create tokens for ${label}`);243    }244    let success = false;245    const tokens = [] as any;246    creationResult.result.events.forEach(({event: {data, method, section}}) => {247      if (method === 'ExtrinsicSuccess') {248        success = true;249      } else if ((section === 'common') && (method === 'ItemCreated')) {250        tokens.push({251          collectionId: parseInt(data[0].toString(), 10),252          tokenId: parseInt(data[1].toString(), 10),253          owner: data[2].toJSON(),254        });255      }256    });257    return {success, tokens};258  }259260  static extractTokensFromBurnResult(burnResult: ITransactionResult, label = 'burned tokens') {261    if (burnResult.status !== this.transactionStatus.SUCCESS) {262      throw Error(`Unable to burn tokens for ${label}`);263    }264    let success = false;265    const tokens = [] as any;266    burnResult.result.events.forEach(({event: {data, method, section}}) => {267      if (method === 'ExtrinsicSuccess') {268        success = true;269      } else if ((section === 'common') && (method === 'ItemDestroyed')) {270        tokens.push({271          collectionId: parseInt(data[0].toString(), 10),272          tokenId: parseInt(data[1].toString(), 10),273          owner: data[2].toJSON(),274        });275      }276    });277    return {success, tokens};278  }279280  static findCollectionInEvents(events: {event: IChainEvent}[], collectionId: number, expectedSection: string, expectedMethod: string, label?: string) {281    let eventId = null;282    events.forEach(({event: {data, method, section}}) => {283      if ((section === expectedSection) && (method === expectedMethod)) {284        eventId = parseInt(data[0].toString(), 10);285      }286    });287288    if (eventId === null) {289      throw Error(`No ${expectedMethod} event for ${label}`);290    }291    return eventId === collectionId;292  }293294  static isTokenTransferSuccess(events: {event: IChainEvent}[], collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {295    const normalizeAddress = (address: string | ICrossAccountId) => {296      if(typeof address === 'string') return address;297      const obj = {} as any;298      Object.keys(address).forEach(k => {299        obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];300      });301      if(obj.substrate) return {Substrate: this.normalizeSubstrateAddress(obj.substrate)};302      if(obj.ethereum) return {Ethereum: obj.ethereum.toLocaleLowerCase()};303      return address;304    };305    let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;306    events.forEach(({event: {data, method, section}}) => {307      if ((section === 'common') && (method === 'Transfer')) {308        const hData = (data as any).toJSON();309        transfer = {310          collectionId: hData[0],311          tokenId: hData[1],312          from: normalizeAddress(hData[2]),313          to: normalizeAddress(hData[3]),314          amount: BigInt(hData[4]),315        };316      }317    });318    let isSuccess = parseInt(collectionId.toString()) === transfer.collectionId && parseInt(tokenId.toString()) === transfer.tokenId;319    isSuccess = isSuccess && JSON.stringify(normalizeAddress(fromAddressObj)) === JSON.stringify(transfer.from);320    isSuccess = isSuccess && JSON.stringify(normalizeAddress(toAddressObj)) === JSON.stringify(transfer.to);321    isSuccess = isSuccess && amount === transfer.amount;322    return isSuccess;323  }324}325326327class ChainHelperBase {328  transactionStatus = UniqueUtil.transactionStatus;329  chainLogType = UniqueUtil.chainLogType;330  util: typeof UniqueUtil;331  logger: ILogger;332  api: ApiPromise | null;333  forcedNetwork: TUniqueNetworks | null;334  network: TUniqueNetworks | null;335  chainLog: IUniqueHelperLog[];336337  constructor(logger?: ILogger) {338    this.util = UniqueUtil;339    if (typeof logger == 'undefined') logger = this.util.getDefaultLogger();340    this.logger = logger;341    this.api = null;342    this.forcedNetwork = null;343    this.network = null;344    this.chainLog = [];345  }346347  clearChainLog(): void {348    this.chainLog = [];349  }350351  forceNetwork(value: TUniqueNetworks): void {352    this.forcedNetwork = value;353  }354355  async connect(wsEndpoint: string, listeners?: IApiListeners) {356    if (this.api !== null) throw Error('Already connected');357    const {api, network} = await ChainHelperBase.createConnection(wsEndpoint, listeners, this.forcedNetwork);358    this.api = api;359    this.network = network;360  }361362  async disconnect() {363    if (this.api === null) return;364    await this.api.disconnect();365    this.api = null;366    this.network = null;367  }368369  static async detectNetwork(api: ApiPromise): Promise<TUniqueNetworks> {370    const spec = (await api.query.system.lastRuntimeUpgrade()).toJSON() as any;371    if(['quartz', 'unique'].indexOf(spec.specName) > -1) return spec.specName;372    return 'opal';373  }374375  static async detectNetworkByWsEndpoint(wsEndpoint: string): Promise<TUniqueNetworks> {376    const api = new ApiPromise({provider: new WsProvider(wsEndpoint)});377    await api.isReady;378379    const network = await this.detectNetwork(api);380381    await api.disconnect();382383    return network;384  }385386  static async createConnection(wsEndpoint: string, listeners?: IApiListeners, network?: TUniqueNetworks | null): Promise<{ 387    api: ApiPromise; 388    network: TUniqueNetworks; 389  }> {390    if(typeof network === 'undefined' || network === null) network = 'opal';391    const supportedRPC = {392      opal: {393        unique: require('@unique-nft/opal-testnet-types/definitions').unique.rpc,394      },395      quartz: {396        unique: require('@unique-nft/quartz-mainnet-types/definitions').unique.rpc,397      },398      unique: {399        unique: require('@unique-nft/unique-mainnet-types/definitions').unique.rpc,400      },401    };402    if(!supportedRPC.hasOwnProperty(network)) network = await this.detectNetworkByWsEndpoint(wsEndpoint);403    const rpc = supportedRPC[network];404405    // TODO: investigate how to replace rpc in runtime406    // api._rpcCore.addUserInterfaces(rpc);407408    const api = new ApiPromise({provider: new WsProvider(wsEndpoint), rpc});409410    await api.isReadyOrError;411412    if (typeof listeners === 'undefined') listeners = {};413    for (const event of ['connected', 'disconnected', 'error', 'ready', 'decorated']) {414      if (!listeners.hasOwnProperty(event) || typeof listeners[event as TApiAllowedListeners] === 'undefined') continue;415      api.on(event as ApiInterfaceEvents, listeners[event as TApiAllowedListeners] as (...args: any[]) => any);416    }417418    return {api, network};419  }420421  getTransactionStatus(data: {events: {event: IChainEvent}[], status: any}) {422    const {events, status} = data;423    if (status.isReady) {424      return this.transactionStatus.NOT_READY;425    }426    if (status.isBroadcast) {427      return this.transactionStatus.NOT_READY;428    }429    if (status.isInBlock || status.isFinalized) {430      const errors = events.filter(e => e.event.data.method === 'ExtrinsicFailed');431      if (errors.length > 0) {432        return this.transactionStatus.FAIL;433      }434      if (events.filter(e => e.event.data.method === 'ExtrinsicSuccess').length > 0) {435        return this.transactionStatus.SUCCESS;436      }437    }438439    return this.transactionStatus.FAIL;440  }441442  signTransaction(sender: TSigner, transaction: any, label = 'transaction', options: any = null) {443    const sign = (callback: any) => {444      if(options !== null) return transaction.signAndSend(sender, options, callback);445      return transaction.signAndSend(sender, callback);446    };447    return new Promise(async (resolve, reject) => {448      try {449        const unsub = await sign((result: any) => {450          const status = this.getTransactionStatus(result);451452          if (status === this.transactionStatus.SUCCESS) {453            this.logger.log(`${label} successful`);454            unsub();455            resolve({result, status});456          } else if (status === this.transactionStatus.FAIL) {457            let moduleError = null;458459            if (result.hasOwnProperty('dispatchError')) {460              const dispatchError = result['dispatchError'];461462              if (dispatchError && dispatchError.isModule) {463                const modErr = dispatchError.asModule;464                const errorMeta = dispatchError.registry.findMetaError(modErr);465466                moduleError = `${errorMeta.section}.${errorMeta.name}`;467              }468              else {469                this.logger.log(result, this.logger.level.ERROR);470              }471            }472473            this.logger.log(`Something went wrong with ${label}. Status: ${status}`, this.logger.level.ERROR);474            unsub();475            reject({status, moduleError, result});476          }477        });478      } catch (e) {479        this.logger.log(e, this.logger.level.ERROR);480        reject(e);481      }482    });483  }484485  constructApiCall(apiCall: string, params: any[]) {486    if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);487    let call = this.api as any;488    for(const part of apiCall.slice(4).split('.')) {489      call = call[part];490    }491    return call(...params);492  }493494  async executeExtrinsic(sender: TSigner, extrinsic: string, params: any[], expectSuccess=false, failureMessage='expected success') {495    if(this.api === null) throw Error('API not initialized');496    if(!extrinsic.startsWith('api.tx.')) throw Error(`${extrinsic} is not transaction`);497498    const startTime = (new Date()).getTime();499    let result: ITransactionResult;500    let events = [];501    try {502      result = await this.signTransaction(sender, this.constructApiCall(extrinsic, params), extrinsic) as ITransactionResult;503      events = result.result.events.map((x: any) => x.toHuman());504    }505    catch(e) {506      if(!(e as object).hasOwnProperty('status')) throw e;507      result = e as ITransactionResult;508    }509510    const endTime = (new Date()).getTime();511512    const log = {513      executedAt: endTime,514      executionTime: endTime - startTime,515      type: this.chainLogType.EXTRINSIC,516      status: result.status,517      call: extrinsic,518      params,519    } as IUniqueHelperLog;520521    if(result.status !== this.transactionStatus.SUCCESS && result.moduleError) log.moduleError = result.moduleError;522    if(events.length > 0) log.events = events;523524    this.chainLog.push(log);525526    if(expectSuccess && result.status !== this.transactionStatus.SUCCESS) throw Error(failureMessage);527    return result;528  }529530  async callRpc(rpc: string, params?: any[]) {531    if(typeof params === 'undefined') params = [];532    if(this.api === null) throw Error('API not initialized');533    if(!rpc.startsWith('api.rpc.') && !rpc.startsWith('api.query.')) throw Error(`${rpc} is not RPC call`);534535    const startTime = (new Date()).getTime();536    let result;537    let error = null;538    const log = {539      type: this.chainLogType.RPC,540      call: rpc,541      params,542    } as IUniqueHelperLog;543544    try {545      result = await this.constructApiCall(rpc, params);546    }547    catch(e) {548      error = e;549    }550551    const endTime = (new Date()).getTime();552553    log.executedAt = endTime;554    log.status = (error === null ? this.transactionStatus.SUCCESS : this.transactionStatus.FAIL) as 'Fail' | 'Success';555    log.executionTime = endTime - startTime;556557    this.chainLog.push(log);558559    if(error !== null) throw error;560561    return result;562  }563564  getSignerAddress(signer: IKeyringPair | string): string {565    if(typeof signer === 'string') return signer;566    return signer.address;567  }568}569570571class HelperGroup {572  helper: UniqueHelper;573574  constructor(uniqueHelper: UniqueHelper) {575    this.helper = uniqueHelper;576  }577}578579580class CollectionGroup extends HelperGroup {581  /**582 * Get number of blocks when sponsored transaction is available.583 *584 * @param collectionId ID of collection585 * @param tokenId ID of token586 * @param addressObj address for which the sponsorship is checked587 * @example await getTokenNextSponsored(1, 2, {Substrate: '5DfhbVfww7ThF8q6f3...'});588 * @returns number of blocks or null if sponsorship hasn't been set589 */590  async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {591    return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();592  }593594  /**595   * Get the number of created collections.596   * 597   * @returns number of created collections598   */599  async getTotalCount(): Promise<number> {600    return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();601  }602603  /**604   * Get information about the collection with additional data, including the number of tokens it contains, its administrators, the normalized address of the collection's owner, and decoded name and description.605   * 606   * @param collectionId ID of collection607   * @example await getData(2)608   * @returns collection information object609   */610  async getData(collectionId: number): Promise<{611    id: number;612    name: string;613    description: string;614    tokensCount: number;615    admins: ICrossAccountId[];616    normalizedOwner: TSubstrateAccount;617    raw: any618  } | null> {619    const collection = await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId]);620    const humanCollection = collection.toHuman(), collectionData = {621      id: collectionId, name: null, description: null, tokensCount: 0, admins: [],622      raw: humanCollection,623    } as any, jsonCollection = collection.toJSON();624    if (humanCollection === null) return null;625    collectionData.raw.limits = jsonCollection.limits;626    collectionData.raw.permissions = jsonCollection.permissions;627    collectionData.normalizedOwner = this.helper.address.normalizeSubstrate(collectionData.raw.owner);628    for (const key of ['name', 'description']) {629      collectionData[key] = this.helper.util.vec2str(humanCollection[key]);630    }631632    collectionData.tokensCount = (['RFT', 'NFT'].includes(humanCollection.mode)) ? await this.helper[humanCollection.mode.toLocaleLowerCase() as 'nft' | 'rft'].getLastTokenId(collectionId) : 0;633    collectionData.admins = await this.getAdmins(collectionId);634635    return collectionData;636  }637638  /**639   * Get the normalized addresses of the collection's administrators.640   * 641   * @param collectionId ID of collection642   * @example await getAdmins(1)643   * @returns array of administrators644   */645  async getAdmins(collectionId: number): Promise<ICrossAccountId[]> {646    const normalized = [];647    for(const admin of (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman()) {648      if(admin.Substrate) normalized.push({Substrate: this.helper.address.normalizeSubstrate(admin.Substrate)});649      else normalized.push(admin);650    }651    return normalized;652  }653654  /**655   * Get the effective limits of the collection instead of null for default values656   * 657   * @param collectionId ID of collection658   * @example await getEffectiveLimits(2)659   * @returns object of collection limits660   */661  async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {662    return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();663  }664665  /**666   * Burns the collection if the signer has sufficient permissions and collection is empty.667   * 668   * @param signer keyring of signer669   * @param collectionId ID of collection670   * @param label extra label for log671   * @example await helper.collection.burn(aliceKeyring, 3);672   * @returns ```true``` if extrinsic success, otherwise ```false```673   */674  async burn(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {675    if(typeof label === 'undefined') label = `collection #${collectionId}`;676    const result = await this.helper.executeExtrinsic(677      signer,678      'api.tx.unique.destroyCollection', [collectionId],679      true, `Unable to burn collection for ${label}`,680    );681682    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed', label);683  }684685  /**686   * Sets the sponsor for the collection (Requires the Substrate address).687   * 688   * @param signer keyring of signer689   * @param collectionId ID of collection690   * @param sponsorAddress Sponsor substrate address691   * @param label extra label for log692   * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")693   * @returns ```true``` if extrinsic success, otherwise ```false```694   */695  async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount, label?: string): Promise<boolean> {696    if(typeof label === 'undefined') label = `collection #${collectionId}`;697    const result = await this.helper.executeExtrinsic(698      signer,699      'api.tx.unique.setCollectionSponsor', [collectionId, sponsorAddress],700      true, `Unable to set collection sponsor for ${label}`,701    );702703    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorSet', label);704  }705706  /**707   * Confirms consent to sponsor the collection on behalf of the signer.708   * 709   * @param signer keyring of signer710   * @param collectionId ID of collection711   * @param label extra label for log712   * @example confirmSponsorship(aliceKeyring, 10)713   * @returns ```true``` if extrinsic success, otherwise ```false```714   */715  async confirmSponsorship(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {716    if(typeof label === 'undefined') label = `collection #${collectionId}`;717    const result = await this.helper.executeExtrinsic(718      signer,719      'api.tx.unique.confirmSponsorship', [collectionId],720      true, `Unable to confirm collection sponsorship for ${label}`,721    );722723    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'SponsorshipConfirmed', label);724  }725726  /**727   * Sets the limits of the collection. At least one limit must be specified for a correct call.728   * 729   * @param signer keyring of signer730   * @param collectionId ID of collection731   * @param limits collection limits object732   * @param label extra label for log733   * @example734   * await setLimits(735   *   aliceKeyring,736   *   10,737   *   {738   *     sponsorTransferTimeout: 0,739   *     ownerCanDestroy: false740   *   }741   * )742   * @returns ```true``` if extrinsic success, otherwise ```false```743   */744  async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits, label?: string): Promise<boolean> {745    if(typeof label === 'undefined') label = `collection #${collectionId}`;746    const result = await this.helper.executeExtrinsic(747      signer,748      'api.tx.unique.setCollectionLimits', [collectionId, limits],749      true, `Unable to set collection limits for ${label}`,750    );751752    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionLimitSet', label);753  }754755  /**756   * Changes the owner of the collection to the new Substrate address.757   * 758   * @param signer keyring of signer759   * @param collectionId ID of collection760   * @param ownerAddress substrate address of new owner761   * @param label extra label for log762   * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")763   * @returns ```true``` if extrinsic success, otherwise ```false```764   */765  async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount, label?: string): Promise<boolean> {766    if(typeof label === 'undefined') label = `collection #${collectionId}`;767    const result = await this.helper.executeExtrinsic(768      signer,769      'api.tx.unique.changeCollectionOwner', [collectionId, ownerAddress],770      true, `Unable to change collection owner for ${label}`,771    );772773    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionOwnedChanged', label);774  }775776  /**777   * Adds a collection administrator. 778   * 779   * @param signer keyring of signer780   * @param collectionId ID of collection781   * @param adminAddressObj Administrator address (substrate or ethereum)782   * @param label extra label for log783   * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})784   * @returns ```true``` if extrinsic success, otherwise ```false```785   */786  async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {787    if(typeof label === 'undefined') label = `collection #${collectionId}`;788    const result = await this.helper.executeExtrinsic(789      signer,790      'api.tx.unique.addCollectionAdmin', [collectionId, adminAddressObj],791      true, `Unable to add collection admin for ${label}`,792    );793794    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminAdded', label);795  }796797  /**798   * Removes a collection administrator.799   * 800   * @param signer keyring of signer801   * @param collectionId ID of collection802   * @param adminAddressObj Administrator address (substrate or ethereum)803   * @param label extra label for log804   * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})805   * @returns ```true``` if extrinsic success, otherwise ```false```806   */807  async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {808    if(typeof label === 'undefined') label = `collection #${collectionId}`;809    const result = await this.helper.executeExtrinsic(810      signer,811      'api.tx.unique.removeCollectionAdmin', [collectionId, adminAddressObj],812      true, `Unable to remove collection admin for ${label}`,813    );814815    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminRemoved', label);816  }817818  /**819   * Sets onchain permissions for selected collection.820   * 821   * @param signer keyring of signer822   * @param collectionId ID of collection823   * @param permissions collection permissions object824   * @param label extra label for log825   * @example setPermissions(aliceKeyring, 10, {access:'AllowList', mintMode: true, nesting: {collectionAdmin: true, tokenOwner: true}});826   * @returns ```true``` if extrinsic success, otherwise ```false```827   */828  async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions, label?: string): Promise<boolean> {829    if(typeof label === 'undefined') label = `collection #${collectionId}`;830    const result = await this.helper.executeExtrinsic(831      signer,832      'api.tx.unique.setCollectionPermissions', [collectionId, permissions],833      true, `Unable to set collection permissions for ${label}`,834    );835836    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionPermissionSet', label);837  }838839  /**840   * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.841   * 842   * @param signer keyring of signer843   * @param collectionId ID of collection844   * @param permissions nesting permissions object845   * @param label extra label for log846   * @example enableNesting(aliceKeyring, 10, {collectionAdmin: true, tokenOwner: true});847   * @returns ```true``` if extrinsic success, otherwise ```false```848   */849  async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions, label?: string): Promise<boolean> {850    return await this.setPermissions(signer, collectionId, {nesting: permissions}, label);851  }852853  /**854   * Disables nesting for selected collection.855   * 856   * @param signer keyring of signer857   * @param collectionId ID of collection858   * @param label extra label for log859   * @example disableNesting(aliceKeyring, 10);860   * @returns ```true``` if extrinsic success, otherwise ```false```861   */862  async disableNesting(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {863    return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}}, label);864  }865866  /**867   * Sets onchain properties to the collection.868   * 869   * @param signer keyring of signer870   * @param collectionId ID of collection871   * @param properties array of property objects872   * @param label extra label for log873   * @example setProperties(aliceKeyring, 10, [{key: "gender", value: "male"}]);874   * @returns ```true``` if extrinsic success, otherwise ```false```875   */876  async setProperties(signer: TSigner, collectionId: number, properties: IProperty[], label?: string): Promise<boolean> {877    if(typeof label === 'undefined') label = `collection #${collectionId}`;878    const result = await this.helper.executeExtrinsic(879      signer,880      'api.tx.unique.setCollectionProperties', [collectionId, properties],881      true, `Unable to set collection properties for ${label}`,882    );883884    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet', label);885  }886887  /**888   * Deletes onchain properties from the collection.889   * 890   * @param signer keyring of signer891   * @param collectionId ID of collection892   * @param propertyKeys array of property keys to delete893   * @param label894   * @example deleteProperties(aliceKeyring, 10, ["gender", "age"]);895   * @returns ```true``` if extrinsic success, otherwise ```false```896   */897  async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[], label?: string): Promise<boolean> {898    if(typeof label === 'undefined') label = `collection #${collectionId}`;899    const result = await this.helper.executeExtrinsic(900      signer,901      'api.tx.unique.deleteCollectionProperties', [collectionId, propertyKeys],902      true, `Unable to delete collection properties for ${label}`,903    );904905    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted', label);906  }907908  /**909   * Changes the owner of the token.910   * 911   * @param signer keyring of signer912   * @param collectionId ID of collection913   * @param tokenId ID of token914   * @param addressObj address of a new owner915   * @param amount amount of tokens to be transfered. For NFT must be set to 1n916   * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})917   * @returns true if the token success, otherwise false918   */919  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {920    const result = await this.helper.executeExtrinsic(921      signer,922      'api.tx.unique.transfer', [addressObj, collectionId, tokenId, amount],923      true, `Unable to transfer token #${tokenId} from collection #${collectionId}`,924    );925926    return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);927  }928929  /**930   * 931   * Change ownership of a token(s) on behalf of the owner. 932   * 933   * @param signer keyring of signer934   * @param collectionId ID of collection935   * @param tokenId ID of token936   * @param fromAddressObj address on behalf of which the token will be sent937   * @param toAddressObj new token owner938   * @param amount amount of tokens to be transfered. For NFT must be set to 1n939   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})940   * @returns true if the token success, otherwise false941   */942  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {943    const result = await this.helper.executeExtrinsic(944      signer,945      'api.tx.unique.transferFrom', [fromAddressObj, toAddressObj, collectionId, tokenId, amount],946      true, `Unable to transfer token #${tokenId} from collection #${collectionId}`,947    );948    return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);949  }950951  /**952   * 953   * Destroys a concrete instance of NFT/RFT or burns a specified amount of fungible tokens.954   * 955   * @param signer keyring of signer956   * @param collectionId ID of collection957   * @param tokenId ID of token958   * @param label 959   * @param amount amount of tokens to be burned. For NFT must be set to 1n960   * @example burnToken(aliceKeyring, 10, 5);961   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```962   */963  async burnToken(signer: TSigner, collectionId: number, tokenId: number, label?: string, amount=1n): Promise<{964    success: boolean,965    token: number | null966  }> {967    if(typeof label === 'undefined') label = `collection #${collectionId}`;968    const burnResult = await this.helper.executeExtrinsic(969      signer,970      'api.tx.unique.burnItem', [collectionId, tokenId, amount],971      true, `Unable to burn token for ${label}`,972    );973    const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult, label);974    if (burnedTokens.tokens.length > 1) throw Error('Burned multiple tokens');975    return {success: burnedTokens.success, token: burnedTokens.tokens.length > 0 ? burnedTokens.tokens[0] : null};976  }977978  /**979   * Destroys a concrete instance of NFT on behalf of the owner980   * 981   * @param signer keyring of signer982   * @param collectionId ID of collection983   * @param fromAddressObj address on behalf of which the token will be burnt984   * @param tokenId ID of token985   * @param label 986   * @param amount amount of tokens to be burned. For NFT must be set to 1n987   * @example burnTokenFrom(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."}, 5, {Ethereum: "0x9F0583DbB85..."})988   * @returns ```true``` if extrinsic success, otherwise ```false```989   */990  async burnTokenFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, tokenId: number, label?: string, amount=1n): Promise<boolean> {991    if(typeof label === 'undefined') label = `collection #${collectionId}`;992    const burnResult = await this.helper.executeExtrinsic(993      signer,994      'api.tx.unique.burnFrom', [collectionId, fromAddressObj, tokenId, amount],995      true, `Unable to burn token from for ${label}`,996    );997    const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult, label);998    return burnedTokens.success && burnedTokens.tokens.length > 0;999  }10001001  /**1002   * Set, change, or remove approved address to transfer the ownership of the NFT.1003   * 1004   * @param signer keyring of signer1005   * @param collectionId ID of collection1006   * @param tokenId ID of token1007   * @param toAddressObj 1008   * @param label 1009   * @param amount amount of token to be approved. For NFT must be set to 1n1010   * @returns ```true``` if extrinsic success, otherwise ```false```1011   */1012  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=1n) {1013    if(typeof label === 'undefined') label = `collection #${collectionId}`;1014    const approveResult = await this.helper.executeExtrinsic(1015      signer, 1016      'api.tx.unique.approve', [toAddressObj, collectionId, tokenId, amount],1017      true, `Unable to approve token for ${label}`,1018    );10191020    return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved', label);1021  }10221023  /**1024   * Get the amount of token pieces approved to transfer1025   * @param collectionId ID of collection1026   * @param tokenId ID of token1027   * @param toAccountObj 1028   * @param fromAccountObj1029   * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})1030   * @returns number of approved to transfer pieces1031   */1032  async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {1033    return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();1034  }10351036  /**1037   * Get the last created token id1038   * @param collectionId ID of collection1039   * @example getLastTokenId(10);1040   * @returns id of the last created token1041   */1042  async getLastTokenId(collectionId: number): Promise<number> {1043    return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();1044  }10451046  /**1047   * Check if token exists1048   * @param collectionId ID of collection1049   * @param tokenId ID of token1050   * @example isTokenExists(10, 20);1051   * @returns true if the token exists, otherwise false1052   */1053  async isTokenExists(collectionId: number, tokenId: number): Promise<boolean> {1054    return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();1055  }1056}10571058class NFTnRFT extends CollectionGroup {1059  /**1060   * Get tokens owned by account1061   * 1062   * @param collectionId ID of collection1063   * @param addressObj tokens owner1064   * @example getTokensByAddress(10, {Substrate: "5DyN4Y92vZCjv38fg..."})1065   * @returns array of token ids owned by account1066   */1067  async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {1068    return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();1069  }10701071  /**1072   * Get token data1073   * @param collectionId ID of collection1074   * @param tokenId ID of token1075   * @param blockHashAt 1076   * @param propertyKeys1077   * @example getToken(10, 5);1078   * @returns human readable token data 1079   */1080  async getToken(collectionId: number, tokenId: number, blockHashAt?: string, propertyKeys?: string[]): Promise<{1081    properties: IProperty[];1082    owner: ICrossAccountId;1083    normalizedOwner: ICrossAccountId;1084  }| null> {1085    let tokenData;1086    if(typeof blockHashAt === 'undefined') {1087      tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId]);1088    }1089    else {1090      if(typeof propertyKeys === 'undefined') {1091        const collection = (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1092        if(!collection) return null;1093        propertyKeys = collection.tokenPropertyPermissions.map((x: ITokenPropertyPermission) => x.key);1094      }1095      tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId, propertyKeys, blockHashAt]);1096    }1097    tokenData = tokenData.toHuman();1098    if (tokenData === null || tokenData.owner === null) return null;1099    const owner = {} as any;1100    for (const key of Object.keys(tokenData.owner)) {1101      owner[key.toLocaleLowerCase()] = key.toLocaleLowerCase() === 'substrate' ? this.helper.address.normalizeSubstrate(tokenData.owner[key]) : tokenData.owner[key];1102    }1103    tokenData.normalizedOwner = crossAccountIdFromLower(owner);1104    return tokenData;1105  }11061107  /**1108   * Set permissions to change token properties1109   * @param signer keyring of signer1110   * @param collectionId ID of collection1111   * @param permissions permissions to change a property by the collection owner or admin1112   * @param label 1113   * @example setTokenPropertyPermissions(1114   *   aliceKeyring, 10, [{key: "gender", permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}]1115   * )1116   * @returns true if extrinsic success otherwise false1117   */1118  async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[], label?: string): Promise<boolean> {1119    if(typeof label === 'undefined') label = `collection #${collectionId}`;1120    const result = await this.helper.executeExtrinsic(1121      signer,1122      'api.tx.unique.setTokenPropertyPermissions', [collectionId, permissions],1123      true, `Unable to set token property permissions for ${label}`,1124    );11251126    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet', label);1127  }11281129  /**1130   * Set token properties1131   * @param signer keyring of signer1132   * @param collectionId ID of collection1133   * @param tokenId ID of token1134   * @param properties 1135   * @param label 1136   * @example setTokenProperties(aliceKeyring, 10, 5, [{key: "gender", value: "female"}, {key: "age", value: "23"}])1137   * @returns ```true``` if extrinsic success, otherwise ```false```1138   */1139  async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[], label?: string): Promise<boolean> {1140    if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1141    const result = await this.helper.executeExtrinsic(1142      signer,1143      'api.tx.unique.setTokenProperties', [collectionId, tokenId, properties],1144      true, `Unable to set token properties for ${label}`,1145    );11461147    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet', label);1148  }11491150  /**1151   * Delete the provided properties of a token1152   * @param signer keyring of signer1153   * @param collectionId ID of collection1154   * @param tokenId ID of token1155   * @param propertyKeys property keys to be deleted 1156   * @param label 1157   * @example deleteTokenProperties(aliceKeyring, 10, 5, ["gender", "age"])1158   * @returns ```true``` if extrinsic success, otherwise ```false```1159   */1160  async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[], label?: string): Promise<boolean> {1161    if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1162    const result = await this.helper.executeExtrinsic(1163      signer,1164      'api.tx.unique.deleteTokenProperties', [collectionId, tokenId, propertyKeys],1165      true, `Unable to delete token properties for ${label}`,1166    );11671168    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted', label);1169  }11701171  /**1172   * Mint new collection1173   * @param signer keyring of signer1174   * @param collectionOptions basic collection options and properties 1175   * @param mode NFT or RFT type of a collection1176   * @param errorLabel 1177   * @example mintCollection(aliceKeyring, {name: 'New', description: "New collection", tokenPrefix: "NEW"}, "NFT")1178   * @returns object of the created collection1179   */1180  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT', errorLabel = 'Unable to mint collection'): Promise<UniqueCollectionBase> {1181    collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1182    collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1183    for (const key of ['name', 'description', 'tokenPrefix']) {1184      if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1185    }1186    const creationResult = await this.helper.executeExtrinsic(1187      signer,1188      'api.tx.unique.createCollectionEx', [collectionOptions],1189      true, errorLabel,1190    );1191    return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));1192  }11931194  getCollectionObject(collectionId: number): any {1195    return null;1196  }11971198  getTokenObject(collectionId: number, tokenId: number): any {1199    return null;1200  }1201}120212031204class NFTGroup extends NFTnRFT {1205  /**1206   * Get collection object1207   * @param collectionId ID of collection1208   * @example getCollectionObject(2);1209   * @returns instance of UniqueNFTCollection1210   */1211  getCollectionObject(collectionId: number): UniqueNFTCollection {1212    return new UniqueNFTCollection(collectionId, this.helper);1213  }12141215  /**1216   * Get token object1217   * @param collectionId ID of collection1218   * @param tokenId ID of token1219   * @example getTokenObject(10, 5);1220   * @returns instance of UniqueNFTToken1221   */1222  getTokenObject(collectionId: number, tokenId: number): UniqueNFTToken {1223    return new UniqueNFTToken(tokenId, this.getCollectionObject(collectionId));1224  }12251226  /**1227   * Get token's owner1228   * @param collectionId ID of collection1229   * @param tokenId ID of token1230   * @param blockHashAt 1231   * @example getTokenOwner(10, 5);1232   * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1233   */1234  async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId> {1235    let owner;1236    if (typeof blockHashAt === 'undefined') {1237      owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1238    } else {1239      owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1240    }1241    return crossAccountIdFromLower(owner.toJSON());1242  }12431244  /**1245   * Is token approved to transfer1246   * @param collectionId ID of collection1247   * @param tokenId ID of token1248   * @param toAccountObj address to be approved1249   * @returns ```true``` if extrinsic success, otherwise ```false```1250   */1251  async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {1252    return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;1253  }12541255  /**1256   * Changes the owner of the token.1257   * 1258   * @param signer keyring of signer1259   * @param collectionId ID of collection1260   * @param tokenId ID of token1261   * @param addressObj address of a new owner1262   * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1263   * @returns ```true``` if extrinsic success, otherwise ```false```1264   */1265  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {1266    return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);1267  }12681269  /**1270   * 1271   * Change ownership of a NFT on behalf of the owner. 1272   * 1273   * @param signer keyring of signer1274   * @param collectionId ID of collection1275   * @param tokenId ID of token1276   * @param fromAddressObj address on behalf of which the token will be sent1277   * @param toAddressObj new token owner1278   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})1279   * @returns ```true``` if extrinsic success, otherwise ```false```1280   */1281  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {1282    return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1283  }12841285  /**1286   * Recursively find the address that owns the token1287   * @param collectionId ID of collection1288   * @param tokenId ID of token1289   * @param blockHashAt 1290   * @example getTokenTopmostOwner(10, 5);1291   * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1292   */1293  async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId | null> {1294    let owner;1295    if (typeof blockHashAt === 'undefined') {1296      owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1297    } else {1298      owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1299    }13001301    if (owner === null) return null;13021303    owner = owner.toHuman();13041305    return owner.Substrate ? {Substrate: this.helper.address.normalizeSubstrate(owner.Substrate)} : owner;1306  }13071308  /**1309   * Get tokens nested in the provided token1310   * @param collectionId ID of collection1311   * @param tokenId ID of token1312   * @param blockHashAt 1313   * @example getTokenChildren(10, 5);1314   * @returns tokens whose depth of nesting is <= 5 1315   */1316  async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {1317    let children;1318    if(typeof blockHashAt === 'undefined') {1319      children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId]);1320    } else {1321      children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId, blockHashAt]);1322    }13231324    return children.toJSON().map((x: any) => {1325      return {collectionId: x.collection, tokenId: x.token};1326    });1327  }13281329  /**1330   * Nest one token into another1331   * @param signer keyring of signer1332   * @param tokenObj token to be nested1333   * @param rootTokenObj token to be parent1334   * @param label 1335   * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1336   * @returns ```true``` if extrinsic success, otherwise ```false```1337   */1338  async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, label='nest token'): Promise<boolean> {1339    const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1340    const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1341    if(!result) {1342      throw Error(`Unable to nest token for ${label}`);1343    }1344    return result;1345  }13461347  /**1348   * Remove token from nested state1349   * @param signer keyring of signer1350   * @param tokenObj token to unnest1351   * @param rootTokenObj parent of a token1352   * @param toAddressObj address of a new token owner 1353   * @param label 1354   * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1355   * @returns ```true``` if extrinsic success, otherwise ```false```1356   */1357  async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId, label='unnest token'): Promise<boolean> {1358    const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1359    const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1360    if(!result) {1361      throw Error(`Unable to unnest token for ${label}`);1362    }1363    return result;1364  }13651366  /**1367   * Mint new collection1368   * @param signer keyring of signer1369   * @param collectionOptions Collection options1370   * @param label 1371   * @example 1372   * mintCollection(aliceKeyring, {1373   *   name: 'New',1374   *   description: 'New collection',1375   *   tokenPrefix: 'NEW',1376   * })1377   * @returns object of the created collection1378   */1379  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueNFTCollection> {1380    return await super.mintCollection(signer, collectionOptions, 'NFT', `Unable to mint NFT collection for ${label}`) as UniqueNFTCollection;1381  }13821383  /**1384   * Mint new token1385   * @param signer keyring of signer1386   * @param data token data1387   * @param label 1388   * @returns created token object1389   */1390  async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }, label?: string): Promise<UniqueNFTToken> {1391    if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1392    const creationResult = await this.helper.executeExtrinsic(1393      signer,1394      'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1395        nft: {1396          properties: data.properties,1397        },1398      }],1399      true, `Unable to mint NFT token for ${label}`,1400    );1401    const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult, label);1402    if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1403    if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1404    return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1405  }14061407  /**1408   * Mint multiple NFT tokens1409   * @param signer keyring of signer1410   * @param collectionId ID of collection1411   * @param tokens array of tokens with owner and properties1412   * @param label 1413   * @example 1414   * mintMultipleTokens(aliceKeyring, 10, [{1415   *     owner: {Substrate: "5DyN4Y92vZCjv38fg..."},1416   *     properties: [{key: "gender", value: "male"},{key: "age", value: "45"}],1417   *   },{1418   *     owner: {Ethereum: "0x9F0583DbB855d..."},1419   *     properties: [{key: "gender", value: "female"},{key: "age", value: "22"}],1420   * }]);1421   * @returns ```true``` if extrinsic success, otherwise ```false```1422   */1423  async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1424    if(typeof label === 'undefined') label = `collection #${collectionId}`;1425    const creationResult = await this.helper.executeExtrinsic(1426      signer,1427      'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}],1428      true, `Unable to mint NFT tokens for ${label}`,1429    );1430    const collection = this.getCollectionObject(collectionId);1431    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1432  }14331434  /**1435   * Mint multiple NFT tokens with one owner1436   * @param signer keyring of signer1437   * @param collectionId ID of collection1438   * @param owner tokens owner1439   * @param tokens array of tokens with owner and properties1440   * @param label 1441   * @example1442   * mintMultipleTokensWithOneOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...", [{1443   *   properties: [{1444   *   key: "gender",1445   *   value: "female",1446   *  },{1447   *   key: "age",1448   *   value: "33",1449   *  }],1450   * }]);1451   * @returns array of newly created tokens1452   */1453  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1454    if(typeof label === 'undefined') label = `collection #${collectionId}`;1455    const rawTokens = [];1456    for (const token of tokens) {1457      const raw = {NFT: {properties: token.properties}};1458      rawTokens.push(raw);1459    }1460    const creationResult = await this.helper.executeExtrinsic(1461      signer,1462      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1463      true, `Unable to mint NFT tokens for ${label}`,1464    );1465    const collection = this.getCollectionObject(collectionId);1466    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1467  }14681469  /**1470   * Destroys a concrete instance of NFT.1471   * @param signer keyring of signer1472   * @param collectionId ID of collection1473   * @param tokenId ID of token1474   * @param label 1475   * @example burnToken(aliceKeyring, 10, 5);1476   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```1477   */1478  async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string): Promise<{ success: boolean; token: number | null; }> {1479    return await super.burnToken(signer, collectionId, tokenId, label, 1n);1480  }14811482  /**1483   * Set, change, or remove approved address to transfer the ownership of the NFT.1484   * 1485   * @param signer keyring of signer1486   * @param collectionId ID of collection1487   * @param tokenId ID of token1488   * @param toAddressObj address to approve1489   * @param label 1490   * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1491   * @returns ```true``` if extrinsic success, otherwise ```false```1492   */1493  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {1494    return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, 1n);1495  }1496}149714981499class RFTGroup extends NFTnRFT {1500  /**1501   * Get collection object1502   * @param collectionId ID of collection1503   * @example getCollectionObject(2);1504   * @returns instance of UniqueRFTCollection1505   */1506  getCollectionObject(collectionId: number): UniqueRFTCollection {1507    return new UniqueRFTCollection(collectionId, this.helper);1508  }15091510  /**1511   * Get token object1512   * @param collectionId ID of collection1513   * @param tokenId ID of token1514   * @example getTokenObject(10, 5);1515   * @returns instance of UniqueNFTToken1516   */1517  getTokenObject(collectionId: number, tokenId: number): UniqueRFTToken {1518    return new UniqueRFTToken(tokenId, this.getCollectionObject(collectionId));1519  }15201521  /**1522   * Get top 10 token owners with the largest number of pieces 1523   * @param collectionId ID of collection1524   * @param tokenId ID of token1525   * @example getTokenTop10Owners(10, 5);1526   * @returns array of top 10 owners1527   */1528  async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<ICrossAccountId[]> {1529    return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(crossAccountIdFromLower);1530  }15311532  /**1533   * Get number of pieces owned by address1534   * @param collectionId ID of collection1535   * @param tokenId ID of token1536   * @param addressObj address token owner1537   * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});1538   * @returns number of pieces ownerd by address1539   */1540  async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {1541    return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();1542  }15431544  /**1545   * Transfer pieces of token to another address1546   * @param signer keyring of signer1547   * @param collectionId ID of collection1548   * @param tokenId ID of token1549   * @param addressObj address of a new owner1550   * @param amount number of pieces to be transfered1551   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)1552   * @returns ```true``` if extrinsic success, otherwise ```false```1553   */1554  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=100n): Promise<boolean> {1555    return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);1556  }15571558  /**1559   * Change ownership of some pieces of RFT on behalf of the owner. 1560   * @param signer keyring of signer1561   * @param collectionId ID of collection1562   * @param tokenId ID of token1563   * @param fromAddressObj address on behalf of which the token will be sent1564   * @param toAddressObj new token owner1565   * @param amount number of pieces to be transfered1566   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)1567   * @returns ```true``` if extrinsic success, otherwise ```false```1568   */1569  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n): Promise<boolean> {1570    return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1571  }15721573  /**1574   * Mint new collection1575   * @param signer keyring of signer1576   * @param collectionOptions Collection options1577   * @param label 1578   * @example1579   * mintCollection(aliceKeyring, {1580   *   name: 'New',1581   *   description: 'New collection',1582   *   tokenPrefix: 'NEW',1583   * })1584   * @returns object of the created collection1585   */1586  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueRFTCollection> {1587    return await super.mintCollection(signer, collectionOptions, 'RFT', `Unable to mint RFT collection for ${label}`) as UniqueRFTCollection;1588  }15891590  /**1591   * Mint new token1592   * @param signer keyring of signer1593   * @param data token data1594   * @param label 1595   * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});1596   * @returns created token object1597   */1598  async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }, label?: string): Promise<UniqueRFTToken> {1599    if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1600    const creationResult = await this.helper.executeExtrinsic(1601      signer,1602      'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1603        refungible: {1604          pieces: data.pieces,1605          properties: data.properties,1606        },1607      }],1608      true, `Unable to mint RFT token for ${label}`,1609    );1610    const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult, label);1611    if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1612    if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1613    return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1614  }16151616  async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {1617    throw Error('Not implemented');1618    if(typeof label === 'undefined') label = `collection #${collectionId}`;1619    const creationResult = await this.helper.executeExtrinsic(1620      signer,1621      'api.tx.unique.createMultipleItemsEx', [collectionId, {RefungibleMultipleOwners: tokens}],1622      true, `Unable to mint RFT tokens for ${label}`,1623    );1624    const collection = this.getCollectionObject(collectionId);1625    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1626  }16271628  /**1629   * Mint multiple RFT tokens with one owner1630   * @param signer keyring of signer1631   * @param collectionId ID of collection1632   * @param owner tokens owner1633   * @param tokens array of tokens with properties and pieces1634   * @param label 1635   * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1636   * @returns array of newly created RFT tokens1637   */1638  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {1639    if(typeof label === 'undefined') label = `collection #${collectionId}`;1640    const rawTokens = [];1641    for (const token of tokens) {1642      const raw = {ReFungible: {pieces: token.pieces, properties: token.properties}};1643      rawTokens.push(raw);1644    }1645    const creationResult = await this.helper.executeExtrinsic(1646      signer,1647      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1648      true, `Unable to mint RFT tokens for ${label}`,1649    );1650    const collection = this.getCollectionObject(collectionId);1651    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1652  }16531654  /**1655   * Destroys a concrete instance of RFT.1656   * @param signer keyring of signer1657   * @param collectionId ID of collection1658   * @param tokenId ID of token1659   * @param label 1660   * @param amount number of pieces to be burnt1661   * @example burnToken(aliceKeyring, 10, 5);1662   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```1663   */1664  async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string, amount=100n): Promise<{ success: boolean; token: number | null; }> {1665    return await super.burnToken(signer, collectionId, tokenId, label, amount);1666  }16671668  /**1669   * Set, change, or remove approved address to transfer the ownership of the RFT.1670   * 1671   * @param signer keyring of signer1672   * @param collectionId ID of collection1673   * @param tokenId ID of token1674   * @param toAddressObj address to approve1675   * @param label 1676   * @param amount number of pieces to be approved1677   * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);1678   * @returns true if the token success, otherwise false1679   */1680  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=100n) {1681    return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, amount);1682  }16831684  /**1685   * Get total number of pieces1686   * @param collectionId ID of collection1687   * @param tokenId ID of token1688   * @example getTokenTotalPieces(10, 5);1689   * @returns number of pieces1690   */1691  async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {1692    return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();1693  }16941695  /**1696   * Change number of token pieces. Signer must be the owner of all token pieces.1697   * @param signer keyring of signer1698   * @param collectionId ID of collection1699   * @param tokenId ID of token1700   * @param amount new number of pieces1701   * @param label 1702   * @example repartitionToken(aliceKeyring, 10, 5, 12345n);1703   * @returns true if the repartion was success, otherwise false1704   */1705  async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint, label?: string): Promise<boolean> {1706    if(typeof label === 'undefined') label = `collection #${collectionId}`;1707    const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);1708    const repartitionResult = await this.helper.executeExtrinsic(1709      signer,1710      'api.tx.unique.repartition', [collectionId, tokenId, amount],1711      true, `Unable to repartition RFT token for ${label}`,1712    );1713    if(currentAmount < amount) return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemCreated', label);1714    return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemDestroyed', label);1715  }1716}171717181719class FTGroup extends CollectionGroup {1720  /**1721   * Get collection object1722   * @param collectionId ID of collection1723   * @example getCollectionObject(2);1724   * @returns instance of UniqueFTCollection1725   */1726  getCollectionObject(collectionId: number): UniqueFTCollection {1727    return new UniqueFTCollection(collectionId, this.helper);1728  }17291730  /**1731   * Mint new fungible collection1732   * @param signer keyring of signer1733   * @param collectionOptions Collection options1734   * @param decimalPoints number of token decimals 1735   * @param errorLabel 1736   * @example1737   * mintCollection(aliceKeyring, {1738   *   name: 'New',1739   *   description: 'New collection',1740   *   tokenPrefix: 'NEW',1741   * }, 18)1742   * @returns newly created fungible collection1743   */1744  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, decimalPoints = 0, errorLabel = 'Unable to mint collection'): Promise<UniqueFTCollection> {1745    collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1746    if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');1747    collectionOptions.mode = {fungible: decimalPoints};1748    for (const key of ['name', 'description', 'tokenPrefix']) {1749      if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1750    }1751    const creationResult = await this.helper.executeExtrinsic(1752      signer,1753      'api.tx.unique.createCollectionEx', [collectionOptions],1754      true, errorLabel,1755    );1756    return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));1757  }17581759  /**1760   * Mint tokens1761   * @param signer keyring of signer1762   * @param collectionId ID of collection1763   * @param owner address owner of new tokens1764   * @param amount amount of tokens to be meanted1765   * @param label 1766   * @example mintTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq"}, 1000n);1767   * @returns ```true``` if extrinsic success, otherwise ```false``` 1768   */1769  async mintTokens(signer: TSigner, collectionId: number, owner: ICrossAccountId | string, amount: bigint, label?: string): Promise<boolean> {1770    if(typeof label === 'undefined') label = `collection #${collectionId}`;1771    const creationResult = await this.helper.executeExtrinsic(1772      signer,1773      'api.tx.unique.createItem', [collectionId, (typeof owner === 'string') ? {Substrate: owner} : owner, {1774        fungible: {1775          value: amount,1776        },1777      }],1778      true, `Unable to mint fungible tokens for ${label}`,1779    );1780    return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1781  }17821783  /**1784   * Mint multiple Fungible tokens with one owner1785   * @param signer keyring of signer1786   * @param collectionId ID of collection1787   * @param owner tokens owner1788   * @param tokens array of tokens with properties and pieces1789   * @param label 1790   * @returns ```true``` if extrinsic success, otherwise ```false``` 1791   */1792  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string): Promise<boolean> {1793    if(typeof label === 'undefined') label = `collection #${collectionId}`;1794    const rawTokens = [];1795    for (const token of tokens) {1796      const raw = {Fungible: {Value: token.value}};1797      rawTokens.push(raw);1798    }1799    const creationResult = await this.helper.executeExtrinsic(1800      signer,1801      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1802      true, `Unable to mint RFT tokens for ${label}`,1803    );1804    return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1805  }18061807  /**1808   * Get the top 10 owners with the largest balance for the Fungible collection 1809   * @param collectionId ID of collection1810   * @example getTop10Owners(10);1811   * @returns array of ```ICrossAccountId```1812   */1813  async getTop10Owners(collectionId: number): Promise<ICrossAccountId[]> {1814    return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(crossAccountIdFromLower);1815  }18161817  /**1818   * Get account balance1819   * @param collectionId ID of collection1820   * @param addressObj address of owner1821   * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})1822   * @returns amount of fungible tokens owned by address1823   */1824  async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {1825    return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();1826  }18271828  /**1829   * Transfer tokens to address1830   * @param signer keyring of signer1831   * @param collectionId ID of collection1832   * @param toAddressObj address recepient1833   * @param amount amount of tokens to be sent1834   * @example transfer(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);1835   * @returns ```true``` if extrinsic success, otherwise ```false``` 1836   */1837  async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount: bigint) {1838    return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);1839  }18401841  /**1842   * Transfer some tokens on behalf of the owner.1843   * @param signer keyring of signer1844   * @param collectionId ID of collection1845   * @param fromAddressObj address on behalf of which tokens will be sent1846   * @param toAddressObj address where token to be sent1847   * @param amount number of tokens to be sent1848   * @example transferFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, {Substrate: "5DfhbVfww7ThF8q6f3ij..."}, 10000n);1849   * @returns ```true``` if extrinsic success, otherwise ```false``` 1850   */1851  async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {1852    return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);1853  }18541855  /**1856   * Destroy some amount of tokens1857   * @param signer keyring of signer1858   * @param collectionId ID of collection1859   * @param amount amount of tokens to be destroyed1860   * @param label 1861   * @example burnTokens(aliceKeyring, 10, 1000n);1862   * @returns ```true``` if extrinsic success, otherwise ```false``` 1863   */1864  async burnTokens(signer: IKeyringPair, collectionId: number, amount=100n, label?: string): Promise<boolean> {1865    return (await super.burnToken(signer, collectionId, 0, label, amount)).success;1866  }18671868  /**1869   * Burn some tokens on behalf of the owner.1870   * @param signer keyring of signer1871   * @param collectionId ID of collection1872   * @param fromAddressObj address on behalf of which tokens will be burnt1873   * @param amount amount of tokens to be burnt1874   * @param label 1875   * @example burnTokensFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);1876   * @returns ```true``` if extrinsic success, otherwise ```false``` 1877   */1878  async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=100n, label?: string): Promise<boolean> {1879    return await super.burnTokenFrom(signer, collectionId, fromAddressObj, 0, label, amount);1880  }18811882  /**1883   * Get total collection supply1884   * @param collectionId 1885   * @returns 1886   */1887  async getTotalPieces(collectionId: number): Promise<bigint> {1888    return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();1889  }18901891  /**1892   * Set, change, or remove approved address to transfer tokens.1893   * 1894   * @param signer keyring of signer1895   * @param collectionId ID of collection1896   * @param toAddressObj address to be approved1897   * @param amount amount of tokens to be approved1898   * @param label 1899   * @example approveTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n)1900   * @returns ```true``` if extrinsic success, otherwise ```false``` 1901   */1902  async approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {1903    return super.approveToken(signer, collectionId, 0, toAddressObj, label, amount);1904  }19051906  /**1907   * Get amount of fungible tokens approved to transfer1908   * @param collectionId ID of collection1909   * @param fromAddressObj owner of tokens1910   * @param toAddressObj the address approved for the transfer of tokens on behalf of the owner1911   * @returns number of tokens approved for the transfer1912   */1913  async getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {1914    return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);1915  }1916}191719181919class ChainGroup extends HelperGroup {1920  /**1921   * Get system properties of a chain1922   * @example getChainProperties();1923   * @returns ss58Format, token decimals, and token symbol1924   */1925  getChainProperties(): IChainProperties {1926    const properties = (this.helper.api as any).registry.getChainProperties().toJSON();1927    return {1928      ss58Format: properties.ss58Format.toJSON(),1929      tokenDecimals: properties.tokenDecimals.toJSON(),1930      tokenSymbol: properties.tokenSymbol.toJSON(),1931    };1932  }19331934  /**1935   * Get chain header1936   * @example getLatestBlockNumber();1937   * @returns the number of the last block1938   */1939  async getLatestBlockNumber(): Promise<number> {1940    return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();1941  }19421943  /**1944   * Get block hash by block number1945   * @param blockNumber number of block1946   * @example getBlockHashByNumber(12345);1947   * @returns hash of a block1948   */1949  async getBlockHashByNumber(blockNumber: number): Promise<string | null> {1950    const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();1951    if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;1952    return blockHash;1953  }19541955  /**1956   * Get account nonce1957   * @param address substrate address1958   * @example getNonce("5GrwvaEF5zXb26Fz...");1959   * @returns number, account's nonce1960   */1961  async getNonce(address: TSubstrateAccount): Promise<number> {1962    return (await (this.helper.api as any).query.system.account(address)).nonce.toNumber();1963  }1964}196519661967class BalanceGroup extends HelperGroup {1968  /**1969   * Representation of the native token in the smallest unit1970   * @example getOneTokenNominal()1971   * @returns ```BigInt``` representation of the native token in the smallest unit, e.g. ```1_000_000_000_000_000_000n``` for QTZ.1972   */1973  getOneTokenNominal(): bigint {1974    const chainProperties = this.helper.chain.getChainProperties();1975    return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);1976  }19771978  /**1979   * Get substrate address balance1980   * @param address substrate address1981   * @example getSubstrate("5GrwvaEF5zXb26Fz...")1982   * @returns amount of tokens on address1983   */1984  async getSubstrate(address: TSubstrateAccount): Promise<bigint> {1985    return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();1986  }19871988  /**1989   * Get ethereum address balance1990   * @param address ethereum address1991   * @example getEthereum("0x9F0583DbB855d...")1992   * @returns amount of tokens on address1993   */1994  async getEthereum(address: TEthereumAccount): Promise<bigint> {1995    return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();1996  }19971998  /**1999   * Transfer tokens to substrate address2000   * @param signer keyring of signer2001   * @param address substrate address of a recepient2002   * @param amount amount of tokens to be transfered2003   * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);2004   * @returns ```true``` if extrinsic success, otherwise ```false```2005   */2006  async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {2007    const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`);20082009    let transfer = {from: null, to: null, amount: 0n} as any;2010    result.result.events.forEach(({event: {data, method, section}}) => {2011      if ((section === 'balances') && (method === 'Transfer')) {2012        transfer = {2013          from: this.helper.address.normalizeSubstrate(data[0]),2014          to: this.helper.address.normalizeSubstrate(data[1]),2015          amount: BigInt(data[2]),2016        };2017      }2018    });2019    let isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from;2020    isSuccess = isSuccess && this.helper.address.normalizeSubstrate(address) === transfer.to;2021    isSuccess = isSuccess && BigInt(amount) === transfer.amount;2022    return isSuccess;2023  }2024}202520262027class AddressGroup extends HelperGroup {2028  /**2029   * Normalizes the address to the specified ss58 format, by default ```42```.2030   * @param address substrate address2031   * @param ss58Format format for address conversion, by default ```42```2032   * @example normalizeSubstrate("unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx") // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY2033   * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation2034   */2035  normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {2036    return this.helper.util.normalizeSubstrateAddress(address, ss58Format);2037  }20382039  /**2040   * Get address in the connected chain format2041   * @param address substrate address2042   * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network2043   * @returns address in chain format2044   */2045  async normalizeSubstrateToChainFormat(address: TSubstrateAccount): Promise<TSubstrateAccount> {2046    const info = this.helper.chain.getChainProperties();2047    return encodeAddress(decodeAddress(address), info.ss58Format);2048  }20492050  /**2051   * Get substrate mirror of an ethereum address2052   * @param ethAddress ethereum address2053   * @param toChainFormat false for normalized account2054   * @example ethToSubstrate('0x9F0583DbB855d...')2055   * @returns substrate mirror of a provided ethereum address2056   */2057  async ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): Promise<TSubstrateAccount> {2058    if(!toChainFormat) return evmToAddress(ethAddress);2059    const info = this.helper.chain.getChainProperties();2060    return evmToAddress(ethAddress, info.ss58Format);2061  }20622063  /**2064   * Get ethereum mirror of a substrate address2065   * @param subAddress substrate account2066   * @example substrateToEth("5DnSF6RRjwteE3BrC...")2067   * @returns ethereum mirror of a provided substrate address2068   */2069  substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {2070    return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(subAddress), i => i.toString(16).padStart(2, '0')).join(''));2071  }2072}207320742075export class UniqueHelper extends ChainHelperBase {2076  chain: ChainGroup;2077  balance: BalanceGroup;2078  address: AddressGroup;2079  collection: CollectionGroup;2080  nft: NFTGroup;2081  rft: RFTGroup;2082  ft: FTGroup;20832084  constructor(logger?: ILogger) {2085    super(logger);2086    this.chain = new ChainGroup(this);2087    this.balance = new BalanceGroup(this);2088    this.address = new AddressGroup(this);2089    this.collection = new CollectionGroup(this);2090    this.nft = new NFTGroup(this);2091    this.rft = new RFTGroup(this);2092    this.ft = new FTGroup(this);2093  }  2094}209520962097class UniqueCollectionBase {2098  helper: UniqueHelper;2099  collectionId: number;21002101  constructor(collectionId: number, uniqueHelper: UniqueHelper) {2102    this.collectionId = collectionId;2103    this.helper = uniqueHelper;2104  }21052106  async getData() {2107    return await this.helper.collection.getData(this.collectionId);2108  }21092110  async getLastTokenId() {2111    return await this.helper.collection.getLastTokenId(this.collectionId);2112  }21132114  async isTokenExists(tokenId: number) {2115    return await this.helper.collection.isTokenExists(this.collectionId, tokenId);2116  }21172118  async getAdmins() {2119    return await this.helper.collection.getAdmins(this.collectionId);2120  }21212122  async getEffectiveLimits() {2123    return await this.helper.collection.getEffectiveLimits(this.collectionId);2124  }21252126  async setSponsor(signer: TSigner, sponsorAddress: TSubstrateAccount, label?: string) {2127    return await this.helper.collection.setSponsor(signer, this.collectionId, sponsorAddress, label);2128  }21292130  async confirmSponsorship(signer: TSigner, label?: string) {2131    return await this.helper.collection.confirmSponsorship(signer, this.collectionId, label);2132  }21332134  async setLimits(signer: TSigner, limits: ICollectionLimits, label?: string) {2135    return await this.helper.collection.setLimits(signer, this.collectionId, limits, label);2136  }21372138  async changeOwner(signer: TSigner, ownerAddress: TSubstrateAccount, label?: string) {2139    return await this.helper.collection.changeOwner(signer, this.collectionId, ownerAddress, label);2140  }21412142  async addAdmin(signer: TSigner, adminAddressObj: ICrossAccountId, label?: string) {2143    return await this.helper.collection.addAdmin(signer, this.collectionId, adminAddressObj, label);2144  }21452146  async removeAdmin(signer: TSigner, adminAddressObj: ICrossAccountId, label?: string) {2147    return await this.helper.collection.removeAdmin(signer, this.collectionId, adminAddressObj, label);2148  }21492150  async setProperties(signer: TSigner, properties: IProperty[], label?: string) {2151    return await this.helper.collection.setProperties(signer, this.collectionId, properties, label);2152  }21532154  async deleteProperties(signer: TSigner, propertyKeys: string[], label?: string) {2155    return await this.helper.collection.deleteProperties(signer, this.collectionId, propertyKeys, label);2156  }21572158  async getTokenNextSponsored(tokenId: number, addressObj: ICrossAccountId) {2159    return await this.helper.collection.getTokenNextSponsored(this.collectionId, tokenId, addressObj);2160  }21612162  async setPermissions(signer: TSigner, permissions: ICollectionPermissions, label?: string) {2163    return await this.helper.collection.setPermissions(signer, this.collectionId, permissions, label);2164  }21652166  async enableNesting(signer: TSigner, permissions: INestingPermissions, label?: string) {2167    return await this.helper.collection.enableNesting(signer, this.collectionId, permissions, label);2168  }21692170  async disableNesting(signer: TSigner, label?: string) {2171    return await this.helper.collection.disableNesting(signer, this.collectionId, label);2172  }21732174  async burn(signer: TSigner, label?: string) {2175    return await this.helper.collection.burn(signer, this.collectionId, label);2176  }2177}217821792180class UniqueNFTCollection extends UniqueCollectionBase {2181  getTokenObject(tokenId: number) {2182    return new UniqueNFTToken(tokenId, this);2183  }21842185  async getTokensByAddress(addressObj: ICrossAccountId) {2186    return await this.helper.nft.getTokensByAddress(this.collectionId, addressObj);2187  }21882189  async getToken(tokenId: number, blockHashAt?: string) {2190    return await this.helper.nft.getToken(this.collectionId, tokenId, blockHashAt);2191  }21922193  async getTokenOwner(tokenId: number, blockHashAt?: string) {2194    return await this.helper.nft.getTokenOwner(this.collectionId, tokenId, blockHashAt);2195  }21962197  async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {2198    return await this.helper.nft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);2199  }22002201  async getTokenChildren(tokenId: number, blockHashAt?: string) {2202    return await this.helper.nft.getTokenChildren(this.collectionId, tokenId, blockHashAt);2203  }22042205  async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId) {2206    return await this.helper.nft.transferToken(signer, this.collectionId, tokenId, addressObj);2207  }22082209  async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2210    return await this.helper.nft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj);2211  }22122213  async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {2214    return await this.helper.nft.approveToken(signer, this.collectionId, tokenId, toAddressObj, label);2215  }22162217  async isTokenApproved(tokenId: number, toAddressObj: ICrossAccountId) {2218    return await this.helper.nft.isTokenApproved(this.collectionId, tokenId, toAddressObj);2219  }22202221  async mintToken(signer: TSigner, owner: ICrossAccountId, properties?: IProperty[], label?: string) {2222    return await this.helper.nft.mintToken(signer, {collectionId: this.collectionId, owner, properties}, label);2223  }22242225  async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string) {2226    return await this.helper.nft.mintMultipleTokens(signer, this.collectionId, tokens, label);2227  }22282229  async burnToken(signer: TSigner, tokenId: number, label?: string) {2230    return await this.helper.nft.burnToken(signer, this.collectionId, tokenId, label);2231  }22322233  async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[], label?: string) {2234    return await this.helper.nft.setTokenProperties(signer, this.collectionId, tokenId, properties, label);2235  }22362237  async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[], label?: string) {2238    return await this.helper.nft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys, label);2239  }22402241  async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[], label?: string) {2242    return await this.helper.nft.setTokenPropertyPermissions(signer, this.collectionId, permissions, label);2243  }22442245  async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken, label?: string) {2246    return await this.helper.nft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj, label);2247  }22482249  async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId, label?: string) {2250    return await this.helper.nft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj, label);2251  }2252}225322542255class UniqueRFTCollection extends UniqueCollectionBase {2256  getTokenObject(tokenId: number) {2257    return new UniqueRFTToken(tokenId, this);2258  }22592260  async getTokensByAddress(addressObj: ICrossAccountId) {2261    return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);2262  }22632264  async getTop10TokenOwners(tokenId: number) {2265    return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);2266  }22672268  async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {2269    return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);2270  }22712272  async getTokenTotalPieces(tokenId: number) {2273    return await this.helper.rft.getTokenTotalPieces(this.collectionId, tokenId);2274  }22752276  async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId, amount=100n) {2277    return await this.helper.rft.transferToken(signer, this.collectionId, tokenId, addressObj, amount);2278  }22792280  async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n) {2281    return await this.helper.rft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj, amount);2282  }22832284  async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2285    return await this.helper.rft.approveToken(signer, this.collectionId, tokenId, toAddressObj, label, amount);2286  }22872288  async getTokenApprovedPieces(tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2289    return await this.helper.rft.getTokenApprovedPieces(this.collectionId, tokenId, toAddressObj, fromAddressObj);2290  }22912292  async repartitionToken(signer: TSigner, tokenId: number, amount: bigint, label?: string) {2293    return await this.helper.rft.repartitionToken(signer, this.collectionId, tokenId, amount, label);2294  }22952296  async mintToken(signer: TSigner, owner: ICrossAccountId, pieces=100n, properties?: IProperty[], label?: string) {2297    return await this.helper.rft.mintToken(signer, {collectionId: this.collectionId, owner, pieces, properties}, label);2298  }22992300  async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[], label?: string) {2301    return await this.helper.rft.mintMultipleTokens(signer, this.collectionId, tokens, label);2302  }23032304  async burnToken(signer: TSigner, tokenId: number, amount=100n, label?: string) {2305    return await this.helper.rft.burnToken(signer, this.collectionId, tokenId, label, amount);2306  }23072308  async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[], label?: string) {2309    return await this.helper.rft.setTokenProperties(signer, this.collectionId, tokenId, properties, label);2310  }23112312  async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[], label?: string) {2313    return await this.helper.rft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys, label);2314  }23152316  async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[], label?: string) {2317    return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions, label);2318  }2319}232023212322class UniqueFTCollection extends UniqueCollectionBase {2323  async mint(signer: TSigner, owner: ICrossAccountId, amount: bigint, label?: string) {2324    return await this.helper.ft.mintTokens(signer, this.collectionId, owner, amount, label);2325  }23262327  async mintWithOneOwner(signer: TSigner, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string) {2328    return await this.helper.ft.mintMultipleTokensWithOneOwner(signer, this.collectionId, owner, tokens, label);2329  }23302331  async getBalance(addressObj: ICrossAccountId) {2332    return await this.helper.ft.getBalance(this.collectionId, addressObj);2333  }23342335  async getTop10Owners() {2336    return await this.helper.ft.getTop10Owners(this.collectionId);2337  }23382339  async transfer(signer: TSigner, toAddressObj: ICrossAccountId, amount: bigint) {2340    return await this.helper.ft.transfer(signer, this.collectionId, toAddressObj, amount);2341  }23422343  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {2344    return await this.helper.ft.transferFrom(signer, this.collectionId, fromAddressObj, toAddressObj, amount);2345  }23462347  async burnTokens(signer: TSigner, amount: bigint, label?: string) {2348    return await this.helper.ft.burnTokens(signer, this.collectionId, amount, label);2349  }23502351  async burnTokensFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount: bigint, label?: string) {2352    return await this.helper.ft.burnTokensFrom(signer, this.collectionId, fromAddressObj, amount, label);2353  }23542355  async getTotalPieces() {2356    return await this.helper.ft.getTotalPieces(this.collectionId);2357  }23582359  async approveTokens(signer: TSigner, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2360    return await this.helper.ft.approveTokens(signer, this.collectionId, toAddressObj, amount, label);2361  }23622363  async getApprovedTokens(fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2364    return await this.helper.ft.getApprovedTokens(this.collectionId, fromAddressObj, toAddressObj);2365  }2366}236723682369class UniqueTokenBase implements IToken {2370  collection: UniqueNFTCollection | UniqueRFTCollection;2371  collectionId: number;2372  tokenId: number;23732374  constructor(tokenId: number, collection: UniqueNFTCollection | UniqueRFTCollection) {2375    this.collection = collection;2376    this.collectionId = collection.collectionId;2377    this.tokenId = tokenId;2378  }23792380  async getNextSponsored(addressObj: ICrossAccountId) {2381    return await this.collection.getTokenNextSponsored(this.tokenId, addressObj);2382  }23832384  async setProperties(signer: TSigner, properties: IProperty[], label?: string) {2385    return await this.collection.setTokenProperties(signer, this.tokenId, properties, label);2386  }23872388  async deleteProperties(signer: TSigner, propertyKeys: string[], label?: string) {2389    return await this.collection.deleteTokenProperties(signer, this.tokenId, propertyKeys, label);2390  }2391}239223932394class UniqueNFTToken extends UniqueTokenBase {2395  collection: UniqueNFTCollection;23962397  constructor(tokenId: number, collection: UniqueNFTCollection) {2398    super(tokenId, collection);2399    this.collection = collection;2400  }24012402  async getData(blockHashAt?: string) {2403    return await this.collection.getToken(this.tokenId, blockHashAt);2404  }24052406  async getOwner(blockHashAt?: string) {2407    return await this.collection.getTokenOwner(this.tokenId, blockHashAt);2408  }24092410  async getTopmostOwner(blockHashAt?: string) {2411    return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);2412  }24132414  async getChildren(blockHashAt?: string) {2415    return await this.collection.getTokenChildren(this.tokenId, blockHashAt);2416  }24172418  async nest(signer: TSigner, toTokenObj: IToken, label?: string) {2419    return await this.collection.nestToken(signer, this.tokenId, toTokenObj, label);2420  }24212422  async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId, label?: string) {2423    return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj, label);2424  }24252426  async transfer(signer: TSigner, addressObj: ICrossAccountId) {2427    return await this.collection.transferToken(signer, this.tokenId, addressObj);2428  }24292430  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2431    return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj);2432  }24332434  async approve(signer: TSigner, toAddressObj: ICrossAccountId, label?: string) {2435    return await this.collection.approveToken(signer, this.tokenId, toAddressObj, label);2436  }24372438  async isApproved(toAddressObj: ICrossAccountId) {2439    return await this.collection.isTokenApproved(this.tokenId, toAddressObj);2440  }24412442  async burn(signer: TSigner, label?: string) {2443    return await this.collection.burnToken(signer, this.tokenId, label);2444  }2445}24462447class UniqueRFTToken extends UniqueTokenBase {2448  collection: UniqueRFTCollection;24492450  constructor(tokenId: number, collection: UniqueRFTCollection) {2451    super(tokenId, collection);2452    this.collection = collection;2453  }24542455  async getTop10Owners() {2456    return await this.collection.getTop10TokenOwners(this.tokenId);2457  }24582459  async getBalance(addressObj: ICrossAccountId) {2460    return await this.collection.getTokenBalance(this.tokenId, addressObj);2461  }24622463  async getTotalPieces() {2464    return await this.collection.getTokenTotalPieces(this.tokenId);2465  }24662467  async transfer(signer: TSigner, addressObj: ICrossAccountId, amount=100n) {2468    return await this.collection.transferToken(signer, this.tokenId, addressObj, amount);2469  }24702471  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n) {2472    return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj, amount);2473  }24742475  async approve(signer: TSigner, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2476    return await this.collection.approveToken(signer, this.tokenId, toAddressObj, amount, label);2477  }24782479  async getApprovedPieces(fromAddressObj: ICrossAccountId, toAccountObj: ICrossAccountId) {2480    return await this.collection.getTokenApprovedPieces(this.tokenId, fromAddressObj, toAccountObj);2481  }24822483  async repartition(signer: TSigner, amount: bigint, label?: string) {2484    return await this.collection.repartitionToken(signer, this.tokenId, amount, label);2485  }24862487  async burn(signer: TSigner, amount=100n, label?: string) {2488    return await this.collection.burnToken(signer, this.tokenId, amount, label);2489  }2490}
after · tests/src/util/playgrounds/unique.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034/* eslint-disable @typescript-eslint/no-var-requires */5/* eslint-disable function-call-argument-newline */6/* eslint-disable no-prototype-builtins */78import {ApiPromise, WsProvider, Keyring} from '@polkadot/api';9import {ApiInterfaceEvents} from '@polkadot/api/types';10import {IKeyringPair} from '@polkadot/types/types';11import {encodeAddress, decodeAddress, keccakAsHex, evmToAddress, addressToEvm} from '@polkadot/util-crypto';12import { ICrossAccountIdLower, ICrossAccountId, TUniqueNetworks, IApiListeners, TApiAllowedListeners, TSigner, TSubstrateAccount, ICollectionLimits, ICollectionPermissions, INestingPermissions, IProperty, ITokenPropertyPermission, ICollectionCreationOptions, IToken, IChainProperties, TEthereumAccount } from './types';131415const crossAccountIdFromLower = (lowerAddress: ICrossAccountIdLower): ICrossAccountId => {16  const address = {} as ICrossAccountId;17  if(lowerAddress.substrate) address.Substrate = lowerAddress.substrate;18  if(lowerAddress.ethereum) address.Ethereum = lowerAddress.ethereum;19  return address;20};212223const nesting = {24  toChecksumAddress(address: string): string {25    if (typeof address === 'undefined') return '';2627    if(!/^(0x)?[0-9a-f]{40}$/i.test(address)) throw new Error(`Given address "${address}" is not a valid Ethereum address.`);2829    address = address.toLowerCase().replace(/^0x/i,'');30    const addressHash = keccakAsHex(address).replace(/^0x/i,'');31    const checksumAddress = ['0x'];3233    for (let i = 0; i < address.length; i++) {34      // If ith character is 8 to f then make it uppercase35      if (parseInt(addressHash[i], 16) > 7) {36        checksumAddress.push(address[i].toUpperCase());37      } else {38        checksumAddress.push(address[i]);39      }40    }41    return checksumAddress.join('');42  },43  tokenIdToAddress(collectionId: number, tokenId: number) {44    return this.toChecksumAddress(`0xf8238ccfff8ed887463fd5e0${collectionId.toString(16).padStart(8, '0')}${tokenId.toString(16).padStart(8, '0')}`);45  },46};474849interface IChainEvent {50  data: any;51  method: string;52  section: string;53}5455interface ITransactionResult {56    status: 'Fail' | 'Success';57    result: {58        events: {59          event: IChainEvent60        }[];61    },62    moduleError?: string;63}6465interface ILogger {66  log: (msg: any, level?: string) => void;67  level: {68    ERROR: 'ERROR';69    WARNING: 'WARNING';70    INFO: 'INFO';71    [key: string]: string;72  }73}7475interface IUniqueHelperLog {76  executedAt: number;77  executionTime: number;78  type: 'extrinsic' | 'rpc';79  status: 'Fail' | 'Success';80  call: string;81  params: any[];82  moduleError?: string;83  events?: any;84}8586class UniqueUtil {87  static transactionStatus = {88    NOT_READY: 'NotReady',89    FAIL: 'Fail',90    SUCCESS: 'Success',91  };9293  static chainLogType = {94    EXTRINSIC: 'extrinsic',95    RPC: 'rpc',96  };9798  static getNestingTokenAddress(collectionId: number, tokenId: number) {99    return nesting.tokenIdToAddress(collectionId, tokenId);100  }101102  static getDefaultLogger(): ILogger {103    return {104      log(msg: any, level = 'INFO') {105        console[level.toLocaleLowerCase() === 'error' ? 'error' : 'log'](...(Array.isArray(msg) ? msg : [msg]));106      },107      level: {108        ERROR: 'ERROR',109        WARNING: 'WARNING',110        INFO: 'INFO',111      },112    };113  }114115  static vec2str(arr: string[] | number[]) {116    return arr.map(x => String.fromCharCode(parseInt(x.toString()))).join('');117  }118119  static str2vec(string: string) {120    if (typeof string !== 'string') return string;121    return Array.from(string).map(x => x.charCodeAt(0));122  }123124  static fromSeed(seed: string, ss58Format = 42) {125    const keyring = new Keyring({type: 'sr25519', ss58Format});126    return keyring.addFromUri(seed);127  }128129  static normalizeSubstrateAddress(address: string, ss58Format = 42) {130    return encodeAddress(decodeAddress(address), ss58Format);131  }132133  static extractCollectionIdFromCreationResult(creationResult: ITransactionResult, label = 'new collection') {134    if (creationResult.status !== this.transactionStatus.SUCCESS) {135      throw Error(`Unable to create collection for ${label}`);136    }137138    let collectionId = null;139    creationResult.result.events.forEach(({event: {data, method, section}}) => {140      if ((section === 'common') && (method === 'CollectionCreated')) {141        collectionId = parseInt(data[0].toString(), 10);142      }143    });144145    if (collectionId === null) {146      throw Error(`No CollectionCreated event for ${label}`);147    }148149    return collectionId;150  }151152  static extractTokensFromCreationResult(creationResult: ITransactionResult, label = 'new tokens') {153    if (creationResult.status !== this.transactionStatus.SUCCESS) {154      throw Error(`Unable to create tokens for ${label}`);155    }156    let success = false;157    const tokens = [] as any;158    creationResult.result.events.forEach(({event: {data, method, section}}) => {159      if (method === 'ExtrinsicSuccess') {160        success = true;161      } else if ((section === 'common') && (method === 'ItemCreated')) {162        tokens.push({163          collectionId: parseInt(data[0].toString(), 10),164          tokenId: parseInt(data[1].toString(), 10),165          owner: data[2].toJSON(),166        });167      }168    });169    return {success, tokens};170  }171172  static extractTokensFromBurnResult(burnResult: ITransactionResult, label = 'burned tokens') {173    if (burnResult.status !== this.transactionStatus.SUCCESS) {174      throw Error(`Unable to burn tokens for ${label}`);175    }176    let success = false;177    const tokens = [] as any;178    burnResult.result.events.forEach(({event: {data, method, section}}) => {179      if (method === 'ExtrinsicSuccess') {180        success = true;181      } else if ((section === 'common') && (method === 'ItemDestroyed')) {182        tokens.push({183          collectionId: parseInt(data[0].toString(), 10),184          tokenId: parseInt(data[1].toString(), 10),185          owner: data[2].toJSON(),186        });187      }188    });189    return {success, tokens};190  }191192  static findCollectionInEvents(events: {event: IChainEvent}[], collectionId: number, expectedSection: string, expectedMethod: string, label?: string) {193    let eventId = null;194    events.forEach(({event: {data, method, section}}) => {195      if ((section === expectedSection) && (method === expectedMethod)) {196        eventId = parseInt(data[0].toString(), 10);197      }198    });199200    if (eventId === null) {201      throw Error(`No ${expectedMethod} event for ${label}`);202    }203    return eventId === collectionId;204  }205206  static isTokenTransferSuccess(events: {event: IChainEvent}[], collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n) {207    const normalizeAddress = (address: string | ICrossAccountId) => {208      if(typeof address === 'string') return address;209      const obj = {} as any;210      Object.keys(address).forEach(k => {211        obj[k.toLocaleLowerCase()] = address[k as 'Substrate' | 'Ethereum'];212      });213      if(obj.substrate) return {Substrate: this.normalizeSubstrateAddress(obj.substrate)};214      if(obj.ethereum) return {Ethereum: obj.ethereum.toLocaleLowerCase()};215      return address;216    };217    let transfer = {collectionId: null, tokenId: null, from: null, to: null, amount: 1} as any;218    events.forEach(({event: {data, method, section}}) => {219      if ((section === 'common') && (method === 'Transfer')) {220        const hData = (data as any).toJSON();221        transfer = {222          collectionId: hData[0],223          tokenId: hData[1],224          from: normalizeAddress(hData[2]),225          to: normalizeAddress(hData[3]),226          amount: BigInt(hData[4]),227        };228      }229    });230    let isSuccess = parseInt(collectionId.toString()) === transfer.collectionId && parseInt(tokenId.toString()) === transfer.tokenId;231    isSuccess = isSuccess && JSON.stringify(normalizeAddress(fromAddressObj)) === JSON.stringify(transfer.from);232    isSuccess = isSuccess && JSON.stringify(normalizeAddress(toAddressObj)) === JSON.stringify(transfer.to);233    isSuccess = isSuccess && amount === transfer.amount;234    return isSuccess;235  }236}237238239class ChainHelperBase {240  transactionStatus = UniqueUtil.transactionStatus;241  chainLogType = UniqueUtil.chainLogType;242  util: typeof UniqueUtil;243  logger: ILogger;244  api: ApiPromise | null;245  forcedNetwork: TUniqueNetworks | null;246  network: TUniqueNetworks | null;247  chainLog: IUniqueHelperLog[];248249  constructor(logger?: ILogger) {250    this.util = UniqueUtil;251    if (typeof logger == 'undefined') logger = this.util.getDefaultLogger();252    this.logger = logger;253    this.api = null;254    this.forcedNetwork = null;255    this.network = null;256    this.chainLog = [];257  }258259  clearChainLog(): void {260    this.chainLog = [];261  }262263  forceNetwork(value: TUniqueNetworks): void {264    this.forcedNetwork = value;265  }266267  async connect(wsEndpoint: string, listeners?: IApiListeners) {268    if (this.api !== null) throw Error('Already connected');269    const {api, network} = await ChainHelperBase.createConnection(wsEndpoint, listeners, this.forcedNetwork);270    this.api = api;271    this.network = network;272  }273274  async disconnect() {275    if (this.api === null) return;276    await this.api.disconnect();277    this.api = null;278    this.network = null;279  }280281  static async detectNetwork(api: ApiPromise): Promise<TUniqueNetworks> {282    const spec = (await api.query.system.lastRuntimeUpgrade()).toJSON() as any;283    if(['quartz', 'unique'].indexOf(spec.specName) > -1) return spec.specName;284    return 'opal';285  }286287  static async detectNetworkByWsEndpoint(wsEndpoint: string): Promise<TUniqueNetworks> {288    const api = new ApiPromise({provider: new WsProvider(wsEndpoint)});289    await api.isReady;290291    const network = await this.detectNetwork(api);292293    await api.disconnect();294295    return network;296  }297298  static async createConnection(wsEndpoint: string, listeners?: IApiListeners, network?: TUniqueNetworks | null): Promise<{ 299    api: ApiPromise; 300    network: TUniqueNetworks; 301  }> {302    if(typeof network === 'undefined' || network === null) network = 'opal';303    const supportedRPC = {304      opal: {305        unique: require('@unique-nft/opal-testnet-types/definitions').unique.rpc,306      },307      quartz: {308        unique: require('@unique-nft/quartz-mainnet-types/definitions').unique.rpc,309      },310      unique: {311        unique: require('@unique-nft/unique-mainnet-types/definitions').unique.rpc,312      },313    };314    if(!supportedRPC.hasOwnProperty(network)) network = await this.detectNetworkByWsEndpoint(wsEndpoint);315    const rpc = supportedRPC[network];316317    // TODO: investigate how to replace rpc in runtime318    // api._rpcCore.addUserInterfaces(rpc);319320    const api = new ApiPromise({provider: new WsProvider(wsEndpoint), rpc});321322    await api.isReadyOrError;323324    if (typeof listeners === 'undefined') listeners = {};325    for (const event of ['connected', 'disconnected', 'error', 'ready', 'decorated']) {326      if (!listeners.hasOwnProperty(event) || typeof listeners[event as TApiAllowedListeners] === 'undefined') continue;327      api.on(event as ApiInterfaceEvents, listeners[event as TApiAllowedListeners] as (...args: any[]) => any);328    }329330    return {api, network};331  }332333  getTransactionStatus(data: {events: {event: IChainEvent}[], status: any}) {334    const {events, status} = data;335    if (status.isReady) {336      return this.transactionStatus.NOT_READY;337    }338    if (status.isBroadcast) {339      return this.transactionStatus.NOT_READY;340    }341    if (status.isInBlock || status.isFinalized) {342      const errors = events.filter(e => e.event.data.method === 'ExtrinsicFailed');343      if (errors.length > 0) {344        return this.transactionStatus.FAIL;345      }346      if (events.filter(e => e.event.data.method === 'ExtrinsicSuccess').length > 0) {347        return this.transactionStatus.SUCCESS;348      }349    }350351    return this.transactionStatus.FAIL;352  }353354  signTransaction(sender: TSigner, transaction: any, label = 'transaction', options: any = null) {355    const sign = (callback: any) => {356      if(options !== null) return transaction.signAndSend(sender, options, callback);357      return transaction.signAndSend(sender, callback);358    };359    return new Promise(async (resolve, reject) => {360      try {361        const unsub = await sign((result: any) => {362          const status = this.getTransactionStatus(result);363364          if (status === this.transactionStatus.SUCCESS) {365            this.logger.log(`${label} successful`);366            unsub();367            resolve({result, status});368          } else if (status === this.transactionStatus.FAIL) {369            let moduleError = null;370371            if (result.hasOwnProperty('dispatchError')) {372              const dispatchError = result['dispatchError'];373374              if (dispatchError && dispatchError.isModule) {375                const modErr = dispatchError.asModule;376                const errorMeta = dispatchError.registry.findMetaError(modErr);377378                moduleError = `${errorMeta.section}.${errorMeta.name}`;379              }380              else {381                this.logger.log(result, this.logger.level.ERROR);382              }383            }384385            this.logger.log(`Something went wrong with ${label}. Status: ${status}`, this.logger.level.ERROR);386            unsub();387            reject({status, moduleError, result});388          }389        });390      } catch (e) {391        this.logger.log(e, this.logger.level.ERROR);392        reject(e);393      }394    });395  }396397  constructApiCall(apiCall: string, params: any[]) {398    if(!apiCall.startsWith('api.')) throw Error(`Invalid api call: ${apiCall}`);399    let call = this.api as any;400    for(const part of apiCall.slice(4).split('.')) {401      call = call[part];402    }403    return call(...params);404  }405406  async executeExtrinsic(sender: TSigner, extrinsic: string, params: any[], expectSuccess=false, failureMessage='expected success') {407    if(this.api === null) throw Error('API not initialized');408    if(!extrinsic.startsWith('api.tx.')) throw Error(`${extrinsic} is not transaction`);409410    const startTime = (new Date()).getTime();411    let result: ITransactionResult;412    let events = [];413    try {414      result = await this.signTransaction(sender, this.constructApiCall(extrinsic, params), extrinsic) as ITransactionResult;415      events = result.result.events.map((x: any) => x.toHuman());416    }417    catch(e) {418      if(!(e as object).hasOwnProperty('status')) throw e;419      result = e as ITransactionResult;420    }421422    const endTime = (new Date()).getTime();423424    const log = {425      executedAt: endTime,426      executionTime: endTime - startTime,427      type: this.chainLogType.EXTRINSIC,428      status: result.status,429      call: extrinsic,430      params,431    } as IUniqueHelperLog;432433    if(result.status !== this.transactionStatus.SUCCESS && result.moduleError) log.moduleError = result.moduleError;434    if(events.length > 0) log.events = events;435436    this.chainLog.push(log);437438    if(expectSuccess && result.status !== this.transactionStatus.SUCCESS) throw Error(failureMessage);439    return result;440  }441442  async callRpc(rpc: string, params?: any[]) {443    if(typeof params === 'undefined') params = [];444    if(this.api === null) throw Error('API not initialized');445    if(!rpc.startsWith('api.rpc.') && !rpc.startsWith('api.query.')) throw Error(`${rpc} is not RPC call`);446447    const startTime = (new Date()).getTime();448    let result;449    let error = null;450    const log = {451      type: this.chainLogType.RPC,452      call: rpc,453      params,454    } as IUniqueHelperLog;455456    try {457      result = await this.constructApiCall(rpc, params);458    }459    catch(e) {460      error = e;461    }462463    const endTime = (new Date()).getTime();464465    log.executedAt = endTime;466    log.status = (error === null ? this.transactionStatus.SUCCESS : this.transactionStatus.FAIL) as 'Fail' | 'Success';467    log.executionTime = endTime - startTime;468469    this.chainLog.push(log);470471    if(error !== null) throw error;472473    return result;474  }475476  getSignerAddress(signer: IKeyringPair | string): string {477    if(typeof signer === 'string') return signer;478    return signer.address;479  }480}481482483class HelperGroup {484  helper: UniqueHelper;485486  constructor(uniqueHelper: UniqueHelper) {487    this.helper = uniqueHelper;488  }489}490491492class CollectionGroup extends HelperGroup {493  /**494 * Get number of blocks when sponsored transaction is available.495 *496 * @param collectionId ID of collection497 * @param tokenId ID of token498 * @param addressObj address for which the sponsorship is checked499 * @example await getTokenNextSponsored(1, 2, {Substrate: '5DfhbVfww7ThF8q6f3...'});500 * @returns number of blocks or null if sponsorship hasn't been set501 */502  async getTokenNextSponsored(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<number | null> {503    return (await this.helper.callRpc('api.rpc.unique.nextSponsored', [collectionId, addressObj, tokenId])).toJSON();504  }505506  /**507   * Get the number of created collections.508   * 509   * @returns number of created collections510   */511  async getTotalCount(): Promise<number> {512    return (await this.helper.callRpc('api.rpc.unique.collectionStats')).created.toNumber();513  }514515  /**516   * Get information about the collection with additional data, including the number of tokens it contains, its administrators, the normalized address of the collection's owner, and decoded name and description.517   * 518   * @param collectionId ID of collection519   * @example await getData(2)520   * @returns collection information object521   */522  async getData(collectionId: number): Promise<{523    id: number;524    name: string;525    description: string;526    tokensCount: number;527    admins: ICrossAccountId[];528    normalizedOwner: TSubstrateAccount;529    raw: any530  } | null> {531    const collection = await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId]);532    const humanCollection = collection.toHuman(), collectionData = {533      id: collectionId, name: null, description: null, tokensCount: 0, admins: [],534      raw: humanCollection,535    } as any, jsonCollection = collection.toJSON();536    if (humanCollection === null) return null;537    collectionData.raw.limits = jsonCollection.limits;538    collectionData.raw.permissions = jsonCollection.permissions;539    collectionData.normalizedOwner = this.helper.address.normalizeSubstrate(collectionData.raw.owner);540    for (const key of ['name', 'description']) {541      collectionData[key] = this.helper.util.vec2str(humanCollection[key]);542    }543544    collectionData.tokensCount = (['RFT', 'NFT'].includes(humanCollection.mode)) ? await this.helper[humanCollection.mode.toLocaleLowerCase() as 'nft' | 'rft'].getLastTokenId(collectionId) : 0;545    collectionData.admins = await this.getAdmins(collectionId);546547    return collectionData;548  }549550  /**551   * Get the normalized addresses of the collection's administrators.552   * 553   * @param collectionId ID of collection554   * @example await getAdmins(1)555   * @returns array of administrators556   */557  async getAdmins(collectionId: number): Promise<ICrossAccountId[]> {558    const normalized = [];559    for(const admin of (await this.helper.callRpc('api.rpc.unique.adminlist', [collectionId])).toHuman()) {560      if(admin.Substrate) normalized.push({Substrate: this.helper.address.normalizeSubstrate(admin.Substrate)});561      else normalized.push(admin);562    }563    return normalized;564  }565566  /**567   * Get the effective limits of the collection instead of null for default values568   * 569   * @param collectionId ID of collection570   * @example await getEffectiveLimits(2)571   * @returns object of collection limits572   */573  async getEffectiveLimits(collectionId: number): Promise<ICollectionLimits> {574    return (await this.helper.callRpc('api.rpc.unique.effectiveCollectionLimits', [collectionId])).toJSON();575  }576577  /**578   * Burns the collection if the signer has sufficient permissions and collection is empty.579   * 580   * @param signer keyring of signer581   * @param collectionId ID of collection582   * @param label extra label for log583   * @example await helper.collection.burn(aliceKeyring, 3);584   * @returns ```true``` if extrinsic success, otherwise ```false```585   */586  async burn(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {587    if(typeof label === 'undefined') label = `collection #${collectionId}`;588    const result = await this.helper.executeExtrinsic(589      signer,590      'api.tx.unique.destroyCollection', [collectionId],591      true, `Unable to burn collection for ${label}`,592    );593594    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionDestroyed', label);595  }596597  /**598   * Sets the sponsor for the collection (Requires the Substrate address).599   * 600   * @param signer keyring of signer601   * @param collectionId ID of collection602   * @param sponsorAddress Sponsor substrate address603   * @param label extra label for log604   * @example setSponsor(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")605   * @returns ```true``` if extrinsic success, otherwise ```false```606   */607  async setSponsor(signer: TSigner, collectionId: number, sponsorAddress: TSubstrateAccount, label?: string): Promise<boolean> {608    if(typeof label === 'undefined') label = `collection #${collectionId}`;609    const result = await this.helper.executeExtrinsic(610      signer,611      'api.tx.unique.setCollectionSponsor', [collectionId, sponsorAddress],612      true, `Unable to set collection sponsor for ${label}`,613    );614615    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionSponsorSet', label);616  }617618  /**619   * Confirms consent to sponsor the collection on behalf of the signer.620   * 621   * @param signer keyring of signer622   * @param collectionId ID of collection623   * @param label extra label for log624   * @example confirmSponsorship(aliceKeyring, 10)625   * @returns ```true``` if extrinsic success, otherwise ```false```626   */627  async confirmSponsorship(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {628    if(typeof label === 'undefined') label = `collection #${collectionId}`;629    const result = await this.helper.executeExtrinsic(630      signer,631      'api.tx.unique.confirmSponsorship', [collectionId],632      true, `Unable to confirm collection sponsorship for ${label}`,633    );634635    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'SponsorshipConfirmed', label);636  }637638  /**639   * Sets the limits of the collection. At least one limit must be specified for a correct call.640   * 641   * @param signer keyring of signer642   * @param collectionId ID of collection643   * @param limits collection limits object644   * @param label extra label for log645   * @example646   * await setLimits(647   *   aliceKeyring,648   *   10,649   *   {650   *     sponsorTransferTimeout: 0,651   *     ownerCanDestroy: false652   *   }653   * )654   * @returns ```true``` if extrinsic success, otherwise ```false```655   */656  async setLimits(signer: TSigner, collectionId: number, limits: ICollectionLimits, label?: string): Promise<boolean> {657    if(typeof label === 'undefined') label = `collection #${collectionId}`;658    const result = await this.helper.executeExtrinsic(659      signer,660      'api.tx.unique.setCollectionLimits', [collectionId, limits],661      true, `Unable to set collection limits for ${label}`,662    );663664    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionLimitSet', label);665  }666667  /**668   * Changes the owner of the collection to the new Substrate address.669   * 670   * @param signer keyring of signer671   * @param collectionId ID of collection672   * @param ownerAddress substrate address of new owner673   * @param label extra label for log674   * @example changeOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...")675   * @returns ```true``` if extrinsic success, otherwise ```false```676   */677  async changeOwner(signer: TSigner, collectionId: number, ownerAddress: TSubstrateAccount, label?: string): Promise<boolean> {678    if(typeof label === 'undefined') label = `collection #${collectionId}`;679    const result = await this.helper.executeExtrinsic(680      signer,681      'api.tx.unique.changeCollectionOwner', [collectionId, ownerAddress],682      true, `Unable to change collection owner for ${label}`,683    );684685    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionOwnedChanged', label);686  }687688  /**689   * Adds a collection administrator. 690   * 691   * @param signer keyring of signer692   * @param collectionId ID of collection693   * @param adminAddressObj Administrator address (substrate or ethereum)694   * @param label extra label for log695   * @example addAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})696   * @returns ```true``` if extrinsic success, otherwise ```false```697   */698  async addAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {699    if(typeof label === 'undefined') label = `collection #${collectionId}`;700    const result = await this.helper.executeExtrinsic(701      signer,702      'api.tx.unique.addCollectionAdmin', [collectionId, adminAddressObj],703      true, `Unable to add collection admin for ${label}`,704    );705706    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminAdded', label);707  }708709  /**710   * Removes a collection administrator.711   * 712   * @param signer keyring of signer713   * @param collectionId ID of collection714   * @param adminAddressObj Administrator address (substrate or ethereum)715   * @param label extra label for log716   * @example removeAdmin(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."})717   * @returns ```true``` if extrinsic success, otherwise ```false```718   */719  async removeAdmin(signer: TSigner, collectionId: number, adminAddressObj: ICrossAccountId, label?: string): Promise<boolean> {720    if(typeof label === 'undefined') label = `collection #${collectionId}`;721    const result = await this.helper.executeExtrinsic(722      signer,723      'api.tx.unique.removeCollectionAdmin', [collectionId, adminAddressObj],724      true, `Unable to remove collection admin for ${label}`,725    );726727    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionAdminRemoved', label);728  }729730  /**731   * Sets onchain permissions for selected collection.732   * 733   * @param signer keyring of signer734   * @param collectionId ID of collection735   * @param permissions collection permissions object736   * @param label extra label for log737   * @example setPermissions(aliceKeyring, 10, {access:'AllowList', mintMode: true, nesting: {collectionAdmin: true, tokenOwner: true}});738   * @returns ```true``` if extrinsic success, otherwise ```false```739   */740  async setPermissions(signer: TSigner, collectionId: number, permissions: ICollectionPermissions, label?: string): Promise<boolean> {741    if(typeof label === 'undefined') label = `collection #${collectionId}`;742    const result = await this.helper.executeExtrinsic(743      signer,744      'api.tx.unique.setCollectionPermissions', [collectionId, permissions],745      true, `Unable to set collection permissions for ${label}`,746    );747748    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'unique', 'CollectionPermissionSet', label);749  }750751  /**752   * Enables nesting for selected collection. If `restricted` set, you can nest only tokens from specified collections.753   * 754   * @param signer keyring of signer755   * @param collectionId ID of collection756   * @param permissions nesting permissions object757   * @param label extra label for log758   * @example enableNesting(aliceKeyring, 10, {collectionAdmin: true, tokenOwner: true});759   * @returns ```true``` if extrinsic success, otherwise ```false```760   */761  async enableNesting(signer: TSigner, collectionId: number, permissions: INestingPermissions, label?: string): Promise<boolean> {762    return await this.setPermissions(signer, collectionId, {nesting: permissions}, label);763  }764765  /**766   * Disables nesting for selected collection.767   * 768   * @param signer keyring of signer769   * @param collectionId ID of collection770   * @param label extra label for log771   * @example disableNesting(aliceKeyring, 10);772   * @returns ```true``` if extrinsic success, otherwise ```false```773   */774  async disableNesting(signer: TSigner, collectionId: number, label?: string): Promise<boolean> {775    return await this.setPermissions(signer, collectionId, {nesting: {tokenOwner: false, collectionAdmin: false}}, label);776  }777778  /**779   * Sets onchain properties to the collection.780   * 781   * @param signer keyring of signer782   * @param collectionId ID of collection783   * @param properties array of property objects784   * @param label extra label for log785   * @example setProperties(aliceKeyring, 10, [{key: "gender", value: "male"}]);786   * @returns ```true``` if extrinsic success, otherwise ```false```787   */788  async setProperties(signer: TSigner, collectionId: number, properties: IProperty[], label?: string): Promise<boolean> {789    if(typeof label === 'undefined') label = `collection #${collectionId}`;790    const result = await this.helper.executeExtrinsic(791      signer,792      'api.tx.unique.setCollectionProperties', [collectionId, properties],793      true, `Unable to set collection properties for ${label}`,794    );795796    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertySet', label);797  }798799  /**800   * Deletes onchain properties from the collection.801   * 802   * @param signer keyring of signer803   * @param collectionId ID of collection804   * @param propertyKeys array of property keys to delete805   * @param label806   * @example deleteProperties(aliceKeyring, 10, ["gender", "age"]);807   * @returns ```true``` if extrinsic success, otherwise ```false```808   */809  async deleteProperties(signer: TSigner, collectionId: number, propertyKeys: string[], label?: string): Promise<boolean> {810    if(typeof label === 'undefined') label = `collection #${collectionId}`;811    const result = await this.helper.executeExtrinsic(812      signer,813      'api.tx.unique.deleteCollectionProperties', [collectionId, propertyKeys],814      true, `Unable to delete collection properties for ${label}`,815    );816817    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'CollectionPropertyDeleted', label);818  }819820  /**821   * Changes the owner of the token.822   * 823   * @param signer keyring of signer824   * @param collectionId ID of collection825   * @param tokenId ID of token826   * @param addressObj address of a new owner827   * @param amount amount of tokens to be transfered. For NFT must be set to 1n828   * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})829   * @returns true if the token success, otherwise false830   */831  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=1n): Promise<boolean> {832    const result = await this.helper.executeExtrinsic(833      signer,834      'api.tx.unique.transfer', [addressObj, collectionId, tokenId, amount],835      true, `Unable to transfer token #${tokenId} from collection #${collectionId}`,836    );837838    return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, {Substrate: typeof signer === 'string' ? signer : signer.address}, addressObj, amount);839  }840841  /**842   * 843   * Change ownership of a token(s) on behalf of the owner. 844   * 845   * @param signer keyring of signer846   * @param collectionId ID of collection847   * @param tokenId ID of token848   * @param fromAddressObj address on behalf of which the token will be sent849   * @param toAddressObj new token owner850   * @param amount amount of tokens to be transfered. For NFT must be set to 1n851   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg"}, {Ethereum: "0x9F0583DbB85..."})852   * @returns true if the token success, otherwise false853   */854  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=1n): Promise<boolean> {855    const result = await this.helper.executeExtrinsic(856      signer,857      'api.tx.unique.transferFrom', [fromAddressObj, toAddressObj, collectionId, tokenId, amount],858      true, `Unable to transfer token #${tokenId} from collection #${collectionId}`,859    );860    return this.helper.util.isTokenTransferSuccess(result.result.events, collectionId, tokenId, fromAddressObj, toAddressObj, amount);861  }862863  /**864   * 865   * Destroys a concrete instance of NFT/RFT or burns a specified amount of fungible tokens.866   * 867   * @param signer keyring of signer868   * @param collectionId ID of collection869   * @param tokenId ID of token870   * @param label 871   * @param amount amount of tokens to be burned. For NFT must be set to 1n872   * @example burnToken(aliceKeyring, 10, 5);873   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```874   */875  async burnToken(signer: TSigner, collectionId: number, tokenId: number, label?: string, amount=1n): Promise<{876    success: boolean,877    token: number | null878  }> {879    if(typeof label === 'undefined') label = `collection #${collectionId}`;880    const burnResult = await this.helper.executeExtrinsic(881      signer,882      'api.tx.unique.burnItem', [collectionId, tokenId, amount],883      true, `Unable to burn token for ${label}`,884    );885    const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult, label);886    if (burnedTokens.tokens.length > 1) throw Error('Burned multiple tokens');887    return {success: burnedTokens.success, token: burnedTokens.tokens.length > 0 ? burnedTokens.tokens[0] : null};888  }889890  /**891   * Destroys a concrete instance of NFT on behalf of the owner892   * 893   * @param signer keyring of signer894   * @param collectionId ID of collection895   * @param fromAddressObj address on behalf of which the token will be burnt896   * @param tokenId ID of token897   * @param label 898   * @param amount amount of tokens to be burned. For NFT must be set to 1n899   * @example burnTokenFrom(aliceKeyring, 10, {Substrate: "5DyN4Y92vZCjv38fg..."}, 5, {Ethereum: "0x9F0583DbB85..."})900   * @returns ```true``` if extrinsic success, otherwise ```false```901   */902  async burnTokenFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, tokenId: number, label?: string, amount=1n): Promise<boolean> {903    if(typeof label === 'undefined') label = `collection #${collectionId}`;904    const burnResult = await this.helper.executeExtrinsic(905      signer,906      'api.tx.unique.burnFrom', [collectionId, fromAddressObj, tokenId, amount],907      true, `Unable to burn token from for ${label}`,908    );909    const burnedTokens = this.helper.util.extractTokensFromBurnResult(burnResult, label);910    return burnedTokens.success && burnedTokens.tokens.length > 0;911  }912913  /**914   * Set, change, or remove approved address to transfer the ownership of the NFT.915   * 916   * @param signer keyring of signer917   * @param collectionId ID of collection918   * @param tokenId ID of token919   * @param toAddressObj 920   * @param label 921   * @param amount amount of token to be approved. For NFT must be set to 1n922   * @returns ```true``` if extrinsic success, otherwise ```false```923   */924  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=1n) {925    if(typeof label === 'undefined') label = `collection #${collectionId}`;926    const approveResult = await this.helper.executeExtrinsic(927      signer, 928      'api.tx.unique.approve', [toAddressObj, collectionId, tokenId, amount],929      true, `Unable to approve token for ${label}`,930    );931932    return this.helper.util.findCollectionInEvents(approveResult.result.events, collectionId, 'common', 'Approved', label);933  }934935  /**936   * Get the amount of token pieces approved to transfer937   * @param collectionId ID of collection938   * @param tokenId ID of token939   * @param toAccountObj 940   * @param fromAccountObj941   * @example getTokenApprovedPieces(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5ERZNF88Mm7UGfPP3mdG..."})942   * @returns number of approved to transfer pieces943   */944  async getTokenApprovedPieces(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId, fromAccountObj: ICrossAccountId): Promise<bigint> {945    return (await this.helper.callRpc('api.rpc.unique.allowance', [collectionId, fromAccountObj, toAccountObj, tokenId])).toBigInt();946  }947948  /**949   * Get the last created token id950   * @param collectionId ID of collection951   * @example getLastTokenId(10);952   * @returns id of the last created token953   */954  async getLastTokenId(collectionId: number): Promise<number> {955    return (await this.helper.callRpc('api.rpc.unique.lastTokenId', [collectionId])).toNumber();956  }957958  /**959   * Check if token exists960   * @param collectionId ID of collection961   * @param tokenId ID of token962   * @example isTokenExists(10, 20);963   * @returns true if the token exists, otherwise false964   */965  async isTokenExists(collectionId: number, tokenId: number): Promise<boolean> {966    return (await this.helper.callRpc('api.rpc.unique.tokenExists', [collectionId, tokenId])).toJSON();967  }968}969970class NFTnRFT extends CollectionGroup {971  /**972   * Get tokens owned by account973   * 974   * @param collectionId ID of collection975   * @param addressObj tokens owner976   * @example getTokensByAddress(10, {Substrate: "5DyN4Y92vZCjv38fg..."})977   * @returns array of token ids owned by account978   */979  async getTokensByAddress(collectionId: number, addressObj: ICrossAccountId): Promise<number[]> {980    return (await this.helper.callRpc('api.rpc.unique.accountTokens', [collectionId, addressObj])).toJSON();981  }982983  /**984   * Get token data985   * @param collectionId ID of collection986   * @param tokenId ID of token987   * @param blockHashAt 988   * @param propertyKeys989   * @example getToken(10, 5);990   * @returns human readable token data 991   */992  async getToken(collectionId: number, tokenId: number, blockHashAt?: string, propertyKeys?: string[]): Promise<{993    properties: IProperty[];994    owner: ICrossAccountId;995    normalizedOwner: ICrossAccountId;996  }| null> {997    let tokenData;998    if(typeof blockHashAt === 'undefined') {999      tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId]);1000    }1001    else {1002      if(typeof propertyKeys === 'undefined') {1003        const collection = (await this.helper.callRpc('api.rpc.unique.collectionById', [collectionId])).toHuman();1004        if(!collection) return null;1005        propertyKeys = collection.tokenPropertyPermissions.map((x: ITokenPropertyPermission) => x.key);1006      }1007      tokenData = await this.helper.callRpc('api.rpc.unique.tokenData', [collectionId, tokenId, propertyKeys, blockHashAt]);1008    }1009    tokenData = tokenData.toHuman();1010    if (tokenData === null || tokenData.owner === null) return null;1011    const owner = {} as any;1012    for (const key of Object.keys(tokenData.owner)) {1013      owner[key.toLocaleLowerCase()] = key.toLocaleLowerCase() === 'substrate' ? this.helper.address.normalizeSubstrate(tokenData.owner[key]) : tokenData.owner[key];1014    }1015    tokenData.normalizedOwner = crossAccountIdFromLower(owner);1016    return tokenData;1017  }10181019  /**1020   * Set permissions to change token properties1021   * @param signer keyring of signer1022   * @param collectionId ID of collection1023   * @param permissions permissions to change a property by the collection owner or admin1024   * @param label 1025   * @example setTokenPropertyPermissions(1026   *   aliceKeyring, 10, [{key: "gender", permission: {tokenOwner: true, mutable: true, collectionAdmin: true}}]1027   * )1028   * @returns true if extrinsic success otherwise false1029   */1030  async setTokenPropertyPermissions(signer: TSigner, collectionId: number, permissions: ITokenPropertyPermission[], label?: string): Promise<boolean> {1031    if(typeof label === 'undefined') label = `collection #${collectionId}`;1032    const result = await this.helper.executeExtrinsic(1033      signer,1034      'api.tx.unique.setTokenPropertyPermissions', [collectionId, permissions],1035      true, `Unable to set token property permissions for ${label}`,1036    );10371038    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'PropertyPermissionSet', label);1039  }10401041  /**1042   * Set token properties1043   * @param signer keyring of signer1044   * @param collectionId ID of collection1045   * @param tokenId ID of token1046   * @param properties 1047   * @param label 1048   * @example setTokenProperties(aliceKeyring, 10, 5, [{key: "gender", value: "female"}, {key: "age", value: "23"}])1049   * @returns ```true``` if extrinsic success, otherwise ```false```1050   */1051  async setTokenProperties(signer: TSigner, collectionId: number, tokenId: number, properties: IProperty[], label?: string): Promise<boolean> {1052    if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1053    const result = await this.helper.executeExtrinsic(1054      signer,1055      'api.tx.unique.setTokenProperties', [collectionId, tokenId, properties],1056      true, `Unable to set token properties for ${label}`,1057    );10581059    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertySet', label);1060  }10611062  /**1063   * Delete the provided properties of a token1064   * @param signer keyring of signer1065   * @param collectionId ID of collection1066   * @param tokenId ID of token1067   * @param propertyKeys property keys to be deleted 1068   * @param label 1069   * @example deleteTokenProperties(aliceKeyring, 10, 5, ["gender", "age"])1070   * @returns ```true``` if extrinsic success, otherwise ```false```1071   */1072  async deleteTokenProperties(signer: TSigner, collectionId: number, tokenId: number, propertyKeys: string[], label?: string): Promise<boolean> {1073    if(typeof label === 'undefined') label = `token #${tokenId} from collection #${collectionId}`;1074    const result = await this.helper.executeExtrinsic(1075      signer,1076      'api.tx.unique.deleteTokenProperties', [collectionId, tokenId, propertyKeys],1077      true, `Unable to delete token properties for ${label}`,1078    );10791080    return this.helper.util.findCollectionInEvents(result.result.events, collectionId, 'common', 'TokenPropertyDeleted', label);1081  }10821083  /**1084   * Mint new collection1085   * @param signer keyring of signer1086   * @param collectionOptions basic collection options and properties 1087   * @param mode NFT or RFT type of a collection1088   * @param errorLabel 1089   * @example mintCollection(aliceKeyring, {name: 'New', description: "New collection", tokenPrefix: "NEW"}, "NFT")1090   * @returns object of the created collection1091   */1092  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, mode: 'NFT' | 'RFT', errorLabel = 'Unable to mint collection'): Promise<UniqueCollectionBase> {1093    collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1094    collectionOptions.mode = (mode === 'NFT') ? {nft: null} : {refungible: null};1095    for (const key of ['name', 'description', 'tokenPrefix']) {1096      if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1097    }1098    const creationResult = await this.helper.executeExtrinsic(1099      signer,1100      'api.tx.unique.createCollectionEx', [collectionOptions],1101      true, errorLabel,1102    );1103    return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));1104  }11051106  getCollectionObject(collectionId: number): any {1107    return null;1108  }11091110  getTokenObject(collectionId: number, tokenId: number): any {1111    return null;1112  }1113}111411151116class NFTGroup extends NFTnRFT {1117  /**1118   * Get collection object1119   * @param collectionId ID of collection1120   * @example getCollectionObject(2);1121   * @returns instance of UniqueNFTCollection1122   */1123  getCollectionObject(collectionId: number): UniqueNFTCollection {1124    return new UniqueNFTCollection(collectionId, this.helper);1125  }11261127  /**1128   * Get token object1129   * @param collectionId ID of collection1130   * @param tokenId ID of token1131   * @example getTokenObject(10, 5);1132   * @returns instance of UniqueNFTToken1133   */1134  getTokenObject(collectionId: number, tokenId: number): UniqueNFTToken {1135    return new UniqueNFTToken(tokenId, this.getCollectionObject(collectionId));1136  }11371138  /**1139   * Get token's owner1140   * @param collectionId ID of collection1141   * @param tokenId ID of token1142   * @param blockHashAt 1143   * @example getTokenOwner(10, 5);1144   * @returns Address in CrossAccountId format, e.g. {Substrate: "5DnSF6RRjwteE3BrCj..."}1145   */1146  async getTokenOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId> {1147    let owner;1148    if (typeof blockHashAt === 'undefined') {1149      owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId]);1150    } else {1151      owner = await this.helper.callRpc('api.rpc.unique.tokenOwner', [collectionId, tokenId, blockHashAt]);1152    }1153    return crossAccountIdFromLower(owner.toJSON());1154  }11551156  /**1157   * Is token approved to transfer1158   * @param collectionId ID of collection1159   * @param tokenId ID of token1160   * @param toAccountObj address to be approved1161   * @returns ```true``` if extrinsic success, otherwise ```false```1162   */1163  async isTokenApproved(collectionId: number, tokenId: number, toAccountObj: ICrossAccountId): Promise<boolean> {1164    return (await this.getTokenApprovedPieces(collectionId, tokenId, toAccountObj, await this.getTokenOwner(collectionId, tokenId))) === 1n;1165  }11661167  /**1168   * Changes the owner of the token.1169   * 1170   * @param signer keyring of signer1171   * @param collectionId ID of collection1172   * @param tokenId ID of token1173   * @param addressObj address of a new owner1174   * @example transferToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1175   * @returns ```true``` if extrinsic success, otherwise ```false```1176   */1177  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<boolean> {1178    return await super.transferToken(signer, collectionId, tokenId, addressObj, 1n);1179  }11801181  /**1182   * 1183   * Change ownership of a NFT on behalf of the owner. 1184   * 1185   * @param signer keyring of signer1186   * @param collectionId ID of collection1187   * @param tokenId ID of token1188   * @param fromAddressObj address on behalf of which the token will be sent1189   * @param toAddressObj new token owner1190   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Ethereum: "0x9F0583DbB85..."})1191   * @returns ```true``` if extrinsic success, otherwise ```false```1192   */1193  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId): Promise<boolean> {1194    return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, 1n);1195  }11961197  /**1198   * Recursively find the address that owns the token1199   * @param collectionId ID of collection1200   * @param tokenId ID of token1201   * @param blockHashAt 1202   * @example getTokenTopmostOwner(10, 5);1203   * @returns address in CrossAccountId format, e.g. {Substrate: "5DyN4Y92vZCjv38fg..."}1204   */1205  async getTokenTopmostOwner(collectionId: number, tokenId: number, blockHashAt?: string): Promise<ICrossAccountId | null> {1206    let owner;1207    if (typeof blockHashAt === 'undefined') {1208      owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId]);1209    } else {1210      owner = await this.helper.callRpc('api.rpc.unique.topmostTokenOwner', [collectionId, tokenId, blockHashAt]);1211    }12121213    if (owner === null) return null;12141215    owner = owner.toHuman();12161217    return owner.Substrate ? {Substrate: this.helper.address.normalizeSubstrate(owner.Substrate)} : owner;1218  }12191220  /**1221   * Get tokens nested in the provided token1222   * @param collectionId ID of collection1223   * @param tokenId ID of token1224   * @param blockHashAt 1225   * @example getTokenChildren(10, 5);1226   * @returns tokens whose depth of nesting is <= 5 1227   */1228  async getTokenChildren(collectionId: number, tokenId: number, blockHashAt?: string): Promise<IToken[]> {1229    let children;1230    if(typeof blockHashAt === 'undefined') {1231      children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId]);1232    } else {1233      children = await this.helper.callRpc('api.rpc.unique.tokenChildren', [collectionId, tokenId, blockHashAt]);1234    }12351236    return children.toJSON().map((x: any) => {1237      return {collectionId: x.collection, tokenId: x.token};1238    });1239  }12401241  /**1242   * Nest one token into another1243   * @param signer keyring of signer1244   * @param tokenObj token to be nested1245   * @param rootTokenObj token to be parent1246   * @param label 1247   * @example nestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4});1248   * @returns ```true``` if extrinsic success, otherwise ```false```1249   */1250  async nestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, label='nest token'): Promise<boolean> {1251    const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1252    const result = await this.transferToken(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress);1253    if(!result) {1254      throw Error(`Unable to nest token for ${label}`);1255    }1256    return result;1257  }12581259  /**1260   * Remove token from nested state1261   * @param signer keyring of signer1262   * @param tokenObj token to unnest1263   * @param rootTokenObj parent of a token1264   * @param toAddressObj address of a new token owner 1265   * @param label 1266   * @example unnestToken(aliceKeyring, {collectionId: 10, tokenId: 5}, {collectionId: 10, tokenId: 4}, {Substrate: "5DyN4Y92vZCjv38fg..."});1267   * @returns ```true``` if extrinsic success, otherwise ```false```1268   */1269  async unnestToken(signer: TSigner, tokenObj: IToken, rootTokenObj: IToken, toAddressObj: ICrossAccountId, label='unnest token'): Promise<boolean> {1270    const rootTokenAddress = {Ethereum: this.helper.util.getNestingTokenAddress(rootTokenObj.collectionId, rootTokenObj.tokenId)};1271    const result = await this.transferTokenFrom(signer, tokenObj.collectionId, tokenObj.tokenId, rootTokenAddress, toAddressObj);1272    if(!result) {1273      throw Error(`Unable to unnest token for ${label}`);1274    }1275    return result;1276  }12771278  /**1279   * Mint new collection1280   * @param signer keyring of signer1281   * @param collectionOptions Collection options1282   * @param label 1283   * @example 1284   * mintCollection(aliceKeyring, {1285   *   name: 'New',1286   *   description: 'New collection',1287   *   tokenPrefix: 'NEW',1288   * })1289   * @returns object of the created collection1290   */1291  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueNFTCollection> {1292    return await super.mintCollection(signer, collectionOptions, 'NFT', `Unable to mint NFT collection for ${label}`) as UniqueNFTCollection;1293  }12941295  /**1296   * Mint new token1297   * @param signer keyring of signer1298   * @param data token data1299   * @param label 1300   * @returns created token object1301   */1302  async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; properties?: IProperty[]; }, label?: string): Promise<UniqueNFTToken> {1303    if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1304    const creationResult = await this.helper.executeExtrinsic(1305      signer,1306      'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1307        nft: {1308          properties: data.properties,1309        },1310      }],1311      true, `Unable to mint NFT token for ${label}`,1312    );1313    const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult, label);1314    if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1315    if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1316    return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1317  }13181319  /**1320   * Mint multiple NFT tokens1321   * @param signer keyring of signer1322   * @param collectionId ID of collection1323   * @param tokens array of tokens with owner and properties1324   * @param label 1325   * @example 1326   * mintMultipleTokens(aliceKeyring, 10, [{1327   *     owner: {Substrate: "5DyN4Y92vZCjv38fg..."},1328   *     properties: [{key: "gender", value: "male"},{key: "age", value: "45"}],1329   *   },{1330   *     owner: {Ethereum: "0x9F0583DbB855d..."},1331   *     properties: [{key: "gender", value: "female"},{key: "age", value: "22"}],1332   * }]);1333   * @returns ```true``` if extrinsic success, otherwise ```false```1334   */1335  async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1336    if(typeof label === 'undefined') label = `collection #${collectionId}`;1337    const creationResult = await this.helper.executeExtrinsic(1338      signer,1339      'api.tx.unique.createMultipleItemsEx', [collectionId, {NFT: tokens}],1340      true, `Unable to mint NFT tokens for ${label}`,1341    );1342    const collection = this.getCollectionObject(collectionId);1343    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1344  }13451346  /**1347   * Mint multiple NFT tokens with one owner1348   * @param signer keyring of signer1349   * @param collectionId ID of collection1350   * @param owner tokens owner1351   * @param tokens array of tokens with owner and properties1352   * @param label 1353   * @example1354   * mintMultipleTokensWithOneOwner(aliceKeyring, 10, "5DyN4Y92vZCjv38fg...", [{1355   *   properties: [{1356   *   key: "gender",1357   *   value: "female",1358   *  },{1359   *   key: "age",1360   *   value: "33",1361   *  }],1362   * }]);1363   * @returns array of newly created tokens1364   */1365  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {properties?: IProperty[]}[], label?: string): Promise<UniqueNFTToken[]> {1366    if(typeof label === 'undefined') label = `collection #${collectionId}`;1367    const rawTokens = [];1368    for (const token of tokens) {1369      const raw = {NFT: {properties: token.properties}};1370      rawTokens.push(raw);1371    }1372    const creationResult = await this.helper.executeExtrinsic(1373      signer,1374      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1375      true, `Unable to mint NFT tokens for ${label}`,1376    );1377    const collection = this.getCollectionObject(collectionId);1378    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1379  }13801381  /**1382   * Destroys a concrete instance of NFT.1383   * @param signer keyring of signer1384   * @param collectionId ID of collection1385   * @param tokenId ID of token1386   * @param label 1387   * @example burnToken(aliceKeyring, 10, 5);1388   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```1389   */1390  async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string): Promise<{ success: boolean; token: number | null; }> {1391    return await super.burnToken(signer, collectionId, tokenId, label, 1n);1392  }13931394  /**1395   * Set, change, or remove approved address to transfer the ownership of the NFT.1396   * 1397   * @param signer keyring of signer1398   * @param collectionId ID of collection1399   * @param tokenId ID of token1400   * @param toAddressObj address to approve1401   * @param label 1402   * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."})1403   * @returns ```true``` if extrinsic success, otherwise ```false```1404   */1405  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {1406    return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, 1n);1407  }1408}140914101411class RFTGroup extends NFTnRFT {1412  /**1413   * Get collection object1414   * @param collectionId ID of collection1415   * @example getCollectionObject(2);1416   * @returns instance of UniqueRFTCollection1417   */1418  getCollectionObject(collectionId: number): UniqueRFTCollection {1419    return new UniqueRFTCollection(collectionId, this.helper);1420  }14211422  /**1423   * Get token object1424   * @param collectionId ID of collection1425   * @param tokenId ID of token1426   * @example getTokenObject(10, 5);1427   * @returns instance of UniqueNFTToken1428   */1429  getTokenObject(collectionId: number, tokenId: number): UniqueRFTToken {1430    return new UniqueRFTToken(tokenId, this.getCollectionObject(collectionId));1431  }14321433  /**1434   * Get top 10 token owners with the largest number of pieces 1435   * @param collectionId ID of collection1436   * @param tokenId ID of token1437   * @example getTokenTop10Owners(10, 5);1438   * @returns array of top 10 owners1439   */1440  async getTokenTop10Owners(collectionId: number, tokenId: number): Promise<ICrossAccountId[]> {1441    return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, tokenId])).toJSON().map(crossAccountIdFromLower);1442  }14431444  /**1445   * Get number of pieces owned by address1446   * @param collectionId ID of collection1447   * @param tokenId ID of token1448   * @param addressObj address token owner1449   * @example getTokenBalance(10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."});1450   * @returns number of pieces ownerd by address1451   */1452  async getTokenBalance(collectionId: number, tokenId: number, addressObj: ICrossAccountId): Promise<bigint> {1453    return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, tokenId])).toBigInt();1454  }14551456  /**1457   * Transfer pieces of token to another address1458   * @param signer keyring of signer1459   * @param collectionId ID of collection1460   * @param tokenId ID of token1461   * @param addressObj address of a new owner1462   * @param amount number of pieces to be transfered1463   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, 2000n)1464   * @returns ```true``` if extrinsic success, otherwise ```false```1465   */1466  async transferToken(signer: TSigner, collectionId: number, tokenId: number, addressObj: ICrossAccountId, amount=100n): Promise<boolean> {1467    return await super.transferToken(signer, collectionId, tokenId, addressObj, amount);1468  }14691470  /**1471   * Change ownership of some pieces of RFT on behalf of the owner. 1472   * @param signer keyring of signer1473   * @param collectionId ID of collection1474   * @param tokenId ID of token1475   * @param fromAddressObj address on behalf of which the token will be sent1476   * @param toAddressObj new token owner1477   * @param amount number of pieces to be transfered1478   * @example transferTokenFrom(aliceKeyring, 10, 5, {Substrate: "5DyN4Y92vZCjv38fg..."}, {Substrate: "5DfhbVfww7ThF8q6f3i..."}, 2000n)1479   * @returns ```true``` if extrinsic success, otherwise ```false```1480   */1481  async transferTokenFrom(signer: TSigner, collectionId: number, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n): Promise<boolean> {1482    return await super.transferTokenFrom(signer, collectionId, tokenId, fromAddressObj, toAddressObj, amount);1483  }14841485  /**1486   * Mint new collection1487   * @param signer keyring of signer1488   * @param collectionOptions Collection options1489   * @param label 1490   * @example1491   * mintCollection(aliceKeyring, {1492   *   name: 'New',1493   *   description: 'New collection',1494   *   tokenPrefix: 'NEW',1495   * })1496   * @returns object of the created collection1497   */1498  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, label = 'new collection'): Promise<UniqueRFTCollection> {1499    return await super.mintCollection(signer, collectionOptions, 'RFT', `Unable to mint RFT collection for ${label}`) as UniqueRFTCollection;1500  }15011502  /**1503   * Mint new token1504   * @param signer keyring of signer1505   * @param data token data1506   * @param label 1507   * @example mintToken(aliceKeyring, {collectionId: 10, owner: {Substrate: '5GHoZe9c73RYbVzq...'}, pieces: 10000n});1508   * @returns created token object1509   */1510  async mintToken(signer: TSigner, data: { collectionId: number; owner: ICrossAccountId | string; pieces: bigint; properties?: IProperty[]; }, label?: string): Promise<UniqueRFTToken> {1511    if(typeof label === 'undefined') label = `collection #${data.collectionId}`;1512    const creationResult = await this.helper.executeExtrinsic(1513      signer,1514      'api.tx.unique.createItem', [data.collectionId, (typeof data.owner === 'string') ? {Substrate: data.owner} : data.owner, {1515        refungible: {1516          pieces: data.pieces,1517          properties: data.properties,1518        },1519      }],1520      true, `Unable to mint RFT token for ${label}`,1521    );1522    const createdTokens = this.helper.util.extractTokensFromCreationResult(creationResult, label);1523    if (createdTokens.tokens.length > 1) throw Error('Minted multiple tokens');1524    if (createdTokens.tokens.length < 1) throw Error('No tokens minted');1525    return this.getTokenObject(data.collectionId, createdTokens.tokens[0].tokenId);1526  }15271528  async mintMultipleTokens(signer: TSigner, collectionId: number, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {1529    throw Error('Not implemented');1530    if(typeof label === 'undefined') label = `collection #${collectionId}`;1531    const creationResult = await this.helper.executeExtrinsic(1532      signer,1533      'api.tx.unique.createMultipleItemsEx', [collectionId, {RefungibleMultipleOwners: tokens}],1534      true, `Unable to mint RFT tokens for ${label}`,1535    );1536    const collection = this.getCollectionObject(collectionId);1537    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1538  }15391540  /**1541   * Mint multiple RFT tokens with one owner1542   * @param signer keyring of signer1543   * @param collectionId ID of collection1544   * @param owner tokens owner1545   * @param tokens array of tokens with properties and pieces1546   * @param label 1547   * @example mintMultipleTokensWithOneOwner(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, [{pieces: 100000n, properties: [{key: "gender", value: "male"}]}]);1548   * @returns array of newly created RFT tokens1549   */1550  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {pieces: bigint, properties?: IProperty[]}[], label?: string): Promise<UniqueRFTToken[]> {1551    if(typeof label === 'undefined') label = `collection #${collectionId}`;1552    const rawTokens = [];1553    for (const token of tokens) {1554      const raw = {ReFungible: {pieces: token.pieces, properties: token.properties}};1555      rawTokens.push(raw);1556    }1557    const creationResult = await this.helper.executeExtrinsic(1558      signer,1559      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1560      true, `Unable to mint RFT tokens for ${label}`,1561    );1562    const collection = this.getCollectionObject(collectionId);1563    return this.helper.util.extractTokensFromCreationResult(creationResult, label).tokens.map((x: IToken) => collection.getTokenObject(x.tokenId));1564  }15651566  /**1567   * Destroys a concrete instance of RFT.1568   * @param signer keyring of signer1569   * @param collectionId ID of collection1570   * @param tokenId ID of token1571   * @param label 1572   * @param amount number of pieces to be burnt1573   * @example burnToken(aliceKeyring, 10, 5);1574   * @returns ```true``` and burnt token number is extrinsic success. Otherwise ```false``` and ```null```1575   */1576  async burnToken(signer: IKeyringPair, collectionId: number, tokenId: number, label?: string, amount=100n): Promise<{ success: boolean; token: number | null; }> {1577    return await super.burnToken(signer, collectionId, tokenId, label, amount);1578  }15791580  /**1581   * Set, change, or remove approved address to transfer the ownership of the RFT.1582   * 1583   * @param signer keyring of signer1584   * @param collectionId ID of collection1585   * @param tokenId ID of token1586   * @param toAddressObj address to approve1587   * @param label 1588   * @param amount number of pieces to be approved1589   * @example approveToken(aliceKeyring, 10, 5, {Substrate: "5GHoZe9c73RYbVzq..."}, "", 10000n);1590   * @returns true if the token success, otherwise false1591   */1592  async approveToken(signer: IKeyringPair, collectionId: number, tokenId: number, toAddressObj: ICrossAccountId, label?: string, amount=100n) {1593    return super.approveToken(signer, collectionId, tokenId, toAddressObj, label, amount);1594  }15951596  /**1597   * Get total number of pieces1598   * @param collectionId ID of collection1599   * @param tokenId ID of token1600   * @example getTokenTotalPieces(10, 5);1601   * @returns number of pieces1602   */1603  async getTokenTotalPieces(collectionId: number, tokenId: number): Promise<bigint> {1604    return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, tokenId])).unwrap().toBigInt();1605  }16061607  /**1608   * Change number of token pieces. Signer must be the owner of all token pieces.1609   * @param signer keyring of signer1610   * @param collectionId ID of collection1611   * @param tokenId ID of token1612   * @param amount new number of pieces1613   * @param label 1614   * @example repartitionToken(aliceKeyring, 10, 5, 12345n);1615   * @returns true if the repartion was success, otherwise false1616   */1617  async repartitionToken(signer: TSigner, collectionId: number, tokenId: number, amount: bigint, label?: string): Promise<boolean> {1618    if(typeof label === 'undefined') label = `collection #${collectionId}`;1619    const currentAmount = await this.getTokenTotalPieces(collectionId, tokenId);1620    const repartitionResult = await this.helper.executeExtrinsic(1621      signer,1622      'api.tx.unique.repartition', [collectionId, tokenId, amount],1623      true, `Unable to repartition RFT token for ${label}`,1624    );1625    if(currentAmount < amount) return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemCreated', label);1626    return this.helper.util.findCollectionInEvents(repartitionResult.result.events, collectionId, 'common', 'ItemDestroyed', label);1627  }1628}162916301631class FTGroup extends CollectionGroup {1632  /**1633   * Get collection object1634   * @param collectionId ID of collection1635   * @example getCollectionObject(2);1636   * @returns instance of UniqueFTCollection1637   */1638  getCollectionObject(collectionId: number): UniqueFTCollection {1639    return new UniqueFTCollection(collectionId, this.helper);1640  }16411642  /**1643   * Mint new fungible collection1644   * @param signer keyring of signer1645   * @param collectionOptions Collection options1646   * @param decimalPoints number of token decimals 1647   * @param errorLabel 1648   * @example1649   * mintCollection(aliceKeyring, {1650   *   name: 'New',1651   *   description: 'New collection',1652   *   tokenPrefix: 'NEW',1653   * }, 18)1654   * @returns newly created fungible collection1655   */1656  async mintCollection(signer: TSigner, collectionOptions: ICollectionCreationOptions, decimalPoints = 0, errorLabel = 'Unable to mint collection'): Promise<UniqueFTCollection> {1657    collectionOptions = JSON.parse(JSON.stringify(collectionOptions)) as ICollectionCreationOptions; // Clone object1658    if(collectionOptions.tokenPropertyPermissions) throw Error('Fungible collections has no tokenPropertyPermissions');1659    collectionOptions.mode = {fungible: decimalPoints};1660    for (const key of ['name', 'description', 'tokenPrefix']) {1661      if (typeof collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] === 'string') collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] = this.helper.util.str2vec(collectionOptions[key as 'name' | 'description' | 'tokenPrefix'] as string);1662    }1663    const creationResult = await this.helper.executeExtrinsic(1664      signer,1665      'api.tx.unique.createCollectionEx', [collectionOptions],1666      true, errorLabel,1667    );1668    return this.getCollectionObject(this.helper.util.extractCollectionIdFromCreationResult(creationResult, errorLabel));1669  }16701671  /**1672   * Mint tokens1673   * @param signer keyring of signer1674   * @param collectionId ID of collection1675   * @param owner address owner of new tokens1676   * @param amount amount of tokens to be meanted1677   * @param label 1678   * @example mintTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq"}, 1000n);1679   * @returns ```true``` if extrinsic success, otherwise ```false``` 1680   */1681  async mintTokens(signer: TSigner, collectionId: number, owner: ICrossAccountId | string, amount: bigint, label?: string): Promise<boolean> {1682    if(typeof label === 'undefined') label = `collection #${collectionId}`;1683    const creationResult = await this.helper.executeExtrinsic(1684      signer,1685      'api.tx.unique.createItem', [collectionId, (typeof owner === 'string') ? {Substrate: owner} : owner, {1686        fungible: {1687          value: amount,1688        },1689      }],1690      true, `Unable to mint fungible tokens for ${label}`,1691    );1692    return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1693  }16941695  /**1696   * Mint multiple Fungible tokens with one owner1697   * @param signer keyring of signer1698   * @param collectionId ID of collection1699   * @param owner tokens owner1700   * @param tokens array of tokens with properties and pieces1701   * @param label 1702   * @returns ```true``` if extrinsic success, otherwise ```false``` 1703   */1704  async mintMultipleTokensWithOneOwner(signer: TSigner, collectionId: number, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string): Promise<boolean> {1705    if(typeof label === 'undefined') label = `collection #${collectionId}`;1706    const rawTokens = [];1707    for (const token of tokens) {1708      const raw = {Fungible: {Value: token.value}};1709      rawTokens.push(raw);1710    }1711    const creationResult = await this.helper.executeExtrinsic(1712      signer,1713      'api.tx.unique.createMultipleItems', [collectionId, owner, rawTokens],1714      true, `Unable to mint RFT tokens for ${label}`,1715    );1716    return this.helper.util.findCollectionInEvents(creationResult.result.events, collectionId, 'common', 'ItemCreated', label);1717  }17181719  /**1720   * Get the top 10 owners with the largest balance for the Fungible collection 1721   * @param collectionId ID of collection1722   * @example getTop10Owners(10);1723   * @returns array of ```ICrossAccountId```1724   */1725  async getTop10Owners(collectionId: number): Promise<ICrossAccountId[]> {1726    return (await this.helper.callRpc('api.rpc.unique.tokenOwners', [collectionId, 0])).toJSON().map(crossAccountIdFromLower);1727  }17281729  /**1730   * Get account balance1731   * @param collectionId ID of collection1732   * @param addressObj address of owner1733   * @example getBalance(10, {Substrate: "5GHoZe9c73RYbVzq..."})1734   * @returns amount of fungible tokens owned by address1735   */1736  async getBalance(collectionId: number, addressObj: ICrossAccountId): Promise<bigint> {1737    return (await this.helper.callRpc('api.rpc.unique.balance', [collectionId, addressObj, 0])).toBigInt();1738  }17391740  /**1741   * Transfer tokens to address1742   * @param signer keyring of signer1743   * @param collectionId ID of collection1744   * @param toAddressObj address recepient1745   * @param amount amount of tokens to be sent1746   * @example transfer(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);1747   * @returns ```true``` if extrinsic success, otherwise ```false``` 1748   */1749  async transfer(signer: TSigner, collectionId: number, toAddressObj: ICrossAccountId, amount: bigint) {1750    return await super.transferToken(signer, collectionId, 0, toAddressObj, amount);1751  }17521753  /**1754   * Transfer some tokens on behalf of the owner.1755   * @param signer keyring of signer1756   * @param collectionId ID of collection1757   * @param fromAddressObj address on behalf of which tokens will be sent1758   * @param toAddressObj address where token to be sent1759   * @param amount number of tokens to be sent1760   * @example transferFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, {Substrate: "5DfhbVfww7ThF8q6f3ij..."}, 10000n);1761   * @returns ```true``` if extrinsic success, otherwise ```false``` 1762   */1763  async transferFrom(signer: TSigner, collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {1764    return await super.transferTokenFrom(signer, collectionId, 0, fromAddressObj, toAddressObj, amount);1765  }17661767  /**1768   * Destroy some amount of tokens1769   * @param signer keyring of signer1770   * @param collectionId ID of collection1771   * @param amount amount of tokens to be destroyed1772   * @param label 1773   * @example burnTokens(aliceKeyring, 10, 1000n);1774   * @returns ```true``` if extrinsic success, otherwise ```false``` 1775   */1776  async burnTokens(signer: IKeyringPair, collectionId: number, amount=100n, label?: string): Promise<boolean> {1777    return (await super.burnToken(signer, collectionId, 0, label, amount)).success;1778  }17791780  /**1781   * Burn some tokens on behalf of the owner.1782   * @param signer keyring of signer1783   * @param collectionId ID of collection1784   * @param fromAddressObj address on behalf of which tokens will be burnt1785   * @param amount amount of tokens to be burnt1786   * @param label 1787   * @example burnTokensFrom(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n);1788   * @returns ```true``` if extrinsic success, otherwise ```false``` 1789   */1790  async burnTokensFrom(signer: IKeyringPair, collectionId: number, fromAddressObj: ICrossAccountId, amount=100n, label?: string): Promise<boolean> {1791    return await super.burnTokenFrom(signer, collectionId, fromAddressObj, 0, label, amount);1792  }17931794  /**1795   * Get total collection supply1796   * @param collectionId 1797   * @returns 1798   */1799  async getTotalPieces(collectionId: number): Promise<bigint> {1800    return (await this.helper.callRpc('api.rpc.unique.totalPieces', [collectionId, 0])).unwrap().toBigInt();1801  }18021803  /**1804   * Set, change, or remove approved address to transfer tokens.1805   * 1806   * @param signer keyring of signer1807   * @param collectionId ID of collection1808   * @param toAddressObj address to be approved1809   * @param amount amount of tokens to be approved1810   * @param label 1811   * @example approveTokens(aliceKeyring, 10, {Substrate: "5GHoZe9c73RYbVzq..."}, 1000n)1812   * @returns ```true``` if extrinsic success, otherwise ```false``` 1813   */1814  async approveTokens(signer: IKeyringPair, collectionId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {1815    return super.approveToken(signer, collectionId, 0, toAddressObj, label, amount);1816  }18171818  /**1819   * Get amount of fungible tokens approved to transfer1820   * @param collectionId ID of collection1821   * @param fromAddressObj owner of tokens1822   * @param toAddressObj the address approved for the transfer of tokens on behalf of the owner1823   * @returns number of tokens approved for the transfer1824   */1825  async getApprovedTokens(collectionId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {1826    return super.getTokenApprovedPieces(collectionId, 0, toAddressObj, fromAddressObj);1827  }1828}182918301831class ChainGroup extends HelperGroup {1832  /**1833   * Get system properties of a chain1834   * @example getChainProperties();1835   * @returns ss58Format, token decimals, and token symbol1836   */1837  getChainProperties(): IChainProperties {1838    const properties = (this.helper.api as any).registry.getChainProperties().toJSON();1839    return {1840      ss58Format: properties.ss58Format.toJSON(),1841      tokenDecimals: properties.tokenDecimals.toJSON(),1842      tokenSymbol: properties.tokenSymbol.toJSON(),1843    };1844  }18451846  /**1847   * Get chain header1848   * @example getLatestBlockNumber();1849   * @returns the number of the last block1850   */1851  async getLatestBlockNumber(): Promise<number> {1852    return (await this.helper.callRpc('api.rpc.chain.getHeader')).number.toNumber();1853  }18541855  /**1856   * Get block hash by block number1857   * @param blockNumber number of block1858   * @example getBlockHashByNumber(12345);1859   * @returns hash of a block1860   */1861  async getBlockHashByNumber(blockNumber: number): Promise<string | null> {1862    const blockHash = (await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])).toJSON();1863    if(blockHash === '0x0000000000000000000000000000000000000000000000000000000000000000') return null;1864    return blockHash;1865  }18661867  /**1868   * Get account nonce1869   * @param address substrate address1870   * @example getNonce("5GrwvaEF5zXb26Fz...");1871   * @returns number, account's nonce1872   */1873  async getNonce(address: TSubstrateAccount): Promise<number> {1874    return (await (this.helper.api as any).query.system.account(address)).nonce.toNumber();1875  }1876}187718781879class BalanceGroup extends HelperGroup {1880  /**1881   * Representation of the native token in the smallest unit1882   * @example getOneTokenNominal()1883   * @returns ```BigInt``` representation of the native token in the smallest unit, e.g. ```1_000_000_000_000_000_000n``` for QTZ.1884   */1885  getOneTokenNominal(): bigint {1886    const chainProperties = this.helper.chain.getChainProperties();1887    return 10n ** BigInt((chainProperties.tokenDecimals || [18])[0]);1888  }18891890  /**1891   * Get substrate address balance1892   * @param address substrate address1893   * @example getSubstrate("5GrwvaEF5zXb26Fz...")1894   * @returns amount of tokens on address1895   */1896  async getSubstrate(address: TSubstrateAccount): Promise<bigint> {1897    return (await this.helper.callRpc('api.query.system.account', [address])).data.free.toBigInt();1898  }18991900  /**1901   * Get ethereum address balance1902   * @param address ethereum address1903   * @example getEthereum("0x9F0583DbB855d...")1904   * @returns amount of tokens on address1905   */1906  async getEthereum(address: TEthereumAccount): Promise<bigint> {1907    return (await this.helper.callRpc('api.rpc.eth.getBalance', [address])).toBigInt();1908  }19091910  /**1911   * Transfer tokens to substrate address1912   * @param signer keyring of signer1913   * @param address substrate address of a recepient1914   * @param amount amount of tokens to be transfered1915   * @example transferToSubstrate(aliceKeyring, "5GrwvaEF5zXb26Fz...", 100_000_000_000n);1916   * @returns ```true``` if extrinsic success, otherwise ```false```1917   */1918  async transferToSubstrate(signer: TSigner, address: TSubstrateAccount, amount: bigint | string): Promise<boolean> {1919    const result = await this.helper.executeExtrinsic(signer, 'api.tx.balances.transfer', [address, amount], true, `Unable to transfer balance from ${this.helper.getSignerAddress(signer)} to ${address}`);19201921    let transfer = {from: null, to: null, amount: 0n} as any;1922    result.result.events.forEach(({event: {data, method, section}}) => {1923      if ((section === 'balances') && (method === 'Transfer')) {1924        transfer = {1925          from: this.helper.address.normalizeSubstrate(data[0]),1926          to: this.helper.address.normalizeSubstrate(data[1]),1927          amount: BigInt(data[2]),1928        };1929      }1930    });1931    let isSuccess = this.helper.address.normalizeSubstrate(typeof signer === 'string' ? signer : signer.address) === transfer.from;1932    isSuccess = isSuccess && this.helper.address.normalizeSubstrate(address) === transfer.to;1933    isSuccess = isSuccess && BigInt(amount) === transfer.amount;1934    return isSuccess;1935  }1936}193719381939class AddressGroup extends HelperGroup {1940  /**1941   * Normalizes the address to the specified ss58 format, by default ```42```.1942   * @param address substrate address1943   * @param ss58Format format for address conversion, by default ```42```1944   * @example normalizeSubstrate("unjKJQJrRd238pkUZZvzDQrfKuM39zBSnQ5zjAGAGcdRhaJTx") // returns 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY1945   * @returns substrate address converted to normalized (i.e., starting with 5) or specified explicitly representation1946   */1947  normalizeSubstrate(address: TSubstrateAccount, ss58Format = 42): TSubstrateAccount {1948    return this.helper.util.normalizeSubstrateAddress(address, ss58Format);1949  }19501951  /**1952   * Get address in the connected chain format1953   * @param address substrate address1954   * @example normalizeSubstrateToChainFormat("5GrwvaEF5zXb26Fz...") // returns unjKJQJrRd238pkUZZ... for Unique Network1955   * @returns address in chain format1956   */1957  async normalizeSubstrateToChainFormat(address: TSubstrateAccount): Promise<TSubstrateAccount> {1958    const info = this.helper.chain.getChainProperties();1959    return encodeAddress(decodeAddress(address), info.ss58Format);1960  }19611962  /**1963   * Get substrate mirror of an ethereum address1964   * @param ethAddress ethereum address1965   * @param toChainFormat false for normalized account1966   * @example ethToSubstrate('0x9F0583DbB855d...')1967   * @returns substrate mirror of a provided ethereum address1968   */1969  async ethToSubstrate(ethAddress: TEthereumAccount, toChainFormat=false): Promise<TSubstrateAccount> {1970    if(!toChainFormat) return evmToAddress(ethAddress);1971    const info = this.helper.chain.getChainProperties();1972    return evmToAddress(ethAddress, info.ss58Format);1973  }19741975  /**1976   * Get ethereum mirror of a substrate address1977   * @param subAddress substrate account1978   * @example substrateToEth("5DnSF6RRjwteE3BrC...")1979   * @returns ethereum mirror of a provided substrate address1980   */1981  substrateToEth(subAddress: TSubstrateAccount): TEthereumAccount {1982    return nesting.toChecksumAddress('0x' + Array.from(addressToEvm(subAddress), i => i.toString(16).padStart(2, '0')).join(''));1983  }1984}198519861987export class UniqueHelper extends ChainHelperBase {1988  chain: ChainGroup;1989  balance: BalanceGroup;1990  address: AddressGroup;1991  collection: CollectionGroup;1992  nft: NFTGroup;1993  rft: RFTGroup;1994  ft: FTGroup;19951996  constructor(logger?: ILogger) {1997    super(logger);1998    this.chain = new ChainGroup(this);1999    this.balance = new BalanceGroup(this);2000    this.address = new AddressGroup(this);2001    this.collection = new CollectionGroup(this);2002    this.nft = new NFTGroup(this);2003    this.rft = new RFTGroup(this);2004    this.ft = new FTGroup(this);2005  }  2006}200720082009class UniqueCollectionBase {2010  helper: UniqueHelper;2011  collectionId: number;20122013  constructor(collectionId: number, uniqueHelper: UniqueHelper) {2014    this.collectionId = collectionId;2015    this.helper = uniqueHelper;2016  }20172018  async getData() {2019    return await this.helper.collection.getData(this.collectionId);2020  }20212022  async getLastTokenId() {2023    return await this.helper.collection.getLastTokenId(this.collectionId);2024  }20252026  async isTokenExists(tokenId: number) {2027    return await this.helper.collection.isTokenExists(this.collectionId, tokenId);2028  }20292030  async getAdmins() {2031    return await this.helper.collection.getAdmins(this.collectionId);2032  }20332034  async getEffectiveLimits() {2035    return await this.helper.collection.getEffectiveLimits(this.collectionId);2036  }20372038  async setSponsor(signer: TSigner, sponsorAddress: TSubstrateAccount, label?: string) {2039    return await this.helper.collection.setSponsor(signer, this.collectionId, sponsorAddress, label);2040  }20412042  async confirmSponsorship(signer: TSigner, label?: string) {2043    return await this.helper.collection.confirmSponsorship(signer, this.collectionId, label);2044  }20452046  async setLimits(signer: TSigner, limits: ICollectionLimits, label?: string) {2047    return await this.helper.collection.setLimits(signer, this.collectionId, limits, label);2048  }20492050  async changeOwner(signer: TSigner, ownerAddress: TSubstrateAccount, label?: string) {2051    return await this.helper.collection.changeOwner(signer, this.collectionId, ownerAddress, label);2052  }20532054  async addAdmin(signer: TSigner, adminAddressObj: ICrossAccountId, label?: string) {2055    return await this.helper.collection.addAdmin(signer, this.collectionId, adminAddressObj, label);2056  }20572058  async removeAdmin(signer: TSigner, adminAddressObj: ICrossAccountId, label?: string) {2059    return await this.helper.collection.removeAdmin(signer, this.collectionId, adminAddressObj, label);2060  }20612062  async setProperties(signer: TSigner, properties: IProperty[], label?: string) {2063    return await this.helper.collection.setProperties(signer, this.collectionId, properties, label);2064  }20652066  async deleteProperties(signer: TSigner, propertyKeys: string[], label?: string) {2067    return await this.helper.collection.deleteProperties(signer, this.collectionId, propertyKeys, label);2068  }20692070  async getTokenNextSponsored(tokenId: number, addressObj: ICrossAccountId) {2071    return await this.helper.collection.getTokenNextSponsored(this.collectionId, tokenId, addressObj);2072  }20732074  async setPermissions(signer: TSigner, permissions: ICollectionPermissions, label?: string) {2075    return await this.helper.collection.setPermissions(signer, this.collectionId, permissions, label);2076  }20772078  async enableNesting(signer: TSigner, permissions: INestingPermissions, label?: string) {2079    return await this.helper.collection.enableNesting(signer, this.collectionId, permissions, label);2080  }20812082  async disableNesting(signer: TSigner, label?: string) {2083    return await this.helper.collection.disableNesting(signer, this.collectionId, label);2084  }20852086  async burn(signer: TSigner, label?: string) {2087    return await this.helper.collection.burn(signer, this.collectionId, label);2088  }2089}209020912092class UniqueNFTCollection extends UniqueCollectionBase {2093  getTokenObject(tokenId: number) {2094    return new UniqueNFTToken(tokenId, this);2095  }20962097  async getTokensByAddress(addressObj: ICrossAccountId) {2098    return await this.helper.nft.getTokensByAddress(this.collectionId, addressObj);2099  }21002101  async getToken(tokenId: number, blockHashAt?: string) {2102    return await this.helper.nft.getToken(this.collectionId, tokenId, blockHashAt);2103  }21042105  async getTokenOwner(tokenId: number, blockHashAt?: string) {2106    return await this.helper.nft.getTokenOwner(this.collectionId, tokenId, blockHashAt);2107  }21082109  async getTokenTopmostOwner(tokenId: number, blockHashAt?: string) {2110    return await this.helper.nft.getTokenTopmostOwner(this.collectionId, tokenId, blockHashAt);2111  }21122113  async getTokenChildren(tokenId: number, blockHashAt?: string) {2114    return await this.helper.nft.getTokenChildren(this.collectionId, tokenId, blockHashAt);2115  }21162117  async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId) {2118    return await this.helper.nft.transferToken(signer, this.collectionId, tokenId, addressObj);2119  }21202121  async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2122    return await this.helper.nft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj);2123  }21242125  async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, label?: string) {2126    return await this.helper.nft.approveToken(signer, this.collectionId, tokenId, toAddressObj, label);2127  }21282129  async isTokenApproved(tokenId: number, toAddressObj: ICrossAccountId) {2130    return await this.helper.nft.isTokenApproved(this.collectionId, tokenId, toAddressObj);2131  }21322133  async mintToken(signer: TSigner, owner: ICrossAccountId, properties?: IProperty[], label?: string) {2134    return await this.helper.nft.mintToken(signer, {collectionId: this.collectionId, owner, properties}, label);2135  }21362137  async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, properties?: IProperty[]}[], label?: string) {2138    return await this.helper.nft.mintMultipleTokens(signer, this.collectionId, tokens, label);2139  }21402141  async burnToken(signer: TSigner, tokenId: number, label?: string) {2142    return await this.helper.nft.burnToken(signer, this.collectionId, tokenId, label);2143  }21442145  async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[], label?: string) {2146    return await this.helper.nft.setTokenProperties(signer, this.collectionId, tokenId, properties, label);2147  }21482149  async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[], label?: string) {2150    return await this.helper.nft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys, label);2151  }21522153  async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[], label?: string) {2154    return await this.helper.nft.setTokenPropertyPermissions(signer, this.collectionId, permissions, label);2155  }21562157  async nestToken(signer: TSigner, tokenId: number, toTokenObj: IToken, label?: string) {2158    return await this.helper.nft.nestToken(signer, {collectionId: this.collectionId, tokenId}, toTokenObj, label);2159  }21602161  async unnestToken(signer: TSigner, tokenId: number, fromTokenObj: IToken, toAddressObj: ICrossAccountId, label?: string) {2162    return await this.helper.nft.unnestToken(signer, {collectionId: this.collectionId, tokenId}, fromTokenObj, toAddressObj, label);2163  }2164}216521662167class UniqueRFTCollection extends UniqueCollectionBase {2168  getTokenObject(tokenId: number) {2169    return new UniqueRFTToken(tokenId, this);2170  }21712172  async getTokensByAddress(addressObj: ICrossAccountId) {2173    return await this.helper.rft.getTokensByAddress(this.collectionId, addressObj);2174  }21752176  async getTop10TokenOwners(tokenId: number) {2177    return await this.helper.rft.getTokenTop10Owners(this.collectionId, tokenId);2178  }21792180  async getTokenBalance(tokenId: number, addressObj: ICrossAccountId) {2181    return await this.helper.rft.getTokenBalance(this.collectionId, tokenId, addressObj);2182  }21832184  async getTokenTotalPieces(tokenId: number) {2185    return await this.helper.rft.getTokenTotalPieces(this.collectionId, tokenId);2186  }21872188  async transferToken(signer: TSigner, tokenId: number, addressObj: ICrossAccountId, amount=100n) {2189    return await this.helper.rft.transferToken(signer, this.collectionId, tokenId, addressObj, amount);2190  }21912192  async transferTokenFrom(signer: TSigner, tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n) {2193    return await this.helper.rft.transferTokenFrom(signer, this.collectionId, tokenId, fromAddressObj, toAddressObj, amount);2194  }21952196  async approveToken(signer: TSigner, tokenId: number, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2197    return await this.helper.rft.approveToken(signer, this.collectionId, tokenId, toAddressObj, label, amount);2198  }21992200  async getTokenApprovedPieces(tokenId: number, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2201    return await this.helper.rft.getTokenApprovedPieces(this.collectionId, tokenId, toAddressObj, fromAddressObj);2202  }22032204  async repartitionToken(signer: TSigner, tokenId: number, amount: bigint, label?: string) {2205    return await this.helper.rft.repartitionToken(signer, this.collectionId, tokenId, amount, label);2206  }22072208  async mintToken(signer: TSigner, owner: ICrossAccountId, pieces=100n, properties?: IProperty[], label?: string) {2209    return await this.helper.rft.mintToken(signer, {collectionId: this.collectionId, owner, pieces, properties}, label);2210  }22112212  async mintMultipleTokens(signer: TSigner, tokens: {owner: ICrossAccountId, pieces: bigint, properties?: IProperty[]}[], label?: string) {2213    return await this.helper.rft.mintMultipleTokens(signer, this.collectionId, tokens, label);2214  }22152216  async burnToken(signer: TSigner, tokenId: number, amount=100n, label?: string) {2217    return await this.helper.rft.burnToken(signer, this.collectionId, tokenId, label, amount);2218  }22192220  async setTokenProperties(signer: TSigner, tokenId: number, properties: IProperty[], label?: string) {2221    return await this.helper.rft.setTokenProperties(signer, this.collectionId, tokenId, properties, label);2222  }22232224  async deleteTokenProperties(signer: TSigner, tokenId: number, propertyKeys: string[], label?: string) {2225    return await this.helper.rft.deleteTokenProperties(signer, this.collectionId, tokenId, propertyKeys, label);2226  }22272228  async setTokenPropertyPermissions(signer: TSigner, permissions: ITokenPropertyPermission[], label?: string) {2229    return await this.helper.rft.setTokenPropertyPermissions(signer, this.collectionId, permissions, label);2230  }2231}223222332234class UniqueFTCollection extends UniqueCollectionBase {2235  async mint(signer: TSigner, owner: ICrossAccountId, amount: bigint, label?: string) {2236    return await this.helper.ft.mintTokens(signer, this.collectionId, owner, amount, label);2237  }22382239  async mintWithOneOwner(signer: TSigner, owner: ICrossAccountId, tokens: {value: bigint}[], label?: string) {2240    return await this.helper.ft.mintMultipleTokensWithOneOwner(signer, this.collectionId, owner, tokens, label);2241  }22422243  async getBalance(addressObj: ICrossAccountId) {2244    return await this.helper.ft.getBalance(this.collectionId, addressObj);2245  }22462247  async getTop10Owners() {2248    return await this.helper.ft.getTop10Owners(this.collectionId);2249  }22502251  async transfer(signer: TSigner, toAddressObj: ICrossAccountId, amount: bigint) {2252    return await this.helper.ft.transfer(signer, this.collectionId, toAddressObj, amount);2253  }22542255  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount: bigint) {2256    return await this.helper.ft.transferFrom(signer, this.collectionId, fromAddressObj, toAddressObj, amount);2257  }22582259  async burnTokens(signer: TSigner, amount: bigint, label?: string) {2260    return await this.helper.ft.burnTokens(signer, this.collectionId, amount, label);2261  }22622263  async burnTokensFrom(signer: TSigner, fromAddressObj: ICrossAccountId, amount: bigint, label?: string) {2264    return await this.helper.ft.burnTokensFrom(signer, this.collectionId, fromAddressObj, amount, label);2265  }22662267  async getTotalPieces() {2268    return await this.helper.ft.getTotalPieces(this.collectionId);2269  }22702271  async approveTokens(signer: TSigner, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2272    return await this.helper.ft.approveTokens(signer, this.collectionId, toAddressObj, amount, label);2273  }22742275  async getApprovedTokens(fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2276    return await this.helper.ft.getApprovedTokens(this.collectionId, fromAddressObj, toAddressObj);2277  }2278}227922802281class UniqueTokenBase implements IToken {2282  collection: UniqueNFTCollection | UniqueRFTCollection;2283  collectionId: number;2284  tokenId: number;22852286  constructor(tokenId: number, collection: UniqueNFTCollection | UniqueRFTCollection) {2287    this.collection = collection;2288    this.collectionId = collection.collectionId;2289    this.tokenId = tokenId;2290  }22912292  async getNextSponsored(addressObj: ICrossAccountId) {2293    return await this.collection.getTokenNextSponsored(this.tokenId, addressObj);2294  }22952296  async setProperties(signer: TSigner, properties: IProperty[], label?: string) {2297    return await this.collection.setTokenProperties(signer, this.tokenId, properties, label);2298  }22992300  async deleteProperties(signer: TSigner, propertyKeys: string[], label?: string) {2301    return await this.collection.deleteTokenProperties(signer, this.tokenId, propertyKeys, label);2302  }2303}230423052306class UniqueNFTToken extends UniqueTokenBase {2307  collection: UniqueNFTCollection;23082309  constructor(tokenId: number, collection: UniqueNFTCollection) {2310    super(tokenId, collection);2311    this.collection = collection;2312  }23132314  async getData(blockHashAt?: string) {2315    return await this.collection.getToken(this.tokenId, blockHashAt);2316  }23172318  async getOwner(blockHashAt?: string) {2319    return await this.collection.getTokenOwner(this.tokenId, blockHashAt);2320  }23212322  async getTopmostOwner(blockHashAt?: string) {2323    return await this.collection.getTokenTopmostOwner(this.tokenId, blockHashAt);2324  }23252326  async getChildren(blockHashAt?: string) {2327    return await this.collection.getTokenChildren(this.tokenId, blockHashAt);2328  }23292330  async nest(signer: TSigner, toTokenObj: IToken, label?: string) {2331    return await this.collection.nestToken(signer, this.tokenId, toTokenObj, label);2332  }23332334  async unnest(signer: TSigner, fromTokenObj: IToken, toAddressObj: ICrossAccountId, label?: string) {2335    return await this.collection.unnestToken(signer, this.tokenId, fromTokenObj, toAddressObj, label);2336  }23372338  async transfer(signer: TSigner, addressObj: ICrossAccountId) {2339    return await this.collection.transferToken(signer, this.tokenId, addressObj);2340  }23412342  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId) {2343    return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj);2344  }23452346  async approve(signer: TSigner, toAddressObj: ICrossAccountId, label?: string) {2347    return await this.collection.approveToken(signer, this.tokenId, toAddressObj, label);2348  }23492350  async isApproved(toAddressObj: ICrossAccountId) {2351    return await this.collection.isTokenApproved(this.tokenId, toAddressObj);2352  }23532354  async burn(signer: TSigner, label?: string) {2355    return await this.collection.burnToken(signer, this.tokenId, label);2356  }2357}23582359class UniqueRFTToken extends UniqueTokenBase {2360  collection: UniqueRFTCollection;23612362  constructor(tokenId: number, collection: UniqueRFTCollection) {2363    super(tokenId, collection);2364    this.collection = collection;2365  }23662367  async getTop10Owners() {2368    return await this.collection.getTop10TokenOwners(this.tokenId);2369  }23702371  async getBalance(addressObj: ICrossAccountId) {2372    return await this.collection.getTokenBalance(this.tokenId, addressObj);2373  }23742375  async getTotalPieces() {2376    return await this.collection.getTokenTotalPieces(this.tokenId);2377  }23782379  async transfer(signer: TSigner, addressObj: ICrossAccountId, amount=100n) {2380    return await this.collection.transferToken(signer, this.tokenId, addressObj, amount);2381  }23822383  async transferFrom(signer: TSigner, fromAddressObj: ICrossAccountId, toAddressObj: ICrossAccountId, amount=100n) {2384    return await this.collection.transferTokenFrom(signer, this.tokenId, fromAddressObj, toAddressObj, amount);2385  }23862387  async approve(signer: TSigner, toAddressObj: ICrossAccountId, amount=100n, label?: string) {2388    return await this.collection.approveToken(signer, this.tokenId, toAddressObj, amount, label);2389  }23902391  async getApprovedPieces(fromAddressObj: ICrossAccountId, toAccountObj: ICrossAccountId) {2392    return await this.collection.getTokenApprovedPieces(this.tokenId, fromAddressObj, toAccountObj);2393  }23942395  async repartition(signer: TSigner, amount: bigint, label?: string) {2396    return await this.collection.repartitionToken(signer, this.tokenId, amount, label);2397  }23982399  async burn(signer: TSigner, amount=100n, label?: string) {2400    return await this.collection.burnToken(signer, this.tokenId, amount, label);2401  }2402}