Lazy decorate (part 6, API internals) (#4150)
* Lazy decorate (part 6, API internals) * Rework tx decoration * Adjust metadata decoration triggers * Reformat registry lazy decoration * storage
This commit is contained in:
@@ -9,7 +9,7 @@ import type { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
|
||||
import type { StorageEntry } from '@polkadot/types/primitive/types';
|
||||
import type { AnyFunction, AnyTuple, CallFunction, Codec, DefinitionRpc, DefinitionRpcSub, DetectCodec, IMethod, IStorageKey, Registry, RegistryError, RegistryTypes } from '@polkadot/types/types';
|
||||
import type { SubmittableExtrinsic } from '../submittable/types';
|
||||
import type { ApiDecoration, ApiInterfaceRx, ApiOptions, ApiTypes, AugmentedQuery, DecoratedErrors, DecoratedEvents, DecoratedRpc, DecoratedRpcSection, DecorateMethod, GenericStorageEntryFunction, PaginationOptions, QueryableConsts, QueryableModuleStorage, QueryableModuleStorageAt, QueryableStorage, QueryableStorageAt, QueryableStorageEntry, QueryableStorageEntryAt, QueryableStorageMulti, QueryableStorageMultiArg, SubmittableExtrinsicFunction, SubmittableExtrinsics, SubmittableModuleExtrinsics } from '../types';
|
||||
import type { ApiDecoration, ApiInterfaceRx, ApiOptions, ApiTypes, AugmentedQuery, DecoratedErrors, DecoratedEvents, DecoratedRpc, DecoratedRpcSection, DecorateMethod, GenericStorageEntryFunction, PaginationOptions, QueryableConsts, QueryableStorage, QueryableStorageAt, QueryableStorageEntry, QueryableStorageEntryAt, QueryableStorageMulti, QueryableStorageMultiArg, SubmittableExtrinsicFunction, SubmittableExtrinsics } from '../types';
|
||||
import type { VersionedRegistry } from './types';
|
||||
|
||||
import { BehaviorSubject, combineLatest, from, map, of, switchMap, tap, toArray } from 'rxjs';
|
||||
@@ -17,7 +17,7 @@ import { BehaviorSubject, combineLatest, from, map, of, switchMap, tap, toArray
|
||||
import { ExactDerive, getAvailableDerives } from '@polkadot/api-derive';
|
||||
import { memo, RpcCore } from '@polkadot/rpc-core';
|
||||
import { WsProvider } from '@polkadot/rpc-provider';
|
||||
import { expandMetadata, Metadata, TypeRegistry, unwrapStorageType } from '@polkadot/types';
|
||||
import { expandMetadata, lazyMethod, lazyMethods, Metadata, TypeRegistry, unwrapStorageType } from '@polkadot/types';
|
||||
import { arrayChunk, arrayFlatten, assert, BN, BN_ZERO, compactStripLength, logger, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import { createSubmittable } from '../submittable';
|
||||
@@ -427,19 +427,20 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
}
|
||||
|
||||
protected _decorateExtrinsics<ApiType extends ApiTypes> ({ tx }: DecoratedMeta, decorateMethod: DecorateMethod<ApiType>): SubmittableExtrinsics<ApiType> {
|
||||
const creator = createSubmittable(this._type, this._rx, decorateMethod) as SubmittableExtrinsics<ApiType>;
|
||||
const result = createSubmittable(this._type, this._rx, decorateMethod) as SubmittableExtrinsics<ApiType>;
|
||||
|
||||
for (const [name, section] of Object.entries(tx)) {
|
||||
const methods: SubmittableModuleExtrinsics<ApiType> = {};
|
||||
const lazySection = (s: string) =>
|
||||
lazyMethods(Object.keys(tx[s]), (m: string) =>
|
||||
this._decorateExtrinsicEntry(tx[s][m], result)
|
||||
);
|
||||
|
||||
for (const [name, method] of Object.entries(section)) {
|
||||
methods[name] = this._decorateExtrinsicEntry(method, creator);
|
||||
}
|
||||
const sections = Object.keys(tx);
|
||||
|
||||
creator[name] = methods;
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
lazyMethod(result, sections[i], lazySection);
|
||||
}
|
||||
|
||||
return creator;
|
||||
return result;
|
||||
}
|
||||
|
||||
private _decorateExtrinsicEntry<ApiType extends ApiTypes> (method: CallFunction, creator: (value: Call | Uint8Array | string) => SubmittableExtrinsic<ApiType>): SubmittableExtrinsicFunction<ApiType> {
|
||||
@@ -457,14 +458,15 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
protected _decorateStorage<ApiType extends ApiTypes> ({ query }: DecoratedMeta, decorateMethod: DecorateMethod<ApiType>): QueryableStorage<ApiType> {
|
||||
const result = {} as QueryableStorage<ApiType>;
|
||||
|
||||
for (const [name, section] of Object.entries(query)) {
|
||||
const methods: QueryableModuleStorage<ApiType> = {};
|
||||
const lazySection = (s: string) =>
|
||||
lazyMethods(Object.keys(query[s]), (m: string) =>
|
||||
this._decorateStorageEntry(query[s][m], decorateMethod)
|
||||
);
|
||||
|
||||
for (const [name, method] of Object.entries(section)) {
|
||||
methods[name] = this._decorateStorageEntry(method, decorateMethod);
|
||||
}
|
||||
const sections = Object.keys(query);
|
||||
|
||||
result[name] = methods;
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
lazyMethod(result, sections[i], lazySection);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -473,14 +475,15 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
protected _decorateStorageAt<ApiType extends ApiTypes> ({ query, registry }: DecoratedMeta, decorateMethod: DecorateMethod<ApiType>, blockHash: Uint8Array): QueryableStorageAt<ApiType> {
|
||||
const result = {} as QueryableStorageAt<ApiType>;
|
||||
|
||||
for (const [name, section] of Object.entries(query)) {
|
||||
const methods: QueryableModuleStorageAt<ApiType> = {};
|
||||
const lazySection = (s: string) =>
|
||||
lazyMethods(Object.keys(query[s]), (m: string) =>
|
||||
this._decorateStorageEntryAt(registry, query[s][m], decorateMethod, blockHash)
|
||||
);
|
||||
|
||||
for (const [name, method] of Object.entries(section)) {
|
||||
methods[name] = this._decorateStorageEntryAt(registry, method, decorateMethod, blockHash);
|
||||
}
|
||||
const sections = Object.keys(query);
|
||||
|
||||
result[name] = methods;
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
lazyMethod(result, sections[i], lazySection);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -59,21 +59,22 @@ function getVariantStringIdx ({ index }: SiVariant): string {
|
||||
function injectErrors (_: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, RegistryError>>): void {
|
||||
const lazySection = ({ errors, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, errors.unwrap()),
|
||||
({ docs, fields, index, name }: SiVariant): RegistryError => ({
|
||||
args: getFieldArgs(lookup, fields),
|
||||
docs: docs.map(valueToString),
|
||||
fields,
|
||||
index: index.toNumber(),
|
||||
method: name.toString(),
|
||||
name: name.toString(),
|
||||
section
|
||||
}),
|
||||
getVariantStringIdx
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, errors.unwrap()),
|
||||
({ docs, fields, index, name }: SiVariant): RegistryError => ({
|
||||
args: getFieldArgs(lookup, fields),
|
||||
docs: docs.map(valueToString),
|
||||
fields,
|
||||
index: index.toNumber(),
|
||||
method: name.toString(),
|
||||
name: name.toString(),
|
||||
section
|
||||
}),
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
@@ -91,24 +92,25 @@ function injectErrors (_: Registry, { lookup, pallets }: MetadataLatest, version
|
||||
function injectEvents (registry: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, Constructor<GenericEventData>>>): void {
|
||||
const lazySection = ({ events, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, events.unwrap()),
|
||||
(variant: SiVariant): Constructor<GenericEventData> => {
|
||||
const meta = registry.createType('EventMetadataLatest', {
|
||||
...variant,
|
||||
args: getFieldArgs(lookup, variant.fields)
|
||||
});
|
||||
|
||||
return class extends GenericEventData {
|
||||
constructor (registry: Registry, value: Uint8Array) {
|
||||
super(registry, value, meta, section, variant.name.toString());
|
||||
}
|
||||
};
|
||||
},
|
||||
getVariantStringIdx
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, events.unwrap()),
|
||||
(variant: SiVariant): Constructor<GenericEventData> => {
|
||||
const meta = registry.createType('EventMetadataLatest', {
|
||||
...variant,
|
||||
args: getFieldArgs(lookup, variant.fields)
|
||||
});
|
||||
|
||||
return class extends GenericEventData {
|
||||
constructor (registry: Registry, value: Uint8Array) {
|
||||
super(registry, value, meta, section, variant.name.toString());
|
||||
}
|
||||
};
|
||||
},
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
@@ -126,14 +128,15 @@ function injectEvents (registry: Registry, { lookup, pallets }: MetadataLatest,
|
||||
function injectExtrinsics (registry: Registry, { lookup, pallets }: MetadataLatest, version: number, result: Record<string, Record<string, CallFunction>>): void {
|
||||
const lazySection = ({ calls, name }: PalletMetadataLatest, index: number): void => {
|
||||
const section = stringCamelCase(name);
|
||||
const creator = () => lazyMethods(
|
||||
getVariants(lookup, calls.unwrap()),
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, index, section),
|
||||
getVariantStringIdx
|
||||
);
|
||||
|
||||
lazyMethod(result, index, creator);
|
||||
lazyMethod(result, index, () =>
|
||||
lazyMethods(
|
||||
getVariants(lookup, calls.unwrap()),
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, index, section),
|
||||
getVariantStringIdx
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
clearRecord(result);
|
||||
|
||||
@@ -24,17 +24,13 @@ export function decorateConstants (registry: Registry, { pallets }: MetadataLate
|
||||
const result: Constants = {};
|
||||
|
||||
const lazySection = ({ constants, name }: PalletMetadataLatest): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
constants,
|
||||
(constants: PalletConstantMetadataLatest[]) =>
|
||||
lazyMethods(
|
||||
constants,
|
||||
(constant: PalletConstantMetadataLatest) =>
|
||||
createConstantCodec(registry, constant),
|
||||
objectNameToCamel
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
constants,
|
||||
(constant: PalletConstantMetadataLatest) =>
|
||||
createConstantCodec(registry, constant),
|
||||
objectNameToCamel
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -42,17 +42,13 @@ export function decorateErrors (registry: Registry, { lookup, pallets }: Metadat
|
||||
const result: Errors = {};
|
||||
|
||||
const lazySection = ({ errors, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(errors.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createIsError(registry, lookup, variant, sectionIndex),
|
||||
objectNameToString
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(errors.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createIsError(registry, lookup, v, sectionIndex),
|
||||
objectNameToString
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -29,17 +29,13 @@ export function decorateEvents (registry: Registry, { lookup, pallets }: Metadat
|
||||
const result: Events = {};
|
||||
|
||||
const lazySection = ({ events, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(events.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createIsEvent(registry, lookup, variant, sectionIndex),
|
||||
objectNameToString
|
||||
),
|
||||
() => stringCamelCase(name)
|
||||
lazyMethod(result, stringCamelCase(name), () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(events.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createIsEvent(registry, lookup, v, sectionIndex),
|
||||
objectNameToString
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -48,17 +48,13 @@ export function decorateExtrinsics (registry: Registry, { lookup, pallets }: Met
|
||||
const lazySection = ({ calls, name }: PalletMetadataLatest, sectionIndex: number): void => {
|
||||
const sectionName = stringCamelCase(name);
|
||||
|
||||
lazyMethod(
|
||||
result,
|
||||
lookup.getSiType(calls.unwrap().type).def.asVariant.variants,
|
||||
(variants: SiVariant[]) =>
|
||||
lazyMethods(
|
||||
variants,
|
||||
(variant: SiVariant) =>
|
||||
createCallFunction(registry, lookup, variant, sectionIndex, sectionName),
|
||||
objectNameToCamel
|
||||
),
|
||||
() => sectionName
|
||||
lazyMethod(result, sectionName, () =>
|
||||
lazyMethods(
|
||||
lookup.getSiType(calls.unwrap().type).def.asVariant.variants,
|
||||
(v: SiVariant) =>
|
||||
createCallFunction(registry, lookup, v, sectionIndex, sectionName),
|
||||
objectNameToCamel
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
@@ -23,24 +23,20 @@ export function decorateStorage (registry: Registry, { pallets }: MetadataLatest
|
||||
const { items, prefix: _prefix } = storage.unwrap();
|
||||
const prefix = _prefix.toString();
|
||||
|
||||
lazyMethod(
|
||||
result,
|
||||
items,
|
||||
(items: StorageEntryMetadataLatest[]) =>
|
||||
lazyMethods(
|
||||
items,
|
||||
(meta: StorageEntryMetadataLatest) =>
|
||||
createFunction(registry, { meta, method: meta.name.toString(), prefix, section }, {}),
|
||||
objectNameFirstLower,
|
||||
{
|
||||
palletVersion: createRuntimeFunction(
|
||||
{ method: 'palletVersion', prefix, section },
|
||||
createKeyRaw(registry, { method: ':__STORAGE_VERSION__:', prefix: name.toString() }, [], [], []),
|
||||
{ docs: 'Returns the current pallet version from storage', type: 'u16' }
|
||||
)(registry)
|
||||
}
|
||||
),
|
||||
() => section
|
||||
lazyMethod(result, section, () =>
|
||||
lazyMethods(
|
||||
items,
|
||||
(meta: StorageEntryMetadataLatest) =>
|
||||
createFunction(registry, { meta, method: meta.name.toString(), prefix, section }, {}),
|
||||
objectNameFirstLower,
|
||||
{
|
||||
palletVersion: createRuntimeFunction(
|
||||
{ method: 'palletVersion', prefix, section },
|
||||
createKeyRaw(registry, { method: ':__STORAGE_VERSION__:', prefix: name.toString() }, [], [], []),
|
||||
{ docs: 'Returns the current pallet version from storage', type: 'u16' }
|
||||
)(registry)
|
||||
}
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user