Split @polkadot/types/known into @polkadot/types-known (#2120)

* Split @polkadot/types/known into @polkadot/types-known

* Rreadme link

* tsconfig typo

* script aliasses
This commit is contained in:
Jaco Greeff
2020-03-31 13:40:59 +02:00
committed by GitHub
parent df0de49a67
commit 39c44bb5aa
29 changed files with 573 additions and 256 deletions
-66
View File
@@ -1,66 +0,0 @@
// 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 { Registry, 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>
/** @internal */
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
/** @internal */
export function getMetadataTypes (_registry: Registry, version: number): RegistryTypes {
return filterVersions(TYPES_META, version);
}
// based on the chain and runtimeVersion, get the applicable types (ready for registration)
/** @internal */
export function getSpecTypes (_registry: Registry, chainName: Text | string, { specName, specVersion }: RuntimeVersion): RegistryTypes {
const _chainName = chainName.toString();
const _specName = specName.toString();
const _specVersion = specVersion.toNumber();
return {
...filterVersions(TYPES_SPEC[_specName], _specVersion),
...filterVersions(TYPES_CHAIN[_chainName], _specVersion)
};
}
// based on the spec and chain, get the specific user-defind types
/** @internal */
export function getUserTypes ({ knownTypes }: Registry, chainName: Text | string, { specName }: RuntimeVersion): RegistryTypes {
const _chainName = chainName.toString();
const _specName = specName.toString();
return {
...(knownTypes.typesSpec?.[_specName] || {}),
...(knownTypes.typesChain?.[_chainName] || {})
};
}
// get types for specific modules (metadata override)
/** @internal */
export function getModuleTypes ({ knownTypes }: Registry, section: string): OverrideModuleType {
return {
...(TYPES_MODULES[section] || {}),
...(knownTypes.typesAlias?.[section] || {})
};
}
@@ -1,49 +0,0 @@
// 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 { TypeRegistry } from '../create/registry';
import { getModuleTypes } from './';
const registry = new TypeRegistry();
registry.setKnownTypes({
typesAlias: {
identity: {
Id: 'IdentityId'
},
testModule: {
Proposal: 'TestProposal'
},
treasury: {
Proposal: 'TreasuryProposals2'
}
}
});
describe('getModuleTypes', (): void => {
it('collects the pre-defined types for contracts', (): void => {
expect(getModuleTypes(registry, 'contracts')).toEqual({
StorageKey: 'ContractStorageKey'
});
});
it('collects the user-defined types for testModule', (): void => {
expect(getModuleTypes(registry, 'testModule')).toEqual({
Proposal: 'TestProposal'
});
});
it('overrides pre-defined with user-defined for treasury', (): void => {
expect(getModuleTypes(registry, 'treasury')).toEqual({
Proposal: 'TreasuryProposals2'
});
});
it('merges pre-defined and user-defined for identity', (): void => {
expect(getModuleTypes(registry, 'identity')).toEqual({
Judgement: 'IdentityJudgement',
Id: 'IdentityId'
});
});
});
-167
View File
@@ -1,167 +0,0 @@
// 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 duplication between modules exist)
const TYPES_MODULES: Record<string, OverrideModuleType> = {
// old metadata & naming
contract: {
// v2 & v3
AccountInfo: 'ContractAccountInfo'
},
// current from here on
balances: {
Releases: 'ReleasesBalances'
},
contracts: {
StorageKey: 'ContractStorageKey'
},
identity: {
Judgement: 'IdentityJudgement'
},
parachains: {
Id: 'ParaId'
},
society: {
Judgement: 'SocietyJudgement',
Vote: 'SocietyVote'
},
staking: {
Compact: 'CompactAssignments',
Releases: 'ReleasesStaking'
},
treasury: {
Proposal: 'TreasuryProposal'
}
};
// 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, 1003],
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
LookupSource: 'Address',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
}
},
{
minmax: [1004, 1004],
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5',
LookupSource: 'AccountId',
ReferendumInfo: 'ReferendumInfoTo239'
}
},
{
minmax: [1005, undefined],
types: {
Address: 'AccountId',
Keys: 'SessionKeys5',
LookupSource: 'AccountId'
}
}
];
const TYPES_KUSAMA_VERSIONED: OverrideVersionedType[] = [
{
// 1020 is first CC3
minmax: [1019, 1031],
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
DispatchError: 'DispatchErrorTo198',
Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
}
},
{
minmax: [1032, 1042],
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239',
SlashingSpans: 'SlashingSpansTo204',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
}
},
{
// actual at 1045 (1043-1044 is dev)
minmax: [1043, 1045],
types: {
Address: 'GenericAddress',
BalanceLock: 'BalanceLockTo212',
Keys: 'SessionKeys5',
LookupSource: 'Address',
ReferendumInfo: 'ReferendumInfoTo239',
StakingLedger: 'StakingLedgerTo223',
Votes: 'VotesTo230'
}
},
{
// actual at 1050 (1046-1049 is dev)
minmax: [1046, 1054],
types: {
// Indices optional, not in transaction
Address: 'AccountId',
Keys: 'SessionKeys5',
LookupSource: 'AccountId',
ReferendumInfo: 'ReferendumInfoTo239'
}
},
{
minmax: [1055, undefined],
types: {
Address: 'AccountId',
Keys: 'SessionKeys5',
LookupSource: 'AccountId'
}
}
];
// 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: {
Address: 'GenericAddress',
BlockNumber: 'u64',
Index: 'u64',
EventRecord: 'EventRecordTo76',
ValidatorPrefs: 'ValidatorPrefsTo145',
StakingLedger: 'StakingLedgerTo223'
}
}
];
// 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 };
-12
View File
@@ -1,12 +0,0 @@
// 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 type OverrideModuleType = Record<string, string>;
+7 -1
View File
@@ -3,7 +3,6 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ChainProperties } from '../interfaces/system';
import { OverrideModuleType } from '../known/types';
import { CallFunction } from './calls';
import { Codec, Constructor } from './codec';
import { AnyJson } from './helpers';
@@ -13,6 +12,13 @@ import BN from 'bn.js';
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface InterfaceTypes { }
export interface OverrideVersionedType {
minmax: [number?, number?]; // min (v >= min) and max (v <= max)
types: RegistryTypes;
}
export type OverrideModuleType = Record<string, string>;
export type RegistryTypes = Record<string, Constructor | string | Record<string, string> | { _enum: string[] | Record<string, string | null> } | { _set: Record<string, number> }>;
export interface RegistryMetadataText extends String, Codec {