@dedot/codegen 0.0.1-next.3 → 0.0.1-next.324f6c64.55

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 (74) hide show
  1. package/cjs/generator/ConstsGen.js +13 -14
  2. package/cjs/generator/ErrorsGen.js +15 -16
  3. package/cjs/generator/EventsGen.js +16 -17
  4. package/cjs/generator/IndexGen.js +9 -10
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +27 -19
  7. package/cjs/generator/RuntimeApisGen.js +110 -32
  8. package/cjs/generator/TxGen.js +23 -21
  9. package/cjs/generator/TypeImports.js +13 -3
  10. package/cjs/generator/TypesGen.js +27 -33
  11. package/cjs/generator/index.js +10 -10
  12. package/cjs/generator/known-codecs.js +151 -0
  13. package/cjs/generator/utils.js +30 -1
  14. package/cjs/index.js +40 -40
  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 +2 -2
  25. package/generator/ConstsGen.js +7 -8
  26. package/generator/ErrorsGen.d.ts +2 -2
  27. package/generator/ErrorsGen.js +8 -9
  28. package/generator/EventsGen.d.ts +2 -2
  29. package/generator/EventsGen.js +8 -9
  30. package/generator/IndexGen.d.ts +3 -4
  31. package/generator/IndexGen.js +7 -8
  32. package/generator/JsonRpcGen.d.ts +7 -0
  33. package/generator/JsonRpcGen.js +55 -0
  34. package/generator/QueryGen.d.ts +2 -2
  35. package/generator/QueryGen.js +23 -15
  36. package/generator/RuntimeApisGen.d.ts +6 -6
  37. package/generator/RuntimeApisGen.js +101 -23
  38. package/generator/TxGen.d.ts +2 -2
  39. package/generator/TxGen.js +13 -11
  40. package/generator/TypeImports.d.ts +8 -1
  41. package/generator/TypeImports.js +13 -3
  42. package/generator/TypesGen.d.ts +4 -4
  43. package/generator/TypesGen.js +14 -20
  44. package/generator/index.d.ts +10 -10
  45. package/generator/index.js +10 -10
  46. package/generator/known-codecs.d.ts +23 -0
  47. package/generator/known-codecs.js +120 -0
  48. package/generator/utils.d.ts +5 -0
  49. package/generator/utils.js +28 -0
  50. package/index.d.ts +3 -4
  51. package/index.js +32 -32
  52. package/package.json +10 -18
  53. package/templates/consts.hbs +1 -1
  54. package/templates/errors.hbs +1 -1
  55. package/templates/events.hbs +1 -1
  56. package/templates/index.hbs +15 -10
  57. package/templates/json-rpc.hbs +5 -0
  58. package/templates/query.hbs +1 -1
  59. package/templates/runtime.hbs +1 -1
  60. package/templates/tx.hbs +1 -1
  61. package/cjs/genSupportedChainTypes.js +0 -79
  62. package/cjs/generator/RpcGen.js +0 -175
  63. package/cjs/packageInfo.js +0 -5
  64. package/cjs/templates/rpc.hbs +0 -7
  65. package/cjs/types.js +0 -2
  66. package/genSupportedChainTypes.d.ts +0 -1
  67. package/genSupportedChainTypes.js +0 -74
  68. package/generator/RpcGen.d.ts +0 -10
  69. package/generator/RpcGen.js +0 -171
  70. package/packageInfo.d.ts +0 -4
  71. package/packageInfo.js +0 -2
  72. package/templates/rpc.hbs +0 -7
  73. package/types.d.ts +0 -6
  74. package/types.js +0 -1
@@ -1,9 +1,9 @@
1
1
  // Generated by @dedot/codegen
2
2
 
3
- import { GenericSubstrateApi } from '@dedot/types';
3
+ import { GenericSubstrateApi, RpcLegacy, RpcV2, RpcVersion } from '@dedot/types';
4
4
  import { ChainConsts } from './consts';
5
5
  import { ChainStorage } from './query';
6
- import { RpcCalls } from './rpc';
6
+ import { ChainJsonRpcApis } from './json-rpc';
7
7
  import { ChainErrors } from './errors';
8
8
  import { ChainEvents } from './events';
9
9
  import { RuntimeApis } from './runtime';
@@ -11,12 +11,17 @@ import { ChainTx } from './tx';
11
11
 
12
12
  export * from './types';
13
13
 
14
- export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
15
- rpc: RpcCalls;
16
- consts: ChainConsts;
17
- query: ChainStorage;
18
- errors: ChainErrors;
19
- events: ChainEvents;
20
- call: RuntimeApis;
21
- tx: ChainTx;
14
+ export interface Versioned{{{interfaceName}}}Api<Rv extends RpcVersion> extends GenericSubstrateApi<Rv> {
15
+ rpc: ChainJsonRpcApis<Rv>;
16
+ consts: ChainConsts<Rv>;
17
+ query: ChainStorage<Rv>;
18
+ errors: ChainErrors<Rv>;
19
+ events: ChainEvents<Rv>;
20
+ call: RuntimeApis<Rv>;
21
+ tx: ChainTx<Rv>;
22
+ }
23
+
24
+ export interface {{{interfaceName}}}Api {
25
+ legacy: Versioned{{{interfaceName}}}Api<RpcLegacy>;
26
+ v2: Versioned{{{interfaceName}}}Api<RpcV2>;
22
27
  }
@@ -0,0 +1,5 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export type ChainJsonRpcApis<Rv extends RpcVersion> = Pick<JsonRpcApis, {{{jsonRpcMethods}}}> & GenericJsonRpcApis<Rv>;
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ChainStorage extends GenericChainStorage {
5
+ export interface ChainStorage<Rv extends RpcVersion> extends GenericChainStorage<Rv> {
6
6
  {{{ defTypeOut }}}
7
7
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface RuntimeApis extends GenericRuntimeApis {
5
+ export interface RuntimeApis<Rv extends RpcVersion> extends GenericRuntimeApis<Rv> {
6
6
  {{{ runtimeCallsOut }}}
7
7
  }
8
8
 
@@ -4,6 +4,6 @@
4
4
 
5
5
  {{{ defTypes }}}
6
6
 
7
- export interface ChainTx extends GenericChainTx<TxCall> {
7
+ export interface ChainTx<Rv extends RpcVersion> extends GenericChainTx<Rv, TxCall<Rv>> {
8
8
  {{{ txDefsOut }}}
9
9
  }
@@ -1,4 +1,4 @@
1
- import { TypesGen } from '../generator';
1
+ import { TypesGen } from '../generator/index.js';
2
2
  export declare abstract class ApiGen {
3
3
  readonly typesGen: TypesGen;
4
4
  constructor(typesGen: TypesGen);
@@ -44,7 +44,7 @@ export declare abstract class ApiGen {
44
44
  } | {
45
45
  tag: "Primitive";
46
46
  value: {
47
- kind: "bool" | "char" | "str" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "i8" | "i16" | "i32" | "i64" | "i128" | "i256";
47
+ kind: "bool" | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "u256" | "i256" | "str" | "char";
48
48
  };
49
49
  } | {
50
50
  tag: "Compact";
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
134
134
  }>;
135
135
  };
136
136
  };
137
- get registry(): import("@dedot/codecs").CodecRegistry;
137
+ get registry(): import("@dedot/codecs").PortableRegistry;
138
138
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class ConstsGen extends ApiGen {
3
- generate(): Promise<string>;
3
+ generate(useSubPaths?: boolean): Promise<string>;
4
4
  }
@@ -1,12 +1,11 @@
1
- import { stringLowerFirst } from '@polkadot/util';
2
- import { normalizeName } from '@dedot/utils';
3
- import { ApiGen } from '../generator';
4
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
1
+ import { normalizeName, stringCamelCase } from 'dedot/utils';
2
+ import { ApiGen } from './ApiGen.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class ConstsGen extends ApiGen {
6
- generate() {
5
+ generate(useSubPaths = false) {
7
6
  const { pallets } = this.metadata;
8
7
  this.typesGen.clearCache();
9
- this.typesGen.typeImports.addKnownType('GenericChainConsts');
8
+ this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
10
9
  let defTypeOut = '';
11
10
  for (let pallet of pallets) {
12
11
  const typedConstants = pallet.constants.map((one) => ({
@@ -15,13 +14,13 @@ export class ConstsGen extends ApiGen {
15
14
  docs: one.docs,
16
15
  }));
17
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
18
- defTypeOut += `${stringLowerFirst(pallet.name)}: {
17
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
18
  ${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
20
19
 
21
20
  ${commentBlock('Generic pallet constant')}[name: string]: any,
22
21
  },`;
23
22
  }
24
- const importTypes = this.typesGen.typeImports.toImports();
23
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
25
24
  const template = compileTemplate('consts.hbs');
26
25
  return beautifySourceCode(template({ importTypes, defTypeOut }));
27
26
  }
@@ -1,5 +1,5 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class ErrorsGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,12 +1,11 @@
1
- import { ApiGen } from './ApiGen';
2
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
3
- import { stringCamelCase, stringPascalCase } from '@polkadot/util';
4
- import { assert } from '@dedot/utils';
1
+ import { assert, stringCamelCase, stringPascalCase } from 'dedot/utils';
2
+ import { ApiGen } from './ApiGen.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class ErrorsGen extends ApiGen {
6
- generate() {
5
+ generate(useSubPaths = false) {
7
6
  const { pallets } = this.metadata;
8
7
  this.typesGen.clearCache();
9
- this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
8
+ this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
10
9
  let defTypeOut = '';
11
10
  for (let pallet of pallets) {
12
11
  const errorTypeId = pallet.error;
@@ -17,13 +16,13 @@ export class ErrorsGen extends ApiGen {
17
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
18
17
  defTypeOut += `${stringCamelCase(pallet.name)}: {
19
18
  ${errorDefs
20
- .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError`)
19
+ .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError<Rv>`)
21
20
  .join(',\n')}
22
21
 
23
- ${commentBlock('Generic pallet error')}[error: string]: GenericPalletError,
22
+ ${commentBlock('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
24
23
  },`;
25
24
  }
26
- const importTypes = this.typesGen.typeImports.toImports();
25
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
27
26
  const template = compileTemplate('errors.hbs');
28
27
  return beautifySourceCode(template({ importTypes, defTypeOut }));
29
28
  }
@@ -1,5 +1,5 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class EventsGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,12 +1,11 @@
1
- import { ApiGen } from './ApiGen';
2
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
3
- import { stringCamelCase, stringPascalCase } from '@polkadot/util';
4
- import { assert } from '@dedot/utils';
1
+ import { assert, stringCamelCase, stringPascalCase } from 'dedot/utils';
2
+ import { ApiGen } from './ApiGen.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class EventsGen extends ApiGen {
6
- generate() {
5
+ generate(useSubPaths = false) {
7
6
  const { pallets } = this.metadata;
8
7
  this.typesGen.clearCache();
9
- this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
8
+ this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
10
9
  let defTypeOut = '';
11
10
  for (let pallet of pallets) {
12
11
  const eventTypeId = pallet.event;
@@ -29,13 +28,13 @@ export class EventsGen extends ApiGen {
29
28
  }
30
29
  return { ...def, genericParts };
31
30
  })
32
- .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
31
+ .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
33
32
  .join(',\n')}
34
33
 
35
- ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent,
34
+ ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
36
35
  },`;
37
36
  }
38
- const importTypes = this.typesGen.typeImports.toImports();
37
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
39
38
  const template = compileTemplate('events.hbs');
40
39
  return beautifySourceCode(template({ importTypes, defTypeOut }));
41
40
  }
@@ -1,6 +1,5 @@
1
- import { NetworkInfo } from '../types';
2
1
  export declare class IndexGen {
3
- readonly networkInfo: NetworkInfo;
4
- constructor(networkInfo: NetworkInfo);
5
- generate(): Promise<string>;
2
+ readonly chain: string;
3
+ constructor(chain: string);
4
+ generate(useSubPaths?: boolean): Promise<string>;
6
5
  }
@@ -1,13 +1,12 @@
1
- import { beautifySourceCode, compileTemplate } from './utils';
2
- import { stringPascalCase } from '@polkadot/util';
1
+ import { stringPascalCase } from 'dedot/utils';
2
+ import { beautifySourceCode, compileTemplate } from './utils.js';
3
3
  export class IndexGen {
4
- networkInfo;
5
- constructor(networkInfo) {
6
- this.networkInfo = networkInfo;
4
+ chain;
5
+ constructor(chain) {
6
+ this.chain = chain;
7
7
  }
8
- async generate() {
9
- const { chain } = this.networkInfo;
10
- const interfaceName = stringPascalCase(chain);
8
+ async generate(useSubPaths = false) {
9
+ const interfaceName = stringPascalCase(this.chain);
11
10
  const template = compileTemplate('index.hbs');
12
11
  return beautifySourceCode(template({ interfaceName }));
13
12
  }
@@ -0,0 +1,7 @@
1
+ import { ApiGen, TypesGen } from '../generator/index.js';
2
+ export declare class JsonRpcGen extends ApiGen {
3
+ readonly typesGen: TypesGen;
4
+ readonly rpcMethods: string[];
5
+ constructor(typesGen: TypesGen, rpcMethods: string[]);
6
+ generate(useSubPaths?: boolean): Promise<string>;
7
+ }
@@ -0,0 +1,55 @@
1
+ import { subscriptionsInfo } from 'dedot';
2
+ import { ApiGen } from '../generator/index.js';
3
+ import { beautifySourceCode, compileTemplate } from './utils.js';
4
+ const HIDDEN_RPCS = [
5
+ // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
6
+ 'rpc_methods',
7
+ ];
8
+ const ALIAS_RPCS = [
9
+ // TODO include these into the specs
10
+ 'account_nextIndex',
11
+ 'system_dryRunAt',
12
+ 'state_callAt',
13
+ 'state_getKeysPagedAt',
14
+ 'state_getStorageAt',
15
+ 'state_getStorageHashAt',
16
+ 'state_getStorageSizeAt',
17
+ 'chain_getRuntimeVersion',
18
+ 'chain_getHead',
19
+ 'chain_getFinalisedHead',
20
+ 'childstate_getKeysPagedAt',
21
+ // Subscription pairs
22
+ 'chain_subscribeRuntimeVersion',
23
+ 'chain_unsubscribeRuntimeVersion',
24
+ 'chain_unsubscribeNewHead',
25
+ 'chain_subscribeNewHead',
26
+ 'subscribe_newHead',
27
+ 'unsubscribe_newHead',
28
+ 'chain_subscribeFinalisedHeads',
29
+ 'chain_unsubscribeFinalisedHeads',
30
+ ];
31
+ export class JsonRpcGen extends ApiGen {
32
+ typesGen;
33
+ rpcMethods;
34
+ constructor(typesGen, rpcMethods) {
35
+ super(typesGen);
36
+ this.typesGen = typesGen;
37
+ this.rpcMethods = rpcMethods;
38
+ HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
39
+ rpcMethods.sort();
40
+ }
41
+ generate(useSubPaths = false) {
42
+ this.typesGen.clearCache();
43
+ this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
44
+ this.typesGen.typeImports.addKnownJsonRpcType('JsonRpcApis');
45
+ const toExclude = Object.values(subscriptionsInfo).flat();
46
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
47
+ const template = compileTemplate('json-rpc.hbs');
48
+ const jsonRpcMethods = this.rpcMethods
49
+ .filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
50
+ .filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
51
+ .map((one) => `'${one}'`)
52
+ .join(' | ');
53
+ return beautifySourceCode(template({ importTypes, jsonRpcMethods }));
54
+ }
55
+ }
@@ -1,5 +1,5 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class QueryGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }
@@ -1,39 +1,46 @@
1
- import { stringLowerFirst } from '@polkadot/util';
2
- import { normalizeName } from '@dedot/utils';
3
- import { ApiGen } from '../generator';
4
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
1
+ import { normalizeName, stringCamelCase } from 'dedot/utils';
2
+ import { ApiGen } from './ApiGen.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class QueryGen extends ApiGen {
6
- generate() {
5
+ generate(useSubPaths = false) {
7
6
  const { pallets } = this.metadata;
8
7
  this.typesGen.clearCache();
9
- this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
8
+ this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
10
9
  let defTypeOut = '';
11
10
  for (let pallet of pallets) {
12
11
  const storage = pallet.storage;
13
12
  if (!storage)
14
13
  continue;
15
14
  const queries = storage.entries.map((one) => this.#generateEntry(one));
16
- const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
15
+ const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
16
+ if (keyTypeOut) {
17
+ return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}, ${keyTypeOut}>`;
18
+ }
19
+ else {
20
+ return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}>`;
21
+ }
22
+ });
17
23
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
18
- defTypeOut += `${stringLowerFirst(pallet.name)}: {
24
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
25
  ${queryDefs.join(',\n')}
20
26
 
21
- ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
27
+ ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
22
28
  },`;
23
29
  }
24
- const importTypes = this.typesGen.typeImports.toImports();
30
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
25
31
  const template = compileTemplate('query.hbs');
26
32
  return beautifySourceCode(template({ importTypes, defTypeOut }));
27
33
  }
28
34
  #generateEntry(entry) {
29
35
  const { name, type, docs, modifier } = entry;
30
- let valueType, keyType;
36
+ let valueType, keyTypeOut, keyTypeIn;
31
37
  if (type.tag === 'Plain') {
32
38
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
33
39
  }
34
40
  else if (type.tag === 'Map') {
35
41
  valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
36
- keyType = this.typesGen.generateType(type.value.keyTypeId, 1);
42
+ keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
43
+ keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
37
44
  }
38
45
  else {
39
46
  throw Error('Invalid entry type!');
@@ -41,14 +48,15 @@ export class QueryGen extends ApiGen {
41
48
  const isOptional = modifier === 'Optional';
42
49
  valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
43
50
  docs.push('\n');
44
- if (keyType) {
45
- docs.push(`@param {${keyType}} arg`);
51
+ if (keyTypeIn) {
52
+ docs.push(`@param {${keyTypeIn}} arg`);
46
53
  }
47
54
  docs.push(`@param {Callback<${valueType}> =} callback`);
48
55
  return {
49
56
  name: normalizeName(name),
50
57
  valueType,
51
- keyType: keyType ? `arg: ${keyType}` : '',
58
+ keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
59
+ keyTypeOut,
52
60
  docs,
53
61
  };
54
62
  }
@@ -1,9 +1,9 @@
1
- import { TypesGen } from './TypesGen';
2
- import { RpcGen } from './RpcGen';
3
- export declare class RuntimeApisGen extends RpcGen {
1
+ import { ApiGen } from './ApiGen.js';
2
+ import { TypesGen } from './TypesGen.js';
3
+ export declare class RuntimeApisGen extends ApiGen {
4
4
  #private;
5
5
  readonly typesGen: TypesGen;
6
- readonly runtimeApis: [string, number][];
7
- constructor(typesGen: TypesGen, runtimeApis: [string, number][]);
8
- generate(): Promise<string>;
6
+ readonly runtimeApis: Record<string, number>;
7
+ constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
8
+ generate(useSubPaths?: boolean): Promise<string>;
9
9
  }
@@ -1,28 +1,28 @@
1
- import { findRuntimeApiSpec } from '@dedot/specs';
2
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
3
- import { calculateRuntimeApiHash, stringSnakeCase } from '@dedot/utils';
4
- import { RpcGen } from './RpcGen';
5
- import { stringCamelCase } from '@polkadot/util';
6
- export class RuntimeApisGen extends RpcGen {
1
+ import { getRuntimeApiNames, getRuntimeApiSpecs } from 'dedot/runtime-specs';
2
+ import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from 'dedot/utils';
3
+ import { ApiGen } from './ApiGen.js';
4
+ import { findKnownCodecType } from './known-codecs.js';
5
+ import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
6
+ export class RuntimeApisGen extends ApiGen {
7
7
  typesGen;
8
8
  runtimeApis;
9
9
  constructor(typesGen, runtimeApis) {
10
- super(typesGen, []);
10
+ super(typesGen);
11
11
  this.typesGen = typesGen;
12
12
  this.runtimeApis = runtimeApis;
13
13
  }
14
- generate() {
14
+ generate(useSubPaths = false) {
15
15
  this.typesGen.clearCache();
16
- this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
16
+ this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
17
17
  let runtimeCallsOut = '';
18
18
  if (this.metadata.apis.length > 0) {
19
19
  this.metadata.apis.forEach((runtimeApi) => {
20
20
  const { name: runtimeApiName, methods } = runtimeApi;
21
- runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calculateRuntimeApiHash(runtimeApiName)}`);
21
+ runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calcRuntimeApiHash(runtimeApiName)}`);
22
22
  runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
23
23
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
24
24
 
25
- ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
25
+ ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
26
26
  }`;
27
27
  });
28
28
  }
@@ -39,11 +39,11 @@ export class RuntimeApisGen extends RpcGen {
39
39
  }))
40
40
  .join('\n')}
41
41
 
42
- ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
42
+ ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
43
43
  }`;
44
44
  });
45
45
  }
46
- const importTypes = this.typesGen.typeImports.toImports();
46
+ const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
47
47
  const template = compileTemplate('runtime.hbs');
48
48
  return beautifySourceCode(template({ importTypes, runtimeCallsOut }));
49
49
  }
@@ -57,25 +57,25 @@ export class RuntimeApisGen extends RpcGen {
57
57
  const { docs = [], params, type, runtimeApiName, methodName } = spec;
58
58
  const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
59
59
  const defaultDocs = [`@callname: ${callName}`];
60
- this.addTypeImport(type, false);
61
- params.forEach(({ type }) => this.addTypeImport(type));
60
+ this.#addTypeImport(type, false);
61
+ params.forEach(({ type }) => this.#addTypeImport(type));
62
62
  const typedParams = params.map((param, idx) => ({
63
63
  ...param,
64
64
  isOptional: this.#isOptionalParam(params, param.type, idx),
65
- plainType: this.getGeneratedTypeName(param.type),
65
+ plainType: this.#getGeneratedTypeName(param.type),
66
66
  }));
67
67
  const paramsOut = typedParams
68
68
  .map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
69
69
  .join(', ');
70
- const typeOut = this.getGeneratedTypeName(type, false);
71
- return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
70
+ const typeOut = this.#getGeneratedTypeName(type, false);
71
+ return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
72
72
  }
73
73
  #generateMethodDef(runtimeApiName, methodDef) {
74
74
  const { name: methodName, inputs, output, docs } = methodDef;
75
75
  const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
76
76
  const defaultDocs = [`@callname: ${callName}`];
77
77
  const typeOut = this.typesGen.generateType(output, 1, true);
78
- this.addTypeImport(typeOut, false);
78
+ this.#addTypeImport(typeOut, false);
79
79
  const typedInputs = inputs
80
80
  .map((input, idx) => ({
81
81
  ...input,
@@ -85,15 +85,15 @@ export class RuntimeApisGen extends RpcGen {
85
85
  ...input,
86
86
  isOptional: this.#isOptionalParam(inputs, input.type, idx),
87
87
  }));
88
- this.addTypeImport(typedInputs.map((t) => t.type));
88
+ this.#addTypeImport(typedInputs.map((t) => t.type));
89
89
  const paramsOut = typedInputs
90
90
  .map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
91
91
  .join(', ');
92
- return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
92
+ return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
93
93
  }
94
94
  #targetRuntimeApiSpecs() {
95
- const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
96
- const runtimeApiSpec = findRuntimeApiSpec(runtimeApiHash, version);
95
+ const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
96
+ const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
97
97
  if (!runtimeApiSpec)
98
98
  return;
99
99
  return {
@@ -108,4 +108,82 @@ export class RuntimeApisGen extends RpcGen {
108
108
  return [...o, spec];
109
109
  }, []);
110
110
  }
111
+ #findRuntimeApiSpec = (runtimeApiHash, version) => {
112
+ const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
113
+ return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
114
+ };
115
+ // TODO check typeIn, typeOut if param type, or rpc type isScale
116
+ #addTypeImport(type, toTypeIn = true) {
117
+ if (Array.isArray(type)) {
118
+ type.forEach((one) => this.#addTypeImport(one, toTypeIn));
119
+ return;
120
+ }
121
+ type = type.trim();
122
+ if (isNativeType(type)) {
123
+ return;
124
+ }
125
+ // Handle generic wrapper types
126
+ const matchArray = type.match(WRAPPER_TYPE_REGEX);
127
+ if (matchArray) {
128
+ const [_, $1, $2] = matchArray;
129
+ this.#addTypeImport($1, toTypeIn);
130
+ if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
131
+ this.#addTypeImport($2, toTypeIn);
132
+ }
133
+ else {
134
+ this.#addTypeImport($2.split(','), toTypeIn);
135
+ }
136
+ return;
137
+ }
138
+ // Check tuple type
139
+ if (type.match(TUPLE_TYPE_REGEX)) {
140
+ this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
141
+ return;
142
+ }
143
+ if (type.includes(' | ')) {
144
+ this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
145
+ return;
146
+ }
147
+ try {
148
+ const codecType = this.#getCodecType(type, toTypeIn);
149
+ if (isNativeType(codecType))
150
+ return;
151
+ this.typesGen.typeImports.addCodecType(codecType);
152
+ return;
153
+ }
154
+ catch (e) { }
155
+ this.typesGen.addTypeImport(type);
156
+ }
157
+ #getGeneratedTypeName(type, toTypeIn = true) {
158
+ try {
159
+ const matchArray = type.match(WRAPPER_TYPE_REGEX);
160
+ if (matchArray) {
161
+ const [_, $1, $2] = matchArray;
162
+ const wrapperTypeName = this.#getCodecType($1, toTypeIn);
163
+ if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
164
+ return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
165
+ }
166
+ const innerTypeNames = $2
167
+ .split(',')
168
+ .map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
169
+ .join(', ');
170
+ return `${wrapperTypeName}<${innerTypeNames}>`;
171
+ }
172
+ else if (type.match(TUPLE_TYPE_REGEX)) {
173
+ const innerTypeNames = type
174
+ .slice(1, -1)
175
+ .split(',')
176
+ .map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
177
+ .join(', ');
178
+ return `[${innerTypeNames}]`;
179
+ }
180
+ return this.#getCodecType(type, toTypeIn);
181
+ }
182
+ catch (e) { }
183
+ return type;
184
+ }
185
+ #getCodecType(type, toTypeIn = true) {
186
+ const { typeIn, typeOut } = findKnownCodecType(type);
187
+ return toTypeIn ? typeIn : typeOut;
188
+ }
111
189
  }
@@ -1,5 +1,5 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.js';
2
2
  export declare class TxGen extends ApiGen {
3
3
  #private;
4
- generate(): Promise<string>;
4
+ generate(useSubPaths?: boolean): Promise<string>;
5
5
  }