@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,294 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { PortableType, SiLookupTypeId, SiPath, SiTypeParameter } from '@pezkuwi/types/interfaces';
|
|
5
|
+
import type { PortableRegistry } from '@pezkuwi/types/metadata';
|
|
6
|
+
import type { Registry } from '@pezkuwi/types/types';
|
|
7
|
+
import type { TypeDef } from '@pezkuwi/types-create/types';
|
|
8
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
9
|
+
|
|
10
|
+
import Handlebars from 'handlebars';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
import * as defaultDefinitions from '@pezkuwi/types/interfaces/definitions';
|
|
14
|
+
import staticAhKusama from '@pezkuwi/types-support/metadata/v15/asset-hub-kusama-hex';
|
|
15
|
+
import staticAhPolkadot from '@pezkuwi/types-support/metadata/v15/asset-hub-polkadot-hex';
|
|
16
|
+
import staticKusama from '@pezkuwi/types-support/metadata/v15/kusama-hex';
|
|
17
|
+
import staticPolkadot from '@pezkuwi/types-support/metadata/v15/polkadot-hex';
|
|
18
|
+
import staticSubstrate from '@pezkuwi/types-support/metadata/v15/substrate-hex';
|
|
19
|
+
import { isString, stringify } from '@pezkuwi/util';
|
|
20
|
+
|
|
21
|
+
import { createImports, exportInterface, initMeta, readTemplate, type TypeImports, writeFile } from '../util/index.js';
|
|
22
|
+
import { typeEncoders } from './tsDef.js';
|
|
23
|
+
|
|
24
|
+
// Record<string, >
|
|
25
|
+
interface ParsedDef {
|
|
26
|
+
_set: Record<string, number>;
|
|
27
|
+
|
|
28
|
+
[key: string]: string | Record<string, string> | Record<string, number>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const WITH_TYPEDEF = false;
|
|
32
|
+
|
|
33
|
+
const generateLookupDefsTmpl = Handlebars.compile(readTemplate('lookup/defs'));
|
|
34
|
+
const generateLookupDefsNamedTmpl = Handlebars.compile(readTemplate('lookup/defs-named'));
|
|
35
|
+
const generateLookupIndexTmpl = Handlebars.compile(readTemplate('lookup/index'));
|
|
36
|
+
const generateLookupTypesTmpl = Handlebars.compile(readTemplate('lookup/types'));
|
|
37
|
+
const generateRegistryTmpl = Handlebars.compile(readTemplate('interfaceRegistry'));
|
|
38
|
+
|
|
39
|
+
function generateParamType (registry: Registry, { name, type }: SiTypeParameter): string {
|
|
40
|
+
if (type.isSome) {
|
|
41
|
+
const link = registry.lookup.types[type.unwrap().toNumber()];
|
|
42
|
+
|
|
43
|
+
if (link.type.path.length) {
|
|
44
|
+
return generateTypeDocs(registry, null, link.type.path, link.type.params);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return name.toString();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function generateTypeDocs (registry: Registry, id: SiLookupTypeId | null, path: SiPath, params: SiTypeParameter[]): string {
|
|
52
|
+
return `${id ? `${registry.createLookupType(id)}${path.length ? ': ' : ''}` : ''}${path.map((p) => p.toString()).join('::')}${params.length ? `<${params.map((p) => generateParamType(registry, p)).join(', ')}>` : ''}`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function formatObject (lines: string[]): string[] {
|
|
56
|
+
const max = lines.length - 1;
|
|
57
|
+
|
|
58
|
+
return [
|
|
59
|
+
'{',
|
|
60
|
+
...lines.map((l, index) =>
|
|
61
|
+
(l.endsWith(',') || l.endsWith('{') || index === max || lines[index + 1].endsWith('}') || lines[index + 1].endsWith('}'))
|
|
62
|
+
? l
|
|
63
|
+
: `${l},`
|
|
64
|
+
),
|
|
65
|
+
'}'
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function expandSet (parsed: Record<string, number>): string[] {
|
|
70
|
+
return formatObject(
|
|
71
|
+
Object.entries(parsed).reduce<string[]>((all, [k, v]) => {
|
|
72
|
+
all.push(`${k}: ${v}`);
|
|
73
|
+
|
|
74
|
+
return all;
|
|
75
|
+
}, [])
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function expandObject (parsed: ParsedDef): string[] {
|
|
80
|
+
if (parsed._set) {
|
|
81
|
+
return expandSet(parsed._set);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return formatObject(
|
|
85
|
+
Object.entries(parsed).reduce<string[]>((all, [k, v]) => {
|
|
86
|
+
const inner = isString(v)
|
|
87
|
+
? expandType(v)
|
|
88
|
+
: Array.isArray(v)
|
|
89
|
+
? [`[${(v as string[]).map((e) => `'${e}'`).join(', ')}]`]
|
|
90
|
+
: expandObject(v as ParsedDef);
|
|
91
|
+
|
|
92
|
+
inner.forEach((l, index): void => {
|
|
93
|
+
all.push(`${
|
|
94
|
+
index === 0
|
|
95
|
+
? `${k}: ${l}`
|
|
96
|
+
: `${l}`
|
|
97
|
+
}`);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return all;
|
|
101
|
+
}, [])
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function expandType (encoded: string): string[] {
|
|
106
|
+
if (!encoded.startsWith('{')) {
|
|
107
|
+
return [`'${encoded}'`];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return expandObject(JSON.parse(encoded) as ParsedDef);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function expandDefToString ({ lookupNameRoot, type }: TypeDef, indent: number): string {
|
|
114
|
+
if (lookupNameRoot) {
|
|
115
|
+
return `'${lookupNameRoot}'`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const lines = expandType(type);
|
|
119
|
+
let inc = 0;
|
|
120
|
+
|
|
121
|
+
return lines.map((l, index) => {
|
|
122
|
+
let r: string;
|
|
123
|
+
|
|
124
|
+
if (l.endsWith('{')) {
|
|
125
|
+
r = index === 0
|
|
126
|
+
? l
|
|
127
|
+
: `${' '.padStart(indent + inc)}${l}`;
|
|
128
|
+
inc += 2;
|
|
129
|
+
} else {
|
|
130
|
+
if (l.endsWith('},') || l.endsWith('}')) {
|
|
131
|
+
inc -= 2;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
r = index === 0
|
|
135
|
+
? l
|
|
136
|
+
: `${' '.padStart(indent + inc)}${l}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return r;
|
|
140
|
+
}).join('\n');
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function getFilteredTypes (lookup: PortableRegistry, exclude: string[] = []): [PortableType, TypeDef][] {
|
|
144
|
+
const named = lookup.types.filter(({ id }) => !!lookup.getTypeDef(id).lookupName);
|
|
145
|
+
const names = named.map(({ id }) => lookup.getName(id));
|
|
146
|
+
|
|
147
|
+
return named
|
|
148
|
+
.filter((_, index) =>
|
|
149
|
+
!names.some((n, iindex) =>
|
|
150
|
+
index > iindex &&
|
|
151
|
+
n === names[index]
|
|
152
|
+
)
|
|
153
|
+
)
|
|
154
|
+
.map((p): [PortableType, TypeDef] => [p, lookup.getTypeDef(p.id)])
|
|
155
|
+
.filter(([, typeDef]) => !exclude.includes(typeDef.lookupName || '<invalid>'));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function generateLookupDefs (registry: Registry, filtered: [PortableType, TypeDef][], destDir: string, subPath?: string): void {
|
|
159
|
+
writeFile(path.join(destDir, `${subPath || 'definitions'}.ts`), (): string => {
|
|
160
|
+
const all = filtered.map(([{ id, type: { params, path } }, typeDef]) => {
|
|
161
|
+
const typeLookup = registry.createLookupType(id);
|
|
162
|
+
const def = expandDefToString(typeDef, subPath ? 2 : 4);
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
docs: [
|
|
166
|
+
generateTypeDocs(registry, id, path, params),
|
|
167
|
+
WITH_TYPEDEF
|
|
168
|
+
? `@typeDef ${stringify(typeDef)}`
|
|
169
|
+
: null
|
|
170
|
+
].filter((d): d is string => !!d),
|
|
171
|
+
type: { def, typeLookup, typeName: typeDef.lookupName }
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
const max = all.length - 1;
|
|
175
|
+
|
|
176
|
+
return (subPath ? generateLookupDefsNamedTmpl : generateLookupDefsTmpl)({
|
|
177
|
+
defs: all.map(({ docs, type }, i) => {
|
|
178
|
+
const { def, typeLookup, typeName } = type;
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
defs: [
|
|
182
|
+
[typeName || typeLookup, `${def}${i !== max ? ',' : ''}`]
|
|
183
|
+
].map(([n, t]) => `${n}: ${t}`),
|
|
184
|
+
docs
|
|
185
|
+
};
|
|
186
|
+
}),
|
|
187
|
+
headerType: 'defs'
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function generateLookupTypes (registry: Registry, filtered: [PortableType, TypeDef][], destDir: string, subPath?: string): void {
|
|
193
|
+
const imports = {
|
|
194
|
+
...createImports(
|
|
195
|
+
{ '@pezkuwi/types/interfaces': defaultDefinitions },
|
|
196
|
+
{ types: {} }
|
|
197
|
+
),
|
|
198
|
+
interfaces: []
|
|
199
|
+
};
|
|
200
|
+
const items = filtered
|
|
201
|
+
.map(([, typeDef]) => {
|
|
202
|
+
typeDef.name = typeDef.lookupName;
|
|
203
|
+
|
|
204
|
+
return typeDef.lookupNameRoot && typeDef.lookupName
|
|
205
|
+
? exportInterface(typeDef.lookupIndex, typeDef.lookupName, typeDef.lookupNameRoot)
|
|
206
|
+
: typeEncoders[typeDef.info](registry, imports.definitions, typeDef, imports);
|
|
207
|
+
})
|
|
208
|
+
.filter((t): t is string => !!t)
|
|
209
|
+
.map((t) => t.replace(/\nexport /, '\n'));
|
|
210
|
+
|
|
211
|
+
writeFile(path.join(destDir, `types${subPath ? `-${subPath}` : ''}.ts`), () => generateLookupTypesTmpl({
|
|
212
|
+
headerType: 'defs',
|
|
213
|
+
imports,
|
|
214
|
+
items: items.map((l) =>
|
|
215
|
+
l
|
|
216
|
+
.split('\n')
|
|
217
|
+
.map((l) => l.length ? ` ${l}` : '')
|
|
218
|
+
.join('\n')
|
|
219
|
+
),
|
|
220
|
+
types: [
|
|
221
|
+
...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({
|
|
222
|
+
file: packagePath,
|
|
223
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
224
|
+
}))
|
|
225
|
+
]
|
|
226
|
+
}), true);
|
|
227
|
+
writeFile(path.join(destDir, 'index.ts'), () => generateLookupIndexTmpl({ headerType: 'defs' }), true);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function generateRegistry (_registry: Registry, filtered: [PortableType, TypeDef][], destDir: string, subPath: string): void {
|
|
231
|
+
writeFile(path.join(destDir, `${subPath}.ts`), (): string => {
|
|
232
|
+
const items = filtered
|
|
233
|
+
.map(([, { lookupName }]) => lookupName)
|
|
234
|
+
.filter((n): n is string => !!n)
|
|
235
|
+
.sort()
|
|
236
|
+
.reduce((all: string[], n) => all.includes(n) ? all : all.concat(n), []);
|
|
237
|
+
const imports = createImports({}, { types: {} });
|
|
238
|
+
|
|
239
|
+
imports.lookupTypes = items.reduce((all, n) => ({ ...all, [n]: true }), {});
|
|
240
|
+
|
|
241
|
+
return generateRegistryTmpl({
|
|
242
|
+
headerType: 'defs',
|
|
243
|
+
imports,
|
|
244
|
+
items,
|
|
245
|
+
types: []
|
|
246
|
+
});
|
|
247
|
+
}, true);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function generateLookup (destDir: string, entries: [string, HexString][]): void {
|
|
251
|
+
entries.reduce<string[]>((exclude, [subPath, staticMeta]): string[] => {
|
|
252
|
+
const { lookup, registry } = initMeta(staticMeta).metadata.asLatest;
|
|
253
|
+
const filtered = getFilteredTypes(lookup, exclude);
|
|
254
|
+
|
|
255
|
+
generateLookupDefs(registry, filtered, destDir, subPath);
|
|
256
|
+
generateLookupTypes(registry, filtered, destDir, subPath);
|
|
257
|
+
generateRegistry(registry, filtered, destDir, subPath === 'lookup' ? 'registry' : `../registry/${subPath}`);
|
|
258
|
+
|
|
259
|
+
return exclude.concat(
|
|
260
|
+
...filtered
|
|
261
|
+
.map(([, typeDef]) => typeDef.lookupName)
|
|
262
|
+
.filter((n): n is string => !!n)
|
|
263
|
+
);
|
|
264
|
+
}, []);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Generate `packages/types/src/lookup/*s`, the registry of all lookup types
|
|
268
|
+
export function generateDefaultLookup (destDir = 'packages/types-augment/src/lookup', staticData?: HexString): void {
|
|
269
|
+
generateLookup(
|
|
270
|
+
destDir,
|
|
271
|
+
staticData
|
|
272
|
+
? [['lookup', staticData]]
|
|
273
|
+
: [
|
|
274
|
+
['substrate', staticSubstrate],
|
|
275
|
+
['polkadot', staticPolkadot],
|
|
276
|
+
['kusama', staticKusama],
|
|
277
|
+
['assetHubPolkadot', staticAhPolkadot],
|
|
278
|
+
['assetHubKusama', staticAhKusama]
|
|
279
|
+
]
|
|
280
|
+
);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Based on a list of types, it filters out the lookup types that are not needed.
|
|
284
|
+
export function ignoreUnusedLookups (usedTypes: string[], imports: TypeImports) {
|
|
285
|
+
const usedStringified = usedTypes.toString();
|
|
286
|
+
|
|
287
|
+
const [lookupKey, typeDefinitions] = Object.entries(imports.localTypes).find(([typeModule, _]) => typeModule.includes('/lookup')) || ['', {}];
|
|
288
|
+
|
|
289
|
+
Object.keys(typeDefinitions).forEach((typeDef) => {
|
|
290
|
+
if (!(usedStringified.includes(typeDef))) {
|
|
291
|
+
delete (imports.localTypes[lookupKey])[typeDef];
|
|
292
|
+
}
|
|
293
|
+
});
|
|
294
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { StorageEntryMetadataLatest } from '@pezkuwi/types/interfaces';
|
|
5
|
+
import type { Metadata, PortableRegistry } from '@pezkuwi/types/metadata';
|
|
6
|
+
import type { Definitions, Registry } from '@pezkuwi/types/types';
|
|
7
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
8
|
+
import type { ModuleTypes, TypeImports } from '../util/imports.js';
|
|
9
|
+
|
|
10
|
+
import Handlebars from 'handlebars';
|
|
11
|
+
|
|
12
|
+
import * as defaultDefs from '@pezkuwi/types/interfaces/definitions';
|
|
13
|
+
import { unwrapStorageSi } from '@pezkuwi/types/util';
|
|
14
|
+
import lookupDefinitions from '@pezkuwi/types-augment/lookup/definitions';
|
|
15
|
+
import { stringCamelCase } from '@pezkuwi/util';
|
|
16
|
+
|
|
17
|
+
import { compareName, createImports, formatType, getSimilarTypes, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
18
|
+
import { ignoreUnusedLookups } from './lookup.js';
|
|
19
|
+
import { type ExtraTypes, getDeprecationNotice } from './types.js';
|
|
20
|
+
|
|
21
|
+
const generateForMetaTemplate = Handlebars.compile(readTemplate('query'));
|
|
22
|
+
|
|
23
|
+
// From a storage entry metadata, we return [args, returnType]
|
|
24
|
+
/** @internal */
|
|
25
|
+
function entrySignature (lookup: PortableRegistry, allDefs: Record<string, ModuleTypes>, registry: Registry, section: string, storageEntry: StorageEntryMetadataLatest, imports: TypeImports): [boolean, string, string, string] {
|
|
26
|
+
try {
|
|
27
|
+
const outputType = lookup.getTypeDef(unwrapStorageSi(storageEntry.type));
|
|
28
|
+
|
|
29
|
+
if (storageEntry.type.isPlain) {
|
|
30
|
+
const typeDef = lookup.getTypeDef(storageEntry.type.asPlain);
|
|
31
|
+
|
|
32
|
+
setImports(allDefs, imports, [
|
|
33
|
+
typeDef.lookupName || typeDef.type,
|
|
34
|
+
storageEntry.modifier.isOptional
|
|
35
|
+
? 'Option'
|
|
36
|
+
: null
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
return [storageEntry.modifier.isOptional, '', '', formatType(registry, allDefs, outputType, imports)];
|
|
40
|
+
} else if (storageEntry.type.isMap) {
|
|
41
|
+
const { hashers, key, value } = storageEntry.type.asMap;
|
|
42
|
+
const keyDefs = hashers.length === 1
|
|
43
|
+
? [lookup.getTypeDef(key)]
|
|
44
|
+
: lookup.getSiType(key).def.asTuple.map((k) => lookup.getTypeDef(k));
|
|
45
|
+
const similarTypes = keyDefs.map((k) => getSimilarTypes(registry, allDefs, k.lookupName || k.type, imports));
|
|
46
|
+
const keyTypes = similarTypes.map((t) => t.join(' | '));
|
|
47
|
+
const defValue = lookup.getTypeDef(value);
|
|
48
|
+
|
|
49
|
+
setImports(allDefs, imports, [
|
|
50
|
+
...similarTypes.reduce<string[]>((all, t) => all.concat(t), []),
|
|
51
|
+
storageEntry.modifier.isOptional
|
|
52
|
+
? 'Option'
|
|
53
|
+
: null,
|
|
54
|
+
defValue.lookupName || defValue.type
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
return [
|
|
58
|
+
storageEntry.modifier.isOptional,
|
|
59
|
+
keyDefs.map((k) => formatType(registry, allDefs, k.lookupName || k.type, imports)).join(', '),
|
|
60
|
+
keyTypes.map((t, i) => `arg${keyTypes.length === 1 ? '' : (i + 1)}: ${t}`).join(', '),
|
|
61
|
+
outputType.lookupName || formatType(registry, allDefs, outputType, imports)
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
throw new Error(`Expected Plain or Map type, found ${storageEntry.type.type}`);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
throw new Error(`entrySignature: Cannot create signature for query ${section}.${storageEntry.name.toString()}:: ${(error as Error).message}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** @internal */
|
|
72
|
+
function generateForMeta (registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void {
|
|
73
|
+
writeFile(dest, (): string => {
|
|
74
|
+
const allTypes: ExtraTypes = {
|
|
75
|
+
'@pezkuwi/types-augment': {
|
|
76
|
+
lookup: {
|
|
77
|
+
...lookupDefinitions,
|
|
78
|
+
...customLookupDefinitions
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
'@pezkuwi/types/interfaces': defaultDefs,
|
|
82
|
+
...extraTypes
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const imports = createImports(allTypes);
|
|
86
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
87
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
88
|
+
}, {});
|
|
89
|
+
const { lookup, pallets } = meta.asLatest;
|
|
90
|
+
const usedTypes = new Set<string>([]);
|
|
91
|
+
const modules = pallets
|
|
92
|
+
.filter(({ storage }) => storage.isSome)
|
|
93
|
+
.map(({ name, storage }) => {
|
|
94
|
+
const items = storage.unwrap().items
|
|
95
|
+
.map((storageEntry) => {
|
|
96
|
+
const { deprecationInfo, docs, name } = storageEntry;
|
|
97
|
+
const [isOptional, args, params, _returnType] = entrySignature(lookup, allDefs, registry, name.toString(), storageEntry, imports);
|
|
98
|
+
|
|
99
|
+
if (!deprecationInfo.isNotDeprecated) {
|
|
100
|
+
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name));
|
|
101
|
+
const items = docs.length
|
|
102
|
+
? ['', deprecationNotice]
|
|
103
|
+
: [deprecationNotice];
|
|
104
|
+
|
|
105
|
+
docs.push(...items.map((text) => registry.createType('Text', text)));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Add the type and args to the list of used types
|
|
109
|
+
if (!(imports.primitiveTypes[_returnType])) {
|
|
110
|
+
usedTypes.add(_returnType);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (!(imports.primitiveTypes[args])) {
|
|
114
|
+
usedTypes.add(args);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const returnType = isOptional
|
|
118
|
+
? `Option<${_returnType}>`
|
|
119
|
+
: _returnType;
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
args,
|
|
123
|
+
docs,
|
|
124
|
+
entryType: 'AugmentedQuery',
|
|
125
|
+
name: stringCamelCase(storageEntry.name),
|
|
126
|
+
params,
|
|
127
|
+
returnType
|
|
128
|
+
};
|
|
129
|
+
})
|
|
130
|
+
.sort(compareName);
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
items,
|
|
134
|
+
name: stringCamelCase(name)
|
|
135
|
+
};
|
|
136
|
+
})
|
|
137
|
+
.sort(compareName);
|
|
138
|
+
|
|
139
|
+
imports.typesTypes['Observable'] = true;
|
|
140
|
+
|
|
141
|
+
// filter out the unused lookup types from imports
|
|
142
|
+
ignoreUnusedLookups([...usedTypes], imports);
|
|
143
|
+
|
|
144
|
+
return generateForMetaTemplate({
|
|
145
|
+
headerType: 'chain',
|
|
146
|
+
imports,
|
|
147
|
+
isStrict,
|
|
148
|
+
modules,
|
|
149
|
+
types: [
|
|
150
|
+
...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({
|
|
151
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
152
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
153
|
+
})),
|
|
154
|
+
{
|
|
155
|
+
file: '@pezkuwi/api-base/types',
|
|
156
|
+
types: ['ApiTypes', 'AugmentedQuery', 'QueryableStorageEntry']
|
|
157
|
+
}
|
|
158
|
+
]
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Call `generateForMeta()` with current static metadata
|
|
164
|
+
/** @internal */
|
|
165
|
+
export function generateDefaultQuery (dest: string, data: HexString, extraTypes: ExtraTypes = {}, isStrict = false, customLookupDefinitions?: Definitions): void {
|
|
166
|
+
const { metadata, registry } = initMeta(data, extraTypes);
|
|
167
|
+
|
|
168
|
+
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
|
169
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
import type { Definitions } from '@pezkuwi/types/types';
|
|
6
|
+
import type { ExtraTypes } from './types.js';
|
|
7
|
+
|
|
8
|
+
import Handlebars from 'handlebars';
|
|
9
|
+
|
|
10
|
+
import * as defaultDefinitions from '@pezkuwi/types/interfaces/definitions';
|
|
11
|
+
import staticSubstrate from '@pezkuwi/types-support/metadata/static-substrate';
|
|
12
|
+
|
|
13
|
+
import { createImports, formatType, getSimilarTypes, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
14
|
+
|
|
15
|
+
interface ItemDef {
|
|
16
|
+
args: string;
|
|
17
|
+
docs: string[];
|
|
18
|
+
generic: string | undefined;
|
|
19
|
+
name: string;
|
|
20
|
+
type: string | undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface ModuleDef {
|
|
24
|
+
items: ItemDef[];
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const StorageKeyType = 'StorageKey | string | Uint8Array | any';
|
|
29
|
+
|
|
30
|
+
const generateRpcTypesTemplate = Handlebars.compile(readTemplate('rpc'));
|
|
31
|
+
|
|
32
|
+
/** @internal */
|
|
33
|
+
export function generateRpcTypes (registry: TypeRegistry, importDefinitions: Record<string, Definitions>, dest: string, extraTypes: ExtraTypes): void {
|
|
34
|
+
writeFile(dest, (): string => {
|
|
35
|
+
const allTypes: ExtraTypes = { '@pezkuwi/types/interfaces': importDefinitions, ...extraTypes };
|
|
36
|
+
const imports = createImports(allTypes);
|
|
37
|
+
const definitions = imports.definitions as Record<string, Definitions>;
|
|
38
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
39
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
40
|
+
}, {});
|
|
41
|
+
|
|
42
|
+
const rpcKeys = Object
|
|
43
|
+
.keys(definitions)
|
|
44
|
+
.filter((key) => Object.keys(definitions[key].rpc || {}).length !== 0)
|
|
45
|
+
.sort();
|
|
46
|
+
|
|
47
|
+
const additional: Record<string, ModuleDef> = {};
|
|
48
|
+
const modules = rpcKeys.map((sectionFullName) => {
|
|
49
|
+
const rpc = definitions[sectionFullName].rpc || {};
|
|
50
|
+
const section = sectionFullName.split('/').pop();
|
|
51
|
+
|
|
52
|
+
const allMethods = Object.keys(rpc).sort().map((methodName) => {
|
|
53
|
+
const def = rpc[methodName];
|
|
54
|
+
|
|
55
|
+
let args;
|
|
56
|
+
let type;
|
|
57
|
+
let generic;
|
|
58
|
+
|
|
59
|
+
// These are too hard to type with generics, do manual overrides
|
|
60
|
+
if (section === 'state') {
|
|
61
|
+
setImports(allDefs, imports, ['Codec', 'Hash', 'StorageKey', 'Vec']);
|
|
62
|
+
|
|
63
|
+
if (methodName === 'getStorage') {
|
|
64
|
+
generic = 'T = Codec';
|
|
65
|
+
args = [`key: ${StorageKeyType}, block?: Hash | Uint8Array | string`];
|
|
66
|
+
type = 'T';
|
|
67
|
+
} else if (methodName === 'queryStorage') {
|
|
68
|
+
generic = 'T = Codec[]';
|
|
69
|
+
args = [`keys: Vec<StorageKey> | (${StorageKeyType})[], fromBlock?: Hash | Uint8Array | string, toBlock?: Hash | Uint8Array | string`];
|
|
70
|
+
type = '[Hash, T][]';
|
|
71
|
+
} else if (methodName === 'queryStorageAt') {
|
|
72
|
+
generic = 'T = Codec[]';
|
|
73
|
+
args = [`keys: Vec<StorageKey> | (${StorageKeyType})[], at?: Hash | Uint8Array | string`];
|
|
74
|
+
type = 'T';
|
|
75
|
+
} else if (methodName === 'subscribeStorage') {
|
|
76
|
+
generic = 'T = Codec[]';
|
|
77
|
+
args = [`keys?: Vec<StorageKey> | (${StorageKeyType})[]`];
|
|
78
|
+
type = 'T';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (args === undefined) {
|
|
83
|
+
setImports(allDefs, imports, [def.type]);
|
|
84
|
+
|
|
85
|
+
args = def.params.map((param) => {
|
|
86
|
+
const similarTypes = getSimilarTypes(registry, definitions, param.type, imports);
|
|
87
|
+
|
|
88
|
+
setImports(allDefs, imports, [param.type, ...similarTypes]);
|
|
89
|
+
|
|
90
|
+
return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
type = formatType(registry, allDefs, def.type, imports);
|
|
94
|
+
generic = '';
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const item = {
|
|
98
|
+
args: args.join(', '),
|
|
99
|
+
docs: def.deprecated
|
|
100
|
+
? [`@deprecated ${def.deprecated}`, def.description]
|
|
101
|
+
: [def.description],
|
|
102
|
+
generic,
|
|
103
|
+
name: methodName,
|
|
104
|
+
type
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
if (def.aliasSection) {
|
|
108
|
+
if (!additional[def.aliasSection]) {
|
|
109
|
+
additional[def.aliasSection] = {
|
|
110
|
+
items: [],
|
|
111
|
+
name: def.aliasSection
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
additional[def.aliasSection].items.push(item);
|
|
116
|
+
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return item;
|
|
121
|
+
}).filter((item): item is ItemDef => !!item);
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
items: allMethods,
|
|
125
|
+
name: section || 'unknown'
|
|
126
|
+
};
|
|
127
|
+
}).concat(...Object.values(additional)).sort((a, b) => a.name.localeCompare(b.name));
|
|
128
|
+
|
|
129
|
+
imports.typesTypes['Observable'] = true;
|
|
130
|
+
|
|
131
|
+
return generateRpcTypesTemplate({
|
|
132
|
+
headerType: 'chain',
|
|
133
|
+
imports,
|
|
134
|
+
modules,
|
|
135
|
+
types: [
|
|
136
|
+
...Object.keys(imports.localTypes).sort().map((packagePath): { file: string; types: string[] } => ({
|
|
137
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
138
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
139
|
+
})),
|
|
140
|
+
{
|
|
141
|
+
file: '@pezkuwi/rpc-core/types',
|
|
142
|
+
types: ['AugmentedRpc']
|
|
143
|
+
}
|
|
144
|
+
]
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function generateDefaultRpc (dest = 'packages/rpc-augment/src/augment/jsonrpc.ts', extraTypes: ExtraTypes = {}): void {
|
|
150
|
+
const { registry } = initMeta(staticSubstrate, extraTypes);
|
|
151
|
+
|
|
152
|
+
generateRpcTypes(
|
|
153
|
+
registry,
|
|
154
|
+
defaultDefinitions,
|
|
155
|
+
dest,
|
|
156
|
+
extraTypes
|
|
157
|
+
);
|
|
158
|
+
}
|