Method -> Call, expose findMethod on api (#1222)

* Rename Method -> Call (interface, align with Rust)

* Export Call types from types, expose findCall
This commit is contained in:
Jaco Greeff
2019-07-29 09:04:37 +02:00
committed by GitHub
parent a06db4cfb9
commit fe35527a65
37 changed files with 164 additions and 161 deletions
@@ -2,16 +2,16 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Method } from '@polkadot/types/interfaces';
import { Call } from '@polkadot/types/interfaces';
import { CallFunction } from '@polkadot/types/types';
import { FunctionMetadata } from '@polkadot/types/Metadata/v7/Calls';
import { MethodFunction } from '@polkadot/types/primitive/Generic/Method';
import { createType } 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]].
* that will return the an [[CallFunction]].
*
* @param section - Name of the module section.
* @param sectionIndex - Index of the module section in the modules array.
@@ -23,17 +23,17 @@ export default function createDescriptor (
sectionIndex: number,
methodIndex: number,
callMetadata: FunctionMetadata
): MethodFunction {
): CallFunction {
const callIndex = new Uint8Array([sectionIndex, methodIndex]);
const expectedArgs = callMetadata.args;
const funcName = stringCamelCase(callMetadata.name.toString());
const extrinsicFn = (...args: any[]): Method => {
const extrinsicFn = (...args: any[]): Call => {
assert(
expectedArgs.length.valueOf() === args.length,
`Extrinsic ${section}.${funcName} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`
);
return createType('Method', {
return createType('Call', {
args,
callIndex
}, callMetadata);
@@ -46,5 +46,5 @@ export default function createDescriptor (
extrinsicFn.toJSON = (): any =>
callMetadata.toJSON();
return extrinsicFn as MethodFunction;
return extrinsicFn as CallFunction;
}
@@ -2,8 +2,9 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Calls, ModulesWithCalls } from '@polkadot/types/types';
import { ModuleMetadata } from '@polkadot/types/Metadata/v7/Metadata';
import { Methods, ModulesWithMethods } from '@polkadot/types/primitive/Generic/Method';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase } from '@polkadot/util';
@@ -15,19 +16,19 @@ import createUnchecked from './createUnchecked';
*
* @param metadata - The metadata
*/
export default function fromMetadata (metadata: Metadata): ModulesWithMethods {
export default function fromMetadata (metadata: Metadata): ModulesWithCalls {
return metadata.asV7.modules
.filter((modul): boolean => modul.calls.isSome)
.reduce((result, modul: ModuleMetadata, sectionIndex): ModulesWithMethods => {
.reduce((result, modul: ModuleMetadata, sectionIndex): ModulesWithCalls => {
const section = stringCamelCase(modul.name.toString());
result[section] = modul.calls.unwrap().reduce((newModule, callMetadata, methodIndex): Methods => {
result[section] = modul.calls.unwrap().reduce((newModule, callMetadata, methodIndex): Calls => {
const method = stringCamelCase(callMetadata.name.toString());
newModule[method] = createUnchecked(section, sectionIndex, methodIndex, callMetadata);
return newModule;
}, {} as unknown as Methods);
}, {} as unknown as Calls);
return result;
}, { ...extrinsics });
@@ -2,8 +2,8 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { ModulesWithMethods } from '@polkadot/types/primitive/Generic/Method';
import { ModulesWithCalls } from '@polkadot/types/types';
const extrinsics: ModulesWithMethods = {};
const extrinsics: ModulesWithCalls = {};
export default extrinsics;
@@ -2,13 +2,14 @@
// 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 { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/static';
import { ModulesWithMethods } from '@polkadot/types/primitive/Generic/Method';
import fromMetadata from './fromMetadata';
const staticMetadata: ModulesWithMethods = fromMetadata(
const staticMetadata: ModulesWithCalls = fromMetadata(
new Metadata(metadataRpc)
);
export default staticMetadata;