@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,8 +1,8 @@
1
1
  import * as fs from 'fs';
2
2
  import handlebars from 'handlebars';
3
3
  import * as path from 'path';
4
- import * as process from 'process';
5
4
  import * as prettier from 'prettier';
5
+ import { currentDirname } from './dirname';
6
6
  export const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
7
7
  export const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
8
8
  export const commentBlock = (...docs) => {
@@ -18,16 +18,12 @@ ${flatLines.map((line) => `* ${line.replaceAll(/\s+/g, ' ').trim()}`).join('\n')
18
18
  `;
19
19
  }
20
20
  };
21
- export const resolveFilePath = (relativePath) => {
22
- relativePath = Array.isArray(relativePath) ? relativePath : [relativePath];
23
- return path.resolve(process.cwd(), ...relativePath);
24
- };
25
21
  export const beautifySourceCode = async (source) => {
26
- const prettierOptions = await prettier.resolveConfig(resolveFilePath('./.prettierrc.js'));
22
+ const prettierOptions = await prettier.resolveConfig(path.resolve(currentDirname(), '../../../../.prettierrc.js'));
27
23
  return prettier.format(source, { parser: 'babel-ts', ...prettierOptions });
28
24
  };
29
25
  export const compileTemplate = (templateFileName) => {
30
- const templateFilePath = resolveFilePath(`packages/codegen/src/templates/${templateFileName}`);
26
+ const templateFilePath = path.resolve(currentDirname(), `../templates/${templateFileName}`);
31
27
  return handlebars.compile(fs.readFileSync(templateFilePath, 'utf8'));
32
28
  };
33
29
  // TODO add more reserved words
@@ -37,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
37
33
  * @param word
38
34
  */
39
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): Promise<void>;
3
+ export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
package/index.js CHANGED
@@ -1,29 +1,29 @@
1
1
  import { Dedot } from 'dedot';
2
2
  import * as fs from 'fs';
3
3
  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);
4
+ import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, RpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
5
+ import { stringCamelCase } from '@dedot/utils';
6
+ export async function generateTypesFromEndpoint(chain, endpoint, outDir) {
7
+ const api = await Dedot.new(endpoint);
8
8
  const { methods } = await api.rpc.rpc.methods();
9
9
  const apis = api.runtimeVersion?.apis || [];
10
- if (!network.chain) {
11
- network.chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
10
+ if (!chain) {
11
+ chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
12
12
  }
13
- await generateTypes(network, api.metadataLatest, methods, apis, outDir);
13
+ await generateTypes(chain, api.metadataLatest, methods, apis, outDir);
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.ts`);
19
- const constsTypesFileName = path.join(dirPath, `consts.ts`);
20
- const queryTypesFileName = path.join(dirPath, `query.ts`);
21
- const rpcCallsFileName = path.join(dirPath, `rpc.ts`);
22
- const indexFileName = path.join(dirPath, `index.ts`);
23
- const errorsFileName = path.join(dirPath, `errors.ts`);
24
- const eventsFileName = path.join(dirPath, `events.ts`);
25
- const runtimeApisFileName = path.join(dirPath, `runtime.ts`);
26
- const txFileName = path.join(dirPath, `tx.ts`);
16
+ export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.') {
17
+ const dirPath = path.resolve(outDir, 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`);
27
27
  if (!fs.existsSync(dirPath)) {
28
28
  fs.mkdirSync(dirPath, { recursive: true });
29
29
  }
@@ -31,7 +31,7 @@ export async function generateTypes(network, metadata, rpcMethods, runtimeApis,
31
31
  const constsGen = new ConstsGen(typesGen);
32
32
  const queryGen = new QueryGen(typesGen);
33
33
  const rpcGen = new RpcGen(typesGen, rpcMethods);
34
- const indexGen = new IndexGen(network);
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dedot/codegen",
3
- "version": "0.0.1-alpha.3+a40e2d2",
3
+ "version": "0.0.1-alpha.31",
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,18 +10,19 @@
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
- "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json",
16
- "clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo"
16
+ "build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && yarn copy",
17
+ "clean": "rm -rf ./dist && rm -rf ./tsconfig.tsbuildinfo ./tsconfig.build.tsbuildinfo",
18
+ "copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
17
19
  },
18
20
  "dependencies": {
19
- "@dedot/codecs": "0.0.1-alpha.3+a40e2d2",
20
- "@dedot/shape": "0.0.1-alpha.3+a40e2d2",
21
- "@dedot/specs": "0.0.1-alpha.3+a40e2d2",
22
- "@dedot/utils": "0.0.1-alpha.3+a40e2d2",
23
- "@polkadot/util": "^12.6.2",
24
- "dedot": "0.0.1-alpha.3+a40e2d2",
21
+ "@dedot/codecs": "0.0.1-alpha.31",
22
+ "@dedot/shape": "0.0.1-alpha.31",
23
+ "@dedot/specs": "0.0.1-alpha.31",
24
+ "@dedot/utils": "0.0.1-alpha.31",
25
+ "dedot": "0.0.1-alpha.31",
25
26
  "handlebars": "^4.7.8",
26
27
  "prettier": "^3.0.3"
27
28
  },
@@ -30,7 +31,7 @@
30
31
  "directory": "dist"
31
32
  },
32
33
  "license": "Apache-2.0",
33
- "gitHead": "a40e2d23db9f7245e816c026cd44c7a4313d5434",
34
+ "gitHead": "082111d893805a81cff92968cfb5b7b2446a08c5",
34
35
  "module": "./index.js",
35
36
  "types": "./index.d.ts",
36
37
  "exports": {
@@ -0,0 +1,7 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainConsts extends GenericChainConsts {
6
+ {{{ defTypeOut }}}
7
+ }
@@ -0,0 +1,7 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainErrors extends GenericChainErrors {
6
+ {{{ defTypeOut }}}
7
+ }
@@ -0,0 +1,7 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainEvents extends GenericChainEvents {
6
+ {{{ defTypeOut }}}
7
+ }
@@ -0,0 +1,22 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ import { GenericSubstrateApi } from '@dedot/types';
4
+ import { ChainConsts } from './consts';
5
+ import { ChainStorage } from './query';
6
+ import { RpcCalls } from './rpc';
7
+ import { ChainErrors } from './errors';
8
+ import { ChainEvents } from './events';
9
+ import { RuntimeApis } from './runtime';
10
+ import { ChainTx } from './tx';
11
+
12
+ export * from './types';
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;
22
+ }
@@ -0,0 +1,7 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface ChainStorage extends GenericChainStorage {
6
+ {{{ defTypeOut }}}
7
+ }
@@ -0,0 +1,7 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface RpcCalls extends GenericRpcCalls {
6
+ {{{rpcCallsOut}}}
7
+ }
@@ -0,0 +1,8 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ export interface RuntimeApis extends GenericRuntimeApis {
6
+ {{{ runtimeCallsOut }}}
7
+ }
8
+
@@ -0,0 +1,9 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ {{{ defTypes }}}
6
+
7
+ export interface ChainTx extends GenericChainTx<TxCall> {
8
+ {{{ txDefsOut }}}
9
+ }
@@ -0,0 +1,5 @@
1
+ // Generated by @dedot/codegen
2
+
3
+ {{{ importTypes }}}
4
+
5
+ {{{ defTypeOut }}}
@@ -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);
@@ -1,5 +0,0 @@
1
- "use strict";
2
- // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.packageInfo = void 0;
5
- exports.packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.23' };
@@ -1 +0,0 @@
1
- export {};
@@ -1,74 +0,0 @@
1
- import { generateTypes, generateTypesFromChain } from './index';
2
- import { rpc } from '@polkadot/types-support/metadata/static-substrate';
3
- import staticSubstrate from '@polkadot/types-support/metadata/v15/substrate-hex';
4
- import { $Metadata, CodecRegistry } from '@dedot/codecs';
5
- import { ConstantExecutor } from 'dedot';
6
- const NETWORKS = [
7
- {
8
- chain: 'substrate',
9
- metadataHex: staticSubstrate,
10
- rpcMethods: rpc.methods,
11
- },
12
- {
13
- chain: 'polkadot',
14
- endpoint: 'wss://rpc.polkadot.io',
15
- },
16
- {
17
- chain: 'kusama',
18
- endpoint: 'wss://kusama-rpc.polkadot.io',
19
- },
20
- {
21
- chain: 'astar',
22
- endpoint: 'wss://rpc.astar.network',
23
- },
24
- {
25
- chain: 'moonbeam',
26
- endpoint: 'wss://moonbeam.api.onfinality.io/public-ws',
27
- },
28
- {
29
- chain: 'polkadotAssetHub',
30
- endpoint: 'wss://polkadot-asset-hub-rpc.polkadot.io/',
31
- },
32
- {
33
- chain: 'kusamaAssetHub',
34
- endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
35
- },
36
- {
37
- chain: 'rococoAssetHub',
38
- endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
39
- },
40
- {
41
- chain: 'aleph',
42
- endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
43
- },
44
- {
45
- chain: 'westendAssetHub',
46
- endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
47
- },
48
- ];
49
- const OUT_DIR = 'packages/chaintypes/src';
50
- async function run() {
51
- for (const network of NETWORKS) {
52
- const { chain, endpoint, metadataHex, rpcMethods } = network;
53
- if (endpoint) {
54
- console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
55
- await generateTypesFromChain(network, endpoint, OUT_DIR);
56
- }
57
- else if (metadataHex && rpcMethods) {
58
- console.log(`Generate types for ${chain} via raw data`);
59
- const metadata = $Metadata.tryDecode(metadataHex);
60
- const runtimeVersion = getRuntimeVersion(metadata);
61
- await generateTypes(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
62
- }
63
- }
64
- console.log('DONE!');
65
- }
66
- const getRuntimeVersion = (metadata) => {
67
- const registry = new CodecRegistry(metadata.latest);
68
- const executor = new ConstantExecutor({
69
- registry,
70
- metadataLatest: metadata.latest,
71
- });
72
- return executor.execute('system', 'version');
73
- };
74
- run().catch(console.log);
package/packageInfo.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export declare const packageInfo: {
2
- name: string;
3
- version: string;
4
- };
package/packageInfo.js DELETED
@@ -1,2 +0,0 @@
1
- // THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
2
- export const packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.23' };
package/types.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export type NetworkInfo = {
2
- chain: string;
3
- endpoint?: string;
4
- metadataHex?: string;
5
- rpcMethods?: string[];
6
- };
package/types.js DELETED
@@ -1 +0,0 @@
1
- export {};