@dedot/codegen 0.0.1-next.6cd3bbb0.6 → 0.0.1-next.714802d9.27

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 (72) hide show
  1. package/cjs/generator/ConstsGen.js +4 -5
  2. package/cjs/generator/ErrorsGen.js +5 -6
  3. package/cjs/generator/EventsGen.js +5 -6
  4. package/cjs/generator/IndexGen.js +5 -6
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +20 -12
  7. package/cjs/generator/RuntimeApisGen.js +97 -23
  8. package/cjs/generator/TxGen.js +14 -12
  9. package/cjs/generator/TypeImports.js +8 -0
  10. package/cjs/generator/TypesGen.js +8 -15
  11. package/cjs/generator/index.js +1 -1
  12. package/cjs/generator/known-codecs.js +1 -1
  13. package/cjs/generator/utils.js +30 -1
  14. package/cjs/index.js +24 -24
  15. package/cjs/templates/consts.hbs +1 -1
  16. package/cjs/templates/errors.hbs +1 -1
  17. package/cjs/templates/events.hbs +1 -1
  18. package/cjs/templates/index.hbs +15 -10
  19. package/cjs/templates/json-rpc.hbs +5 -0
  20. package/cjs/templates/query.hbs +1 -1
  21. package/cjs/templates/runtime.hbs +1 -1
  22. package/cjs/templates/tx.hbs +1 -1
  23. package/generator/ApiGen.d.ts +3 -3
  24. package/generator/ConstsGen.d.ts +1 -1
  25. package/generator/ConstsGen.js +4 -5
  26. package/generator/ErrorsGen.js +4 -5
  27. package/generator/EventsGen.js +4 -5
  28. package/generator/IndexGen.d.ts +2 -3
  29. package/generator/IndexGen.js +5 -6
  30. package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
  31. package/generator/JsonRpcGen.js +55 -0
  32. package/generator/QueryGen.d.ts +1 -1
  33. package/generator/QueryGen.js +20 -12
  34. package/generator/RuntimeApisGen.d.ts +4 -4
  35. package/generator/RuntimeApisGen.js +94 -20
  36. package/generator/TxGen.js +9 -7
  37. package/generator/TypeImports.d.ts +2 -0
  38. package/generator/TypeImports.js +8 -0
  39. package/generator/TypesGen.d.ts +1 -1
  40. package/generator/TypesGen.js +3 -10
  41. package/generator/index.d.ts +1 -1
  42. package/generator/index.js +1 -1
  43. package/generator/known-codecs.js +1 -1
  44. package/generator/utils.d.ts +5 -0
  45. package/generator/utils.js +28 -0
  46. package/index.d.ts +2 -3
  47. package/index.js +24 -24
  48. package/package.json +11 -16
  49. package/templates/consts.hbs +1 -1
  50. package/templates/errors.hbs +1 -1
  51. package/templates/events.hbs +1 -1
  52. package/templates/index.hbs +15 -10
  53. package/templates/json-rpc.hbs +5 -0
  54. package/templates/query.hbs +1 -1
  55. package/templates/runtime.hbs +1 -1
  56. package/templates/tx.hbs +1 -1
  57. package/cjs/genSupportedChainTypes.js +0 -83
  58. package/cjs/generator/RpcGen.js +0 -176
  59. package/cjs/generator/known-types.js +0 -33
  60. package/cjs/packageInfo.js +0 -5
  61. package/cjs/templates/rpc.hbs +0 -7
  62. package/cjs/types.js +0 -2
  63. package/genSupportedChainTypes.d.ts +0 -1
  64. package/genSupportedChainTypes.js +0 -78
  65. package/generator/RpcGen.js +0 -172
  66. package/generator/known-types.d.ts +0 -6
  67. package/generator/known-types.js +0 -30
  68. package/packageInfo.d.ts +0 -4
  69. package/packageInfo.js +0 -2
  70. package/templates/rpc.hbs +0 -7
  71. package/types.d.ts +0 -6
  72. package/types.js +0 -1
@@ -1,15 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConstsGen = void 0;
4
- const util_1 = require("@polkadot/util");
5
4
  const utils_1 = require("@dedot/utils");
6
- const index_js_1 = require("../generator/index.js");
5
+ const ApiGen_js_1 = require("./ApiGen.js");
7
6
  const utils_js_1 = require("./utils.js");
8
- class ConstsGen extends index_js_1.ApiGen {
7
+ class ConstsGen extends ApiGen_js_1.ApiGen {
9
8
  generate() {
10
9
  const { pallets } = this.metadata;
11
10
  this.typesGen.clearCache();
12
- this.typesGen.typeImports.addKnownType('GenericChainConsts');
11
+ this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
13
12
  let defTypeOut = '';
14
13
  for (let pallet of pallets) {
15
14
  const typedConstants = pallet.constants.map((one) => ({
@@ -18,7 +17,7 @@ class ConstsGen extends index_js_1.ApiGen {
18
17
  docs: one.docs,
19
18
  }));
20
19
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s constants`);
21
- defTypeOut += `${(0, util_1.stringLowerFirst)(pallet.name)}: {
20
+ defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
22
21
  ${typedConstants.map(({ name, type, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: ${type}`).join(',\n')}
23
22
 
24
23
  ${(0, utils_js_1.commentBlock)('Generic pallet constant')}[name: string]: any,
@@ -1,15 +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 util_1 = require("@polkadot/util");
7
- const utils_1 = require("@dedot/utils");
8
7
  class ErrorsGen extends ApiGen_js_1.ApiGen {
9
8
  generate() {
10
9
  const { pallets } = this.metadata;
11
10
  this.typesGen.clearCache();
12
- this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
11
+ this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
13
12
  let defTypeOut = '';
14
13
  for (let pallet of pallets) {
15
14
  const errorTypeId = pallet.error;
@@ -18,12 +17,12 @@ class ErrorsGen extends ApiGen_js_1.ApiGen {
18
17
  }
19
18
  const errorDefs = this.#getErrorDefs(errorTypeId);
20
19
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
21
- defTypeOut += `${(0, util_1.stringCamelCase)(pallet.name)}: {
20
+ defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
22
21
  ${errorDefs
23
- .map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, util_1.stringPascalCase)(name)}: GenericPalletError`)
22
+ .map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError<Rv>`)
24
23
  .join(',\n')}
25
24
 
26
- ${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError,
25
+ ${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
27
26
  },`;
28
27
  }
29
28
  const importTypes = this.typesGen.typeImports.toImports();
@@ -1,15 +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 util_1 = require("@polkadot/util");
7
- const utils_1 = require("@dedot/utils");
8
7
  class EventsGen extends ApiGen_js_1.ApiGen {
9
8
  generate() {
10
9
  const { pallets } = this.metadata;
11
10
  this.typesGen.clearCache();
12
- this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
11
+ this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
13
12
  let defTypeOut = '';
14
13
  for (let pallet of pallets) {
15
14
  const eventTypeId = pallet.event;
@@ -19,7 +18,7 @@ class EventsGen extends ApiGen_js_1.ApiGen {
19
18
  const eventDefs = this.#getEventDefs(eventTypeId);
20
19
  const flatMembers = eventDefs.every((d) => d.fields.length === 0);
21
20
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s events`);
22
- defTypeOut += `${(0, util_1.stringCamelCase)(pallet.name)}: {
21
+ defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
23
22
  ${eventDefs
24
23
  .map((def) => {
25
24
  const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
@@ -32,10 +31,10 @@ class EventsGen extends ApiGen_js_1.ApiGen {
32
31
  }
33
32
  return { ...def, genericParts };
34
33
  })
35
- .map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, util_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(', ')}>`)
36
35
  .join(',\n')}
37
36
 
38
- ${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent,
37
+ ${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
39
38
  },`;
40
39
  }
41
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_1 = require("@dedot/utils");
4
5
  const utils_js_1 = require("./utils.js");
5
- const util_1 = require("@polkadot/util");
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, util_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;
@@ -1,27 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.QueryGen = void 0;
4
- const util_1 = require("@polkadot/util");
5
4
  const utils_1 = require("@dedot/utils");
6
- const index_js_1 = require("../generator/index.js");
5
+ const ApiGen_js_1 = require("./ApiGen.js");
7
6
  const utils_js_1 = require("./utils.js");
8
- class QueryGen extends index_js_1.ApiGen {
7
+ class QueryGen extends ApiGen_js_1.ApiGen {
9
8
  generate() {
10
9
  const { pallets } = this.metadata;
11
10
  this.typesGen.clearCache();
12
- this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
11
+ this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
13
12
  let defTypeOut = '';
14
13
  for (let pallet of pallets) {
15
14
  const storage = pallet.storage;
16
15
  if (!storage)
17
16
  continue;
18
17
  const queries = storage.entries.map((one) => this.#generateEntry(one));
19
- 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
+ });
20
26
  defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
21
- defTypeOut += `${(0, util_1.stringLowerFirst)(pallet.name)}: {
27
+ defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
22
28
  ${queryDefs.join(',\n')}
23
29
 
24
- ${(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>;
25
31
  },`;
26
32
  }
27
33
  const importTypes = this.typesGen.typeImports.toImports();
@@ -30,13 +36,14 @@ class QueryGen extends index_js_1.ApiGen {
30
36
  }
31
37
  #generateEntry(entry) {
32
38
  const { name, type, docs, modifier } = entry;
33
- let valueType, keyType;
39
+ let valueType, keyTypeOut, keyTypeIn;
34
40
  if (type.tag === 'Plain') {
35
41
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
36
42
  }
37
43
  else if (type.tag === 'Map') {
38
44
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
39
- 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);
40
47
  }
41
48
  else {
42
49
  throw Error('Invalid entry type!');
@@ -44,14 +51,15 @@ class QueryGen extends index_js_1.ApiGen {
44
51
  const isOptional = modifier === 'Optional';
45
52
  valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
46
53
  docs.push('\n');
47
- if (keyType) {
48
- docs.push(`@param {${keyType}} arg`);
54
+ if (keyTypeIn) {
55
+ docs.push(`@param {${keyTypeIn}} arg`);
49
56
  }
50
57
  docs.push(`@param {Callback<${valueType}> =} callback`);
51
58
  return {
52
59
  name: (0, utils_1.normalizeName)(name),
53
60
  valueType,
54
- keyType: keyType ? `arg: ${keyType}` : '',
61
+ keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
62
+ keyTypeOut,
55
63
  docs,
56
64
  };
57
65
  }
@@ -2,30 +2,30 @@
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
- const util_1 = require("@polkadot/util");
9
- 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 {
10
10
  typesGen;
11
11
  runtimeApis;
12
12
  constructor(typesGen, runtimeApis) {
13
- super(typesGen, []);
13
+ super(typesGen);
14
14
  this.typesGen = typesGen;
15
15
  this.runtimeApis = runtimeApis;
16
16
  }
17
17
  generate() {
18
18
  this.typesGen.clearCache();
19
- this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
19
+ this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
20
20
  let runtimeCallsOut = '';
21
21
  if (this.metadata.apis.length > 0) {
22
22
  this.metadata.apis.forEach((runtimeApi) => {
23
23
  const { name: runtimeApiName, methods } = runtimeApi;
24
- runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${(0, utils_1.calculateRuntimeApiHash)(runtimeApiName)}`);
25
- runtimeCallsOut += `${(0, util_1.stringCamelCase)(runtimeApiName)}: {
24
+ runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${(0, utils_1.calcRuntimeApiHash)(runtimeApiName)}`);
25
+ runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
26
26
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
27
27
 
28
- ${(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>
29
29
  }`;
30
30
  });
31
31
  }
@@ -33,7 +33,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
33
33
  const specs = this.#targetRuntimeApiSpecs();
34
34
  specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
35
35
  runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
36
- runtimeCallsOut += `${(0, util_1.stringCamelCase)(runtimeApiName)}: {
36
+ runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
37
37
  ${Object.keys(methods)
38
38
  .map((methodName) => this.#generateMethodDefFromSpec({
39
39
  ...methods[methodName],
@@ -42,7 +42,7 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
42
42
  }))
43
43
  .join('\n')}
44
44
 
45
- ${(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>
46
46
  }`;
47
47
  });
48
48
  }
@@ -60,25 +60,25 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
60
60
  const { docs = [], params, type, runtimeApiName, methodName } = spec;
61
61
  const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
62
62
  const defaultDocs = [`@callname: ${callName}`];
63
- this.addTypeImport(type, false);
64
- params.forEach(({ type }) => this.addTypeImport(type));
63
+ this.#addTypeImport(type, false);
64
+ params.forEach(({ type }) => this.#addTypeImport(type));
65
65
  const typedParams = params.map((param, idx) => ({
66
66
  ...param,
67
67
  isOptional: this.#isOptionalParam(params, param.type, idx),
68
- plainType: this.getGeneratedTypeName(param.type),
68
+ plainType: this.#getGeneratedTypeName(param.type),
69
69
  }));
70
70
  const paramsOut = typedParams
71
- .map(({ name, isOptional, plainType }) => `${(0, util_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
71
+ .map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
72
72
  .join(', ');
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<(${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}>>`;
75
75
  }
76
76
  #generateMethodDef(runtimeApiName, methodDef) {
77
77
  const { name: methodName, inputs, output, docs } = methodDef;
78
78
  const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
79
79
  const defaultDocs = [`@callname: ${callName}`];
80
80
  const typeOut = this.typesGen.generateType(output, 1, true);
81
- this.addTypeImport(typeOut, false);
81
+ this.#addTypeImport(typeOut, false);
82
82
  const typedInputs = inputs
83
83
  .map((input, idx) => ({
84
84
  ...input,
@@ -88,14 +88,14 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
88
88
  ...input,
89
89
  isOptional: this.#isOptionalParam(inputs, input.type, idx),
90
90
  }));
91
- this.addTypeImport(typedInputs.map((t) => t.type));
91
+ this.#addTypeImport(typedInputs.map((t) => t.type));
92
92
  const paramsOut = typedInputs
93
- .map(({ name, type, isOptional }) => `${(0, util_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
93
+ .map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
94
94
  .join(', ');
95
- return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, util_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}>>`;
96
96
  }
97
97
  #targetRuntimeApiSpecs() {
98
- const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
98
+ const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
99
99
  const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
100
100
  if (!runtimeApiSpec)
101
101
  return;
@@ -112,8 +112,82 @@ class RuntimeApisGen extends RpcGen_js_1.RpcGen {
112
112
  }, []);
113
113
  }
114
114
  #findRuntimeApiSpec = (runtimeApiHash, version) => {
115
- const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calculateRuntimeApiHash)(one) === runtimeApiHash);
115
+ const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calcRuntimeApiHash)(one) === runtimeApiHash);
116
116
  return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
117
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
+ }
118
192
  }
119
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 utils_1 = require("@dedot/utils");
4
5
  const index_js_1 = require("../generator/index.js");
5
- const util_1 = require("@polkadot/util");
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);
@@ -26,9 +26,9 @@ class TxGen extends index_js_1.ApiGen {
26
26
  const isFlatEnum = type.value.members.every((m) => m.fields.length === 0);
27
27
  const typedTxs = type.value.members
28
28
  .map((one) => ({
29
- functionName: (0, util_1.stringCamelCase)(one.name),
29
+ functionName: (0, utils_1.stringCamelCase)(one.name),
30
30
  params: one.fields.map((f) => ({
31
- name: (0, util_1.stringCamelCase)(f.name),
31
+ name: (0, utils_1.stringCamelCase)(f.name),
32
32
  normalizedName: this.#normalizeParamName(f.name),
33
33
  type: this.typesGen.generateType(f.typeId, 1),
34
34
  docs: f.docs,
@@ -38,32 +38,34 @@ class TxGen extends index_js_1.ApiGen {
38
38
  .map((f) => {
39
39
  return {
40
40
  ...f,
41
- callInput: this.#generateCallInput((0, util_1.stringPascalCase)(pallet.name), (0, util_1.stringPascalCase)(f.functionName), !isFlatEnum ? f.params.map((p) => `${p.name}: ${p.type}`) : undefined),
41
+ callInput: this.#generateCallInput((0, utils_1.stringPascalCase)(pallet.name), (0, utils_1.stringPascalCase)(f.functionName), !isFlatEnum ? f.params.map((p) => `${p.name}: ${p.type}`) : undefined),
42
42
  };
43
43
  });
44
44
  txDefsOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s transaction calls`);
45
- txDefsOut += `${(0, util_1.stringCamelCase)(pallet.name)}: {
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 }));
64
66
  }
65
67
  #normalizeParamName(name) {
66
- name = (0, util_1.stringCamelCase)(name);
68
+ name = (0, utils_1.stringCamelCase)(name);
67
69
  return (0, utils_js_1.isReservedWord)(name) ? `${name}_` : name;
68
70
  }
69
71
  #generateCallInput(palletName, callName, params) {
@@ -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
  }
@@ -1,13 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
4
- const util_1 = require("@polkadot/util");
5
4
  const codecs_1 = require("@dedot/codecs");
6
5
  const utils_1 = require("@dedot/utils");
7
- const utils_js_1 = require("./utils.js");
8
- const known_types_js_1 = require("./known-types.js");
9
6
  const TypeImports_js_1 = require("./TypeImports.js");
10
7
  const known_codecs_js_1 = require("./known-codecs.js");
8
+ const utils_js_1 = require("./utils.js");
11
9
  // Skip generate types for these
12
10
  // as we do have native types for them
13
11
  const SKIP_TYPES = [
@@ -151,12 +149,12 @@ class TypesGen {
151
149
  return 'null';
152
150
  }
153
151
  else if (members.every((x) => x.fields.length === 0)) {
154
- return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, util_1.stringPascalCase)(name)}'`).join(' | ');
152
+ return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
155
153
  }
156
154
  else {
157
155
  const membersType = [];
158
156
  for (const { fields, name, docs } of members) {
159
- const keyName = (0, util_1.stringPascalCase)(name);
157
+ const keyName = (0, utils_1.stringPascalCase)(name);
160
158
  if (fields.length === 0) {
161
159
  membersType.push([keyName, null, docs]);
162
160
  }
@@ -315,7 +313,7 @@ class TypesGen {
315
313
  */
316
314
  #cleanPath(path) {
317
315
  return path
318
- .map((one) => (0, util_1.stringPascalCase)(one))
316
+ .map((one) => (0, utils_1.stringPascalCase)(one))
319
317
  .filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
320
318
  .join('');
321
319
  }
@@ -390,10 +388,10 @@ class TypesGen {
390
388
  if (diffParam?.typeId) {
391
389
  const diffType = types[diffParam.typeId];
392
390
  if (diffType.path.length > 0) {
393
- return (0, util_1.stringPascalCase)(diffType.path.at(-1));
391
+ return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
394
392
  }
395
393
  else if (diffType.type.tag === 'Primitive') {
396
- return (0, util_1.stringPascalCase)(diffType.type.value.kind);
394
+ return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
397
395
  }
398
396
  }
399
397
  // Last resort!
@@ -404,7 +402,7 @@ class TypesGen {
404
402
  typeName.forEach((one) => this.addTypeImport(one));
405
403
  return;
406
404
  }
407
- if ((0, utils_1.isNativeType)(typeName)) {
405
+ if ((0, utils_js_1.isNativeType)(typeName)) {
408
406
  return;
409
407
  }
410
408
  for (let type of Object.values(this.includedTypes)) {
@@ -426,12 +424,7 @@ class TypesGen {
426
424
  this.typeImports.addCodecType(typeName);
427
425
  return;
428
426
  }
429
- if (known_types_js_1.knownTypes.includes(typeName)) {
430
- this.typeImports.addKnownType(typeName);
431
- }
432
- else {
433
- this.typeImports.addOutType(typeName);
434
- }
427
+ this.typeImports.addOutType(typeName);
435
428
  }
436
429
  }
437
430
  exports.TypesGen = TypesGen;