Skip to content

Commit

Permalink
Adjust lazyVariants with internal $type (polkadot-js#5081)
Browse files Browse the repository at this point in the history
* Adjust lazyVariants with internal $type

* Adjust

* Allow for empty paths

* Allow any $ override

* Adjust

* Small usage fixes, dedupe
  • Loading branch information
jacogr authored Jul 18, 2022
1 parent 12750bc commit 14f2a9d
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 33 deletions.
4 changes: 2 additions & 2 deletions packages/api-base/src/types/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>> =
Expand All @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-base/src/types/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/api-base/src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-base/src/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-base/src/types/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down
4 changes: 2 additions & 2 deletions packages/api-base/src/types/submittable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -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> {
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/src/balances/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/src/balances/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
6 changes: 3 additions & 3 deletions packages/api-derive/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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) || [];
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/src/collective/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/api-derive/src/elections/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/base/Decorate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions packages/types/src/create/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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;
}
20 changes: 10 additions & 10 deletions packages/types/src/create/registry.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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++) {
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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>();

Expand Down Expand Up @@ -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> {
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/create/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions packages/types/src/types/augmentRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 14f2a9d

Please sign in to comment.