@pezkuwi/typegen 16.5.5 → 16.5.6
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/LICENSE +201 -0
- package/augment.js +2 -0
- package/bundle.js +2 -0
- package/cjs/augment.d.ts +2 -0
- package/cjs/augment.js +4 -0
- package/cjs/bundle.d.ts +2 -0
- package/cjs/bundle.js +7 -0
- package/cjs/extractChain.js +44 -0
- package/cjs/fromChain.js +95 -0
- package/cjs/fromDefs.js +82 -0
- package/cjs/generate/consts.js +91 -0
- package/cjs/generate/errors.js +59 -0
- package/cjs/generate/events.js +139 -0
- package/cjs/generate/index.js +23 -0
- package/cjs/generate/interfaceRegistry.js +64 -0
- package/cjs/generate/lookup.js +240 -0
- package/cjs/generate/query.js +140 -0
- package/cjs/generate/rpc.js +115 -0
- package/cjs/generate/runtime.js +233 -0
- package/{build → cjs}/generate/tsDef.d.ts +1 -1
- package/cjs/generate/tsDef.js +254 -0
- package/cjs/generate/tx.js +121 -0
- package/cjs/generate/types.js +16 -0
- package/cjs/index.js +5 -0
- package/cjs/interfacesTs.js +28 -0
- package/cjs/metadataMd.js +652 -0
- package/cjs/package.json +3 -0
- package/cjs/packageDetect.js +8 -0
- package/cjs/packageInfo.js +4 -0
- package/cjs/util/assert.js +15 -0
- package/cjs/util/derived.js +128 -0
- package/cjs/util/docs.js +10 -0
- package/cjs/util/file.js +33 -0
- package/{build → cjs}/util/formatting.d.ts +1 -1
- package/cjs/util/formatting.js +237 -0
- package/cjs/util/imports.js +126 -0
- package/{build → cjs}/util/index.d.ts +1 -0
- package/cjs/util/index.js +39 -0
- package/cjs/util/initMeta.js +22 -0
- package/cjs/util/register.js +10 -0
- package/cjs/util/wsMeta.js +60 -0
- package/extractChain.d.ts +1 -0
- package/extractChain.js +40 -0
- package/fromChain.d.ts +1 -0
- package/fromChain.js +91 -0
- package/fromDefs.d.ts +1 -0
- package/fromDefs.js +78 -0
- package/generate/consts.d.ts +5 -0
- package/generate/consts.js +87 -0
- package/generate/errors.d.ts +4 -0
- package/generate/errors.js +55 -0
- package/generate/events.d.ts +5 -0
- package/generate/events.js +135 -0
- package/{src/generate/index.ts → generate/index.d.ts} +0 -3
- package/generate/index.js +10 -0
- package/generate/interfaceRegistry.d.ts +4 -0
- package/generate/interfaceRegistry.js +59 -0
- package/generate/lookup.d.ts +4 -0
- package/generate/lookup.js +235 -0
- package/generate/query.d.ts +5 -0
- package/generate/query.js +136 -0
- package/generate/rpc.d.ts +6 -0
- package/generate/rpc.js +110 -0
- package/generate/runtime.d.ts +7 -0
- package/generate/runtime.js +228 -0
- package/generate/tsDef.d.ts +16 -0
- package/generate/tsDef.js +246 -0
- package/generate/tx.d.ts +5 -0
- package/generate/tx.js +117 -0
- package/generate/types.d.ts +12 -0
- package/generate/types.js +13 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/interfacesTs.d.ts +1 -0
- package/interfacesTs.js +24 -0
- package/metadataMd.d.ts +1 -0
- package/metadataMd.js +648 -0
- package/package.json +520 -24
- package/packageDetect.d.ts +1 -0
- package/{src/packageDetect.ts → packageDetect.js} +0 -8
- package/packageInfo.d.ts +6 -0
- package/packageInfo.js +1 -0
- package/scripts/{polkadot-types-chain-info.mjs → pezkuwi-types-chain-info.mjs} +1 -1
- package/scripts/{polkadot-types-from-chain.mjs → pezkuwi-types-from-chain.mjs} +1 -1
- package/scripts/{polkadot-types-from-defs.mjs → pezkuwi-types-from-defs.mjs} +1 -1
- package/{build/scripts/polkadot-types-internal-interfaces.mjs → scripts/pezkuwi-types-internal-interfaces.mjs} +1 -1
- package/{build/scripts/polkadot-types-internal-metadata.mjs → scripts/pezkuwi-types-internal-metadata.mjs} +1 -1
- package/{src/templates → templates}/calls.hbs +2 -2
- package/{src/templates → templates}/consts.hbs +2 -2
- package/{src/templates → templates}/errors.hbs +2 -2
- package/{src/templates → templates}/events.hbs +2 -2
- package/{src/templates → templates}/interfaceRegistry.hbs +2 -2
- package/{src/templates → templates}/lookup/types.hbs +2 -2
- package/{src/templates → templates}/query.hbs +2 -2
- package/{src/templates → templates}/rpc.hbs +2 -2
- package/{src/templates → templates}/tx.hbs +2 -2
- package/util/assert.d.ts +2 -0
- package/util/assert.js +10 -0
- package/util/derived.d.ts +4 -0
- package/util/derived.js +125 -0
- package/util/docs.d.ts +1 -0
- package/util/docs.js +6 -0
- package/util/file.d.ts +2 -0
- package/util/file.js +28 -0
- package/util/formatting.d.ts +11 -0
- package/util/formatting.js +230 -0
- package/util/imports.d.ts +23 -0
- package/util/imports.js +121 -0
- package/{src/util/index.ts → util/index.d.ts} +6 -8
- package/util/index.js +34 -0
- package/util/initMeta.d.ts +12 -0
- package/util/initMeta.js +19 -0
- package/util/register.d.ts +4 -0
- package/util/register.js +7 -0
- package/util/wsMeta.d.ts +4 -0
- package/util/wsMeta.js +55 -0
- package/build/scripts/polkadot-types-chain-info.mjs +0 -7
- package/build/scripts/polkadot-types-from-chain.mjs +0 -7
- package/build/scripts/polkadot-types-from-defs.mjs +0 -7
- package/scripts/polkadot-types-internal-interfaces.mjs +0 -7
- package/scripts/polkadot-types-internal-metadata.mjs +0 -7
- package/src/augment.ts +0 -5
- package/src/bundle.ts +0 -5
- package/src/extractChain.ts +0 -54
- package/src/fromChain.ts +0 -123
- package/src/fromDefs.ts +0 -106
- package/src/generate/consts.ts +0 -112
- package/src/generate/errors.ts +0 -75
- package/src/generate/events.ts +0 -165
- package/src/generate/interfaceRegistry.ts +0 -85
- package/src/generate/lookup.ts +0 -294
- package/src/generate/query.ts +0 -169
- package/src/generate/rpc.ts +0 -158
- package/src/generate/runtime.ts +0 -284
- package/src/generate/tsDef.ts +0 -321
- package/src/generate/tx.ts +0 -152
- package/src/generate/types.ts +0 -26
- package/src/index.ts +0 -6
- package/src/interfacesTs.ts +0 -35
- package/src/metadataMd.ts +0 -844
- package/src/packageInfo.ts +0 -6
- package/src/util/assert.ts +0 -18
- package/src/util/derived.spec.ts +0 -58
- package/src/util/derived.ts +0 -133
- package/src/util/docs.ts +0 -13
- package/src/util/file.ts +0 -42
- package/src/util/formatting.spec.ts +0 -30
- package/src/util/formatting.ts +0 -295
- package/src/util/imports.ts +0 -164
- package/src/util/initMeta.ts +0 -37
- package/src/util/register.ts +0 -12
- package/src/util/wsMeta.ts +0 -70
- package/tsconfig.build.json +0 -28
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.scripts.json +0 -19
- package/tsconfig.spec.json +0 -17
- /package/{build/augment.d.ts → augment.d.ts} +0 -0
- /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
- /package/{build → cjs}/extractChain.d.ts +0 -0
- /package/{build → cjs}/fromChain.d.ts +0 -0
- /package/{build → cjs}/fromDefs.d.ts +0 -0
- /package/{build → cjs}/generate/consts.d.ts +0 -0
- /package/{build → cjs}/generate/errors.d.ts +0 -0
- /package/{build → cjs}/generate/events.d.ts +0 -0
- /package/{build → cjs}/generate/index.d.ts +0 -0
- /package/{build → cjs}/generate/interfaceRegistry.d.ts +0 -0
- /package/{build → cjs}/generate/lookup.d.ts +0 -0
- /package/{build → cjs}/generate/query.d.ts +0 -0
- /package/{build → cjs}/generate/rpc.d.ts +0 -0
- /package/{build → cjs}/generate/runtime.d.ts +0 -0
- /package/{build → cjs}/generate/tx.d.ts +0 -0
- /package/{build → cjs}/generate/types.d.ts +0 -0
- /package/{build → cjs}/index.d.ts +0 -0
- /package/{build → cjs}/interfacesTs.d.ts +0 -0
- /package/{build → cjs}/metadataMd.d.ts +0 -0
- /package/{build → cjs}/packageDetect.d.ts +0 -0
- /package/{build → cjs}/packageInfo.d.ts +0 -0
- /package/{build → cjs}/util/assert.d.ts +0 -0
- /package/{build → cjs}/util/derived.d.ts +0 -0
- /package/{build → cjs}/util/docs.d.ts +0 -0
- /package/{build → cjs}/util/file.d.ts +0 -0
- /package/{build → cjs}/util/imports.d.ts +0 -0
- /package/{build → cjs}/util/initMeta.d.ts +0 -0
- /package/{build → cjs}/util/register.d.ts +0 -0
- /package/{build → cjs}/util/wsMeta.d.ts +0 -0
- /package/{src/templates → templates}/docs.hbs +0 -0
- /package/{src/templates → templates}/header.hbs +0 -0
- /package/{src/templates → templates}/lookup/defs-named.hbs +0 -0
- /package/{src/templates → templates}/lookup/defs.hbs +0 -0
- /package/{src/templates → templates}/lookup/index.hbs +0 -0
- /package/{src/templates → templates}/tsDef/index.hbs +0 -0
- /package/{src/templates → templates}/tsDef/moduleTypes.hbs +0 -0
- /package/{src/templates → templates}/tsDef/types.hbs +0 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
2
|
+
import { type TypeImports } from '../util/index.js';
|
|
3
|
+
export declare function generateDefaultLookup(destDir?: string, staticData?: HexString): void;
|
|
4
|
+
export declare function ignoreUnusedLookups(usedTypes: string[], imports: TypeImports): void;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import Handlebars from 'handlebars';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import * as defaultDefinitions from '@pezkuwi/types/interfaces/definitions';
|
|
4
|
+
import staticAhDicle from '@pezkuwi/types-support/metadata/v15/asset-hub-dicle-hex';
|
|
5
|
+
import staticAhPezkuwi from '@pezkuwi/types-support/metadata/v15/asset-hub-pezkuwi-hex';
|
|
6
|
+
import staticDicle from '@pezkuwi/types-support/metadata/v15/dicle-hex';
|
|
7
|
+
import staticPezkuwi from '@pezkuwi/types-support/metadata/v15/pezkuwi-hex';
|
|
8
|
+
import staticBizinikiwi from '@pezkuwi/types-support/metadata/v15/bizinikiwi-hex';
|
|
9
|
+
import { isString, stringify } from '@pezkuwi/util';
|
|
10
|
+
import { createImports, exportInterface, initMeta, readTemplate, rebrandTypeName, writeFile } from '../util/index.js';
|
|
11
|
+
import { typeEncoders } from './tsDef.js';
|
|
12
|
+
function deepRebrandTypeDef(typeDef, isTopLevel = true) {
|
|
13
|
+
const rebrandedLookupName = typeDef.lookupName ? rebrandTypeName(typeDef.lookupName) : typeDef.lookupName;
|
|
14
|
+
const rebranded = {
|
|
15
|
+
...typeDef,
|
|
16
|
+
type: rebrandTypeName(typeDef.type),
|
|
17
|
+
// For top-level types: set name from lookupName (mimics original: typeDef.name = typeDef.lookupName)
|
|
18
|
+
// For sub types (enum variants, struct fields): preserve the original name (field/variant name)
|
|
19
|
+
name: isTopLevel ? (rebrandedLookupName || typeDef.name) : typeDef.name,
|
|
20
|
+
lookupName: rebrandedLookupName,
|
|
21
|
+
lookupNameRoot: typeDef.lookupNameRoot ? rebrandTypeName(typeDef.lookupNameRoot) : typeDef.lookupNameRoot
|
|
22
|
+
};
|
|
23
|
+
// Recursively rebrand sub types (mark as not top-level)
|
|
24
|
+
if (typeDef.sub) {
|
|
25
|
+
if (Array.isArray(typeDef.sub)) {
|
|
26
|
+
rebranded.sub = typeDef.sub.map((s) => deepRebrandTypeDef(s, false));
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
rebranded.sub = deepRebrandTypeDef(typeDef.sub, false);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return rebranded;
|
|
33
|
+
}
|
|
34
|
+
const WITH_TYPEDEF = false;
|
|
35
|
+
const generateLookupDefsTmpl = Handlebars.compile(readTemplate('lookup/defs'));
|
|
36
|
+
const generateLookupDefsNamedTmpl = Handlebars.compile(readTemplate('lookup/defs-named'));
|
|
37
|
+
const generateLookupIndexTmpl = Handlebars.compile(readTemplate('lookup/index'));
|
|
38
|
+
const generateLookupTypesTmpl = Handlebars.compile(readTemplate('lookup/types'));
|
|
39
|
+
const generateRegistryTmpl = Handlebars.compile(readTemplate('interfaceRegistry'));
|
|
40
|
+
function generateParamType(registry, { name, type }) {
|
|
41
|
+
if (type.isSome) {
|
|
42
|
+
const link = registry.lookup.types[type.unwrap().toNumber()];
|
|
43
|
+
if (link.type.path.length) {
|
|
44
|
+
return generateTypeDocs(registry, null, link.type.path, link.type.params);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return name.toString();
|
|
48
|
+
}
|
|
49
|
+
function generateTypeDocs(registry, id, path, params) {
|
|
50
|
+
return `${id ? `${registry.createLookupType(id)}${path.length ? ': ' : ''}` : ''}${path.map((p) => p.toString()).join('::')}${params.length ? `<${params.map((p) => generateParamType(registry, p)).join(', ')}>` : ''}`;
|
|
51
|
+
}
|
|
52
|
+
function formatObject(lines) {
|
|
53
|
+
const max = lines.length - 1;
|
|
54
|
+
return [
|
|
55
|
+
'{',
|
|
56
|
+
...lines.map((l, index) => (l.endsWith(',') || l.endsWith('{') || index === max || lines[index + 1].endsWith('}') || lines[index + 1].endsWith('}'))
|
|
57
|
+
? l
|
|
58
|
+
: `${l},`),
|
|
59
|
+
'}'
|
|
60
|
+
];
|
|
61
|
+
}
|
|
62
|
+
function expandSet(parsed) {
|
|
63
|
+
return formatObject(Object.entries(parsed).reduce((all, [k, v]) => {
|
|
64
|
+
all.push(`${k}: ${v}`);
|
|
65
|
+
return all;
|
|
66
|
+
}, []));
|
|
67
|
+
}
|
|
68
|
+
function expandObject(parsed) {
|
|
69
|
+
if (parsed._set) {
|
|
70
|
+
return expandSet(parsed._set);
|
|
71
|
+
}
|
|
72
|
+
return formatObject(Object.entries(parsed).reduce((all, [k, v]) => {
|
|
73
|
+
const inner = isString(v)
|
|
74
|
+
? expandType(v)
|
|
75
|
+
: Array.isArray(v)
|
|
76
|
+
? [`[${v.map((e) => `'${e}'`).join(', ')}]`]
|
|
77
|
+
: expandObject(v);
|
|
78
|
+
inner.forEach((l, index) => {
|
|
79
|
+
all.push(`${index === 0
|
|
80
|
+
? `${k}: ${l}`
|
|
81
|
+
: `${l}`}`);
|
|
82
|
+
});
|
|
83
|
+
return all;
|
|
84
|
+
}, []));
|
|
85
|
+
}
|
|
86
|
+
function expandType(encoded) {
|
|
87
|
+
if (!encoded.startsWith('{')) {
|
|
88
|
+
return [`'${rebrandTypeName(encoded)}'`];
|
|
89
|
+
}
|
|
90
|
+
return expandObject(JSON.parse(encoded));
|
|
91
|
+
}
|
|
92
|
+
function expandDefToString({ lookupNameRoot, type }, indent) {
|
|
93
|
+
if (lookupNameRoot) {
|
|
94
|
+
return `'${rebrandTypeName(lookupNameRoot)}'`;
|
|
95
|
+
}
|
|
96
|
+
const lines = expandType(type);
|
|
97
|
+
let inc = 0;
|
|
98
|
+
return lines.map((l, index) => {
|
|
99
|
+
let r;
|
|
100
|
+
if (l.endsWith('{')) {
|
|
101
|
+
r = index === 0
|
|
102
|
+
? l
|
|
103
|
+
: `${' '.padStart(indent + inc)}${l}`;
|
|
104
|
+
inc += 2;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
if (l.endsWith('},') || l.endsWith('}')) {
|
|
108
|
+
inc -= 2;
|
|
109
|
+
}
|
|
110
|
+
r = index === 0
|
|
111
|
+
? l
|
|
112
|
+
: `${' '.padStart(indent + inc)}${l}`;
|
|
113
|
+
}
|
|
114
|
+
return r;
|
|
115
|
+
}).join('\n');
|
|
116
|
+
}
|
|
117
|
+
function getFilteredTypes(lookup, exclude = []) {
|
|
118
|
+
const named = lookup.types.filter(({ id }) => !!lookup.getTypeDef(id).lookupName);
|
|
119
|
+
const names = named.map(({ id }) => lookup.getName(id));
|
|
120
|
+
return named
|
|
121
|
+
.filter((_, index) => !names.some((n, iindex) => index > iindex &&
|
|
122
|
+
n === names[index]))
|
|
123
|
+
.map((p) => [p, lookup.getTypeDef(p.id)])
|
|
124
|
+
.filter(([, typeDef]) => !exclude.includes(typeDef.lookupName || '<invalid>'));
|
|
125
|
+
}
|
|
126
|
+
function generateLookupDefs(registry, filtered, destDir, subPath) {
|
|
127
|
+
writeFile(path.join(destDir, `${subPath || 'definitions'}.ts`), () => {
|
|
128
|
+
const all = filtered.map(([{ id, type: { params, path } }, typeDef]) => {
|
|
129
|
+
const typeLookup = registry.createLookupType(id);
|
|
130
|
+
const def = expandDefToString(typeDef, subPath ? 2 : 4);
|
|
131
|
+
return {
|
|
132
|
+
docs: [
|
|
133
|
+
generateTypeDocs(registry, id, path, params),
|
|
134
|
+
WITH_TYPEDEF
|
|
135
|
+
? `@typeDef ${stringify(typeDef)}`
|
|
136
|
+
: null
|
|
137
|
+
].filter((d) => !!d),
|
|
138
|
+
type: { def, typeLookup, typeName: typeDef.lookupName ? rebrandTypeName(typeDef.lookupName) : undefined }
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
const max = all.length - 1;
|
|
142
|
+
return (subPath ? generateLookupDefsNamedTmpl : generateLookupDefsTmpl)({
|
|
143
|
+
defs: all.map(({ docs, type }, i) => {
|
|
144
|
+
const { def, typeLookup, typeName } = type;
|
|
145
|
+
return {
|
|
146
|
+
defs: [
|
|
147
|
+
[typeName || typeLookup, `${def}${i !== max ? ',' : ''}`]
|
|
148
|
+
].map(([n, t]) => `${n}: ${t}`),
|
|
149
|
+
docs
|
|
150
|
+
};
|
|
151
|
+
}),
|
|
152
|
+
headerType: 'defs'
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
function generateLookupTypes(registry, filtered, destDir, subPath) {
|
|
157
|
+
const imports = {
|
|
158
|
+
...createImports({ '@pezkuwi/types/interfaces': defaultDefinitions }, { types: {} }),
|
|
159
|
+
interfaces: []
|
|
160
|
+
};
|
|
161
|
+
const items = filtered
|
|
162
|
+
.map(([, typeDef]) => {
|
|
163
|
+
// Deep rebrand the type names (including nested sub types) before generating interfaces
|
|
164
|
+
const rebranded = deepRebrandTypeDef(typeDef);
|
|
165
|
+
return rebranded.lookupNameRoot && rebranded.lookupName
|
|
166
|
+
? exportInterface(rebranded.lookupIndex, rebranded.lookupName, rebranded.lookupNameRoot)
|
|
167
|
+
: typeEncoders[rebranded.info](registry, imports.definitions, rebranded, imports);
|
|
168
|
+
})
|
|
169
|
+
.filter((t) => !!t)
|
|
170
|
+
.map((t) => t.replace(/\nexport /, '\n'));
|
|
171
|
+
writeFile(path.join(destDir, `types${subPath ? `-${subPath}` : ''}.ts`), () => generateLookupTypesTmpl({
|
|
172
|
+
headerType: 'defs',
|
|
173
|
+
imports,
|
|
174
|
+
items: items.map((l) => l
|
|
175
|
+
.split('\n')
|
|
176
|
+
.map((l) => l.length ? ` ${l}` : '')
|
|
177
|
+
.join('\n')),
|
|
178
|
+
types: [
|
|
179
|
+
...Object.keys(imports.localTypes).sort().map((packagePath) => ({
|
|
180
|
+
file: packagePath,
|
|
181
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
182
|
+
}))
|
|
183
|
+
]
|
|
184
|
+
}), true);
|
|
185
|
+
writeFile(path.join(destDir, 'index.ts'), () => generateLookupIndexTmpl({ headerType: 'defs' }), true);
|
|
186
|
+
}
|
|
187
|
+
function generateRegistry(_registry, filtered, destDir, subPath) {
|
|
188
|
+
writeFile(path.join(destDir, `${subPath}.ts`), () => {
|
|
189
|
+
const items = filtered
|
|
190
|
+
.map(([, { lookupName }]) => lookupName ? rebrandTypeName(lookupName) : lookupName)
|
|
191
|
+
.filter((n) => !!n)
|
|
192
|
+
.sort()
|
|
193
|
+
.reduce((all, n) => all.includes(n) ? all : all.concat(n), []);
|
|
194
|
+
const imports = createImports({}, { types: {} });
|
|
195
|
+
imports.lookupTypes = items.reduce((all, n) => ({ ...all, [n]: true }), {});
|
|
196
|
+
return generateRegistryTmpl({
|
|
197
|
+
headerType: 'defs',
|
|
198
|
+
imports,
|
|
199
|
+
items,
|
|
200
|
+
types: []
|
|
201
|
+
});
|
|
202
|
+
}, true);
|
|
203
|
+
}
|
|
204
|
+
function generateLookup(destDir, entries) {
|
|
205
|
+
entries.reduce((exclude, [subPath, staticMeta]) => {
|
|
206
|
+
const { lookup, registry } = initMeta(staticMeta).metadata.asLatest;
|
|
207
|
+
const filtered = getFilteredTypes(lookup, exclude);
|
|
208
|
+
generateLookupDefs(registry, filtered, destDir, subPath);
|
|
209
|
+
generateLookupTypes(registry, filtered, destDir, subPath);
|
|
210
|
+
generateRegistry(registry, filtered, destDir, subPath === 'lookup' ? 'registry' : `../registry/${subPath}`);
|
|
211
|
+
return exclude.concat(...filtered
|
|
212
|
+
.map(([, typeDef]) => typeDef.lookupName)
|
|
213
|
+
.filter((n) => !!n));
|
|
214
|
+
}, []);
|
|
215
|
+
}
|
|
216
|
+
export function generateDefaultLookup(destDir = 'packages/types-augment/src/lookup', staticData) {
|
|
217
|
+
generateLookup(destDir, staticData
|
|
218
|
+
? [['lookup', staticData]]
|
|
219
|
+
: [
|
|
220
|
+
['bizinikiwi', staticBizinikiwi],
|
|
221
|
+
['pezkuwi', staticPezkuwi],
|
|
222
|
+
['dicle', staticDicle],
|
|
223
|
+
['assetHubPezkuwi', staticAhPezkuwi],
|
|
224
|
+
['assetHubDicle', staticAhDicle]
|
|
225
|
+
]);
|
|
226
|
+
}
|
|
227
|
+
export function ignoreUnusedLookups(usedTypes, imports) {
|
|
228
|
+
const usedStringified = usedTypes.toString();
|
|
229
|
+
const [lookupKey, typeDefinitions] = Object.entries(imports.localTypes).find(([typeModule, _]) => typeModule.includes('/lookup')) || ['', {}];
|
|
230
|
+
Object.keys(typeDefinitions).forEach((typeDef) => {
|
|
231
|
+
if (!(usedStringified.includes(typeDef))) {
|
|
232
|
+
delete (imports.localTypes[lookupKey])[typeDef];
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Definitions } from '@pezkuwi/types/types';
|
|
2
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
3
|
+
import { type ExtraTypes } from './types.js';
|
|
4
|
+
/** @internal */
|
|
5
|
+
export declare function generateDefaultQuery(dest: string, data: HexString, extraTypes?: ExtraTypes, isStrict?: boolean, customLookupDefinitions?: Definitions): void;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import Handlebars from 'handlebars';
|
|
2
|
+
import * as defaultDefs from '@pezkuwi/types/interfaces/definitions';
|
|
3
|
+
import { unwrapStorageSi } from '@pezkuwi/types/util';
|
|
4
|
+
import lookupDefinitions from '@pezkuwi/types-augment/lookup/definitions';
|
|
5
|
+
import { stringCamelCase } from '@pezkuwi/util';
|
|
6
|
+
import { compareName, createImports, formatType, getSimilarTypes, initMeta, readTemplate, rebrandTypeName, setImports, writeFile } from '../util/index.js';
|
|
7
|
+
import { ignoreUnusedLookups } from './lookup.js';
|
|
8
|
+
import { getDeprecationNotice } from './types.js';
|
|
9
|
+
const generateForMetaTemplate = Handlebars.compile(readTemplate('query'));
|
|
10
|
+
/** @internal */
|
|
11
|
+
function entrySignature(lookup, allDefs, registry, section, storageEntry, imports) {
|
|
12
|
+
try {
|
|
13
|
+
const outputType = lookup.getTypeDef(unwrapStorageSi(storageEntry.type));
|
|
14
|
+
if (storageEntry.type.isPlain) {
|
|
15
|
+
const typeDef = lookup.getTypeDef(storageEntry.type.asPlain);
|
|
16
|
+
setImports(allDefs, imports, [
|
|
17
|
+
rebrandTypeName(typeDef.lookupName || typeDef.type),
|
|
18
|
+
storageEntry.modifier.isOptional
|
|
19
|
+
? 'Option'
|
|
20
|
+
: null
|
|
21
|
+
]);
|
|
22
|
+
return [storageEntry.modifier.isOptional, '', '', formatType(registry, allDefs, outputType, imports)];
|
|
23
|
+
}
|
|
24
|
+
else if (storageEntry.type.isMap) {
|
|
25
|
+
const { hashers, key, value } = storageEntry.type.asMap;
|
|
26
|
+
const keyDefs = hashers.length === 1
|
|
27
|
+
? [lookup.getTypeDef(key)]
|
|
28
|
+
: lookup.getSiType(key).def.asTuple.map((k) => lookup.getTypeDef(k));
|
|
29
|
+
const similarTypes = keyDefs.map((k) => getSimilarTypes(registry, allDefs, k.lookupName || k.type, imports));
|
|
30
|
+
const keyTypes = similarTypes.map((t) => t.join(' | '));
|
|
31
|
+
const defValue = lookup.getTypeDef(value);
|
|
32
|
+
setImports(allDefs, imports, [
|
|
33
|
+
...similarTypes.reduce((all, t) => all.concat(t), []),
|
|
34
|
+
storageEntry.modifier.isOptional
|
|
35
|
+
? 'Option'
|
|
36
|
+
: null,
|
|
37
|
+
rebrandTypeName(defValue.lookupName || defValue.type)
|
|
38
|
+
]);
|
|
39
|
+
return [
|
|
40
|
+
storageEntry.modifier.isOptional,
|
|
41
|
+
keyDefs.map((k) => formatType(registry, allDefs, k.lookupName || k.type, imports)).join(', '),
|
|
42
|
+
keyTypes.map((t, i) => `arg${keyTypes.length === 1 ? '' : (i + 1)}: ${t}`).join(', '),
|
|
43
|
+
rebrandTypeName(outputType.lookupName || '') || formatType(registry, allDefs, outputType, imports)
|
|
44
|
+
];
|
|
45
|
+
}
|
|
46
|
+
throw new Error(`Expected Plain or Map type, found ${storageEntry.type.type}`);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new Error(`entrySignature: Cannot create signature for query ${section}.${storageEntry.name.toString()}:: ${error.message}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** @internal */
|
|
53
|
+
function generateForMeta(registry, meta, dest, extraTypes, isStrict, customLookupDefinitions) {
|
|
54
|
+
writeFile(dest, () => {
|
|
55
|
+
const allTypes = {
|
|
56
|
+
'@pezkuwi/types-augment': {
|
|
57
|
+
lookup: {
|
|
58
|
+
...lookupDefinitions,
|
|
59
|
+
...customLookupDefinitions
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
'@pezkuwi/types/interfaces': defaultDefs,
|
|
63
|
+
...extraTypes
|
|
64
|
+
};
|
|
65
|
+
const imports = createImports(allTypes);
|
|
66
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
67
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
68
|
+
}, {});
|
|
69
|
+
const { lookup, pallets } = meta.asLatest;
|
|
70
|
+
const usedTypes = new Set([]);
|
|
71
|
+
const modules = pallets
|
|
72
|
+
.filter(({ storage }) => storage.isSome)
|
|
73
|
+
.map(({ name, storage }) => {
|
|
74
|
+
const items = storage.unwrap().items
|
|
75
|
+
.map((storageEntry) => {
|
|
76
|
+
const { deprecationInfo, docs, name } = storageEntry;
|
|
77
|
+
const [isOptional, args, params, _returnType] = entrySignature(lookup, allDefs, registry, name.toString(), storageEntry, imports);
|
|
78
|
+
if (!deprecationInfo.isNotDeprecated) {
|
|
79
|
+
const deprecationNotice = getDeprecationNotice(deprecationInfo, stringCamelCase(name));
|
|
80
|
+
const items = docs.length
|
|
81
|
+
? ['', deprecationNotice]
|
|
82
|
+
: [deprecationNotice];
|
|
83
|
+
docs.push(...items.map((text) => registry.createType('Text', text)));
|
|
84
|
+
}
|
|
85
|
+
// Add the type and args to the list of used types
|
|
86
|
+
if (!(imports.primitiveTypes[_returnType])) {
|
|
87
|
+
usedTypes.add(_returnType);
|
|
88
|
+
}
|
|
89
|
+
if (!(imports.primitiveTypes[args])) {
|
|
90
|
+
usedTypes.add(args);
|
|
91
|
+
}
|
|
92
|
+
const returnType = isOptional
|
|
93
|
+
? `Option<${_returnType}>`
|
|
94
|
+
: _returnType;
|
|
95
|
+
return {
|
|
96
|
+
args,
|
|
97
|
+
docs,
|
|
98
|
+
entryType: 'AugmentedQuery',
|
|
99
|
+
name: stringCamelCase(storageEntry.name),
|
|
100
|
+
params,
|
|
101
|
+
returnType
|
|
102
|
+
};
|
|
103
|
+
})
|
|
104
|
+
.sort(compareName);
|
|
105
|
+
return {
|
|
106
|
+
items,
|
|
107
|
+
name: stringCamelCase(name)
|
|
108
|
+
};
|
|
109
|
+
})
|
|
110
|
+
.sort(compareName);
|
|
111
|
+
imports.typesTypes['Observable'] = true;
|
|
112
|
+
// filter out the unused lookup types from imports
|
|
113
|
+
ignoreUnusedLookups([...usedTypes], imports);
|
|
114
|
+
return generateForMetaTemplate({
|
|
115
|
+
headerType: 'chain',
|
|
116
|
+
imports,
|
|
117
|
+
isStrict,
|
|
118
|
+
modules,
|
|
119
|
+
types: [
|
|
120
|
+
...Object.keys(imports.localTypes).sort().map((packagePath) => ({
|
|
121
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
122
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
123
|
+
})),
|
|
124
|
+
{
|
|
125
|
+
file: '@pezkuwi/api-base/types',
|
|
126
|
+
types: ['ApiTypes', 'AugmentedQuery', 'QueryableStorageEntry']
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** @internal */
|
|
133
|
+
export function generateDefaultQuery(dest, data, extraTypes = {}, isStrict = false, customLookupDefinitions) {
|
|
134
|
+
const { metadata, registry } = initMeta(data, extraTypes);
|
|
135
|
+
return generateForMeta(registry, metadata, dest, extraTypes, isStrict, customLookupDefinitions);
|
|
136
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { TypeRegistry } from '@pezkuwi/types/create';
|
|
2
|
+
import type { Definitions } from '@pezkuwi/types/types';
|
|
3
|
+
import type { ExtraTypes } from './types.js';
|
|
4
|
+
/** @internal */
|
|
5
|
+
export declare function generateRpcTypes(registry: TypeRegistry, importDefinitions: Record<string, Definitions>, dest: string, extraTypes: ExtraTypes): void;
|
|
6
|
+
export declare function generateDefaultRpc(dest?: string, extraTypes?: ExtraTypes): void;
|
package/generate/rpc.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import Handlebars from 'handlebars';
|
|
2
|
+
import * as defaultDefinitions from '@pezkuwi/types/interfaces/definitions';
|
|
3
|
+
import staticBizinikiwi from '@pezkuwi/types-support/metadata/static-bizinikiwi';
|
|
4
|
+
import { createImports, formatType, getSimilarTypes, initMeta, readTemplate, setImports, writeFile } from '../util/index.js';
|
|
5
|
+
const StorageKeyType = 'StorageKey | string | Uint8Array | any';
|
|
6
|
+
const generateRpcTypesTemplate = Handlebars.compile(readTemplate('rpc'));
|
|
7
|
+
/** @internal */
|
|
8
|
+
export function generateRpcTypes(registry, importDefinitions, dest, extraTypes) {
|
|
9
|
+
writeFile(dest, () => {
|
|
10
|
+
const allTypes = { '@pezkuwi/types/interfaces': importDefinitions, ...extraTypes };
|
|
11
|
+
const imports = createImports(allTypes);
|
|
12
|
+
const definitions = imports.definitions;
|
|
13
|
+
const allDefs = Object.entries(allTypes).reduce((defs, [path, obj]) => {
|
|
14
|
+
return Object.entries(obj).reduce((defs, [key, value]) => ({ ...defs, [`${path}/${key}`]: value }), defs);
|
|
15
|
+
}, {});
|
|
16
|
+
const rpcKeys = Object
|
|
17
|
+
.keys(definitions)
|
|
18
|
+
.filter((key) => Object.keys(definitions[key].rpc || {}).length !== 0)
|
|
19
|
+
.sort();
|
|
20
|
+
const additional = {};
|
|
21
|
+
const modules = rpcKeys.map((sectionFullName) => {
|
|
22
|
+
const rpc = definitions[sectionFullName].rpc || {};
|
|
23
|
+
const section = sectionFullName.split('/').pop();
|
|
24
|
+
const allMethods = Object.keys(rpc).sort().map((methodName) => {
|
|
25
|
+
const def = rpc[methodName];
|
|
26
|
+
let args;
|
|
27
|
+
let type;
|
|
28
|
+
let generic;
|
|
29
|
+
// These are too hard to type with generics, do manual overrides
|
|
30
|
+
if (section === 'state') {
|
|
31
|
+
setImports(allDefs, imports, ['Codec', 'Hash', 'StorageKey', 'Vec']);
|
|
32
|
+
if (methodName === 'getStorage') {
|
|
33
|
+
generic = 'T = Codec';
|
|
34
|
+
args = [`key: ${StorageKeyType}, block?: Hash | Uint8Array | string`];
|
|
35
|
+
type = 'T';
|
|
36
|
+
}
|
|
37
|
+
else if (methodName === 'queryStorage') {
|
|
38
|
+
generic = 'T = Codec[]';
|
|
39
|
+
args = [`keys: Vec<StorageKey> | (${StorageKeyType})[], fromBlock?: Hash | Uint8Array | string, toBlock?: Hash | Uint8Array | string`];
|
|
40
|
+
type = '[Hash, T][]';
|
|
41
|
+
}
|
|
42
|
+
else if (methodName === 'queryStorageAt') {
|
|
43
|
+
generic = 'T = Codec[]';
|
|
44
|
+
args = [`keys: Vec<StorageKey> | (${StorageKeyType})[], at?: Hash | Uint8Array | string`];
|
|
45
|
+
type = 'T';
|
|
46
|
+
}
|
|
47
|
+
else if (methodName === 'subscribeStorage') {
|
|
48
|
+
generic = 'T = Codec[]';
|
|
49
|
+
args = [`keys?: Vec<StorageKey> | (${StorageKeyType})[]`];
|
|
50
|
+
type = 'T';
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (args === undefined) {
|
|
54
|
+
setImports(allDefs, imports, [def.type]);
|
|
55
|
+
args = def.params.map((param) => {
|
|
56
|
+
const similarTypes = getSimilarTypes(registry, definitions, param.type, imports);
|
|
57
|
+
setImports(allDefs, imports, [param.type, ...similarTypes]);
|
|
58
|
+
return `${param.name}${param.isOptional ? '?' : ''}: ${similarTypes.join(' | ')}`;
|
|
59
|
+
});
|
|
60
|
+
type = formatType(registry, allDefs, def.type, imports);
|
|
61
|
+
generic = '';
|
|
62
|
+
}
|
|
63
|
+
const item = {
|
|
64
|
+
args: args.join(', '),
|
|
65
|
+
docs: def.deprecated
|
|
66
|
+
? [`@deprecated ${def.deprecated}`, def.description]
|
|
67
|
+
: [def.description],
|
|
68
|
+
generic,
|
|
69
|
+
name: methodName,
|
|
70
|
+
type
|
|
71
|
+
};
|
|
72
|
+
if (def.aliasSection) {
|
|
73
|
+
if (!additional[def.aliasSection]) {
|
|
74
|
+
additional[def.aliasSection] = {
|
|
75
|
+
items: [],
|
|
76
|
+
name: def.aliasSection
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
additional[def.aliasSection].items.push(item);
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return item;
|
|
83
|
+
}).filter((item) => !!item);
|
|
84
|
+
return {
|
|
85
|
+
items: allMethods,
|
|
86
|
+
name: section || 'unknown'
|
|
87
|
+
};
|
|
88
|
+
}).concat(...Object.values(additional)).sort((a, b) => a.name.localeCompare(b.name));
|
|
89
|
+
imports.typesTypes['Observable'] = true;
|
|
90
|
+
return generateRpcTypesTemplate({
|
|
91
|
+
headerType: 'chain',
|
|
92
|
+
imports,
|
|
93
|
+
modules,
|
|
94
|
+
types: [
|
|
95
|
+
...Object.keys(imports.localTypes).sort().map((packagePath) => ({
|
|
96
|
+
file: packagePath.replace('@pezkuwi/types-augment', '@pezkuwi/types'),
|
|
97
|
+
types: Object.keys(imports.localTypes[packagePath])
|
|
98
|
+
})),
|
|
99
|
+
{
|
|
100
|
+
file: '@pezkuwi/rpc-core/types',
|
|
101
|
+
types: ['AugmentedRpc']
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
export function generateDefaultRpc(dest = 'packages/rpc-augment/src/augment/jsonrpc.ts', extraTypes = {}) {
|
|
108
|
+
const { registry } = initMeta(staticBizinikiwi, extraTypes);
|
|
109
|
+
generateRpcTypes(registry, defaultDefinitions, dest, extraTypes);
|
|
110
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Metadata } from '@pezkuwi/types/metadata/Metadata';
|
|
2
|
+
import type { Definitions, Registry } from '@pezkuwi/types/types';
|
|
3
|
+
import type { HexString } from '@pezkuwi/util/types';
|
|
4
|
+
import { type ExtraTypes } from './types.js';
|
|
5
|
+
/** @internal */
|
|
6
|
+
export declare function generateCallTypes(registry: Registry, meta: Metadata, dest: string, extraTypes: ExtraTypes, isStrict: boolean, customLookupDefinitions?: Definitions): void;
|
|
7
|
+
export declare function generateDefaultRuntime(dest: string, data: HexString, extraTypes?: ExtraTypes, isStrict?: boolean, customLookupDefinitions?: Definitions): void;
|