@dedot/codegen 0.0.1-next.c3d95ac1.3 → 0.0.1-next.cb041b89.17

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.
Files changed (130) hide show
  1. package/{generator → chaintypes/generator}/ApiGen.d.ts +4 -4
  2. package/{generator → chaintypes/generator}/ConstsGen.d.ts +1 -1
  3. package/{generator → chaintypes/generator}/ConstsGen.js +5 -5
  4. package/{generator → chaintypes/generator}/ErrorsGen.d.ts +1 -1
  5. package/{generator → chaintypes/generator}/ErrorsGen.js +8 -8
  6. package/{generator → chaintypes/generator}/EventsGen.d.ts +1 -1
  7. package/{generator → chaintypes/generator}/EventsGen.js +8 -8
  8. package/{generator → chaintypes/generator}/IndexGen.d.ts +1 -1
  9. package/chaintypes/generator/IndexGen.js +17 -0
  10. package/{generator → chaintypes/generator}/JsonRpcGen.d.ts +3 -2
  11. package/{generator → chaintypes/generator}/JsonRpcGen.js +8 -8
  12. package/{generator → chaintypes/generator}/QueryGen.d.ts +1 -1
  13. package/{generator → chaintypes/generator}/QueryGen.js +21 -12
  14. package/{generator → chaintypes/generator}/RuntimeApisGen.d.ts +2 -2
  15. package/{generator → chaintypes/generator}/RuntimeApisGen.js +11 -11
  16. package/{generator → chaintypes/generator}/TxGen.d.ts +1 -1
  17. package/{generator → chaintypes/generator}/TxGen.js +13 -11
  18. package/chaintypes/generator/TypesGen.d.ts +11 -0
  19. package/chaintypes/generator/TypesGen.js +55 -0
  20. package/chaintypes/index.d.ts +3 -0
  21. package/chaintypes/index.js +49 -0
  22. package/chaintypes/templates/consts.hbs +7 -0
  23. package/chaintypes/templates/errors.hbs +7 -0
  24. package/chaintypes/templates/events.hbs +7 -0
  25. package/chaintypes/templates/index.hbs +27 -0
  26. package/chaintypes/templates/json-rpc.hbs +5 -0
  27. package/chaintypes/templates/query.hbs +7 -0
  28. package/chaintypes/templates/runtime.hbs +8 -0
  29. package/chaintypes/templates/tx.hbs +9 -0
  30. package/{templates → chaintypes/templates}/types.hbs +1 -1
  31. package/cjs/{generator → chaintypes/generator}/ConstsGen.js +5 -5
  32. package/cjs/{generator → chaintypes/generator}/ErrorsGen.js +8 -8
  33. package/cjs/{generator → chaintypes/generator}/EventsGen.js +8 -8
  34. package/cjs/chaintypes/generator/IndexGen.js +21 -0
  35. package/cjs/{generator → chaintypes/generator}/JsonRpcGen.js +10 -10
  36. package/cjs/{generator → chaintypes/generator}/QueryGen.js +21 -12
  37. package/cjs/{generator → chaintypes/generator}/RuntimeApisGen.js +14 -14
  38. package/cjs/{generator → chaintypes/generator}/TxGen.js +13 -11
  39. package/cjs/chaintypes/generator/TypesGen.js +59 -0
  40. package/cjs/chaintypes/index.js +77 -0
  41. package/cjs/chaintypes/templates/consts.hbs +7 -0
  42. package/cjs/chaintypes/templates/errors.hbs +7 -0
  43. package/cjs/chaintypes/templates/events.hbs +7 -0
  44. package/cjs/chaintypes/templates/index.hbs +27 -0
  45. package/cjs/chaintypes/templates/json-rpc.hbs +5 -0
  46. package/cjs/chaintypes/templates/query.hbs +7 -0
  47. package/cjs/chaintypes/templates/runtime.hbs +8 -0
  48. package/cjs/chaintypes/templates/tx.hbs +9 -0
  49. package/cjs/{templates → chaintypes/templates}/types.hbs +1 -1
  50. package/cjs/index.js +4 -62
  51. package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +88 -140
  52. package/cjs/{generator → shared}/TypeImports.js +27 -9
  53. package/cjs/shared/index.js +19 -0
  54. package/cjs/{generator → shared}/known-codecs.js +24 -5
  55. package/cjs/typink/generator/ConstructorQueryGen.js +24 -0
  56. package/cjs/typink/generator/ConstructorTxGen.js +24 -0
  57. package/cjs/typink/generator/IndexGen.js +24 -0
  58. package/cjs/typink/generator/QueryGen.js +72 -0
  59. package/cjs/typink/generator/TxGen.js +25 -0
  60. package/cjs/typink/generator/TypesGen.js +35 -0
  61. package/cjs/typink/generator/index.js +22 -0
  62. package/cjs/typink/index.js +37 -0
  63. package/cjs/typink/templates/constructor-query.hbs +7 -0
  64. package/cjs/typink/templates/constructor-tx.hbs +7 -0
  65. package/cjs/typink/templates/index.hbs +14 -0
  66. package/cjs/typink/templates/query.hbs +7 -0
  67. package/cjs/typink/templates/tx.hbs +7 -0
  68. package/cjs/typink/templates/types.hbs +5 -0
  69. package/cjs/{generator/utils.js → utils.js} +3 -3
  70. package/index.d.ts +2 -3
  71. package/index.js +2 -48
  72. package/package.json +16 -18
  73. package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +12 -11
  74. package/{generator/TypesGen.js → shared/BaseTypesGen.js} +85 -137
  75. package/{generator → shared}/TypeImports.d.ts +12 -3
  76. package/{generator → shared}/TypeImports.js +27 -9
  77. package/shared/index.d.ts +3 -0
  78. package/shared/index.js +3 -0
  79. package/{generator → shared}/known-codecs.d.ts +1 -1
  80. package/{generator → shared}/known-codecs.js +22 -3
  81. package/typink/generator/ConstructorQueryGen.d.ts +6 -0
  82. package/typink/generator/ConstructorQueryGen.js +20 -0
  83. package/typink/generator/ConstructorTxGen.d.ts +6 -0
  84. package/typink/generator/ConstructorTxGen.js +20 -0
  85. package/typink/generator/IndexGen.d.ts +6 -0
  86. package/typink/generator/IndexGen.js +20 -0
  87. package/typink/generator/QueryGen.d.ts +12 -0
  88. package/typink/generator/QueryGen.js +68 -0
  89. package/typink/generator/TxGen.d.ts +6 -0
  90. package/typink/generator/TxGen.js +21 -0
  91. package/typink/generator/TypesGen.d.ts +8 -0
  92. package/typink/generator/TypesGen.js +31 -0
  93. package/typink/generator/index.d.ts +6 -0
  94. package/typink/generator/index.js +6 -0
  95. package/typink/index.d.ts +2 -0
  96. package/typink/index.js +30 -0
  97. package/typink/templates/constructor-query.hbs +7 -0
  98. package/typink/templates/constructor-tx.hbs +7 -0
  99. package/typink/templates/index.hbs +14 -0
  100. package/typink/templates/query.hbs +7 -0
  101. package/typink/templates/tx.hbs +7 -0
  102. package/typink/templates/types.hbs +5 -0
  103. package/{generator/utils.d.ts → utils.d.ts} +1 -1
  104. package/{generator/utils.js → utils.js} +3 -3
  105. package/cjs/generator/IndexGen.js +0 -17
  106. package/cjs/templates/consts.hbs +0 -7
  107. package/cjs/templates/errors.hbs +0 -7
  108. package/cjs/templates/events.hbs +0 -7
  109. package/cjs/templates/index.hbs +0 -22
  110. package/cjs/templates/json-rpc.hbs +0 -5
  111. package/cjs/templates/query.hbs +0 -7
  112. package/cjs/templates/runtime.hbs +0 -8
  113. package/cjs/templates/tx.hbs +0 -9
  114. package/generator/IndexGen.js +0 -13
  115. package/templates/consts.hbs +0 -7
  116. package/templates/errors.hbs +0 -7
  117. package/templates/events.hbs +0 -7
  118. package/templates/index.hbs +0 -22
  119. package/templates/json-rpc.hbs +0 -5
  120. package/templates/query.hbs +0 -7
  121. package/templates/runtime.hbs +0 -8
  122. package/templates/tx.hbs +0 -9
  123. /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
  124. /package/{generator → chaintypes/generator}/index.d.ts +0 -0
  125. /package/{generator → chaintypes/generator}/index.js +0 -0
  126. /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
  127. /package/cjs/{generator → chaintypes/generator}/index.js +0 -0
  128. /package/cjs/{generator/dirname.js → dirname.js} +0 -0
  129. /package/{generator/dirname.d.ts → dirname.d.ts} +0 -0
  130. /package/{generator/dirname.js → dirname.js} +0 -0
@@ -1,4 +1,4 @@
1
- // Generated by @dedot/codegen
1
+ // Generated by dedot cli
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
package/cjs/index.js CHANGED
@@ -10,67 +10,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
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);
24
15
  };
25
16
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.generateTypes = exports.generateTypesFromEndpoint = void 0;
27
- const dedot_1 = require("dedot");
28
- const fs = __importStar(require("fs"));
29
- const path = __importStar(require("path"));
30
- const index_js_1 = require("./generator/index.js");
31
- const utils_1 = require("@dedot/utils");
32
- async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
33
- const api = await dedot_1.Dedot.new(endpoint);
34
- const { methods } = await api.rpc.rpc_methods();
35
- const apis = api.runtimeVersion.apis || {};
36
- if (!chain) {
37
- chain = (0, utils_1.stringCamelCase)(api.runtimeVersion.specName || api.runtimeChain || 'local');
38
- }
39
- await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
40
- await api.disconnect();
41
- }
42
- exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
43
- async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
44
- const dirPath = path.resolve(outDir, chain);
45
- const defTypesFileName = path.join(dirPath, `types.${extension}`);
46
- const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
47
- const queryTypesFileName = path.join(dirPath, `query.${extension}`);
48
- const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
49
- const indexFileName = path.join(dirPath, `index.${extension}`);
50
- const errorsFileName = path.join(dirPath, `errors.${extension}`);
51
- const eventsFileName = path.join(dirPath, `events.${extension}`);
52
- const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
53
- const txFileName = path.join(dirPath, `tx.${extension}`);
54
- if (!fs.existsSync(dirPath)) {
55
- fs.mkdirSync(dirPath, { recursive: true });
56
- }
57
- const typesGen = new index_js_1.TypesGen(metadata);
58
- const constsGen = new index_js_1.ConstsGen(typesGen);
59
- const queryGen = new index_js_1.QueryGen(typesGen);
60
- const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
61
- const indexGen = new index_js_1.IndexGen(chain);
62
- const errorsGen = new index_js_1.ErrorsGen(typesGen);
63
- const eventsGen = new index_js_1.EventsGen(typesGen);
64
- const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
65
- const txGen = new index_js_1.TxGen(typesGen);
66
- fs.writeFileSync(defTypesFileName, await typesGen.generate());
67
- fs.writeFileSync(errorsFileName, await errorsGen.generate());
68
- fs.writeFileSync(eventsFileName, await eventsGen.generate());
69
- fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
70
- fs.writeFileSync(queryTypesFileName, await queryGen.generate());
71
- fs.writeFileSync(constsTypesFileName, await constsGen.generate());
72
- fs.writeFileSync(txFileName, await txGen.generate());
73
- fs.writeFileSync(indexFileName, await indexGen.generate());
74
- fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate());
75
- }
76
- exports.generateTypes = generateTypes;
17
+ __exportStar(require("./chaintypes/index.js"), exports);
18
+ __exportStar(require("./typink/index.js"), exports);
@@ -1,68 +1,93 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
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("./utils.js");
5
+ const utils_js_1 = require("../utils.js");
7
6
  const TypeImports_js_1 = require("./TypeImports.js");
8
7
  const known_codecs_js_1 = require("./known-codecs.js");
9
- // Skip generate types for these
10
- // as we do have native types for them
11
- const SKIP_TYPES = [
12
- 'BoundedBTreeMap',
13
- 'BoundedBTreeSet',
14
- 'BoundedVec',
15
- 'Box',
16
- 'BTreeMap',
17
- 'BTreeSet',
18
- 'Cow',
19
- 'Option',
20
- 'Range',
21
- 'RangeInclusive',
22
- 'Result',
23
- 'WeakBoundedVec',
24
- 'WrapperKeepOpaque',
25
- 'WrapperOpaque',
26
- ];
27
8
  // These are common & generic types, so we'll remove these from all paths at index 1
28
9
  // This helps make the type name shorter
29
10
  const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
30
11
  exports.BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
31
- const WRAPPER_TYPE_REGEX = /^(\w+)(<.*>)$/g;
32
- class TypesGen {
33
- metadata;
34
- /**
35
- * Types will be generated its definition out.
36
- */
12
+ class BaseTypesGen {
13
+ types;
37
14
  includedTypes;
38
- registry;
39
15
  typeImports;
40
- constructor(metadata) {
41
- this.metadata = metadata;
42
- this.registry = new codecs_1.PortableRegistry(this.metadata);
43
- this.includedTypes = this.#includedTypes();
16
+ skipTypes;
17
+ constructor(types) {
18
+ this.types = types;
19
+ this.includedTypes = {};
44
20
  this.typeImports = new TypeImports_js_1.TypeImports();
45
- }
46
- generate() {
47
- this.clearCache();
48
- let defTypeOut = '';
49
- Object.values(this.includedTypes)
50
- .filter(({ skip, knownType }) => !(skip || knownType))
51
- .forEach(({ name, nameOut, id, docs }) => {
52
- defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
53
- if (this.#shouldGenerateTypeIn(id)) {
54
- defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
55
- }
56
- });
57
- const importTypes = this.typeImports.toImports('./types');
58
- const template = (0, utils_js_1.compileTemplate)('types.hbs');
59
- return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
21
+ this.skipTypes = [];
60
22
  }
61
23
  typeCache = {};
62
24
  clearCache() {
63
25
  this.typeCache = {};
64
26
  this.typeImports.clear();
65
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] = (0, known_codecs_js_1.checkKnownCodecType)(joinedPath);
63
+ if (isKnownCodecType) {
64
+ const codecType = (0, known_codecs_js_1.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
+ }
66
91
  generateType(typeId, nestedLevel = 0, typeOut = false) {
67
92
  if (nestedLevel > 0) {
68
93
  const includedDef = this.includedTypes[typeId];
@@ -93,7 +118,7 @@ class TypesGen {
93
118
  return type;
94
119
  }
95
120
  #generateType(typeId, nestedLevel = 0, typeOut = false) {
96
- const def = this.metadata.types[typeId];
121
+ const def = this.types[typeId];
97
122
  if (!def) {
98
123
  throw new Error(`Type def not found ${JSON.stringify(def)}`);
99
124
  }
@@ -170,7 +195,7 @@ class TypesGen {
170
195
  membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
171
196
  }
172
197
  }
173
- const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
198
+ const { tagKey, valueKey } = this.getEnumOptions(typeId);
174
199
  return membersType
175
200
  .map(([keyName, valueType, docs]) => ({
176
201
  tag: `${tagKey}: '${keyName}'`,
@@ -200,7 +225,7 @@ class TypesGen {
200
225
  case 'Sequence':
201
226
  case 'SizedVec': {
202
227
  const fixedSize = tag === 'SizedVec' ? `${value.len}` : null;
203
- const $innerType = this.metadata.types[value.typeParam].type;
228
+ const $innerType = this.types[value.typeParam].type;
204
229
  if ($innerType.tag === 'Primitive' && $innerType.value.kind === 'u8') {
205
230
  return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
206
231
  }
@@ -230,98 +255,20 @@ class TypesGen {
230
255
  #isOptionalType(type) {
231
256
  return type.endsWith('| undefined');
232
257
  }
233
- #includedTypes() {
234
- const { types } = this.metadata;
235
- const pathsCount = new Map();
236
- const typesWithPath = types.filter((one) => one.path.length > 0);
237
- const skipIds = [];
238
- const typeSuffixes = new Map();
239
- typesWithPath.forEach(({ path, id }) => {
240
- const joinedPath = path.join('::');
241
- if (pathsCount.has(joinedPath)) {
242
- // We compare 2 types with the same path here,
243
- // if they are the same type -> skip the current one, keep the first occurrence
244
- // 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
245
- const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
246
- const sameType = this.typeEql(firstOccurrenceTypeId, id);
247
- if (sameType) {
248
- skipIds.push(id);
249
- }
250
- else {
251
- pathsCount.get(joinedPath).push(id);
252
- typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
253
- }
254
- }
255
- else {
256
- pathsCount.set(joinedPath, [id]);
257
- }
258
- });
259
- return typesWithPath.reduce((o, type) => {
260
- const { path, id } = type;
261
- const joinedPath = path.join('::');
262
- if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
263
- return o;
264
- }
265
- const suffix = typeSuffixes.get(id) || '';
266
- let knownType = false;
267
- let name, nameOut;
268
- if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
269
- const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
270
- name = codecType.typeIn;
271
- nameOut = codecType.typeOut;
272
- knownType = true;
273
- }
274
- else if (PATH_RM_INDEX_1.includes(path[1])) {
275
- const newPath = path.slice();
276
- newPath.splice(1, 1);
277
- name = this.#cleanPath(newPath);
278
- }
279
- else {
280
- name = this.#cleanPath(path);
281
- }
282
- if (this.#shouldGenerateTypeIn(id)) {
283
- nameOut = name;
284
- name = name.endsWith('Like') ? name : `${name}Like`;
285
- }
286
- o[id] = {
287
- name: `${name}${suffix}`,
288
- nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
289
- knownType,
290
- skip: skipIds.includes(id),
291
- ...type,
292
- };
293
- return o;
294
- }, {});
295
- }
296
258
  #removeGenericPart(typeName) {
297
- if (typeName.match(WRAPPER_TYPE_REGEX)) {
298
- return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
259
+ if (typeName.match(utils_js_1.WRAPPER_TYPE_REGEX)) {
260
+ return typeName.replace(utils_js_1.WRAPPER_TYPE_REGEX, (_, $1) => $1);
299
261
  }
300
262
  else {
301
263
  return typeName;
302
264
  }
303
265
  }
304
- /**
305
- * @description Remove duplicated part of the path
306
- *
307
- * Example:
308
- * ["pallet_staking", "pallet", "pallet", "Event"]
309
- * => ["pallet_staking", "pallet", "Event"]
310
- *
311
- * @param path
312
- * @private
313
- */
314
- #cleanPath(path) {
266
+ cleanPath(path) {
315
267
  return path
316
268
  .map((one) => (0, utils_1.stringPascalCase)(one))
317
269
  .filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
318
270
  .join('');
319
271
  }
320
- #shouldGenerateTypeIn(id) {
321
- const { callTypeId } = this.metadata.extrinsic;
322
- const palletCallTypeIds = this.registry.getPalletCallTypeIds();
323
- return callTypeId === id || palletCallTypeIds.includes(id);
324
- }
325
272
  eqlCache = new Map();
326
273
  typeEql(idA, idB, level = 0) {
327
274
  const cacheKey = `${idA}==${idB}`;
@@ -334,9 +281,8 @@ class TypesGen {
334
281
  #typeEql(idA, idB, lvl = 0) {
335
282
  if (idA === idB)
336
283
  return true;
337
- const { types } = this.metadata;
338
- const typeA = types[idA];
339
- const typeB = types[idB];
284
+ const typeA = this.types[idA];
285
+ const typeB = this.types[idB];
340
286
  if (typeA.path.join('::') !== typeB.path.join('::'))
341
287
  return false;
342
288
  const { type: defA, params: paramsA } = typeA;
@@ -379,14 +325,13 @@ class TypesGen {
379
325
  (param1.typeId === undefined) === (param2.typeId === undefined) &&
380
326
  (param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
381
327
  }
382
- #extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
383
- const { types } = this.metadata;
384
- const originalTypeParams = types[originalTypeId].params;
385
- const dupTypeParams = types[dupTypeId].params;
328
+ extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
329
+ const originalTypeParams = this.types[originalTypeId].params;
330
+ const dupTypeParams = this.types[dupTypeId].params;
386
331
  const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
387
332
  // TODO make sure these suffix is unique if a type is duplicated more than 2 times
388
333
  if (diffParam?.typeId) {
389
- const diffType = types[diffParam.typeId];
334
+ const diffType = this.types[diffParam.typeId];
390
335
  if (diffType.path.length > 0) {
391
336
  return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
392
337
  }
@@ -426,5 +371,8 @@ class TypesGen {
426
371
  }
427
372
  this.typeImports.addOutType(typeName);
428
373
  }
374
+ getEnumOptions(_typeId) {
375
+ return { tagKey: 'tag', valueKey: 'value' };
376
+ }
429
377
  }
430
- exports.TypesGen = TypesGen;
378
+ exports.BaseTypesGen = BaseTypesGen;
@@ -8,29 +8,41 @@ 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
- specTypes;
11
+ knownJsonRpcTypes;
12
12
  // External types to define explicitly
13
13
  outTypes;
14
+ // Know types defined in @dedot/contracts
15
+ contractTypes;
16
+ // Know types defined in @dedot/api/chaintypes or dedot/chaintypes
17
+ chainTypes;
14
18
  constructor() {
15
19
  this.portableTypes = new Set();
16
20
  this.codecTypes = new Set();
17
21
  this.knownTypes = new Set();
18
- this.specTypes = new Set();
22
+ this.knownJsonRpcTypes = new Set();
19
23
  this.outTypes = new Set();
24
+ this.contractTypes = new Set();
25
+ this.chainTypes = new Set();
20
26
  }
21
27
  clear() {
22
28
  this.portableTypes.clear();
23
29
  this.codecTypes.clear();
24
30
  this.knownTypes.clear();
25
- this.specTypes.clear();
31
+ this.knownJsonRpcTypes.clear();
26
32
  this.outTypes.clear();
33
+ this.contractTypes.clear();
34
+ this.chainTypes.clear();
27
35
  }
28
- toImports(...excludeModules) {
36
+ toImports(config) {
37
+ const { excludeModules = [], useSubPaths = false } = config || {};
29
38
  // TODO generate outTypes!
39
+ const prefix = useSubPaths ? '' : '@';
30
40
  const toImports = [
31
- [this.knownTypes, '@dedot/types'],
32
- [this.specTypes, '@dedot/specs'],
33
- [this.codecTypes, '@dedot/codecs'],
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/contracts'],
34
46
  [this.portableTypes, './types'],
35
47
  ];
36
48
  return toImports
@@ -54,11 +66,17 @@ class TypeImports {
54
66
  addKnownType(...types) {
55
67
  types.forEach((one) => this.knownTypes.add(one));
56
68
  }
57
- addSpecType(...types) {
58
- types.forEach((one) => this.specTypes.add(one));
69
+ addKnownJsonRpcType(...types) {
70
+ types.forEach((one) => this.knownJsonRpcTypes.add(one));
59
71
  }
60
72
  addOutType(...types) {
61
73
  types.forEach((one) => this.outTypes.add(one));
62
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
+ }
63
81
  }
64
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.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
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 isKnownCodecType(path) {
149
+ function checkKnownCodecType(path) {
148
150
  const joinedPath = Array.isArray(path) ? path.join('::') : path;
149
- return KNOWN_PATHS.some((one) => joinedPath.match(one));
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.isKnownCodecType = isKnownCodecType;
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', 'ConstructorResult');
11
+ const { constructors } = this.contractMetadata.spec;
12
+ const constructorsOut = this.doGenerate(constructors);
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<ConstructorResult<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', 'GenericConstructorSubmittableExtrinsic');
11
+ const { constructors } = this.contractMetadata.spec;
12
+ const constructorsOut = this.doGenerate(constructors);
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) => GenericConstructorSubmittableExtrinsic<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');
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;