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
+14
View File
@@ -0,0 +1,14 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
import Westend from './westend';
// Type overrides based on specific nodes
const typesChain: Record<string, OverrideVersionedType[]> = {
Westend
};
export default typesChain;
+21
View File
@@ -0,0 +1,21 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
const versioned: OverrideVersionedType[] = [
{
minmax: [1000, undefined],
types: {
Address: 'GenericAddress',
Keys: 'SessionKeys5',
// previous substrate versions
BalanceLock: 'BalanceLockTo212',
ReferendumInfo: 'ReferendumInfoTo239',
StakingLedger: 'StakingLedgerTo223'
}
}
];
export default versioned;
+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 { TypeRegistry } from '@polkadot/types/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'
});
});
});
+61
View File
@@ -0,0 +1,61 @@
// 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 { Registry, RegistryTypes, OverrideModuleType, OverrideVersionedType } from '@polkadot/types/types';
import BN from 'bn.js';
import { Text } from '@polkadot/types';
import { bnToBn, isUndefined } from '@polkadot/util';
import typesChain from './chain';
import typesMeta from './metadata';
import typesModules from './modules';
import typesSpec from './spec';
// 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
}), {});
}
/**
* @description Based on the metadata version, return the registry types
*/
export function getMetadataTypes (_registry: Registry, version: number): RegistryTypes {
return filterVersions(typesMeta, version);
}
/**
* @description Get types for specific modules (metadata override)
*/
export function getModuleTypes ({ knownTypes }: Registry, section: string): OverrideModuleType {
return {
...(typesModules[section] || {}),
...(knownTypes.typesAlias?.[section] || {})
};
}
/**
* @description Based on the chain and runtimeVersion, get the applicable types (ready for registration)
*/
export function getSpecTypes ({ knownTypes }: Registry, chainName: Text | string, specName: Text | string, specVersion: BN | number): RegistryTypes {
const _chainName = chainName.toString();
const _specName = specName.toString();
const _specVersion = bnToBn(specVersion).toNumber();
return {
...filterVersions(typesSpec[_specName], _specVersion),
...filterVersions(typesChain[_chainName], _specVersion),
...(knownTypes.typesSpec?.[_specName] || {}),
...(knownTypes.typesChain?.[_chainName] || {})
};
}
+26
View File
@@ -0,0 +1,26 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
// Type overrides based on metadata versions
const typesMeta: 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'
}
}
];
export default typesMeta;
+40
View File
@@ -0,0 +1,40 @@
// 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 } from '@polkadot/types/types';
// type overrides for modules (where duplication between modules exist)
const typesModules: 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'
}
};
export default typesModules;
+16
View File
@@ -0,0 +1,16 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
import kusama from './kusama';
import polkadot from './polkadot';
// Type overrides for specific spec types & versions as given in runtimeVersion
const typesSpec: Record<string, OverrideVersionedType[]> = {
kusama,
polkadot
};
export default typesSpec;
+70
View File
@@ -0,0 +1,70 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
const 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'
}
}
];
export default versioned;
+43
View File
@@ -0,0 +1,43 @@
// 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 { OverrideVersionedType } from '@polkadot/types/types';
// 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 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'
}
}
];
export default versioned;