Type aliases on a per-module basis (#1740)

* Type aliasses on a per-module basis

* Remove test interface

* Update wrapper

* Move known types to types
This commit is contained in:
Jaco Greeff
2020-01-22 15:51:23 +01:00
committed by GitHub
parent dc64076c79
commit c1c537a3b5
15 changed files with 227 additions and 133 deletions
+8 -5
View File
@@ -16,12 +16,12 @@ import { AccountInfo, Amount, AssetOf, InherentOfflineReport, LockPeriods, NewAc
import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from '@polkadot/types/interfaces/elections';
import { AssetOptions, Owner, PermissionLatest, PermissionVersions, PermissionsV1 } from '@polkadot/types/interfaces/genericAsset';
import { AuthorityIndex, AuthorityList, AuthorityWeight, NextAuthority, PendingPause, PendingResume, SetId, StoredPendingChange, StoredState } from '@polkadot/types/interfaces/grandpa';
import { IdentityInfo, IdentityInfoAdditional, Judgement, RegistrarIndex, RegistrarInfo, Registration, RegistrationJudgement } from '@polkadot/types/interfaces/identity';
import { IdentityInfo, IdentityInfoAdditional, IdentityJudgement, RegistrarIndex, RegistrarInfo, Registration, RegistrationJudgement } from '@polkadot/types/interfaces/identity';
import { AuthIndex, AuthoritySignature, Heartbeat, OpaqueMultiaddr, OpaqueNetworkState, OpaquePeerId } from '@polkadot/types/interfaces/imOnline';
import { Kind, OffenceDetails, Offender, OpaqueTimeSlot, ReportIdOf, Reporter } from '@polkadot/types/interfaces/offences';
import { ActiveRecovery, RecoveryConfig } from '@polkadot/types/interfaces/recovery';
import { FullIdentification, IdentificationTuple, Keys, SessionIndex, SessionKeys1, SessionKeys2, SessionKeys3, SessionKeys4, SessionKeys5, SessionKeys6 } from '@polkadot/types/interfaces/session';
import { Bid, BidKind, BidKindVouch, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
import { Bid, BidKind, BidKindVouch, SocietyJudgement, StrikeCount, VouchingStatus } from '@polkadot/types/interfaces/society';
import { EraIndex, EraPoints, EraRewards, Exposure, Forcing, IndividualExposure, MomentOf, Nominations, Points, RewardDestination, SlashJournalEntry, SlashingSpans, SlashingSpansTo204, SpanIndex, SpanRecord, StakingLedger, UnappliedSlash, UnappliedSlashOther, UnlockChunk, ValidatorPrefs, ValidatorPrefsTo145, ValidatorPrefsTo196 } from '@polkadot/types/interfaces/staking';
import { DigestOf, DispatchError, DispatchErrorModule, DispatchErrorTo198, DispatchResult, DispatchResultOf, DispatchResultTo198, Event, EventId, EventIndex, EventRecord, EventRecordTo76, Key, Phase } from '@polkadot/types/interfaces/system';
import { OpenTip, OpenTipFinder, OpenTipTip, TreasuryProposal } from '@polkadot/types/interfaces/treasury';
@@ -560,9 +560,9 @@ export interface InterfaceRegistry {
IdentityInfo: IdentityInfo;
'Option<IdentityInfo>': Option<IdentityInfo>;
'Vec<IdentityInfo>': Vec<IdentityInfo>;
Judgement: Judgement;
'Option<Judgement>': Option<Judgement>;
'Vec<Judgement>': Vec<Judgement>;
IdentityJudgement: IdentityJudgement;
'Option<IdentityJudgement>': Option<IdentityJudgement>;
'Vec<IdentityJudgement>': Vec<IdentityJudgement>;
RegistrationJudgement: RegistrationJudgement;
'Option<RegistrationJudgement>': Option<RegistrationJudgement>;
'Vec<RegistrationJudgement>': Vec<RegistrationJudgement>;
@@ -659,6 +659,9 @@ export interface InterfaceRegistry {
BidKind: BidKind;
'Option<BidKind>': Option<BidKind>;
'Vec<BidKind>': Vec<BidKind>;
SocietyJudgement: SocietyJudgement;
'Option<SocietyJudgement>': Option<SocietyJudgement>;
'Vec<SocietyJudgement>': Vec<SocietyJudgement>;
StrikeCount: StrikeCount;
'Compact<StrikeCount>': Compact<StrikeCount>;
'Option<StrikeCount>': Option<StrikeCount>;
@@ -16,7 +16,7 @@ export default {
image: 'Data',
twitter: 'Data'
},
Judgement: {
IdentityJudgement: {
_enum: {
Unknown: 'Null',
FeePaid: 'Balance',
@@ -27,7 +27,7 @@ export default {
Erroneous: 'Null'
}
},
RegistrationJudgement: '(RegistrarIndex, Judgement)',
RegistrationJudgement: '(RegistrarIndex, IdentityJudgement)',
Registration: {
judgements: 'Vec<RegistrationJudgement>',
deposit: 'Balance',
@@ -32,7 +32,7 @@ export interface IdentityInfo extends Struct {
export interface IdentityInfoAdditional extends ITuple<[Data, Data]> {}
/** Enum */
export interface Judgement extends Enum {
export interface IdentityJudgement extends Enum {
/** 0:: Unknown */
readonly isUnknown: boolean;
/** 1:: FeePaid(Balance) */
@@ -74,5 +74,5 @@ export interface Registration extends Struct {
readonly info: IdentityInfo;
}
/** ITuple<[RegistrarIndex, Judgement]> */
export interface RegistrationJudgement extends ITuple<[RegistrarIndex, Judgement]> {}
/** ITuple<[RegistrarIndex, IdentityJudgement]> */
export interface RegistrationJudgement extends ITuple<[RegistrarIndex, IdentityJudgement]> {}
@@ -16,6 +16,10 @@ export default {
Vouch: 'BidKindVouch'
}
},
// a society-specific Judgement (not the same as identity Judgement)
SocietyJudgement: {
_enum: ['Rebid', 'Reject', 'Approve']
},
StrikeCount: 'u32',
VouchingStatus: {
_enum: ['Vouching', 'Banned']
@@ -31,6 +31,16 @@ export interface BidKind extends Enum {
/** ITuple<[AccountId, Balance]> */
export interface BidKindVouch extends ITuple<[AccountId, Balance]> {}
/** Enum */
export interface SocietyJudgement extends Enum {
/** 0:: Rebid */
readonly isRebid: boolean;
/** 1:: Reject */
readonly isReject: boolean;
/** 2:: Approve */
readonly isApprove: boolean;
}
/** u32 */
export interface StrikeCount extends u32 {}
+49
View File
@@ -0,0 +1,49 @@
// Copyright 2017-2020 @polkadot/types 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 { RuntimeVersion } from '../interfaces';
import { RegistryTypes } from '../types';
import { OverrideModuleType, OverrideVersionedType } from './types';
import { Text } from '@polkadot/types';
import { isUndefined } from '@polkadot/util';
import { TYPES_CHAIN, TYPES_META, TYPES_MODULES, TYPES_SPEC } from './overrides';
// flatten a VersionedType[] into a Record<string, string>
function filterVersions (versions: OverrideVersionedType[] = [], version: number): RegistryTypes {
return versions
.filter(({ minmax: [min, max] }): boolean =>
(isUndefined(min) || version >= min) &&
(isUndefined(max) || version <= max)
)
.reduce((result: RegistryTypes, { types }): RegistryTypes => ({
...result,
...types
}), {});
}
// based on the metadata version, return the registry types
export function getMetadataTypes (version: number): RegistryTypes {
return filterVersions(TYPES_META, version);
}
// based on the chain and runtimeVersion, get the applicable types (ready for registration)
export function getChainTypes (chainName: Text, { specName, specVersion }: RuntimeVersion, typesChain: Record<string, RegistryTypes> = {}, typesSpec: Record<string, RegistryTypes> = {}): RegistryTypes {
const _chainName = chainName.toString();
const _specName = specName.toString();
const _specVersion = specVersion.toNumber();
return {
...filterVersions(TYPES_SPEC[_specName], _specVersion),
...filterVersions(TYPES_CHAIN[_chainName], _specVersion),
...(typesSpec[_specName] || {}),
...(typesChain[_chainName] || {})
};
}
// get types for specific modules (metadata override)
export function getModuleTypes (section: string): OverrideModuleType[] {
return TYPES_MODULES[section] || [];
}
+86
View File
@@ -0,0 +1,86 @@
// Copyright 2017-2020 @polkadot/types 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 { 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'
}
]
};
// these are override types for Polkadot & Kusama chains
// NOTE The SessionKeys definition for Polkadot and Substrate (OpaqueKeys
// implementation) are different. Detect Polkadot and inject the `Keys`
// definition as applicable. (4 keys in substrate vs 5 in Polkadot/CC3).
const TYPES_POLKADOT_VERSIONED: OverrideVersionedType[] = [
{
minmax: [1000, undefined], // from launch
types: {
Keys: 'SessionKeys5'
}
}
];
const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
{
minmax: [1019, 1031],
types: {
DispatchError: 'DispatchErrorTo198',
Keys: 'SessionKeys5',
SlashingSpans: 'SlashingSpansTo204'
}
},
{
minmax: [1032, 1042],
types: {
Keys: 'SessionKeys5',
SlashingSpans: 'SlashingSpansTo204'
}
},
{
minmax: [1043, undefined],
types: {
Keys: 'SessionKeys5'
}
}
];
// Type overrides based on specific nodes
const TYPES_CHAIN: Record<string, OverrideVersionedType[]> = {};
// Type overrides based on metadata versions
const TYPES_META: OverrideVersionedType[] = [
{
// NOTE this is for support of old, e.g. Alex, old metadata and BlockNumber/Index
// This is detected based on metadata version, since this is what we have up-front
// v3 = Alex
// v4 = v1.0 branch
minmax: [0, 4],
types: {
BlockNumber: 'u64',
Index: 'u64',
EventRecord: 'EventRecordTo76',
ValidatorPrefs: 'ValidatorPrefsTo145'
}
}
];
// Type overrides for specific spec types & versions as given in runtimeVersion
const TYPES_SPEC: Record<string, OverrideVersionedType[]> = {
kusama: TYPES_KUSAMA_VERSIONED,
polkadot: TYPES_POLKADOT_VERSIONED
};
export { TYPES_CHAIN, TYPES_META, TYPES_MODULES, TYPES_SPEC };
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2017-2020 @polkadot/types 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 { RegistryTypes } from '../types';
export interface OverrideVersionedType {
minmax: [number?, number?]; // min (v >= min) and max (v <= max)
types: RegistryTypes;
}
export interface OverrideModuleType {
name: string;
override: string;
}
+9 -2
View File
@@ -23,6 +23,7 @@ import Raw from '../codec/Raw';
// - Strings should probably be trimmed (docs do come through with extra padding)
export default class Text extends String implements Codec {
public readonly registry: Registry;
private _override: string | null = null;
constructor (registry: Registry, value: Text | string | AnyU8a | { toString: () => string } = '') {
super(Text.decodeText(value));
@@ -93,6 +94,13 @@ export default class Text extends String implements Codec {
: false;
}
/**
* @description Set an override value for this
*/
public setOverride (override: string): void {
this._override = override;
}
/**
* @description Returns a hex string representation of the value
*/
@@ -120,8 +128,7 @@ export default class Text extends String implements Codec {
* @description Returns the string representation of the value
*/
public toString (): string {
// only included here since we do not inherit docs
return super.toString();
return this._override || super.toString();
}
/**
+13 -7
View File
@@ -325,10 +325,18 @@ export interface ISignerPayload {
toRaw (): SignerPayloadRaw;
}
export interface RegistryMetadataText extends String, Codec {
setOverride (override: string): void;
}
export interface RegistryMetadataCallArg {
name: RegistryMetadataText;
type: RegistryMetadataText;
}
export interface RegistryMetadataCall {
args: any[];
// eslint-disable-next-line @typescript-eslint/ban-types
name: String & Codec;
args: RegistryMetadataCallArg[];
name: RegistryMetadataText;
toJSON (): string | AnyJsonObject;
}
@@ -356,8 +364,7 @@ export type RegistryMetadataErrors = RegistryMetadataError[];
export interface RegistryMetadataEvent {
args: any[];
// eslint-disable-next-line @typescript-eslint/ban-types
name: String & Codec;
name: RegistryMetadataText;
}
export interface RegistryMetadataEvents {
@@ -369,8 +376,7 @@ export interface RegistryMetadataModule {
calls: RegistryMetadataCalls;
errors: RegistryMetadataErrors;
events: RegistryMetadataEvents;
// eslint-disable-next-line @typescript-eslint/ban-types
name: String & Codec;
name: RegistryMetadataText;
}
export interface RegistryMetadataLatest {