@dedot/codegen 0.0.1-next.f5bf4fc2.1 → 0.0.1-next.fecbe32d.13
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 +9 -10
- package/cjs/generator/ErrorsGen.js +12 -13
- package/cjs/generator/EventsGen.js +13 -14
- package/cjs/generator/IndexGen.js +8 -9
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +23 -15
- package/cjs/generator/RuntimeApisGen.js +102 -28
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +20 -27
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +1 -1
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +32 -32
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +4 -4
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +4 -5
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +3 -4
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +3 -4
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +6 -7
- package/generator/JsonRpcGen.d.ts +7 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +19 -11
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +89 -15
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +2 -2
- package/generator/TypesGen.js +5 -12
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/known-codecs.js +1 -1
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +23 -23
- package/package.json +7 -8
- 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 -176
- 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.d.ts +0 -10
- package/generator/RpcGen.js +0 -172
- 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,13 +1,13 @@
|
|
|
1
1
|
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export class RuntimeApisGen extends
|
|
2
|
+
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
3
|
+
import { ApiGen } from './ApiGen.js';
|
|
4
|
+
import { findKnownCodecType } from './known-codecs.js';
|
|
5
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
6
|
+
export class RuntimeApisGen extends ApiGen {
|
|
7
7
|
typesGen;
|
|
8
8
|
runtimeApis;
|
|
9
9
|
constructor(typesGen, runtimeApis) {
|
|
10
|
-
super(typesGen
|
|
10
|
+
super(typesGen);
|
|
11
11
|
this.typesGen = typesGen;
|
|
12
12
|
this.runtimeApis = runtimeApis;
|
|
13
13
|
}
|
|
@@ -18,7 +18,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
18
18
|
if (this.metadata.apis.length > 0) {
|
|
19
19
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
20
20
|
const { name: runtimeApiName, methods } = runtimeApi;
|
|
21
|
-
runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${
|
|
21
|
+
runtimeCallsOut += commentBlock(`@runtimeapi: ${runtimeApiName} - ${calcRuntimeApiHash(runtimeApiName)}`);
|
|
22
22
|
runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
|
|
23
23
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
24
24
|
|
|
@@ -57,17 +57,17 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
57
57
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
58
58
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
59
59
|
const defaultDocs = [`@callname: ${callName}`];
|
|
60
|
-
this
|
|
61
|
-
params.forEach(({ type }) => this
|
|
60
|
+
this.#addTypeImport(type, false);
|
|
61
|
+
params.forEach(({ type }) => this.#addTypeImport(type));
|
|
62
62
|
const typedParams = params.map((param, idx) => ({
|
|
63
63
|
...param,
|
|
64
64
|
isOptional: this.#isOptionalParam(params, param.type, idx),
|
|
65
|
-
plainType: this
|
|
65
|
+
plainType: this.#getGeneratedTypeName(param.type),
|
|
66
66
|
}));
|
|
67
67
|
const paramsOut = typedParams
|
|
68
68
|
.map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
69
69
|
.join(', ');
|
|
70
|
-
const typeOut = this
|
|
70
|
+
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
71
71
|
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
72
72
|
}
|
|
73
73
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
@@ -75,7 +75,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
75
75
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
76
76
|
const defaultDocs = [`@callname: ${callName}`];
|
|
77
77
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
78
|
-
this
|
|
78
|
+
this.#addTypeImport(typeOut, false);
|
|
79
79
|
const typedInputs = inputs
|
|
80
80
|
.map((input, idx) => ({
|
|
81
81
|
...input,
|
|
@@ -85,14 +85,14 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
85
85
|
...input,
|
|
86
86
|
isOptional: this.#isOptionalParam(inputs, input.type, idx),
|
|
87
87
|
}));
|
|
88
|
-
this
|
|
88
|
+
this.#addTypeImport(typedInputs.map((t) => t.type));
|
|
89
89
|
const paramsOut = typedInputs
|
|
90
90
|
.map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
91
91
|
.join(', ');
|
|
92
92
|
return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
93
93
|
}
|
|
94
94
|
#targetRuntimeApiSpecs() {
|
|
95
|
-
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
95
|
+
const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
|
|
96
96
|
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
97
97
|
if (!runtimeApiSpec)
|
|
98
98
|
return;
|
|
@@ -109,7 +109,81 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
109
109
|
}, []);
|
|
110
110
|
}
|
|
111
111
|
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
112
|
-
const runtimeApiName = getRuntimeApiNames().find((one) =>
|
|
112
|
+
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
113
113
|
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
114
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
|
+
}
|
|
115
189
|
}
|
package/generator/TxGen.d.ts
CHANGED
package/generator/TxGen.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils';
|
|
1
|
+
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
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;
|
|
@@ -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,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { TypeImports } from './TypeImports';
|
|
1
|
+
import { Field, MetadataLatest, PortableRegistry, 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;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import { stringPascalCase } from '@polkadot/util';
|
|
2
1
|
import { PortableRegistry } from '@dedot/codecs';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs';
|
|
2
|
+
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
3
|
+
import { TypeImports } from './TypeImports.js';
|
|
4
|
+
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
|
|
5
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
|
|
8
6
|
// Skip generate types for these
|
|
9
7
|
// as we do have native types for them
|
|
10
8
|
const SKIP_TYPES = [
|
|
@@ -423,11 +421,6 @@ export class TypesGen {
|
|
|
423
421
|
this.typeImports.addCodecType(typeName);
|
|
424
422
|
return;
|
|
425
423
|
}
|
|
426
|
-
|
|
427
|
-
this.typeImports.addKnownType(typeName);
|
|
428
|
-
}
|
|
429
|
-
else {
|
|
430
|
-
this.typeImports.addOutType(typeName);
|
|
431
|
-
}
|
|
424
|
+
this.typeImports.addOutType(typeName);
|
|
432
425
|
}
|
|
433
426
|
}
|
package/generator/index.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from './TypesGen';
|
|
2
|
-
export * from './ApiGen';
|
|
3
|
-
export * from './ConstsGen';
|
|
4
|
-
export * from './QueryGen';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './IndexGen';
|
|
7
|
-
export * from './ErrorsGen';
|
|
8
|
-
export * from './EventsGen';
|
|
9
|
-
export * from './TxGen';
|
|
10
|
-
export * from './RuntimeApisGen';
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
package/generator/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
export * from './TypesGen';
|
|
2
|
-
export * from './ApiGen';
|
|
3
|
-
export * from './ConstsGen';
|
|
4
|
-
export * from './QueryGen';
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './IndexGen';
|
|
7
|
-
export * from './ErrorsGen';
|
|
8
|
-
export * from './EventsGen';
|
|
9
|
-
export * from './TxGen';
|
|
10
|
-
export * from './RuntimeApisGen';
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as $ from '@dedot/shape';
|
|
2
1
|
import * as Codecs from '@dedot/codecs';
|
|
3
2
|
import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
|
|
3
|
+
import * as $ from '@dedot/shape';
|
|
4
4
|
import { assert } from '@dedot/utils';
|
|
5
5
|
export const normalizeCodecName = (name) => {
|
|
6
6
|
return name.startsWith('$') ? name : `$${name}`;
|
package/generator/utils.d.ts
CHANGED
|
@@ -8,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
|
|
|
8
8
|
* @param word
|
|
9
9
|
*/
|
|
10
10
|
export declare const isReservedWord: (word: string) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check if a type is native JS/TS type
|
|
13
|
+
* @param type
|
|
14
|
+
*/
|
|
15
|
+
export declare const isNativeType: (type: string) => boolean;
|
package/generator/utils.js
CHANGED
|
@@ -33,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
|
|
|
33
33
|
* @param word
|
|
34
34
|
*/
|
|
35
35
|
export const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
|
|
36
|
+
const TS_PRIMITIVE_TYPES = [
|
|
37
|
+
'void',
|
|
38
|
+
'undefined',
|
|
39
|
+
'null',
|
|
40
|
+
'number',
|
|
41
|
+
'boolean',
|
|
42
|
+
'bigint',
|
|
43
|
+
'Map',
|
|
44
|
+
'Set',
|
|
45
|
+
'string',
|
|
46
|
+
'any',
|
|
47
|
+
'Array',
|
|
48
|
+
'Record',
|
|
49
|
+
];
|
|
50
|
+
/**
|
|
51
|
+
* Check if a type is native JS/TS type
|
|
52
|
+
* @param type
|
|
53
|
+
*/
|
|
54
|
+
export const isNativeType = (type) => {
|
|
55
|
+
return TS_PRIMITIVE_TYPES.some((one) => {
|
|
56
|
+
if (typeof one === 'string') {
|
|
57
|
+
return one === type;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return type.match(one);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
-
|
|
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: Record<string, number>, outDir?: string, extension?: string): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
1
2
|
import { Dedot } from 'dedot';
|
|
2
3
|
import * as fs from 'fs';
|
|
3
4
|
import * as path from 'path';
|
|
4
|
-
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
network.chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
5
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, JsonRpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
6
|
+
export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
|
|
7
|
+
const api = await Dedot.new(endpoint);
|
|
8
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
9
|
+
const apis = api.runtimeVersion.apis || {};
|
|
10
|
+
if (!chain) {
|
|
11
|
+
chain = stringCamelCase(api.runtimeVersion.specName || 'local');
|
|
12
12
|
}
|
|
13
|
-
await generateTypes(
|
|
13
|
+
await generateTypes(chain, api.metadata.latest, 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.fecbe32d.13+fecbe32",
|
|
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,12 +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
|
-
"
|
|
26
|
-
"dedot": "0.0.1-next.f5bf4fc2.1+f5bf4fc",
|
|
21
|
+
"@dedot/codecs": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
22
|
+
"@dedot/shape": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
23
|
+
"@dedot/specs": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
24
|
+
"@dedot/utils": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
25
|
+
"dedot": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
27
26
|
"handlebars": "^4.7.8",
|
|
28
27
|
"prettier": "^3.0.3"
|
|
29
28
|
},
|
|
@@ -32,7 +31,7 @@
|
|
|
32
31
|
"directory": "dist"
|
|
33
32
|
},
|
|
34
33
|
"license": "Apache-2.0",
|
|
35
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "fecbe32d9d4b04cdb6802bf0ac604222f54b8331",
|
|
36
35
|
"module": "./index.js",
|
|
37
36
|
"types": "./index.d.ts",
|
|
38
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
|
-
rpc:
|
|
15
|
+
rpc: 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_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: '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_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_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);
|