From 62e437ba06b8fd6feb760e4b4bcbeeb38ca251e6 Mon Sep 17 00:00:00 2001 From: Jaco Date: Sat, 9 Jul 2022 07:24:23 +0200 Subject: [PATCH] Use system.version constant for runtime typegen (#5048) * Use system.version constant for runtime typegen * Adjust entries cast * Adjust --- CHANGELOG.md | 1 + packages/typegen/src/fromChain.ts | 25 ++----- packages/typegen/src/fromDefs.ts | 2 +- packages/typegen/src/generate/runtime.ts | 29 ++++++-- packages/typegen/src/interfacesTs.ts | 8 +-- packages/typegen/src/util/wsMeta.ts | 12 +--- packages/types-support/src/versions/kusama.ts | 72 ------------------- .../types-support/src/versions/polkadot.ts | 72 ------------------- .../types-support/src/versions/substrate.ts | 68 ------------------ 9 files changed, 35 insertions(+), 254 deletions(-) delete mode 100644 packages/types-support/src/versions/kusama.ts delete mode 100644 packages/types-support/src/versions/polkadot.ts delete mode 100644 packages/types-support/src/versions/substrate.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 849e9ff168..bbae505857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/typegen/src/fromChain.ts b/packages/typegen/src/fromChain.ts index 781ada79b8..52de5eb1ca 100644 --- a/packages/typegen/src/fromChain.ts +++ b/packages/typegen/src/fromChain.ts @@ -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).result; - } - // eslint-disable-next-line @typescript-eslint/no-var-requires const metadata = (require(assertFile(path.join(process.cwd(), endpoint))) as Record).result; - generate(metadata, rtv, pkg, output, isStrict); + generate(metadata, pkg, output, isStrict); } } diff --git a/packages/typegen/src/fromDefs.ts b/packages/typegen/src/fromDefs.ts index bcf8c2884b..0796dfd120 100644 --- a/packages/typegen/src/fromDefs.ts +++ b/packages/typegen/src/fromDefs.ts @@ -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 diff --git a/packages/typegen/src/generate/runtime.ts b/packages/typegen/src/generate/runtime.ts index 36e03cdde1..22bf19e22a 100644 --- a/packages/typegen/src/generate/runtime.ts +++ b/packages/typegen/src/generate/runtime.ts @@ -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): 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 diff --git a/packages/typegen/src/interfacesTs.ts b/packages/typegen/src/interfacesTs.ts index 6a5b12ee93..e0eab269af 100644 --- a/packages/typegen/src/interfacesTs.ts +++ b/packages/typegen/src/interfacesTs.ts @@ -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({ 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); } } diff --git a/packages/typegen/src/util/wsMeta.ts b/packages/typegen/src/util/wsMeta.ts index d8fb25786d..3a84246639 100644 --- a/packages/typegen/src/util/wsMeta.ts +++ b/packages/typegen/src/util/wsMeta.ts @@ -41,14 +41,6 @@ async function getWsData (endpoint: string, method: 'state_getMetadata' | 's }); } -export async function getMetadataViaWs (endpoint: string): Promise<{ metadata: HexString, version: { apis: [HexString, number][] } }> { - return Promise - .all([ - getWsData(endpoint, 'state_getMetadata'), - getWsData<{ apis: [HexString, number][] }>(endpoint, 'state_getRuntimeVersion') - ]) - .then(([metadata, version]) => ({ - metadata, - version - })); +export async function getMetadataViaWs (endpoint: string): Promise { + return getWsData(endpoint, 'state_getMetadata'); } diff --git a/packages/types-support/src/versions/kusama.ts b/packages/types-support/src/versions/kusama.ts deleted file mode 100644 index 129c368a71..0000000000 --- a/packages/types-support/src/versions/kusama.ts +++ /dev/null @@ -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 -}; diff --git a/packages/types-support/src/versions/polkadot.ts b/packages/types-support/src/versions/polkadot.ts deleted file mode 100644 index 19e085b25f..0000000000 --- a/packages/types-support/src/versions/polkadot.ts +++ /dev/null @@ -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 -}; diff --git a/packages/types-support/src/versions/substrate.ts b/packages/types-support/src/versions/substrate.ts deleted file mode 100644 index 16f5b92127..0000000000 --- a/packages/types-support/src/versions/substrate.ts +++ /dev/null @@ -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 -};