@pezkuwi/typegen 16.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/build/augment.d.ts +2 -0
- package/build/bundle.d.ts +2 -0
- package/build/extractChain.d.ts +1 -0
- package/build/fromChain.d.ts +1 -0
- package/build/fromDefs.d.ts +1 -0
- package/build/generate/consts.d.ts +5 -0
- package/build/generate/errors.d.ts +4 -0
- package/build/generate/events.d.ts +5 -0
- package/build/generate/index.d.ts +10 -0
- package/build/generate/interfaceRegistry.d.ts +4 -0
- package/build/generate/lookup.d.ts +4 -0
- package/build/generate/query.d.ts +5 -0
- package/build/generate/rpc.d.ts +6 -0
- package/build/generate/runtime.d.ts +7 -0
- package/build/generate/tsDef.d.ts +16 -0
- package/build/generate/tx.d.ts +5 -0
- package/build/generate/types.d.ts +12 -0
- package/build/index.d.ts +2 -0
- package/build/interfacesTs.d.ts +1 -0
- package/build/metadataMd.d.ts +1 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/scripts/polkadot-types-chain-info.mjs +7 -0
- package/build/scripts/polkadot-types-from-chain.mjs +7 -0
- package/build/scripts/polkadot-types-from-defs.mjs +7 -0
- package/build/scripts/polkadot-types-internal-interfaces.mjs +7 -0
- package/build/scripts/polkadot-types-internal-metadata.mjs +7 -0
- package/build/util/assert.d.ts +2 -0
- package/build/util/derived.d.ts +4 -0
- package/build/util/docs.d.ts +1 -0
- package/build/util/file.d.ts +2 -0
- package/build/util/formatting.d.ts +11 -0
- package/build/util/imports.d.ts +23 -0
- package/build/util/index.d.ts +15 -0
- package/build/util/initMeta.d.ts +12 -0
- package/build/util/register.d.ts +4 -0
- package/build/util/wsMeta.d.ts +4 -0
- package/package.json +52 -0
- package/scripts/polkadot-types-chain-info.mjs +7 -0
- package/scripts/polkadot-types-from-chain.mjs +7 -0
- package/scripts/polkadot-types-from-defs.mjs +7 -0
- package/scripts/polkadot-types-internal-interfaces.mjs +7 -0
- package/scripts/polkadot-types-internal-metadata.mjs +7 -0
- package/src/augment.ts +5 -0
- package/src/bundle.ts +5 -0
- package/src/extractChain.ts +54 -0
- package/src/fromChain.ts +123 -0
- package/src/fromDefs.ts +106 -0
- package/src/generate/consts.ts +112 -0
- package/src/generate/errors.ts +75 -0
- package/src/generate/events.ts +165 -0
- package/src/generate/index.ts +13 -0
- package/src/generate/interfaceRegistry.ts +85 -0
- package/src/generate/lookup.ts +294 -0
- package/src/generate/query.ts +169 -0
- package/src/generate/rpc.ts +158 -0
- package/src/generate/runtime.ts +284 -0
- package/src/generate/tsDef.ts +321 -0
- package/src/generate/tx.ts +152 -0
- package/src/generate/types.ts +26 -0
- package/src/index.ts +6 -0
- package/src/interfacesTs.ts +35 -0
- package/src/metadataMd.ts +844 -0
- package/src/packageDetect.ts +14 -0
- package/src/packageInfo.ts +6 -0
- package/src/templates/calls.hbs +30 -0
- package/src/templates/consts.hbs +28 -0
- package/src/templates/docs.hbs +7 -0
- package/src/templates/errors.hbs +28 -0
- package/src/templates/events.hbs +28 -0
- package/src/templates/header.hbs +2 -0
- package/src/templates/interfaceRegistry.hbs +15 -0
- package/src/templates/lookup/defs-named.hbs +12 -0
- package/src/templates/lookup/defs.hbs +15 -0
- package/src/templates/lookup/index.hbs +3 -0
- package/src/templates/lookup/types.hbs +14 -0
- package/src/templates/query.hbs +29 -0
- package/src/templates/rpc.hbs +22 -0
- package/src/templates/tsDef/index.hbs +3 -0
- package/src/templates/tsDef/moduleTypes.hbs +10 -0
- package/src/templates/tsDef/types.hbs +7 -0
- package/src/templates/tx.hbs +30 -0
- package/src/util/assert.ts +18 -0
- package/src/util/derived.spec.ts +58 -0
- package/src/util/derived.ts +133 -0
- package/src/util/docs.ts +13 -0
- package/src/util/file.ts +42 -0
- package/src/util/formatting.spec.ts +30 -0
- package/src/util/formatting.ts +295 -0
- package/src/util/imports.ts +164 -0
- package/src/util/index.ts +18 -0
- package/src/util/initMeta.ts +37 -0
- package/src/util/register.ts +12 -0
- package/src/util/wsMeta.ts +70 -0
- package/tsconfig.build.json +28 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.scripts.json +19 -0
- package/tsconfig.spec.json +17 -0
package/src/fromDefs.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
5
|
+
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import yargs from 'yargs';
|
|
9
|
+
import { hideBin } from 'yargs/helpers';
|
|
10
|
+
|
|
11
|
+
import * as substrateDefs from '@pezkuwi/types/interfaces/definitions';
|
|
12
|
+
import { isHex } from '@pezkuwi/util';
|
|
13
|
+
|
|
14
|
+
import { generateDefaultLookup } from './generate/index.js';
|
|
15
|
+
import { generateInterfaceTypes } from './generate/interfaceRegistry.js';
|
|
16
|
+
import { generateTsDef } from './generate/tsDef.js';
|
|
17
|
+
import { assertDir, assertFile, getMetadataViaWs } from './util/index.js';
|
|
18
|
+
|
|
19
|
+
interface ArgV { input: string; package: string; endpoint?: string; }
|
|
20
|
+
|
|
21
|
+
async function mainPromise (): Promise<void> {
|
|
22
|
+
const { endpoint, input, package: pkg } = yargs(hideBin(process.argv)).strict().options({
|
|
23
|
+
endpoint: {
|
|
24
|
+
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',
|
|
25
|
+
type: 'string'
|
|
26
|
+
},
|
|
27
|
+
input: {
|
|
28
|
+
description: 'The directory to use for the user definitions',
|
|
29
|
+
required: true,
|
|
30
|
+
type: 'string'
|
|
31
|
+
},
|
|
32
|
+
package: {
|
|
33
|
+
description: 'The package name & path to use for the user types',
|
|
34
|
+
required: true,
|
|
35
|
+
type: 'string'
|
|
36
|
+
}
|
|
37
|
+
}).argv as ArgV;
|
|
38
|
+
|
|
39
|
+
const inputPath = assertDir(path.join(process.cwd(), input));
|
|
40
|
+
let userDefs: Record<string, any> = {};
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
const defCont = await import(
|
|
44
|
+
assertFile(path.join(inputPath, 'definitions.ts'))
|
|
45
|
+
) as Record<string, any>;
|
|
46
|
+
|
|
47
|
+
userDefs = defCont;
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('ERROR: Unable to load user definitions:', (error as Error).message);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const userKeys = Object.keys(userDefs);
|
|
53
|
+
const filteredBase = Object
|
|
54
|
+
.entries(substrateDefs as Record<string, unknown>)
|
|
55
|
+
.filter(([key]) => {
|
|
56
|
+
if (userKeys.includes(key)) {
|
|
57
|
+
console.warn(`Override found for ${key} in user types, ignoring in @polkadot/types`);
|
|
58
|
+
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
})
|
|
64
|
+
.reduce((defs: Record<string, any>, [key, value]) => {
|
|
65
|
+
defs[key] = value;
|
|
66
|
+
|
|
67
|
+
return defs;
|
|
68
|
+
}, {});
|
|
69
|
+
|
|
70
|
+
const allDefs = {
|
|
71
|
+
'@pezkuwi/types/interfaces': filteredBase,
|
|
72
|
+
[pkg]: userDefs
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
generateTsDef(allDefs, inputPath, pkg);
|
|
76
|
+
generateInterfaceTypes(allDefs, path.join(inputPath, 'augment-types.ts'));
|
|
77
|
+
|
|
78
|
+
if (endpoint) {
|
|
79
|
+
let metaHex: HexString;
|
|
80
|
+
|
|
81
|
+
if (endpoint.startsWith('wss://') || endpoint.startsWith('ws://')) {
|
|
82
|
+
metaHex = await getMetadataViaWs(endpoint);
|
|
83
|
+
} else {
|
|
84
|
+
metaHex = (
|
|
85
|
+
JSON.parse(
|
|
86
|
+
fs.readFileSync(assertFile(path.join(process.cwd(), endpoint)), 'utf-8')
|
|
87
|
+
) as { result: HexString }
|
|
88
|
+
).result;
|
|
89
|
+
|
|
90
|
+
if (!isHex(metaHex)) {
|
|
91
|
+
throw new Error('Invalid metadata file');
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
generateDefaultLookup(inputPath, metaHex);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function main (): void {
|
|
100
|
+
mainPromise().catch((error) => {
|
|
101
|
+
console.error();
|
|
102
|
+
console.error(error);
|
|
103
|
+
console.error();
|
|
104
|
+
process.exit(1);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { Metadata } from '@pezkuwi/types/metadata/Metadata';
|
|
5
|
+
import type { Definitions } from '@pezkuwi/types/types';
|
|
6
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
7
|
+
|
|
8
|
+
import Handlebars from 'handlebars';
|
|
9
|
+
|
|
10
|
+
import * as defaultDefs from '@pezkuwi/types/interfaces/definitions';
|
|
11
|
+
import lookupDefinitions from '@pezkuwi/types-augment/lookup/definitions';
|
|
12
|
+
import { stringCamelCase } from '@pezkuwi/util';
|
|
13
|
+
|
|
14
|
+
import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
15
|
+
import { ignoreUnusedLookups } from './lookup.js';
|
|
16
|
+
import { type ExtraTypes, getDeprecationNotice } from './types.js';
|
|
17
|
+
|
|
18
|
+
const generateForMetaTemplate = Handlebars.compile(readTemplate('consts'));
|
|
19
|
+
|
|
20
|
+
/** @internal */
|
|
21
|
+
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
|
22
|
+
writeFile(dest, (): string => {
|
|
23
|
+
const allTypes = {
|
|
24
|
+
'@pezkuwi/types-augment': {
|
|
25
|
+
lookup: {
|
|
26
|
+
...lookupDefinitions,
|
|
27
|
+
...customLookupDefinitions
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
'@pezkuwi/types/interfaces': defaultDefs,
|
|
31
|
+
...extraTypes
|
|
32
|
+
};
|
|
33
|
+
const imports = createImports(allTypes);
|
|
34
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
35
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
36
|
+
}, {});
|
|
37
|
+
const { lookup, pallets, registry } = meta.asLatest;
|
|
38
|
+
const usedTypes = new Set<string>([]);
|
|
39
|
+
|
|
40
|
+
const modules = pallets
|
|
41
|
+
.filter(({ constants }) => constants.length > 0)
|
|
42
|
+
.map(({ constants, name }) => {
|
|
43
|
+
if (!isStrict) {
|
|
44
|
+
setImports(allDefs, imports, ['Codec']);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const items = constants
|
|
48
|
+
.map(({ deprecationInfo, docs, name, type }) => {
|
|
49
|
+
const typeDef = lookup.getTypeDef(type);
|
|
50
|
+
const returnType = typeDef.lookupName || formatType(registry, allDefs, typeDef, imports);
|
|
51
|
+
|
|
52
|
+
if (!deprecationInfo.isNotDeprecated) {
|
|
53
|
+
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name), 'Constant');
|
|
54
|
+
|
|
55
|
+
const items = docs.length
|
|
56
|
+
? ['', deprecationNotice]
|
|
57
|
+
: [deprecationNotice];
|
|
58
|
+
|
|
59
|
+
docs.push(...items.map((text) => registry.createType('Text', text)));
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Add the type to the list of used types
|
|
63
|
+
if (!(imports.primitiveTypes[returnType])) {
|
|
64
|
+
usedTypes.add(returnType);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
setImports(allDefs, imports, [returnType]);
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
docs,
|
|
71
|
+
name: stringCamelCase(name),
|
|
72
|
+
type: returnType
|
|
73
|
+
};
|
|
74
|
+
})
|
|
75
|
+
.sort(compareName);
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
items,
|
|
79
|
+
name: stringCamelCase(name)
|
|
80
|
+
};
|
|
81
|
+
})
|
|
82
|
+
.sort(compareName);
|
|
83
|
+
|
|
84
|
+
// filter out the unused lookup types from imports
|
|
85
|
+
ignoreUnusedLookups([...usedTypes], imports);
|
|
86
|
+
|
|
87
|
+
return generateForMetaTemplate({
|
|
88
|
+
headerType: 'chain',
|
|
89
|
+
imports,
|
|
90
|
+
isStrict,
|
|
91
|
+
modules,
|
|
92
|
+
types: [
|
|
93
|
+
...Object.keys(imports.localTypes).sort().map<{ file: string; types: string[] }>((packagePath) => ({
|
|
94
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
95
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
96
|
+
})),
|
|
97
|
+
{
|
|
98
|
+
file: '@pezkuwi/api-base/types',
|
|
99
|
+
types: ['ApiTypes', 'AugmentedConst']
|
|
100
|
+
}
|
|
101
|
+
]
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Call `generateForMeta()` with current static metadata
|
|
107
|
+
/** @internal */
|
|
108
|
+
export function generateDefaultConsts (dest: string, data: HexString, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
|
109
|
+
const { metadata } = initMeta(data, extraTypes);
|
|
110
|
+
|
|
111
|
+
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
|
112
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { VariantDeprecationInfoV16 } from '@pezkuwi/types/interfaces';
|
|
5
|
+
import type { Metadata } from '@pezkuwi/types/metadata/Metadata';
|
|
6
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
7
|
+
|
|
8
|
+
import Handlebars from 'handlebars';
|
|
9
|
+
|
|
10
|
+
import { stringCamelCase } from '@pezkuwi/util';
|
|
11
|
+
|
|
12
|
+
import { compareName, createImports, initMeta, readTemplate, writeFile } from '../util/index.js';
|
|
13
|
+
import { type ExtraTypes, getDeprecationNotice } from './types.js';
|
|
14
|
+
|
|
15
|
+
const generateForMetaTemplate = Handlebars.compile(readTemplate('errors'));
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
function generateForMeta (meta: Metadata, dest: string, isStrict: boolean): void {
|
|
19
|
+
writeFile(dest, (): string => {
|
|
20
|
+
const imports = createImports({});
|
|
21
|
+
const { lookup, pallets } = meta.asLatest;
|
|
22
|
+
const modules = pallets
|
|
23
|
+
.filter(({ errors }) => errors.isSome)
|
|
24
|
+
.map((data) => {
|
|
25
|
+
const name = data.name;
|
|
26
|
+
const errors = data.errors.unwrap();
|
|
27
|
+
const deprecationInfo = errors.deprecationInfo.toJSON();
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
items: lookup.getSiType(errors.type).def.asVariant.variants
|
|
31
|
+
.map(({ docs, index, name }) => {
|
|
32
|
+
const rawStatus = deprecationInfo?.[index.toNumber()];
|
|
33
|
+
|
|
34
|
+
if (rawStatus) {
|
|
35
|
+
const deprecationVariantInfo: VariantDeprecationInfoV16 = meta.registry.createTypeUnsafe('VariantDeprecationInfoV16', [rawStatus]);
|
|
36
|
+
|
|
37
|
+
const deprecationNotice = getDeprecationNotice(deprecationVariantInfo, name.toString());
|
|
38
|
+
const notice = docs.length ? ['', deprecationNotice] : [deprecationNotice];
|
|
39
|
+
|
|
40
|
+
docs.push(...notice.map((text) => meta.registry.createType('Text', text)));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
docs,
|
|
45
|
+
name: name.toString()
|
|
46
|
+
};
|
|
47
|
+
})
|
|
48
|
+
.sort(compareName),
|
|
49
|
+
name: stringCamelCase(name)
|
|
50
|
+
};
|
|
51
|
+
})
|
|
52
|
+
.sort(compareName);
|
|
53
|
+
|
|
54
|
+
return generateForMetaTemplate({
|
|
55
|
+
headerType: 'chain',
|
|
56
|
+
imports,
|
|
57
|
+
isStrict,
|
|
58
|
+
modules,
|
|
59
|
+
types: [
|
|
60
|
+
{
|
|
61
|
+
file: '@pezkuwi/api-base/types',
|
|
62
|
+
types: ['ApiTypes', 'AugmentedError']
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Call `generateForMeta()` with current static metadata
|
|
70
|
+
/** @internal */
|
|
71
|
+
export function generateDefaultErrors (dest: string, data: HexString, extraTypes: ExtraTypes = {}, isStrict = false): void {
|
|
72
|
+
const { metadata } = initMeta(data, extraTypes);
|
|
73
|
+
|
|
74
|
+
return generateForMeta(metadata, dest, isStrict);
|
|
75
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { VariantDeprecationInfoV16 } from '@pezkuwi/types/interfaces';
|
|
5
|
+
import type { Metadata } from '@pezkuwi/types/metadata/Metadata';
|
|
6
|
+
import type { Definitions } from '@pezkuwi/types/types';
|
|
7
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
8
|
+
|
|
9
|
+
import Handlebars from 'handlebars';
|
|
10
|
+
|
|
11
|
+
import * as defaultDefs from '@pezkuwi/types/interfaces/definitions';
|
|
12
|
+
import lookupDefinitions from '@pezkuwi/types-augment/lookup/definitions';
|
|
13
|
+
import { stringCamelCase } from '@pezkuwi/util';
|
|
14
|
+
|
|
15
|
+
import { compareName, createImports, formatType, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
16
|
+
import { ignoreUnusedLookups } from './lookup.js';
|
|
17
|
+
import { type ExtraTypes, getDeprecationNotice } from './types.js';
|
|
18
|
+
|
|
19
|
+
const generateForMetaTemplate = Handlebars.compile(readTemplate('events'));
|
|
20
|
+
|
|
21
|
+
// For babel itself we need some extra aliasing
|
|
22
|
+
// Also avoid reserved words to prevent generating invalid TS
|
|
23
|
+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
|
|
24
|
+
const ALIAS = [
|
|
25
|
+
'symbol',
|
|
26
|
+
'break',
|
|
27
|
+
'case',
|
|
28
|
+
'catch',
|
|
29
|
+
'class',
|
|
30
|
+
'const',
|
|
31
|
+
'continue',
|
|
32
|
+
'debugger',
|
|
33
|
+
'default',
|
|
34
|
+
'delete',
|
|
35
|
+
'do',
|
|
36
|
+
'else',
|
|
37
|
+
'export',
|
|
38
|
+
'extends',
|
|
39
|
+
'false',
|
|
40
|
+
'finally',
|
|
41
|
+
'for',
|
|
42
|
+
'function',
|
|
43
|
+
'if',
|
|
44
|
+
'import',
|
|
45
|
+
'in',
|
|
46
|
+
'instanceof',
|
|
47
|
+
'new',
|
|
48
|
+
'null',
|
|
49
|
+
'return',
|
|
50
|
+
'static',
|
|
51
|
+
'super',
|
|
52
|
+
'switch',
|
|
53
|
+
'this',
|
|
54
|
+
'throw',
|
|
55
|
+
'true',
|
|
56
|
+
'try',
|
|
57
|
+
'typeof',
|
|
58
|
+
'var',
|
|
59
|
+
'void',
|
|
60
|
+
'while',
|
|
61
|
+
'with',
|
|
62
|
+
'yield'
|
|
63
|
+
];
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
function generateForMeta (meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
|
67
|
+
writeFile(dest, (): string => {
|
|
68
|
+
const allTypes = {
|
|
69
|
+
'@pezkuwi/types-augment': {
|
|
70
|
+
lookup: {
|
|
71
|
+
...lookupDefinitions,
|
|
72
|
+
...customLookupDefinitions
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
'@pezkuwi/types/interfaces': defaultDefs,
|
|
76
|
+
...extraTypes
|
|
77
|
+
};
|
|
78
|
+
const imports = createImports(allTypes);
|
|
79
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
80
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
81
|
+
}, {});
|
|
82
|
+
const { lookup, pallets, registry } = meta.asLatest;
|
|
83
|
+
const usedTypes = new Set<string>([]);
|
|
84
|
+
const modules = pallets
|
|
85
|
+
.filter(({ events }) => events.isSome)
|
|
86
|
+
.map((data) => {
|
|
87
|
+
const name = data.name;
|
|
88
|
+
const events = data.events.unwrap();
|
|
89
|
+
const deprecationInfo = events.deprecationInfo.toJSON();
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
items: lookup.getSiType(events.type).def.asVariant.variants
|
|
93
|
+
.map(({ docs, fields, index, name }) => {
|
|
94
|
+
const rawStatus = deprecationInfo?.[index.toNumber()];
|
|
95
|
+
|
|
96
|
+
if (rawStatus) {
|
|
97
|
+
const deprecationVariantInfo: VariantDeprecationInfoV16 = meta.registry.createTypeUnsafe('VariantDeprecationInfoV16', [rawStatus]);
|
|
98
|
+
const deprecationNotice = getDeprecationNotice(deprecationVariantInfo, name.toString());
|
|
99
|
+
const notice = docs.length ? ['', deprecationNotice] : [deprecationNotice];
|
|
100
|
+
|
|
101
|
+
docs.push(...notice.map((text) => meta.registry.createType('Text', text)));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const args = fields
|
|
105
|
+
.map(({ type }) => lookup.getTypeDef(type))
|
|
106
|
+
.map((typeDef) => {
|
|
107
|
+
const arg = typeDef.lookupName || formatType(registry, allDefs, typeDef, imports);
|
|
108
|
+
|
|
109
|
+
// Add the type to the list of used types
|
|
110
|
+
if (!(imports.primitiveTypes[arg])) {
|
|
111
|
+
usedTypes.add(arg);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return arg;
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const names = fields
|
|
118
|
+
.map(({ name }) => registry.lookup.sanitizeField(name)[0])
|
|
119
|
+
.filter((n): n is string => !!n);
|
|
120
|
+
|
|
121
|
+
setImports(allDefs, imports, args);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
docs,
|
|
125
|
+
name: name.toString(),
|
|
126
|
+
type: names.length !== 0 && names.length === args.length
|
|
127
|
+
? `[${names.map((n, i) => `${ALIAS.includes(n) ? `${n}_` : n}: ${args[i]}`).join(', ')}], { ${names.map((n, i) => `${n}: ${args[i]}`).join(', ')} }`
|
|
128
|
+
: `[${args.join(', ')}]`
|
|
129
|
+
};
|
|
130
|
+
})
|
|
131
|
+
.sort(compareName),
|
|
132
|
+
name: stringCamelCase(name)
|
|
133
|
+
};
|
|
134
|
+
})
|
|
135
|
+
.sort(compareName);
|
|
136
|
+
|
|
137
|
+
// filter out the unused lookup types from imports
|
|
138
|
+
ignoreUnusedLookups([...usedTypes], imports);
|
|
139
|
+
|
|
140
|
+
return generateForMetaTemplate({
|
|
141
|
+
headerType: 'chain',
|
|
142
|
+
imports,
|
|
143
|
+
isStrict,
|
|
144
|
+
modules,
|
|
145
|
+
types: [
|
|
146
|
+
...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({
|
|
147
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
148
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
149
|
+
})),
|
|
150
|
+
{
|
|
151
|
+
file: '@pezkuwi/api-base/types',
|
|
152
|
+
types: ['ApiTypes', 'AugmentedEvent']
|
|
153
|
+
}
|
|
154
|
+
]
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Call `generateForMeta()` with current static metadata
|
|
160
|
+
/** @internal */
|
|
161
|
+
export function generateDefaultEvents (dest: string, data: HexString, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
|
162
|
+
const { metadata } = initMeta(data, extraTypes);
|
|
163
|
+
|
|
164
|
+
return generateForMeta(metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
|
165
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { generateDefaultConsts } from './consts.js';
|
|
5
|
+
export { generateDefaultErrors } from './errors.js';
|
|
6
|
+
export { generateDefaultEvents } from './events.js';
|
|
7
|
+
export { generateDefaultInterface } from './interfaceRegistry.js';
|
|
8
|
+
export { generateDefaultLookup } from './lookup.js';
|
|
9
|
+
export { generateDefaultQuery } from './query.js';
|
|
10
|
+
export { generateDefaultRpc } from './rpc.js';
|
|
11
|
+
export { generateDefaultRuntime } from './runtime.js';
|
|
12
|
+
export { generateDefaultTsDef } from './tsDef.js';
|
|
13
|
+
export { generateDefaultTx } from './tx.js';
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { ModuleTypes } from '../util/imports.js';
|
|
5
|
+
|
|
6
|
+
import Handlebars from 'handlebars';
|
|
7
|
+
|
|
8
|
+
import { Json, Raw } from '@pezkuwi/types/codec';
|
|
9
|
+
import { TypeRegistry } from '@pezkuwi/types/create';
|
|
10
|
+
import * as defaultDefinitions from '@pezkuwi/types/interfaces/definitions';
|
|
11
|
+
import * as defaultPrimitives from '@pezkuwi/types/primitive';
|
|
12
|
+
|
|
13
|
+
import { createImports, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
14
|
+
|
|
15
|
+
const primitiveClasses = {
|
|
16
|
+
...defaultPrimitives,
|
|
17
|
+
Json,
|
|
18
|
+
Raw
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const generateInterfaceTypesTemplate = Handlebars.compile(readTemplate('interfaceRegistry'));
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
export function generateInterfaceTypes (importDefinitions: Record<string, Record<string, ModuleTypes>>, dest: string): void {
|
|
25
|
+
const registry = new TypeRegistry();
|
|
26
|
+
|
|
27
|
+
writeFile(dest, (): string => {
|
|
28
|
+
Object.entries(importDefinitions).reduce((acc, def) => Object.assign(acc, def), {});
|
|
29
|
+
|
|
30
|
+
const imports = createImports(importDefinitions);
|
|
31
|
+
const definitions = imports.definitions;
|
|
32
|
+
const items: string[] = [];
|
|
33
|
+
|
|
34
|
+
// first we create imports for our known classes from the API
|
|
35
|
+
Object
|
|
36
|
+
.keys(primitiveClasses)
|
|
37
|
+
.filter((name) => !name.includes('Generic'))
|
|
38
|
+
.forEach((primitiveName): void => {
|
|
39
|
+
setImports(definitions, imports, [primitiveName]);
|
|
40
|
+
|
|
41
|
+
items.push(primitiveName);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const existingTypes: Record<string, boolean> = {};
|
|
45
|
+
|
|
46
|
+
// ensure we have everything registered since we will get the definition
|
|
47
|
+
// form the available types (so any unknown should show after this)
|
|
48
|
+
Object.values(definitions).forEach(({ types }) => {
|
|
49
|
+
registry.register(types as Record<string, string>);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// create imports for everything that we have available
|
|
53
|
+
Object.values(definitions).forEach(({ types }) => {
|
|
54
|
+
setImports(definitions, imports, Object.keys(types));
|
|
55
|
+
|
|
56
|
+
const uniqueTypes = Object.keys(types).filter((type) => !existingTypes[type]);
|
|
57
|
+
|
|
58
|
+
uniqueTypes.forEach((type): void => {
|
|
59
|
+
existingTypes[type] = true;
|
|
60
|
+
|
|
61
|
+
items.push(type);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
return generateInterfaceTypesTemplate({
|
|
66
|
+
headerType: 'defs',
|
|
67
|
+
imports,
|
|
68
|
+
items: items.sort((a, b) => a.localeCompare(b)),
|
|
69
|
+
types: [
|
|
70
|
+
...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({
|
|
71
|
+
file: packagePath,
|
|
72
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
73
|
+
}))
|
|
74
|
+
]
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Generate `packages/types/src/interfaceRegistry.ts`, the registry of all interfaces
|
|
80
|
+
export function generateDefaultInterface (): void {
|
|
81
|
+
generateInterfaceTypes(
|
|
82
|
+
{ '@pezkuwi/types/interfaces': defaultDefinitions },
|
|
83
|
+
'packages/types-augment/src/registry/interfaces.ts'
|
|
84
|
+
);
|
|
85
|
+
}
|