From 6c9fe763e14e85ff030ea4977fe7c6d7e3ce4a3d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 15 Jul 2019 11:13:59 +0200 Subject: [PATCH] Export meta on consts (#1119) * Export meta on consts * Comment around meta extension * Add meta manual check --- .../src/consts/fromMetadata/index.ts | 17 +++++++++++++---- .../src/consts/fromMetadata/types.ts | 11 ++++++----- packages/api/src/checkTypes.manual.ts | 6 ++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/packages/api-metadata/src/consts/fromMetadata/index.ts b/packages/api-metadata/src/consts/fromMetadata/index.ts index e00647b1e2..70244e5e98 100644 --- a/packages/api-metadata/src/consts/fromMetadata/index.ts +++ b/packages/api-metadata/src/consts/fromMetadata/index.ts @@ -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); diff --git a/packages/api-metadata/src/consts/fromMetadata/types.ts b/packages/api-metadata/src/consts/fromMetadata/types.ts index 0e14ac0b69..a3552ed0ff 100644 --- a/packages/api-metadata/src/consts/fromMetadata/types.ts +++ b/packages/api-metadata/src/consts/fromMetadata/types.ts @@ -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; + +export type Constants = Record; diff --git a/packages/api/src/checkTypes.manual.ts b/packages/api/src/checkTypes.manual.ts index 1ec2f9d695..e5dc317cbb 100644 --- a/packages/api/src/checkTypes.manual.ts +++ b/packages/api/src/checkTypes.manual.ts @@ -49,6 +49,12 @@ export default async function test (): Promise { console.log('current author:', header.author); }); + // constants has actual value & metadata + console.log( + api.consts.balances.creationFee.toHex(), + api.consts.balances.creationFee.meta.documentation.map((s) => s.toString()).join('') + ); + const transfer = api.tx.balances.transfer(keyring.bob.address, 12345); console.log('transfer as Method', transfer as IMethod);