Export meta on consts (#1119)

* Export meta on consts

* Comment around meta extension

* Add meta manual check
This commit is contained in:
Jaco Greeff
2019-07-15 11:13:59 +02:00
committed by GitHub
parent 1653b3598b
commit 6c9fe763e1
3 changed files with 25 additions and 9 deletions
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Constants, ConstantCodec, ModuleConstants } from './types';
import { createType } from '@polkadot/types';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase } from '@polkadot/util';
import { Constants, ModuleConstants } from './types';
/**
* Retrieve the parameter types (module constants) from the runtime metadata.
*
@@ -22,8 +22,17 @@ export default function fromMetadata (metadata: Metadata): Constants {
const { name } = moduleMetadata;
// For access, we change the index names, i.e. Democracy.EnactmentPeriod -> democracy.enactmentPeriod
result[stringCamelCase(name.toString())] = moduleMetadata.constants.reduce((newModule, constantMeta): ModuleConstants => {
newModule[stringCamelCase(constantMeta.name.toString())] = createType(constantMeta.type, constantMeta.value);
result[stringCamelCase(name.toString())] = moduleMetadata.constants.reduce((newModule, meta): ModuleConstants => {
const codec = createType(meta.type, meta.value);
const ccodec = codec as ConstantCodec;
// This is not a perfect idea, however as it stands with number-only constants on the metadata
// does not have any effect. However, this could become problematic in cases where items are
// exposed that contain their own metadata. As of now, the compatibility with current, e.g.
// storage is the driving factor, one consistent way of handling interfaces
ccodec.meta = meta;
newModule[stringCamelCase(meta.name.toString())] = ccodec;
return newModule;
}, {} as unknown as ModuleConstants);
@@ -3,11 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec } from '@polkadot/types/types';
import { ModuleConstantMetadata as MetaV6 } from '@polkadot/types/Metadata/v6/Constants';
export interface ModuleConstants {
[key: string]: Codec;
export interface ConstantCodec extends Codec {
meta: MetaV6;
}
export interface Constants {
[key: string]: ModuleConstants; // Will hold modules returned by state_getMetadata
}
export type ModuleConstants = Record<string, ConstantCodec>;
export type Constants = Record<string, ModuleConstants>;