refactor: Metadata overhaul (#913)

* Replace part1

* Replace part2

* Replace part3

* Replace part5

* Replace part6

* Fix small bugs

* Fix double map toString

* Small fixes

* Update to v4

* Fix docs script

* Grumbles

* Alphabetical

* Reads better

* comment

* module to modul
This commit is contained in:
Amaury Martiny
2019-06-12 16:45:45 +12:00
committed by Ian He
parent 194077033a
commit 2699dcfc9c
77 changed files with 1198 additions and 4550 deletions
@@ -1,58 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { RuntimeModuleMetadata } from '@plugnet/types/Metadata/v0/Modules';
import { Methods, ModulesWithMethods } from '@plugnet/types/primitive/Method';
import MetadataV0 from '@plugnet/types/Metadata/v0';
import { stringCamelCase } from '@plugnet/util';
import createUnchecked from './utils/createUnchecked';
import extrinsics from '.';
/**
* Extend a storage object with the storage modules & module functions present
* in the metadata.
*
* @param extrinsics - An extrinsics object to be extended.
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: MetadataV0): ModulesWithMethods {
let indexCount = -1;
const findIndex = (prefix: string): number => {
indexCount++;
const mod = metadata.calls.find((item) => item.prefix.toString() === prefix);
if (!mod) {
console.error(`Unable to find module index for '${prefix}'`);
// compatible with old versions
return indexCount;
}
return mod.index.toNumber();
};
return metadata.modules.reduce((result, meta: RuntimeModuleMetadata) => {
if (!meta.module.call || !meta.module.call.functions.length) {
return result;
}
const prefix = stringCamelCase(meta.prefix.toString());
const index = findIndex(meta.prefix.toString());
result[prefix] = meta.module.call.functions.reduce((newModule, funcMeta) => {
// extrinsics.balances.set_balance -> extrinsics.balances.setBalance
const funcName = stringCamelCase(funcMeta.name.toString());
newModule[funcName] = createUnchecked(prefix, funcName, index, funcMeta);
return newModule;
}, {} as Methods);
return result;
}, { ...extrinsics });
}
@@ -0,0 +1,48 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { FunctionMetadata } from '@polkadot/types/Metadata/v4/Calls';
import { MethodFunction } from '@polkadot/types/primitive/Method';
import { Method } from '@polkadot/types';
import { assert, stringCamelCase } from '@polkadot/util';
/**
* From the metadata of a function in the module's storage, generate the function
* that will return the an [[MethodFunction]].
*
* @param section - Name of the module section.
* @param sectionIndex - Index of the module section in the modules array.
* @param methodIndex - Index of the method inside the section.
* @param callMetadata - Metadata of the call function.
*/
export default function createDescriptor (
section: string,
sectionIndex: number,
methodIndex: number,
callMetadata: FunctionMetadata
): MethodFunction {
const callIndex = new Uint8Array([sectionIndex, methodIndex]);
const expectedArgs = callMetadata.args;
const funcName = stringCamelCase(callMetadata.name.toString());
const extrinsicFn = (...args: any[]): Method => {
assert(
expectedArgs.length.valueOf() === args.length,
`Extrinsic ${section}.${funcName} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`
);
return new Method({
args,
callIndex
}, callMetadata);
};
extrinsicFn.callIndex = callIndex;
extrinsicFn.meta = callMetadata;
extrinsicFn.method = funcName;
extrinsicFn.section = section;
extrinsicFn.toJSON = (): any =>
callMetadata.toJSON();
return extrinsicFn as MethodFunction;
}
@@ -3,13 +3,13 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Extrinsic, Metadata } from '@plugnet/types';
import json from '@plugnet/types/Metadata/v0/static';
import json from '@plugnet/types/Metadata/v4/static';
import fromMetadata from './fromMetadata';
import fromV4 from '.';
// Use the pre-generated metadata
const metadata = new Metadata(json).asV0;
const newExtrinsics = fromMetadata(metadata);
const metadata = new Metadata(json);
const newExtrinsics = fromV4(metadata);
describe('fromMetadata', () => {
it('should throw if an incorrect number of args is supplied', () => {
@@ -0,0 +1,36 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ModuleMetadata } from '@polkadot/types/Metadata/v4/Metadata';
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Method';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase } from '@polkadot/util';
import extrinsics from '../';
import createUnchecked from './createUnchecked';
/**
* Extend a storage object with the storage modules & module functions present
* in the metadata.
*
* @param extrinsics - An extrinsics object to be extended.
* @param metadata - The metadata to extend the storage object against.
*/
export default function fromMetadata (metadata: Metadata): ModulesWithMethods {
return metadata.asV4.modules
.filter((modul) => modul.calls.isSome)
.reduce((result, modul: ModuleMetadata, sectionIndex) => {
const prefix = stringCamelCase(modul.prefix.toString());
result[prefix] = modul.calls.unwrap().reduce((newModule, callMetadata, methodIndex) => {
const funcName = stringCamelCase(callMetadata.name.toString());
newModule[funcName] = createUnchecked(prefix, sectionIndex, methodIndex, callMetadata);
return newModule;
}, {} as Methods);
return result;
}, { ...extrinsics });
}
+2 -2
View File
@@ -3,12 +3,12 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { Metadata } from '@plugnet/types';
import metadataRpc from '@plugnet/types/Metadata/v0/static';
import metadataRpc from '@plugnet/types/Metadata/v4/static';
import { ModulesWithMethods } from '@plugnet/types/primitive/Method';
import fromMetadata from './fromMetadata';
const staticMetadata: ModulesWithMethods = fromMetadata(
new Metadata(metadataRpc).asV0
new Metadata(metadataRpc)
);
export default staticMetadata;
@@ -1,44 +0,0 @@
// Copyright 2017-2019 @polkadot/extrinsics authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { FunctionMetadata } from '@plugnet/types/Metadata/v0/Modules';
import { MethodFunction } from '@plugnet/types/primitive/Method';
import { Method } from '@plugnet/types';
import { assert } from '@plugnet/util';
/**
* From the metadata of a function in the module's storage, generate the function
* that will return the an UncheckExtrinsic.
*
* @param index - Index of the module section in the modules array.
*/
export default function createDescriptor (
section: string,
method: string,
index: number,
meta: FunctionMetadata
): MethodFunction {
const callIndex = new Uint8Array([index, meta.id.toNumber()]);
let extrinsicFn: any;
const expectedArgs = Method.filterOrigin(meta);
extrinsicFn = (...args: any[]): Method => {
assert(expectedArgs.length.valueOf() === args.length, `Extrinsic ${section}.${method} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`);
return new Method({
args,
callIndex
}, meta);
};
extrinsicFn.callIndex = callIndex;
extrinsicFn.meta = meta;
extrinsicFn.method = method;
extrinsicFn.section = section;
extrinsicFn.toJSON = (): any =>
meta.toJSON();
return extrinsicFn as MethodFunction;
}