Apply aliases for constants (#3003)

* Apply aliasses for constants

* Remove todo

* shortcut

* Re-add metaVersion
This commit is contained in:
Jaco Greeff
2020-12-29 22:17:31 +01:00
committed by GitHub
parent 1ca00c7633
commit 361f9e4419
+32 -26
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { EventMetadataLatest, EventMetadataV12, FunctionMetadataLatest, FunctionMetadataV12, MetadataLatest, MetadataV12, ModuleMetadataLatest, ModuleMetadataV12, StorageEntryMetadataLatest, StorageMetadataLatest, StorageMetadataV12 } from '@polkadot/types/interfaces/metadata';
import type { EventMetadataLatest, EventMetadataV12, FunctionMetadataLatest, FunctionMetadataV12, MetadataLatest, MetadataV12, ModuleConstantMetadataLatest, ModuleConstantMetadataV12, ModuleMetadataLatest, ModuleMetadataV12, StorageEntryMetadataLatest, StorageMetadataLatest, StorageMetadataV12 } from '@polkadot/types/interfaces/metadata';
import type { OverrideModuleType, Registry } from '@polkadot/types/types';
import { Type } from '@polkadot/types/primitive';
@@ -16,8 +16,6 @@ const KNOWN_ORIGINS: Record<string, string> = {
TechnicalCommittee: 'CollectiveOrigin'
};
// TODO Handle consts as well
/**
* Find and apply the correct type override
* @internal
@@ -45,10 +43,22 @@ function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
* @internal
**/
function convertCalls (registry: Registry, calls: FunctionMetadataV12[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
args.forEach(({ type }) => setTypeOverride(sectionTypes, type));
return calls.map((c): FunctionMetadataLatest => {
c.args.forEach(({ type }) => setTypeOverride(sectionTypes, type));
return registry.createType('FunctionMetadataLatest', { args, documentation, name });
return registry.createType('FunctionMetadataLatest', c);
});
}
/**
* Apply module-specific type overrides (always be done as part of toLatest)
* @internal
*/
function convertConstants (registry: Registry, constants: ModuleConstantMetadataV12[], sectionTypes: OverrideModuleType): ModuleConstantMetadataLatest[] {
return constants.map((c): ModuleConstantMetadataLatest => {
setTypeOverride(sectionTypes, c.type);
return registry.createType('ModuleConstantMetadataLatest', c);
});
}
@@ -57,10 +67,10 @@ function convertCalls (registry: Registry, calls: FunctionMetadataV12[], section
* @internal
**/
function convertEvents (registry: Registry, events: EventMetadataV12[], sectionTypes: OverrideModuleType): EventMetadataLatest[] {
return events.map(({ args, documentation, name }): EventMetadataLatest => {
args.forEach((type) => setTypeOverride(sectionTypes, type));
return events.map((e): EventMetadataLatest => {
e.args.forEach((type) => setTypeOverride(sectionTypes, type));
return registry.createType('EventMetadataLatest', { args, documentation, name });
return registry.createType('EventMetadataLatest', e);
});
}
@@ -70,20 +80,20 @@ function convertEvents (registry: Registry, events: EventMetadataV12[], sectionT
**/
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV12, sectionTypes: OverrideModuleType): StorageMetadataLatest {
return registry.createType('StorageMetadataLatest', {
items: items.map(({ documentation, fallback, modifier, name, type }): StorageEntryMetadataLatest => {
items: items.map((s): StorageEntryMetadataLatest => {
let resultType: Type;
if (type.isMap) {
resultType = type.asMap.value;
} else if (type.isDoubleMap) {
resultType = type.asDoubleMap.value;
if (s.type.isMap) {
resultType = s.type.asMap.value;
} else if (s.type.isDoubleMap) {
resultType = s.type.asDoubleMap.value;
} else {
resultType = type.asPlain;
resultType = s.type.asPlain;
}
setTypeOverride(sectionTypes, resultType);
return registry.createType('StorageEntryMetadataLatest', { documentation, fallback, modifier, name, type });
return registry.createType('StorageEntryMetadataLatest', s);
}),
prefix
});
@@ -113,20 +123,15 @@ function registerOriginCaller (registry: Registry, modules: ModuleMetadataV12[],
}
/** @internal */
function createModule (registry: Registry, mod: ModuleMetadataV12, { calls, events, storage }: { calls: FunctionMetadataV12[] | null, events: EventMetadataV12[] | null, storage: StorageMetadataV12 | null }): ModuleMetadataLatest {
function createModule (registry: Registry, mod: ModuleMetadataV12, { calls, constants, events, storage }: { calls: FunctionMetadataV12[] | null, constants: ModuleConstantMetadataV12[], events: EventMetadataV12[] | null, storage: StorageMetadataV12 | null }): ModuleMetadataLatest {
const sectionTypes = getModuleTypes(registry, stringCamelCase(mod.name));
return registry.createType('ModuleMetadataLatest', {
...mod,
calls: calls
? convertCalls(registry, calls, sectionTypes)
: null,
events: events
? convertEvents(registry, events, sectionTypes)
: null,
storage: storage
? convertStorage(registry, storage, sectionTypes)
: null
calls: calls && convertCalls(registry, calls, sectionTypes),
constants: convertConstants(registry, constants, sectionTypes),
events: events && convertEvents(registry, events, sectionTypes),
storage: storage && convertStorage(registry, storage, sectionTypes)
});
}
@@ -142,6 +147,7 @@ export function toLatest (registry: Registry, { extrinsic, modules }: MetadataV1
extrinsic,
modules: modules.map((mod) => createModule(registry, mod, {
calls: mod.calls.unwrapOr(null),
constants: mod.constants,
events: mod.events.unwrapOr(null),
storage: mod.storage.unwrapOr(null)
}))