Introduce OriginCaller (& apply to Scheduler) (#2692)

* Introduce OriginCaller (& apply to Scheduler)

* RawOrigin

* Create OriginCaller type dynamically

* Update packages/metadata/src/Metadata/v12/toLatest.ts
This commit is contained in:
Jaco Greeff
2020-10-08 13:27:50 +02:00
committed by GitHub
parent 2faf68c4d2
commit 8b54a22914
5 changed files with 64 additions and 1 deletions
+36 -1
View File
@@ -1,13 +1,21 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { EventMetadataV12, EventMetadataLatest, FunctionMetadataV12, FunctionMetadataLatest, MetadataV12, MetadataLatest, ModuleMetadataLatest, StorageMetadataV12, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { EventMetadataV12, EventMetadataLatest, FunctionMetadataV12, FunctionMetadataLatest, MetadataV12, MetadataLatest, ModuleMetadataLatest, ModuleMetadataV12, StorageMetadataV12, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { Registry, OverrideModuleType } from '@polkadot/types/types';
import { getModuleTypes } from '@polkadot/types-known';
import { Type } from '@polkadot/types/primitive';
import { stringCamelCase } from '@polkadot/util';
// Since we don't have insight into the origin specification, we can only define what we know about
// in a pure Substrate/Polkadot implementation, any other custom origins won't be handled at all
const KNOWN_ORIGINS: Record<string, string> = {
Council: 'CollectiveOrigin',
System: 'SystemOrigin',
TechnicalCommittee: 'CollectiveOrigin'
};
// TODO Handle consts as well
/**
@@ -81,12 +89,39 @@ function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV
});
}
// generate & register the OriginCaller type
function registerOriginCaller (registry: Registry, modules: ModuleMetadataV12[]): void {
const isIndexed = modules.some(({ index }) => !index.eqn(255));
registry.register({
OriginCaller: {
_enum: modules
.map((mod, index): [string, number] => [
mod.name.toString(),
isIndexed ? mod.index.toNumber() : index
])
.sort((a, b) => a[1] - b[1])
.reduce((result: Record<string, string>, [name, index]): Record<string, string> => {
for (let i = Object.keys(result).length; i < index; i++) {
result[`Empty${i}`] = 'Null';
}
result[name] = KNOWN_ORIGINS[name] || 'Null';
return result;
}, {})
}
});
}
/**
* Convert the Metadata (which is an alias) to latest - effectively this _always_ get applied to the top-level &
* most-recent metadata, since it allows us a chance to actually apply call and storage specific type aliasses
* @internal
**/
export default function toLatest (registry: Registry, { extrinsic, modules }: MetadataV12): MetadataLatest {
registerOriginCaller(registry, modules);
return registry.createType('MetadataLatest', {
extrinsic,
modules: modules.map((mod): ModuleMetadataLatest => {
@@ -9,6 +9,12 @@ import { Definitions } from '../../types';
export default {
rpc: {},
types: {
CollectiveOrigin: {
_enum: {
Members: '(MemberCount, MemberCount)',
Member: 'AccountId'
}
},
MemberCount: 'u32',
ProposalIndex: 'u32',
VotesTo230: {
@@ -84,6 +84,13 @@ export default {
Moment: 'u64',
OpaqueCall: 'Bytes',
Origin: 'DoNotConstruct<Origin>',
OriginCaller: {
_enum: {
// this should be dynamically built from the actual modules, based on index
System: 'SystemOrigin'
}
},
PalletsOrigin: 'OriginCaller',
PalletVersion: {
major: 'u16',
minor: 'u8',
@@ -14,6 +14,13 @@ export default {
SchedulePeriod: 'Period',
SchedulePriority: 'Priority',
Scheduled: {
maybeId: 'Option<Bytes>',
priority: 'SchedulePriority',
call: 'Call',
maybePeriodic: 'Option<SchedulePeriod>',
origin: 'PalletsOrigin'
},
ScheduledTo254: {
maybeId: 'Option<Bytes>',
priority: 'SchedulePriority',
call: 'Call',
@@ -270,8 +270,16 @@ export default {
Initialization: 'Null'
}
},
RawOrigin: {
_enum: {
Root: 'Null',
Signed: 'AccountId',
None: 'Null'
}
},
RefCount: 'u32',
RefCountTo259: 'u8',
SystemOrigin: 'RawOrigin',
TransactionValidityError: {
_enum: {
Invalid: 'InvalidTransaction',