@dedot/codegen 0.0.1-alpha.11

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 (76) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +3 -0
  3. package/cjs/genSupportedChainTypes.js +79 -0
  4. package/cjs/generator/ApiGen.js +16 -0
  5. package/cjs/generator/ConstsGen.js +32 -0
  6. package/cjs/generator/ErrorsGen.js +41 -0
  7. package/cjs/generator/EventsGen.js +68 -0
  8. package/cjs/generator/IndexGen.js +18 -0
  9. package/cjs/generator/QueryGen.js +59 -0
  10. package/cjs/generator/RpcGen.js +175 -0
  11. package/cjs/generator/RuntimeApisGen.js +120 -0
  12. package/cjs/generator/TxGen.js +90 -0
  13. package/cjs/generator/TypeImports.js +56 -0
  14. package/cjs/generator/TypesGen.js +436 -0
  15. package/cjs/generator/dirname.js +5 -0
  16. package/cjs/generator/index.js +26 -0
  17. package/cjs/generator/utils.js +68 -0
  18. package/cjs/index.js +76 -0
  19. package/cjs/package.json +1 -0
  20. package/cjs/packageInfo.js +5 -0
  21. package/cjs/templates/consts.hbs +7 -0
  22. package/cjs/templates/errors.hbs +7 -0
  23. package/cjs/templates/events.hbs +7 -0
  24. package/cjs/templates/index.hbs +22 -0
  25. package/cjs/templates/query.hbs +7 -0
  26. package/cjs/templates/rpc.hbs +7 -0
  27. package/cjs/templates/runtime.hbs +8 -0
  28. package/cjs/templates/tx.hbs +9 -0
  29. package/cjs/templates/types.hbs +5 -0
  30. package/cjs/types.js +2 -0
  31. package/genSupportedChainTypes.d.ts +1 -0
  32. package/genSupportedChainTypes.js +74 -0
  33. package/generator/ApiGen.d.ts +138 -0
  34. package/generator/ApiGen.js +12 -0
  35. package/generator/ConstsGen.d.ts +4 -0
  36. package/generator/ConstsGen.js +28 -0
  37. package/generator/ErrorsGen.d.ts +5 -0
  38. package/generator/ErrorsGen.js +37 -0
  39. package/generator/EventsGen.d.ts +5 -0
  40. package/generator/EventsGen.js +64 -0
  41. package/generator/IndexGen.d.ts +6 -0
  42. package/generator/IndexGen.js +14 -0
  43. package/generator/QueryGen.d.ts +5 -0
  44. package/generator/QueryGen.js +55 -0
  45. package/generator/RpcGen.d.ts +10 -0
  46. package/generator/RpcGen.js +171 -0
  47. package/generator/RuntimeApisGen.d.ts +9 -0
  48. package/generator/RuntimeApisGen.js +116 -0
  49. package/generator/TxGen.d.ts +5 -0
  50. package/generator/TxGen.js +86 -0
  51. package/generator/TypeImports.d.ts +14 -0
  52. package/generator/TypeImports.js +52 -0
  53. package/generator/TypesGen.d.ts +30 -0
  54. package/generator/TypesGen.js +432 -0
  55. package/generator/dirname.d.ts +1 -0
  56. package/generator/dirname.js +6 -0
  57. package/generator/index.d.ts +10 -0
  58. package/generator/index.js +10 -0
  59. package/generator/utils.d.ts +10 -0
  60. package/generator/utils.js +35 -0
  61. package/index.d.ts +4 -0
  62. package/index.js +48 -0
  63. package/package.json +49 -0
  64. package/packageInfo.d.ts +4 -0
  65. package/packageInfo.js +2 -0
  66. package/templates/consts.hbs +7 -0
  67. package/templates/errors.hbs +7 -0
  68. package/templates/events.hbs +7 -0
  69. package/templates/index.hbs +22 -0
  70. package/templates/query.hbs +7 -0
  71. package/templates/rpc.hbs +7 -0
  72. package/templates/runtime.hbs +8 -0
  73. package/templates/tx.hbs +9 -0
  74. package/templates/types.hbs +5 -0
  75. package/types.d.ts +6 -0
  76. package/types.js +1 -0
@@ -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 }}}
package/cjs/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
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);
@@ -0,0 +1,138 @@
1
+ import { TypesGen } from '../generator';
2
+ export declare abstract class ApiGen {
3
+ readonly typesGen: TypesGen;
4
+ constructor(typesGen: TypesGen);
5
+ get metadata(): {
6
+ types: {
7
+ id: number;
8
+ path: string[];
9
+ params: {
10
+ name: string;
11
+ typeId: number | undefined;
12
+ }[];
13
+ type: {
14
+ tag: "Struct";
15
+ value: {
16
+ fields: import("@dedot/codecs").Field[];
17
+ };
18
+ } | {
19
+ tag: "Enum";
20
+ value: {
21
+ members: {
22
+ name: string;
23
+ fields: import("@dedot/codecs").Field[];
24
+ index: number;
25
+ docs: string[];
26
+ }[];
27
+ };
28
+ } | {
29
+ tag: "Sequence";
30
+ value: {
31
+ typeParam: number;
32
+ };
33
+ } | {
34
+ tag: "SizedVec";
35
+ value: {
36
+ len: number;
37
+ typeParam: number;
38
+ };
39
+ } | {
40
+ tag: "Tuple";
41
+ value: {
42
+ fields: number[];
43
+ };
44
+ } | {
45
+ tag: "Primitive";
46
+ value: {
47
+ kind: "bool" | "char" | "str" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "i8" | "i16" | "i32" | "i64" | "i128" | "i256";
48
+ };
49
+ } | {
50
+ tag: "Compact";
51
+ value: {
52
+ typeParam: number;
53
+ };
54
+ } | {
55
+ tag: "BitSequence";
56
+ value: {
57
+ bitOrderType: number;
58
+ bitStoreType: number;
59
+ };
60
+ };
61
+ docs: string[];
62
+ }[];
63
+ pallets: {
64
+ name: string;
65
+ storage: {
66
+ prefix: string;
67
+ entries: {
68
+ name: string;
69
+ modifier: string;
70
+ type: {
71
+ tag: "Plain";
72
+ value: {
73
+ valueTypeId: number;
74
+ };
75
+ } | {
76
+ tag: "Map";
77
+ value: {
78
+ hashers: ("identity" | "blake2_128" | "blake2_256" | "blake2_128Concat" | "twox128" | "twox256" | "twox64Concat")[];
79
+ keyTypeId: number;
80
+ valueTypeId: number;
81
+ };
82
+ };
83
+ default: `0x${string}`;
84
+ docs: string[];
85
+ }[];
86
+ } | undefined;
87
+ calls: number | undefined;
88
+ event: number | undefined;
89
+ constants: {
90
+ name: string;
91
+ typeId: number;
92
+ value: `0x${string}`;
93
+ docs: string[];
94
+ }[];
95
+ error: number | undefined;
96
+ index: number;
97
+ docs: string[];
98
+ }[];
99
+ extrinsic: {
100
+ version: number;
101
+ addressTypeId: number;
102
+ callTypeId: number;
103
+ signatureTypeId: number;
104
+ extraTypeId: number;
105
+ signedExtensions: {
106
+ ident: string;
107
+ typeId: number;
108
+ additionalSigned: number;
109
+ }[];
110
+ };
111
+ runtimeType: number;
112
+ apis: {
113
+ name: string;
114
+ methods: {
115
+ name: string;
116
+ inputs: {
117
+ name: string;
118
+ typeId: number;
119
+ }[];
120
+ output: number;
121
+ docs: string[];
122
+ }[];
123
+ docs: string[];
124
+ }[];
125
+ outerEnums: {
126
+ callEnumTypeId: number;
127
+ eventEnumTypeId: number;
128
+ errorEnumTypeId: number;
129
+ };
130
+ custom: {
131
+ map: ReadonlyMap<string, {
132
+ typeId: number;
133
+ value: `0x${string}`;
134
+ }>;
135
+ };
136
+ };
137
+ get registry(): import("@dedot/codecs").CodecRegistry;
138
+ }
@@ -0,0 +1,12 @@
1
+ export class ApiGen {
2
+ typesGen;
3
+ constructor(typesGen) {
4
+ this.typesGen = typesGen;
5
+ }
6
+ get metadata() {
7
+ return this.typesGen.metadata;
8
+ }
9
+ get registry() {
10
+ return this.typesGen.registry;
11
+ }
12
+ }
@@ -0,0 +1,4 @@
1
+ import { ApiGen } from '../generator';
2
+ export declare class ConstsGen extends ApiGen {
3
+ generate(): Promise<string>;
4
+ }
@@ -0,0 +1,28 @@
1
+ import { stringLowerFirst } from '@polkadot/util';
2
+ import { normalizeName } from '@dedot/utils';
3
+ import { ApiGen } from '../generator';
4
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
5
+ export class ConstsGen extends ApiGen {
6
+ generate() {
7
+ const { pallets } = this.metadata;
8
+ this.typesGen.clearCache();
9
+ this.typesGen.typeImports.addKnownType('GenericChainConsts');
10
+ let defTypeOut = '';
11
+ for (let pallet of pallets) {
12
+ const typedConstants = pallet.constants.map((one) => ({
13
+ name: normalizeName(one.name),
14
+ type: this.typesGen.generateType(one.typeId, 1, true),
15
+ docs: one.docs,
16
+ }));
17
+ defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s constants`);
18
+ defTypeOut += `${stringLowerFirst(pallet.name)}: {
19
+ ${typedConstants.map(({ name, type, docs }) => `${commentBlock(docs)}${name}: ${type}`).join(',\n')}
20
+
21
+ ${commentBlock('Generic pallet constant')}[name: string]: any,
22
+ },`;
23
+ }
24
+ const importTypes = this.typesGen.typeImports.toImports();
25
+ const template = compileTemplate('consts.hbs');
26
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
27
+ }
28
+ }
@@ -0,0 +1,5 @@
1
+ import { ApiGen } from './ApiGen';
2
+ export declare class ErrorsGen extends ApiGen {
3
+ #private;
4
+ generate(): Promise<string>;
5
+ }
@@ -0,0 +1,37 @@
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';
5
+ export class ErrorsGen extends ApiGen {
6
+ generate() {
7
+ const { pallets } = this.metadata;
8
+ this.typesGen.clearCache();
9
+ this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
10
+ let defTypeOut = '';
11
+ for (let pallet of pallets) {
12
+ const errorTypeId = pallet.error;
13
+ if (!errorTypeId) {
14
+ continue;
15
+ }
16
+ const errorDefs = this.#getErrorDefs(errorTypeId);
17
+ defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
18
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
19
+ ${errorDefs
20
+ .map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError`)
21
+ .join(',\n')}
22
+
23
+ ${commentBlock('Generic pallet error')}[error: string]: GenericPalletError,
24
+ },`;
25
+ }
26
+ const importTypes = this.typesGen.typeImports.toImports();
27
+ const template = compileTemplate('errors.hbs');
28
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
29
+ }
30
+ #getErrorDefs(errorTypeId) {
31
+ const def = this.metadata.types[errorTypeId];
32
+ assert(def, `Error def not found for id ${errorTypeId}`);
33
+ const { tag, value } = def.type;
34
+ assert(tag === 'Enum', `Invalid pallet error type!`);
35
+ return value.members;
36
+ }
37
+ }
@@ -0,0 +1,5 @@
1
+ import { ApiGen } from './ApiGen';
2
+ export declare class EventsGen extends ApiGen {
3
+ #private;
4
+ generate(): Promise<string>;
5
+ }
@@ -0,0 +1,64 @@
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';
5
+ export class EventsGen extends ApiGen {
6
+ generate() {
7
+ const { pallets } = this.metadata;
8
+ this.typesGen.clearCache();
9
+ this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
10
+ let defTypeOut = '';
11
+ for (let pallet of pallets) {
12
+ const eventTypeId = pallet.event;
13
+ if (!eventTypeId) {
14
+ continue;
15
+ }
16
+ const eventDefs = this.#getEventDefs(eventTypeId);
17
+ const flatMembers = eventDefs.every((d) => d.fields.length === 0);
18
+ defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s events`);
19
+ defTypeOut += `${stringCamelCase(pallet.name)}: {
20
+ ${eventDefs
21
+ .map((def) => {
22
+ const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
23
+ const eventDataPart = this.#generateMemberDefType(def.fields);
24
+ if (eventDataPart) {
25
+ genericParts.push(eventDataPart);
26
+ }
27
+ else {
28
+ genericParts.push(flatMembers ? 'undefined' : 'null');
29
+ }
30
+ return { ...def, genericParts };
31
+ })
32
+ .map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
33
+ .join(',\n')}
34
+
35
+ ${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent,
36
+ },`;
37
+ }
38
+ const importTypes = this.typesGen.typeImports.toImports();
39
+ const template = compileTemplate('events.hbs');
40
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
41
+ }
42
+ #getEventDefs(typeId) {
43
+ const def = this.metadata.types[typeId];
44
+ assert(def, `Event def not found for id ${typeId}`);
45
+ const { tag, value } = def.type;
46
+ assert(tag === 'Enum', 'Invalid pallet event type!');
47
+ return value.members;
48
+ }
49
+ #generateMemberDefType(fields) {
50
+ if (fields.length === 0) {
51
+ return null;
52
+ }
53
+ else if (fields[0].name === undefined) {
54
+ return fields.length === 1
55
+ ? this.typesGen.generateType(fields[0].typeId, 1, true)
56
+ : `[${fields
57
+ .map(({ typeId, docs }) => `${commentBlock(docs)}${this.typesGen.generateType(typeId, 1, true)}`)
58
+ .join(', ')}]`;
59
+ }
60
+ else {
61
+ return this.typesGen.generateObjectType(fields, 1, true);
62
+ }
63
+ }
64
+ }
@@ -0,0 +1,6 @@
1
+ import { NetworkInfo } from '../types';
2
+ export declare class IndexGen {
3
+ readonly networkInfo: NetworkInfo;
4
+ constructor(networkInfo: NetworkInfo);
5
+ generate(): Promise<string>;
6
+ }
@@ -0,0 +1,14 @@
1
+ import { beautifySourceCode, compileTemplate } from './utils';
2
+ import { stringPascalCase } from '@polkadot/util';
3
+ export class IndexGen {
4
+ networkInfo;
5
+ constructor(networkInfo) {
6
+ this.networkInfo = networkInfo;
7
+ }
8
+ async generate() {
9
+ const { chain } = this.networkInfo;
10
+ const interfaceName = stringPascalCase(chain);
11
+ const template = compileTemplate('index.hbs');
12
+ return beautifySourceCode(template({ interfaceName }));
13
+ }
14
+ }
@@ -0,0 +1,5 @@
1
+ import { ApiGen } from '../generator';
2
+ export declare class QueryGen extends ApiGen {
3
+ #private;
4
+ generate(): Promise<string>;
5
+ }
@@ -0,0 +1,55 @@
1
+ import { stringLowerFirst } from '@polkadot/util';
2
+ import { normalizeName } from '@dedot/utils';
3
+ import { ApiGen } from '../generator';
4
+ import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
5
+ export class QueryGen extends ApiGen {
6
+ generate() {
7
+ const { pallets } = this.metadata;
8
+ this.typesGen.clearCache();
9
+ this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
10
+ let defTypeOut = '';
11
+ for (let pallet of pallets) {
12
+ const storage = pallet.storage;
13
+ if (!storage)
14
+ continue;
15
+ const queries = storage.entries.map((one) => this.#generateEntry(one));
16
+ const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
17
+ defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
18
+ defTypeOut += `${stringLowerFirst(pallet.name)}: {
19
+ ${queryDefs.join(',\n')}
20
+
21
+ ${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
22
+ },`;
23
+ }
24
+ const importTypes = this.typesGen.typeImports.toImports();
25
+ const template = compileTemplate('query.hbs');
26
+ return beautifySourceCode(template({ importTypes, defTypeOut }));
27
+ }
28
+ #generateEntry(entry) {
29
+ const { name, type, docs, modifier } = entry;
30
+ let valueType, keyType;
31
+ if (type.tag === 'Plain') {
32
+ valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
33
+ }
34
+ else if (type.tag === 'Map') {
35
+ valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
36
+ keyType = this.typesGen.generateType(type.value.keyTypeId, 1);
37
+ }
38
+ else {
39
+ throw Error('Invalid entry type!');
40
+ }
41
+ const isOptional = modifier === 'Optional';
42
+ valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
43
+ docs.push('\n');
44
+ if (keyType) {
45
+ docs.push(`@param {${keyType}} arg`);
46
+ }
47
+ docs.push(`@param {Callback<${valueType}> =} callback`);
48
+ return {
49
+ name: normalizeName(name),
50
+ valueType,
51
+ keyType: keyType ? `arg: ${keyType}` : '',
52
+ docs,
53
+ };
54
+ }
55
+ }
@@ -0,0 +1,10 @@
1
+ import { ApiGen, TypesGen } from '../generator';
2
+ export declare class RpcGen extends ApiGen {
3
+ #private;
4
+ readonly typesGen: TypesGen;
5
+ readonly rpcMethods: string[];
6
+ constructor(typesGen: TypesGen, rpcMethods: string[]);
7
+ generate(): Promise<string>;
8
+ addTypeImport(type: string | string[], toTypeIn?: boolean): void;
9
+ getGeneratedTypeName(type: string, toTypeIn?: boolean): string;
10
+ }