Files
api/packages/api-metadata/src/extrinsics/fromMetadata/index.ts
T
Jaco Greeff 51d589e26b Readability on Metadata & ExtrinsicSignature (#1314)
* Readbility on Metadata & ExtrinsicSignature

* Version  payload

* Adjust versioning

* De-dup asVx
2019-08-16 11:25:38 +02:00

36 lines
1.3 KiB
TypeScript

// Copyright 2017-2019 @polkadot/api-metadata 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 { Calls, ModulesWithCalls } from '@polkadot/types/types';
import { ModuleMetadata } from '@polkadot/types/Metadata/v7/Metadata';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase } from '@polkadot/util';
import extrinsics from '../';
import createUnchecked from './createUnchecked';
/**
* Return an object with the modules and the functions to use extrinsics.
*
* @param metadata - The metadata
*/
export default function fromMetadata (metadata: Metadata): ModulesWithCalls {
return metadata.asLatest.modules
.filter(({ calls }): boolean => calls.isSome)
.reduce((result, modul: ModuleMetadata, sectionIndex): ModulesWithCalls => {
const section = stringCamelCase(modul.name.toString());
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 Calls);
return result;
}, { ...extrinsics });
}