Adjust instance check to handle Call paths (#5082)

This commit is contained in:
Jaco
2022-07-18 10:35:14 +03:00
committed by GitHub
parent 72469ae6da
commit 34a5cbed90
9 changed files with 54 additions and 35 deletions
+2 -2
View File
@@ -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<ApiType extends ApiTypes, F extends AnyFunction = (...args: any[]) => Observable<Codec>> =
@@ -23,7 +23,7 @@ export interface AugmentedCalls<ApiType extends ApiTypes> {
export interface QueryableCalls<ApiType extends ApiTypes> extends AugmentedCalls<ApiType> {
// when non-augmented, we need to at least have Codec results
[key: string]: QueryableModuleCalls<ApiType> & SectionMetadata;
[key: string]: QueryableModuleCalls<ApiType>;
}
export interface QueryableModuleCalls<ApiType extends ApiTypes> {
+2 -2
View File
@@ -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<ApiType extends ApiTypes> {
export interface QueryableConsts<ApiType extends ApiTypes> extends AugmentedConsts<ApiType> {
// when non-augmented, we need to at least have Codec results
[key: string]: QueryableModuleConsts & SectionMetadata;
[key: string]: QueryableModuleConsts;
}
export interface QueryableModuleConsts {
+1 -2
View File
@@ -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<ApiType extends ApiTypes> {
export interface DecoratedErrors<ApiType extends ApiTypes> extends AugmentedErrors<ApiType> {
// when non-augmented, we need to at least have Codec results
[key: string]: ModuleErrors<ApiType> & SectionMetadata;
[key: string]: ModuleErrors<ApiType>;
}
export interface ModuleErrors<ApiType extends ApiTypes> {
+2 -2
View File
@@ -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<ApiType extends ApiTypes> {
export interface DecoratedEvents<ApiType extends ApiTypes> extends AugmentedEvents<ApiType> {
// when non-augmented, we need to at least have Codec results
[key: string]: ModuleEvents<ApiType> & SectionMetadata;
[key: string]: ModuleEvents<ApiType>;
}
export interface ModuleEvents<ApiType extends ApiTypes> {
+2 -2
View File
@@ -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<R extends Codec = Codec> {
@@ -125,7 +125,7 @@ export interface AugmentedQueries<ApiType extends ApiTypes> {
export interface QueryableStorage<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {
// when non-augmented, we need to at least have Codec results
[key: string]: QueryableModuleStorage<ApiType> & SectionMetadata;
[key: string]: QueryableModuleStorage<ApiType>;
}
export interface QueryableStorageAt<ApiType extends ApiTypes> extends AugmentedQueries<ApiType> {
+2 -2
View File
@@ -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 extends AnyFunction, A extends AnyTuple = AnyTuple> = T & CallBase<A>;
@@ -88,7 +88,7 @@ export interface AugmentedSubmittables<ApiType extends ApiTypes> {
export interface SubmittableExtrinsics<ApiType extends ApiTypes> extends AugmentedSubmittables<ApiType> {
(extrinsic: Call | Extrinsic | Uint8Array | string): SubmittableExtrinsic<ApiType>;
// when non-augmented, we need to at least have Codec results
[key: string]: SubmittableModuleExtrinsics<ApiType> & SectionMetadata;
[key: string]: SubmittableModuleExtrinsics<ApiType>;
}
export interface SubmittableModuleExtrinsics<ApiType extends ApiTypes> {
+2 -8
View File
@@ -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 <T> (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record<string, T> {
const result: Record<string, T> & SectionMetadata = {};
const { def, path } = lookup.getSiType(type);
const variants = def.asVariant.variants;
const result: Record<string, T> = {};
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;
}
+41 -11
View File
@@ -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<string, Record<string, RegistryError> & SectionMetadata>): void {
function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError>>): 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<string, Record<string, CodecClass<GenericEventData> & SectionMetadata>>): void {
function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CodecClass<GenericEventData>>>): 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<string, Record<string, CallFunction> & SectionMetadata>): void {
function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction>>, mapping: Record<string, string[]>): 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<string, Record<string, CallFunction> & SectionMetadata> = {};
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError> & SectionMetadata> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>> & SectionMetadata> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
readonly #moduleMap: Record<string, string[]> = {};
#unknownTypes = new Map<string, boolean>();
@@ -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 <T extends Codec = Codec, K extends string = string, R = DetectCodec<T, K>> (name: K, msg?: string): CodecClass<R> {
@@ -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);
-4
View File
@@ -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;
}