@dedot/codegen 0.0.1-next.41a5fa17.16 → 0.0.1-next.49d80ab2.3
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 +13 -13
- package/chaintypes/generator/ConstsGen.d.ts +4 -0
- package/{generator → chaintypes/generator}/ConstsGen.js +6 -6
- package/{generator → chaintypes/generator}/ErrorsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/ErrorsGen.js +10 -10
- package/{generator → chaintypes/generator}/EventsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/EventsGen.js +10 -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 +27 -18
- package/chaintypes/generator/RuntimeApisGen.d.ts +9 -0
- package/{generator → chaintypes/generator}/RuntimeApisGen.js +95 -20
- package/{generator → chaintypes/generator}/TxGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/TxGen.js +17 -15
- package/chaintypes/generator/TypesGen.d.ts +11 -0
- package/chaintypes/generator/TypesGen.js +55 -0
- package/{generator → chaintypes/generator}/index.d.ts +1 -1
- package/{generator → chaintypes/generator}/index.js +1 -1
- 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 +7 -7
- package/cjs/{generator → chaintypes/generator}/ErrorsGen.js +10 -10
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +10 -10
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/chaintypes/generator/JsonRpcGen.js +59 -0
- package/cjs/{generator → chaintypes/generator}/QueryGen.js +28 -19
- package/cjs/{generator → chaintypes/generator}/RuntimeApisGen.js +97 -22
- package/cjs/{generator → chaintypes/generator}/TxGen.js +17 -15
- package/cjs/chaintypes/generator/TypesGen.js +59 -0
- package/cjs/{generator → chaintypes/generator}/index.js +1 -1
- 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} +112 -166
- package/cjs/{generator → shared}/TypeImports.js +29 -3
- package/cjs/shared/index.js +19 -0
- 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} +4 -4
- package/index.d.ts +2 -4
- package/index.js +2 -48
- package/package.json +15 -17
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +12 -11
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +109 -163
- 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} +1 -1
- package/{generator/utils.js → utils.js} +4 -4
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/IndexGen.js +0 -18
- package/cjs/generator/RpcGen.js +0 -175
- 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/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 -171
- package/generator/RuntimeApisGen.d.ts +0 -9
- 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,69 +1,96 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const codecs_1 = require("@dedot/codecs");
|
|
3
|
+
exports.BaseTypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
5
4
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const utils_js_1 = require("
|
|
7
|
-
const known_types_js_1 = require("./known-types.js");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
8
6
|
const TypeImports_js_1 = require("./TypeImports.js");
|
|
9
7
|
const known_codecs_js_1 = require("./known-codecs.js");
|
|
10
|
-
// Skip generate types for these
|
|
11
|
-
// as we do have native types for them
|
|
12
|
-
const SKIP_TYPES = [
|
|
13
|
-
'BoundedBTreeMap',
|
|
14
|
-
'BoundedBTreeSet',
|
|
15
|
-
'BoundedVec',
|
|
16
|
-
'Box',
|
|
17
|
-
'BTreeMap',
|
|
18
|
-
'BTreeSet',
|
|
19
|
-
'Cow',
|
|
20
|
-
'Option',
|
|
21
|
-
'Range',
|
|
22
|
-
'RangeInclusive',
|
|
23
|
-
'Result',
|
|
24
|
-
'WeakBoundedVec',
|
|
25
|
-
'WrapperKeepOpaque',
|
|
26
|
-
'WrapperOpaque',
|
|
27
|
-
];
|
|
28
8
|
// These are common & generic types, so we'll remove these from all paths at index 1
|
|
29
9
|
// This helps make the type name shorter
|
|
30
10
|
const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
|
|
31
11
|
exports.BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
metadata;
|
|
35
|
-
/**
|
|
36
|
-
* Types will be generated its definition out.
|
|
37
|
-
*/
|
|
12
|
+
class BaseTypesGen {
|
|
13
|
+
types;
|
|
38
14
|
includedTypes;
|
|
39
|
-
registry;
|
|
40
15
|
typeImports;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.
|
|
44
|
-
this.includedTypes =
|
|
16
|
+
skipTypes;
|
|
17
|
+
constructor(types) {
|
|
18
|
+
this.types = types;
|
|
19
|
+
this.includedTypes = {};
|
|
45
20
|
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
21
|
+
this.skipTypes = [];
|
|
46
22
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
let defTypeOut = '';
|
|
50
|
-
Object.values(this.includedTypes)
|
|
51
|
-
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
52
|
-
.forEach(({ name, nameOut, id, docs }) => {
|
|
53
|
-
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
54
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
55
|
-
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
59
|
-
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
60
|
-
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
23
|
+
shouldGenerateTypeIn(_id) {
|
|
24
|
+
return false;
|
|
61
25
|
}
|
|
62
26
|
typeCache = {};
|
|
63
27
|
clearCache() {
|
|
64
28
|
this.typeCache = {};
|
|
65
29
|
this.typeImports.clear();
|
|
66
30
|
}
|
|
31
|
+
includeTypes() {
|
|
32
|
+
const pathsCount = new Map();
|
|
33
|
+
const typesWithPath = this.types.filter((one) => one.path.length > 0);
|
|
34
|
+
const skipIds = [];
|
|
35
|
+
const typeSuffixes = new Map();
|
|
36
|
+
typesWithPath.forEach(({ path, id }) => {
|
|
37
|
+
const joinedPath = path.join('::');
|
|
38
|
+
if (pathsCount.has(joinedPath)) {
|
|
39
|
+
// We compare 2 types with the same path here,
|
|
40
|
+
// if they are the same type -> skip the current one, keep the first occurrence
|
|
41
|
+
// 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
|
|
42
|
+
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
43
|
+
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
44
|
+
if (sameType) {
|
|
45
|
+
skipIds.push(id);
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
pathsCount.get(joinedPath).push(id);
|
|
49
|
+
typeSuffixes.set(id, this.extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
pathsCount.set(joinedPath, [id]);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
return typesWithPath.reduce((o, type) => {
|
|
57
|
+
const { path, id } = type;
|
|
58
|
+
const joinedPath = path.join('::');
|
|
59
|
+
if (this.skipTypes.includes(joinedPath) || this.skipTypes.includes(path.at(-1))) {
|
|
60
|
+
return o;
|
|
61
|
+
}
|
|
62
|
+
const suffix = typeSuffixes.get(id) || '';
|
|
63
|
+
let knownType = false;
|
|
64
|
+
let name, nameOut;
|
|
65
|
+
const [isKnownCodecType, codecName] = (0, known_codecs_js_1.checkKnownCodecType)(joinedPath);
|
|
66
|
+
if (isKnownCodecType) {
|
|
67
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(codecName);
|
|
68
|
+
name = codecType.typeIn;
|
|
69
|
+
nameOut = codecType.typeOut;
|
|
70
|
+
knownType = true;
|
|
71
|
+
}
|
|
72
|
+
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
73
|
+
const newPath = path.slice();
|
|
74
|
+
newPath.splice(1, 1);
|
|
75
|
+
name = this.cleanPath(newPath);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
name = this.cleanPath(path);
|
|
79
|
+
}
|
|
80
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
81
|
+
nameOut = name;
|
|
82
|
+
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
83
|
+
}
|
|
84
|
+
o[id] = {
|
|
85
|
+
name: `${name}${suffix}`,
|
|
86
|
+
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
87
|
+
knownType,
|
|
88
|
+
skip: skipIds.includes(id),
|
|
89
|
+
...type,
|
|
90
|
+
};
|
|
91
|
+
return o;
|
|
92
|
+
}, {});
|
|
93
|
+
}
|
|
67
94
|
generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
68
95
|
if (nestedLevel > 0) {
|
|
69
96
|
const includedDef = this.includedTypes[typeId];
|
|
@@ -94,13 +121,13 @@ class TypesGen {
|
|
|
94
121
|
return type;
|
|
95
122
|
}
|
|
96
123
|
#generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
97
|
-
const def = this.
|
|
124
|
+
const def = this.types[typeId];
|
|
98
125
|
if (!def) {
|
|
99
126
|
throw new Error(`Type def not found ${JSON.stringify(def)}`);
|
|
100
127
|
}
|
|
101
|
-
const {
|
|
102
|
-
const {
|
|
103
|
-
switch (
|
|
128
|
+
const { typeDef, path, docs } = def;
|
|
129
|
+
const { type, value } = typeDef;
|
|
130
|
+
switch (type) {
|
|
104
131
|
case 'Primitive':
|
|
105
132
|
const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
|
|
106
133
|
if ($codec.nativeType) {
|
|
@@ -171,14 +198,14 @@ class TypesGen {
|
|
|
171
198
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
172
199
|
}
|
|
173
200
|
}
|
|
174
|
-
const { tagKey, valueKey } = this.
|
|
201
|
+
const { tagKey, valueKey } = this.getEnumOptions(typeId);
|
|
175
202
|
return membersType
|
|
176
203
|
.map(([keyName, valueType, docs]) => ({
|
|
177
|
-
|
|
204
|
+
type: `${tagKey}: '${keyName}'`,
|
|
178
205
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
179
206
|
docs,
|
|
180
207
|
}))
|
|
181
|
-
.map(({
|
|
208
|
+
.map(({ type, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${type}${value} }`)
|
|
182
209
|
.join(' | ');
|
|
183
210
|
}
|
|
184
211
|
}
|
|
@@ -200,9 +227,9 @@ class TypesGen {
|
|
|
200
227
|
return this.generateType(value.typeParam, nestedLevel + 1, typeOut);
|
|
201
228
|
case 'Sequence':
|
|
202
229
|
case 'SizedVec': {
|
|
203
|
-
const fixedSize =
|
|
204
|
-
const $innerType = this.
|
|
205
|
-
if ($innerType.
|
|
230
|
+
const fixedSize = type === 'SizedVec' ? `${value.len}` : null;
|
|
231
|
+
const $innerType = this.types[value.typeParam].typeDef;
|
|
232
|
+
if ($innerType.type === 'Primitive' && $innerType.value.kind === 'u8') {
|
|
206
233
|
return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
|
|
207
234
|
}
|
|
208
235
|
else {
|
|
@@ -211,7 +238,7 @@ class TypesGen {
|
|
|
211
238
|
}
|
|
212
239
|
}
|
|
213
240
|
default:
|
|
214
|
-
throw new Error(`Invalid type! ${
|
|
241
|
+
throw new Error(`Invalid type! ${type}`);
|
|
215
242
|
}
|
|
216
243
|
}
|
|
217
244
|
generateObjectType(fields, nestedLevel = 0, typeOut = false) {
|
|
@@ -231,98 +258,20 @@ class TypesGen {
|
|
|
231
258
|
#isOptionalType(type) {
|
|
232
259
|
return type.endsWith('| undefined');
|
|
233
260
|
}
|
|
234
|
-
#includedTypes() {
|
|
235
|
-
const { types } = this.metadata;
|
|
236
|
-
const pathsCount = new Map();
|
|
237
|
-
const typesWithPath = types.filter((one) => one.path.length > 0);
|
|
238
|
-
const skipIds = [];
|
|
239
|
-
const typeSuffixes = new Map();
|
|
240
|
-
typesWithPath.forEach(({ path, id }) => {
|
|
241
|
-
const joinedPath = path.join('::');
|
|
242
|
-
if (pathsCount.has(joinedPath)) {
|
|
243
|
-
// We compare 2 types with the same path here,
|
|
244
|
-
// if they are the same type -> skip the current one, keep the first occurrence
|
|
245
|
-
// 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
|
|
246
|
-
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
247
|
-
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
248
|
-
if (sameType) {
|
|
249
|
-
skipIds.push(id);
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
pathsCount.get(joinedPath).push(id);
|
|
253
|
-
typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
pathsCount.set(joinedPath, [id]);
|
|
258
|
-
}
|
|
259
|
-
});
|
|
260
|
-
return typesWithPath.reduce((o, type) => {
|
|
261
|
-
const { path, id } = type;
|
|
262
|
-
const joinedPath = path.join('::');
|
|
263
|
-
if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
|
|
264
|
-
return o;
|
|
265
|
-
}
|
|
266
|
-
const suffix = typeSuffixes.get(id) || '';
|
|
267
|
-
let knownType = false;
|
|
268
|
-
let name, nameOut;
|
|
269
|
-
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
270
|
-
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
271
|
-
name = codecType.typeIn;
|
|
272
|
-
nameOut = codecType.typeOut;
|
|
273
|
-
knownType = true;
|
|
274
|
-
}
|
|
275
|
-
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
276
|
-
const newPath = path.slice();
|
|
277
|
-
newPath.splice(1, 1);
|
|
278
|
-
name = this.#cleanPath(newPath);
|
|
279
|
-
}
|
|
280
|
-
else {
|
|
281
|
-
name = this.#cleanPath(path);
|
|
282
|
-
}
|
|
283
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
284
|
-
nameOut = name;
|
|
285
|
-
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
286
|
-
}
|
|
287
|
-
o[id] = {
|
|
288
|
-
name: `${name}${suffix}`,
|
|
289
|
-
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
290
|
-
knownType,
|
|
291
|
-
skip: skipIds.includes(id),
|
|
292
|
-
...type,
|
|
293
|
-
};
|
|
294
|
-
return o;
|
|
295
|
-
}, {});
|
|
296
|
-
}
|
|
297
261
|
#removeGenericPart(typeName) {
|
|
298
|
-
if (typeName.match(WRAPPER_TYPE_REGEX)) {
|
|
299
|
-
return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
262
|
+
if (typeName.match(utils_js_1.WRAPPER_TYPE_REGEX)) {
|
|
263
|
+
return typeName.replace(utils_js_1.WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
300
264
|
}
|
|
301
265
|
else {
|
|
302
266
|
return typeName;
|
|
303
267
|
}
|
|
304
268
|
}
|
|
305
|
-
|
|
306
|
-
* @description Remove duplicated part of the path
|
|
307
|
-
*
|
|
308
|
-
* Example:
|
|
309
|
-
* ["pallet_staking", "pallet", "pallet", "Event"]
|
|
310
|
-
* => ["pallet_staking", "pallet", "Event"]
|
|
311
|
-
*
|
|
312
|
-
* @param path
|
|
313
|
-
* @private
|
|
314
|
-
*/
|
|
315
|
-
#cleanPath(path) {
|
|
269
|
+
cleanPath(path) {
|
|
316
270
|
return path
|
|
317
271
|
.map((one) => (0, utils_1.stringPascalCase)(one))
|
|
318
272
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
319
273
|
.join('');
|
|
320
274
|
}
|
|
321
|
-
#shouldGenerateTypeIn(id) {
|
|
322
|
-
const { callTypeId } = this.metadata.extrinsic;
|
|
323
|
-
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
324
|
-
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
325
|
-
}
|
|
326
275
|
eqlCache = new Map();
|
|
327
276
|
typeEql(idA, idB, level = 0) {
|
|
328
277
|
const cacheKey = `${idA}==${idB}`;
|
|
@@ -335,36 +284,36 @@ class TypesGen {
|
|
|
335
284
|
#typeEql(idA, idB, lvl = 0) {
|
|
336
285
|
if (idA === idB)
|
|
337
286
|
return true;
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
const typeB = types[idB];
|
|
287
|
+
const typeA = this.types[idA];
|
|
288
|
+
const typeB = this.types[idB];
|
|
341
289
|
if (typeA.path.join('::') !== typeB.path.join('::'))
|
|
342
290
|
return false;
|
|
343
|
-
const {
|
|
344
|
-
const {
|
|
291
|
+
const { typeDef: defA, params: paramsA } = typeA;
|
|
292
|
+
const { typeDef: defB, params: paramsB } = typeB;
|
|
345
293
|
if (!this.#eqlArray(paramsA, paramsB, (valA, valB) => this.#eqlTypeParam(valA, valB))) {
|
|
346
294
|
return false;
|
|
347
295
|
}
|
|
348
|
-
if (defA.
|
|
296
|
+
if (defA.type !== defB.type)
|
|
349
297
|
return false;
|
|
350
|
-
if (defA.
|
|
298
|
+
if (defA.type === 'BitSequence')
|
|
351
299
|
return true;
|
|
352
|
-
if (defA.
|
|
300
|
+
if (defA.type === 'Primitive' && defB.type === 'Primitive') {
|
|
353
301
|
return defA.value.kind === defB.value.kind;
|
|
354
302
|
}
|
|
355
|
-
if ((defA.
|
|
303
|
+
if ((defA.type === 'Compact' && defB.type === 'Compact') ||
|
|
304
|
+
(defA.type === 'Sequence' && defB.type === 'Sequence')) {
|
|
356
305
|
return this.typeEql(defA.value.typeParam, defB.value.typeParam, lvl + 1);
|
|
357
306
|
}
|
|
358
|
-
if (defA.
|
|
307
|
+
if (defA.type === 'SizedVec' && defB.type === 'SizedVec') {
|
|
359
308
|
return defA.value.len === defB.value.len && this.typeEql(defA.value.typeParam, defB.value.typeParam, lvl + 1);
|
|
360
309
|
}
|
|
361
|
-
if (defA.
|
|
310
|
+
if (defA.type === 'Tuple' && defB.type === 'Tuple') {
|
|
362
311
|
return this.#eqlArray(defA.value.fields, defB.value.fields, (val1, val2) => this.typeEql(val1, val2, lvl + 1));
|
|
363
312
|
}
|
|
364
|
-
if (defA.
|
|
313
|
+
if (defA.type === 'Struct' && defB.type === 'Struct') {
|
|
365
314
|
return this.#eqlFields(defA.value.fields, defB.value.fields, lvl);
|
|
366
315
|
}
|
|
367
|
-
if (defA.
|
|
316
|
+
if (defA.type === 'Enum' && defB.type === 'Enum') {
|
|
368
317
|
return this.#eqlArray(defA.value.members, defB.value.members, (val1, val2) => val1.name === val2.name && val1.index === val2.index && this.#eqlFields(val1.fields, val2.fields, lvl));
|
|
369
318
|
}
|
|
370
319
|
return false;
|
|
@@ -380,19 +329,18 @@ class TypesGen {
|
|
|
380
329
|
(param1.typeId === undefined) === (param2.typeId === undefined) &&
|
|
381
330
|
(param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
|
|
382
331
|
}
|
|
383
|
-
|
|
384
|
-
const
|
|
385
|
-
const
|
|
386
|
-
const dupTypeParams = types[dupTypeId].params;
|
|
332
|
+
extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
|
|
333
|
+
const originalTypeParams = this.types[originalTypeId].params;
|
|
334
|
+
const dupTypeParams = this.types[dupTypeId].params;
|
|
387
335
|
const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
|
|
388
336
|
// TODO make sure these suffix is unique if a type is duplicated more than 2 times
|
|
389
337
|
if (diffParam?.typeId) {
|
|
390
|
-
const diffType = types[diffParam.typeId];
|
|
338
|
+
const diffType = this.types[diffParam.typeId];
|
|
391
339
|
if (diffType.path.length > 0) {
|
|
392
340
|
return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
|
|
393
341
|
}
|
|
394
|
-
else if (diffType.type
|
|
395
|
-
return (0, utils_1.stringPascalCase)(diffType.
|
|
342
|
+
else if (diffType.typeDef.type === 'Primitive') {
|
|
343
|
+
return (0, utils_1.stringPascalCase)(diffType.typeDef.value.kind);
|
|
396
344
|
}
|
|
397
345
|
}
|
|
398
346
|
// Last resort!
|
|
@@ -425,12 +373,10 @@ class TypesGen {
|
|
|
425
373
|
this.typeImports.addCodecType(typeName);
|
|
426
374
|
return;
|
|
427
375
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
this.typeImports.addOutType(typeName);
|
|
433
|
-
}
|
|
376
|
+
this.typeImports.addOutType(typeName);
|
|
377
|
+
}
|
|
378
|
+
getEnumOptions(_typeId) {
|
|
379
|
+
return { tagKey: 'type', valueKey: 'value' };
|
|
434
380
|
}
|
|
435
381
|
}
|
|
436
|
-
exports.
|
|
382
|
+
exports.BaseTypesGen = BaseTypesGen;
|
|
@@ -8,30 +8,47 @@ class TypeImports {
|
|
|
8
8
|
codecTypes;
|
|
9
9
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
10
10
|
knownTypes;
|
|
11
|
+
knownJsonRpcTypes;
|
|
11
12
|
// External types to define explicitly
|
|
12
13
|
outTypes;
|
|
14
|
+
// Know types defined in @dedot/contracts
|
|
15
|
+
contractTypes;
|
|
16
|
+
// Know types defined in @dedot/api/chaintypes or dedot/chaintypes
|
|
17
|
+
chainTypes;
|
|
13
18
|
constructor() {
|
|
14
19
|
this.portableTypes = new Set();
|
|
15
20
|
this.codecTypes = new Set();
|
|
16
21
|
this.knownTypes = new Set();
|
|
22
|
+
this.knownJsonRpcTypes = new Set();
|
|
17
23
|
this.outTypes = new Set();
|
|
24
|
+
this.contractTypes = new Set();
|
|
25
|
+
this.chainTypes = new Set();
|
|
18
26
|
}
|
|
19
27
|
clear() {
|
|
20
28
|
this.portableTypes.clear();
|
|
21
29
|
this.codecTypes.clear();
|
|
22
30
|
this.knownTypes.clear();
|
|
31
|
+
this.knownJsonRpcTypes.clear();
|
|
23
32
|
this.outTypes.clear();
|
|
33
|
+
this.contractTypes.clear();
|
|
34
|
+
this.chainTypes.clear();
|
|
24
35
|
}
|
|
25
|
-
toImports(
|
|
36
|
+
toImports(config) {
|
|
37
|
+
const { excludeModules = [], useSubPaths = false } = config || {};
|
|
26
38
|
// TODO generate outTypes!
|
|
39
|
+
const prefix = useSubPaths ? '' : '@';
|
|
27
40
|
const toImports = [
|
|
28
|
-
[this.knownTypes,
|
|
29
|
-
[this.
|
|
41
|
+
[this.knownTypes, `${prefix}dedot/types`],
|
|
42
|
+
[this.knownJsonRpcTypes, `${prefix}dedot/types/json-rpc`],
|
|
43
|
+
[this.codecTypes, `${prefix}dedot/codecs`],
|
|
44
|
+
[this.contractTypes, `${prefix}dedot/contracts`],
|
|
45
|
+
[this.chainTypes, prefix ? '@dedot/api/chaintypes' : 'dedot/chaintypes'],
|
|
30
46
|
[this.portableTypes, './types'],
|
|
31
47
|
];
|
|
32
48
|
return toImports
|
|
33
49
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
34
50
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
51
|
+
.filter((line) => line.length > 0)
|
|
35
52
|
.join('\n');
|
|
36
53
|
}
|
|
37
54
|
#toImportLine(types, module) {
|
|
@@ -49,8 +66,17 @@ class TypeImports {
|
|
|
49
66
|
addKnownType(...types) {
|
|
50
67
|
types.forEach((one) => this.knownTypes.add(one));
|
|
51
68
|
}
|
|
69
|
+
addKnownJsonRpcType(...types) {
|
|
70
|
+
types.forEach((one) => this.knownJsonRpcTypes.add(one));
|
|
71
|
+
}
|
|
52
72
|
addOutType(...types) {
|
|
53
73
|
types.forEach((one) => this.outTypes.add(one));
|
|
54
74
|
}
|
|
75
|
+
addContractType(...types) {
|
|
76
|
+
types.forEach((one) => this.contractTypes.add(one));
|
|
77
|
+
}
|
|
78
|
+
addChainType(...types) {
|
|
79
|
+
types.forEach((one) => this.chainTypes.add(one));
|
|
80
|
+
}
|
|
55
81
|
}
|
|
56
82
|
exports.TypeImports = TypeImports;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./BaseTypesGen.js"), exports);
|
|
18
|
+
__exportStar(require("./known-codecs.js"), exports);
|
|
19
|
+
__exportStar(require("./TypeImports.js"), exports);
|
|
@@ -23,10 +23,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
const $ = __importStar(require("@dedot/shape"));
|
|
26
|
+
exports.checkKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
|
|
28
27
|
const Codecs = __importStar(require("@dedot/codecs"));
|
|
29
28
|
const codecs_1 = require("@dedot/codecs");
|
|
29
|
+
const $ = __importStar(require("@dedot/shape"));
|
|
30
30
|
const utils_1 = require("@dedot/utils");
|
|
31
31
|
const normalizeCodecName = (name) => {
|
|
32
32
|
return name.startsWith('$') ? name : `$${name}`;
|
|
@@ -57,6 +57,8 @@ const KNOWN_PATHS = [
|
|
|
57
57
|
/^primitive_types::\w+$/,
|
|
58
58
|
/^sp_arithmetic::per_things::\w+$/,
|
|
59
59
|
/^sp_arithmetic::fixed_point::\w+$/,
|
|
60
|
+
'ink_primitives::types::Hash',
|
|
61
|
+
{ path: 'ink_primitives::types::AccountId', codec: 'AccountId32' },
|
|
60
62
|
];
|
|
61
63
|
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
62
64
|
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
@@ -144,8 +146,25 @@ function findKnownWrapperCodec(typeName) {
|
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
148
|
exports.findKnownWrapperCodec = findKnownWrapperCodec;
|
|
147
|
-
function
|
|
149
|
+
function checkKnownCodecType(path) {
|
|
148
150
|
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
149
|
-
|
|
151
|
+
const knownPath = KNOWN_PATHS.find((one) => {
|
|
152
|
+
if (typeof one === 'string') {
|
|
153
|
+
return one === joinedPath;
|
|
154
|
+
}
|
|
155
|
+
else if (one instanceof RegExp) {
|
|
156
|
+
return one.test(joinedPath);
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
return one.path === joinedPath;
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
if (!knownPath) {
|
|
163
|
+
return [false, ''];
|
|
164
|
+
}
|
|
165
|
+
if (typeof knownPath === 'string' || knownPath instanceof RegExp) {
|
|
166
|
+
return [true, joinedPath.split('::').at(-1)];
|
|
167
|
+
}
|
|
168
|
+
return [true, knownPath.codec];
|
|
150
169
|
}
|
|
151
|
-
exports.
|
|
170
|
+
exports.checkKnownCodecType = checkKnownCodecType;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstructorQueryGen = void 0;
|
|
4
|
+
const utils_js_1 = require("../../utils.js");
|
|
5
|
+
const QueryGen_js_1 = require("./QueryGen.js");
|
|
6
|
+
class ConstructorQueryGen extends QueryGen_js_1.QueryGen {
|
|
7
|
+
generate(useSubPaths = false) {
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
10
|
+
this.typesGen.typeImports.addContractType('GenericConstructorQuery', 'GenericConstructorQueryCall', 'ConstructorCallOptions', 'ContractInstantiateResult');
|
|
11
|
+
const { constructors } = this.contractMetadata.spec;
|
|
12
|
+
const constructorsOut = this.doGenerate(constructors, 'ConstructorCallOptions');
|
|
13
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
14
|
+
const template = (0, utils_js_1.compileTemplate)('typink/templates/constructor-query.hbs');
|
|
15
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, constructorsOut }));
|
|
16
|
+
}
|
|
17
|
+
generateMethodDef(def) {
|
|
18
|
+
const { args } = def;
|
|
19
|
+
args.forEach(({ type: { type } }) => this.importType(type));
|
|
20
|
+
const paramsOut = this.generateParamsOut(args);
|
|
21
|
+
return `GenericConstructorQueryCall<ChainApi, (${paramsOut && `${paramsOut},`} options: ConstructorCallOptions) => Promise<ContractInstantiateResult<ChainApi>>>`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.ConstructorQueryGen = ConstructorQueryGen;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ConstructorTxGen = void 0;
|
|
4
|
+
const utils_js_1 = require("../../utils.js");
|
|
5
|
+
const QueryGen_js_1 = require("./QueryGen.js");
|
|
6
|
+
class ConstructorTxGen extends QueryGen_js_1.QueryGen {
|
|
7
|
+
generate(useSubPaths = false) {
|
|
8
|
+
this.typesGen.clearCache();
|
|
9
|
+
this.typesGen.typeImports.addKnownType('GenericSubstrateApi');
|
|
10
|
+
this.typesGen.typeImports.addContractType('GenericConstructorTx', 'GenericConstructorTxCall', 'ConstructorTxOptions', 'GenericInstantiateSubmittableExtrinsic');
|
|
11
|
+
const { constructors } = this.contractMetadata.spec;
|
|
12
|
+
const constructorsOut = this.doGenerate(constructors, 'ConstructorTxOptions');
|
|
13
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
14
|
+
const template = (0, utils_js_1.compileTemplate)('typink/templates/constructor-tx.hbs');
|
|
15
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, constructorsOut }));
|
|
16
|
+
}
|
|
17
|
+
generateMethodDef(def) {
|
|
18
|
+
const { args } = def;
|
|
19
|
+
args.forEach(({ type: { type } }) => this.importType(type));
|
|
20
|
+
const paramsOut = this.generateParamsOut(args);
|
|
21
|
+
return `GenericConstructorTxCall<ChainApi, (${paramsOut && `${paramsOut},`} options: ConstructorTxOptions) => GenericInstantiateSubmittableExtrinsic<ChainApi>>`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.ConstructorTxGen = ConstructorTxGen;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IndexGen = void 0;
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const index_js_1 = require("../../shared/index.js");
|
|
6
|
+
const utils_js_1 = require("../../utils.js");
|
|
7
|
+
const SUFFIX = 'contract_api';
|
|
8
|
+
class IndexGen {
|
|
9
|
+
contractMetadata;
|
|
10
|
+
constructor(contractMetadata) {
|
|
11
|
+
this.contractMetadata = contractMetadata;
|
|
12
|
+
}
|
|
13
|
+
generate(useSubPaths = false) {
|
|
14
|
+
const contractName = (0, utils_1.stringPascalCase)(`${this.contractMetadata.contract.name}_${SUFFIX}`);
|
|
15
|
+
const typeImports = new index_js_1.TypeImports();
|
|
16
|
+
typeImports.addKnownType('VersionedGenericSubstrateApi', 'RpcVersion', 'RpcV2');
|
|
17
|
+
typeImports.addContractType('GenericContractApi');
|
|
18
|
+
typeImports.addChainType('SubstrateApi');
|
|
19
|
+
const importTypes = typeImports.toImports({ useSubPaths });
|
|
20
|
+
const template = (0, utils_js_1.compileTemplate)('typink/templates/index.hbs');
|
|
21
|
+
return (0, utils_js_1.beautifySourceCode)(template({ contractName, importTypes }));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.IndexGen = IndexGen;
|