Move (some) from session & elections to srml/types (#1158)
* Move (some) from session & elections to srml/types * Just use createType instad of (new ClassOf(type))(value) * Cleanup generation script (import file duplication)
This commit is contained in:
@@ -3,8 +3,7 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import extrinsicsFromMeta from '@polkadot/api-metadata/extrinsics/fromMetadata';
|
||||
import { getTypeRegistry } from '@polkadot/types';
|
||||
import * as srmlTypes from '@polkadot/types/srml/definitions';
|
||||
import { injectDefinitions } from '@polkadot/types/srml';
|
||||
|
||||
import createType from '../../codec/createType';
|
||||
import Metadata from '../Metadata';
|
||||
@@ -12,12 +11,6 @@ import Method from '../../primitive/Method';
|
||||
import { MetadataInterface } from '../types';
|
||||
import { Codec } from '../../types';
|
||||
|
||||
export function injectDefinitions (): void {
|
||||
Object.values(srmlTypes).forEach(({ types }): void =>
|
||||
getTypeRegistry().register(types)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the static `rpcData` and the `latestSubstrate` JSON file, Metadata
|
||||
* should decode `rpcData` and output `latestSubstrate`.
|
||||
|
||||
@@ -174,9 +174,9 @@ export function getTypeDef (_type: Text | string, name?: string): TypeDef {
|
||||
return value;
|
||||
}
|
||||
|
||||
export function createClass (type: Text | string): Constructor {
|
||||
export function createClass <T extends Codec = Codec> (type: Text | string): Constructor<T> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
||||
return getTypeClass(
|
||||
return getTypeClass<T>(
|
||||
getTypeDef(type)
|
||||
);
|
||||
}
|
||||
@@ -192,8 +192,8 @@ export function getTypeClassMap (defs: TypeDef[]): { [index: string]: Constructo
|
||||
}
|
||||
|
||||
// Returns the type Class for construction
|
||||
export function getTypeClass (value: TypeDef, Fallback?: Constructor): Constructor {
|
||||
const Type = getRegistry().get(value.type);
|
||||
export function getTypeClass <T extends Codec = Codec> (value: TypeDef, Fallback?: Constructor<T>): Constructor<T> {
|
||||
const Type = getRegistry().get<T>(value.type);
|
||||
|
||||
if (Type) {
|
||||
return Type;
|
||||
@@ -204,58 +204,60 @@ export function getTypeClass (value: TypeDef, Fallback?: Constructor): Construct
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Compact');
|
||||
|
||||
return Compact.with(
|
||||
getTypeClass(value.sub as TypeDef) as Constructor<UInt>
|
||||
);
|
||||
getTypeClass<UInt>(value.sub as TypeDef)
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Enum:
|
||||
assert(value.sub && Array.isArray(value.sub), 'Expected subtype for Enum');
|
||||
|
||||
return Enum.with(
|
||||
getTypeClassMap(value.sub as TypeDef[])
|
||||
);
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Option:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Option');
|
||||
|
||||
return Option.with(
|
||||
getTypeClass(value.sub as TypeDef)
|
||||
);
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Struct:
|
||||
assert(Array.isArray(value.sub), 'Expected nested subtypes for Struct');
|
||||
|
||||
return Struct.with(
|
||||
getTypeClassMap(value.sub as TypeDef[])
|
||||
);
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Tuple:
|
||||
assert(Array.isArray(value.sub), 'Expected nested subtypes for Tuple');
|
||||
|
||||
return Tuple.with(
|
||||
(value.sub as TypeDef[]).map((Type): Constructor<Codec> => getTypeClass(Type))
|
||||
);
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Vector:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Vector');
|
||||
|
||||
return Vector.with(
|
||||
getTypeClass(value.sub as TypeDef)
|
||||
);
|
||||
getTypeClass<Codec>(value.sub as TypeDef)
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.VectorFixed:
|
||||
assert(value.ext, 'Expected length & type information for fixed vector');
|
||||
|
||||
const ext = value.ext as TypeDefExtVecFixed;
|
||||
|
||||
return ext.type === 'u8'
|
||||
? U8aFixed.with((ext.length * 8) as U8aFixedBitLength)
|
||||
: VectorFixed.with(createClass(ext.type), ext.length);
|
||||
return (
|
||||
ext.type === 'u8'
|
||||
? U8aFixed.with((ext.length * 8) as U8aFixedBitLength)
|
||||
: VectorFixed.with(createClass<Codec>(ext.type), ext.length)
|
||||
) as unknown as Constructor<T>;
|
||||
case TypeDefInfo.Linkage:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for Linkage');
|
||||
|
||||
return Linkage.withKey(
|
||||
getTypeClass(value.sub as TypeDef)
|
||||
);
|
||||
getTypeClass<Codec>(value.sub as TypeDef)
|
||||
)as unknown as Constructor<T>;
|
||||
case TypeDefInfo.DoubleMap:
|
||||
assert(value.sub && !Array.isArray(value.sub), 'Expected subtype for DoubleMap');
|
||||
|
||||
return getTypeClass(value.sub as TypeDef);
|
||||
case TypeDefInfo.Null:
|
||||
return Null;
|
||||
return Null as unknown as Constructor<T>;
|
||||
}
|
||||
|
||||
if (Fallback) {
|
||||
@@ -266,11 +268,11 @@ export function getTypeClass (value: TypeDef, Fallback?: Constructor): Construct
|
||||
}
|
||||
|
||||
// alias for createClass
|
||||
export function ClassOf (name: string): Constructor {
|
||||
return createClass(name);
|
||||
export function ClassOf <T extends Codec = Codec> (name: string): Constructor<T> {
|
||||
return createClass<T>(name);
|
||||
}
|
||||
|
||||
function initType (Type: Constructor, value?: any, isPedantic?: boolean): Codec {
|
||||
function initType <T extends Codec = Codec> (Type: Constructor<T>, value?: any, isPedantic?: boolean): T {
|
||||
try {
|
||||
const created = new Type(value);
|
||||
|
||||
@@ -301,11 +303,11 @@ function initType (Type: Constructor, value?: any, isPedantic?: boolean): Codec
|
||||
}
|
||||
}
|
||||
|
||||
export default function createType (type: Text | string, value?: any, isPedantic?: boolean): Codec {
|
||||
export default function createType <T extends Codec = Codec> (type: Text | string, value?: any, isPedantic?: boolean): T {
|
||||
// l.debug(() => ['createType', { type, value }]);
|
||||
|
||||
try {
|
||||
return initType(createClass(type), value, isPedantic);
|
||||
return initType(createClass<T>(type), value, isPedantic);
|
||||
} catch (error) {
|
||||
throw new Error(`createType(${type}):: ${error.message}`);
|
||||
}
|
||||
|
||||
@@ -24,5 +24,5 @@ export { default as U8a } from './U8a';
|
||||
export { default as UInt } from './UInt';
|
||||
|
||||
// Type management helper functions
|
||||
export { default as createType, createClass, getTypeClass, getTypeDef, TypeDef, TypeDefInfo } from './createType';
|
||||
export { default as createType, createClass, getTypeClass, getTypeDef, ClassOf, TypeDef, TypeDefInfo } from './createType';
|
||||
export { default as getTypeRegistry } from './typeRegistry';
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import { isFunction, isString, isUndefined } from '@polkadot/util';
|
||||
|
||||
import { Constructor, RegistryTypes } from '../types';
|
||||
import { Codec, Constructor, RegistryTypes } from '../types';
|
||||
import { createClass } from './createType';
|
||||
|
||||
export class TypeRegistry {
|
||||
@@ -49,12 +49,12 @@ export class TypeRegistry {
|
||||
});
|
||||
}
|
||||
|
||||
public get (name: string): Constructor | undefined {
|
||||
return this._registry.get(name);
|
||||
public get <T extends Codec = Codec> (name: string): Constructor<T> | undefined {
|
||||
return this._registry.get(name) as unknown as Constructor<T>;
|
||||
}
|
||||
|
||||
public getOrThrow (name: string, msg?: string): Constructor {
|
||||
const type = this.get(name);
|
||||
public getOrThrow <T extends Codec = Codec> (name: string, msg?: string): Constructor<T> {
|
||||
const type = this.get<T>(name);
|
||||
|
||||
if (isUndefined(type)) {
|
||||
throw new Error(msg || `type ${name} not found`);
|
||||
|
||||
@@ -6,6 +6,7 @@ import createType from '../codec/createType';
|
||||
import Vector from '../codec/Vector';
|
||||
import json1 from '../json/EventRecord.001.json';
|
||||
import json3 from '../json/EventRecord.003.json';
|
||||
import { injectDefinitions } from '../srml';
|
||||
import Metadata from '../Metadata';
|
||||
import metadataV0 from '../Metadata/v0/static';
|
||||
import metadata from '../Metadata/static';
|
||||
@@ -15,6 +16,8 @@ import EventRecord from './EventRecord';
|
||||
describe('EventRecord', (): void => {
|
||||
describe('EventRecord_0_76', (): void => {
|
||||
beforeEach((): void => {
|
||||
injectDefinitions();
|
||||
|
||||
Event.injectMetadata(
|
||||
new Metadata(metadataV0)
|
||||
);
|
||||
@@ -30,6 +33,8 @@ describe('EventRecord', (): void => {
|
||||
|
||||
describe('EventRecord (current)', (): void => {
|
||||
beforeEach((): void => {
|
||||
injectDefinitions();
|
||||
|
||||
Event.injectMetadata(
|
||||
new Metadata(metadata)
|
||||
);
|
||||
|
||||
@@ -6,8 +6,15 @@
|
||||
// layers can re-use types from higher layers as this is being injected. The
|
||||
// injection order is critical.
|
||||
|
||||
export { default as babe } from './babe/definitions';
|
||||
// session before grandpa, ImOnline
|
||||
export { default as session } from './session/definitions';
|
||||
|
||||
// grandpa before parachains
|
||||
export { default as grandpa } from './grandpa/definitions';
|
||||
|
||||
// remaining, alphabetical odering
|
||||
export { default as babe } from './babe/definitions';
|
||||
export { default as contracts } from './contracts/definitions';
|
||||
export { default as elections } from './elections/definitions';
|
||||
export { default as imOnline } from './imOnline/definitions';
|
||||
export { default as parachains } from './parachains/definitions';
|
||||
|
||||
@@ -2,13 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import SessionKeys from './SessionKeys';
|
||||
|
||||
/**
|
||||
* @name Keys
|
||||
* @description
|
||||
* The session keys
|
||||
*/
|
||||
|
||||
export default class Keys extends SessionKeys {
|
||||
}
|
||||
export default {
|
||||
types: {
|
||||
SetIndex: 'u32'
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
/* eslint-disable @typescript-eslint/no-empty-interface */
|
||||
// Auto-generated via `yarn build:srmlTs`, do not edit
|
||||
|
||||
import { u32 } from '../../primitive';
|
||||
|
||||
export interface SetIndex extends u32 {}
|
||||
@@ -4,7 +4,8 @@
|
||||
import { Codec } from '../../types';
|
||||
import { Enum, Struct, Vector } from '../../codec';
|
||||
import { u64 } from '../../primitive';
|
||||
import { BlockNumber, SessionKey } from '../../type';
|
||||
import { BlockNumber } from '../../type';
|
||||
import { SessionKey } from '../session/types';
|
||||
|
||||
export interface AuthorityWeight extends u64 {}
|
||||
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
|
||||
import { Struct, Vector } from '../../codec';
|
||||
import { Bytes } from '../../primitive';
|
||||
import { AuthorityId, BlockNumber, SessionIndex } from '../../type';
|
||||
import { AuthorityId, BlockNumber } from '../../type';
|
||||
import { SessionIndex } from '../session/types';
|
||||
|
||||
export interface Heartbeat extends Struct {
|
||||
readonly blockNumber: BlockNumber;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// Copyright 2017-2019 @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 getTypeRegistry from '../codec/typeRegistry';
|
||||
import * as srmlTypes from './definitions';
|
||||
|
||||
/**
|
||||
* @description A utility method that injects all the srml definitions into the type registry
|
||||
*/
|
||||
export function injectDefinitions (): void {
|
||||
const registry = getTypeRegistry();
|
||||
|
||||
Object.values(srmlTypes).forEach(({ types }): void =>
|
||||
registry.register(types)
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,8 @@
|
||||
import { Codec } from '../../types';
|
||||
import { Enum, Struct, Vector } from '../../codec';
|
||||
import { AccountId, BalanceOf, Bytes, Hash, Signature, u32, u64 } from '../../primitive';
|
||||
import { BlockNumber, SessionKey } from '../../type';
|
||||
import { BlockNumber } from '../../type';
|
||||
import { SessionKey } from '../session/types';
|
||||
|
||||
export interface AttestedCandidate extends Struct {
|
||||
readonly candidate: CandidateReceipt;
|
||||
|
||||
+10
-9
@@ -2,12 +2,13 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import U32 from '../primitive/U32';
|
||||
|
||||
/**
|
||||
* @name SetIndex
|
||||
* @description
|
||||
* Set index, implemented as a [[U32]]
|
||||
*/
|
||||
export default class SetIndex extends U32 {
|
||||
}
|
||||
export default {
|
||||
types: {
|
||||
SessionIndex: 'u32',
|
||||
SessionKey: 'AuthorityId',
|
||||
SessionKeys: {
|
||||
ed25519: 'SessionKey'
|
||||
},
|
||||
Keys: 'SessionKeys'
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,16 @@
|
||||
/* eslint-disable @typescript-eslint/no-empty-interface */
|
||||
// Auto-generated via `yarn build:srmlTs`, do not edit
|
||||
|
||||
import { Struct } from '../../codec';
|
||||
import { u32 } from '../../primitive';
|
||||
import { AuthorityId } from '../../type';
|
||||
|
||||
export interface Keys extends SessionKeys {}
|
||||
|
||||
export interface SessionIndex extends u32 {}
|
||||
|
||||
export interface SessionKey extends AuthorityId {}
|
||||
|
||||
export interface SessionKeys extends Struct {
|
||||
readonly ed25519: SessionKey;
|
||||
}
|
||||
@@ -2,8 +2,12 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './babe/types';
|
||||
// keep ordering here exactly the same as definitions
|
||||
export * from './session/types';
|
||||
export * from './grandpa/types';
|
||||
|
||||
export * from './babe/types';
|
||||
export * from './contracts/types';
|
||||
export * from './elections/types';
|
||||
export * from './imOnline/types';
|
||||
export * from './parachains/types';
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2019 @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 U32 from '../primitive/U32';
|
||||
|
||||
/**
|
||||
* @name SessionIndex
|
||||
* @description
|
||||
* Simple index type with which we can count sessions
|
||||
*/
|
||||
export default class SessionIndex extends U32 {
|
||||
}
|
||||
@@ -24,7 +24,6 @@ export { default as Exposure } from './Exposure';
|
||||
export { default as IndividualExposure } from './IndividualExposure';
|
||||
export { default as InherentOfflineReport } from './InherentOfflineReport';
|
||||
export { default as Key } from './Key';
|
||||
export { default as Keys } from './Keys';
|
||||
export { default as KeyValue } from './KeyValue';
|
||||
export { default as LockIdentifier } from './LockIdentifier';
|
||||
export { default as LockPeriods } from './LockPeriods';
|
||||
@@ -45,10 +44,6 @@ export { default as ReferendumIndex } from './ReferendumIndex';
|
||||
export { default as ReferendumInfo } from './ReferendumInfo';
|
||||
export { default as RewardDestination } from './RewardDestination';
|
||||
export { default as SeedOf } from './SeedOf';
|
||||
export { default as SessionIndex } from './SessionIndex';
|
||||
export { default as SessionKey } from './SessionKey';
|
||||
export { default as SessionKeys } from './SessionKeys';
|
||||
export { default as SetIndex } from './SetIndex';
|
||||
export { default as StakingLedger } from './StakingLedger';
|
||||
export { default as TreasuryProposal } from './TreasuryProposal';
|
||||
export { default as UncleEntryItem } from './UncleEntryItem';
|
||||
|
||||
Reference in New Issue
Block a user