diff --git a/packages/api-base/src/types/calls.ts b/packages/api-base/src/types/calls.ts index 2c9cd78d4f70..389bb4e3cf6d 100644 --- a/packages/api-base/src/types/calls.ts +++ b/packages/api-base/src/types/calls.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Observable } from 'rxjs'; -import type { AnyFunction, Codec, DefinitionCallNamed } from '@polkadot/types/types'; +import type { AnyFunction, Codec, DefinitionCallNamed, SectionMetadata } from '@polkadot/types/types'; import type { ApiTypes, ReturnCodec } from './base'; export type DecoratedCallBase Observable> = @@ -23,7 +23,7 @@ export interface AugmentedCalls { export interface QueryableCalls extends AugmentedCalls { // when non-augmented, we need to at least have Codec results - [key: string]: QueryableModuleCalls; + [key: string]: QueryableModuleCalls & SectionMetadata; } export interface QueryableModuleCalls { diff --git a/packages/api-base/src/types/consts.ts b/packages/api-base/src/types/consts.ts index 1c0369ffbd30..23b50ef3fa92 100644 --- a/packages/api-base/src/types/consts.ts +++ b/packages/api-base/src/types/consts.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { PalletConstantMetadataLatest } from '@polkadot/types/interfaces'; -import type { Codec } from '@polkadot/types/types'; +import type { Codec, SectionMetadata } from '@polkadot/types/types'; import type { ApiTypes } from './base'; // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -19,7 +19,7 @@ export interface AugmentedConsts { export interface QueryableConsts extends AugmentedConsts { // when non-augmented, we need to at least have Codec results - [key: string]: QueryableModuleConsts; + [key: string]: QueryableModuleConsts & SectionMetadata; } export interface QueryableModuleConsts { diff --git a/packages/api-base/src/types/errors.ts b/packages/api-base/src/types/errors.ts index 5fa014095fe6..7ac6d81c0057 100644 --- a/packages/api-base/src/types/errors.ts +++ b/packages/api-base/src/types/errors.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { IsError } from '@polkadot/types/metadata/decorate/types'; +import type { SectionMetadata } from '@polkadot/types/types'; import type { ApiTypes } from './base'; // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -16,7 +17,7 @@ export interface AugmentedErrors { export interface DecoratedErrors extends AugmentedErrors { // when non-augmented, we need to at least have Codec results - [key: string]: ModuleErrors; + [key: string]: ModuleErrors & SectionMetadata; } export interface ModuleErrors { diff --git a/packages/api-base/src/types/events.ts b/packages/api-base/src/types/events.ts index 2069ae86392b..b17e32486d78 100644 --- a/packages/api-base/src/types/events.ts +++ b/packages/api-base/src/types/events.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { IsEvent } from '@polkadot/types/metadata/decorate/types'; -import type { AnyTuple } from '@polkadot/types/types'; +import type { AnyTuple, SectionMetadata } from '@polkadot/types/types'; import type { ApiTypes } from './base'; // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -17,7 +17,7 @@ export interface AugmentedEvents { export interface DecoratedEvents extends AugmentedEvents { // when non-augmented, we need to at least have Codec results - [key: string]: ModuleEvents; + [key: string]: ModuleEvents & SectionMetadata; } export interface ModuleEvents { diff --git a/packages/api-base/src/types/storage.ts b/packages/api-base/src/types/storage.ts index 21efa6d10576..82359b32b504 100644 --- a/packages/api-base/src/types/storage.ts +++ b/packages/api-base/src/types/storage.ts @@ -5,7 +5,7 @@ import type { Observable } from 'rxjs'; import type { StorageKey, u64 } from '@polkadot/types'; import type { Hash } from '@polkadot/types/interfaces'; import type { StorageEntry } from '@polkadot/types/primitive/types'; -import type { AnyFunction, AnyTuple, Callback, Codec, IStorageKey } from '@polkadot/types/types'; +import type { AnyFunction, AnyTuple, Callback, Codec, IStorageKey, SectionMetadata } from '@polkadot/types/types'; import type { ApiTypes, DropLast, MethodResult, PaginationOptions, PromiseOrObs, ReturnCodec, UnsubscribePromise } from './base'; interface StorageEntryObservableMulti { @@ -125,7 +125,7 @@ export interface AugmentedQueries { export interface QueryableStorage extends AugmentedQueries { // when non-augmented, we need to at least have Codec results - [key: string]: QueryableModuleStorage; + [key: string]: QueryableModuleStorage & SectionMetadata; } export interface QueryableStorageAt extends AugmentedQueries { diff --git a/packages/api-base/src/types/submittable.ts b/packages/api-base/src/types/submittable.ts index e4c3ac9ab3d8..13a37bb31c4f 100644 --- a/packages/api-base/src/types/submittable.ts +++ b/packages/api-base/src/types/submittable.ts @@ -3,7 +3,7 @@ import type { Observable } from 'rxjs'; import type { AccountId, Address, ApplyExtrinsicResult, Call, DispatchError, DispatchInfo, EventRecord, Extrinsic, ExtrinsicStatus, Hash, RuntimeDispatchInfo } from '@polkadot/types/interfaces'; -import type { AnyFunction, AnyNumber, AnyTuple, Callback, CallBase, Codec, IExtrinsicEra, IKeyringPair, ISubmittableResult, Signer } from '@polkadot/types/types'; +import type { AnyFunction, AnyNumber, AnyTuple, Callback, CallBase, Codec, IExtrinsicEra, IKeyringPair, ISubmittableResult, SectionMetadata, Signer } from '@polkadot/types/types'; import type { ApiTypes, PromiseOrObs } from './base'; export type AugmentedSubmittable = T & CallBase; @@ -88,7 +88,7 @@ export interface AugmentedSubmittables { export interface SubmittableExtrinsics extends AugmentedSubmittables { (extrinsic: Call | Extrinsic | Uint8Array | string): SubmittableExtrinsic; // when non-augmented, we need to at least have Codec results - [key: string]: SubmittableModuleExtrinsics; + [key: string]: SubmittableModuleExtrinsics & SectionMetadata; } export interface SubmittableModuleExtrinsics { diff --git a/packages/api-derive/src/balances/account.ts b/packages/api-derive/src/balances/account.ts index cdb7ef936bff..5aea33224e9e 100644 --- a/packages/api-derive/src/balances/account.ts +++ b/packages/api-derive/src/balances/account.ts @@ -150,7 +150,7 @@ function querySystemAccount (api: DeriveApi, accountId: AccountId): Observable Observable { - const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'balances'); + const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName, 'balances'); return memo(instanceId, (address: AccountIndex | AccountId | Address | string): Observable => api.derive.accounts.accountId(address).pipe( diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index 698c90e59aab..01fa9af7628c 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -211,7 +211,7 @@ function queryCurrent (api: DeriveApi, accountId: AccountId | string, balanceIns * ``` */ export function all (instanceId: string, api: DeriveApi): (address: AccountId | string) => Observable { - const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'balances'); + const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName, 'balances'); return memo(instanceId, (address: AccountId | string): Observable => combineLatest([ diff --git a/packages/api-derive/src/bundle.ts b/packages/api-derive/src/bundle.ts index 8d847c07c161..4aae5c042252 100644 --- a/packages/api-derive/src/bundle.ts +++ b/packages/api-derive/src/bundle.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { DeriveCustom } from '@polkadot/api-base/types'; -import type { AnyFunction } from '@polkadot/types/types'; +import type { AnyFunction, AnyString } from '@polkadot/types/types'; import type { ExactDerive } from './derive'; import type { DeriveApi } from './types'; @@ -84,7 +84,7 @@ const checks: Record = { } }; -function getModuleInstances (api: DeriveApi, specName: string, moduleName: string): string[] { +function getModuleInstances (api: DeriveApi, specName: AnyString, moduleName: string): string[] { return api.registry.getModuleInstances(specName, moduleName) || []; } @@ -97,7 +97,7 @@ function injectFunctions (instanceId: string, api: DeriveApi, derives: DeriveCus const result: Record> = {}; const names = Object.keys(derives); const keys = Object.keys(api.query); - const specName = api.runtimeVersion.specName.toString(); + const specName = api.runtimeVersion.specName; const filterKeys = (q: string) => keys.includes(q); const filterInstances = (q: string) => getModuleInstances(api, specName, q).some(filterKeys); diff --git a/packages/api-derive/src/collective/helpers.ts b/packages/api-derive/src/collective/helpers.ts index b6334cf03170..c936bd963e18 100644 --- a/packages/api-derive/src/collective/helpers.ts +++ b/packages/api-derive/src/collective/helpers.ts @@ -12,7 +12,7 @@ import { isFunction } from '@polkadot/util'; import { memo } from '../util'; export function getInstance (api: DeriveApi, section: string): DeriveApi['query']['council'] { - const instances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), section); + const instances = api.registry.getModuleInstances(api.runtimeVersion.specName, section); const name = instances && instances.length ? instances[0] : section; diff --git a/packages/api-derive/src/elections/info.ts b/packages/api-derive/src/elections/info.ts index 12cb441f6dd8..fd320ddc08b0 100644 --- a/packages/api-derive/src/elections/info.ts +++ b/packages/api-derive/src/elections/info.ts @@ -55,7 +55,7 @@ function getConstants (api: DeriveApi, elections: string | null): Partial extends Events { const lazySection = (section: string) => lazyMethods({}, Object.keys(tx[section]), (method: string) => - this._decorateExtrinsicEntry(tx[section][method], result) + method.startsWith('$') + ? tx[section][method] + : this._decorateExtrinsicEntry(tx[section][method], result) ); const sections = Object.keys(tx); diff --git a/packages/types/src/create/lazy.ts b/packages/types/src/create/lazy.ts index 45ee70fd8d4c..9d21010baa3b 100644 --- a/packages/types/src/create/lazy.ts +++ b/packages/types/src/create/lazy.ts @@ -3,6 +3,7 @@ import type { SiLookupTypeId, SiVariant } from '../interfaces'; import type { PortableRegistry } from '../metadata'; +import type { SectionMetadata } from './types'; import { lazyMethod } from '@polkadot/util'; @@ -11,12 +12,17 @@ interface TypeHolder { } export function lazyVariants (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record { - const result: Record = {}; - const variants = lookup.getSiType(type).def.asVariant.variants; + const result: Record & SectionMetadata = {}; + const { def, path } = lookup.getSiType(type); + const variants = def.asVariant.variants; for (let i = 0; i < variants.length; i++) { lazyMethod(result, variants[i], creator, getName); } + result.$path = path.length + ? path.join('::') + : undefined; + return result; } diff --git a/packages/types/src/create/registry.ts b/packages/types/src/create/registry.ts index 92fe3a7ffbad..c2b3ea8db5a9 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -1,11 +1,11 @@ // Copyright 2017-2022 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { Codec, CodecClass, IU8a } from '@polkadot/types-codec/types'; +import type { AnyString, Codec, CodecClass, IU8a } from '@polkadot/types-codec/types'; import type { CreateOptions, TypeDef } from '@polkadot/types-create/types'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { ChainProperties, DispatchErrorModule, DispatchErrorModuleU8, DispatchErrorModuleU8a, EventMetadataLatest, Hash, MetadataLatest, SiField, SiLookupTypeId, SiVariant } from '../interfaces/types'; -import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } from '../types'; +import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes, SectionMetadata } from '../types'; import { DoNotConstruct, Json, Raw } from '@polkadot/types-codec'; import { constructTypeClass, createClassUnsafe, createTypeUnsafe } from '@polkadot/types-create'; @@ -57,7 +57,7 @@ function getVariantStringIdx ({ index }: SiVariant): string { } // create error mapping from metadata -function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>): void { +function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record & SectionMetadata>): void { clearRecord(result); for (let i = 0; i < pallets.length; i++) { @@ -82,7 +82,7 @@ function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, ver } // create event classes from metadata -function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>>): void { +function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record & SectionMetadata>>): void { const filtered = pallets.filter(filterEventsSome); clearRecord(result); @@ -105,7 +105,7 @@ function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLate } // create extrinsic mapping from metadata -function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>): void { +function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record & SectionMetadata>): void { const filtered = pallets.filter(filterCallsSome); clearRecord(result); @@ -150,11 +150,11 @@ export class TypeRegistry implements Registry { #metadataVersion = 0; - readonly #metadataCalls: Record> = {}; + readonly #metadataCalls: Record & SectionMetadata> = {}; - readonly #metadataErrors: Record> = {}; + readonly #metadataErrors: Record & SectionMetadata> = {}; - readonly #metadataEvents: Record>> = {}; + readonly #metadataEvents: Record> & SectionMetadata> = {}; #unknownTypes = new Map(); @@ -403,8 +403,8 @@ export class TypeRegistry implements Registry { return this.#definitions.get(typeName); } - public getModuleInstances (specName: string, moduleName: string): string[] | undefined { - return this.#knownTypes?.typesBundle?.spec?.[specName]?.instances?.[moduleName]; + public getModuleInstances (specName: AnyString, moduleName: string): string[] | undefined { + return this.#knownTypes?.typesBundle?.spec?.[specName.toString()]?.instances?.[moduleName]; } public getOrThrow > (name: K, msg?: string): CodecClass { diff --git a/packages/types/src/create/types.ts b/packages/types/src/create/types.ts index 50eb2ee383ec..16afcedc5b17 100644 --- a/packages/types/src/create/types.ts +++ b/packages/types/src/create/types.ts @@ -5,3 +5,7 @@ export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/ export type { TypeDef } from '@polkadot/types-create/types'; export { TypeDefInfo } from '@polkadot/types-create'; + +export interface SectionMetadata { + $path?: string; +} diff --git a/packages/types/src/types/augmentRegistry.ts b/packages/types/src/types/augmentRegistry.ts index f0d966883c9c..4ba8589dd426 100644 --- a/packages/types/src/types/augmentRegistry.ts +++ b/packages/types/src/types/augmentRegistry.ts @@ -6,7 +6,7 @@ import '@polkadot/types-codec/types/registry'; import '@polkadot/types-create/types/augmentRegistry'; -import type { Codec, CodecClass } from '@polkadot/types-codec/types'; +import type { AnyString, Codec, CodecClass } from '@polkadot/types-codec/types'; import type { TypeDef } from '@polkadot/types-create/types'; import type { ExtDef } from '../extrinsic/signedExtensions/types'; import type { MetadataLatest } from '../interfaces/metadata'; @@ -41,7 +41,7 @@ declare module '@polkadot/types-codec/types/registry' { get (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass> | undefined; getChainProperties (): ChainProperties | undefined; getDefinition (typeName: string): string | undefined; - getModuleInstances (specName: string, moduleName: string): string[] | undefined; + getModuleInstances (specName: AnyString, moduleName: string): string[] | undefined; setKnownTypes (types: RegisteredTypes): void; setChainProperties (properties?: ChainProperties): void;