Call.injectMetadata (align with Events) (#1462)

* Call.injectMetadata (align with Events)

* Fix interface builds

* Fix md builds

* Fixup tests with injection
This commit is contained in:
Jaco Greeff
2019-10-10 11:05:01 +02:00
committed by GitHub
parent 2870513453
commit b8d4bc5c6a
27 changed files with 125 additions and 92 deletions
@@ -14,7 +14,7 @@ import { stringCamelCase } from '@polkadot/util';
* @param metadata - The metadata
*/
export default function fromMetadata (metadata: Metadata): Constants {
return metadata.asLatest.modules.reduce((result, moduleMetadata): Constants => {
return metadata.asLatest.modules.reduce((result: Constants, moduleMetadata): Constants => {
if (moduleMetadata.constants.isEmpty) {
return result;
}
@@ -22,7 +22,7 @@ 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, meta): ModuleConstants => {
result[stringCamelCase(name.toString())] = moduleMetadata.constants.reduce((newModule: ModuleConstants, meta): ModuleConstants => {
const codec = createTypeUnsafe(meta.type.toString(), [meta.value]);
// This is not a perfect idea, however as it stands with number-only constants on the metadata
@@ -33,8 +33,8 @@ export default function fromMetadata (metadata: Metadata): Constants {
newModule[stringCamelCase(meta.name.toString())] = codec;
return newModule;
}, {} as unknown as ModuleConstants);
}, {});
return result;
}, {} as unknown as Constants);
}, {});
}
@@ -5,7 +5,7 @@
import { Call, FunctionMetadataLatest } from '@polkadot/types/interfaces';
import { CallFunction } from '@polkadot/types/types';
import { createType } from '@polkadot/types';
import { createType } from '@polkadot/types/codec';
import { assert, stringCamelCase } from '@polkadot/util';
/**
@@ -19,16 +19,16 @@ import createUnchecked from './createUnchecked';
export default function fromMetadata (metadata: Metadata): ModulesWithCalls {
return metadata.asLatest.modules
.filter(({ calls }): boolean => calls.isSome)
.reduce((result, modul: ModuleMetadataV8, sectionIndex): ModulesWithCalls => {
const section = stringCamelCase(modul.name.toString());
.reduce((result, { calls, name }: ModuleMetadataV8, sectionIndex): ModulesWithCalls => {
const section = stringCamelCase(name.toString());
result[section] = modul.calls.unwrap().reduce((newModule, callMetadata, methodIndex): Calls => {
result[section] = calls.unwrap().reduce((newModule: Calls, callMetadata, methodIndex): Calls => {
const method = stringCamelCase(callMetadata.name.toString());
newModule[method] = createUnchecked(section, sectionIndex, methodIndex, callMetadata);
return newModule;
}, {} as unknown as Calls);
}, {});
return result;
}, { ...extrinsics });
@@ -2,17 +2,26 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ModulesWithCalls } from '@polkadot/types/types';
import '@polkadot/types/injector';
import BN from 'bn.js';
import testingPairs from '@polkadot/keyring/testingPairs';
import { createType } from '@polkadot/types';
import { createType, Metadata } from '@polkadot/types';
import metadataStatic from '@polkadot/types/Metadata/static';
import extrinsics from './static';
import fromMetadata from './fromMetadata';
const keyring = testingPairs({ type: 'ed25519' }, false);
describe('extrinsics', (): void => {
let extrinsics: ModulesWithCalls;
beforeEach((): void => {
extrinsics = fromMetadata(new Metadata(metadataStatic));
});
it('encodes an actual transfer (actual data)', (): void => {
expect(
createType('Extrinsic',
@@ -4,7 +4,7 @@
import { ModulesWithCalls } from '@polkadot/types/types';
import { Metadata } from '@polkadot/types';
import Metadata from '@polkadot/types/Metadata';
import metadataRpc from '@polkadot/types/Metadata/static';
import fromMetadata from './fromMetadata';
@@ -5,8 +5,7 @@
import { Codec } from '@polkadot/types/types';
import BN from 'bn.js';
import { Bytes, Compact, StorageKey, U8a } from '@polkadot/types';
import { createType, createTypeUnsafe } from '@polkadot/types/codec';
import { Compact, U8a, createType, createTypeUnsafe } from '@polkadot/types/codec';
import { StorageEntryMetadata, StorageEntryType } from '@polkadot/types/Metadata/v8/Storage';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';
@@ -123,13 +122,13 @@ function extendLinkedMap ({ meta: { documentation, name, type } }: CreateItemFn,
name,
modifier: createType('StorageEntryModifierLatest', 1), // required
type: new StorageEntryType(createType('PlainTypeLatest', type.asMap.key), 0),
fallback: new Bytes(createTypeUnsafe(type.asMap.key.toString()).toHex()),
fallback: createType('Bytes', createTypeUnsafe(type.asMap.key.toString()).toHex()),
documentation
});
// here we pass the section/method through as well - these are not on
// the function itself, so specify these explicitly to the constructor
storageFn.headKey = new StorageKey(headFn, {
storageFn.headKey = createType('StorageKey', headFn, {
method: storageFn.method,
section: `head of ${storageFn.section}`
});
@@ -5,18 +5,12 @@
import '@polkadot/types/injector';
import testingPairs from '@polkadot/keyring/testingPairs';
import { Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/static';
import { u8aToHex } from '@polkadot/util';
import fromMetadata from '../fromMetadata';
import storage from '../static';
const keyring = testingPairs({ type: 'ed25519' });
// Use the pre-generated metadata
const metadata = new Metadata(json);
const storage = fromMetadata(metadata);
describe('fromMetadata', (): void => {
it('should throw if the storage function expects an argument', (): void => {
expect((): any => storage.balances.freeBalance()).toThrowError(/requires one argument/);
@@ -4,7 +4,7 @@
import { StorageEntryMetadata, StorageEntryType } from '@polkadot/types/Metadata/v8/Storage';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { createType, Text, Vec } from '@polkadot/types';
import { createType } from '@polkadot/types/codec';
import createFunction from './createFunction';
@@ -18,7 +18,7 @@ const createRuntimeFunction = (method: string, key: string, { documentation, typ
createFunction(
{
meta: {
documentation: new Vec(Text, [documentation]),
documentation: createType('Vec<Text>', [documentation]),
modifier: createType('StorageEntryModifierLatest', 1), // required
type: new StorageEntryType(type, 0),
toJSON: (): any => key