Cleanup module-specific type-overrides (#1756)

This commit is contained in:
Jaco Greeff
2020-01-28 16:18:40 +01:00
committed by GitHub
parent cedbd8fa06
commit 7de647ec0b
7 changed files with 34 additions and 51 deletions
@@ -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",
+13 -15
View File
@@ -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,
@@ -10,8 +10,6 @@ const ALLOWED_BOXES = ['BTreeMap', 'BTreeSet', 'Compact', 'Linkage', 'Result', '
const mappings: Mapper[] = [
// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
_alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport'),
// alias TreasuryProposal from Proposal<T::AccountId, BalanceOf<T>>
_alias('Proposal<T::AccountId, BalanceOf<T>>', 'TreasuryProposal'),
// <T::Balance as HasCompact>
_cleanupCompact(),
// Remove all the trait prefixes
+2 -2
View File
@@ -48,6 +48,6 @@ export function getChainTypes (chainName: Text, { specName, specVersion }: Runti
// get types for specific modules (metadata override)
/** @internal */
export function getModuleTypes (section: string): OverrideModuleType[] {
return TYPES_MODULES[section] || [];
export function getModuleTypes (section: string): OverrideModuleType {
return TYPES_MODULES[section] || {};
}
+15 -18
View File
@@ -4,24 +4,21 @@
import { OverrideModuleType, OverrideVersionedType } from './types';
// type overrides for modules (where dupliacted between modules do exist)
const TYPES_MODULES: Record<string, OverrideModuleType[]> = {
identity: [
{
name: 'Judgement',
override: 'IdentityJudgement'
}
],
society: [
{
name: 'Judgement',
override: 'SocietyJudgement'
},
{
name: 'Vote',
override: 'SocietyVote'
}
]
// type overrides for modules (where duplication between modules exist)
const TYPES_MODULES: Record<string, OverrideModuleType> = {
contracts: {
StorageKey: 'ContractStorageKey'
},
identity: {
Judgement: 'IdentityJudgement'
},
society: {
Judgement: 'SocietyJudgement',
Vote: 'SocietyVote'
},
treasury: {
Proposal: 'TreasuryProposal'
}
};
// these are override types for Polkadot & Kusama chains
+1 -4
View File
@@ -9,7 +9,4 @@ export interface OverrideVersionedType {
types: RegistryTypes;
}
export interface OverrideModuleType {
name: string;
override: string;
}
export type OverrideModuleType = Record<string, string>;