@dedot/codegen 0.0.1-next.ac8e621c.3 → 0.0.1-next.b6596712.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/{generator → chaintypes/generator}/ApiGen.d.ts +1 -1
- package/chaintypes/generator/ConstsGen.d.ts +4 -0
- package/{generator → chaintypes/generator}/ConstsGen.js +8 -9
- package/chaintypes/generator/ErrorsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/ErrorsGen.js +9 -10
- package/chaintypes/generator/EventsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/EventsGen.js +9 -10
- package/chaintypes/generator/IndexGen.d.ts +5 -0
- package/chaintypes/generator/IndexGen.js +17 -0
- package/chaintypes/generator/JsonRpcGen.d.ts +8 -0
- package/chaintypes/generator/JsonRpcGen.js +55 -0
- package/chaintypes/generator/QueryGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/QueryGen.js +24 -16
- package/chaintypes/generator/RuntimeApisGen.d.ts +9 -0
- package/{generator → chaintypes/generator}/RuntimeApisGen.js +98 -24
- package/chaintypes/generator/TxGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/TxGen.js +14 -12
- package/chaintypes/generator/TypesGen.d.ts +11 -0
- package/chaintypes/generator/TypesGen.js +55 -0
- package/chaintypes/generator/index.d.ts +10 -0
- package/chaintypes/generator/index.js +10 -0
- package/chaintypes/index.d.ts +3 -0
- package/chaintypes/index.js +49 -0
- package/chaintypes/templates/consts.hbs +7 -0
- package/chaintypes/templates/errors.hbs +7 -0
- package/chaintypes/templates/events.hbs +7 -0
- package/chaintypes/templates/index.hbs +27 -0
- package/chaintypes/templates/json-rpc.hbs +5 -0
- package/chaintypes/templates/query.hbs +7 -0
- package/chaintypes/templates/runtime.hbs +8 -0
- package/chaintypes/templates/tx.hbs +9 -0
- package/{cjs → chaintypes}/templates/types.hbs +1 -1
- package/cjs/{generator → chaintypes/generator}/ConstsGen.js +12 -13
- package/cjs/chaintypes/generator/ErrorsGen.js +40 -0
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +16 -17
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/chaintypes/generator/JsonRpcGen.js +59 -0
- package/cjs/{generator → chaintypes/generator}/QueryGen.js +26 -18
- package/cjs/chaintypes/generator/RuntimeApisGen.js +193 -0
- package/cjs/{generator → chaintypes/generator}/TxGen.js +23 -21
- package/cjs/chaintypes/generator/TypesGen.js +59 -0
- package/cjs/chaintypes/generator/index.js +26 -0
- package/cjs/chaintypes/index.js +77 -0
- package/cjs/chaintypes/templates/consts.hbs +7 -0
- package/cjs/chaintypes/templates/errors.hbs +7 -0
- package/cjs/chaintypes/templates/events.hbs +7 -0
- package/cjs/chaintypes/templates/index.hbs +27 -0
- package/cjs/chaintypes/templates/json-rpc.hbs +5 -0
- package/cjs/chaintypes/templates/query.hbs +7 -0
- package/cjs/chaintypes/templates/runtime.hbs +8 -0
- package/cjs/chaintypes/templates/tx.hbs +9 -0
- package/{templates → cjs/chaintypes/templates}/types.hbs +1 -1
- package/cjs/index.js +4 -62
- package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +104 -160
- package/cjs/{generator → shared}/TypeImports.js +29 -3
- package/cjs/{generator → shared}/index.js +3 -10
- package/cjs/{generator → shared}/known-codecs.js +24 -5
- package/cjs/typink/generator/ConstructorQueryGen.js +24 -0
- package/cjs/typink/generator/ConstructorTxGen.js +24 -0
- package/cjs/typink/generator/IndexGen.js +24 -0
- package/cjs/typink/generator/QueryGen.js +72 -0
- package/cjs/typink/generator/TxGen.js +25 -0
- package/cjs/typink/generator/TypesGen.js +28 -0
- package/cjs/typink/generator/index.js +22 -0
- package/cjs/typink/index.js +37 -0
- package/cjs/typink/templates/constructor-query.hbs +7 -0
- package/cjs/typink/templates/constructor-tx.hbs +7 -0
- package/cjs/typink/templates/index.hbs +14 -0
- package/cjs/typink/templates/query.hbs +7 -0
- package/cjs/typink/templates/tx.hbs +7 -0
- package/cjs/typink/templates/types.hbs +5 -0
- package/cjs/{generator/utils.js → utils.js} +34 -5
- package/index.d.ts +2 -4
- package/index.js +2 -48
- package/package.json +15 -18
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +13 -12
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +90 -146
- package/{generator → shared}/TypeImports.d.ts +12 -1
- package/{generator → shared}/TypeImports.js +29 -3
- package/shared/index.d.ts +3 -0
- package/shared/index.js +3 -0
- package/{generator → shared}/known-codecs.d.ts +1 -1
- package/{generator → shared}/known-codecs.js +22 -3
- package/typink/generator/ConstructorQueryGen.d.ts +6 -0
- package/typink/generator/ConstructorQueryGen.js +20 -0
- package/typink/generator/ConstructorTxGen.d.ts +6 -0
- package/typink/generator/ConstructorTxGen.js +20 -0
- package/typink/generator/IndexGen.d.ts +6 -0
- package/typink/generator/IndexGen.js +20 -0
- package/typink/generator/QueryGen.d.ts +12 -0
- package/typink/generator/QueryGen.js +68 -0
- package/typink/generator/TxGen.d.ts +6 -0
- package/typink/generator/TxGen.js +21 -0
- package/typink/generator/TypesGen.d.ts +7 -0
- package/typink/generator/TypesGen.js +24 -0
- package/typink/generator/index.d.ts +6 -0
- package/typink/generator/index.js +6 -0
- package/typink/index.d.ts +2 -0
- package/typink/index.js +30 -0
- package/typink/templates/constructor-query.hbs +7 -0
- package/typink/templates/constructor-tx.hbs +7 -0
- package/typink/templates/index.hbs +14 -0
- package/typink/templates/query.hbs +7 -0
- package/typink/templates/tx.hbs +7 -0
- package/typink/templates/types.hbs +5 -0
- package/{generator/utils.d.ts → utils.d.ts} +6 -1
- package/{generator/utils.js → utils.js} +32 -4
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/ErrorsGen.js +0 -41
- package/cjs/generator/IndexGen.js +0 -18
- package/cjs/generator/RpcGen.js +0 -176
- package/cjs/generator/RuntimeApisGen.js +0 -119
- package/cjs/generator/known-types.js +0 -33
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/consts.hbs +0 -7
- package/cjs/templates/errors.hbs +0 -7
- package/cjs/templates/events.hbs +0 -7
- package/cjs/templates/index.hbs +0 -22
- package/cjs/templates/query.hbs +0 -7
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/templates/runtime.hbs +0 -8
- package/cjs/templates/tx.hbs +0 -9
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/generator/ConstsGen.d.ts +0 -4
- package/generator/ErrorsGen.d.ts +0 -5
- package/generator/EventsGen.d.ts +0 -5
- package/generator/IndexGen.d.ts +0 -6
- package/generator/IndexGen.js +0 -14
- package/generator/QueryGen.d.ts +0 -5
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -172
- package/generator/RuntimeApisGen.d.ts +0 -9
- package/generator/TxGen.d.ts +0 -5
- package/generator/index.d.ts +0 -10
- package/generator/index.js +0 -10
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/consts.hbs +0 -7
- package/templates/errors.hbs +0 -7
- package/templates/events.hbs +0 -7
- package/templates/index.hbs +0 -22
- package/templates/query.hbs +0 -7
- package/templates/rpc.hbs +0 -7
- package/templates/runtime.hbs +0 -8
- package/templates/tx.hbs +0 -9
- package/types.d.ts +0 -6
- package/types.js +0 -1
- /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/cjs/{generator/dirname.js → dirname.js} +0 -0
- /package/{generator/dirname.d.ts → dirname.d.ts} +0 -0
- /package/{generator/dirname.js → dirname.js} +0 -0
|
@@ -1,67 +1,93 @@
|
|
|
1
|
-
import { stringPascalCase } from '@
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { knownTypes } from './known-types';
|
|
6
|
-
import { TypeImports } from './TypeImports';
|
|
7
|
-
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs';
|
|
8
|
-
// Skip generate types for these
|
|
9
|
-
// as we do have native types for them
|
|
10
|
-
const SKIP_TYPES = [
|
|
11
|
-
'BoundedBTreeMap',
|
|
12
|
-
'BoundedBTreeSet',
|
|
13
|
-
'BoundedVec',
|
|
14
|
-
'Box',
|
|
15
|
-
'BTreeMap',
|
|
16
|
-
'BTreeSet',
|
|
17
|
-
'Cow',
|
|
18
|
-
'Option',
|
|
19
|
-
'Range',
|
|
20
|
-
'RangeInclusive',
|
|
21
|
-
'Result',
|
|
22
|
-
'WeakBoundedVec',
|
|
23
|
-
'WrapperKeepOpaque',
|
|
24
|
-
'WrapperOpaque',
|
|
25
|
-
];
|
|
1
|
+
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { commentBlock, isNativeType, WRAPPER_TYPE_REGEX } from '../utils.js';
|
|
3
|
+
import { TypeImports } from './TypeImports.js';
|
|
4
|
+
import { checkKnownCodecType, findKnownCodec, findKnownCodecType } from './known-codecs.js';
|
|
26
5
|
// These are common & generic types, so we'll remove these from all paths at index 1
|
|
27
6
|
// This helps make the type name shorter
|
|
28
7
|
const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
|
|
29
8
|
export const BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
metadata;
|
|
33
|
-
/**
|
|
34
|
-
* Types will be generated its definition out.
|
|
35
|
-
*/
|
|
9
|
+
export class BaseTypesGen {
|
|
10
|
+
types;
|
|
36
11
|
includedTypes;
|
|
37
|
-
registry;
|
|
38
12
|
typeImports;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.
|
|
42
|
-
this.includedTypes =
|
|
13
|
+
skipTypes;
|
|
14
|
+
constructor(types) {
|
|
15
|
+
this.types = types;
|
|
16
|
+
this.includedTypes = {};
|
|
43
17
|
this.typeImports = new TypeImports();
|
|
18
|
+
this.skipTypes = [];
|
|
44
19
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
let defTypeOut = '';
|
|
48
|
-
Object.values(this.includedTypes)
|
|
49
|
-
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
50
|
-
.forEach(({ name, nameOut, id, docs }) => {
|
|
51
|
-
defTypeOut += `${commentBlock(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
52
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
53
|
-
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
57
|
-
const template = compileTemplate('types.hbs');
|
|
58
|
-
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
20
|
+
shouldGenerateTypeIn(_id) {
|
|
21
|
+
return false;
|
|
59
22
|
}
|
|
60
23
|
typeCache = {};
|
|
61
24
|
clearCache() {
|
|
62
25
|
this.typeCache = {};
|
|
63
26
|
this.typeImports.clear();
|
|
64
27
|
}
|
|
28
|
+
includeTypes() {
|
|
29
|
+
const pathsCount = new Map();
|
|
30
|
+
const typesWithPath = this.types.filter((one) => one.path.length > 0);
|
|
31
|
+
const skipIds = [];
|
|
32
|
+
const typeSuffixes = new Map();
|
|
33
|
+
typesWithPath.forEach(({ path, id }) => {
|
|
34
|
+
const joinedPath = path.join('::');
|
|
35
|
+
if (pathsCount.has(joinedPath)) {
|
|
36
|
+
// We compare 2 types with the same path here,
|
|
37
|
+
// if they are the same type -> skip the current one, keep the first occurrence
|
|
38
|
+
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
39
|
+
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
40
|
+
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
41
|
+
if (sameType) {
|
|
42
|
+
skipIds.push(id);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
pathsCount.get(joinedPath).push(id);
|
|
46
|
+
typeSuffixes.set(id, this.extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
pathsCount.set(joinedPath, [id]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return typesWithPath.reduce((o, type) => {
|
|
54
|
+
const { path, id } = type;
|
|
55
|
+
const joinedPath = path.join('::');
|
|
56
|
+
if (this.skipTypes.includes(joinedPath) || this.skipTypes.includes(path.at(-1))) {
|
|
57
|
+
return o;
|
|
58
|
+
}
|
|
59
|
+
const suffix = typeSuffixes.get(id) || '';
|
|
60
|
+
let knownType = false;
|
|
61
|
+
let name, nameOut;
|
|
62
|
+
const [isKnownCodecType, codecName] = checkKnownCodecType(joinedPath);
|
|
63
|
+
if (isKnownCodecType) {
|
|
64
|
+
const codecType = findKnownCodecType(codecName);
|
|
65
|
+
name = codecType.typeIn;
|
|
66
|
+
nameOut = codecType.typeOut;
|
|
67
|
+
knownType = true;
|
|
68
|
+
}
|
|
69
|
+
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
70
|
+
const newPath = path.slice();
|
|
71
|
+
newPath.splice(1, 1);
|
|
72
|
+
name = this.cleanPath(newPath);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
name = this.cleanPath(path);
|
|
76
|
+
}
|
|
77
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
78
|
+
nameOut = name;
|
|
79
|
+
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
80
|
+
}
|
|
81
|
+
o[id] = {
|
|
82
|
+
name: `${name}${suffix}`,
|
|
83
|
+
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
84
|
+
knownType,
|
|
85
|
+
skip: skipIds.includes(id),
|
|
86
|
+
...type,
|
|
87
|
+
};
|
|
88
|
+
return o;
|
|
89
|
+
}, {});
|
|
90
|
+
}
|
|
65
91
|
generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
66
92
|
if (nestedLevel > 0) {
|
|
67
93
|
const includedDef = this.includedTypes[typeId];
|
|
@@ -92,7 +118,7 @@ export class TypesGen {
|
|
|
92
118
|
return type;
|
|
93
119
|
}
|
|
94
120
|
#generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
95
|
-
const def = this.
|
|
121
|
+
const def = this.types[typeId];
|
|
96
122
|
if (!def) {
|
|
97
123
|
throw new Error(`Type def not found ${JSON.stringify(def)}`);
|
|
98
124
|
}
|
|
@@ -169,7 +195,7 @@ export class TypesGen {
|
|
|
169
195
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
170
196
|
}
|
|
171
197
|
}
|
|
172
|
-
const { tagKey, valueKey } = this.
|
|
198
|
+
const { tagKey, valueKey } = this.getEnumOptions(typeId);
|
|
173
199
|
return membersType
|
|
174
200
|
.map(([keyName, valueType, docs]) => ({
|
|
175
201
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -199,7 +225,7 @@ export class TypesGen {
|
|
|
199
225
|
case 'Sequence':
|
|
200
226
|
case 'SizedVec': {
|
|
201
227
|
const fixedSize = tag === 'SizedVec' ? `${value.len}` : null;
|
|
202
|
-
const $innerType = this.
|
|
228
|
+
const $innerType = this.types[value.typeParam].type;
|
|
203
229
|
if ($innerType.tag === 'Primitive' && $innerType.value.kind === 'u8') {
|
|
204
230
|
return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
|
|
205
231
|
}
|
|
@@ -229,69 +255,6 @@ export class TypesGen {
|
|
|
229
255
|
#isOptionalType(type) {
|
|
230
256
|
return type.endsWith('| undefined');
|
|
231
257
|
}
|
|
232
|
-
#includedTypes() {
|
|
233
|
-
const { types } = this.metadata;
|
|
234
|
-
const pathsCount = new Map();
|
|
235
|
-
const typesWithPath = types.filter((one) => one.path.length > 0);
|
|
236
|
-
const skipIds = [];
|
|
237
|
-
const typeSuffixes = new Map();
|
|
238
|
-
typesWithPath.forEach(({ path, id }) => {
|
|
239
|
-
const joinedPath = path.join('::');
|
|
240
|
-
if (pathsCount.has(joinedPath)) {
|
|
241
|
-
// We compare 2 types with the same path here,
|
|
242
|
-
// if they are the same type -> skip the current one, keep the first occurrence
|
|
243
|
-
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
244
|
-
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
245
|
-
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
246
|
-
if (sameType) {
|
|
247
|
-
skipIds.push(id);
|
|
248
|
-
}
|
|
249
|
-
else {
|
|
250
|
-
pathsCount.get(joinedPath).push(id);
|
|
251
|
-
typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
else {
|
|
255
|
-
pathsCount.set(joinedPath, [id]);
|
|
256
|
-
}
|
|
257
|
-
});
|
|
258
|
-
return typesWithPath.reduce((o, type) => {
|
|
259
|
-
const { path, id } = type;
|
|
260
|
-
const joinedPath = path.join('::');
|
|
261
|
-
if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
|
|
262
|
-
return o;
|
|
263
|
-
}
|
|
264
|
-
const suffix = typeSuffixes.get(id) || '';
|
|
265
|
-
let knownType = false;
|
|
266
|
-
let name, nameOut;
|
|
267
|
-
if (isKnownCodecType(joinedPath)) {
|
|
268
|
-
const codecType = findKnownCodecType(path.at(-1));
|
|
269
|
-
name = codecType.typeIn;
|
|
270
|
-
nameOut = codecType.typeOut;
|
|
271
|
-
knownType = true;
|
|
272
|
-
}
|
|
273
|
-
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
274
|
-
const newPath = path.slice();
|
|
275
|
-
newPath.splice(1, 1);
|
|
276
|
-
name = this.#cleanPath(newPath);
|
|
277
|
-
}
|
|
278
|
-
else {
|
|
279
|
-
name = this.#cleanPath(path);
|
|
280
|
-
}
|
|
281
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
282
|
-
nameOut = name;
|
|
283
|
-
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
284
|
-
}
|
|
285
|
-
o[id] = {
|
|
286
|
-
name: `${name}${suffix}`,
|
|
287
|
-
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
288
|
-
knownType,
|
|
289
|
-
skip: skipIds.includes(id),
|
|
290
|
-
...type,
|
|
291
|
-
};
|
|
292
|
-
return o;
|
|
293
|
-
}, {});
|
|
294
|
-
}
|
|
295
258
|
#removeGenericPart(typeName) {
|
|
296
259
|
if (typeName.match(WRAPPER_TYPE_REGEX)) {
|
|
297
260
|
return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
@@ -300,27 +263,12 @@ export class TypesGen {
|
|
|
300
263
|
return typeName;
|
|
301
264
|
}
|
|
302
265
|
}
|
|
303
|
-
|
|
304
|
-
* @description Remove duplicated part of the path
|
|
305
|
-
*
|
|
306
|
-
* Example:
|
|
307
|
-
* ["pallet_staking", "pallet", "pallet", "Event"]
|
|
308
|
-
* => ["pallet_staking", "pallet", "Event"]
|
|
309
|
-
*
|
|
310
|
-
* @param path
|
|
311
|
-
* @private
|
|
312
|
-
*/
|
|
313
|
-
#cleanPath(path) {
|
|
266
|
+
cleanPath(path) {
|
|
314
267
|
return path
|
|
315
268
|
.map((one) => stringPascalCase(one))
|
|
316
269
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
317
270
|
.join('');
|
|
318
271
|
}
|
|
319
|
-
#shouldGenerateTypeIn(id) {
|
|
320
|
-
const { callTypeId } = this.metadata.extrinsic;
|
|
321
|
-
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
322
|
-
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
323
|
-
}
|
|
324
272
|
eqlCache = new Map();
|
|
325
273
|
typeEql(idA, idB, level = 0) {
|
|
326
274
|
const cacheKey = `${idA}==${idB}`;
|
|
@@ -333,9 +281,8 @@ export class TypesGen {
|
|
|
333
281
|
#typeEql(idA, idB, lvl = 0) {
|
|
334
282
|
if (idA === idB)
|
|
335
283
|
return true;
|
|
336
|
-
const
|
|
337
|
-
const
|
|
338
|
-
const typeB = types[idB];
|
|
284
|
+
const typeA = this.types[idA];
|
|
285
|
+
const typeB = this.types[idB];
|
|
339
286
|
if (typeA.path.join('::') !== typeB.path.join('::'))
|
|
340
287
|
return false;
|
|
341
288
|
const { type: defA, params: paramsA } = typeA;
|
|
@@ -378,14 +325,13 @@ export class TypesGen {
|
|
|
378
325
|
(param1.typeId === undefined) === (param2.typeId === undefined) &&
|
|
379
326
|
(param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
|
|
380
327
|
}
|
|
381
|
-
|
|
382
|
-
const
|
|
383
|
-
const
|
|
384
|
-
const dupTypeParams = types[dupTypeId].params;
|
|
328
|
+
extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
|
|
329
|
+
const originalTypeParams = this.types[originalTypeId].params;
|
|
330
|
+
const dupTypeParams = this.types[dupTypeId].params;
|
|
385
331
|
const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
|
|
386
332
|
// TODO make sure these suffix is unique if a type is duplicated more than 2 times
|
|
387
333
|
if (diffParam?.typeId) {
|
|
388
|
-
const diffType = types[diffParam.typeId];
|
|
334
|
+
const diffType = this.types[diffParam.typeId];
|
|
389
335
|
if (diffType.path.length > 0) {
|
|
390
336
|
return stringPascalCase(diffType.path.at(-1));
|
|
391
337
|
}
|
|
@@ -423,11 +369,9 @@ export class TypesGen {
|
|
|
423
369
|
this.typeImports.addCodecType(typeName);
|
|
424
370
|
return;
|
|
425
371
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
this.typeImports.addOutType(typeName);
|
|
431
|
-
}
|
|
372
|
+
this.typeImports.addOutType(typeName);
|
|
373
|
+
}
|
|
374
|
+
getEnumOptions(_typeId) {
|
|
375
|
+
return { tagKey: 'tag', valueKey: 'value' };
|
|
432
376
|
}
|
|
433
377
|
}
|
|
@@ -1,14 +1,25 @@
|
|
|
1
|
+
type ImportConfig = {
|
|
2
|
+
excludeModules?: string[];
|
|
3
|
+
useSubPaths?: boolean;
|
|
4
|
+
};
|
|
1
5
|
export declare class TypeImports {
|
|
2
6
|
#private;
|
|
3
7
|
portableTypes: Set<string>;
|
|
4
8
|
codecTypes: Set<string>;
|
|
5
9
|
knownTypes: Set<string>;
|
|
10
|
+
knownJsonRpcTypes: Set<string>;
|
|
6
11
|
outTypes: Set<string>;
|
|
12
|
+
contractTypes: Set<string>;
|
|
13
|
+
chainTypes: Set<string>;
|
|
7
14
|
constructor();
|
|
8
15
|
clear(): void;
|
|
9
|
-
toImports(
|
|
16
|
+
toImports(config?: ImportConfig): string;
|
|
10
17
|
addPortableType(...types: string[]): void;
|
|
11
18
|
addCodecType(...types: string[]): void;
|
|
12
19
|
addKnownType(...types: string[]): void;
|
|
20
|
+
addKnownJsonRpcType(...types: string[]): void;
|
|
13
21
|
addOutType(...types: string[]): void;
|
|
22
|
+
addContractType(...types: string[]): void;
|
|
23
|
+
addChainType(...types: string[]): void;
|
|
14
24
|
}
|
|
25
|
+
export {};
|
|
@@ -5,30 +5,47 @@ export class TypeImports {
|
|
|
5
5
|
codecTypes;
|
|
6
6
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
7
7
|
knownTypes;
|
|
8
|
+
knownJsonRpcTypes;
|
|
8
9
|
// External types to define explicitly
|
|
9
10
|
outTypes;
|
|
11
|
+
// Know types defined in @dedot/contracts
|
|
12
|
+
contractTypes;
|
|
13
|
+
// Know types defined in @dedot/api/chaintypes or dedot/chaintypes
|
|
14
|
+
chainTypes;
|
|
10
15
|
constructor() {
|
|
11
16
|
this.portableTypes = new Set();
|
|
12
17
|
this.codecTypes = new Set();
|
|
13
18
|
this.knownTypes = new Set();
|
|
19
|
+
this.knownJsonRpcTypes = new Set();
|
|
14
20
|
this.outTypes = new Set();
|
|
21
|
+
this.contractTypes = new Set();
|
|
22
|
+
this.chainTypes = new Set();
|
|
15
23
|
}
|
|
16
24
|
clear() {
|
|
17
25
|
this.portableTypes.clear();
|
|
18
26
|
this.codecTypes.clear();
|
|
19
27
|
this.knownTypes.clear();
|
|
28
|
+
this.knownJsonRpcTypes.clear();
|
|
20
29
|
this.outTypes.clear();
|
|
30
|
+
this.contractTypes.clear();
|
|
31
|
+
this.chainTypes.clear();
|
|
21
32
|
}
|
|
22
|
-
toImports(
|
|
33
|
+
toImports(config) {
|
|
34
|
+
const { excludeModules = [], useSubPaths = false } = config || {};
|
|
23
35
|
// TODO generate outTypes!
|
|
36
|
+
const prefix = useSubPaths ? '' : '@';
|
|
24
37
|
const toImports = [
|
|
25
|
-
[this.knownTypes,
|
|
26
|
-
[this.
|
|
38
|
+
[this.knownTypes, `${prefix}dedot/types`],
|
|
39
|
+
[this.knownJsonRpcTypes, `${prefix}dedot/types/json-rpc`],
|
|
40
|
+
[this.codecTypes, `${prefix}dedot/codecs`],
|
|
41
|
+
[this.contractTypes, `${prefix}dedot/contracts`],
|
|
42
|
+
[this.chainTypes, prefix ? '@dedot/api/chaintypes' : 'dedot/chaintypes'],
|
|
27
43
|
[this.portableTypes, './types'],
|
|
28
44
|
];
|
|
29
45
|
return toImports
|
|
30
46
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
31
47
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
48
|
+
.filter((line) => line.length > 0)
|
|
32
49
|
.join('\n');
|
|
33
50
|
}
|
|
34
51
|
#toImportLine(types, module) {
|
|
@@ -46,7 +63,16 @@ export class TypeImports {
|
|
|
46
63
|
addKnownType(...types) {
|
|
47
64
|
types.forEach((one) => this.knownTypes.add(one));
|
|
48
65
|
}
|
|
66
|
+
addKnownJsonRpcType(...types) {
|
|
67
|
+
types.forEach((one) => this.knownJsonRpcTypes.add(one));
|
|
68
|
+
}
|
|
49
69
|
addOutType(...types) {
|
|
50
70
|
types.forEach((one) => this.outTypes.add(one));
|
|
51
71
|
}
|
|
72
|
+
addContractType(...types) {
|
|
73
|
+
types.forEach((one) => this.contractTypes.add(one));
|
|
74
|
+
}
|
|
75
|
+
addChainType(...types) {
|
|
76
|
+
types.forEach((one) => this.chainTypes.add(one));
|
|
77
|
+
}
|
|
52
78
|
}
|
package/shared/index.js
ADDED
|
@@ -20,4 +20,4 @@ export declare const looseTypeCodecs: Record<string, AnyShape>;
|
|
|
20
20
|
export declare function findKnownCodecType(name: string): CodecType;
|
|
21
21
|
export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
|
|
22
22
|
export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
|
|
23
|
-
export declare function
|
|
23
|
+
export declare function checkKnownCodecType(path: string | string[]): [boolean, string];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as $ from '@dedot/shape';
|
|
2
1
|
import * as Codecs from '@dedot/codecs';
|
|
3
2
|
import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
|
|
3
|
+
import * as $ from '@dedot/shape';
|
|
4
4
|
import { assert } from '@dedot/utils';
|
|
5
5
|
export const normalizeCodecName = (name) => {
|
|
6
6
|
return name.startsWith('$') ? name : `$${name}`;
|
|
@@ -30,6 +30,8 @@ const KNOWN_PATHS = [
|
|
|
30
30
|
/^primitive_types::\w+$/,
|
|
31
31
|
/^sp_arithmetic::per_things::\w+$/,
|
|
32
32
|
/^sp_arithmetic::fixed_point::\w+$/,
|
|
33
|
+
'ink_primitives::types::Hash',
|
|
34
|
+
{ path: 'ink_primitives::types::AccountId', codec: 'AccountId32' },
|
|
33
35
|
];
|
|
34
36
|
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
35
37
|
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
@@ -114,7 +116,24 @@ export function findKnownWrapperCodec(typeName) {
|
|
|
114
116
|
return $.Tuple(...$inner);
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
|
-
export function
|
|
119
|
+
export function checkKnownCodecType(path) {
|
|
118
120
|
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
119
|
-
|
|
121
|
+
const knownPath = KNOWN_PATHS.find((one) => {
|
|
122
|
+
if (typeof one === 'string') {
|
|
123
|
+
return one === joinedPath;
|
|
124
|
+
}
|
|
125
|
+
else if (one instanceof RegExp) {
|
|
126
|
+
return one.test(joinedPath);
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return one.path === joinedPath;
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
if (!knownPath) {
|
|
133
|
+
return [false, ''];
|
|
134
|
+
}
|
|
135
|
+
if (typeof knownPath === 'string' || knownPath instanceof RegExp) {
|
|
136
|
+
return [true, joinedPath.split('::').at(-1)];
|
|
137
|
+
}
|
|
138
|
+
return [true, knownPath.codec];
|
|
120
139
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContractConstructorMessage } from '@dedot/contracts';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export declare class ConstructorQueryGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
5
|
+
generateMethodDef(def: ContractConstructorMessage): string;
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export class ConstructorQueryGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths = false) {
|
|
5
|
+
this.typesGen.clearCache();
|
|
6
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
7
|
+
this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'ConstructorCallOptions', 'ContractInstantiateResult');
|
|
8
|
+
const { constructors } = this.contractMetadata.spec;
|
|
9
|
+
const constructorsOut = this.doGenerate(constructors, 'ConstructorCallOptions');
|
|
10
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
11
|
+
const template = compileTemplate('typink/templates/constructor-query.hbs');
|
|
12
|
+
return beautifySourceCode(template({ importTypes, constructorsOut }));
|
|
13
|
+
}
|
|
14
|
+
generateMethodDef(def) {
|
|
15
|
+
const { args } = def;
|
|
16
|
+
args.forEach(({ type: { type } }) => this.importType(type));
|
|
17
|
+
const paramsOut = this.generateParamsOut(args);
|
|
18
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} options: ConstructorCallOptions) => Promise<ContractInstantiateResult<ChainApi>>>`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ContractConstructorMessage } from '@dedot/contracts';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export declare class ConstructorTxGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
5
|
+
generateMethodDef(def: ContractConstructorMessage): string;
|
|
6
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
2
|
+
import { QueryGen } from './QueryGen.js';
|
|
3
|
+
export class ConstructorTxGen extends QueryGen {
|
|
4
|
+
generate(useSubPaths = false) {
|
|
5
|
+
this.typesGen.clearCache();
|
|
6
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
7
|
+
this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'GenericInstantiateSubmittableExtrinsic');
|
|
8
|
+
const { constructors } = this.contractMetadata.spec;
|
|
9
|
+
const constructorsOut = this.doGenerate(constructors, 'ConstructorTxOptions');
|
|
10
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
11
|
+
const template = compileTemplate('typink/templates/constructor-tx.hbs');
|
|
12
|
+
return beautifySourceCode(template({ importTypes, constructorsOut }));
|
|
13
|
+
}
|
|
14
|
+
generateMethodDef(def) {
|
|
15
|
+
const { args } = def;
|
|
16
|
+
args.forEach(({ type: { type } }) => this.importType(type));
|
|
17
|
+
const paramsOut = this.generateParamsOut(args);
|
|
18
|
+
return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} options: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi>>`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { TypeImports } from '../../shared/index.js';
|
|
3
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
4
|
+
const SUFFIX = 'contract_api';
|
|
5
|
+
export class IndexGen {
|
|
6
|
+
contractMetadata;
|
|
7
|
+
constructor(contractMetadata) {
|
|
8
|
+
this.contractMetadata = contractMetadata;
|
|
9
|
+
}
|
|
10
|
+
generate(useSubPaths = false) {
|
|
11
|
+
const contractName = stringPascalCase(`${this.contractMetadata.contract.name}_${SUFFIX}`);
|
|
12
|
+
const typeImports = new TypeImports();
|
|
13
|
+
typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
14
|
+
typeImports.addContractType('GenericContractApi');
|
|
15
|
+
typeImports.addChainType('SubstrateApi');
|
|
16
|
+
const importTypes = typeImports.toImports({ useSubPaths });
|
|
17
|
+
const template = compileTemplate('typink/templates/index.hbs');
|
|
18
|
+
return beautifySourceCode(template({ contractName, importTypes }));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { ContractMetadata, ContractMessage, ContractMessageArg } from '@dedot/contracts';
|
|
2
|
+
import { TypesGen } from './TypesGen.js';
|
|
3
|
+
export declare class QueryGen {
|
|
4
|
+
contractMetadata: ContractMetadata;
|
|
5
|
+
typesGen: TypesGen;
|
|
6
|
+
constructor(contractMetadata: ContractMetadata, typeGen: TypesGen);
|
|
7
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
8
|
+
doGenerate(messages: ContractMessage[], optionsTypeName?: string): string;
|
|
9
|
+
generateMethodDef(def: ContractMessage): string;
|
|
10
|
+
generateParamsOut(args: ContractMessageArg[]): string;
|
|
11
|
+
importType(typeId: number): any;
|
|
12
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { normalizeContractTypeDef, normalizeLabel, } from '@dedot/contracts';
|
|
2
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
4
|
+
export class QueryGen {
|
|
5
|
+
contractMetadata;
|
|
6
|
+
typesGen;
|
|
7
|
+
constructor(contractMetadata, typeGen) {
|
|
8
|
+
this.contractMetadata = contractMetadata;
|
|
9
|
+
this.typesGen = typeGen;
|
|
10
|
+
}
|
|
11
|
+
generate(useSubPaths = false) {
|
|
12
|
+
this.typesGen.clearCache();
|
|
13
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
14
|
+
this.typesGen.typeImports.addContractType('GenericContractQuery', 'GenericContractQueryCall', 'ContractCallOptions', 'GenericContractCallResult', 'ContractCallResult');
|
|
15
|
+
const { messages } = this.contractMetadata.spec;
|
|
16
|
+
const queryCallsOut = this.doGenerate(messages, 'ContractCallOptions');
|
|
17
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
18
|
+
const template = compileTemplate('typink/templates/query.hbs');
|
|
19
|
+
return beautifySourceCode(template({ importTypes, queryCallsOut }));
|
|
20
|
+
}
|
|
21
|
+
doGenerate(messages, optionsTypeName) {
|
|
22
|
+
let callsOut = '';
|
|
23
|
+
messages.forEach((messageDef) => {
|
|
24
|
+
const { label, docs, selector, args } = messageDef;
|
|
25
|
+
callsOut += `${commentBlock(docs, '\n', args.map((arg) => `@param {${this.typesGen.generateType(arg.type.type, 1)}} ${stringCamelCase(arg.label)}`), optionsTypeName ? `@param {${optionsTypeName}} options` : '', '\n', `@selector ${selector}`)}`;
|
|
26
|
+
callsOut += `${normalizeLabel(label)}: ${this.generateMethodDef(messageDef)};\n\n`;
|
|
27
|
+
});
|
|
28
|
+
return callsOut;
|
|
29
|
+
}
|
|
30
|
+
generateMethodDef(def) {
|
|
31
|
+
const { args, returnType } = def;
|
|
32
|
+
this.importType(returnType.type);
|
|
33
|
+
args.forEach(({ type: { type } }) => this.importType(type));
|
|
34
|
+
const paramsOut = this.generateParamsOut(args);
|
|
35
|
+
const typeOut = this.typesGen.generateType(returnType.type, 0, true);
|
|
36
|
+
return `GenericContractQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} options: ContractCallOptions) => Promise<GenericContractCallResult<${typeOut}, ContractCallResult<ChainApi>>>>`;
|
|
37
|
+
}
|
|
38
|
+
generateParamsOut(args) {
|
|
39
|
+
return args
|
|
40
|
+
.map(({ type: { type }, label }) => `${stringCamelCase(label)}: ${this.typesGen.generateType(type, 1)}`)
|
|
41
|
+
.join(', ');
|
|
42
|
+
}
|
|
43
|
+
importType(typeId) {
|
|
44
|
+
const type = this.contractMetadata.types[typeId];
|
|
45
|
+
const def = normalizeContractTypeDef(this.contractMetadata.types[typeId].type.def);
|
|
46
|
+
if (this.typesGen.includedTypes[type.id]) {
|
|
47
|
+
this.typesGen.addTypeImport(this.typesGen.includedTypes[type.id].name);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const { tag, value } = def;
|
|
51
|
+
switch (tag) {
|
|
52
|
+
case 'Compact':
|
|
53
|
+
case 'Primitive':
|
|
54
|
+
case 'BitSequence':
|
|
55
|
+
return;
|
|
56
|
+
case 'Struct':
|
|
57
|
+
return value.fields.forEach(({ typeId }) => this.importType(typeId));
|
|
58
|
+
case 'Enum':
|
|
59
|
+
return value.members.forEach(({ fields }) => fields.map(({ typeId }) => this.importType(typeId)).flat());
|
|
60
|
+
case 'Tuple':
|
|
61
|
+
return value.fields.forEach((typeId) => this.importType(typeId));
|
|
62
|
+
case 'SizedVec':
|
|
63
|
+
return this.importType(value.typeParam);
|
|
64
|
+
case 'Sequence':
|
|
65
|
+
return this.importType(value.typeParam);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|