Cleanup module-specific type-overrides (#1756)
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { createType, TypeRegistry } from '@polkadot/types/codec';
|
||||
import { TypeRegistry } from '@polkadot/types/codec';
|
||||
|
||||
import Metadata from '../';
|
||||
import latestSubstrateV0 from './static-substrate.json';
|
||||
@@ -12,14 +12,7 @@ import { defaultValues, toLatest } from '../util/testUtil';
|
||||
describe('Metadata', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('works with fallback', (): void => {
|
||||
const metadata = new Metadata(registry, rpcData);
|
||||
const metadataV0 = createType(registry, 'MetadataV0', rpcData);
|
||||
|
||||
expect(metadata.asV0.toString()).toEqual(metadataV0.toString());
|
||||
});
|
||||
|
||||
it('decodes latest substrate properly', (): void => {
|
||||
it('decodes into latest substrate', (): void => {
|
||||
const metadata = new Metadata(registry, rpcData);
|
||||
const json = metadata.asV0.toJSON();
|
||||
|
||||
|
||||
@@ -2679,7 +2679,7 @@
|
||||
"type": {
|
||||
"Map": {
|
||||
"key": "ProposalIndex",
|
||||
"value": "Proposal"
|
||||
"value": "TreasuryProposal"
|
||||
}
|
||||
},
|
||||
"fallback": "0x00",
|
||||
|
||||
@@ -10,17 +10,19 @@ import { getModuleTypes } from '@polkadot/types/known';
|
||||
import { Type, createType } from '@polkadot/types';
|
||||
import { stringCamelCase } from '@polkadot/util';
|
||||
|
||||
function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
|
||||
const override = Object.keys(sectionTypes).find((aliased): boolean => type.eq(aliased));
|
||||
|
||||
if (override) {
|
||||
type.setOverride(sectionTypes[override]);
|
||||
}
|
||||
}
|
||||
|
||||
// apply module-specific type overrides (always be done as part of toLatest)
|
||||
/** @internal */
|
||||
function convertCalls (registry: Registry, calls: FunctionMetadataV10[], sectionTypes: OverrideModuleType[]): FunctionMetadataLatest[] {
|
||||
function convertCalls (registry: Registry, calls: FunctionMetadataV10[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
|
||||
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
|
||||
args.forEach(({ type }): void => {
|
||||
const queued = sectionTypes.find(({ name }): boolean => type.eq(name));
|
||||
|
||||
if (queued) {
|
||||
type.setOverride(queued.override);
|
||||
}
|
||||
});
|
||||
args.forEach(({ type }): void => setTypeOverride(sectionTypes, type));
|
||||
|
||||
return createType(registry, 'FunctionMetadataLatest', { args, name, documentation });
|
||||
});
|
||||
@@ -28,7 +30,7 @@ function convertCalls (registry: Registry, calls: FunctionMetadataV10[], section
|
||||
|
||||
// apply module-specific storage type overrides (always part of toLatest)
|
||||
/** @internal */
|
||||
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV10, sectionTypes: OverrideModuleType[]): StorageMetadataLatest {
|
||||
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV10, sectionTypes: OverrideModuleType): StorageMetadataLatest {
|
||||
return createType(registry, 'StorageMetadataLatest', {
|
||||
items: items.map(({ documentation, fallback, modifier, name, type }): StorageEntryMetadataLatest => {
|
||||
let resultType: Type;
|
||||
@@ -41,11 +43,7 @@ function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV
|
||||
resultType = type.asPlain;
|
||||
}
|
||||
|
||||
const queued = sectionTypes.find(({ name }): boolean => resultType.eq(name));
|
||||
|
||||
if (queued) {
|
||||
resultType.setOverride(queued.override);
|
||||
}
|
||||
setTypeOverride(sectionTypes, resultType);
|
||||
|
||||
return createType(registry, 'StorageEntryMetadataLatest', { documentation, fallback, modifier, name, type });
|
||||
}),
|
||||
@@ -57,7 +55,7 @@ function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV
|
||||
function convertModule (registry: Registry, mod: ModuleMetadataV10): ModuleMetadataLatest {
|
||||
const calls = mod.calls.unwrapOr(null);
|
||||
const storage = mod.storage.unwrapOr(null);
|
||||
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString())) || [];
|
||||
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString()));
|
||||
|
||||
return createType(registry, 'ModuleMetadataLatest', {
|
||||
...mod,
|
||||
|
||||
Reference in New Issue
Block a user