Set static metadata to v15 and update typegen to generate runtime apis and use v15 metadata (#5987)

* Set static metadata to v15

* Fix testUtil

* Fix metadata spec

* support metadata files

* lint and fix commented tests

* Fix test

* Add augment static runtime types by metadata

* Fix alias typing for runtime def types

* Cleanup and add remaining generated files

* lint

* Fix typegen for edgecase

* Update polkadot to 1003003

* Fix tests

* Set correct rpc
This commit is contained in:
Tarik Gul
2024-10-18 05:01:08 +03:00
committed by GitHub
parent 1772458304
commit bc199956f3
42 changed files with 160378 additions and 12515 deletions
+3 -3
View File
@@ -11,9 +11,9 @@ import Handlebars from 'handlebars';
import path from 'node:path';
import * as defaultDefinitions from '@polkadot/types/interfaces/definitions';
import staticKusama from '@polkadot/types-support/metadata/static-kusama';
import staticPolkadot from '@polkadot/types-support/metadata/static-polkadot';
import staticSubstrate from '@polkadot/types-support/metadata/static-substrate';
import staticKusama from '@polkadot/types-support/metadata/v15/kusama-hex';
import staticPolkadot from '@polkadot/types-support/metadata/v15/polkadot-hex';
import staticSubstrate from '@polkadot/types-support/metadata/v15/substrate-hex';
import { isString, stringify } from '@polkadot/util';
import { createImports, exportInterface, initMeta, readTemplate, writeFile } from '../util/index.js';
+122 -29
View File
@@ -1,8 +1,10 @@
// Copyright 2017-2024 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { RuntimeApiMethodMetadataV15, SiLookupTypeId } from '@polkadot/types/interfaces';
import type { Metadata } from '@polkadot/types/metadata/Metadata';
import type { DefinitionCallNamed, Definitions, Registry } from '@polkadot/types/types';
import type { DefinitionCall, DefinitionCallNamed, Definitions, DefinitionsCall, Registry } from '@polkadot/types/types';
import type { Vec } from '@polkadot/types-codec';
import type { HexString } from '@polkadot/util/types';
import type { ExtraTypes } from './types.js';
@@ -19,45 +21,136 @@ type Apis = [HexString, number][];
const generateCallsTypesTemplate = Handlebars.compile(readTemplate('calls'));
// This works similar to the PATHS_ALIAS set from the PortableRegistry
const aliases: Record<string, string> = {
KitchensinkRuntimeRuntimeCall: 'RuntimeCall',
OpaqueValue: 'Bytes',
PolkadotParachainPrimitivesPrimitivesId: 'ParaId',
PolkadotParachainPrimitivesPrimitivesValidationCodeHash: 'ValidationCodeHash',
PolkadotPrimitivesV7SlashingOpaqueKeyOwnershipProof: 'OpaqueKeyOwnershipProof',
PolkadotRuntimeRuntimeCall: 'RuntimeCall',
PrimitiveTypesH256: 'H256',
SpConsensusBabeOpaqueKeyOwnershipProof: 'OpaqueKeyOwnershipProof',
SpConsensusSlotsSlot: 'Slot',
SpCoreCryptoAccountId32: 'AccountId32',
SpCoreOpaqueMetadata: 'OpaqueMetadata',
SpRuntimeOpaqueValue: 'Bytes',
SpRuntimeUncheckedExtrinsic: 'Extrinsic',
StagingKusamaRuntimeRuntimeCall: 'RuntimeCall'
};
const getTypesViaAlias = (registry: Registry, id: SiLookupTypeId) => {
const typeName = registry.lookup.getName(id) || registry.lookup.getTypeDef(id).type;
if (aliases[typeName]) {
return aliases[typeName];
}
return typeName;
};
/** @internal */
function getDefs (apis: Apis | null, defs: Record<string, Definitions>): Record<string, Record<string, DefinitionCallNamed>> {
function getMethods (registry: Registry, methods: Vec<RuntimeApiMethodMetadataV15>) {
const result: Record<string, DefinitionCall> = {};
methods.forEach((m) => {
const { docs, inputs, name, output } = m;
result[name.toString()] = {
description: docs.map((d) => d.toString()).join(),
params: inputs.map(({ name, type }) => {
return { name: name.toString(), type: getTypesViaAlias(registry, type) };
}),
type: getTypesViaAlias(registry, output)
};
});
return result;
}
/** @internal */
function getRuntimeDefViaMetadata (registry: Registry) {
const result: DefinitionsCall = {};
const { apis } = registry.metadata;
for (let i = 0, count = apis.length; i < count; i++) {
const { methods, name } = apis[i];
result[name.toString()] = [{
methods: getMethods(registry, methods),
// We set the version to 0 here since it will not be relevant when we are grabbing the runtime apis
// from the Metadata.
version: 0
}];
}
return Object.entries(result);
}
/** @internal */
function getDefs (apis: Apis | null, defs: Record<string, Definitions>, registry: Registry): Record<string, Record<string, DefinitionCallNamed>> {
const named: Record<string, Record<string, DefinitionCallNamed>> = {};
const all = Object.values(defs);
const isApiInMetadata = registry.metadata.apis.length > 0;
for (let j = 0, jcount = all.length; j < jcount; j++) {
const set = all[j].runtime;
if (isApiInMetadata) {
const sections = getRuntimeDefViaMetadata(registry);
if (set) {
const sections = Object.entries(set);
for (let j = 0, jcount = sections.length; j < jcount; j++) {
const [_section, secs] = sections[j];
const sec = secs[0];
const sectionHash = blake2AsHex(_section, 64);
for (let i = 0, scount = sections.length; i < scount; i++) {
const [_section, sec] = sections[i];
const sectionHash = blake2AsHex(_section, 64);
const api = apis?.find(([h]) => h === sectionHash);
const section = stringCamelCase(_section);
const methods = Object.entries(sec.methods);
if (api) {
const ver = sec.find(({ version }) => version === api[1]);
if (!named[section]) {
named[section] = {};
}
if (ver) {
const methods = Object.entries(ver.methods);
const mcount = methods.length;
for (let m = 0, mcount = methods.length; m < mcount; m++) {
const [_method, def] = methods[m];
const method = stringCamelCase(_method);
if (mcount) {
const section = stringCamelCase(_section);
named[section][method] = objectSpread({ method, name: `${_section}_${_method}`, section, sectionHash }, def);
}
}
} else {
for (let j = 0, jcount = all.length; j < jcount; j++) {
const set = all[j].runtime;
if (!named[section]) {
named[section] = {};
}
for (let m = 0; m < mcount; m++) {
const [_method, def] = methods[m];
const method = stringCamelCase(_method);
named[section][method] = objectSpread({ method, name: `${_section}_${method}`, section, sectionHash, version: ver.version }, def);
if (set) {
const sections = Object.entries(set);
for (let i = 0, scount = sections.length; i < scount; i++) {
const [_section, sec] = sections[i];
const sectionHash = blake2AsHex(_section, 64);
const api = apis?.find(([h]) => h === sectionHash);
if (api) {
const ver = sec.find(({ version }) => version === api[1]);
if (ver) {
const methods = Object.entries(ver.methods);
const mcount = methods.length;
if (mcount) {
const section = stringCamelCase(_section);
if (!named[section]) {
named[section] = {};
}
for (let m = 0; m < mcount; m++) {
const [_method, def] = methods[m];
const method = stringCamelCase(_method);
named[section][method] = objectSpread({ method, name: `${_section}_${method}`, section, sectionHash, version: ver.version }, def);
}
}
} else {
console.warn(`Unable to find matching version for runtime ${_section}, expected ${api[1]}`);
}
} else {
console.warn(`Unable to find matching version for runtime ${_section}, expected ${api[1]}`);
}
}
}
@@ -101,7 +194,7 @@ export function generateCallTypes (registry: Registry, meta: Metadata, dest: str
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
}, {});
const definitions = getDefs(apis, imports.definitions as Record<string, Definitions>);
const definitions = getDefs(apis, imports.definitions as Record<string, Definitions>, registry);
const callKeys = Object.keys(definitions);
const modules = callKeys.map((section) => {
+2 -1
View File
@@ -57,8 +57,9 @@ function tsEnum (registry: Registry, definitions: Record<string, ModuleTypes>, {
// When the parent type does not have a lookupName, and the sub type is the same
// type as the parent we can take the lookupName from the sub.
// This is specific to `StagingXcmV4Junction`.
// see: https://github.com/polkadot-js/api/pull/5812
if (sub && !Array.isArray(sub) && type.includes(`${sub.type};`)) {
if (sub && !Array.isArray(sub) && type.includes(`${sub.type};`) && sub.lookupName === 'StagingXcmV4Junction') {
extractedLookupName = sub.lookupName;
}
+3 -3
View File
@@ -3,9 +3,9 @@
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 kusama from '@polkadot/types-support/metadata/v15/kusama-hex';
import polkadot from '@polkadot/types-support/metadata/v15/polkadot-hex';
import substrate from '@polkadot/types-support/metadata/v15/substrate-hex';
import { generateDefaultConsts, generateDefaultErrors, generateDefaultEvents, generateDefaultInterface, generateDefaultLookup, generateDefaultQuery, generateDefaultRpc, generateDefaultRuntime, generateDefaultTsDef, generateDefaultTx } from './generate/index.js';
+12 -1
View File
@@ -13,12 +13,23 @@ interface Result {
registry: TypeRegistry;
}
/**
* This helper method has been transitioned to work with V14, V15 and up.
*/
export function initMeta (staticMeta: HexString, extraTypes: ExtraTypes = {}): Result {
const registry = new TypeRegistry();
registerDefinitions(registry, extraTypes);
const metadata = new Metadata(registry, staticMeta);
let metadata: Metadata;
try {
const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', staticMeta).toU8a()).unwrap();
metadata = new Metadata(registry, opaqueMetadata.toHex());
} catch {
metadata = new Metadata(registry, staticMeta);
}
registry.setMetadata(metadata);