@dedot/codegen 0.0.1-next.41a5fa17.16 → 0.0.1-next.4f5df6a3.2
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.
- package/cjs/generator/ConstsGen.js +2 -2
- package/cjs/generator/IndexGen.js +4 -5
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +2 -2
- package/cjs/generator/RuntimeApisGen.js +84 -9
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +1 -7
- package/cjs/generator/index.js +1 -1
- package/cjs/index.js +22 -22
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +1 -1
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +4 -5
- package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +1 -1
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +85 -10
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +2 -8
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/index.d.ts +2 -3
- package/index.js +21 -21
- package/package.json +7 -7
- package/templates/index.hbs +2 -2
- package/templates/json-rpc.hbs +5 -0
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/generator/known-types.js +0 -33
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/generator/RpcGen.js +0 -171
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -6
- package/types.js +0 -1
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
3
3
|
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { ApiGen } from './ApiGen.js';
|
|
5
|
+
import { findKnownCodecType } from './known-codecs.js';
|
|
6
|
+
export class RuntimeApisGen extends ApiGen {
|
|
6
7
|
typesGen;
|
|
7
8
|
runtimeApis;
|
|
8
9
|
constructor(typesGen, runtimeApis) {
|
|
9
|
-
super(typesGen
|
|
10
|
+
super(typesGen);
|
|
10
11
|
this.typesGen = typesGen;
|
|
11
12
|
this.runtimeApis = runtimeApis;
|
|
12
13
|
}
|
|
@@ -56,17 +57,17 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
56
57
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
57
58
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
58
59
|
const defaultDocs = [`@callname: ${callName}`];
|
|
59
|
-
this
|
|
60
|
-
params.forEach(({ type }) => this
|
|
60
|
+
this.#addTypeImport(type, false);
|
|
61
|
+
params.forEach(({ type }) => this.#addTypeImport(type));
|
|
61
62
|
const typedParams = params.map((param, idx) => ({
|
|
62
63
|
...param,
|
|
63
64
|
isOptional: this.#isOptionalParam(params, param.type, idx),
|
|
64
|
-
plainType: this
|
|
65
|
+
plainType: this.#getGeneratedTypeName(param.type),
|
|
65
66
|
}));
|
|
66
67
|
const paramsOut = typedParams
|
|
67
68
|
.map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
68
69
|
.join(', ');
|
|
69
|
-
const typeOut = this
|
|
70
|
+
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
70
71
|
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
71
72
|
}
|
|
72
73
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
@@ -74,7 +75,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
74
75
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
75
76
|
const defaultDocs = [`@callname: ${callName}`];
|
|
76
77
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
77
|
-
this
|
|
78
|
+
this.#addTypeImport(typeOut, false);
|
|
78
79
|
const typedInputs = inputs
|
|
79
80
|
.map((input, idx) => ({
|
|
80
81
|
...input,
|
|
@@ -84,7 +85,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
84
85
|
...input,
|
|
85
86
|
isOptional: this.#isOptionalParam(inputs, input.type, idx),
|
|
86
87
|
}));
|
|
87
|
-
this
|
|
88
|
+
this.#addTypeImport(typedInputs.map((t) => t.type));
|
|
88
89
|
const paramsOut = typedInputs
|
|
89
90
|
.map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
90
91
|
.join(', ');
|
|
@@ -111,4 +112,78 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
111
112
|
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
112
113
|
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
113
114
|
};
|
|
115
|
+
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
116
|
+
#addTypeImport(type, toTypeIn = true) {
|
|
117
|
+
if (Array.isArray(type)) {
|
|
118
|
+
type.forEach((one) => this.#addTypeImport(one, toTypeIn));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
type = type.trim();
|
|
122
|
+
if (isNativeType(type)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Handle generic wrapper types
|
|
126
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
127
|
+
if (matchArray) {
|
|
128
|
+
const [_, $1, $2] = matchArray;
|
|
129
|
+
this.#addTypeImport($1, toTypeIn);
|
|
130
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
131
|
+
this.#addTypeImport($2, toTypeIn);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
this.#addTypeImport($2.split(','), toTypeIn);
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Check tuple type
|
|
139
|
+
if (type.match(TUPLE_TYPE_REGEX)) {
|
|
140
|
+
this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (type.includes(' | ')) {
|
|
144
|
+
this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const codecType = this.#getCodecType(type, toTypeIn);
|
|
149
|
+
if (isNativeType(codecType))
|
|
150
|
+
return;
|
|
151
|
+
this.typesGen.typeImports.addCodecType(codecType);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
catch (e) { }
|
|
155
|
+
this.typesGen.addTypeImport(type);
|
|
156
|
+
}
|
|
157
|
+
#getGeneratedTypeName(type, toTypeIn = true) {
|
|
158
|
+
try {
|
|
159
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
160
|
+
if (matchArray) {
|
|
161
|
+
const [_, $1, $2] = matchArray;
|
|
162
|
+
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
163
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
164
|
+
return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
|
|
165
|
+
}
|
|
166
|
+
const innerTypeNames = $2
|
|
167
|
+
.split(',')
|
|
168
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
169
|
+
.join(', ');
|
|
170
|
+
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
171
|
+
}
|
|
172
|
+
else if (type.match(TUPLE_TYPE_REGEX)) {
|
|
173
|
+
const innerTypeNames = type
|
|
174
|
+
.slice(1, -1)
|
|
175
|
+
.split(',')
|
|
176
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
177
|
+
.join(', ');
|
|
178
|
+
return `[${innerTypeNames}]`;
|
|
179
|
+
}
|
|
180
|
+
return this.#getCodecType(type, toTypeIn);
|
|
181
|
+
}
|
|
182
|
+
catch (e) { }
|
|
183
|
+
return type;
|
|
184
|
+
}
|
|
185
|
+
#getCodecType(type, toTypeIn = true) {
|
|
186
|
+
const { typeIn, typeOut } = findKnownCodecType(type);
|
|
187
|
+
return toTypeIn ? typeIn : typeOut;
|
|
188
|
+
}
|
|
114
189
|
}
|
|
@@ -3,6 +3,7 @@ export declare class TypeImports {
|
|
|
3
3
|
portableTypes: Set<string>;
|
|
4
4
|
codecTypes: Set<string>;
|
|
5
5
|
knownTypes: Set<string>;
|
|
6
|
+
specTypes: Set<string>;
|
|
6
7
|
outTypes: Set<string>;
|
|
7
8
|
constructor();
|
|
8
9
|
clear(): void;
|
|
@@ -10,5 +11,6 @@ export declare class TypeImports {
|
|
|
10
11
|
addPortableType(...types: string[]): void;
|
|
11
12
|
addCodecType(...types: string[]): void;
|
|
12
13
|
addKnownType(...types: string[]): void;
|
|
14
|
+
addSpecType(...types: string[]): void;
|
|
13
15
|
addOutType(...types: string[]): void;
|
|
14
16
|
}
|
package/generator/TypeImports.js
CHANGED
|
@@ -5,30 +5,35 @@ export class TypeImports {
|
|
|
5
5
|
codecTypes;
|
|
6
6
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
7
7
|
knownTypes;
|
|
8
|
+
specTypes;
|
|
8
9
|
// External types to define explicitly
|
|
9
10
|
outTypes;
|
|
10
11
|
constructor() {
|
|
11
12
|
this.portableTypes = new Set();
|
|
12
13
|
this.codecTypes = new Set();
|
|
13
14
|
this.knownTypes = new Set();
|
|
15
|
+
this.specTypes = new Set();
|
|
14
16
|
this.outTypes = new Set();
|
|
15
17
|
}
|
|
16
18
|
clear() {
|
|
17
19
|
this.portableTypes.clear();
|
|
18
20
|
this.codecTypes.clear();
|
|
19
21
|
this.knownTypes.clear();
|
|
22
|
+
this.specTypes.clear();
|
|
20
23
|
this.outTypes.clear();
|
|
21
24
|
}
|
|
22
25
|
toImports(...excludeModules) {
|
|
23
26
|
// TODO generate outTypes!
|
|
24
27
|
const toImports = [
|
|
25
28
|
[this.knownTypes, '@dedot/types'],
|
|
29
|
+
[this.specTypes, '@dedot/specs'],
|
|
26
30
|
[this.codecTypes, '@dedot/codecs'],
|
|
27
31
|
[this.portableTypes, './types'],
|
|
28
32
|
];
|
|
29
33
|
return toImports
|
|
30
34
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
31
35
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
36
|
+
.filter((line) => line.length > 0)
|
|
32
37
|
.join('\n');
|
|
33
38
|
}
|
|
34
39
|
#toImportLine(types, module) {
|
|
@@ -46,6 +51,9 @@ export class TypeImports {
|
|
|
46
51
|
addKnownType(...types) {
|
|
47
52
|
types.forEach((one) => this.knownTypes.add(one));
|
|
48
53
|
}
|
|
54
|
+
addSpecType(...types) {
|
|
55
|
+
types.forEach((one) => this.specTypes.add(one));
|
|
56
|
+
}
|
|
49
57
|
addOutType(...types) {
|
|
50
58
|
types.forEach((one) => this.outTypes.add(one));
|
|
51
59
|
}
|
package/generator/TypesGen.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Field, MetadataLatest, PortableRegistry, PortableType, TypeId } from '@dedot/codecs';
|
|
2
2
|
import { TypeImports } from './TypeImports.js';
|
|
3
3
|
interface NamedType extends PortableType {
|
|
4
4
|
name: string;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { PortableRegistry } from '@dedot/codecs';
|
|
2
2
|
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
3
|
-
import {
|
|
4
|
-
import { knownTypes } from './known-types.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
|
|
5
4
|
import { TypeImports } from './TypeImports.js';
|
|
6
5
|
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
|
|
7
6
|
// Skip generate types for these
|
|
@@ -422,11 +421,6 @@ export class TypesGen {
|
|
|
422
421
|
this.typeImports.addCodecType(typeName);
|
|
423
422
|
return;
|
|
424
423
|
}
|
|
425
|
-
|
|
426
|
-
this.typeImports.addKnownType(typeName);
|
|
427
|
-
}
|
|
428
|
-
else {
|
|
429
|
-
this.typeImports.addOutType(typeName);
|
|
430
|
-
}
|
|
424
|
+
this.typeImports.addOutType(typeName);
|
|
431
425
|
}
|
|
432
426
|
}
|
package/generator/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export * from './TypesGen.js';
|
|
|
2
2
|
export * from './ApiGen.js';
|
|
3
3
|
export * from './ConstsGen.js';
|
|
4
4
|
export * from './QueryGen.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
6
|
export * from './IndexGen.js';
|
|
7
7
|
export * from './ErrorsGen.js';
|
|
8
8
|
export * from './EventsGen.js';
|
package/generator/index.js
CHANGED
|
@@ -2,7 +2,7 @@ export * from './TypesGen.js';
|
|
|
2
2
|
export * from './ApiGen.js';
|
|
3
3
|
export * from './ConstsGen.js';
|
|
4
4
|
export * from './QueryGen.js';
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
6
|
export * from './IndexGen.js';
|
|
7
7
|
export * from './ErrorsGen.js';
|
|
8
8
|
export * from './EventsGen.js';
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
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: any[], outDir?: string, extension?: string): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
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,
|
|
4
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, JsonRpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
5
5
|
import { stringCamelCase } from '@dedot/utils';
|
|
6
|
-
export async function
|
|
7
|
-
const api = await Dedot.
|
|
8
|
-
const { methods } = await api.rpc.
|
|
6
|
+
export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
|
|
7
|
+
const api = await Dedot.new(endpoint);
|
|
8
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
9
9
|
const apis = api.runtimeVersion?.apis || [];
|
|
10
|
-
if (!
|
|
11
|
-
|
|
10
|
+
if (!chain) {
|
|
11
|
+
chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
12
12
|
}
|
|
13
|
-
await generateTypes(
|
|
13
|
+
await generateTypes(chain, api.metadataLatest, methods, apis, outDir, extension);
|
|
14
14
|
await api.disconnect();
|
|
15
15
|
}
|
|
16
|
-
export async function generateTypes(
|
|
17
|
-
const dirPath = path.resolve(outDir,
|
|
18
|
-
const defTypesFileName = path.join(dirPath, `types
|
|
19
|
-
const constsTypesFileName = path.join(dirPath, `consts
|
|
20
|
-
const queryTypesFileName = path.join(dirPath, `query
|
|
21
|
-
const
|
|
22
|
-
const indexFileName = path.join(dirPath, `index
|
|
23
|
-
const errorsFileName = path.join(dirPath, `errors
|
|
24
|
-
const eventsFileName = path.join(dirPath, `events
|
|
25
|
-
const runtimeApisFileName = path.join(dirPath, `runtime
|
|
26
|
-
const txFileName = path.join(dirPath, `tx
|
|
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
|
|
34
|
-
const indexGen = new IndexGen(
|
|
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(
|
|
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
|
+
"version": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
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",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dedot/codecs": "0.0.1-next.
|
|
22
|
-
"@dedot/shape": "0.0.1-next.
|
|
23
|
-
"@dedot/specs": "0.0.1-next.
|
|
24
|
-
"@dedot/utils": "0.0.1-next.
|
|
25
|
-
"dedot": "0.0.1-next.
|
|
21
|
+
"@dedot/codecs": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
22
|
+
"@dedot/shape": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
23
|
+
"@dedot/specs": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
24
|
+
"@dedot/utils": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
25
|
+
"dedot": "0.0.1-next.4f5df6a3.2+4f5df6a",
|
|
26
26
|
"handlebars": "^4.7.8",
|
|
27
27
|
"prettier": "^3.0.3"
|
|
28
28
|
},
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"directory": "dist"
|
|
32
32
|
},
|
|
33
33
|
"license": "Apache-2.0",
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "4f5df6a328a278f140f5e0f69ef4a92d96e4b34a",
|
|
35
35
|
"module": "./index.js",
|
|
36
36
|
"types": "./index.d.ts",
|
|
37
37
|
"exports": {
|
package/templates/index.hbs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
4
|
import { ChainConsts } from './consts';
|
|
5
5
|
import { ChainStorage } from './query';
|
|
6
|
-
import {
|
|
6
|
+
import { ChainJsonRpcApis } from './json-rpc';
|
|
7
7
|
import { ChainErrors } from './errors';
|
|
8
8
|
import { ChainEvents } from './events';
|
|
9
9
|
import { RuntimeApis } from './runtime';
|
|
@@ -12,7 +12,7 @@ import { ChainTx } from './tx';
|
|
|
12
12
|
export * from './types';
|
|
13
13
|
|
|
14
14
|
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
-
|
|
15
|
+
jsonrpc: ChainJsonRpcApis;
|
|
16
16
|
consts: ChainConsts;
|
|
17
17
|
query: ChainStorage;
|
|
18
18
|
errors: ChainErrors;
|
|
@@ -1,83 +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_js_1 = require("./index.js");
|
|
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: 'rococo',
|
|
43
|
-
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
chain: 'rococoAssetHub',
|
|
47
|
-
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
chain: 'aleph',
|
|
51
|
-
endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
chain: 'westendAssetHub',
|
|
55
|
-
endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
|
|
56
|
-
},
|
|
57
|
-
];
|
|
58
|
-
const OUT_DIR = 'packages/chaintypes/src';
|
|
59
|
-
async function run() {
|
|
60
|
-
for (const network of NETWORKS) {
|
|
61
|
-
const { chain, endpoint, metadataHex, rpcMethods } = network;
|
|
62
|
-
if (endpoint) {
|
|
63
|
-
console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
|
|
64
|
-
await (0, index_js_1.generateTypesFromChain)(network, endpoint, OUT_DIR);
|
|
65
|
-
}
|
|
66
|
-
else if (metadataHex && rpcMethods) {
|
|
67
|
-
console.log(`Generate types for ${chain} via raw data`);
|
|
68
|
-
const metadata = codecs_1.$Metadata.tryDecode(metadataHex);
|
|
69
|
-
const runtimeVersion = getRuntimeVersion(metadata);
|
|
70
|
-
await (0, index_js_1.generateTypes)(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
console.log('DONE!');
|
|
74
|
-
}
|
|
75
|
-
const getRuntimeVersion = (metadata) => {
|
|
76
|
-
const registry = new codecs_1.PortableRegistry(metadata.latest);
|
|
77
|
-
const executor = new dedot_1.ConstantExecutor({
|
|
78
|
-
registry,
|
|
79
|
-
metadataLatest: metadata.latest,
|
|
80
|
-
});
|
|
81
|
-
return executor.execute('system', 'version');
|
|
82
|
-
};
|
|
83
|
-
run().catch(console.log);
|
package/cjs/generator/RpcGen.js
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RpcGen = void 0;
|
|
4
|
-
const specs_1 = require("@dedot/specs");
|
|
5
|
-
const index_js_1 = require("../generator/index.js");
|
|
6
|
-
const utils_js_1 = require("./utils.js");
|
|
7
|
-
const known_codecs_js_1 = require("./known-codecs.js");
|
|
8
|
-
const HIDDEN_RPCS = [
|
|
9
|
-
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
10
|
-
'rpc_methods',
|
|
11
|
-
];
|
|
12
|
-
class RpcGen extends index_js_1.ApiGen {
|
|
13
|
-
typesGen;
|
|
14
|
-
rpcMethods;
|
|
15
|
-
constructor(typesGen, rpcMethods) {
|
|
16
|
-
super(typesGen);
|
|
17
|
-
this.typesGen = typesGen;
|
|
18
|
-
this.rpcMethods = rpcMethods;
|
|
19
|
-
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
20
|
-
rpcMethods.sort();
|
|
21
|
-
}
|
|
22
|
-
generate() {
|
|
23
|
-
this.typesGen.clearCache();
|
|
24
|
-
this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
|
|
25
|
-
const specsByModule = this.rpcMethods
|
|
26
|
-
.filter((one) => !(0, specs_1.findAliasRpcSpec)(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
|
|
27
|
-
.filter((one) => !(0, specs_1.isUnsubscribeMethod)(one)) // we'll ignore unsubscribe method as well
|
|
28
|
-
.map((one) => {
|
|
29
|
-
const spec = (0, specs_1.findRpcSpec)(one);
|
|
30
|
-
if (spec) {
|
|
31
|
-
return spec;
|
|
32
|
-
}
|
|
33
|
-
const [module, ...methodParts] = one.split('_');
|
|
34
|
-
const method = methodParts.join('_');
|
|
35
|
-
return {
|
|
36
|
-
params: [],
|
|
37
|
-
type: 'GenericRpcCall',
|
|
38
|
-
module,
|
|
39
|
-
method,
|
|
40
|
-
};
|
|
41
|
-
})
|
|
42
|
-
.reduce((o, spec) => {
|
|
43
|
-
const { module, method } = spec;
|
|
44
|
-
// ignore if rpc name does not confront with the general convention
|
|
45
|
-
if (!module || !method) {
|
|
46
|
-
return o;
|
|
47
|
-
}
|
|
48
|
-
return {
|
|
49
|
-
...o,
|
|
50
|
-
[module]: o[module] ? [...o[module], spec] : [spec],
|
|
51
|
-
};
|
|
52
|
-
}, {});
|
|
53
|
-
let rpcCallsOut = '';
|
|
54
|
-
Object.keys(specsByModule).forEach((module) => {
|
|
55
|
-
const specs = specsByModule[module];
|
|
56
|
-
// TODO add alias info to docs block!
|
|
57
|
-
rpcCallsOut += `${module}: {
|
|
58
|
-
${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
|
|
59
|
-
|
|
60
|
-
[method: string]: GenericRpcCall,
|
|
61
|
-
},`;
|
|
62
|
-
});
|
|
63
|
-
// TODO include & define external types
|
|
64
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
65
|
-
const template = (0, utils_js_1.compileTemplate)('rpc.hbs');
|
|
66
|
-
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
67
|
-
}
|
|
68
|
-
#generateMethodDef(spec) {
|
|
69
|
-
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
70
|
-
const rpcName = name || `${module}_${method}`;
|
|
71
|
-
let defaultDocs = [`@rpcname: ${rpcName}`];
|
|
72
|
-
if (deprecated) {
|
|
73
|
-
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
74
|
-
}
|
|
75
|
-
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
76
|
-
return `${(0, utils_js_1.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
77
|
-
}
|
|
78
|
-
this.addTypeImport(type, false);
|
|
79
|
-
params.forEach(({ type, isScale }) => {
|
|
80
|
-
this.addTypeImport(type, !!isScale);
|
|
81
|
-
});
|
|
82
|
-
const typedParams = params.map((param) => ({
|
|
83
|
-
...param,
|
|
84
|
-
plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
|
|
85
|
-
}));
|
|
86
|
-
const isSubscription = !!pubsub;
|
|
87
|
-
const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
|
|
88
|
-
const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
|
|
89
|
-
const typeOut = this.getGeneratedTypeName(type, false);
|
|
90
|
-
if (isSubscription) {
|
|
91
|
-
defaultDocs.shift();
|
|
92
|
-
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
93
|
-
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
94
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
101
|
-
addTypeImport(type, toTypeIn = true) {
|
|
102
|
-
if (Array.isArray(type)) {
|
|
103
|
-
type.forEach((one) => this.addTypeImport(one, toTypeIn));
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
type = type.trim();
|
|
107
|
-
if ((0, utils_js_1.isNativeType)(type)) {
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
// Handle generic wrapper types
|
|
111
|
-
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
112
|
-
if (matchArray) {
|
|
113
|
-
const [_, $1, $2] = matchArray;
|
|
114
|
-
this.addTypeImport($1, toTypeIn);
|
|
115
|
-
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
116
|
-
this.addTypeImport($2, toTypeIn);
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
this.addTypeImport($2.split(','), toTypeIn);
|
|
120
|
-
}
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
// Check tuple type
|
|
124
|
-
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
125
|
-
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
if (type.includes(' | ')) {
|
|
129
|
-
this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
try {
|
|
133
|
-
const codecType = this.#getCodecType(type, toTypeIn);
|
|
134
|
-
if ((0, utils_js_1.isNativeType)(codecType))
|
|
135
|
-
return;
|
|
136
|
-
this.typesGen.typeImports.addCodecType(codecType);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
catch (e) { }
|
|
140
|
-
this.typesGen.addTypeImport(type);
|
|
141
|
-
}
|
|
142
|
-
getGeneratedTypeName(type, toTypeIn = true) {
|
|
143
|
-
try {
|
|
144
|
-
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
145
|
-
if (matchArray) {
|
|
146
|
-
const [_, $1, $2] = matchArray;
|
|
147
|
-
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
148
|
-
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
149
|
-
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
150
|
-
}
|
|
151
|
-
const innerTypeNames = $2
|
|
152
|
-
.split(',')
|
|
153
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
154
|
-
.join(', ');
|
|
155
|
-
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
156
|
-
}
|
|
157
|
-
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
158
|
-
const innerTypeNames = type
|
|
159
|
-
.slice(1, -1)
|
|
160
|
-
.split(',')
|
|
161
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
162
|
-
.join(', ');
|
|
163
|
-
return `[${innerTypeNames}]`;
|
|
164
|
-
}
|
|
165
|
-
return this.#getCodecType(type, toTypeIn);
|
|
166
|
-
}
|
|
167
|
-
catch (e) { }
|
|
168
|
-
return type;
|
|
169
|
-
}
|
|
170
|
-
#getCodecType(type, toTypeIn = true) {
|
|
171
|
-
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
172
|
-
return toTypeIn ? typeIn : typeOut;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
exports.RpcGen = RpcGen;
|