@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
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { AnyString, Registry } from '@pezkuwi/types/types';
|
|
5
|
+
import type { LookupString } from '@pezkuwi/types-codec/types';
|
|
6
|
+
import type { TypeDef } from '@pezkuwi/types-create/types';
|
|
7
|
+
import type { ModuleTypes, TypeImports } from './imports.js';
|
|
8
|
+
|
|
9
|
+
import Handlebars from 'handlebars';
|
|
10
|
+
|
|
11
|
+
import * as typesCodec from '@pezkuwi/types-codec';
|
|
12
|
+
import { getTypeDef, paramsNotation, TypeDefInfo } from '@pezkuwi/types-create';
|
|
13
|
+
import { isString, stringify } from '@pezkuwi/util';
|
|
14
|
+
|
|
15
|
+
import { readTemplate } from './file.js';
|
|
16
|
+
import { setImports } from './imports.js';
|
|
17
|
+
|
|
18
|
+
interface ImportDef {
|
|
19
|
+
file: string;
|
|
20
|
+
types: string[];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface This {
|
|
24
|
+
imports: TypeImports;
|
|
25
|
+
types: ImportDef[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const NO_CODEC = ['Tuple', 'VecFixed'];
|
|
29
|
+
const ON_CODEC = Object.keys(typesCodec);
|
|
30
|
+
const ON_CODEC_TYPES = ['Codec', 'AnyJson', 'AnyFunction', 'AnyNumber', 'AnyString', 'AnyTuple', 'AnyU8a', 'ICompact', 'IEnum', 'IMap', 'INumber', 'IOption', 'IResult', 'ISet', 'IStruct', 'ITuple', 'IU8a', 'IVec', 'IMethod'];
|
|
31
|
+
|
|
32
|
+
export const HEADER = (type: 'chain' | 'defs'): string => `// Auto-generated via \`yarn polkadot-types-from-${type}\`, do not edit\n/* eslint-disable */\n\n`;
|
|
33
|
+
|
|
34
|
+
function extractImports ({ imports, types }: This): string[] {
|
|
35
|
+
const toplevel = [
|
|
36
|
+
...Object.keys(imports.codecTypes),
|
|
37
|
+
...Object.keys(imports.extrinsicTypes),
|
|
38
|
+
...Object.keys(imports.genericTypes),
|
|
39
|
+
...Object.keys(imports.metadataTypes),
|
|
40
|
+
...Object.keys(imports.primitiveTypes)
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
return [
|
|
44
|
+
{
|
|
45
|
+
file: '@pezkuwi/types',
|
|
46
|
+
types: toplevel.filter((n) =>
|
|
47
|
+
!NO_CODEC.includes(n) && !ON_CODEC.includes(n)
|
|
48
|
+
)
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
file: '@pezkuwi/types/lookup',
|
|
52
|
+
types: Object.keys(imports.lookupTypes)
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
file: '@pezkuwi/types/types',
|
|
56
|
+
types: Object.keys(imports.typesTypes).filter((n) =>
|
|
57
|
+
!ON_CODEC_TYPES.includes(n)
|
|
58
|
+
)
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
file: '@pezkuwi/types-codec',
|
|
62
|
+
types: toplevel.filter((n) =>
|
|
63
|
+
!NO_CODEC.includes(n) && ON_CODEC.includes(n)
|
|
64
|
+
)
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
file: '@pezkuwi/types-codec/types',
|
|
68
|
+
types: Object.keys(imports.typesTypes).filter((n) =>
|
|
69
|
+
ON_CODEC_TYPES.includes(n)
|
|
70
|
+
)
|
|
71
|
+
},
|
|
72
|
+
...types
|
|
73
|
+
]
|
|
74
|
+
.filter(({ types }) => types.length)
|
|
75
|
+
.sort(({ file }, b) => file.localeCompare(b.file))
|
|
76
|
+
.map(({ file, types }) => `import type { ${types.sort().join(', ')} } from '${file}';`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
Handlebars.registerPartial({
|
|
80
|
+
header: Handlebars.compile(readTemplate('header'))
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
Handlebars.registerHelper({
|
|
84
|
+
importsAll () {
|
|
85
|
+
return extractImports(this as unknown as This)
|
|
86
|
+
.join('\n');
|
|
87
|
+
},
|
|
88
|
+
importsPackage () {
|
|
89
|
+
return extractImports(this as unknown as This)
|
|
90
|
+
.filter((l) => !l.includes("from '."))
|
|
91
|
+
.join('\n ');
|
|
92
|
+
},
|
|
93
|
+
importsRelative () {
|
|
94
|
+
return extractImports(this as unknown as This)
|
|
95
|
+
.filter((l) => l.includes("from '."))
|
|
96
|
+
.join('\n');
|
|
97
|
+
},
|
|
98
|
+
trim (options: { fn: (self: unknown) => string }) {
|
|
99
|
+
return options.fn(this).trim();
|
|
100
|
+
},
|
|
101
|
+
upper (options: { fn: (self: unknown) => string }) {
|
|
102
|
+
return options.fn(this).toUpperCase();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
// helper to generate a `export interface <Name> extends <Base> {<Body>}
|
|
107
|
+
/** @internal */
|
|
108
|
+
export function exportInterface (lookupIndex = -1, name = '', base: string, body = '', withShortcut = false): string {
|
|
109
|
+
// * @description extends [[${base}]]
|
|
110
|
+
const doc = withShortcut
|
|
111
|
+
? ''
|
|
112
|
+
: `/** @name ${name}${lookupIndex !== -1 ? ` (${lookupIndex})` : ''} */\n`;
|
|
113
|
+
|
|
114
|
+
return `${doc}${withShortcut ? '' : `export interface ${name} extends ${base} `}{${body.length ? '\n' : ''}${body}${withShortcut ? ' ' : ''}}`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function singleParamNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
|
|
118
|
+
const sub = (typeDef.sub as TypeDef);
|
|
119
|
+
|
|
120
|
+
setImports(definitions, imports, [wrapper, sub.lookupName]);
|
|
121
|
+
|
|
122
|
+
return paramsNotation(wrapper, sub.lookupName || formatType(registry, definitions, sub.type, imports, withShortcut));
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function dualParamsNotation (registry: Registry, wrapper: string, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean): string {
|
|
126
|
+
const [a, b] = (typeDef.sub as TypeDef[]);
|
|
127
|
+
|
|
128
|
+
setImports(definitions, imports, [wrapper, a.lookupName, b.lookupName]);
|
|
129
|
+
|
|
130
|
+
return paramsNotation(wrapper, [
|
|
131
|
+
a.lookupName || formatType(registry, definitions, a.type, imports, withShortcut),
|
|
132
|
+
b.lookupName || formatType(registry, definitions, b.type, imports, withShortcut)
|
|
133
|
+
]);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const formatters: Record<TypeDefInfo, (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => string> = {
|
|
137
|
+
[TypeDefInfo.Compact]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
138
|
+
return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut);
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
[TypeDefInfo.DoNotConstruct]: (_registry: Registry, _typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, _withShortcut: boolean) => {
|
|
142
|
+
setImports(definitions, imports, ['DoNotConstruct']);
|
|
143
|
+
|
|
144
|
+
return 'DoNotConstruct';
|
|
145
|
+
},
|
|
146
|
+
|
|
147
|
+
[TypeDefInfo.Enum]: (_registry: Registry, typeDef: TypeDef, _definitions: Record<string, ModuleTypes>, _imports: TypeImports, _withShortcut: boolean) => {
|
|
148
|
+
if (typeDef.lookupName) {
|
|
149
|
+
return typeDef.lookupName;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
throw new Error(`TypeDefInfo.Enum: Parameter formatting not implemented on ${stringify(typeDef)}`);
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
[TypeDefInfo.Int]: (_registry: Registry, typeDef: TypeDef, _definitions: Record<string, ModuleTypes>, _imports: TypeImports, _withShortcut: boolean) => {
|
|
156
|
+
throw new Error(`TypeDefInfo.Int: Parameter formatting not implemented on ${stringify(typeDef)}`);
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
[TypeDefInfo.UInt]: (_registry: Registry, typeDef: TypeDef, _definitions: Record<string, ModuleTypes>, _imports: TypeImports, _withShortcut: boolean) => {
|
|
160
|
+
throw new Error(`TypeDefInfo.UInt: Parameter formatting not implemented on ${stringify(typeDef)}`);
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
[TypeDefInfo.Null]: (_registry: Registry, _typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, _withShortcut: boolean) => {
|
|
164
|
+
setImports(definitions, imports, ['Null']);
|
|
165
|
+
|
|
166
|
+
return 'Null';
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
[TypeDefInfo.Option]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
170
|
+
return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut);
|
|
171
|
+
},
|
|
172
|
+
|
|
173
|
+
[TypeDefInfo.Plain]: (_registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, _withShortcut: boolean) => {
|
|
174
|
+
setImports(definitions, imports, [typeDef.type]);
|
|
175
|
+
|
|
176
|
+
return typeDef.type;
|
|
177
|
+
},
|
|
178
|
+
|
|
179
|
+
[TypeDefInfo.Range]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
180
|
+
return singleParamNotation(registry, 'Range', typeDef, definitions, imports, withShortcut);
|
|
181
|
+
},
|
|
182
|
+
|
|
183
|
+
[TypeDefInfo.RangeInclusive]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
184
|
+
return singleParamNotation(registry, 'RangeInclusive', typeDef, definitions, imports, withShortcut);
|
|
185
|
+
},
|
|
186
|
+
|
|
187
|
+
[TypeDefInfo.Set]: (_registry: Registry, typeDef: TypeDef, _definitions: Record<string, ModuleTypes>, _imports: TypeImports, _withShortcut: boolean) => {
|
|
188
|
+
throw new Error(`TypeDefInfo.Set: Parameter formatting not implemented on ${stringify(typeDef)}`);
|
|
189
|
+
},
|
|
190
|
+
|
|
191
|
+
[TypeDefInfo.Si]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
192
|
+
return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type as LookupString), imports, withShortcut);
|
|
193
|
+
},
|
|
194
|
+
|
|
195
|
+
[TypeDefInfo.Struct]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
196
|
+
if (typeDef.lookupName) {
|
|
197
|
+
return typeDef.lookupName;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const sub = typeDef.sub as TypeDef[];
|
|
201
|
+
|
|
202
|
+
setImports(definitions, imports, ['Struct', ...sub.map(({ lookupName }) => lookupName)]);
|
|
203
|
+
|
|
204
|
+
return `{${withShortcut ? ' ' : '\n'}${
|
|
205
|
+
sub.map(({ lookupName, name, type }, index) => [
|
|
206
|
+
name || `unknown${index}`,
|
|
207
|
+
lookupName || formatType(registry, definitions, type, imports, withShortcut)
|
|
208
|
+
]).map(([k, t]) => `${withShortcut ? '' : ' readonly '}${k}: ${t};`).join(withShortcut ? ' ' : '\n')
|
|
209
|
+
}${withShortcut ? ' ' : '\n '}} & Struct`;
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
[TypeDefInfo.Tuple]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
213
|
+
const sub = typeDef.sub as TypeDef[];
|
|
214
|
+
|
|
215
|
+
setImports(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => lookupName)]);
|
|
216
|
+
|
|
217
|
+
// `(a,b)` gets transformed into `ITuple<[a, b]>`
|
|
218
|
+
return paramsNotation('ITuple', `[${
|
|
219
|
+
sub.map(({ lookupName, type }) =>
|
|
220
|
+
lookupName || formatType(registry, definitions, type, imports, withShortcut)
|
|
221
|
+
).join(', ')
|
|
222
|
+
}]`);
|
|
223
|
+
},
|
|
224
|
+
|
|
225
|
+
[TypeDefInfo.Vec]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
226
|
+
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
[TypeDefInfo.VecFixed]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
230
|
+
const sub = (typeDef.sub as TypeDef);
|
|
231
|
+
|
|
232
|
+
if (sub.type === 'u8') {
|
|
233
|
+
setImports(definitions, imports, ['U8aFixed']);
|
|
234
|
+
|
|
235
|
+
return 'U8aFixed';
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
[TypeDefInfo.BTreeMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
242
|
+
return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut);
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
[TypeDefInfo.BTreeSet]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
246
|
+
return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut);
|
|
247
|
+
},
|
|
248
|
+
|
|
249
|
+
[TypeDefInfo.HashMap]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
250
|
+
return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut);
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
[TypeDefInfo.Linkage]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
254
|
+
return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut);
|
|
255
|
+
},
|
|
256
|
+
|
|
257
|
+
[TypeDefInfo.Result]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
258
|
+
return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut);
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
[TypeDefInfo.WrapperKeepOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
262
|
+
return singleParamNotation(registry, 'WrapperKeepOpaque', typeDef, definitions, imports, withShortcut);
|
|
263
|
+
},
|
|
264
|
+
|
|
265
|
+
[TypeDefInfo.WrapperOpaque]: (registry: Registry, typeDef: TypeDef, definitions: Record<string, ModuleTypes>, imports: TypeImports, withShortcut: boolean) => {
|
|
266
|
+
return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Correctly format a given type
|
|
272
|
+
*/
|
|
273
|
+
/** @internal */
|
|
274
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
275
|
+
export function formatType (registry: Registry, definitions: Record<string, ModuleTypes>, type: AnyString | TypeDef, imports: TypeImports, withShortcut = false): string {
|
|
276
|
+
let typeDef: TypeDef;
|
|
277
|
+
|
|
278
|
+
if (isString(type)) {
|
|
279
|
+
const _type = type.toString();
|
|
280
|
+
|
|
281
|
+
// If type is "unorthodox" (i.e. `{ something: any }` for an Enum input or `[a | b | c, d | e | f]` for a Tuple's similar types),
|
|
282
|
+
// we return it as-is
|
|
283
|
+
if (withShortcut && /(^{.+:.+})|^\([^,]+\)|^\(.+\)\[\]|^\[.+\]/.exec(_type) && !/\[\w+;\w+\]/.exec(_type)) {
|
|
284
|
+
return _type;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
typeDef = getTypeDef(type);
|
|
288
|
+
} else {
|
|
289
|
+
typeDef = type;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
setImports(definitions, imports, [typeDef.lookupName || typeDef.type]);
|
|
293
|
+
|
|
294
|
+
return formatters[typeDef.info](registry, typeDef, definitions, imports, withShortcut);
|
|
295
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import * as codecClasses from '@pezkuwi/types/codec';
|
|
5
|
+
import * as extrinsicClasses from '@pezkuwi/types/extrinsic';
|
|
6
|
+
import * as genericClasses from '@pezkuwi/types/generic';
|
|
7
|
+
import * as primitiveClasses from '@pezkuwi/types/primitive';
|
|
8
|
+
import { getTypeDef, TypeDefInfo } from '@pezkuwi/types-create';
|
|
9
|
+
|
|
10
|
+
export interface ModuleTypes {
|
|
11
|
+
types: Record<string, unknown>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// these map all the codec and primitive types for import, see the TypeImports below. If
|
|
15
|
+
// we have an unseen type, it is `undefined`/`false`, if we need to import it, it is `true`
|
|
16
|
+
type TypeExist = Record<string, boolean>;
|
|
17
|
+
|
|
18
|
+
// local and absolute imports map to this format
|
|
19
|
+
// { [moduleName]: { [typeName]: true } }
|
|
20
|
+
type TypeExistMap = Record<string, TypeExist>;
|
|
21
|
+
|
|
22
|
+
export interface TypeImports {
|
|
23
|
+
codecTypes: TypeExist; // `import {} from '@pezkuwi/types/codec`
|
|
24
|
+
extrinsicTypes: TypeExist; // `import {} from '@pezkuwi/types/extrinsic`
|
|
25
|
+
genericTypes: TypeExist; // `import {} from '@pezkuwi/types/generic`
|
|
26
|
+
ignoredTypes: string[]; // No need to import these types
|
|
27
|
+
localTypes: TypeExistMap; // `import {} from '../something'`
|
|
28
|
+
lookupTypes: TypeExistMap; // `import {} from '@pezkuwi/types/lookup`
|
|
29
|
+
primitiveTypes: TypeExist; // `import {} from '@pezkuwi/types/primitive`
|
|
30
|
+
metadataTypes: TypeExist; // `import {} from '@pezkuwi/types/metadata`
|
|
31
|
+
typesTypes: TypeExist; // `import {} from '@pezkuwi/types/types`
|
|
32
|
+
definitions: Record<string, ModuleTypes>; // all definitions
|
|
33
|
+
typeToModule: Record<string, string>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// returns the top-level types in the alternatives list, taking into account nested [], <> and () items
|
|
37
|
+
// E.g. for the string '[a<b,c>, d | e]' we return the array ['a<b,c>', 'd', 'e']
|
|
38
|
+
function splitAlternatives (type: string): string[] {
|
|
39
|
+
const alternatives = [];
|
|
40
|
+
let beginOfAlternative = 1;
|
|
41
|
+
let level = 0;
|
|
42
|
+
|
|
43
|
+
// we assume that the string starts with '['
|
|
44
|
+
for (let i = 1, count = type.length; i < count; i++) {
|
|
45
|
+
if (level === 0) {
|
|
46
|
+
switch (type[i]) {
|
|
47
|
+
case ']':
|
|
48
|
+
case ',':
|
|
49
|
+
case '|':
|
|
50
|
+
alternatives.push(type.substring(beginOfAlternative, i).trim());
|
|
51
|
+
beginOfAlternative = i + 1;
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
switch (type[i]) {
|
|
57
|
+
case '[':
|
|
58
|
+
case '(':
|
|
59
|
+
case '<':
|
|
60
|
+
level++;
|
|
61
|
+
break;
|
|
62
|
+
case ']':
|
|
63
|
+
case ')':
|
|
64
|
+
case '>':
|
|
65
|
+
level--;
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return alternatives;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Maps the types as found to the source location. This is used to generate the
|
|
74
|
+
// imports in the output file, dep-duped and sorted
|
|
75
|
+
/** @internal */
|
|
76
|
+
export function setImports (allDefs: Record<string, ModuleTypes>, imports: TypeImports, types: (string | null | undefined)[]): void {
|
|
77
|
+
const { codecTypes, extrinsicTypes, genericTypes, ignoredTypes, localTypes, metadataTypes, primitiveTypes, typesTypes } = imports;
|
|
78
|
+
|
|
79
|
+
types.filter((t): t is string => !!t).forEach((type): void => {
|
|
80
|
+
if (ignoredTypes.includes(type)) {
|
|
81
|
+
// do nothing
|
|
82
|
+
} else if (['AnyNumber', 'CallFunction', 'Codec', 'IExtrinsic', 'IMethod', 'ITuple'].includes(type)) {
|
|
83
|
+
typesTypes[type] = true;
|
|
84
|
+
} else if (['Metadata', 'PortableRegistry'].includes(type)) {
|
|
85
|
+
metadataTypes[type] = true;
|
|
86
|
+
} else if ((codecClasses as Record<string, unknown>)[type]) {
|
|
87
|
+
codecTypes[type] = true;
|
|
88
|
+
} else if ((extrinsicClasses as Record<string, unknown>)[type]) {
|
|
89
|
+
extrinsicTypes[type] = true;
|
|
90
|
+
} else if ((genericClasses as Record<string, unknown>)[type]) {
|
|
91
|
+
genericTypes[type] = true;
|
|
92
|
+
} else if ((primitiveClasses as Record<string, unknown>)[type]) {
|
|
93
|
+
primitiveTypes[type] = true;
|
|
94
|
+
} else if (type.startsWith('[') && type.includes('|')) {
|
|
95
|
+
const splitTypes = splitAlternatives(type);
|
|
96
|
+
|
|
97
|
+
setImports(allDefs, imports, splitTypes);
|
|
98
|
+
} else if (type.includes('<') || type.includes('(') || type.includes('[')) {
|
|
99
|
+
// If the type is a bit special (tuple, fixed u8, nested type...), then we
|
|
100
|
+
// need to parse it with `getTypeDef`.
|
|
101
|
+
const typeDef = getTypeDef(type);
|
|
102
|
+
|
|
103
|
+
setImports(allDefs, imports, [TypeDefInfo[typeDef.info]]);
|
|
104
|
+
|
|
105
|
+
// TypeDef.sub is a `TypeDef | TypeDef[]`
|
|
106
|
+
if (Array.isArray(typeDef.sub)) {
|
|
107
|
+
typeDef.sub.forEach((subType) => setImports(allDefs, imports, [subType.lookupName || subType.type]));
|
|
108
|
+
} else if (typeDef.sub && (typeDef.info !== TypeDefInfo.VecFixed || typeDef.sub.type !== 'u8')) {
|
|
109
|
+
// typeDef.sub is a TypeDef in this case
|
|
110
|
+
setImports(allDefs, imports, [typeDef.sub.lookupName || typeDef.sub.type]);
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
// find this module inside the exports from the rest
|
|
114
|
+
const [moduleName] = Object.entries(allDefs).find(([, { types }]): boolean =>
|
|
115
|
+
Object.keys(types).includes(type)
|
|
116
|
+
) || [null];
|
|
117
|
+
|
|
118
|
+
if (moduleName) {
|
|
119
|
+
localTypes[moduleName][type] = true;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Create an Imports object, can be pre-filled with `ignoredTypes`
|
|
126
|
+
/** @internal */
|
|
127
|
+
export function createImports (importDefinitions: Record<string, Record<string, ModuleTypes>>, { types }: ModuleTypes = { types: {} }): TypeImports {
|
|
128
|
+
const definitions = {} as Record<string, ModuleTypes>;
|
|
129
|
+
const typeToModule = {} as Record<string, string>;
|
|
130
|
+
|
|
131
|
+
Object.entries(importDefinitions).forEach(([packagePath, packageDef]): void => {
|
|
132
|
+
Object.entries(packageDef).forEach(([name, moduleDef]): void => {
|
|
133
|
+
const fullName = `${packagePath}/${name}`;
|
|
134
|
+
|
|
135
|
+
definitions[fullName] = moduleDef;
|
|
136
|
+
|
|
137
|
+
Object.keys(moduleDef.types).forEach((type): void => {
|
|
138
|
+
if (typeToModule[type]) {
|
|
139
|
+
console.warn(`\t\tWARN: Overwriting duplicated type '${type}' ${typeToModule[type]} -> ${fullName}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
typeToModule[type] = fullName;
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
codecTypes: {},
|
|
149
|
+
definitions,
|
|
150
|
+
extrinsicTypes: {},
|
|
151
|
+
genericTypes: {},
|
|
152
|
+
ignoredTypes: Object.keys(types),
|
|
153
|
+
localTypes: Object.keys(definitions).reduce((local: Record<string, TypeExist>, mod): Record<string, TypeExist> => {
|
|
154
|
+
local[mod] = {};
|
|
155
|
+
|
|
156
|
+
return local;
|
|
157
|
+
}, {}),
|
|
158
|
+
lookupTypes: {},
|
|
159
|
+
metadataTypes: {},
|
|
160
|
+
primitiveTypes: {},
|
|
161
|
+
typeToModule,
|
|
162
|
+
typesTypes: {}
|
|
163
|
+
};
|
|
164
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export * from './assert.js';
|
|
5
|
+
export * from './derived.js';
|
|
6
|
+
export * from './docs.js';
|
|
7
|
+
export * from './file.js';
|
|
8
|
+
export * from './formatting.js';
|
|
9
|
+
export * from './imports.js';
|
|
10
|
+
export * from './initMeta.js';
|
|
11
|
+
export * from './register.js';
|
|
12
|
+
export * from './wsMeta.js';
|
|
13
|
+
|
|
14
|
+
interface Cmp { name: { toString(): string } }
|
|
15
|
+
|
|
16
|
+
export function compareName (a: Cmp, b: Cmp): number {
|
|
17
|
+
return a.name.toString().localeCompare(b.name.toString());
|
|
18
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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
|
+
import type { ExtraTypes } from '../generate/types.js';
|
|
6
|
+
|
|
7
|
+
import { Metadata, TypeRegistry } from '@pezkuwi/types';
|
|
8
|
+
|
|
9
|
+
import { registerDefinitions } from './register.js';
|
|
10
|
+
|
|
11
|
+
interface Result {
|
|
12
|
+
metadata: Metadata;
|
|
13
|
+
registry: TypeRegistry;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This helper method has been transitioned to work with V14, V15 and up.
|
|
18
|
+
*/
|
|
19
|
+
export function initMeta (staticMeta: HexString, extraTypes: ExtraTypes = {}): Result {
|
|
20
|
+
const registry = new TypeRegistry();
|
|
21
|
+
|
|
22
|
+
registerDefinitions(registry, extraTypes);
|
|
23
|
+
|
|
24
|
+
let metadata: Metadata;
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', staticMeta).toU8a()).unwrap();
|
|
28
|
+
|
|
29
|
+
metadata = new Metadata(registry, opaqueMetadata.toHex());
|
|
30
|
+
} catch {
|
|
31
|
+
metadata = new Metadata(registry, staticMeta);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
registry.setMetadata(metadata);
|
|
35
|
+
|
|
36
|
+
return { metadata, registry };
|
|
37
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { TypeRegistry } from '@pezkuwi/types/create';
|
|
5
|
+
|
|
6
|
+
export function registerDefinitions (registry: TypeRegistry, extras: Record<string, Record<string, { types: Record<string, any> }>>): void {
|
|
7
|
+
Object.values(extras).forEach((def): void => {
|
|
8
|
+
Object.values(def).forEach(({ types }): void => {
|
|
9
|
+
registry.register(types);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
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 { promiseTracker } from '@pezkuwi/api/promise/decorateMethod';
|
|
7
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
8
|
+
import { stringify, u8aToHex } from '@pezkuwi/util';
|
|
9
|
+
import { WebSocket } from '@pezkuwi/x-ws';
|
|
10
|
+
|
|
11
|
+
async function getWsData <T> (endpoint: string, method: 'rpc_methods' | 'state_call' | 'state_getMetadata' | 'state_getRuntimeVersion', params?: string[]): Promise<T> {
|
|
12
|
+
return new Promise((resolve, reject): void => {
|
|
13
|
+
const tracker = promiseTracker<T>(resolve, reject);
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
const websocket = new WebSocket(endpoint);
|
|
17
|
+
|
|
18
|
+
websocket.onclose = (event: { code: number; reason: string }): void => {
|
|
19
|
+
if (event.code !== 1000) {
|
|
20
|
+
tracker.reject(new Error(`disconnected, code: '${event.code}' reason: '${event.reason}'`));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
websocket.onerror = (event: unknown): void => {
|
|
25
|
+
tracker.reject(new Error(`WebSocket error:: ${stringify(event)}`));
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
websocket.onopen = (): void => {
|
|
29
|
+
console.log('connected');
|
|
30
|
+
params
|
|
31
|
+
? websocket.send(`{"id":"1","jsonrpc":"2.0","method":"${method}","params":[${params.map((param) => `"${param}"`).join(',')}]}`)
|
|
32
|
+
: websocket.send(`{"id":"1","jsonrpc":"2.0","method":"${method}","params":[]}`);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
websocket.onmessage = (message: { data: string }): void => {
|
|
36
|
+
try {
|
|
37
|
+
tracker.resolve((JSON.parse(message.data) as { result: T }).result);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
tracker.reject(error as Error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
websocket.close();
|
|
43
|
+
};
|
|
44
|
+
} catch (error) {
|
|
45
|
+
tracker.reject(error as Error);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function getMetadataViaWs (endpoint: string, metadataVer?: number): Promise<HexString> {
|
|
51
|
+
const registry = new TypeRegistry();
|
|
52
|
+
|
|
53
|
+
if (metadataVer) {
|
|
54
|
+
return await getWsData<HexString>(endpoint, 'state_call', ['Metadata_metadata_at_version', u8aToHex(registry.createType('u32', metadataVer).toU8a())]);
|
|
55
|
+
} else {
|
|
56
|
+
return await getWsData<HexString>(endpoint, 'state_getMetadata');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function getRpcMethodsViaWs (endpoint: string): Promise<string[]> {
|
|
61
|
+
const result = await getWsData<{ methods: string[] }>(endpoint, 'rpc_methods');
|
|
62
|
+
|
|
63
|
+
return result.methods;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export async function getRuntimeVersionViaWs (endpoint: string): Promise<[apiHash: string, apiVersion: number][]> {
|
|
67
|
+
const result = await getWsData<{ apis: [string, number][] }>(endpoint, 'state_getRuntimeVersion');
|
|
68
|
+
|
|
69
|
+
return result.apis;
|
|
70
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": "..",
|
|
5
|
+
"outDir": "./build",
|
|
6
|
+
"rootDir": "./src"
|
|
7
|
+
},
|
|
8
|
+
"include": [
|
|
9
|
+
"src/**/*"
|
|
10
|
+
],
|
|
11
|
+
"exclude": [
|
|
12
|
+
"scripts/**/*",
|
|
13
|
+
"**/*.spec.ts",
|
|
14
|
+
"**/mod.ts"
|
|
15
|
+
],
|
|
16
|
+
"references": [
|
|
17
|
+
{ "path": "../api/tsconfig.build.json" },
|
|
18
|
+
{ "path": "../api-augment/tsconfig.build.json" },
|
|
19
|
+
{ "path": "../api-derive/tsconfig.build.json" },
|
|
20
|
+
{ "path": "../rpc-augment/tsconfig.build.json" },
|
|
21
|
+
{ "path": "../rpc-provider/tsconfig.build.json" },
|
|
22
|
+
{ "path": "../types/tsconfig.build.json" },
|
|
23
|
+
{ "path": "../types-augment/tsconfig.build.json" },
|
|
24
|
+
{ "path": "../types-codec/tsconfig.build.json" },
|
|
25
|
+
{ "path": "../types-create/tsconfig.build.json" },
|
|
26
|
+
{ "path": "../types-support/tsconfig.build.json" }
|
|
27
|
+
]
|
|
28
|
+
}
|