Adjust lazyVariants with internal $type (#5081)

* Adjust lazyVariants with internal $type

* Adjust

* Allow for empty paths

* Allow any $ override

* Adjust

* Small usage fixes, dedupe
This commit is contained in:
Jaco
2022-07-18 09:12:14 +03:00
committed by GitHub
parent 12750bc83d
commit 14f2a9da39
16 changed files with 46 additions and 33 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 } from '@polkadot/types/types';
import type { AnyFunction, Codec, DefinitionCallNamed, SectionMetadata } 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>;
[key: string]: QueryableModuleCalls<ApiType> & SectionMetadata;
}
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 } 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<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;
[key: string]: QueryableModuleConsts & SectionMetadata;
}
export interface QueryableModuleConsts {
+2 -1
View File
@@ -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<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>;
[key: string]: ModuleErrors<ApiType> & SectionMetadata;
}
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 } 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<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>;
[key: string]: ModuleEvents<ApiType> & SectionMetadata;
}
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 } 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<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>;
[key: string]: QueryableModuleStorage<ApiType> & SectionMetadata;
}
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, 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 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>;
[key: string]: SubmittableModuleExtrinsics<ApiType> & SectionMetadata;
}
export interface SubmittableModuleExtrinsics<ApiType extends ApiTypes> {
+1 -1
View File
@@ -150,7 +150,7 @@ function querySystemAccount (api: DeriveApi, accountId: AccountId): Observable<R
* ```
*/
export function account (instanceId: string, api: DeriveApi): (address: AccountIndex | AccountId | Address | string) => Observable<DeriveBalancesAccount> {
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<DeriveBalancesAccount> =>
api.derive.accounts.accountId(address).pipe(
+1 -1
View File
@@ -211,7 +211,7 @@ function queryCurrent (api: DeriveApi, accountId: AccountId | string, balanceIns
* ```
*/
export function all (instanceId: string, api: DeriveApi): (address: AccountId | string) => Observable<DeriveBalancesAll> {
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<DeriveBalancesAll> =>
combineLatest([
+3 -3
View File
@@ -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<string, Avail> = {
}
};
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<string, Record<string, AnyFunction>> = {};
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);
@@ -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;
+1 -1
View File
@@ -55,7 +55,7 @@ function getConstants (api: DeriveApi, elections: string | null): Partial<Derive
}
function getModules (api: DeriveApi): [string, string | null] {
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'council') || ['council'];
const [council] = api.registry.getModuleInstances(api.runtimeVersion.specName, 'council') || ['council'];
const elections = api.query.phragmenElection
? 'phragmenElection'
: api.query.electionsPhragmen
+3 -1
View File
@@ -626,7 +626,9 @@ export abstract class Decorate<ApiType extends ApiTypes> 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);
+8 -2
View File
@@ -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 <T> (lookup: PortableRegistry, { type }: TypeHolder, getName: (v: SiVariant) => string, creator: (v: SiVariant) => T): Record<string, T> {
const result: Record<string, T> = {};
const variants = lookup.getSiType(type).def.asVariant.variants;
const result: Record<string, T> & 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;
}
+10 -10
View File
@@ -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<string, Record<string, RegistryError>>): void {
function injectErrors (_: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError> & 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<string, Record<string, CodecClass<GenericEventData>>>): void {
function injectEvents (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CodecClass<GenericEventData> & 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<string, Record<string, CallFunction>>): void {
function injectExtrinsics (registry: TypeRegistry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction> & SectionMetadata>): void {
const filtered = pallets.filter(filterCallsSome);
clearRecord(result);
@@ -150,11 +150,11 @@ export class TypeRegistry implements Registry {
#metadataVersion = 0;
readonly #metadataCalls: Record<string, Record<string, CallFunction>> = {};
readonly #metadataCalls: Record<string, Record<string, CallFunction> & SectionMetadata> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError>> = {};
readonly #metadataErrors: Record<string, Record<string, RegistryError> & SectionMetadata> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>>> = {};
readonly #metadataEvents: Record<string, Record<string, CodecClass<GenericEventData>> & SectionMetadata> = {};
#unknownTypes = new Map<string, boolean>();
@@ -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 <T extends Codec = Codec, K extends string = string, R = DetectCodec<T, K>> (name: K, msg?: string): CodecClass<R> {
+4
View File
@@ -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;
}
+2 -2
View File
@@ -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 <T extends Codec = Codec, K extends string = string> (name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<DetectCodec<T, K>> | 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;