Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/api/src/base/Decorate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Observable } from 'rxjs';
import type { AugmentedCall, DeriveCustom, QueryableCalls } from '@polkadot/api-base/types';
import type { RpcInterface } from '@polkadot/rpc-core/types';
import type { Metadata, StorageKey, Text, u64, Vec } from '@polkadot/types';
import type { Call, Hash, RuntimeApiMethodMetadataV15, RuntimeVersion } from '@polkadot/types/interfaces';
import type { Call, Hash, RuntimeApiMethodMetadataV16, RuntimeVersion } from '@polkadot/types/interfaces';
import type { DecoratedMeta } from '@polkadot/types/metadata/decorate/types';
import type { StorageEntry } from '@polkadot/types/primitive/types';
import type { AnyFunction, AnyJson, AnyTuple, CallFunction, Codec, DefinitionCall, DefinitionCallNamed, DefinitionRpc, DefinitionRpcSub, DefinitionsCall, DefinitionsCallEntry, DetectCodec, IMethod, IStorageKey, Registry, RegistryError, RegistryTypes } from '@polkadot/types/types';
Expand Down Expand Up @@ -494,7 +494,7 @@ export abstract class Decorate<ApiType extends ApiTypes> extends Events {
}

// Helper for _getRuntimeDefsViaMetadata
protected _getMethods (registry: Registry, methods: Vec<RuntimeApiMethodMetadataV15>) {
protected _getMethods (registry: Registry, methods: Vec<RuntimeApiMethodMetadataV16>) {
const result: Record<string, DefinitionCall> = {};

methods.forEach((m) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/api/src/base/Init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { VersionedRegistry } from './types.js';
import { firstValueFrom, map, of, switchMap } from 'rxjs';

import { Metadata, TypeRegistry } from '@polkadot/types';
import { LATEST_EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/constants';
import { getSpecAlias, getSpecExtensions, getSpecHasher, getSpecRpc, getSpecTypes, getUpgradeVersion } from '@polkadot/types-known';
import { assertReturn, BN_ZERO, isUndefined, logger, noop, objectSpread, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
import { blake2AsHex, cryptoWaitReady } from '@polkadot/util-crypto';
Expand All @@ -22,7 +23,8 @@ import { Decorate } from './Decorate.js';

const KEEPALIVE_INTERVAL = 10000;
const WITH_VERSION_SHORTCUT = false;
const SUPPORTED_METADATA_VERSIONS = [15, 14];

const SUPPORTED_METADATA_VERSIONS = [16, 15, 14];

const l = logger('api/init');

Expand Down Expand Up @@ -371,7 +373,7 @@ export abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
throw new Error('Invalid initializion order, runtimeVersion is not available');
}

this._extrinsicType = metadata.asLatest.extrinsic.version.toNumber();
this._extrinsicType = metadata.asLatest.extrinsic.versions.at(-1) || LATEST_EXTRINSIC_VERSION;
this._rx.extrinsicType = this._extrinsicType;
this._rx.genesisHash = this._genesisHash;
this._rx.runtimeVersion = runtimeVersion;
Expand Down
14 changes: 12 additions & 2 deletions packages/typegen/src/generate/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import type { Metadata } from '@polkadot/types/metadata/Metadata';
import type { Definitions } from '@polkadot/types/types';
import type { HexString } from '@polkadot/util/types';
import type { ExtraTypes } from './types.js';

import Handlebars from 'handlebars';

Expand All @@ -14,6 +13,7 @@ import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
import { ignoreUnusedLookups } from './lookup.js';
import { type ExtraTypes, getDeprecationNotice } from './types.js';

const generateForMetaTemplate = Handlebars.compile(readTemplate('consts'));

Expand Down Expand Up @@ -45,10 +45,20 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
}

const items = constants
.map(({ docs, name, type }) => {
.map(({ deprecationInfo, docs, name, type }) => {
const typeDef = lookup.getTypeDef(type);
const returnType = typeDef.lookupName || formatType(registry, allDefs, typeDef, imports);

if (!deprecationInfo.isNotDeprecated) {
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name), 'Constant');

const items = docs.length
? ['', deprecationNotice]
: [deprecationNotice];

docs.push(...items.map((text) => registry.createType('Text', text)));
}

// Add the type to the list of used types
if (!(imports.primitiveTypes[returnType])) {
usedTypes.add(returnType);
Expand Down
40 changes: 30 additions & 10 deletions packages/typegen/src/generate/errors.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// Copyright 2017-2025 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { VariantDeprecationInfoV16 } from '@polkadot/types/interfaces';
import type { Metadata } from '@polkadot/types/metadata/Metadata';
import type { HexString } from '@polkadot/util/types';
import type { ExtraTypes } from './types.js';

import Handlebars from 'handlebars';

import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, initMeta, readTemplate, writeFile } from '../util/index.js';
import { type ExtraTypes, getDeprecationNotice } from './types.js';

const generateForMetaTemplate = Handlebars.compile(readTemplate('errors'));

Expand All @@ -20,15 +21,34 @@ function generateForMeta (meta: Metadata, dest: string, isStrict: boolean): void
const { lookup, pallets } = meta.asLatest;
const modules = pallets
.filter(({ errors }) => errors.isSome)
.map(({ errors, name }) => ({
items: lookup.getSiType(errors.unwrap().type).def.asVariant.variants
.map(({ docs, name }) => ({
docs,
name: name.toString()
}))
.sort(compareName),
name: stringCamelCase(name)
}))
.map((data) => {
const name = data.name;
const errors = data.errors.unwrap();
const deprecationInfo = errors.deprecationInfo.toJSON();

return {
items: lookup.getSiType(errors.type).def.asVariant.variants
.map(({ docs, index, name }) => {
const rawStatus = deprecationInfo?.[index.toNumber()];

if (rawStatus) {
const deprecationVariantInfo: VariantDeprecationInfoV16 = meta.registry.createTypeUnsafe('VariantDeprecationInfoV16', [rawStatus]);

const deprecationNotice = getDeprecationNotice(deprecationVariantInfo, name.toString());
const notice = docs.length ? ['', deprecationNotice] : [deprecationNotice];

docs.push(...notice.map((text) => meta.registry.createType('Text', text)));
}

return {
docs,
name: name.toString()
};
})
.sort(compareName),
name: stringCamelCase(name)
};
})
.sort(compareName);

return generateForMetaTemplate({
Expand Down
85 changes: 51 additions & 34 deletions packages/typegen/src/generate/events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2017-2025 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { VariantDeprecationInfoV16 } from '@polkadot/types/interfaces';
import type { Metadata } from '@polkadot/types/metadata/Metadata';
import type { Definitions } from '@polkadot/types/types';
import type { HexString } from '@polkadot/util/types';
import type { ExtraTypes } from './types.js';

import Handlebars from 'handlebars';

Expand All @@ -14,6 +14,7 @@ import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
import { ignoreUnusedLookups } from './lookup.js';
import { type ExtraTypes, getDeprecationNotice } from './types.js';

const generateForMetaTemplate = Handlebars.compile(readTemplate('events'));

Expand Down Expand Up @@ -82,39 +83,55 @@ function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes,
const usedTypes = new Set<string>([]);
const modules = pallets
.filter(({ events }) => events.isSome)
.map(({ events, name }) => ({
items: lookup.getSiType(events.unwrap().type).def.asVariant.variants
.map(({ docs, fields, name }) => {
const args = fields
.map(({ type }) => lookup.getTypeDef(type))
.map((typeDef) => {
const arg = typeDef.lookupName || formatType(registry, allDefs, typeDef, imports);

// Add the type to the list of used types
if (!(imports.primitiveTypes[arg])) {
usedTypes.add(arg);
}

return arg;
});

const names = fields
.map(({ name }) => registry.lookup.sanitizeField(name)[0])
.filter((n): n is string => !!n);

setImports(allDefs, imports, args);

return {
docs,
name: name.toString(),
type: names.length !== 0 && names.length === args.length
? `[${names.map((n, i) => `${ALIAS.includes(n) ? `${n}_` : n}: ${args[i]}`).join(', ')}], { ${names.map((n, i) => `${n}: ${args[i]}`).join(', ')} }`
: `[${args.join(', ')}]`
};
})
.sort(compareName),
name: stringCamelCase(name)
}))
.map((data) => {
const name = data.name;
const events = data.events.unwrap();
const deprecationInfo = events.deprecationInfo.toJSON();

return {
items: lookup.getSiType(events.type).def.asVariant.variants
.map(({ docs, fields, index, name }) => {
const rawStatus = deprecationInfo?.[index.toNumber()];

if (rawStatus) {
const deprecationVariantInfo: VariantDeprecationInfoV16 = meta.registry.createTypeUnsafe('VariantDeprecationInfoV16', [rawStatus]);
const deprecationNotice = getDeprecationNotice(deprecationVariantInfo, name.toString());
const notice = docs.length ? ['', deprecationNotice] : [deprecationNotice];

docs.push(...notice.map((text) => meta.registry.createType('Text', text)));
}

const args = fields
.map(({ type }) => lookup.getTypeDef(type))
.map((typeDef) => {
const arg = typeDef.lookupName || formatType(registry, allDefs, typeDef, imports);

// Add the type to the list of used types
if (!(imports.primitiveTypes[arg])) {
usedTypes.add(arg);
}

return arg;
});

const names = fields
.map(({ name }) => registry.lookup.sanitizeField(name)[0])
.filter((n): n is string => !!n);

setImports(allDefs, imports, args);

return {
docs,
name: name.toString(),
type: names.length !== 0 && names.length === args.length
? `[${names.map((n, i) => `${ALIAS.includes(n) ? `${n}_` : n}: ${args[i]}`).join(', ')}], { ${names.map((n, i) => `${n}: ${args[i]}`).join(', ')} }`
: `[${args.join(', ')}]`
};
})
.sort(compareName),
name: stringCamelCase(name)
};
})
.sort(compareName);

// filter out the unused lookup types from imports
Expand Down
14 changes: 12 additions & 2 deletions packages/typegen/src/generate/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { Metadata, PortableRegistry } from '@polkadot/types/metadata';
import type { Definitions, Registry } from '@polkadot/types/types';
import type { HexString } from '@polkadot/util/types';
import type { ModuleTypes, TypeImports } from '../util/imports.js';
import type { ExtraTypes } from './types.js';

import Handlebars from 'handlebars';

Expand All @@ -17,6 +16,7 @@ import { stringCamelCase } from '@polkadot/util';

import { compareName, createImports, formatType, getSimilarTypes, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
import { ignoreUnusedLookups } from './lookup.js';
import { type ExtraTypes, getDeprecationNotice } from './types.js';

const generateForMetaTemplate = Handlebars.compile(readTemplate('query'));

Expand Down Expand Up @@ -93,8 +93,18 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr
.map(({ name, storage }) => {
const items = storage.unwrap().items
.map((storageEntry) => {
const { deprecationInfo, docs, name } = storageEntry;
const [isOptional, args, params, _returnType] = entrySignature(lookup, allDefs, registry, name.toString(), storageEntry, imports);

if (!deprecationInfo.isNotDeprecated) {
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name));
const items = docs.length
? ['', deprecationNotice]
: [deprecationNotice];

docs.push(...items.map((text) => registry.createType('Text', text)));
}

// Add the type and args to the list of used types
if (!(imports.primitiveTypes[_returnType])) {
usedTypes.add(_returnType);
Expand All @@ -110,7 +120,7 @@ function generateForMeta (registry: Registry, meta: Metadata, dest: string, extr

return {
args,
docs: storageEntry.docs,
docs,
entryType: 'AugmentedQuery',
name: stringCamelCase(storageEntry.name),
params,
Expand Down
18 changes: 13 additions & 5 deletions packages/typegen/src/generate/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Copyright 2017-2025 @polkadot/typegen authors & contributors
// SPDX-License-Identifier: Apache-2.0

import type { RuntimeApiMethodMetadataV15, SiLookupTypeId } from '@polkadot/types/interfaces';
import type { RuntimeApiMethodMetadataV16, SiLookupTypeId } from '@polkadot/types/interfaces';
import type { Metadata } from '@polkadot/types/metadata/Metadata';
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';

import Handlebars from 'handlebars';

Expand All @@ -16,6 +15,7 @@ import { objectSpread, stringCamelCase } from '@polkadot/util';
import { blake2AsHex } from '@polkadot/util-crypto';

import { createImports, formatType, getSimilarTypes, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
import { type ExtraTypes, getDeprecationNotice } from './types.js';

type Apis = [HexString, number][];

Expand Down Expand Up @@ -52,14 +52,22 @@ const getTypesViaAlias = (registry: Registry, id: SiLookupTypeId) => {
};

/** @internal */
function getMethods (registry: Registry, methods: Vec<RuntimeApiMethodMetadataV15>) {
function getMethods (registry: Registry, methods: Vec<RuntimeApiMethodMetadataV16>) {
const result: Record<string, DefinitionCall> = {};

methods.forEach((m) => {
const { docs, inputs, name, output } = m;
const { deprecationInfo, docs, inputs, name, output } = m;
let description = docs.map((d) => d.toString()).join();

if (!deprecationInfo.isNotDeprecated) {
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name));
const notice = description.length ? `\n * ${deprecationNotice}` : ` * ${deprecationNotice}`;

description += notice;
}

result[name.toString()] = {
description: docs.map((d) => d.toString()).join(),
description,
params: inputs.map(({ name, type }) => {
return { name: name.toString(), type: getTypesViaAlias(registry, type) };
}),
Expand Down
Loading