@dedot/codegen 0.0.1-alpha.3 → 0.0.1-alpha.31

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 (73) hide show
  1. package/cjs/generator/ConstsGen.js +9 -10
  2. package/cjs/generator/ErrorsGen.js +12 -13
  3. package/cjs/generator/EventsGen.js +13 -14
  4. package/cjs/generator/IndexGen.js +8 -9
  5. package/cjs/generator/QueryGen.js +9 -10
  6. package/cjs/generator/RpcGen.js +18 -18
  7. package/cjs/generator/RuntimeApisGen.js +34 -36
  8. package/cjs/generator/TxGen.js +15 -15
  9. package/cjs/generator/TypesGen.js +24 -24
  10. package/cjs/{types.js → generator/dirname.js} +3 -0
  11. package/cjs/generator/index.js +10 -10
  12. package/cjs/generator/known-codecs.js +151 -0
  13. package/cjs/generator/known-types.js +33 -0
  14. package/cjs/generator/utils.js +33 -9
  15. package/cjs/index.js +29 -29
  16. package/cjs/templates/consts.hbs +7 -0
  17. package/cjs/templates/errors.hbs +7 -0
  18. package/cjs/templates/events.hbs +7 -0
  19. package/cjs/templates/index.hbs +22 -0
  20. package/cjs/templates/query.hbs +7 -0
  21. package/cjs/templates/rpc.hbs +7 -0
  22. package/cjs/templates/runtime.hbs +8 -0
  23. package/cjs/templates/tx.hbs +9 -0
  24. package/cjs/templates/types.hbs +5 -0
  25. package/generator/ApiGen.d.ts +3 -3
  26. package/generator/ConstsGen.d.ts +1 -1
  27. package/generator/ConstsGen.js +4 -5
  28. package/generator/ErrorsGen.d.ts +1 -1
  29. package/generator/ErrorsGen.js +3 -4
  30. package/generator/EventsGen.d.ts +1 -1
  31. package/generator/EventsGen.js +3 -4
  32. package/generator/IndexGen.d.ts +2 -3
  33. package/generator/IndexGen.js +6 -7
  34. package/generator/QueryGen.d.ts +1 -1
  35. package/generator/QueryGen.js +4 -5
  36. package/generator/RpcGen.d.ts +1 -1
  37. package/generator/RpcGen.js +4 -4
  38. package/generator/RuntimeApisGen.d.ts +2 -2
  39. package/generator/RuntimeApisGen.js +23 -25
  40. package/generator/TxGen.d.ts +1 -1
  41. package/generator/TxGen.js +3 -3
  42. package/generator/TypesGen.d.ts +3 -3
  43. package/generator/TypesGen.js +13 -13
  44. package/generator/dirname.d.ts +1 -0
  45. package/generator/dirname.js +6 -0
  46. package/generator/index.d.ts +10 -10
  47. package/generator/index.js +10 -10
  48. package/generator/known-codecs.d.ts +23 -0
  49. package/generator/known-codecs.js +120 -0
  50. package/generator/known-types.d.ts +6 -0
  51. package/generator/known-types.js +30 -0
  52. package/generator/utils.d.ts +5 -1
  53. package/generator/utils.js +31 -7
  54. package/index.d.ts +2 -3
  55. package/index.js +19 -19
  56. package/package.json +11 -10
  57. package/templates/consts.hbs +7 -0
  58. package/templates/errors.hbs +7 -0
  59. package/templates/events.hbs +7 -0
  60. package/templates/index.hbs +22 -0
  61. package/templates/query.hbs +7 -0
  62. package/templates/rpc.hbs +7 -0
  63. package/templates/runtime.hbs +8 -0
  64. package/templates/tx.hbs +9 -0
  65. package/templates/types.hbs +5 -0
  66. package/cjs/genSupportedChainTypes.js +0 -79
  67. package/cjs/packageInfo.js +0 -5
  68. package/genSupportedChainTypes.d.ts +0 -1
  69. package/genSupportedChainTypes.js +0 -74
  70. package/packageInfo.d.ts +0 -4
  71. package/packageInfo.js +0 -2
  72. package/types.d.ts +0 -6
  73. package/types.js +0 -1
@@ -1,7 +1,6 @@
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 '../generator/index.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class ConstsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -15,7 +14,7 @@ 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,
@@ -1,4 +1,4 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class ErrorsGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,7 +1,6 @@
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 { ApiGen } from './ApiGen.js';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
5
4
  export class ErrorsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -1,4 +1,4 @@
1
- import { ApiGen } from './ApiGen';
1
+ import { ApiGen } from './ApiGen.js';
2
2
  export declare class EventsGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,7 +1,6 @@
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 { ApiGen } from './ApiGen.js';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
5
4
  export class EventsGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -1,6 +1,5 @@
1
- import { NetworkInfo } from '../types';
2
1
  export declare class IndexGen {
3
- readonly networkInfo: NetworkInfo;
4
- constructor(networkInfo: NetworkInfo);
2
+ readonly chain: string;
3
+ constructor(chain: string);
5
4
  generate(): Promise<string>;
6
5
  }
@@ -1,13 +1,12 @@
1
- import { beautifySourceCode, compileTemplate } from './utils';
2
- import { stringPascalCase } from '@polkadot/util';
1
+ import { beautifySourceCode, compileTemplate } from './utils.js';
2
+ import { stringPascalCase } from '@dedot/utils';
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
8
  async generate() {
9
- const { chain } = this.networkInfo;
10
- const interfaceName = stringPascalCase(chain);
9
+ const interfaceName = stringPascalCase(this.chain);
11
10
  const template = compileTemplate('index.hbs');
12
11
  return beautifySourceCode(template({ interfaceName }));
13
12
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.js';
2
2
  export declare class QueryGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,7 +1,6 @@
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 '../generator/index.js';
3
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
5
4
  export class QueryGen extends ApiGen {
6
5
  generate() {
7
6
  const { pallets } = this.metadata;
@@ -15,7 +14,7 @@ export class QueryGen extends ApiGen {
15
14
  const queries = storage.entries.map((one) => this.#generateEntry(one));
16
15
  const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
17
16
  defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
18
- defTypeOut += `${stringLowerFirst(pallet.name)}: {
17
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
18
  ${queryDefs.join(',\n')}
20
19
 
21
20
  ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
@@ -1,4 +1,4 @@
1
- import { ApiGen, TypesGen } from '../generator';
1
+ import { ApiGen, TypesGen } from '../generator/index.js';
2
2
  export declare class RpcGen extends ApiGen {
3
3
  #private;
4
4
  readonly typesGen: TypesGen;
@@ -1,7 +1,7 @@
1
1
  import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs';
2
- import { isNativeType } from '@dedot/utils';
3
- import { ApiGen } from '../generator';
4
- import { beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX } from './utils';
2
+ import { ApiGen } from '../generator/index.js';
3
+ import { isNativeType, beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
4
+ import { findKnownCodecType } from './known-codecs.js';
5
5
  const HIDDEN_RPCS = [
6
6
  // Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
7
7
  'rpc_methods',
@@ -165,7 +165,7 @@ export class RpcGen extends ApiGen {
165
165
  return type;
166
166
  }
167
167
  #getCodecType(type, toTypeIn = true) {
168
- const { typeIn, typeOut } = this.registry.findCodecType(type);
168
+ const { typeIn, typeOut } = findKnownCodecType(type);
169
169
  return toTypeIn ? typeIn : typeOut;
170
170
  }
171
171
  }
@@ -1,5 +1,5 @@
1
- import { TypesGen } from './TypesGen';
2
- import { RpcGen } from './RpcGen';
1
+ import { TypesGen } from './TypesGen.js';
2
+ import { RpcGen } from './RpcGen.js';
3
3
  export declare class RuntimeApisGen extends RpcGen {
4
4
  #private;
5
5
  readonly typesGen: TypesGen;
@@ -1,8 +1,7 @@
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';
1
+ import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
2
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
3
+ import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
4
+ import { RpcGen } from './RpcGen.js';
6
5
  export class RuntimeApisGen extends RpcGen {
7
6
  typesGen;
8
7
  runtimeApis;
@@ -18,7 +17,7 @@ export class RuntimeApisGen extends RpcGen {
18
17
  if (this.metadata.apis.length > 0) {
19
18
  this.metadata.apis.forEach((runtimeApi) => {
20
19
  const { name: runtimeApiName, methods } = runtimeApi;
21
- runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calculateRuntimeApiHash(runtimeApiName)}`);
20
+ runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calcRuntimeApiHash(runtimeApiName)}`);
22
21
  runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
23
22
  ${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
24
23
 
@@ -27,18 +26,20 @@ export class RuntimeApisGen extends RpcGen {
27
26
  });
28
27
  }
29
28
  else {
30
- const specsByModule = this.#runtimeApisSpecsByModule();
31
- Object.values(specsByModule).forEach((specs) => {
32
- specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
33
- runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
34
- runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
29
+ const specs = this.#targetRuntimeApiSpecs();
30
+ specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
31
+ runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
32
+ runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
35
33
  ${Object.keys(methods)
36
- .map((methodName) => this.#generateMethodDefFromSpec({ ...methods[methodName], runtimeApiName, methodName }))
37
- .join('\n')}
34
+ .map((methodName) => this.#generateMethodDefFromSpec({
35
+ ...methods[methodName],
36
+ runtimeApiName,
37
+ methodName,
38
+ }))
39
+ .join('\n')}
38
40
 
39
41
  ${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
40
42
  }`;
41
- });
42
43
  });
43
44
  }
44
45
  const importTypes = this.typesGen.typeImports.toImports();
@@ -89,9 +90,9 @@ export class RuntimeApisGen extends RpcGen {
89
90
  .join(', ');
90
91
  return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
91
92
  }
92
- #runtimeApisSpecsByModule() {
93
+ #targetRuntimeApiSpecs() {
93
94
  const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
94
- const runtimeApiSpec = findRuntimeApiSpec(runtimeApiHash, version);
95
+ const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
95
96
  if (!runtimeApiSpec)
96
97
  return;
97
98
  return {
@@ -103,14 +104,11 @@ export class RuntimeApisGen extends RpcGen {
103
104
  if (!spec) {
104
105
  return o;
105
106
  }
106
- const { moduleName } = spec;
107
- if (!moduleName) {
108
- return o;
109
- }
110
- return {
111
- ...o,
112
- [moduleName]: o[moduleName] ? [...o[moduleName], spec] : [spec],
113
- };
114
- }, {});
107
+ return [...o, spec];
108
+ }, []);
115
109
  }
110
+ #findRuntimeApiSpec = (runtimeApiHash, version) => {
111
+ const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
112
+ return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
113
+ };
116
114
  }
@@ -1,4 +1,4 @@
1
- import { ApiGen } from '../generator';
1
+ import { ApiGen } from '../generator/index.js';
2
2
  export declare class TxGen extends ApiGen {
3
3
  #private;
4
4
  generate(): Promise<string>;
@@ -1,6 +1,6 @@
1
- import { ApiGen } from '../generator';
2
- import { stringCamelCase, stringPascalCase } from '@polkadot/util';
3
- import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils';
1
+ import { ApiGen } from '../generator/index.js';
2
+ import { stringCamelCase, stringPascalCase } from '@dedot/utils';
3
+ import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
4
4
  export class TxGen extends ApiGen {
5
5
  generate() {
6
6
  const { pallets, types } = this.metadata;
@@ -1,5 +1,5 @@
1
- import { CodecRegistry, Field, MetadataLatest, PortableType, TypeId } from '@dedot/codecs';
2
- import { TypeImports } from './TypeImports';
1
+ import { PortableRegistry, Field, MetadataLatest, PortableType, TypeId } from '@dedot/codecs';
2
+ import { TypeImports } from './TypeImports.js';
3
3
  interface NamedType extends PortableType {
4
4
  name: string;
5
5
  nameOut: string;
@@ -15,7 +15,7 @@ export declare class TypesGen {
15
15
  * Types will be generated its definition out.
16
16
  */
17
17
  includedTypes: Record<TypeId, NamedType>;
18
- registry: CodecRegistry;
18
+ registry: PortableRegistry;
19
19
  typeImports: TypeImports;
20
20
  constructor(metadata: MetadataLatest);
21
21
  generate(): Promise<string>;
@@ -1,9 +1,9 @@
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 { isNativeType, beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
4
+ import { knownTypes } from './known-types.js';
5
+ import { TypeImports } from './TypeImports.js';
6
+ import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
7
7
  // Skip generate types for these
8
8
  // as we do have native types for them
9
9
  const SKIP_TYPES = [
@@ -37,7 +37,7 @@ export class TypesGen {
37
37
  typeImports;
38
38
  constructor(metadata) {
39
39
  this.metadata = metadata;
40
- this.registry = new CodecRegistry(this.metadata);
40
+ this.registry = new PortableRegistry(this.metadata);
41
41
  this.includedTypes = this.#includedTypes();
42
42
  this.typeImports = new TypeImports();
43
43
  }
@@ -99,7 +99,7 @@ export class TypesGen {
99
99
  const { tag, value } = type;
100
100
  switch (tag) {
101
101
  case 'Primitive':
102
- const $codec = this.registry.findCodec(value.kind);
102
+ const $codec = findKnownCodec(value.kind);
103
103
  if ($codec.nativeType) {
104
104
  return $codec.nativeType;
105
105
  }
@@ -168,7 +168,7 @@ export class TypesGen {
168
168
  membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
169
169
  }
170
170
  }
171
- const { tagKey, valueKey } = this.registry.portableRegistry.getEnumOptions(typeId);
171
+ const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
172
172
  return membersType
173
173
  .map(([keyName, valueType, docs]) => ({
174
174
  tag: `${tagKey}: '${keyName}'`,
@@ -263,8 +263,8 @@ export class TypesGen {
263
263
  const suffix = typeSuffixes.get(id) || '';
264
264
  let knownType = false;
265
265
  let name, nameOut;
266
- if (this.registry.isKnownType(joinedPath)) {
267
- const codecType = this.registry.findCodecType(path.at(-1));
266
+ if (isKnownCodecType(joinedPath)) {
267
+ const codecType = findKnownCodecType(path.at(-1));
268
268
  name = codecType.typeIn;
269
269
  nameOut = codecType.typeOut;
270
270
  knownType = true;
@@ -317,7 +317,7 @@ export class TypesGen {
317
317
  }
318
318
  #shouldGenerateTypeIn(id) {
319
319
  const { callTypeId } = this.metadata.extrinsic;
320
- const palletCallTypeIds = this.registry.portableRegistry.getPalletCallTypeIds();
320
+ const palletCallTypeIds = this.registry.getPalletCallTypeIds();
321
321
  return callTypeId === id || palletCallTypeIds.includes(id);
322
322
  }
323
323
  eqlCache = new Map();
@@ -422,7 +422,7 @@ export class TypesGen {
422
422
  this.typeImports.addCodecType(typeName);
423
423
  return;
424
424
  }
425
- if (registry.has(typeName)) {
425
+ if (knownTypes.includes(typeName)) {
426
426
  this.typeImports.addKnownType(typeName);
427
427
  }
428
428
  else {
@@ -0,0 +1 @@
1
+ export function currentDirname(): string;
@@ -0,0 +1,6 @@
1
+ import { dirname } from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ export const currentDirname = () => {
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ return dirname(__filename);
6
+ };
@@ -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 './RpcGen.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 './RpcGen.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 $ from '@dedot/shape';
2
+ import * as Codecs from '@dedot/codecs';
3
+ import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
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
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Known type names registered in @dedot/types
3
+ * Since TS interfaces are trim-off when compiling,
4
+ * So we need to register them explicitly here for RPC codegen process
5
+ */
6
+ export declare const knownTypes: string[];
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Known type names registered in @dedot/types
3
+ * Since TS interfaces are trim-off when compiling,
4
+ * So we need to register them explicitly here for RPC codegen process
5
+ */
6
+ export const knownTypes = [
7
+ 'ExtrinsicOrHash',
8
+ 'EpochAuthorship',
9
+ 'BlockStats',
10
+ 'Prevotes',
11
+ 'Precommits',
12
+ 'RoundState',
13
+ 'ReportedRoundStates',
14
+ 'JustificationNotification',
15
+ 'EncodedFinalityProofs',
16
+ 'LeavesProof',
17
+ 'StorageKind',
18
+ 'RpcMethods',
19
+ 'ReadProof',
20
+ 'StorageChangeSet',
21
+ 'TraceBlockResponse',
22
+ 'MigrationStatusResult',
23
+ 'ChainType',
24
+ 'ChainProperties',
25
+ 'Health',
26
+ 'SyncState',
27
+ 'PeerInfo',
28
+ 'NodeRole',
29
+ 'NetworkState',
30
+ ];
@@ -1,7 +1,6 @@
1
1
  export declare const WRAPPER_TYPE_REGEX: RegExp;
2
2
  export declare const TUPLE_TYPE_REGEX: RegExp;
3
3
  export declare const commentBlock: (...docs: (string | string[])[]) => string;
4
- export declare const resolveFilePath: (relativePath: string | string[]) => string;
5
4
  export declare const beautifySourceCode: (source: string) => Promise<string>;
6
5
  export declare const compileTemplate: (templateFileName: string) => HandlebarsTemplateDelegate<any>;
7
6
  /**
@@ -9,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
9
8
  * @param word
10
9
  */
11
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;