@dedot/codegen 0.0.1-next.41a5fa17.16 → 0.0.1-next.4deddfff.34

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 (69) hide show
  1. package/cjs/generator/ConstsGen.js +3 -3
  2. package/cjs/generator/ErrorsGen.js +4 -4
  3. package/cjs/generator/EventsGen.js +4 -4
  4. package/cjs/generator/IndexGen.js +5 -6
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +19 -10
  7. package/cjs/generator/RuntimeApisGen.js +91 -16
  8. package/cjs/generator/TxGen.js +9 -7
  9. package/cjs/generator/TypeImports.js +8 -0
  10. package/cjs/generator/TypesGen.js +2 -8
  11. package/cjs/generator/index.js +1 -1
  12. package/cjs/generator/known-codecs.js +1 -1
  13. package/cjs/index.js +24 -24
  14. package/cjs/templates/consts.hbs +1 -1
  15. package/cjs/templates/errors.hbs +1 -1
  16. package/cjs/templates/events.hbs +1 -1
  17. package/cjs/templates/index.hbs +15 -10
  18. package/cjs/templates/json-rpc.hbs +5 -0
  19. package/cjs/templates/query.hbs +1 -1
  20. package/cjs/templates/runtime.hbs +1 -1
  21. package/cjs/templates/tx.hbs +1 -1
  22. package/generator/ApiGen.d.ts +3 -3
  23. package/generator/ConstsGen.d.ts +1 -1
  24. package/generator/ConstsGen.js +2 -2
  25. package/generator/ErrorsGen.js +4 -4
  26. package/generator/EventsGen.js +4 -4
  27. package/generator/IndexGen.d.ts +2 -3
  28. package/generator/IndexGen.js +5 -6
  29. package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
  30. package/generator/JsonRpcGen.js +55 -0
  31. package/generator/QueryGen.d.ts +1 -1
  32. package/generator/QueryGen.js +18 -9
  33. package/generator/RuntimeApisGen.d.ts +4 -4
  34. package/generator/RuntimeApisGen.js +91 -16
  35. package/generator/TxGen.js +9 -7
  36. package/generator/TypeImports.d.ts +2 -0
  37. package/generator/TypeImports.js +8 -0
  38. package/generator/TypesGen.d.ts +1 -1
  39. package/generator/TypesGen.js +2 -8
  40. package/generator/index.d.ts +1 -1
  41. package/generator/index.js +1 -1
  42. package/generator/known-codecs.js +1 -1
  43. package/index.d.ts +2 -3
  44. package/index.js +23 -23
  45. package/package.json +11 -15
  46. package/templates/consts.hbs +1 -1
  47. package/templates/errors.hbs +1 -1
  48. package/templates/events.hbs +1 -1
  49. package/templates/index.hbs +15 -10
  50. package/templates/json-rpc.hbs +5 -0
  51. package/templates/query.hbs +1 -1
  52. package/templates/runtime.hbs +1 -1
  53. package/templates/tx.hbs +1 -1
  54. package/cjs/genSupportedChainTypes.js +0 -83
  55. package/cjs/generator/RpcGen.js +0 -175
  56. package/cjs/generator/known-types.js +0 -33
  57. package/cjs/packageInfo.js +0 -5
  58. package/cjs/templates/rpc.hbs +0 -7
  59. package/cjs/types.js +0 -2
  60. package/genSupportedChainTypes.d.ts +0 -1
  61. package/genSupportedChainTypes.js +0 -78
  62. package/generator/RpcGen.js +0 -171
  63. package/generator/known-types.d.ts +0 -6
  64. package/generator/known-types.js +0 -30
  65. package/packageInfo.d.ts +0 -4
  66. package/packageInfo.js +0 -2
  67. package/templates/rpc.hbs +0 -7
  68. package/types.d.ts +0 -6
  69. package/types.js +0 -1
@@ -2,13 +2,13 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstsGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
- const index_js_1 = require("../generator/index.js");
5
+ const ApiGen_js_1 = require("./ApiGen.js");
6
6
  const utils_js_1 = require("./utils.js");
7
- class ConstsGen extends index_js_1.ApiGen {
7
+ class ConstsGen extends ApiGen_js_1.ApiGen {
8
8
  generate() {
9
9
  const { pallets } = this.metadata;
10
10
  this.typesGen.clearCache();
11
- this.typesGen.typeImports.addKnownType('GenericChainConsts');
11
+ this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
12
12
  let defTypeOut = '';
13
13
  for (let pallet of pallets) {
14
14
  const typedConstants = pallet.constants.map((one) => ({
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ErrorsGen = void 0;
4
+ const utils_1 = require("@dedot/utils");
4
5
  const ApiGen_js_1 = require("./ApiGen.js");
5
6
  const utils_js_1 = require("./utils.js");
6
- const utils_1 = require("@dedot/utils");
7
7
  class ErrorsGen extends ApiGen_js_1.ApiGen {
8
8
  generate() {
9
9
  const { pallets } = this.metadata;
10
10
  this.typesGen.clearCache();
11
- this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
11
+ this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
12
12
  let defTypeOut = '';
13
13
  for (let pallet of pallets) {
14
14
  const errorTypeId = pallet.error;
@@ -19,10 +19,10 @@ class ErrorsGen extends ApiGen_js_1.ApiGen {
19
19
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
20
20
  defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
21
21
  ${errorDefs
22
- .map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError`)
22
+ .map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError<Rv>`)
23
23
  .join(',\n')}
24
24
 
25
- ${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError,
25
+ ${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
26
26
  },`;
27
27
  }
28
28
  const importTypes = this.typesGen.typeImports.toImports();
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.EventsGen = void 0;
4
+ const utils_1 = require("@dedot/utils");
4
5
  const ApiGen_js_1 = require("./ApiGen.js");
5
6
  const utils_js_1 = require("./utils.js");
6
- const utils_1 = require("@dedot/utils");
7
7
  class EventsGen extends ApiGen_js_1.ApiGen {
8
8
  generate() {
9
9
  const { pallets } = this.metadata;
10
10
  this.typesGen.clearCache();
11
- this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
11
+ this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
12
12
  let defTypeOut = '';
13
13
  for (let pallet of pallets) {
14
14
  const eventTypeId = pallet.event;
@@ -31,10 +31,10 @@ class EventsGen extends ApiGen_js_1.ApiGen {
31
31
  }
32
32
  return { ...def, genericParts };
33
33
  })
34
- .map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
34
+ .map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
35
35
  .join(',\n')}
36
36
 
37
- ${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent,
37
+ ${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
38
38
  },`;
39
39
  }
40
40
  const importTypes = this.typesGen.typeImports.toImports();
@@ -1,16 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.IndexGen = void 0;
4
- const utils_js_1 = require("./utils.js");
5
4
  const utils_1 = require("@dedot/utils");
5
+ const utils_js_1 = require("./utils.js");
6
6
  class IndexGen {
7
- networkInfo;
8
- constructor(networkInfo) {
9
- this.networkInfo = networkInfo;
7
+ chain;
8
+ constructor(chain) {
9
+ this.chain = chain;
10
10
  }
11
11
  async generate() {
12
- const { chain } = this.networkInfo;
13
- const interfaceName = (0, utils_1.stringPascalCase)(chain);
12
+ const interfaceName = (0, utils_1.stringPascalCase)(this.chain);
14
13
  const template = (0, utils_js_1.compileTemplate)('index.hbs');
15
14
  return (0, utils_js_1.beautifySourceCode)(template({ interfaceName }));
16
15
  }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JsonRpcGen = void 0;
4
+ const specs_1 = require("@dedot/specs");
5
+ const index_js_1 = require("../generator/index.js");
6
+ const utils_js_1 = require("./utils.js");
7
+ const HIDDEN_RPCS = [
8
+ // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
9
+ 'rpc_methods',
10
+ ];
11
+ const ALIAS_RPCS = [
12
+ // TODO include these into the specs
13
+ 'account_nextIndex',
14
+ 'system_dryRunAt',
15
+ 'state_callAt',
16
+ 'state_getKeysPagedAt',
17
+ 'state_getStorageAt',
18
+ 'state_getStorageHashAt',
19
+ 'state_getStorageSizeAt',
20
+ 'chain_getRuntimeVersion',
21
+ 'chain_getHead',
22
+ 'chain_getFinalisedHead',
23
+ 'childstate_getKeysPagedAt',
24
+ // Subscription pairs
25
+ 'chain_subscribeRuntimeVersion',
26
+ 'chain_unsubscribeRuntimeVersion',
27
+ 'chain_unsubscribeNewHead',
28
+ 'chain_subscribeNewHead',
29
+ 'subscribe_newHead',
30
+ 'unsubscribe_newHead',
31
+ 'chain_subscribeFinalisedHeads',
32
+ 'chain_unsubscribeFinalisedHeads',
33
+ ];
34
+ class JsonRpcGen extends index_js_1.ApiGen {
35
+ typesGen;
36
+ rpcMethods;
37
+ constructor(typesGen, rpcMethods) {
38
+ super(typesGen);
39
+ this.typesGen = typesGen;
40
+ this.rpcMethods = rpcMethods;
41
+ HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
42
+ rpcMethods.sort();
43
+ }
44
+ generate() {
45
+ this.typesGen.clearCache();
46
+ this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
47
+ this.typesGen.typeImports.addSpecType('JsonRpcApis');
48
+ const toExclude = Object.values(specs_1.subscriptionsInfo).flat();
49
+ const importTypes = this.typesGen.typeImports.toImports();
50
+ const template = (0, utils_js_1.compileTemplate)('json-rpc.hbs');
51
+ const jsonRpcMethods = this.rpcMethods
52
+ .filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
53
+ .filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
54
+ .map((one) => `'${one}'`)
55
+ .join(' | ');
56
+ return (0, utils_js_1.beautifySourceCode)(template({ importTypes, jsonRpcMethods }));
57
+ }
58
+ }
59
+ exports.JsonRpcGen = JsonRpcGen;
@@ -2,25 +2,32 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryGen = void 0;
4
4
  const utils_1 = require("@dedot/utils");
5
- const index_js_1 = require("../generator/index.js");
5
+ const ApiGen_js_1 = require("./ApiGen.js");
6
6
  const utils_js_1 = require("./utils.js");
7
- class QueryGen extends index_js_1.ApiGen {
7
+ class QueryGen extends ApiGen_js_1.ApiGen {
8
8
  generate() {
9
9
  const { pallets } = this.metadata;
10
10
  this.typesGen.clearCache();
11
- this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
11
+ this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
12
12
  let defTypeOut = '';
13
13
  for (let pallet of pallets) {
14
14
  const storage = pallet.storage;
15
15
  if (!storage)
16
16
  continue;
17
17
  const queries = storage.entries.map((one) => this.#generateEntry(one));
18
- const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
18
+ const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
19
+ if (keyTypeOut) {
20
+ return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}, ${keyTypeOut}>`;
21
+ }
22
+ else {
23
+ return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}>`;
24
+ }
25
+ });
19
26
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
20
27
  defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
21
28
  ${queryDefs.join(',\n')}
22
29
 
23
- ${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
30
+ ${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
24
31
  },`;
25
32
  }
26
33
  const importTypes = this.typesGen.typeImports.toImports();
@@ -29,13 +36,14 @@ class QueryGen extends index_js_1.ApiGen {
29
36
  }
30
37
  #generateEntry(entry) {
31
38
  const { name, type, docs, modifier } = entry;
32
- let valueType, keyType;
39
+ let valueType, keyTypeOut, keyTypeIn;
33
40
  if (type.tag === 'Plain') {
34
41
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
35
42
  }
36
43
  else if (type.tag === 'Map') {
37
44
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
38
- keyType = this.typesGen.generateType(type.value.keyTypeId, 1);
45
+ keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
46
+ keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
39
47
  }
40
48
  else {
41
49
  throw Error('Invalid entry type!');
@@ -43,14 +51,15 @@ class QueryGen extends index_js_1.ApiGen {
43
51
  const isOptional = modifier === 'Optional';
44
52
  valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
45
53
  docs.push('\n');
46
- if (keyType) {
47
- docs.push(`@param {${keyType}} arg`);
54
+ if (keyTypeIn) {
55
+ docs.push(`@param {${keyTypeIn}} arg`);
48
56
  }
49
57
  docs.push(`@param {Callback<${valueType}> =} callback`);
50
58
  return {
51
59
  name: (0, utils_1.normalizeName)(name),
52
60
  valueType,
53
- keyType: keyType ? `arg: ${keyType}` : '',
61
+ keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
62
+ keyTypeOut,
54
63
  docs,
55
64
  };
56
65
  }
@@ -2,20 +2,21 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RuntimeApisGen = void 0;
4
4
  const specs_1 = require("@dedot/specs");
5
- const utils_js_1 = require("./utils.js");
6
5
  const utils_1 = require("@dedot/utils");
7
- const RpcGen_js_1 = require("./RpcGen.js");
8
- class RuntimeApisGen extends RpcGen_js_1.RpcGen {
6
+ const ApiGen_js_1 = require("./ApiGen.js");
7
+ const known_codecs_js_1 = require("./known-codecs.js");
8
+ const utils_js_1 = require("./utils.js");
9
+ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
9
10
  typesGen;
10
11
  runtimeApis;
11
12
  constructor(typesGen, runtimeApis) {
12
- super(typesGen, []);
13
+ super(typesGen);
13
14
  this.typesGen = typesGen;
14
15
  this.runtimeApis = runtimeApis;
15
16
  }
16
17
  generate() {
17
18
  this.typesGen.clearCache();
18
- this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
19
+ this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
19
20
  let runtimeCallsOut = '';
20
21
  if (this.metadata.apis.length > 0) {
21
22
  this.metadata.apis.forEach((runtimeApi) => {
@@ -24,7 +25,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
24
25
  runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
25
26
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
26
27
 
27
- ${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
28
+ ${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
28
29
  }`;
29
30
  });
30
31
  }
@@ -41,7 +42,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
41
42
  }))
42
43
  .join('\n')}
43
44
 
44
- ${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
45
+ ${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
45
46
  }`;
46
47
  });
47
48
  }
@@ -59,25 +60,25 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
59
60
  const { docs = [], params, type, runtimeApiName, methodName } = spec;
60
61
  const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
61
62
  const defaultDocs = [`@callname: ${callName}`];
62
- this.addTypeImport(type, false);
63
- params.forEach(({ type }) => this.addTypeImport(type));
63
+ this.#addTypeImport(type, false);
64
+ params.forEach(({ type }) => this.#addTypeImport(type));
64
65
  const typedParams = params.map((param, idx) => ({
65
66
  ...param,
66
67
  isOptional: this.#isOptionalParam(params, param.type, idx),
67
- plainType: this.getGeneratedTypeName(param.type),
68
+ plainType: this.#getGeneratedTypeName(param.type),
68
69
  }));
69
70
  const paramsOut = typedParams
70
71
  .map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
71
72
  .join(', ');
72
- const typeOut = this.getGeneratedTypeName(type, false);
73
- return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
73
+ const typeOut = this.#getGeneratedTypeName(type, false);
74
+ return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
74
75
  }
75
76
  #generateMethodDef(runtimeApiName, methodDef) {
76
77
  const { name: methodName, inputs, output, docs } = methodDef;
77
78
  const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
78
79
  const defaultDocs = [`@callname: ${callName}`];
79
80
  const typeOut = this.typesGen.generateType(output, 1, true);
80
- this.addTypeImport(typeOut, false);
81
+ this.#addTypeImport(typeOut, false);
81
82
  const typedInputs = inputs
82
83
  .map((input, idx) => ({
83
84
  ...input,
@@ -87,14 +88,14 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
87
88
  ...input,
88
89
  isOptional: this.#isOptionalParam(inputs, input.type, idx),
89
90
  }));
90
- this.addTypeImport(typedInputs.map((t) => t.type));
91
+ this.#addTypeImport(typedInputs.map((t) => t.type));
91
92
  const paramsOut = typedInputs
92
93
  .map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
93
94
  .join(', ');
94
- return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
95
+ return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
95
96
  }
96
97
  #targetRuntimeApiSpecs() {
97
- const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
98
+ const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
98
99
  const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
99
100
  if (!runtimeApiSpec)
100
101
  return;
@@ -114,5 +115,79 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
114
115
  const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calcRuntimeApiHash)(one) === runtimeApiHash);
115
116
  return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
116
117
  };
118
+ // TODO check typeIn, typeOut if param type, or rpc type isScale
119
+ #addTypeImport(type, toTypeIn = true) {
120
+ if (Array.isArray(type)) {
121
+ type.forEach((one) => this.#addTypeImport(one, toTypeIn));
122
+ return;
123
+ }
124
+ type = type.trim();
125
+ if ((0, utils_js_1.isNativeType)(type)) {
126
+ return;
127
+ }
128
+ // Handle generic wrapper types
129
+ const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
130
+ if (matchArray) {
131
+ const [_, $1, $2] = matchArray;
132
+ this.#addTypeImport($1, toTypeIn);
133
+ if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
134
+ this.#addTypeImport($2, toTypeIn);
135
+ }
136
+ else {
137
+ this.#addTypeImport($2.split(','), toTypeIn);
138
+ }
139
+ return;
140
+ }
141
+ // Check tuple type
142
+ if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
143
+ this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
144
+ return;
145
+ }
146
+ if (type.includes(' | ')) {
147
+ this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
148
+ return;
149
+ }
150
+ try {
151
+ const codecType = this.#getCodecType(type, toTypeIn);
152
+ if ((0, utils_js_1.isNativeType)(codecType))
153
+ return;
154
+ this.typesGen.typeImports.addCodecType(codecType);
155
+ return;
156
+ }
157
+ catch (e) { }
158
+ this.typesGen.addTypeImport(type);
159
+ }
160
+ #getGeneratedTypeName(type, toTypeIn = true) {
161
+ try {
162
+ const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
163
+ if (matchArray) {
164
+ const [_, $1, $2] = matchArray;
165
+ const wrapperTypeName = this.#getCodecType($1, toTypeIn);
166
+ if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
167
+ return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
168
+ }
169
+ const innerTypeNames = $2
170
+ .split(',')
171
+ .map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
172
+ .join(', ');
173
+ return `${wrapperTypeName}<${innerTypeNames}>`;
174
+ }
175
+ else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
176
+ const innerTypeNames = type
177
+ .slice(1, -1)
178
+ .split(',')
179
+ .map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
180
+ .join(', ');
181
+ return `[${innerTypeNames}]`;
182
+ }
183
+ return this.#getCodecType(type, toTypeIn);
184
+ }
185
+ catch (e) { }
186
+ return type;
187
+ }
188
+ #getCodecType(type, toTypeIn = true) {
189
+ const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
190
+ return toTypeIn ? typeIn : typeOut;
191
+ }
117
192
  }
118
193
  exports.RuntimeApisGen = RuntimeApisGen;
@@ -1,14 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TxGen = void 0;
4
- const index_js_1 = require("../generator/index.js");
5
4
  const utils_1 = require("@dedot/utils");
5
+ const index_js_1 = require("../generator/index.js");
6
6
  const utils_js_1 = require("./utils.js");
7
7
  class TxGen extends index_js_1.ApiGen {
8
8
  generate() {
9
9
  const { pallets, types } = this.metadata;
10
10
  this.typesGen.clearCache();
11
- this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall');
11
+ this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionStatusLegacy', 'TransactionStatusV2');
12
12
  const { callTypeId, addressTypeId, signatureTypeId } = this.metadata.extrinsic;
13
13
  const callTypeIn = this.typesGen.generateType(callTypeId, 1);
14
14
  const addressTypeIn = this.typesGen.generateType(addressTypeId, 1);
@@ -44,20 +44,22 @@ class TxGen extends index_js_1.ApiGen {
44
44
  txDefsOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s transaction calls`);
45
45
  txDefsOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
46
46
  ${typedTxs
47
- .map(({ functionName, params, docs, callInput }) => `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<${callInput}>>`)
47
+ .map(({ functionName, params, docs, callInput }) => `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`)
48
48
  .join(',\n')}
49
49
 
50
- ${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall>,
50
+ ${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
51
51
  },`;
52
52
  }
53
53
  const importTypes = this.typesGen.typeImports.toImports();
54
54
  // TODO make explicit separate type for Extra
55
55
  const defTypes = `
56
- export type ChainSubmittableExtrinsic<T extends IRuntimeTxCall = ${callTypeIn}> =
56
+ export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
57
57
  Extrinsic<${addressTypeIn}, T, ${signatureTypeIn}, any[]> &
58
- ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord>>
58
+ (Rv extends RpcV2
59
+ ? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionStatusV2>>
60
+ : ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionStatusLegacy>>)
59
61
 
60
- export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic;
62
+ export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
61
63
  `;
62
64
  const template = (0, utils_js_1.compileTemplate)('tx.hbs');
63
65
  return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypes, txDefsOut }));
@@ -8,30 +8,35 @@ 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
12
  // External types to define explicitly
12
13
  outTypes;
13
14
  constructor() {
14
15
  this.portableTypes = new Set();
15
16
  this.codecTypes = new Set();
16
17
  this.knownTypes = new Set();
18
+ this.specTypes = new Set();
17
19
  this.outTypes = new Set();
18
20
  }
19
21
  clear() {
20
22
  this.portableTypes.clear();
21
23
  this.codecTypes.clear();
22
24
  this.knownTypes.clear();
25
+ this.specTypes.clear();
23
26
  this.outTypes.clear();
24
27
  }
25
28
  toImports(...excludeModules) {
26
29
  // TODO generate outTypes!
27
30
  const toImports = [
28
31
  [this.knownTypes, '@dedot/types'],
32
+ [this.specTypes, '@dedot/specs'],
29
33
  [this.codecTypes, '@dedot/codecs'],
30
34
  [this.portableTypes, './types'],
31
35
  ];
32
36
  return toImports
33
37
  .filter(([_, module]) => !excludeModules.includes(module))
34
38
  .map(([types, module]) => this.#toImportLine(types, module))
39
+ .filter((line) => line.length > 0)
35
40
  .join('\n');
36
41
  }
37
42
  #toImportLine(types, module) {
@@ -49,6 +54,9 @@ class TypeImports {
49
54
  addKnownType(...types) {
50
55
  types.forEach((one) => this.knownTypes.add(one));
51
56
  }
57
+ addSpecType(...types) {
58
+ types.forEach((one) => this.specTypes.add(one));
59
+ }
52
60
  addOutType(...types) {
53
61
  types.forEach((one) => this.outTypes.add(one));
54
62
  }
@@ -3,10 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
4
4
  const codecs_1 = require("@dedot/codecs");
5
5
  const utils_1 = require("@dedot/utils");
6
- const utils_js_1 = require("./utils.js");
7
- const known_types_js_1 = require("./known-types.js");
8
6
  const TypeImports_js_1 = require("./TypeImports.js");
9
7
  const known_codecs_js_1 = require("./known-codecs.js");
8
+ const utils_js_1 = require("./utils.js");
10
9
  // Skip generate types for these
11
10
  // as we do have native types for them
12
11
  const SKIP_TYPES = [
@@ -425,12 +424,7 @@ class TypesGen {
425
424
  this.typeImports.addCodecType(typeName);
426
425
  return;
427
426
  }
428
- if (known_types_js_1.knownTypes.includes(typeName)) {
429
- this.typeImports.addKnownType(typeName);
430
- }
431
- else {
432
- this.typeImports.addOutType(typeName);
433
- }
427
+ this.typeImports.addOutType(typeName);
434
428
  }
435
429
  }
436
430
  exports.TypesGen = TypesGen;
@@ -18,7 +18,7 @@ __exportStar(require("./TypesGen.js"), exports);
18
18
  __exportStar(require("./ApiGen.js"), exports);
19
19
  __exportStar(require("./ConstsGen.js"), exports);
20
20
  __exportStar(require("./QueryGen.js"), exports);
21
- __exportStar(require("./RpcGen.js"), exports);
21
+ __exportStar(require("./JsonRpcGen.js"), exports);
22
22
  __exportStar(require("./IndexGen.js"), exports);
23
23
  __exportStar(require("./ErrorsGen.js"), exports);
24
24
  __exportStar(require("./EventsGen.js"), exports);
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
27
- const $ = __importStar(require("@dedot/shape"));
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}`;
package/cjs/index.js CHANGED
@@ -23,42 +23,42 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.generateTypes = exports.generateTypesFromChain = void 0;
26
+ exports.generateTypes = exports.generateTypesFromEndpoint = void 0;
27
+ const utils_1 = require("@dedot/utils");
27
28
  const dedot_1 = require("dedot");
28
29
  const fs = __importStar(require("fs"));
29
30
  const path = __importStar(require("path"));
30
31
  const index_js_1 = require("./generator/index.js");
31
- const utils_1 = require("@dedot/utils");
32
- async function generateTypesFromChain(network, endpoint, outDir) {
33
- const api = await dedot_1.Dedot.create(endpoint);
34
- const { methods } = await api.rpc.rpc.methods();
35
- const apis = api.runtimeVersion?.apis || [];
36
- if (!network.chain) {
37
- network.chain = (0, utils_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
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 || 'local');
38
38
  }
39
- await generateTypes(network, api.metadataLatest, methods, apis, outDir);
39
+ await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
40
40
  await api.disconnect();
41
41
  }
42
- exports.generateTypesFromChain = generateTypesFromChain;
43
- async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir = '.') {
44
- const dirPath = path.resolve(outDir, network.chain);
45
- const defTypesFileName = path.join(dirPath, `types.d.ts`);
46
- const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
47
- const queryTypesFileName = path.join(dirPath, `query.d.ts`);
48
- const rpcCallsFileName = path.join(dirPath, `rpc.d.ts`);
49
- const indexFileName = path.join(dirPath, `index.d.ts`);
50
- const errorsFileName = path.join(dirPath, `errors.d.ts`);
51
- const eventsFileName = path.join(dirPath, `events.d.ts`);
52
- const runtimeApisFileName = path.join(dirPath, `runtime.d.ts`);
53
- const txFileName = path.join(dirPath, `tx.d.ts`);
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
54
  if (!fs.existsSync(dirPath)) {
55
55
  fs.mkdirSync(dirPath, { recursive: true });
56
56
  }
57
57
  const typesGen = new index_js_1.TypesGen(metadata);
58
58
  const constsGen = new index_js_1.ConstsGen(typesGen);
59
59
  const queryGen = new index_js_1.QueryGen(typesGen);
60
- const rpcGen = new index_js_1.RpcGen(typesGen, rpcMethods);
61
- const indexGen = new index_js_1.IndexGen(network);
60
+ const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
61
+ const indexGen = new index_js_1.IndexGen(chain);
62
62
  const errorsGen = new index_js_1.ErrorsGen(typesGen);
63
63
  const eventsGen = new index_js_1.EventsGen(typesGen);
64
64
  const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
@@ -66,7 +66,7 @@ async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir
66
66
  fs.writeFileSync(defTypesFileName, await typesGen.generate());
67
67
  fs.writeFileSync(errorsFileName, await errorsGen.generate());
68
68
  fs.writeFileSync(eventsFileName, await eventsGen.generate());
69
- fs.writeFileSync(rpcCallsFileName, await rpcGen.generate());
69
+ fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
70
70
  fs.writeFileSync(queryTypesFileName, await queryGen.generate());
71
71
  fs.writeFileSync(constsTypesFileName, await constsGen.generate());
72
72
  fs.writeFileSync(txFileName, await txGen.generate());
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ChainConsts extends GenericChainConsts {
5
+ export interface ChainConsts<Rv extends RpcVersion> extends GenericChainConsts<Rv> {
6
6
  {{{ defTypeOut }}}
7
7
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ChainErrors extends GenericChainErrors {
5
+ export interface ChainErrors<Rv extends RpcVersion> extends GenericChainErrors<Rv> {
6
6
  {{{ defTypeOut }}}
7
7
  }