@dedot/codegen 0.0.1-next.3 → 0.0.1-next.3662494a.39

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 +10 -11
  2. package/cjs/generator/ErrorsGen.js +13 -14
  3. package/cjs/generator/EventsGen.js +14 -15
  4. package/cjs/generator/IndexGen.js +8 -9
  5. package/cjs/generator/JsonRpcGen.js +59 -0
  6. package/cjs/generator/QueryGen.js +24 -16
  7. package/cjs/generator/RuntimeApisGen.js +107 -29
  8. package/cjs/generator/TxGen.js +21 -19
  9. package/cjs/generator/TypeImports.js +8 -0
  10. package/cjs/generator/TypesGen.js +23 -29
  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 +32 -32
  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 +5 -5
  24. package/generator/ConstsGen.d.ts +1 -1
  25. package/generator/ConstsGen.js +5 -6
  26. package/generator/ErrorsGen.d.ts +1 -1
  27. package/generator/ErrorsGen.js +6 -7
  28. package/generator/EventsGen.d.ts +1 -1
  29. package/generator/EventsGen.js +6 -7
  30. package/generator/IndexGen.d.ts +2 -3
  31. package/generator/IndexGen.js +6 -7
  32. package/generator/JsonRpcGen.d.ts +7 -0
  33. package/generator/JsonRpcGen.js +55 -0
  34. package/generator/QueryGen.d.ts +1 -1
  35. package/generator/QueryGen.js +21 -13
  36. package/generator/RuntimeApisGen.d.ts +5 -5
  37. package/generator/RuntimeApisGen.js +99 -21
  38. package/generator/TxGen.d.ts +1 -1
  39. package/generator/TxGen.js +11 -9
  40. package/generator/TypeImports.d.ts +2 -0
  41. package/generator/TypeImports.js +8 -0
  42. package/generator/TypesGen.d.ts +3 -3
  43. package/generator/TypesGen.js +12 -18
  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 +2 -3
  51. package/index.js +24 -24
  52. package/package.json +14 -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,8 @@
1
- import { stringPascalCase } from '@polkadot/util';
2
- import { CodecRegistry } from '@dedot/codecs';
3
- import { isNativeType, normalizeName } from '@dedot/utils';
4
- import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
5
- import { registry } from '@dedot/types';
6
- import { TypeImports } from './TypeImports';
1
+ import { PortableRegistry } from '@dedot/codecs';
2
+ import { normalizeName, stringPascalCase } from '@dedot/utils';
3
+ import { TypeImports } from './TypeImports.js';
4
+ import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
5
+ import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
7
6
  // Skip generate types for these
8
7
  // as we do have native types for them
9
8
  const SKIP_TYPES = [
@@ -37,7 +36,7 @@ export class TypesGen {
37
36
  typeImports;
38
37
  constructor(metadata) {
39
38
  this.metadata = metadata;
40
- this.registry = new CodecRegistry(this.metadata);
39
+ this.registry = new PortableRegistry(this.metadata);
41
40
  this.includedTypes = this.#includedTypes();
42
41
  this.typeImports = new TypeImports();
43
42
  }
@@ -99,7 +98,7 @@ export class TypesGen {
99
98
  const { tag, value } = type;
100
99
  switch (tag) {
101
100
  case 'Primitive':
102
- const $codec = this.registry.findCodec(value.kind);
101
+ const $codec = findKnownCodec(value.kind);
103
102
  if ($codec.nativeType) {
104
103
  return $codec.nativeType;
105
104
  }
@@ -168,7 +167,7 @@ export class TypesGen {
168
167
  membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
169
168
  }
170
169
  }
171
- const { tagKey, valueKey } = this.registry.portableRegistry.getEnumOptions(typeId);
170
+ const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
172
171
  return membersType
173
172
  .map(([keyName, valueType, docs]) => ({
174
173
  tag: `${tagKey}: '${keyName}'`,
@@ -263,8 +262,8 @@ export class TypesGen {
263
262
  const suffix = typeSuffixes.get(id) || '';
264
263
  let knownType = false;
265
264
  let name, nameOut;
266
- if (this.registry.isKnownType(joinedPath)) {
267
- const codecType = this.registry.findCodecType(path.at(-1));
265
+ if (isKnownCodecType(joinedPath)) {
266
+ const codecType = findKnownCodecType(path.at(-1));
268
267
  name = codecType.typeIn;
269
268
  nameOut = codecType.typeOut;
270
269
  knownType = true;
@@ -317,7 +316,7 @@ export class TypesGen {
317
316
  }
318
317
  #shouldGenerateTypeIn(id) {
319
318
  const { callTypeId } = this.metadata.extrinsic;
320
- const palletCallTypeIds = this.registry.portableRegistry.getPalletCallTypeIds();
319
+ const palletCallTypeIds = this.registry.getPalletCallTypeIds();
321
320
  return callTypeId === id || palletCallTypeIds.includes(id);
322
321
  }
323
322
  eqlCache = new Map();
@@ -422,11 +421,6 @@ export class TypesGen {
422
421
  this.typeImports.addCodecType(typeName);
423
422
  return;
424
423
  }
425
- if (registry.has(typeName)) {
426
- this.typeImports.addKnownType(typeName);
427
- }
428
- else {
429
- this.typeImports.addOutType(typeName);
430
- }
424
+ this.typeImports.addOutType(typeName);
431
425
  }
432
426
  }
@@ -1,10 +1,10 @@
1
- export * from './TypesGen';
2
- export * from './ApiGen';
3
- export * from './ConstsGen';
4
- export * from './QueryGen';
5
- export * from './RpcGen';
6
- export * from './IndexGen';
7
- export * from './ErrorsGen';
8
- export * from './EventsGen';
9
- export * from './TxGen';
10
- export * from './RuntimeApisGen';
1
+ export * from './TypesGen.js';
2
+ export * from './ApiGen.js';
3
+ export * from './ConstsGen.js';
4
+ export * from './QueryGen.js';
5
+ export * from './JsonRpcGen.js';
6
+ export * from './IndexGen.js';
7
+ export * from './ErrorsGen.js';
8
+ export * from './EventsGen.js';
9
+ export * from './TxGen.js';
10
+ export * from './RuntimeApisGen.js';
@@ -1,10 +1,10 @@
1
- export * from './TypesGen';
2
- export * from './ApiGen';
3
- export * from './ConstsGen';
4
- export * from './QueryGen';
5
- export * from './RpcGen';
6
- export * from './IndexGen';
7
- export * from './ErrorsGen';
8
- export * from './EventsGen';
9
- export * from './TxGen';
10
- export * from './RuntimeApisGen';
1
+ export * from './TypesGen.js';
2
+ export * from './ApiGen.js';
3
+ export * from './ConstsGen.js';
4
+ export * from './QueryGen.js';
5
+ export * from './JsonRpcGen.js';
6
+ export * from './IndexGen.js';
7
+ export * from './ErrorsGen.js';
8
+ export * from './EventsGen.js';
9
+ export * from './TxGen.js';
10
+ export * from './RuntimeApisGen.js';
@@ -0,0 +1,23 @@
1
+ import * as $ from '@dedot/shape';
2
+ import { AnyShape } from '@dedot/shape';
3
+ export type CodecName = `$${string}`;
4
+ export interface CodecType {
5
+ name: CodecName;
6
+ $codec: AnyShape;
7
+ typeIn: string;
8
+ typeOut: string;
9
+ }
10
+ export declare const normalizeCodecName: (name: string | CodecName) => CodecName;
11
+ /**
12
+ * Collection of codec types with loose input types
13
+ *
14
+ * Loose codecs are codecs with different typeIn & typeOut,
15
+ * E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
16
+ *
17
+ * This registry keep track the list of codecs which follow this convention
18
+ */
19
+ export declare const looseTypeCodecs: Record<string, AnyShape>;
20
+ export declare function findKnownCodecType(name: string): CodecType;
21
+ export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
22
+ export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
23
+ export declare function isKnownCodecType(path: string | string[]): boolean;
@@ -0,0 +1,120 @@
1
+ import * as Codecs from '@dedot/codecs';
2
+ import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
3
+ import * as $ from '@dedot/shape';
4
+ import { assert } from '@dedot/utils';
5
+ export const normalizeCodecName = (name) => {
6
+ return name.startsWith('$') ? name : `$${name}`;
7
+ };
8
+ // Known paths for codecs (primitives) that are shared between
9
+ // different substrate-based blockchains
10
+ const KNOWN_PATHS = [
11
+ 'sp_core::crypto::AccountId32',
12
+ 'sp_runtime::generic::era::Era',
13
+ 'sp_runtime::multiaddress::MultiAddress',
14
+ /^sp_runtime::DispatchError$/,
15
+ 'sp_runtime::ModuleError',
16
+ 'sp_runtime::TokenError',
17
+ 'sp_arithmetic::ArithmeticError',
18
+ 'sp_runtime::TransactionalError',
19
+ 'frame_support::dispatch::DispatchInfo',
20
+ 'frame_system::Phase',
21
+ 'sp_version::RuntimeVersion',
22
+ 'fp_account::AccountId20',
23
+ 'account::AccountId20',
24
+ 'polkadot_runtime_common::claims::EthereumAddress',
25
+ 'pallet_identity::types::Data',
26
+ 'sp_runtime::generic::digest::Digest',
27
+ 'sp_runtime::generic::digest::DigestItem',
28
+ 'sp_runtime::generic::header::Header',
29
+ 'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
30
+ /^primitive_types::\w+$/,
31
+ /^sp_arithmetic::per_things::\w+$/,
32
+ /^sp_arithmetic::fixed_point::\w+$/,
33
+ ];
34
+ const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
35
+ const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
36
+ const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
37
+ /**
38
+ * Collection of codec types with loose input types
39
+ *
40
+ * Loose codecs are codecs with different typeIn & typeOut,
41
+ * E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
42
+ *
43
+ * This registry keep track the list of codecs which follow this convention
44
+ */
45
+ export const looseTypeCodecs = {
46
+ $AccountId20,
47
+ $EthereumAddress,
48
+ $AccountId32,
49
+ $ConsensusEngineId,
50
+ $StorageKey,
51
+ $StorageData,
52
+ $PrefixedStorageKey,
53
+ $Bytes,
54
+ $RawBytes,
55
+ $MultiAddress,
56
+ $OpaqueExtrinsic,
57
+ $UncheckedExtrinsic,
58
+ $Era,
59
+ };
60
+ export function findKnownCodecType(name) {
61
+ const normalizedName = normalizeCodecName(name);
62
+ const $knownCodec = looseTypeCodecs[normalizedName];
63
+ if ($knownCodec) {
64
+ return {
65
+ name: normalizedName,
66
+ $codec: $knownCodec,
67
+ typeIn: `${name}Like`,
68
+ typeOut: name,
69
+ };
70
+ }
71
+ const $codec = findKnownCodec(name);
72
+ if ($codec.nativeType && $[name]) {
73
+ return {
74
+ name: normalizedName,
75
+ $codec,
76
+ typeIn: $codec.nativeType,
77
+ typeOut: $codec.nativeType,
78
+ };
79
+ }
80
+ return {
81
+ name: normalizedName,
82
+ $codec,
83
+ typeIn: name,
84
+ typeOut: name,
85
+ };
86
+ }
87
+ export function findKnownCodec(typeName) {
88
+ // @ts-ignore
89
+ const $codec = findKnownWrapperCodec(typeName) || Codecs[normalizeCodecName(typeName)] || $[typeName];
90
+ assert($codec, `Known codec not found - ${typeName}`);
91
+ return $codec;
92
+ }
93
+ export function findKnownWrapperCodec(typeName) {
94
+ const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
95
+ if (matchNames) {
96
+ const [_, wrapper, inner] = matchNames;
97
+ if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
98
+ // @ts-ignore
99
+ const $Wrapper = $[wrapper];
100
+ if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
101
+ return $Wrapper(findKnownWrapperCodec(inner));
102
+ }
103
+ const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
104
+ return $Wrapper(...$inners);
105
+ }
106
+ throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
107
+ }
108
+ else if (typeName.match(TUPLE_TYPE_REGEX)) {
109
+ const $inner = typeName
110
+ .slice(1, -1)
111
+ .split(',')
112
+ .filter((x) => x)
113
+ .map((one) => findKnownCodec(one.trim()));
114
+ return $.Tuple(...$inner);
115
+ }
116
+ }
117
+ export function isKnownCodecType(path) {
118
+ const joinedPath = Array.isArray(path) ? path.join('::') : path;
119
+ return KNOWN_PATHS.some((one) => joinedPath.match(one));
120
+ }
@@ -8,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
8
8
  * @param word
9
9
  */
10
10
  export declare const isReservedWord: (word: string) => boolean;
11
+ /**
12
+ * Check if a type is native JS/TS type
13
+ * @param type
14
+ */
15
+ export declare const isNativeType: (type: string) => boolean;
@@ -33,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
33
33
  * @param word
34
34
  */
35
35
  export const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
36
+ const TS_PRIMITIVE_TYPES = [
37
+ 'void',
38
+ 'undefined',
39
+ 'null',
40
+ 'number',
41
+ 'boolean',
42
+ 'bigint',
43
+ 'Map',
44
+ 'Set',
45
+ 'string',
46
+ 'any',
47
+ 'Array',
48
+ 'Record',
49
+ ];
50
+ /**
51
+ * Check if a type is native JS/TS type
52
+ * @param type
53
+ */
54
+ export const isNativeType = (type) => {
55
+ return TS_PRIMITIVE_TYPES.some((one) => {
56
+ if (typeof one === 'string') {
57
+ return one === type;
58
+ }
59
+ else {
60
+ return type.match(one);
61
+ }
62
+ });
63
+ };
package/index.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  import { MetadataLatest } from '@dedot/codecs';
2
- import { NetworkInfo } from './types';
3
- export declare function generateTypesFromChain(network: NetworkInfo, endpoint: string, outDir: string): Promise<void>;
4
- export declare function generateTypes(network: NetworkInfo, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
2
+ export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string): Promise<void>;
3
+ export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string): Promise<void>;
package/index.js CHANGED
@@ -1,37 +1,37 @@
1
- import { Dedot } from 'dedot';
1
+ import { stringCamelCase } from '@dedot/utils';
2
+ import { Dedot, WsProvider } from 'dedot';
2
3
  import * as fs from 'fs';
3
4
  import * as path from 'path';
4
- import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, RpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator';
5
- import { stringCamelCase } from '@polkadot/util';
6
- export async function generateTypesFromChain(network, endpoint, outDir) {
7
- const api = await Dedot.create(endpoint);
8
- const { methods } = await api.rpc.rpc.methods();
9
- const apis = api.runtimeVersion?.apis || [];
10
- if (!network.chain) {
11
- network.chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
5
+ import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, JsonRpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
6
+ export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
7
+ const api = await Dedot.new(new WsProvider(endpoint));
8
+ const { methods } = await api.rpc.rpc_methods();
9
+ const apis = api.runtimeVersion.apis || {};
10
+ if (!chain) {
11
+ chain = stringCamelCase(api.runtimeVersion.specName || 'local');
12
12
  }
13
- await generateTypes(network, api.metadataLatest, methods, apis, outDir);
13
+ await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
14
14
  await api.disconnect();
15
15
  }
16
- export async function generateTypes(network, metadata, rpcMethods, runtimeApis, outDir = '.') {
17
- const dirPath = path.resolve(outDir, network.chain);
18
- const defTypesFileName = path.join(dirPath, `types.d.ts`);
19
- const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
20
- const queryTypesFileName = path.join(dirPath, `query.d.ts`);
21
- const rpcCallsFileName = path.join(dirPath, `rpc.d.ts`);
22
- const indexFileName = path.join(dirPath, `index.d.ts`);
23
- const errorsFileName = path.join(dirPath, `errors.d.ts`);
24
- const eventsFileName = path.join(dirPath, `events.d.ts`);
25
- const runtimeApisFileName = path.join(dirPath, `runtime.d.ts`);
26
- const txFileName = path.join(dirPath, `tx.d.ts`);
16
+ export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
17
+ const dirPath = path.resolve(outDir, chain);
18
+ const defTypesFileName = path.join(dirPath, `types.${extension}`);
19
+ const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
20
+ const queryTypesFileName = path.join(dirPath, `query.${extension}`);
21
+ const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
22
+ const indexFileName = path.join(dirPath, `index.${extension}`);
23
+ const errorsFileName = path.join(dirPath, `errors.${extension}`);
24
+ const eventsFileName = path.join(dirPath, `events.${extension}`);
25
+ const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
26
+ const txFileName = path.join(dirPath, `tx.${extension}`);
27
27
  if (!fs.existsSync(dirPath)) {
28
28
  fs.mkdirSync(dirPath, { recursive: true });
29
29
  }
30
30
  const typesGen = new TypesGen(metadata);
31
31
  const constsGen = new ConstsGen(typesGen);
32
32
  const queryGen = new QueryGen(typesGen);
33
- const rpcGen = new RpcGen(typesGen, rpcMethods);
34
- const indexGen = new IndexGen(network);
33
+ const jsonRpcGen = new JsonRpcGen(typesGen, rpcMethods);
34
+ const indexGen = new IndexGen(chain);
35
35
  const errorsGen = new ErrorsGen(typesGen);
36
36
  const eventsGen = new EventsGen(typesGen);
37
37
  const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeApis);
@@ -39,7 +39,7 @@ export async function generateTypes(network, metadata, rpcMethods, runtimeApis,
39
39
  fs.writeFileSync(defTypesFileName, await typesGen.generate());
40
40
  fs.writeFileSync(errorsFileName, await errorsGen.generate());
41
41
  fs.writeFileSync(eventsFileName, await eventsGen.generate());
42
- fs.writeFileSync(rpcCallsFileName, await rpcGen.generate());
42
+ fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
43
43
  fs.writeFileSync(queryTypesFileName, await queryGen.generate());
44
44
  fs.writeFileSync(constsTypesFileName, await constsGen.generate());
45
45
  fs.writeFileSync(txFileName, await txGen.generate());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/codegen",
3
- "version": "0.0.1-next.3+b7d0d04",
3
+ "version": "0.0.1-next.3662494a.39+3662494",
4
4
  "description": "Generate types",
5
5
  "author": "Thang X. Vu <thang@coongcrafts.io>",
6
6
  "homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
@@ -10,6 +10,7 @@
10
10
  "url": "https://github.com/dedotdev/dedot.git"
11
11
  },
12
12
  "main": "./cjs/index.js",
13
+ "sideEffects": false,
13
14
  "type": "module",
14
15
  "scripts": {
15
16
  "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && yarn copy",
@@ -17,33 +18,28 @@
17
18
  "copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
18
19
  },
19
20
  "dependencies": {
20
- "@dedot/codecs": "0.0.1-next.3+b7d0d04",
21
- "@dedot/shape": "0.0.1-next.3+b7d0d04",
22
- "@dedot/specs": "0.0.1-next.3+b7d0d04",
23
- "@dedot/utils": "0.0.1-next.3+b7d0d04",
24
- "@polkadot/util": "^12.6.2",
25
- "dedot": "0.0.1-next.3+b7d0d04",
26
- "handlebars": "^4.7.8",
27
- "prettier": "^3.0.3"
21
+ "@dedot/codecs": "0.0.1-next.3662494a.39+3662494",
22
+ "@dedot/shape": "0.0.1-next.3662494a.39+3662494",
23
+ "@dedot/specs": "0.0.1-next.3662494a.39+3662494",
24
+ "@dedot/utils": "0.0.1-next.3662494a.39+3662494",
25
+ "dedot": "0.0.1-next.3662494a.39+3662494",
26
+ "handlebars": "^4.7.7",
27
+ "prettier": "^3.2.5"
28
28
  },
29
29
  "publishConfig": {
30
30
  "access": "public",
31
31
  "directory": "dist"
32
32
  },
33
33
  "license": "Apache-2.0",
34
- "gitHead": "b7d0d0424968ebfb2b3e5e573f2e375ab8c30442",
34
+ "gitHead": "3662494a12d632c9e3c2315544c652fe6e92929d",
35
35
  "module": "./index.js",
36
36
  "types": "./index.d.ts",
37
37
  "exports": {
38
38
  ".": {
39
- "import": {
40
- "types": "./index.d.ts",
41
- "default": "./index.js"
42
- },
43
- "require": {
44
- "types": "./index.d.ts",
45
- "default": "./cjs/index.js"
46
- }
39
+ "types": "./index.d.ts",
40
+ "import": "./index.js",
41
+ "require": "./cjs/index.js",
42
+ "default": "./index.js"
47
43
  }
48
44
  }
49
45
  }
@@ -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
  }
@@ -2,6 +2,6 @@
2
2
 
3
3
  {{{ importTypes }}}
4
4
 
5
- export interface ChainEvents extends GenericChainEvents {
5
+ export interface ChainEvents<Rv extends RpcVersion> extends GenericChainEvents<Rv> {
6
6
  {{{ defTypeOut }}}
7
7
  }
@@ -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
 
package/templates/tx.hbs CHANGED
@@ -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,79 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const index_1 = require("./index");
7
- const static_substrate_1 = require("@polkadot/types-support/metadata/static-substrate");
8
- const substrate_hex_1 = __importDefault(require("@polkadot/types-support/metadata/v15/substrate-hex"));
9
- const codecs_1 = require("@dedot/codecs");
10
- const dedot_1 = require("dedot");
11
- const NETWORKS = [
12
- {
13
- chain: 'substrate',
14
- metadataHex: substrate_hex_1.default,
15
- rpcMethods: static_substrate_1.rpc.methods,
16
- },
17
- {
18
- chain: 'polkadot',
19
- endpoint: 'wss://rpc.polkadot.io',
20
- },
21
- {
22
- chain: 'kusama',
23
- endpoint: 'wss://kusama-rpc.polkadot.io',
24
- },
25
- {
26
- chain: 'astar',
27
- endpoint: 'wss://rpc.astar.network',
28
- },
29
- {
30
- chain: 'moonbeam',
31
- endpoint: 'wss://moonbeam.api.onfinality.io/public-ws',
32
- },
33
- {
34
- chain: 'polkadotAssetHub',
35
- endpoint: 'wss://polkadot-asset-hub-rpc.polkadot.io/',
36
- },
37
- {
38
- chain: 'kusamaAssetHub',
39
- endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
40
- },
41
- {
42
- chain: 'rococoAssetHub',
43
- endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
44
- },
45
- {
46
- chain: 'aleph',
47
- endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
48
- },
49
- {
50
- chain: 'westendAssetHub',
51
- endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
52
- },
53
- ];
54
- const OUT_DIR = 'packages/chaintypes/src';
55
- async function run() {
56
- for (const network of NETWORKS) {
57
- const { chain, endpoint, metadataHex, rpcMethods } = network;
58
- if (endpoint) {
59
- console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
60
- await (0, index_1.generateTypesFromChain)(network, endpoint, OUT_DIR);
61
- }
62
- else if (metadataHex && rpcMethods) {
63
- console.log(`Generate types for ${chain} via raw data`);
64
- const metadata = codecs_1.$Metadata.tryDecode(metadataHex);
65
- const runtimeVersion = getRuntimeVersion(metadata);
66
- await (0, index_1.generateTypes)(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
67
- }
68
- }
69
- console.log('DONE!');
70
- }
71
- const getRuntimeVersion = (metadata) => {
72
- const registry = new codecs_1.CodecRegistry(metadata.latest);
73
- const executor = new dedot_1.ConstantExecutor({
74
- registry,
75
- metadataLatest: metadata.latest,
76
- });
77
- return executor.execute('system', 'version');
78
- };
79
- run().catch(console.log);