Use system.version constant for runtime typegen (#5048)
* Use system.version constant for runtime typegen * Adjust entries cast * Adjust
This commit is contained in:
@@ -9,6 +9,7 @@ Changes:
|
||||
- Adjust type generation to correctly cater for ambient definitions
|
||||
- Improve type argument generation for `Option<...>` types
|
||||
- Adjust usage of deprecated `@polkadot/util` methods
|
||||
- Runtime call typegen from `system.version` constant value
|
||||
- Update to latest Substrate, Polkadot & Kusama metadata
|
||||
|
||||
|
||||
|
||||
@@ -12,9 +12,7 @@ import { formatNumber } from '@polkadot/util';
|
||||
import { generateDefaultCalls, generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultQuery, generateDefaultRpc, generateDefaultTx } from './generate';
|
||||
import { assertDir, assertFile, getMetadataViaWs, HEADER, writeFile } from './util';
|
||||
|
||||
type Version = { apis: [HexString, number][] };
|
||||
|
||||
function generate (metaHex: HexString, version: Version | null, pkg: string | undefined, output: string, isStrict?: boolean): void {
|
||||
function generate (metaHex: HexString, pkg: string | undefined, output: string, isStrict?: boolean): void {
|
||||
console.log(`Generating from metadata, ${formatNumber((metaHex.length - 2) / 2)} bytes`);
|
||||
|
||||
const outputPath = assertDir(path.join(process.cwd(), output));
|
||||
@@ -48,7 +46,7 @@ function generate (metaHex: HexString, version: Version | null, pkg: string | un
|
||||
generateDefaultQuery(path.join(outputPath, 'augment-api-query.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
generateDefaultRpc(path.join(outputPath, 'augment-api-rpc.ts'), extraTypes);
|
||||
generateDefaultTx(path.join(outputPath, 'augment-api-tx.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
generateDefaultCalls(path.join(outputPath, 'augment-api-runtime.ts'), metaHex, version, extraTypes, isStrict, customLookupDefinitions);
|
||||
generateDefaultCalls(path.join(outputPath, 'augment-api-runtime.ts'), metaHex, extraTypes, isStrict, customLookupDefinitions);
|
||||
|
||||
writeFile(path.join(outputPath, 'augment-api.ts'), (): string =>
|
||||
[
|
||||
@@ -64,10 +62,10 @@ function generate (metaHex: HexString, version: Version | null, pkg: string | un
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
type ArgV = { endpoint: string; output: string; package?: string; strict?: boolean, version?: string };
|
||||
type ArgV = { endpoint: string; output: string; package?: string; strict?: boolean };
|
||||
|
||||
export function main (): void {
|
||||
const { endpoint, output, package: pkg, strict: isStrict, version } = yargs.strict().options({
|
||||
const { endpoint, output, package: pkg, strict: isStrict } = yargs.strict().options({
|
||||
endpoint: {
|
||||
description: 'The endpoint to connect to (e.g. wss://kusama-rpc.polkadot.io) or relative path to a file containing the JSON output of an RPC state_getMetadata call',
|
||||
required: true,
|
||||
@@ -85,28 +83,17 @@ export function main (): void {
|
||||
strict: {
|
||||
description: 'Turns on strict mode, no output of catch-all generic versions',
|
||||
type: 'boolean'
|
||||
},
|
||||
version: {
|
||||
description: 'Pass through a version JSON (if endpoint is non-wss)',
|
||||
type: 'string'
|
||||
}
|
||||
}).argv as ArgV;
|
||||
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
getMetadataViaWs(endpoint)
|
||||
.then(({ metadata, version }) => generate(metadata, version, pkg, output, isStrict))
|
||||
.then((metadata) => generate(metadata, pkg, output, isStrict))
|
||||
.catch(() => process.exit(1));
|
||||
} else {
|
||||
let rtv: Version | null = null;
|
||||
|
||||
if (version) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
rtv = (require(assertFile(path.join(process.cwd(), version))) as Record<string, Version>).result;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const metadata = (require(assertFile(path.join(process.cwd(), endpoint))) as Record<string, HexString>).result;
|
||||
|
||||
generate(metadata, rtv, pkg, output, isStrict);
|
||||
generate(metadata, pkg, output, isStrict);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ export function main (): void {
|
||||
if (endpoint) {
|
||||
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
||||
getMetadataViaWs(endpoint)
|
||||
.then(({ metadata }) => generateDefaultLookup(inputPath, metadata))
|
||||
.then((metadata) => generateDefaultLookup(inputPath, metadata))
|
||||
.catch(() => process.exit(1));
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2022 @polkadot/typegen authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { TypeRegistry } from '@polkadot/types/create';
|
||||
import type { Metadata } from '@polkadot/types/metadata/Metadata';
|
||||
import type { DefinitionCallNamed, Definitions } from '@polkadot/types/types';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { ExtraTypes } from './types';
|
||||
@@ -67,7 +67,7 @@ function getDefs (apis: Apis | null, defs: Record<string, Definitions>): Record<
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function generateCallTypes (registry: TypeRegistry, dest: string, apis: Apis | null, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
export function generateCallTypes (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
||||
writeFile(dest, (): string => {
|
||||
const allTypes: ExtraTypes = {
|
||||
'@polkadot/types-augment': {
|
||||
@@ -80,6 +80,24 @@ export function generateCallTypes (registry: TypeRegistry, dest: string, apis: A
|
||||
...extraTypes
|
||||
};
|
||||
const imports = createImports(allTypes);
|
||||
|
||||
// find the system.Version in metadata
|
||||
let apis: Apis | null = null;
|
||||
const { pallets, registry } = meta.asLatest;
|
||||
const sysp = pallets.find(({ name }) => name.eq('System'));
|
||||
|
||||
if (sysp) {
|
||||
const verc = sysp.constants.find(({ name }) => name.eq('Version'));
|
||||
|
||||
if (verc) {
|
||||
apis = registry.createType('RuntimeVersion', verc.value).apis.map(([k, v]): [HexString, number] => [k.toHex(), v.toNumber()]);
|
||||
} else {
|
||||
console.error('Unable to find System.Version pallet, skipping API extraction');
|
||||
}
|
||||
} else {
|
||||
console.error('Unable to find System pallet, skipping API extraction');
|
||||
}
|
||||
|
||||
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
||||
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
||||
}, {});
|
||||
@@ -145,13 +163,12 @@ export function generateCallTypes (registry: TypeRegistry, dest: string, apis: A
|
||||
});
|
||||
}
|
||||
|
||||
export function generateDefaultCalls (dest: string, data: HexString, rtVersion: { apis: Apis } | null = null, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { registry } = initMeta(data, extraTypes);
|
||||
export function generateDefaultCalls (dest: string, data: HexString, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
||||
const { metadata } = initMeta(data, extraTypes);
|
||||
|
||||
generateCallTypes(
|
||||
registry,
|
||||
metadata,
|
||||
dest,
|
||||
rtVersion && rtVersion.apis,
|
||||
extraTypes,
|
||||
isStrict,
|
||||
customLookupDefinitions
|
||||
|
||||
@@ -6,15 +6,11 @@ import type { HexString } from '@polkadot/util/types';
|
||||
import kusama from '@polkadot/types-support/metadata/static-kusama';
|
||||
import polkadot from '@polkadot/types-support/metadata/static-polkadot';
|
||||
import substrate from '@polkadot/types-support/metadata/static-substrate';
|
||||
import kusamaVer from '@polkadot/types-support/versions/kusama';
|
||||
import polkadotVer from '@polkadot/types-support/versions/polkadot';
|
||||
import substrateVer from '@polkadot/types-support/versions/substrate';
|
||||
|
||||
import { generateDefaultCalls, generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultInterface, generateDefaultLookup, generateDefaultQuery, generateDefaultRpc, generateDefaultTsDef, generateDefaultTx } from './generate';
|
||||
|
||||
const BASE = 'packages/api-augment/src';
|
||||
const METAS = Object.entries({ kusama, polkadot, substrate }) as [string, HexString][];
|
||||
const VERS = { kusama: kusamaVer, polkadot: polkadotVer, substrate: substrateVer };
|
||||
const METAS = Object.entries<HexString>({ kusama, polkadot, substrate });
|
||||
|
||||
export function main (): void {
|
||||
generateDefaultInterface();
|
||||
@@ -31,6 +27,6 @@ export function main (): void {
|
||||
generateDefaultEvents(`${BASE}/${name}/events.ts`, staticMeta);
|
||||
generateDefaultQuery(`${BASE}/${name}/query.ts`, staticMeta);
|
||||
generateDefaultTx(`${BASE}/${name}/tx.ts`, staticMeta);
|
||||
generateDefaultCalls(`${BASE}/${name}/runtime.ts`, staticMeta, VERS[name as keyof typeof VERS] as unknown as { apis: [HexString, number][] });
|
||||
generateDefaultCalls(`${BASE}/${name}/runtime.ts`, staticMeta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,14 +41,6 @@ async function getWsData <T> (endpoint: string, method: 'state_getMetadata' | 's
|
||||
});
|
||||
}
|
||||
|
||||
export async function getMetadataViaWs (endpoint: string): Promise<{ metadata: HexString, version: { apis: [HexString, number][] } }> {
|
||||
return Promise
|
||||
.all([
|
||||
getWsData<HexString>(endpoint, 'state_getMetadata'),
|
||||
getWsData<{ apis: [HexString, number][] }>(endpoint, 'state_getRuntimeVersion')
|
||||
])
|
||||
.then(([metadata, version]) => ({
|
||||
metadata,
|
||||
version
|
||||
}));
|
||||
export async function getMetadataViaWs (endpoint: string): Promise<HexString> {
|
||||
return getWsData<HexString>(endpoint, 'state_getMetadata');
|
||||
}
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
// Copyright 2017-2022 @polkadot/types-support authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
export default {
|
||||
specName: 'kusama',
|
||||
implName: 'parity-kusama',
|
||||
authoringVersion: 2,
|
||||
specVersion: 9230,
|
||||
implVersion: 0,
|
||||
apis: [
|
||||
[
|
||||
'0xdf6acb689907609b',
|
||||
4
|
||||
],
|
||||
[
|
||||
'0x37e397fc7c91f5e4',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x40fe3ad401f8959a',
|
||||
6
|
||||
],
|
||||
[
|
||||
'0xd2bc9897eed08f15',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xf78b278be53f454c',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0xaf2c0297a23e6d3d',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0x49eaaf1b548a0cb0',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x91d5df18b0d2cf58',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xed99c5acb25eedf5',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xcbca25e39f142387',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0x687ad44ad37f03c2',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xab3c0572291feb8b',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xbc9d89904f5b923f',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x37c8bb1350a9a2a8',
|
||||
1
|
||||
]
|
||||
],
|
||||
transactionVersion: 11,
|
||||
stateVersion: 0
|
||||
};
|
||||
@@ -1,72 +0,0 @@
|
||||
// Copyright 2017-2022 @polkadot/types-support authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
export default {
|
||||
specName: 'polkadot',
|
||||
implName: 'parity-polkadot',
|
||||
authoringVersion: 0,
|
||||
specVersion: 9230,
|
||||
implVersion: 0,
|
||||
apis: [
|
||||
[
|
||||
'0xdf6acb689907609b',
|
||||
4
|
||||
],
|
||||
[
|
||||
'0x37e397fc7c91f5e4',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x40fe3ad401f8959a',
|
||||
6
|
||||
],
|
||||
[
|
||||
'0xd2bc9897eed08f15',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xf78b278be53f454c',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0xaf2c0297a23e6d3d',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0x49eaaf1b548a0cb0',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x91d5df18b0d2cf58',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xed99c5acb25eedf5',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xcbca25e39f142387',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0x687ad44ad37f03c2',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xab3c0572291feb8b',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xbc9d89904f5b923f',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x37c8bb1350a9a2a8',
|
||||
1
|
||||
]
|
||||
],
|
||||
transactionVersion: 12,
|
||||
stateVersion: 0
|
||||
};
|
||||
@@ -1,68 +0,0 @@
|
||||
// Copyright 2017-2022 @polkadot/types-support authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable sort-keys */
|
||||
|
||||
export default {
|
||||
specName: 'node',
|
||||
implName: 'substrate-node',
|
||||
authoringVersion: 10,
|
||||
specVersion: 268,
|
||||
implVersion: 0,
|
||||
apis: [
|
||||
[
|
||||
'0xdf6acb689907609b',
|
||||
4
|
||||
],
|
||||
[
|
||||
'0x37e397fc7c91f5e4',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x40fe3ad401f8959a',
|
||||
6
|
||||
],
|
||||
[
|
||||
'0xd2bc9897eed08f15',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xf78b278be53f454c',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0xed99c5acb25eedf5',
|
||||
3
|
||||
],
|
||||
[
|
||||
'0xcbca25e39f142387',
|
||||
2
|
||||
],
|
||||
[
|
||||
'0x687ad44ad37f03c2',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xbc9d89904f5b923f',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x68b66ba122c93fa7',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x37c8bb1350a9a2a8',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0x91d5df18b0d2cf58',
|
||||
1
|
||||
],
|
||||
[
|
||||
'0xab3c0572291feb8b',
|
||||
1
|
||||
]
|
||||
],
|
||||
transactionVersion: 2,
|
||||
stateVersion: 1
|
||||
};
|
||||
Reference in New Issue
Block a user