Add runtime decoration defaults (#4993)
* Add runtime decoration defaults * Default exports * Additional * hash + version
This commit is contained in:
@@ -8,7 +8,8 @@ import type { Option, Raw, StorageKey, Text, u64 } from '@polkadot/types';
|
||||
import type { Call, Hash, RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
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 { AnyFunction, AnyTuple, CallFunction, Codec, DefinitionRpc, DefinitionRpcSub, Definitions, DefinitionsCall, DetectCodec, IMethod, IStorageKey, Registry, RegistryError, RegistryTypes } from '@polkadot/types/types';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { SubmittableExtrinsic } from '../submittable/types';
|
||||
import type { ApiDecoration, ApiInterfaceRx, ApiOptions, ApiTypes, AugmentedQuery, DecoratedErrors, DecoratedEvents, DecoratedRpc, DecorateMethod, GenericStorageEntryFunction, PaginationOptions, QueryableConsts, QueryableStorage, QueryableStorageEntry, QueryableStorageEntryAt, QueryableStorageMulti, QueryableStorageMultiArg, SubmittableExtrinsicFunction, SubmittableExtrinsics } from '../types';
|
||||
import type { VersionedRegistry } from './types';
|
||||
@@ -18,9 +19,9 @@ import { BehaviorSubject, combineLatest, from, map, of, switchMap, tap, toArray
|
||||
import { 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, Metadata, typeDefinitions, TypeRegistry, unwrapStorageType } from '@polkadot/types';
|
||||
import { arrayChunk, arrayFlatten, assert, assertReturn, BN, compactStripLength, lazyMethod, lazyMethods, logger, nextTick, objectSpread, stringCamelCase, u8aConcatStrict, u8aToHex } from '@polkadot/util';
|
||||
import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
import { blake2AsHex } from '@polkadot/util-crypto';
|
||||
|
||||
import { createSubmittable } from '../submittable';
|
||||
import { augmentObject } from '../util/augmentObject';
|
||||
@@ -413,48 +414,88 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
|
||||
return out as DecoratedRpc<ApiType, RpcInterface>;
|
||||
}
|
||||
|
||||
// extract all runtime definitions
|
||||
protected _getRuntimeDefs (): DefinitionsCall {
|
||||
const result = objectSpread<DefinitionsCall>({}, this._options.runtime);
|
||||
const defValues = Object.values(typeDefinitions) as Definitions[];
|
||||
|
||||
for (let i = 0; i < defValues.length; i++) {
|
||||
const rt = defValues[i].runtime;
|
||||
|
||||
if (rt) {
|
||||
const apiEntries = Object.entries(rt);
|
||||
|
||||
for (let j = 0; j < apiEntries.length; j++) {
|
||||
const [key, defs] = apiEntries[j];
|
||||
|
||||
if (!result[key]) {
|
||||
// we don't have this runtime definition, add it
|
||||
result[key] = defs;
|
||||
} else {
|
||||
for (let k = 0; k < defs.length; k++) {
|
||||
const def = defs[k];
|
||||
|
||||
if (!result[key].some(({ version }) => def.version === version)) {
|
||||
// we don't have this specific version, add it
|
||||
result[key].push(def);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// pre-metadata decoration
|
||||
protected _decorateCalls<ApiType extends ApiTypes> ({ registry, runtimeVersion: { apis } }: VersionedRegistry<ApiType>, decorateMethod: DecorateMethod<ApiType>, blockHash?: Uint8Array | string | null): QueryableCalls<ApiType> {
|
||||
const result = {} as QueryableCalls<ApiType>;
|
||||
|
||||
if (!this._options.runtime) {
|
||||
return result;
|
||||
}
|
||||
|
||||
const named: Record<string, Record<string, DefinitionCallNamed>> = {};
|
||||
const sections = Object.entries(this._options.runtime);
|
||||
const sections = Object.entries(this._getRuntimeDefs());
|
||||
const hashes: HexString[] = [];
|
||||
|
||||
for (let i = 0; i < sections.length; i++) {
|
||||
const [_section, secs] = sections[i];
|
||||
const sectionHash = blake2AsU8a(_section, 64);
|
||||
const sectionHash = blake2AsHex(_section, 64);
|
||||
const rtApi = apis.find(([a]) => a.eq(sectionHash));
|
||||
|
||||
hashes.push(sectionHash);
|
||||
|
||||
if (rtApi) {
|
||||
const sec = secs.find(({ version = 1 }) => rtApi[1].eq(version));
|
||||
const sec = secs.find(({ version }) => rtApi[1].eq(version));
|
||||
|
||||
if (sec) {
|
||||
const section = stringCamelCase(_section);
|
||||
|
||||
if (!named[section]) {
|
||||
named[section] = {};
|
||||
}
|
||||
|
||||
const methods = Object.entries(sec.methods);
|
||||
|
||||
for (let m = 0; m < methods.length; m++) {
|
||||
const [_method, def] = methods[m];
|
||||
const method = stringCamelCase(_method);
|
||||
if (methods.length) {
|
||||
if (!named[section]) {
|
||||
named[section] = {};
|
||||
}
|
||||
|
||||
named[section][method] = objectSpread({ method, name: `${_section}_${_method}`, section, sectionHash, version: 1 }, def);
|
||||
for (let m = 0; m < methods.length; m++) {
|
||||
const [_method, def] = methods[m];
|
||||
const method = stringCamelCase(_method);
|
||||
|
||||
named[section][method] = objectSpread({ method, name: `${_section}_${_method}`, section, sectionHash }, def);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
l.warn(`Not decorating runtime ${_section}, unable to find version ${rtApi[1].toString()}`);
|
||||
}
|
||||
} else {
|
||||
l.warn(`Not decorating runtime ${_section}, not found in available apis`);
|
||||
}
|
||||
}
|
||||
|
||||
// find the runtimes that we don't have hashes for
|
||||
const notFound = apis
|
||||
.filter(([a]) => !hashes.includes(a.toHex()))
|
||||
.map(([a, v]) => `${a.toHex()}/${v.toString()}`);
|
||||
|
||||
if (notFound.length) {
|
||||
l.warn(`Not decorating runtime apis ${notFound.join(', ')}`);
|
||||
}
|
||||
|
||||
const stateCall = blockHash
|
||||
? (name: string, bytes: Uint8Array) => this._rpcCore.state.call(name, bytes, blockHash)
|
||||
: (name: string, bytes: Uint8Array) => this._rpcCore.state.call(name, bytes);
|
||||
|
||||
Reference in New Issue
Block a user