@dedot/codegen 0.0.1-alpha.3 → 0.0.1-alpha.30
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 -18
- package/cjs/generator/RuntimeApisGen.js +34 -36
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypesGen.js +24 -24
- package/cjs/{types.js → generator/dirname.js} +3 -0
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +151 -0
- package/cjs/generator/known-types.js +33 -0
- package/cjs/generator/utils.js +33 -9
- package/cjs/index.js +29 -29
- package/cjs/templates/consts.hbs +7 -0
- package/cjs/templates/errors.hbs +7 -0
- package/cjs/templates/events.hbs +7 -0
- package/cjs/templates/index.hbs +22 -0
- package/cjs/templates/query.hbs +7 -0
- package/cjs/templates/rpc.hbs +7 -0
- package/cjs/templates/runtime.hbs +8 -0
- package/cjs/templates/tx.hbs +9 -0
- package/cjs/templates/types.hbs +5 -0
- package/generator/ApiGen.d.ts +3 -3
- 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 +4 -4
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +23 -25
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypesGen.d.ts +3 -3
- package/generator/TypesGen.js +13 -13
- package/generator/dirname.d.ts +1 -0
- package/generator/dirname.js +6 -0
- 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/known-types.d.ts +6 -0
- package/generator/known-types.js +30 -0
- package/generator/utils.d.ts +5 -1
- package/generator/utils.js +31 -7
- package/index.d.ts +2 -3
- package/index.js +19 -19
- package/package.json +11 -10
- package/templates/consts.hbs +7 -0
- package/templates/errors.hbs +7 -0
- package/templates/events.hbs +7 -0
- package/templates/index.hbs +22 -0
- package/templates/query.hbs +7 -0
- package/templates/rpc.hbs +7 -0
- package/templates/runtime.hbs +8 -0
- package/templates/tx.hbs +9 -0
- package/templates/types.hbs +5 -0
- package/cjs/genSupportedChainTypes.js +0 -79
- package/cjs/packageInfo.js +0 -5
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -74
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- 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,14 +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
|
|
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
8
|
const HIDDEN_RPCS = [
|
|
9
9
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
10
10
|
'rpc_methods',
|
|
11
11
|
];
|
|
12
|
-
class RpcGen extends
|
|
12
|
+
class RpcGen extends index_js_1.ApiGen {
|
|
13
13
|
typesGen;
|
|
14
14
|
rpcMethods;
|
|
15
15
|
constructor(typesGen, rpcMethods) {
|
|
@@ -62,8 +62,8 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
62
62
|
});
|
|
63
63
|
// TODO include & define external types
|
|
64
64
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
65
|
-
const template = (0,
|
|
66
|
-
return (0,
|
|
65
|
+
const template = (0, utils_js_1.compileTemplate)('rpc.hbs');
|
|
66
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
67
67
|
}
|
|
68
68
|
#generateMethodDef(spec) {
|
|
69
69
|
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
@@ -73,7 +73,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
73
73
|
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
74
74
|
}
|
|
75
75
|
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
76
|
-
return `${(0,
|
|
76
|
+
return `${(0, utils_js_1.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
77
77
|
}
|
|
78
78
|
this.addTypeImport(type, false);
|
|
79
79
|
params.forEach(({ type, isScale }) => {
|
|
@@ -91,10 +91,10 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
91
91
|
defaultDocs.shift();
|
|
92
92
|
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
93
93
|
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
94
|
-
return `${(0,
|
|
94
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
95
95
|
}
|
|
96
96
|
else {
|
|
97
|
-
return `${(0,
|
|
97
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
100
|
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
@@ -104,15 +104,15 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
104
104
|
return;
|
|
105
105
|
}
|
|
106
106
|
type = type.trim();
|
|
107
|
-
if ((0,
|
|
107
|
+
if ((0, utils_js_1.isNativeType)(type)) {
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
110
|
// Handle generic wrapper types
|
|
111
|
-
const matchArray = type.match(
|
|
111
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
112
112
|
if (matchArray) {
|
|
113
113
|
const [_, $1, $2] = matchArray;
|
|
114
114
|
this.addTypeImport($1, toTypeIn);
|
|
115
|
-
if ($2.match(
|
|
115
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
116
116
|
this.addTypeImport($2, toTypeIn);
|
|
117
117
|
}
|
|
118
118
|
else {
|
|
@@ -121,7 +121,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
121
121
|
return;
|
|
122
122
|
}
|
|
123
123
|
// Check tuple type
|
|
124
|
-
if (type.match(
|
|
124
|
+
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
125
125
|
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
126
126
|
return;
|
|
127
127
|
}
|
|
@@ -131,7 +131,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
131
131
|
}
|
|
132
132
|
try {
|
|
133
133
|
const codecType = this.#getCodecType(type, toTypeIn);
|
|
134
|
-
if ((0,
|
|
134
|
+
if ((0, utils_js_1.isNativeType)(codecType))
|
|
135
135
|
return;
|
|
136
136
|
this.typesGen.typeImports.addCodecType(codecType);
|
|
137
137
|
return;
|
|
@@ -141,11 +141,11 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
141
141
|
}
|
|
142
142
|
getGeneratedTypeName(type, toTypeIn = true) {
|
|
143
143
|
try {
|
|
144
|
-
const matchArray = type.match(
|
|
144
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
145
145
|
if (matchArray) {
|
|
146
146
|
const [_, $1, $2] = matchArray;
|
|
147
147
|
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
148
|
-
if ($2.match(
|
|
148
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
149
149
|
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
150
150
|
}
|
|
151
151
|
const innerTypeNames = $2
|
|
@@ -154,7 +154,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
154
154
|
.join(', ');
|
|
155
155
|
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
156
156
|
}
|
|
157
|
-
else if (type.match(
|
|
157
|
+
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
158
158
|
const innerTypeNames = type
|
|
159
159
|
.slice(1, -1)
|
|
160
160
|
.split(',')
|
|
@@ -168,7 +168,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
168
168
|
return type;
|
|
169
169
|
}
|
|
170
170
|
#getCodecType(type, toTypeIn = true) {
|
|
171
|
-
const { typeIn, typeOut } =
|
|
171
|
+
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
172
172
|
return toTypeIn ? typeIn : typeOut;
|
|
173
173
|
}
|
|
174
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,32 +20,34 @@ 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
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
runtimeCallsOut += `${(0, util_1.stringCamelCase)(runtimeApiName)}: {
|
|
32
|
+
const specs = this.#targetRuntimeApiSpecs();
|
|
33
|
+
specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
|
|
34
|
+
runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
|
|
35
|
+
runtimeCallsOut += `${(0, utils_1.stringCamelCase)(runtimeApiName)}: {
|
|
38
36
|
${Object.keys(methods)
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
.map((methodName) => this.#generateMethodDefFromSpec({
|
|
38
|
+
...methods[methodName],
|
|
39
|
+
runtimeApiName,
|
|
40
|
+
methodName,
|
|
41
|
+
}))
|
|
42
|
+
.join('\n')}
|
|
41
43
|
|
|
42
|
-
${(0,
|
|
44
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
43
45
|
}`;
|
|
44
|
-
});
|
|
45
46
|
});
|
|
46
47
|
}
|
|
47
48
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
48
|
-
const template = (0,
|
|
49
|
-
return (0,
|
|
49
|
+
const template = (0, utils_js_1.compileTemplate)('runtime.hbs');
|
|
50
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, runtimeCallsOut }));
|
|
50
51
|
}
|
|
51
52
|
#isOptionalType(type) {
|
|
52
53
|
return type.startsWith('Option<') || type.endsWith('| undefined');
|
|
@@ -56,7 +57,7 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
56
57
|
}
|
|
57
58
|
#generateMethodDefFromSpec(spec) {
|
|
58
59
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
59
|
-
const callName = `${runtimeApiName}_${(0,
|
|
60
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
60
61
|
const defaultDocs = [`@callname: ${callName}`];
|
|
61
62
|
this.addTypeImport(type, false);
|
|
62
63
|
params.forEach(({ type }) => this.addTypeImport(type));
|
|
@@ -66,14 +67,14 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
66
67
|
plainType: this.getGeneratedTypeName(param.type),
|
|
67
68
|
}));
|
|
68
69
|
const paramsOut = typedParams
|
|
69
|
-
.map(({ name, isOptional, plainType }) => `${(0,
|
|
70
|
+
.map(({ name, isOptional, plainType }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
70
71
|
.join(', ');
|
|
71
72
|
const typeOut = this.getGeneratedTypeName(type, false);
|
|
72
|
-
return `${(0,
|
|
73
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
73
74
|
}
|
|
74
75
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
75
76
|
const { name: methodName, inputs, output, docs } = methodDef;
|
|
76
|
-
const callName = `${runtimeApiName}_${(0,
|
|
77
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
77
78
|
const defaultDocs = [`@callname: ${callName}`];
|
|
78
79
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
79
80
|
this.addTypeImport(typeOut, false);
|
|
@@ -88,13 +89,13 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
88
89
|
}));
|
|
89
90
|
this.addTypeImport(typedInputs.map((t) => t.type));
|
|
90
91
|
const paramsOut = typedInputs
|
|
91
|
-
.map(({ name, type, isOptional }) => `${(0,
|
|
92
|
+
.map(({ name, type, isOptional }) => `${(0, utils_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
92
93
|
.join(', ');
|
|
93
|
-
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}>>`;
|
|
94
95
|
}
|
|
95
|
-
#
|
|
96
|
+
#targetRuntimeApiSpecs() {
|
|
96
97
|
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
97
|
-
const runtimeApiSpec =
|
|
98
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
98
99
|
if (!runtimeApiSpec)
|
|
99
100
|
return;
|
|
100
101
|
return {
|
|
@@ -106,15 +107,12 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
106
107
|
if (!spec) {
|
|
107
108
|
return o;
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
return o;
|
|
112
|
-
}
|
|
113
|
-
return {
|
|
114
|
-
...o,
|
|
115
|
-
[moduleName]: o[moduleName] ? [...o[moduleName], spec] : [spec],
|
|
116
|
-
};
|
|
117
|
-
}, {});
|
|
110
|
+
return [...o, spec];
|
|
111
|
+
}, []);
|
|
118
112
|
}
|
|
113
|
+
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
114
|
+
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calcRuntimeApiHash)(one) === runtimeApiHash);
|
|
115
|
+
return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
116
|
+
};
|
|
119
117
|
}
|
|
120
118
|
exports.RuntimeApisGen = RuntimeApisGen;
|
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) {
|