@dedot/codegen 0.0.1-next.9dacef43.1 → 0.0.1-next.a5b1d2fb.22
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 +2 -2
- 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/chaintypes/generator/RuntimeApisGen.js +189 -0
- 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 +76 -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/{generator/dirname.js → dirname.js} +0 -1
- package/cjs/index.js +4 -62
- package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +102 -160
- package/cjs/{generator → shared}/TypeImports.js +29 -3
- package/cjs/{generator → shared}/index.js +3 -10
- package/cjs/shared/known-codecs.js +170 -0
- 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 +35 -0
- package/cjs/typink/generator/index.js +22 -0
- package/cjs/typink/index.js +36 -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 +16 -18
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +13 -12
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +89 -147
- 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/shared/known-codecs.d.ts +23 -0
- package/shared/known-codecs.js +139 -0
- 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 +8 -0
- package/typink/generator/TypesGen.js +31 -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 -79
- package/cjs/generator/ErrorsGen.js +0 -41
- package/cjs/generator/IndexGen.js +0 -18
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/generator/RuntimeApisGen.js +0 -115
- 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 -74
- 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 -171
- package/generator/RuntimeApisGen.d.ts +0 -9
- package/generator/RuntimeApisGen.js +0 -111
- package/generator/TxGen.d.ts +0 -5
- package/generator/index.d.ts +0 -10
- package/generator/index.js +0 -10
- 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/{generator/dirname.d.ts → dirname.d.ts} +0 -0
- /package/{generator/dirname.js → dirname.js} +0 -0
|
@@ -1,66 +1,90 @@
|
|
|
1
|
-
import { stringPascalCase } from '@
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { registry } from '@dedot/types';
|
|
6
|
-
import { TypeImports } from './TypeImports';
|
|
7
|
-
// Skip generate types for these
|
|
8
|
-
// as we do have native types for them
|
|
9
|
-
const SKIP_TYPES = [
|
|
10
|
-
'BoundedBTreeMap',
|
|
11
|
-
'BoundedBTreeSet',
|
|
12
|
-
'BoundedVec',
|
|
13
|
-
'Box',
|
|
14
|
-
'BTreeMap',
|
|
15
|
-
'BTreeSet',
|
|
16
|
-
'Cow',
|
|
17
|
-
'Option',
|
|
18
|
-
'Range',
|
|
19
|
-
'RangeInclusive',
|
|
20
|
-
'Result',
|
|
21
|
-
'WeakBoundedVec',
|
|
22
|
-
'WrapperKeepOpaque',
|
|
23
|
-
'WrapperOpaque',
|
|
24
|
-
];
|
|
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';
|
|
25
5
|
// These are common & generic types, so we'll remove these from all paths at index 1
|
|
26
6
|
// This helps make the type name shorter
|
|
27
7
|
const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
|
|
28
8
|
export const BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
metadata;
|
|
32
|
-
/**
|
|
33
|
-
* Types will be generated its definition out.
|
|
34
|
-
*/
|
|
9
|
+
export class BaseTypesGen {
|
|
10
|
+
types;
|
|
35
11
|
includedTypes;
|
|
36
|
-
registry;
|
|
37
12
|
typeImports;
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.
|
|
41
|
-
this.includedTypes =
|
|
13
|
+
skipTypes;
|
|
14
|
+
constructor(types) {
|
|
15
|
+
this.types = types;
|
|
16
|
+
this.includedTypes = {};
|
|
42
17
|
this.typeImports = new TypeImports();
|
|
43
|
-
|
|
44
|
-
generate() {
|
|
45
|
-
this.clearCache();
|
|
46
|
-
let defTypeOut = '';
|
|
47
|
-
Object.values(this.includedTypes)
|
|
48
|
-
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
49
|
-
.forEach(({ name, nameOut, id, docs }) => {
|
|
50
|
-
defTypeOut += `${commentBlock(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
51
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
52
|
-
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
56
|
-
const template = compileTemplate('types.hbs');
|
|
57
|
-
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
18
|
+
this.skipTypes = [];
|
|
58
19
|
}
|
|
59
20
|
typeCache = {};
|
|
60
21
|
clearCache() {
|
|
61
22
|
this.typeCache = {};
|
|
62
23
|
this.typeImports.clear();
|
|
63
24
|
}
|
|
25
|
+
includeTypes() {
|
|
26
|
+
const pathsCount = new Map();
|
|
27
|
+
const typesWithPath = this.types.filter((one) => one.path.length > 0);
|
|
28
|
+
const skipIds = [];
|
|
29
|
+
const typeSuffixes = new Map();
|
|
30
|
+
typesWithPath.forEach(({ path, id }) => {
|
|
31
|
+
const joinedPath = path.join('::');
|
|
32
|
+
if (pathsCount.has(joinedPath)) {
|
|
33
|
+
// We compare 2 types with the same path here,
|
|
34
|
+
// if they are the same type -> skip the current one, keep the first occurrence
|
|
35
|
+
// 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
|
|
36
|
+
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
37
|
+
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
38
|
+
if (sameType) {
|
|
39
|
+
skipIds.push(id);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
pathsCount.get(joinedPath).push(id);
|
|
43
|
+
typeSuffixes.set(id, this.extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
pathsCount.set(joinedPath, [id]);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return typesWithPath.reduce((o, type) => {
|
|
51
|
+
const { path, id } = type;
|
|
52
|
+
const joinedPath = path.join('::');
|
|
53
|
+
if (this.skipTypes.includes(joinedPath) || this.skipTypes.includes(path.at(-1))) {
|
|
54
|
+
return o;
|
|
55
|
+
}
|
|
56
|
+
const suffix = typeSuffixes.get(id) || '';
|
|
57
|
+
let knownType = false;
|
|
58
|
+
let name, nameOut;
|
|
59
|
+
const [isKnownCodecType, codecName] = checkKnownCodecType(joinedPath);
|
|
60
|
+
if (isKnownCodecType) {
|
|
61
|
+
const codecType = findKnownCodecType(codecName);
|
|
62
|
+
name = codecType.typeIn;
|
|
63
|
+
nameOut = codecType.typeOut;
|
|
64
|
+
knownType = true;
|
|
65
|
+
}
|
|
66
|
+
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
67
|
+
const newPath = path.slice();
|
|
68
|
+
newPath.splice(1, 1);
|
|
69
|
+
name = this.cleanPath(newPath);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
name = this.cleanPath(path);
|
|
73
|
+
}
|
|
74
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
75
|
+
nameOut = name;
|
|
76
|
+
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
77
|
+
}
|
|
78
|
+
o[id] = {
|
|
79
|
+
name: `${name}${suffix}`,
|
|
80
|
+
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
81
|
+
knownType,
|
|
82
|
+
skip: skipIds.includes(id),
|
|
83
|
+
...type,
|
|
84
|
+
};
|
|
85
|
+
return o;
|
|
86
|
+
}, {});
|
|
87
|
+
}
|
|
64
88
|
generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
65
89
|
if (nestedLevel > 0) {
|
|
66
90
|
const includedDef = this.includedTypes[typeId];
|
|
@@ -91,7 +115,7 @@ export class TypesGen {
|
|
|
91
115
|
return type;
|
|
92
116
|
}
|
|
93
117
|
#generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
94
|
-
const def = this.
|
|
118
|
+
const def = this.types[typeId];
|
|
95
119
|
if (!def) {
|
|
96
120
|
throw new Error(`Type def not found ${JSON.stringify(def)}`);
|
|
97
121
|
}
|
|
@@ -99,7 +123,7 @@ export class TypesGen {
|
|
|
99
123
|
const { tag, value } = type;
|
|
100
124
|
switch (tag) {
|
|
101
125
|
case 'Primitive':
|
|
102
|
-
const $codec =
|
|
126
|
+
const $codec = findKnownCodec(value.kind);
|
|
103
127
|
if ($codec.nativeType) {
|
|
104
128
|
return $codec.nativeType;
|
|
105
129
|
}
|
|
@@ -168,7 +192,7 @@ export class TypesGen {
|
|
|
168
192
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
169
193
|
}
|
|
170
194
|
}
|
|
171
|
-
const { tagKey, valueKey } = this.
|
|
195
|
+
const { tagKey, valueKey } = this.getEnumOptions(typeId);
|
|
172
196
|
return membersType
|
|
173
197
|
.map(([keyName, valueType, docs]) => ({
|
|
174
198
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -198,7 +222,7 @@ export class TypesGen {
|
|
|
198
222
|
case 'Sequence':
|
|
199
223
|
case 'SizedVec': {
|
|
200
224
|
const fixedSize = tag === 'SizedVec' ? `${value.len}` : null;
|
|
201
|
-
const $innerType = this.
|
|
225
|
+
const $innerType = this.types[value.typeParam].type;
|
|
202
226
|
if ($innerType.tag === 'Primitive' && $innerType.value.kind === 'u8') {
|
|
203
227
|
return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
|
|
204
228
|
}
|
|
@@ -228,69 +252,6 @@ export class TypesGen {
|
|
|
228
252
|
#isOptionalType(type) {
|
|
229
253
|
return type.endsWith('| undefined');
|
|
230
254
|
}
|
|
231
|
-
#includedTypes() {
|
|
232
|
-
const { types } = this.metadata;
|
|
233
|
-
const pathsCount = new Map();
|
|
234
|
-
const typesWithPath = types.filter((one) => one.path.length > 0);
|
|
235
|
-
const skipIds = [];
|
|
236
|
-
const typeSuffixes = new Map();
|
|
237
|
-
typesWithPath.forEach(({ path, id }) => {
|
|
238
|
-
const joinedPath = path.join('::');
|
|
239
|
-
if (pathsCount.has(joinedPath)) {
|
|
240
|
-
// We compare 2 types with the same path here,
|
|
241
|
-
// if they are the same type -> skip the current one, keep the first occurrence
|
|
242
|
-
// 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
|
|
243
|
-
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
244
|
-
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
245
|
-
if (sameType) {
|
|
246
|
-
skipIds.push(id);
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
pathsCount.get(joinedPath).push(id);
|
|
250
|
-
typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
pathsCount.set(joinedPath, [id]);
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
return typesWithPath.reduce((o, type) => {
|
|
258
|
-
const { path, id } = type;
|
|
259
|
-
const joinedPath = path.join('::');
|
|
260
|
-
if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
|
|
261
|
-
return o;
|
|
262
|
-
}
|
|
263
|
-
const suffix = typeSuffixes.get(id) || '';
|
|
264
|
-
let knownType = false;
|
|
265
|
-
let name, nameOut;
|
|
266
|
-
if (this.registry.isKnownType(joinedPath)) {
|
|
267
|
-
const codecType = this.registry.findCodecType(path.at(-1));
|
|
268
|
-
name = codecType.typeIn;
|
|
269
|
-
nameOut = codecType.typeOut;
|
|
270
|
-
knownType = true;
|
|
271
|
-
}
|
|
272
|
-
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
273
|
-
const newPath = path.slice();
|
|
274
|
-
newPath.splice(1, 1);
|
|
275
|
-
name = this.#cleanPath(newPath);
|
|
276
|
-
}
|
|
277
|
-
else {
|
|
278
|
-
name = this.#cleanPath(path);
|
|
279
|
-
}
|
|
280
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
281
|
-
nameOut = name;
|
|
282
|
-
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
283
|
-
}
|
|
284
|
-
o[id] = {
|
|
285
|
-
name: `${name}${suffix}`,
|
|
286
|
-
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
287
|
-
knownType,
|
|
288
|
-
skip: skipIds.includes(id),
|
|
289
|
-
...type,
|
|
290
|
-
};
|
|
291
|
-
return o;
|
|
292
|
-
}, {});
|
|
293
|
-
}
|
|
294
255
|
#removeGenericPart(typeName) {
|
|
295
256
|
if (typeName.match(WRAPPER_TYPE_REGEX)) {
|
|
296
257
|
return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
@@ -299,27 +260,12 @@ export class TypesGen {
|
|
|
299
260
|
return typeName;
|
|
300
261
|
}
|
|
301
262
|
}
|
|
302
|
-
|
|
303
|
-
* @description Remove duplicated part of the path
|
|
304
|
-
*
|
|
305
|
-
* Example:
|
|
306
|
-
* ["pallet_staking", "pallet", "pallet", "Event"]
|
|
307
|
-
* => ["pallet_staking", "pallet", "Event"]
|
|
308
|
-
*
|
|
309
|
-
* @param path
|
|
310
|
-
* @private
|
|
311
|
-
*/
|
|
312
|
-
#cleanPath(path) {
|
|
263
|
+
cleanPath(path) {
|
|
313
264
|
return path
|
|
314
265
|
.map((one) => stringPascalCase(one))
|
|
315
266
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
316
267
|
.join('');
|
|
317
268
|
}
|
|
318
|
-
#shouldGenerateTypeIn(id) {
|
|
319
|
-
const { callTypeId } = this.metadata.extrinsic;
|
|
320
|
-
const palletCallTypeIds = this.registry.portableRegistry.getPalletCallTypeIds();
|
|
321
|
-
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
322
|
-
}
|
|
323
269
|
eqlCache = new Map();
|
|
324
270
|
typeEql(idA, idB, level = 0) {
|
|
325
271
|
const cacheKey = `${idA}==${idB}`;
|
|
@@ -332,9 +278,8 @@ export class TypesGen {
|
|
|
332
278
|
#typeEql(idA, idB, lvl = 0) {
|
|
333
279
|
if (idA === idB)
|
|
334
280
|
return true;
|
|
335
|
-
const
|
|
336
|
-
const
|
|
337
|
-
const typeB = types[idB];
|
|
281
|
+
const typeA = this.types[idA];
|
|
282
|
+
const typeB = this.types[idB];
|
|
338
283
|
if (typeA.path.join('::') !== typeB.path.join('::'))
|
|
339
284
|
return false;
|
|
340
285
|
const { type: defA, params: paramsA } = typeA;
|
|
@@ -377,14 +322,13 @@ export class TypesGen {
|
|
|
377
322
|
(param1.typeId === undefined) === (param2.typeId === undefined) &&
|
|
378
323
|
(param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
|
|
379
324
|
}
|
|
380
|
-
|
|
381
|
-
const
|
|
382
|
-
const
|
|
383
|
-
const dupTypeParams = types[dupTypeId].params;
|
|
325
|
+
extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
|
|
326
|
+
const originalTypeParams = this.types[originalTypeId].params;
|
|
327
|
+
const dupTypeParams = this.types[dupTypeId].params;
|
|
384
328
|
const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
|
|
385
329
|
// TODO make sure these suffix is unique if a type is duplicated more than 2 times
|
|
386
330
|
if (diffParam?.typeId) {
|
|
387
|
-
const diffType = types[diffParam.typeId];
|
|
331
|
+
const diffType = this.types[diffParam.typeId];
|
|
388
332
|
if (diffType.path.length > 0) {
|
|
389
333
|
return stringPascalCase(diffType.path.at(-1));
|
|
390
334
|
}
|
|
@@ -422,11 +366,9 @@ export class TypesGen {
|
|
|
422
366
|
this.typeImports.addCodecType(typeName);
|
|
423
367
|
return;
|
|
424
368
|
}
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
this.typeImports.addOutType(typeName);
|
|
430
|
-
}
|
|
369
|
+
this.typeImports.addOutType(typeName);
|
|
370
|
+
}
|
|
371
|
+
getEnumOptions(_typeId) {
|
|
372
|
+
return { tagKey: 'tag', valueKey: 'value' };
|
|
431
373
|
}
|
|
432
374
|
}
|
|
@@ -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
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as $ from '@dedot/shape';
|
|
2
|
+
import { AnyShape } from '@dedot/shape';
|
|
3
|
+
export type CodecName = `$${string}`;
|
|
4
|
+
export interface CodecType {
|
|
5
|
+
name: CodecName;
|
|
6
|
+
$codec: AnyShape;
|
|
7
|
+
typeIn: string;
|
|
8
|
+
typeOut: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const normalizeCodecName: (name: string | CodecName) => CodecName;
|
|
11
|
+
/**
|
|
12
|
+
* Collection of codec types with loose input types
|
|
13
|
+
*
|
|
14
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
15
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
16
|
+
*
|
|
17
|
+
* This registry keep track the list of codecs which follow this convention
|
|
18
|
+
*/
|
|
19
|
+
export declare const looseTypeCodecs: Record<string, AnyShape>;
|
|
20
|
+
export declare function findKnownCodecType(name: string): CodecType;
|
|
21
|
+
export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
|
|
22
|
+
export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
|
|
23
|
+
export declare function checkKnownCodecType(path: string | string[]): [boolean, string];
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import * as Codecs from '@dedot/codecs';
|
|
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
|
+
import { assert } from '@dedot/utils';
|
|
5
|
+
export const normalizeCodecName = (name) => {
|
|
6
|
+
return name.startsWith('$') ? name : `$${name}`;
|
|
7
|
+
};
|
|
8
|
+
// Known paths for codecs (primitives) that are shared between
|
|
9
|
+
// different substrate-based blockchains
|
|
10
|
+
const KNOWN_PATHS = [
|
|
11
|
+
'sp_core::crypto::AccountId32',
|
|
12
|
+
'sp_runtime::generic::era::Era',
|
|
13
|
+
'sp_runtime::multiaddress::MultiAddress',
|
|
14
|
+
/^sp_runtime::DispatchError$/,
|
|
15
|
+
'sp_runtime::ModuleError',
|
|
16
|
+
'sp_runtime::TokenError',
|
|
17
|
+
'sp_arithmetic::ArithmeticError',
|
|
18
|
+
'sp_runtime::TransactionalError',
|
|
19
|
+
'frame_support::dispatch::DispatchInfo',
|
|
20
|
+
'frame_system::Phase',
|
|
21
|
+
'sp_version::RuntimeVersion',
|
|
22
|
+
'fp_account::AccountId20',
|
|
23
|
+
'account::AccountId20',
|
|
24
|
+
'polkadot_runtime_common::claims::EthereumAddress',
|
|
25
|
+
'pallet_identity::types::Data',
|
|
26
|
+
'sp_runtime::generic::digest::Digest',
|
|
27
|
+
'sp_runtime::generic::digest::DigestItem',
|
|
28
|
+
'sp_runtime::generic::header::Header',
|
|
29
|
+
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
30
|
+
/^primitive_types::\w+$/,
|
|
31
|
+
/^sp_arithmetic::per_things::\w+$/,
|
|
32
|
+
/^sp_arithmetic::fixed_point::\w+$/,
|
|
33
|
+
'ink_primitives::types::Hash',
|
|
34
|
+
{ path: 'ink_primitives::types::AccountId', codec: 'AccountId32' },
|
|
35
|
+
];
|
|
36
|
+
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
37
|
+
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
38
|
+
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
39
|
+
/**
|
|
40
|
+
* Collection of codec types with loose input types
|
|
41
|
+
*
|
|
42
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
43
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
44
|
+
*
|
|
45
|
+
* This registry keep track the list of codecs which follow this convention
|
|
46
|
+
*/
|
|
47
|
+
export const looseTypeCodecs = {
|
|
48
|
+
$AccountId20,
|
|
49
|
+
$EthereumAddress,
|
|
50
|
+
$AccountId32,
|
|
51
|
+
$ConsensusEngineId,
|
|
52
|
+
$StorageKey,
|
|
53
|
+
$StorageData,
|
|
54
|
+
$PrefixedStorageKey,
|
|
55
|
+
$Bytes,
|
|
56
|
+
$RawBytes,
|
|
57
|
+
$MultiAddress,
|
|
58
|
+
$OpaqueExtrinsic,
|
|
59
|
+
$UncheckedExtrinsic,
|
|
60
|
+
$Era,
|
|
61
|
+
};
|
|
62
|
+
export function findKnownCodecType(name) {
|
|
63
|
+
const normalizedName = normalizeCodecName(name);
|
|
64
|
+
const $knownCodec = looseTypeCodecs[normalizedName];
|
|
65
|
+
if ($knownCodec) {
|
|
66
|
+
return {
|
|
67
|
+
name: normalizedName,
|
|
68
|
+
$codec: $knownCodec,
|
|
69
|
+
typeIn: `${name}Like`,
|
|
70
|
+
typeOut: name,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
const $codec = findKnownCodec(name);
|
|
74
|
+
if ($codec.nativeType && $[name]) {
|
|
75
|
+
return {
|
|
76
|
+
name: normalizedName,
|
|
77
|
+
$codec,
|
|
78
|
+
typeIn: $codec.nativeType,
|
|
79
|
+
typeOut: $codec.nativeType,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
name: normalizedName,
|
|
84
|
+
$codec,
|
|
85
|
+
typeIn: name,
|
|
86
|
+
typeOut: name,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
export function findKnownCodec(typeName) {
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[normalizeCodecName(typeName)] || $[typeName];
|
|
92
|
+
assert($codec, `Known codec not found - ${typeName}`);
|
|
93
|
+
return $codec;
|
|
94
|
+
}
|
|
95
|
+
export function findKnownWrapperCodec(typeName) {
|
|
96
|
+
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
97
|
+
if (matchNames) {
|
|
98
|
+
const [_, wrapper, inner] = matchNames;
|
|
99
|
+
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
100
|
+
// @ts-ignore
|
|
101
|
+
const $Wrapper = $[wrapper];
|
|
102
|
+
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
103
|
+
return $Wrapper(findKnownWrapperCodec(inner));
|
|
104
|
+
}
|
|
105
|
+
const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
|
|
106
|
+
return $Wrapper(...$inners);
|
|
107
|
+
}
|
|
108
|
+
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
109
|
+
}
|
|
110
|
+
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
111
|
+
const $inner = typeName
|
|
112
|
+
.slice(1, -1)
|
|
113
|
+
.split(',')
|
|
114
|
+
.filter((x) => x)
|
|
115
|
+
.map((one) => findKnownCodec(one.trim()));
|
|
116
|
+
return $.Tuple(...$inner);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
export function checkKnownCodecType(path) {
|
|
120
|
+
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
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];
|
|
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');
|
|
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
|
+
}
|