@dedot/codegen 0.0.1-next.8beb88c3.11 → 0.0.1-next.8f4b2c24.6
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 +106 -28
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +23 -29
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +151 -0
- 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 +5 -5
- 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 +94 -16
- 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 +3 -3
- package/generator/TypesGen.js +12 -18
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/known-codecs.d.ts +23 -0
- package/generator/known-codecs.js +120 -0
- 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 -79
- package/cjs/generator/RpcGen.js +0 -175
- 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 -74
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- 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
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ApiGen } from '../generator/index.js';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
3
|
+
import { subscriptionsInfo } from '@dedot/specs';
|
|
4
|
+
const HIDDEN_RPCS = [
|
|
5
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
6
|
+
'rpc_methods',
|
|
7
|
+
];
|
|
8
|
+
const ALIAS_RPCS = [
|
|
9
|
+
// TODO include these into the specs
|
|
10
|
+
'account_nextIndex',
|
|
11
|
+
'system_dryRunAt',
|
|
12
|
+
'state_callAt',
|
|
13
|
+
'state_getKeysPagedAt',
|
|
14
|
+
'state_getStorageAt',
|
|
15
|
+
'state_getStorageHashAt',
|
|
16
|
+
'state_getStorageSizeAt',
|
|
17
|
+
'chain_getRuntimeVersion',
|
|
18
|
+
'chain_getHead',
|
|
19
|
+
'chain_getFinalisedHead',
|
|
20
|
+
'childstate_getKeysPagedAt',
|
|
21
|
+
// Subscription pairs
|
|
22
|
+
'chain_subscribeRuntimeVersion',
|
|
23
|
+
'chain_unsubscribeRuntimeVersion',
|
|
24
|
+
'chain_unsubscribeNewHead',
|
|
25
|
+
'chain_subscribeNewHead',
|
|
26
|
+
'subscribe_newHead',
|
|
27
|
+
'unsubscribe_newHead',
|
|
28
|
+
'chain_subscribeFinalisedHeads',
|
|
29
|
+
'chain_unsubscribeFinalisedHeads',
|
|
30
|
+
];
|
|
31
|
+
export class JsonRpcGen extends ApiGen {
|
|
32
|
+
typesGen;
|
|
33
|
+
rpcMethods;
|
|
34
|
+
constructor(typesGen, rpcMethods) {
|
|
35
|
+
super(typesGen);
|
|
36
|
+
this.typesGen = typesGen;
|
|
37
|
+
this.rpcMethods = rpcMethods;
|
|
38
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
39
|
+
rpcMethods.sort();
|
|
40
|
+
}
|
|
41
|
+
generate() {
|
|
42
|
+
this.typesGen.clearCache();
|
|
43
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis');
|
|
44
|
+
this.typesGen.typeImports.addSpecType('JsonRpcApis');
|
|
45
|
+
const toExclude = Object.values(subscriptionsInfo).flat();
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
47
|
+
const template = compileTemplate('json-rpc.hbs');
|
|
48
|
+
const jsonRpcMethods = this.rpcMethods
|
|
49
|
+
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
50
|
+
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
51
|
+
.map((one) => `'${one}'`)
|
|
52
|
+
.join(' | ');
|
|
53
|
+
return beautifySourceCode(template({ importTypes, jsonRpcMethods }));
|
|
54
|
+
}
|
|
55
|
+
}
|
package/generator/QueryGen.d.ts
CHANGED
package/generator/QueryGen.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
1
|
+
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
5
4
|
export class QueryGen extends ApiGen {
|
|
6
5
|
generate() {
|
|
7
6
|
const { pallets } = this.metadata;
|
|
@@ -13,9 +12,16 @@ export class QueryGen extends ApiGen {
|
|
|
13
12
|
if (!storage)
|
|
14
13
|
continue;
|
|
15
14
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
16
|
-
const queryDefs = queries.map(({ name, valueType, keyType, docs
|
|
15
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
|
|
16
|
+
if (keyTypeOut) {
|
|
17
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}, ${keyTypeOut}>`;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
17
23
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
18
|
-
defTypeOut += `${
|
|
24
|
+
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
19
25
|
${queryDefs.join(',\n')}
|
|
20
26
|
|
|
21
27
|
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
@@ -27,13 +33,14 @@ export class QueryGen extends ApiGen {
|
|
|
27
33
|
}
|
|
28
34
|
#generateEntry(entry) {
|
|
29
35
|
const { name, type, docs, modifier } = entry;
|
|
30
|
-
let valueType,
|
|
36
|
+
let valueType, keyTypeOut, keyTypeIn;
|
|
31
37
|
if (type.tag === 'Plain') {
|
|
32
38
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
33
39
|
}
|
|
34
40
|
else if (type.tag === 'Map') {
|
|
35
41
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
36
|
-
|
|
42
|
+
keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
|
|
43
|
+
keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
|
|
37
44
|
}
|
|
38
45
|
else {
|
|
39
46
|
throw Error('Invalid entry type!');
|
|
@@ -41,14 +48,15 @@ export class QueryGen extends ApiGen {
|
|
|
41
48
|
const isOptional = modifier === 'Optional';
|
|
42
49
|
valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
|
|
43
50
|
docs.push('\n');
|
|
44
|
-
if (
|
|
45
|
-
docs.push(`@param {${
|
|
51
|
+
if (keyTypeIn) {
|
|
52
|
+
docs.push(`@param {${keyTypeIn}} arg`);
|
|
46
53
|
}
|
|
47
54
|
docs.push(`@param {Callback<${valueType}> =} callback`);
|
|
48
55
|
return {
|
|
49
56
|
name: normalizeName(name),
|
|
50
57
|
valueType,
|
|
51
|
-
keyType:
|
|
58
|
+
keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
|
|
59
|
+
keyTypeOut,
|
|
52
60
|
docs,
|
|
53
61
|
};
|
|
54
62
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { TypesGen } from './TypesGen';
|
|
2
|
-
import {
|
|
3
|
-
export declare class RuntimeApisGen extends
|
|
1
|
+
import { TypesGen } from './TypesGen.js';
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
|
+
export declare class RuntimeApisGen extends ApiGen {
|
|
4
4
|
#private;
|
|
5
5
|
readonly typesGen: TypesGen;
|
|
6
|
-
readonly runtimeApis:
|
|
7
|
-
constructor(typesGen: TypesGen, runtimeApis:
|
|
6
|
+
readonly runtimeApis: Record<string, number>;
|
|
7
|
+
constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
|
|
8
8
|
generate(): Promise<string>;
|
|
9
9
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
export class RuntimeApisGen extends
|
|
1
|
+
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
3
|
+
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
4
|
+
import { ApiGen } from './ApiGen.js';
|
|
5
|
+
import { findKnownCodecType } from './known-codecs.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,15 +85,15 @@ 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]) => {
|
|
96
|
-
const runtimeApiSpec = findRuntimeApiSpec(runtimeApiHash, version);
|
|
95
|
+
const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
|
|
96
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
97
97
|
if (!runtimeApiSpec)
|
|
98
98
|
return;
|
|
99
99
|
return {
|
|
@@ -108,4 +108,82 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
108
108
|
return [...o, spec];
|
|
109
109
|
}, []);
|
|
110
110
|
}
|
|
111
|
+
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
112
|
+
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
113
|
+
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
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
|
+
}
|
|
111
189
|
}
|
package/generator/TxGen.d.ts
CHANGED
package/generator/TxGen.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { ApiGen } from '../generator';
|
|
2
|
-
import { stringCamelCase, stringPascalCase } from '@
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils';
|
|
1
|
+
import { ApiGen } from '../generator/index.js';
|
|
2
|
+
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
|
|
4
4
|
export class TxGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets, types } = this.metadata;
|
|
@@ -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;
|
|
@@ -15,7 +15,7 @@ export declare class TypesGen {
|
|
|
15
15
|
* Types will be generated its definition out.
|
|
16
16
|
*/
|
|
17
17
|
includedTypes: Record<TypeId, NamedType>;
|
|
18
|
-
registry:
|
|
18
|
+
registry: PortableRegistry;
|
|
19
19
|
typeImports: TypeImports;
|
|
20
20
|
constructor(metadata: MetadataLatest);
|
|
21
21
|
generate(): Promise<string>;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { TypeImports } from './TypeImports';
|
|
1
|
+
import { PortableRegistry } from '@dedot/codecs';
|
|
2
|
+
import { normalizeName, stringPascalCase } from '@dedot/utils';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType } from './utils.js';
|
|
4
|
+
import { TypeImports } from './TypeImports.js';
|
|
5
|
+
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs.js';
|
|
7
6
|
// Skip generate types for these
|
|
8
7
|
// as we do have native types for them
|
|
9
8
|
const SKIP_TYPES = [
|
|
@@ -37,7 +36,7 @@ export class TypesGen {
|
|
|
37
36
|
typeImports;
|
|
38
37
|
constructor(metadata) {
|
|
39
38
|
this.metadata = metadata;
|
|
40
|
-
this.registry = new
|
|
39
|
+
this.registry = new PortableRegistry(this.metadata);
|
|
41
40
|
this.includedTypes = this.#includedTypes();
|
|
42
41
|
this.typeImports = new TypeImports();
|
|
43
42
|
}
|
|
@@ -99,7 +98,7 @@ export class TypesGen {
|
|
|
99
98
|
const { tag, value } = type;
|
|
100
99
|
switch (tag) {
|
|
101
100
|
case 'Primitive':
|
|
102
|
-
const $codec =
|
|
101
|
+
const $codec = findKnownCodec(value.kind);
|
|
103
102
|
if ($codec.nativeType) {
|
|
104
103
|
return $codec.nativeType;
|
|
105
104
|
}
|
|
@@ -168,7 +167,7 @@ export class TypesGen {
|
|
|
168
167
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
169
168
|
}
|
|
170
169
|
}
|
|
171
|
-
const { tagKey, valueKey } = this.registry.
|
|
170
|
+
const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
|
|
172
171
|
return membersType
|
|
173
172
|
.map(([keyName, valueType, docs]) => ({
|
|
174
173
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -263,8 +262,8 @@ export class TypesGen {
|
|
|
263
262
|
const suffix = typeSuffixes.get(id) || '';
|
|
264
263
|
let knownType = false;
|
|
265
264
|
let name, nameOut;
|
|
266
|
-
if (
|
|
267
|
-
const codecType =
|
|
265
|
+
if (isKnownCodecType(joinedPath)) {
|
|
266
|
+
const codecType = findKnownCodecType(path.at(-1));
|
|
268
267
|
name = codecType.typeIn;
|
|
269
268
|
nameOut = codecType.typeOut;
|
|
270
269
|
knownType = true;
|
|
@@ -317,7 +316,7 @@ export class TypesGen {
|
|
|
317
316
|
}
|
|
318
317
|
#shouldGenerateTypeIn(id) {
|
|
319
318
|
const { callTypeId } = this.metadata.extrinsic;
|
|
320
|
-
const palletCallTypeIds = this.registry.
|
|
319
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
321
320
|
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
322
321
|
}
|
|
323
322
|
eqlCache = new Map();
|
|
@@ -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
|
@@ -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';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as $ from '@dedot/shape';
|
|
2
|
+
import { AnyShape } from '@dedot/shape';
|
|
3
|
+
export type CodecName = `$${string}`;
|
|
4
|
+
export interface CodecType {
|
|
5
|
+
name: CodecName;
|
|
6
|
+
$codec: AnyShape;
|
|
7
|
+
typeIn: string;
|
|
8
|
+
typeOut: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const normalizeCodecName: (name: string | CodecName) => CodecName;
|
|
11
|
+
/**
|
|
12
|
+
* Collection of codec types with loose input types
|
|
13
|
+
*
|
|
14
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
15
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
16
|
+
*
|
|
17
|
+
* This registry keep track the list of codecs which follow this convention
|
|
18
|
+
*/
|
|
19
|
+
export declare const looseTypeCodecs: Record<string, AnyShape>;
|
|
20
|
+
export declare function findKnownCodecType(name: string): CodecType;
|
|
21
|
+
export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
|
|
22
|
+
export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
|
|
23
|
+
export declare function isKnownCodecType(path: string | string[]): boolean;
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import * as $ from '@dedot/shape';
|
|
2
|
+
import * as Codecs from '@dedot/codecs';
|
|
3
|
+
import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
|
|
4
|
+
import { assert } from '@dedot/utils';
|
|
5
|
+
export const normalizeCodecName = (name) => {
|
|
6
|
+
return name.startsWith('$') ? name : `$${name}`;
|
|
7
|
+
};
|
|
8
|
+
// Known paths for codecs (primitives) that are shared between
|
|
9
|
+
// different substrate-based blockchains
|
|
10
|
+
const KNOWN_PATHS = [
|
|
11
|
+
'sp_core::crypto::AccountId32',
|
|
12
|
+
'sp_runtime::generic::era::Era',
|
|
13
|
+
'sp_runtime::multiaddress::MultiAddress',
|
|
14
|
+
/^sp_runtime::DispatchError$/,
|
|
15
|
+
'sp_runtime::ModuleError',
|
|
16
|
+
'sp_runtime::TokenError',
|
|
17
|
+
'sp_arithmetic::ArithmeticError',
|
|
18
|
+
'sp_runtime::TransactionalError',
|
|
19
|
+
'frame_support::dispatch::DispatchInfo',
|
|
20
|
+
'frame_system::Phase',
|
|
21
|
+
'sp_version::RuntimeVersion',
|
|
22
|
+
'fp_account::AccountId20',
|
|
23
|
+
'account::AccountId20',
|
|
24
|
+
'polkadot_runtime_common::claims::EthereumAddress',
|
|
25
|
+
'pallet_identity::types::Data',
|
|
26
|
+
'sp_runtime::generic::digest::Digest',
|
|
27
|
+
'sp_runtime::generic::digest::DigestItem',
|
|
28
|
+
'sp_runtime::generic::header::Header',
|
|
29
|
+
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
30
|
+
/^primitive_types::\w+$/,
|
|
31
|
+
/^sp_arithmetic::per_things::\w+$/,
|
|
32
|
+
/^sp_arithmetic::fixed_point::\w+$/,
|
|
33
|
+
];
|
|
34
|
+
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
35
|
+
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
36
|
+
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
37
|
+
/**
|
|
38
|
+
* Collection of codec types with loose input types
|
|
39
|
+
*
|
|
40
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
41
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
42
|
+
*
|
|
43
|
+
* This registry keep track the list of codecs which follow this convention
|
|
44
|
+
*/
|
|
45
|
+
export const looseTypeCodecs = {
|
|
46
|
+
$AccountId20,
|
|
47
|
+
$EthereumAddress,
|
|
48
|
+
$AccountId32,
|
|
49
|
+
$ConsensusEngineId,
|
|
50
|
+
$StorageKey,
|
|
51
|
+
$StorageData,
|
|
52
|
+
$PrefixedStorageKey,
|
|
53
|
+
$Bytes,
|
|
54
|
+
$RawBytes,
|
|
55
|
+
$MultiAddress,
|
|
56
|
+
$OpaqueExtrinsic,
|
|
57
|
+
$UncheckedExtrinsic,
|
|
58
|
+
$Era,
|
|
59
|
+
};
|
|
60
|
+
export function findKnownCodecType(name) {
|
|
61
|
+
const normalizedName = normalizeCodecName(name);
|
|
62
|
+
const $knownCodec = looseTypeCodecs[normalizedName];
|
|
63
|
+
if ($knownCodec) {
|
|
64
|
+
return {
|
|
65
|
+
name: normalizedName,
|
|
66
|
+
$codec: $knownCodec,
|
|
67
|
+
typeIn: `${name}Like`,
|
|
68
|
+
typeOut: name,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const $codec = findKnownCodec(name);
|
|
72
|
+
if ($codec.nativeType && $[name]) {
|
|
73
|
+
return {
|
|
74
|
+
name: normalizedName,
|
|
75
|
+
$codec,
|
|
76
|
+
typeIn: $codec.nativeType,
|
|
77
|
+
typeOut: $codec.nativeType,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
name: normalizedName,
|
|
82
|
+
$codec,
|
|
83
|
+
typeIn: name,
|
|
84
|
+
typeOut: name,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export function findKnownCodec(typeName) {
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[normalizeCodecName(typeName)] || $[typeName];
|
|
90
|
+
assert($codec, `Known codec not found - ${typeName}`);
|
|
91
|
+
return $codec;
|
|
92
|
+
}
|
|
93
|
+
export function findKnownWrapperCodec(typeName) {
|
|
94
|
+
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
95
|
+
if (matchNames) {
|
|
96
|
+
const [_, wrapper, inner] = matchNames;
|
|
97
|
+
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
const $Wrapper = $[wrapper];
|
|
100
|
+
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
101
|
+
return $Wrapper(findKnownWrapperCodec(inner));
|
|
102
|
+
}
|
|
103
|
+
const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
|
|
104
|
+
return $Wrapper(...$inners);
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
107
|
+
}
|
|
108
|
+
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
109
|
+
const $inner = typeName
|
|
110
|
+
.slice(1, -1)
|
|
111
|
+
.split(',')
|
|
112
|
+
.filter((x) => x)
|
|
113
|
+
.map((one) => findKnownCodec(one.trim()));
|
|
114
|
+
return $.Tuple(...$inner);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function isKnownCodecType(path) {
|
|
118
|
+
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
119
|
+
return KNOWN_PATHS.some((one) => joinedPath.match(one));
|
|
120
|
+
}
|