@dedot/codegen 0.0.1-alpha.27 → 0.0.1-alpha.29
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/QueryGen.js +9 -10
- package/cjs/generator/RpcGen.js +18 -19
- package/cjs/generator/RuntimeApisGen.js +19 -20
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypesGen.js +21 -22
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +20 -20
- package/generator/ApiGen.d.ts +1 -1
- 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/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +4 -5
- package/generator/RpcGen.d.ts +1 -1
- package/generator/RpcGen.js +3 -4
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +5 -6
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +5 -6
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +10 -10
- package/package.json +7 -8
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/packageInfo.js +0 -5
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -3
- package/types.d.ts +0 -6
- package/types.js +0 -1
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConstsGen = void 0;
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
5
4
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
class ConstsGen extends
|
|
5
|
+
const index_js_1 = require("../generator/index.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
class ConstsGen extends index_js_1.ApiGen {
|
|
9
8
|
generate() {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
@@ -17,16 +16,16 @@ class ConstsGen extends generator_1.ApiGen {
|
|
|
17
16
|
type: this.typesGen.generateType(one.typeId, 1, true),
|
|
18
17
|
docs: one.docs,
|
|
19
18
|
}));
|
|
20
|
-
defTypeOut += (0,
|
|
21
|
-
defTypeOut += `${(0,
|
|
22
|
-
${typedConstants.map(({ name, type, docs }) => `${(0,
|
|
19
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s constants`);
|
|
20
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
21
|
+
${typedConstants.map(({ name, type, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: ${type}`).join(',\n')}
|
|
23
22
|
|
|
24
|
-
${(0,
|
|
23
|
+
${(0, utils_js_1.commentBlock)('Generic pallet constant')}[name: string]: any,
|
|
25
24
|
},`;
|
|
26
25
|
}
|
|
27
26
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
28
|
-
const template = (0,
|
|
29
|
-
return (0,
|
|
27
|
+
const template = (0, utils_js_1.compileTemplate)('consts.hbs');
|
|
28
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
exports.ConstsGen = ConstsGen;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ErrorsGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
class ErrorsGen extends ApiGen_1.ApiGen {
|
|
4
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const utils_1 = require("@dedot/utils");
|
|
7
|
+
class ErrorsGen extends ApiGen_js_1.ApiGen {
|
|
9
8
|
generate() {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
@@ -17,24 +16,24 @@ class ErrorsGen extends ApiGen_1.ApiGen {
|
|
|
17
16
|
continue;
|
|
18
17
|
}
|
|
19
18
|
const errorDefs = this.#getErrorDefs(errorTypeId);
|
|
20
|
-
defTypeOut += (0,
|
|
21
|
-
defTypeOut += `${(0,
|
|
19
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
|
|
20
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
22
21
|
${errorDefs
|
|
23
|
-
.map(({ name, docs }) => `${(0,
|
|
22
|
+
.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError`)
|
|
24
23
|
.join(',\n')}
|
|
25
24
|
|
|
26
|
-
${(0,
|
|
25
|
+
${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError,
|
|
27
26
|
},`;
|
|
28
27
|
}
|
|
29
28
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
30
|
-
const template = (0,
|
|
31
|
-
return (0,
|
|
29
|
+
const template = (0, utils_js_1.compileTemplate)('errors.hbs');
|
|
30
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
32
31
|
}
|
|
33
32
|
#getErrorDefs(errorTypeId) {
|
|
34
33
|
const def = this.metadata.types[errorTypeId];
|
|
35
|
-
(0,
|
|
34
|
+
(0, utils_1.assert)(def, `Error def not found for id ${errorTypeId}`);
|
|
36
35
|
const { tag, value } = def.type;
|
|
37
|
-
(0,
|
|
36
|
+
(0, utils_1.assert)(tag === 'Enum', `Invalid pallet error type!`);
|
|
38
37
|
return value.members;
|
|
39
38
|
}
|
|
40
39
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventsGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
class EventsGen extends ApiGen_1.ApiGen {
|
|
4
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const utils_1 = require("@dedot/utils");
|
|
7
|
+
class EventsGen extends ApiGen_js_1.ApiGen {
|
|
9
8
|
generate() {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
@@ -18,8 +17,8 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
18
17
|
}
|
|
19
18
|
const eventDefs = this.#getEventDefs(eventTypeId);
|
|
20
19
|
const flatMembers = eventDefs.every((d) => d.fields.length === 0);
|
|
21
|
-
defTypeOut += (0,
|
|
22
|
-
defTypeOut += `${(0,
|
|
20
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s events`);
|
|
21
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
23
22
|
${eventDefs
|
|
24
23
|
.map((def) => {
|
|
25
24
|
const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
|
|
@@ -32,21 +31,21 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
32
31
|
}
|
|
33
32
|
return { ...def, genericParts };
|
|
34
33
|
})
|
|
35
|
-
.map(({ name, docs, fields, genericParts }) => `${(0,
|
|
34
|
+
.map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
|
|
36
35
|
.join(',\n')}
|
|
37
36
|
|
|
38
|
-
${(0,
|
|
37
|
+
${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent,
|
|
39
38
|
},`;
|
|
40
39
|
}
|
|
41
40
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
42
|
-
const template = (0,
|
|
43
|
-
return (0,
|
|
41
|
+
const template = (0, utils_js_1.compileTemplate)('events.hbs');
|
|
42
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
44
43
|
}
|
|
45
44
|
#getEventDefs(typeId) {
|
|
46
45
|
const def = this.metadata.types[typeId];
|
|
47
|
-
(0,
|
|
46
|
+
(0, utils_1.assert)(def, `Event def not found for id ${typeId}`);
|
|
48
47
|
const { tag, value } = def.type;
|
|
49
|
-
(0,
|
|
48
|
+
(0, utils_1.assert)(tag === 'Enum', 'Invalid pallet event type!');
|
|
50
49
|
return value.members;
|
|
51
50
|
}
|
|
52
51
|
#generateMemberDefType(fields) {
|
|
@@ -57,7 +56,7 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
57
56
|
return fields.length === 1
|
|
58
57
|
? this.typesGen.generateType(fields[0].typeId, 1, true)
|
|
59
58
|
: `[${fields
|
|
60
|
-
.map(({ typeId, docs }) => `${(0,
|
|
59
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.typesGen.generateType(typeId, 1, true)}`)
|
|
61
60
|
.join(', ')}]`;
|
|
62
61
|
}
|
|
63
62
|
else {
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IndexGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
|
+
const utils_1 = require("@dedot/utils");
|
|
6
6
|
class IndexGen {
|
|
7
|
-
|
|
8
|
-
constructor(
|
|
9
|
-
this.
|
|
7
|
+
chain;
|
|
8
|
+
constructor(chain) {
|
|
9
|
+
this.chain = chain;
|
|
10
10
|
}
|
|
11
11
|
async generate() {
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
return (0, utils_1.beautifySourceCode)(template({ interfaceName }));
|
|
12
|
+
const interfaceName = (0, utils_1.stringPascalCase)(this.chain);
|
|
13
|
+
const template = (0, utils_js_1.compileTemplate)('index.hbs');
|
|
14
|
+
return (0, utils_js_1.beautifySourceCode)(template({ interfaceName }));
|
|
16
15
|
}
|
|
17
16
|
}
|
|
18
17
|
exports.IndexGen = IndexGen;
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryGen = void 0;
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
5
4
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
class QueryGen extends
|
|
5
|
+
const index_js_1 = require("../generator/index.js");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
class QueryGen extends index_js_1.ApiGen {
|
|
9
8
|
generate() {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
@@ -16,17 +15,17 @@ class QueryGen extends generator_1.ApiGen {
|
|
|
16
15
|
if (!storage)
|
|
17
16
|
continue;
|
|
18
17
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
19
|
-
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${(0,
|
|
20
|
-
defTypeOut += (0,
|
|
21
|
-
defTypeOut += `${(0,
|
|
18
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
|
|
19
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
20
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
22
21
|
${queryDefs.join(',\n')}
|
|
23
22
|
|
|
24
|
-
${(0,
|
|
23
|
+
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
25
24
|
},`;
|
|
26
25
|
}
|
|
27
26
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
28
|
-
const template = (0,
|
|
29
|
-
return (0,
|
|
27
|
+
const template = (0, utils_js_1.compileTemplate)('query.hbs');
|
|
28
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
30
29
|
}
|
|
31
30
|
#generateEntry(entry) {
|
|
32
31
|
const { name, type, docs, modifier } = entry;
|
package/cjs/generator/RpcGen.js
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RpcGen = void 0;
|
|
4
4
|
const specs_1 = require("@dedot/specs");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const known_codecs_1 = require("./known-codecs");
|
|
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");
|
|
9
8
|
const HIDDEN_RPCS = [
|
|
10
9
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
11
10
|
'rpc_methods',
|
|
12
11
|
];
|
|
13
|
-
class RpcGen extends
|
|
12
|
+
class RpcGen extends index_js_1.ApiGen {
|
|
14
13
|
typesGen;
|
|
15
14
|
rpcMethods;
|
|
16
15
|
constructor(typesGen, rpcMethods) {
|
|
@@ -63,8 +62,8 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
63
62
|
});
|
|
64
63
|
// TODO include & define external types
|
|
65
64
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
66
|
-
const template = (0,
|
|
67
|
-
return (0,
|
|
65
|
+
const template = (0, utils_js_1.compileTemplate)('rpc.hbs');
|
|
66
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
68
67
|
}
|
|
69
68
|
#generateMethodDef(spec) {
|
|
70
69
|
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
@@ -74,7 +73,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
74
73
|
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
75
74
|
}
|
|
76
75
|
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
77
|
-
return `${(0,
|
|
76
|
+
return `${(0, utils_js_1.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
78
77
|
}
|
|
79
78
|
this.addTypeImport(type, false);
|
|
80
79
|
params.forEach(({ type, isScale }) => {
|
|
@@ -92,10 +91,10 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
92
91
|
defaultDocs.shift();
|
|
93
92
|
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
94
93
|
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
95
|
-
return `${(0,
|
|
94
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
96
95
|
}
|
|
97
96
|
else {
|
|
98
|
-
return `${(0,
|
|
97
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
99
98
|
}
|
|
100
99
|
}
|
|
101
100
|
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
@@ -105,15 +104,15 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
105
104
|
return;
|
|
106
105
|
}
|
|
107
106
|
type = type.trim();
|
|
108
|
-
if ((0,
|
|
107
|
+
if ((0, utils_js_1.isNativeType)(type)) {
|
|
109
108
|
return;
|
|
110
109
|
}
|
|
111
110
|
// Handle generic wrapper types
|
|
112
|
-
const matchArray = type.match(
|
|
111
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
113
112
|
if (matchArray) {
|
|
114
113
|
const [_, $1, $2] = matchArray;
|
|
115
114
|
this.addTypeImport($1, toTypeIn);
|
|
116
|
-
if ($2.match(
|
|
115
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
117
116
|
this.addTypeImport($2, toTypeIn);
|
|
118
117
|
}
|
|
119
118
|
else {
|
|
@@ -122,7 +121,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
122
121
|
return;
|
|
123
122
|
}
|
|
124
123
|
// Check tuple type
|
|
125
|
-
if (type.match(
|
|
124
|
+
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
126
125
|
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
127
126
|
return;
|
|
128
127
|
}
|
|
@@ -132,7 +131,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
132
131
|
}
|
|
133
132
|
try {
|
|
134
133
|
const codecType = this.#getCodecType(type, toTypeIn);
|
|
135
|
-
if ((0,
|
|
134
|
+
if ((0, utils_js_1.isNativeType)(codecType))
|
|
136
135
|
return;
|
|
137
136
|
this.typesGen.typeImports.addCodecType(codecType);
|
|
138
137
|
return;
|
|
@@ -142,11 +141,11 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
142
141
|
}
|
|
143
142
|
getGeneratedTypeName(type, toTypeIn = true) {
|
|
144
143
|
try {
|
|
145
|
-
const matchArray = type.match(
|
|
144
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
146
145
|
if (matchArray) {
|
|
147
146
|
const [_, $1, $2] = matchArray;
|
|
148
147
|
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
149
|
-
if ($2.match(
|
|
148
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
150
149
|
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
151
150
|
}
|
|
152
151
|
const innerTypeNames = $2
|
|
@@ -155,7 +154,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
155
154
|
.join(', ');
|
|
156
155
|
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
157
156
|
}
|
|
158
|
-
else if (type.match(
|
|
157
|
+
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
159
158
|
const innerTypeNames = type
|
|
160
159
|
.slice(1, -1)
|
|
161
160
|
.split(',')
|
|
@@ -169,7 +168,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
169
168
|
return type;
|
|
170
169
|
}
|
|
171
170
|
#getCodecType(type, toTypeIn = true) {
|
|
172
|
-
const { typeIn, typeOut } = (0,
|
|
171
|
+
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
173
172
|
return toTypeIn ? typeIn : typeOut;
|
|
174
173
|
}
|
|
175
174
|
}
|
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.RuntimeApisGen = void 0;
|
|
4
4
|
const specs_1 = require("@dedot/specs");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const utils_1 = require("@dedot/utils");
|
|
7
|
+
const RpcGen_js_1 = require("./RpcGen.js");
|
|
8
|
+
class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
10
9
|
typesGen;
|
|
11
10
|
runtimeApis;
|
|
12
11
|
constructor(typesGen, runtimeApis) {
|
|
@@ -21,19 +20,19 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
21
20
|
if (this.metadata.apis.length > 0) {
|
|
22
21
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
23
22
|
const { name: runtimeApiName, methods } = runtimeApi;
|
|
24
|
-
runtimeCallsOut += (0,
|
|
25
|
-
runtimeCallsOut += `${(0,
|
|
23
|
+
runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${(0, utils_1.calcRuntimeApiHash)(runtimeApiName)}`);
|
|
24
|
+
runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
|
|
26
25
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
27
26
|
|
|
28
|
-
${(0,
|
|
27
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
29
28
|
}`;
|
|
30
29
|
});
|
|
31
30
|
}
|
|
32
31
|
else {
|
|
33
32
|
const specs = this.#targetRuntimeApiSpecs();
|
|
34
33
|
specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
|
|
35
|
-
runtimeCallsOut += (0,
|
|
36
|
-
runtimeCallsOut += `${(0,
|
|
34
|
+
runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
|
|
35
|
+
runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
|
|
37
36
|
${Object.keys(methods)
|
|
38
37
|
.map((methodName) => this.#generateMethodDefFromSpec({
|
|
39
38
|
...methods[methodName],
|
|
@@ -42,13 +41,13 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
42
41
|
}))
|
|
43
42
|
.join('\n')}
|
|
44
43
|
|
|
45
|
-
${(0,
|
|
44
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
46
45
|
}`;
|
|
47
46
|
});
|
|
48
47
|
}
|
|
49
48
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
-
const template = (0,
|
|
51
|
-
return (0,
|
|
49
|
+
const template = (0, utils_js_1.compileTemplate)('runtime.hbs');
|
|
50
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, runtimeCallsOut }));
|
|
52
51
|
}
|
|
53
52
|
#isOptionalType(type) {
|
|
54
53
|
return type.startsWith('Option<') || type.endsWith('| undefined');
|
|
@@ -58,7 +57,7 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
58
57
|
}
|
|
59
58
|
#generateMethodDefFromSpec(spec) {
|
|
60
59
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
61
|
-
const callName = `${runtimeApiName}_${(0,
|
|
60
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
62
61
|
const defaultDocs = [`@callname: ${callName}`];
|
|
63
62
|
this.addTypeImport(type, false);
|
|
64
63
|
params.forEach(({ type }) => this.addTypeImport(type));
|
|
@@ -68,14 +67,14 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
68
67
|
plainType: this.getGeneratedTypeName(param.type),
|
|
69
68
|
}));
|
|
70
69
|
const paramsOut = typedParams
|
|
71
|
-
.map(({ name, isOptional, plainType }) => `${(0,
|
|
70
|
+
.map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
72
71
|
.join(', ');
|
|
73
72
|
const typeOut = this.getGeneratedTypeName(type, false);
|
|
74
|
-
return `${(0,
|
|
73
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
75
74
|
}
|
|
76
75
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
77
76
|
const { name: methodName, inputs, output, docs } = methodDef;
|
|
78
|
-
const callName = `${runtimeApiName}_${(0,
|
|
77
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
79
78
|
const defaultDocs = [`@callname: ${callName}`];
|
|
80
79
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
81
80
|
this.addTypeImport(typeOut, false);
|
|
@@ -90,9 +89,9 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
90
89
|
}));
|
|
91
90
|
this.addTypeImport(typedInputs.map((t) => t.type));
|
|
92
91
|
const paramsOut = typedInputs
|
|
93
|
-
.map(({ name, type, isOptional }) => `${(0,
|
|
92
|
+
.map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
94
93
|
.join(', ');
|
|
95
|
-
return `${(0,
|
|
94
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, utils_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
96
95
|
}
|
|
97
96
|
#targetRuntimeApiSpecs() {
|
|
98
97
|
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
@@ -112,7 +111,7 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
112
111
|
}, []);
|
|
113
112
|
}
|
|
114
113
|
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
115
|
-
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0,
|
|
114
|
+
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calcRuntimeApiHash)(one) === runtimeApiHash);
|
|
116
115
|
return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
117
116
|
};
|
|
118
117
|
}
|
package/cjs/generator/TxGen.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TxGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
class TxGen extends
|
|
4
|
+
const index_js_1 = require("../generator/index.js");
|
|
5
|
+
const utils_1 = require("@dedot/utils");
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
class TxGen extends index_js_1.ApiGen {
|
|
8
8
|
generate() {
|
|
9
9
|
const { pallets, types } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
@@ -26,9 +26,9 @@ class TxGen extends generator_1.ApiGen {
|
|
|
26
26
|
const isFlatEnum = type.value.members.every((m) => m.fields.length === 0);
|
|
27
27
|
const typedTxs = type.value.members
|
|
28
28
|
.map((one) => ({
|
|
29
|
-
functionName: (0,
|
|
29
|
+
functionName: (0, utils_1.stringCamelCase)(one.name),
|
|
30
30
|
params: one.fields.map((f) => ({
|
|
31
|
-
name: (0,
|
|
31
|
+
name: (0, utils_1.stringCamelCase)(f.name),
|
|
32
32
|
normalizedName: this.#normalizeParamName(f.name),
|
|
33
33
|
type: this.typesGen.generateType(f.typeId, 1),
|
|
34
34
|
docs: f.docs,
|
|
@@ -38,16 +38,16 @@ class TxGen extends generator_1.ApiGen {
|
|
|
38
38
|
.map((f) => {
|
|
39
39
|
return {
|
|
40
40
|
...f,
|
|
41
|
-
callInput: this.#generateCallInput((0,
|
|
41
|
+
callInput: this.#generateCallInput((0, utils_1.stringPascalCase)(pallet.name), (0, utils_1.stringPascalCase)(f.functionName), !isFlatEnum ? f.params.map((p) => `${p.name}: ${p.type}`) : undefined),
|
|
42
42
|
};
|
|
43
43
|
});
|
|
44
|
-
txDefsOut += (0,
|
|
45
|
-
txDefsOut += `${(0,
|
|
44
|
+
txDefsOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s transaction calls`);
|
|
45
|
+
txDefsOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
46
46
|
${typedTxs
|
|
47
|
-
.map(({ functionName, params, docs, callInput }) => `${(0,
|
|
47
|
+
.map(({ functionName, params, docs, callInput }) => `${(0, utils_js_1.commentBlock)(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<${callInput}>>`)
|
|
48
48
|
.join(',\n')}
|
|
49
49
|
|
|
50
|
-
${(0,
|
|
50
|
+
${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall>,
|
|
51
51
|
},`;
|
|
52
52
|
}
|
|
53
53
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
@@ -59,12 +59,12 @@ class TxGen extends generator_1.ApiGen {
|
|
|
59
59
|
|
|
60
60
|
export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic;
|
|
61
61
|
`;
|
|
62
|
-
const template = (0,
|
|
63
|
-
return (0,
|
|
62
|
+
const template = (0, utils_js_1.compileTemplate)('tx.hbs');
|
|
63
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypes, txDefsOut }));
|
|
64
64
|
}
|
|
65
65
|
#normalizeParamName(name) {
|
|
66
|
-
name = (0,
|
|
67
|
-
return (0,
|
|
66
|
+
name = (0, utils_1.stringCamelCase)(name);
|
|
67
|
+
return (0, utils_js_1.isReservedWord)(name) ? `${name}_` : name;
|
|
68
68
|
}
|
|
69
69
|
#generateCallInput(palletName, callName, params) {
|
|
70
70
|
if (!params) {
|