diff --git a/packages/api-base/src/types/calls.ts b/packages/api-base/src/types/calls.ts index 389bb4e3cf..2c9cd78d4f 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, SectionMetadata } from '@polkadot/types/types'; +import type { AnyFunction, Codec, DefinitionCallNamed } 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 & SectionMetadata; + [key: string]: QueryableModuleCalls; } export interface QueryableModuleCalls { diff --git a/packages/api-base/src/types/consts.ts b/packages/api-base/src/types/consts.ts index 23b50ef3fa..1c0369ffbd 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, SectionMetadata } from '@polkadot/types/types'; +import type { Codec } 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 & SectionMetadata; + [key: string]: QueryableModuleConsts; } export interface QueryableModuleConsts { diff --git a/packages/api-base/src/types/errors.ts b/packages/api-base/src/types/errors.ts index 7ac6d81c00..5fa014095f 100644 --- a/packages/api-base/src/types/errors.ts +++ b/packages/api-base/src/types/errors.ts @@ -2,7 +2,6 @@ // 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 @@ -17,7 +16,7 @@ export interface AugmentedErrors { export interface DecoratedErrors extends AugmentedErrors { // when non-augmented, we need to at least have Codec results - [key: string]: ModuleErrors & SectionMetadata; + [key: string]: ModuleErrors; } export interface ModuleErrors { diff --git a/packages/api-base/src/types/events.ts b/packages/api-base/src/types/events.ts index b17e32486d..2069ae8639 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, SectionMetadata } from '@polkadot/types/types'; +import type { AnyTuple } 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 & SectionMetadata; + [key: string]: ModuleEvents; } export interface ModuleEvents { diff --git a/packages/api-base/src/types/storage.ts b/packages/api-base/src/types/storage.ts index 82359b32b5..21efa6d105 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, SectionMetadata } from '@polkadot/types/types'; +import type { AnyFunction, AnyTuple, Callback, Codec, IStorageKey } 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 & SectionMetadata; + [key: string]: QueryableModuleStorage; } export interface QueryableStorageAt extends AugmentedQueries { diff --git a/packages/api-base/src/types/submittable.ts b/packages/api-base/src/types/submittable.ts index 13a37bb31c..e4c3ac9ab3 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, SectionMetadata, Signer } from '@polkadot/types/types'; +import type { AnyFunction, AnyNumber, AnyTuple, Callback, CallBase, Codec, IExtrinsicEra, IKeyringPair, ISubmittableResult, 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 & SectionMetadata; + [key: string]: SubmittableModuleExtrinsics; } export interface SubmittableModuleExtrinsics { diff --git a/packages/types/src/create/lazy.ts b/packages/types/src/create/lazy.ts index 9d21010baa..45ee70fd8d 100644 --- a/packages/types/src/create/lazy.ts +++ b/packages/types/src/create/lazy.ts @@ -3,7 +3,6 @@ import type { SiLookupTypeId, SiVariant } from '../interfaces'; import type { PortableRegistry } from '../metadata'; -import type { SectionMetadata } from './types'; import { lazyMethod } from '@polkadot/util'; @@ -12,17 +11,12 @@ interface TypeHolder { } export function lazyVariants (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record { - const result: Record & SectionMetadata = {}; - const { def, path } = lookup.getSiType(type); - const variants = def.asVariant.variants; + const result: Record = {}; + const variants = lookup.getSiType(type).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 c2b3ea8db5..c4b6175b69 100644 --- a/packages/types/src/create/registry.ts +++ b/packages/types/src/create/registry.ts @@ -5,7 +5,7 @@ import type { AnyString, Codec, CodecClass, IU8a } from '@polkadot/types-codec/t 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, SectionMetadata } from '../types'; +import type { CallFunction, CodecHasher, Definitions, DetectCodec, RegisteredTypes, Registry, RegistryError, RegistryTypes } 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 & SectionMetadata>): void { +function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>): 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 & SectionMetadata>>): void { +function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>>): void { const filtered = pallets.filter(filterEventsSome); clearRecord(result); @@ -105,20 +105,48 @@ function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLate } // create extrinsic mapping from metadata -function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record & SectionMetadata>): void { +function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record>, mapping: Record): void { const filtered = pallets.filter(filterCallsSome); clearRecord(result); + clearRecord(mapping); for (let i = 0; i < filtered.length; i++) { const { calls, index, name } = filtered[i]; const sectionIndex = version >= 12 ? index.toNumber() : i; + const sectionName = stringCamelCase(name); + const allCalls = calls.unwrap(); lazyMethod(result, sectionIndex, () => - lazyVariants(lookup, calls.unwrap(), getVariantStringIdx, (variant: SiVariant) => - createCallFunction(registry, lookup, variant, stringCamelCase(name), sectionIndex) + lazyVariants(lookup, allCalls, getVariantStringIdx, (variant: SiVariant) => + createCallFunction(registry, lookup, variant, sectionName, sectionIndex) ) ); + + const { path } = registry.lookup.getSiType(allCalls.type); + + // frame_system::pallet::Call / pallet_balances::pallet::Call / polkadot_runtime_parachains::configuration::pallet::Call / + const palletIdx = path.findIndex((v) => v.eq('pallet')); + + if (palletIdx !== -1) { + const name = stringCamelCase( + path + .slice(0, palletIdx) + .map((p, i) => + i === 0 + // frame_system || pallet_balances + ? p.replace(/^(frame|pallet)_/, '') + : p + ) + .join(' ') + ); + + if (!mapping[name]) { + mapping[name] = [sectionName]; + } else { + mapping[name].push(sectionName); + } + } } } @@ -150,11 +178,13 @@ export class TypeRegistry implements Registry { #metadataVersion = 0; - readonly #metadataCalls: Record & SectionMetadata> = {}; + readonly #metadataCalls: Record> = {}; - readonly #metadataErrors: Record & SectionMetadata> = {}; + readonly #metadataErrors: Record> = {}; - readonly #metadataEvents: Record> & SectionMetadata> = {}; + readonly #metadataEvents: Record>> = {}; + + readonly #moduleMap: Record = {}; #unknownTypes = new Map(); @@ -404,7 +434,7 @@ export class TypeRegistry implements Registry { } public getModuleInstances (specName: AnyString, moduleName: string): string[] | undefined { - return this.#knownTypes?.typesBundle?.spec?.[specName.toString()]?.instances?.[moduleName]; + return this.#knownTypes?.typesBundle?.spec?.[specName.toString()]?.instances?.[moduleName] || this.#moduleMap[moduleName]; } public getOrThrow > (name: K, msg?: string): CodecClass { @@ -527,7 +557,7 @@ export class TypeRegistry implements Registry { // attach the lookup at this point (before injecting) this.setLookup(this.#metadata.lookup); - injectExtrinsics(this, this.#metadata, this.#metadataVersion, this.#metadataCalls); + injectExtrinsics(this, this.#metadata, this.#metadataVersion, this.#metadataCalls, this.#moduleMap); injectErrors(this, this.#metadata, this.#metadataVersion, this.#metadataErrors); injectEvents(this, this.#metadata, this.#metadataVersion, this.#metadataEvents); diff --git a/packages/types/src/create/types.ts b/packages/types/src/create/types.ts index 16afcedc5b..50eb2ee383 100644 --- a/packages/types/src/create/types.ts +++ b/packages/types/src/create/types.ts @@ -5,7 +5,3 @@ 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; -}