@dedot/codegen 0.0.1-next.020a0293.9 → 0.0.1-next.2d39ff2d.5
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/genSupportedChainTypes.js +8 -4
- package/cjs/generator/ConstsGen.js +8 -8
- package/cjs/generator/ErrorsGen.js +11 -11
- package/cjs/generator/EventsGen.js +12 -12
- package/cjs/generator/IndexGen.js +3 -3
- package/cjs/generator/QueryGen.js +8 -8
- package/cjs/generator/RpcGen.js +16 -15
- package/cjs/generator/RuntimeApisGen.js +19 -15
- package/cjs/generator/TxGen.js +9 -9
- package/cjs/generator/TypesGen.js +19 -18
- 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/index.js +10 -10
- package/cjs/packageInfo.js +1 -1
- package/genSupportedChainTypes.js +7 -3
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +2 -2
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +2 -2
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +2 -2
- package/generator/IndexGen.d.ts +1 -1
- package/generator/IndexGen.js +1 -1
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +2 -2
- package/generator/RpcGen.d.ts +1 -1
- package/generator/RpcGen.js +4 -3
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +8 -4
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +2 -2
- package/generator/TypesGen.d.ts +3 -3
- package/generator/TypesGen.js +12 -11
- 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/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +7 -7
- package/packageInfo.js +1 -1
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
6
|
+
const index_js_1 = require("./index.js");
|
|
7
7
|
const static_substrate_1 = require("@polkadot/types-support/metadata/static-substrate");
|
|
8
8
|
const substrate_hex_1 = __importDefault(require("@polkadot/types-support/metadata/v15/substrate-hex"));
|
|
9
9
|
const codecs_1 = require("@dedot/codecs");
|
|
@@ -38,6 +38,10 @@ const NETWORKS = [
|
|
|
38
38
|
chain: 'kusamaAssetHub',
|
|
39
39
|
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
40
40
|
},
|
|
41
|
+
{
|
|
42
|
+
chain: 'rococo',
|
|
43
|
+
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
44
|
+
},
|
|
41
45
|
{
|
|
42
46
|
chain: 'rococoAssetHub',
|
|
43
47
|
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
@@ -57,19 +61,19 @@ async function run() {
|
|
|
57
61
|
const { chain, endpoint, metadataHex, rpcMethods } = network;
|
|
58
62
|
if (endpoint) {
|
|
59
63
|
console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
|
|
60
|
-
await (0,
|
|
64
|
+
await (0, index_js_1.generateTypesFromChain)(network, endpoint, OUT_DIR);
|
|
61
65
|
}
|
|
62
66
|
else if (metadataHex && rpcMethods) {
|
|
63
67
|
console.log(`Generate types for ${chain} via raw data`);
|
|
64
68
|
const metadata = codecs_1.$Metadata.tryDecode(metadataHex);
|
|
65
69
|
const runtimeVersion = getRuntimeVersion(metadata);
|
|
66
|
-
await (0,
|
|
70
|
+
await (0, index_js_1.generateTypes)(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
|
|
67
71
|
}
|
|
68
72
|
}
|
|
69
73
|
console.log('DONE!');
|
|
70
74
|
}
|
|
71
75
|
const getRuntimeVersion = (metadata) => {
|
|
72
|
-
const registry = new codecs_1.
|
|
76
|
+
const registry = new codecs_1.PortableRegistry(metadata.latest);
|
|
73
77
|
const executor = new dedot_1.ConstantExecutor({
|
|
74
78
|
registry,
|
|
75
79
|
metadataLatest: metadata.latest,
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ConstsGen = void 0;
|
|
4
4
|
const util_1 = require("@polkadot/util");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
class ConstsGen extends
|
|
6
|
+
const index_js_1 = require("../generator/index.js");
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
class ConstsGen extends index_js_1.ApiGen {
|
|
9
9
|
generate() {
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
@@ -17,16 +17,16 @@ class ConstsGen extends generator_1.ApiGen {
|
|
|
17
17
|
type: this.typesGen.generateType(one.typeId, 1, true),
|
|
18
18
|
docs: one.docs,
|
|
19
19
|
}));
|
|
20
|
-
defTypeOut += (0,
|
|
20
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s constants`);
|
|
21
21
|
defTypeOut += `${(0, util_1.stringLowerFirst)(pallet.name)}: {
|
|
22
|
-
${typedConstants.map(({ name, type, docs }) => `${(0,
|
|
22
|
+
${typedConstants.map(({ name, type, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: ${type}`).join(',\n')}
|
|
23
23
|
|
|
24
|
-
${(0,
|
|
24
|
+
${(0, utils_js_1.commentBlock)('Generic pallet constant')}[name: string]: any,
|
|
25
25
|
},`;
|
|
26
26
|
}
|
|
27
27
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
28
|
-
const template = (0,
|
|
29
|
-
return (0,
|
|
28
|
+
const template = (0, utils_js_1.compileTemplate)('consts.hbs');
|
|
29
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
exports.ConstsGen = ConstsGen;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ErrorsGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
6
|
const util_1 = require("@polkadot/util");
|
|
7
|
-
const
|
|
8
|
-
class ErrorsGen extends
|
|
7
|
+
const utils_1 = require("@dedot/utils");
|
|
8
|
+
class ErrorsGen extends ApiGen_js_1.ApiGen {
|
|
9
9
|
generate() {
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
@@ -17,24 +17,24 @@ class ErrorsGen extends ApiGen_1.ApiGen {
|
|
|
17
17
|
continue;
|
|
18
18
|
}
|
|
19
19
|
const errorDefs = this.#getErrorDefs(errorTypeId);
|
|
20
|
-
defTypeOut += (0,
|
|
20
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
|
|
21
21
|
defTypeOut += `${(0, util_1.stringCamelCase)(pallet.name)}: {
|
|
22
22
|
${errorDefs
|
|
23
|
-
.map(({ name, docs }) => `${(0,
|
|
23
|
+
.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, util_1.stringPascalCase)(name)}: GenericPalletError`)
|
|
24
24
|
.join(',\n')}
|
|
25
25
|
|
|
26
|
-
${(0,
|
|
26
|
+
${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError,
|
|
27
27
|
},`;
|
|
28
28
|
}
|
|
29
29
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
30
|
-
const template = (0,
|
|
31
|
-
return (0,
|
|
30
|
+
const template = (0, utils_js_1.compileTemplate)('errors.hbs');
|
|
31
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
32
32
|
}
|
|
33
33
|
#getErrorDefs(errorTypeId) {
|
|
34
34
|
const def = this.metadata.types[errorTypeId];
|
|
35
|
-
(0,
|
|
35
|
+
(0, utils_1.assert)(def, `Error def not found for id ${errorTypeId}`);
|
|
36
36
|
const { tag, value } = def.type;
|
|
37
|
-
(0,
|
|
37
|
+
(0, utils_1.assert)(tag === 'Enum', `Invalid pallet error type!`);
|
|
38
38
|
return value.members;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventsGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
6
|
const util_1 = require("@polkadot/util");
|
|
7
|
-
const
|
|
8
|
-
class EventsGen extends
|
|
7
|
+
const utils_1 = require("@dedot/utils");
|
|
8
|
+
class EventsGen extends ApiGen_js_1.ApiGen {
|
|
9
9
|
generate() {
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
@@ -18,7 +18,7 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
18
18
|
}
|
|
19
19
|
const eventDefs = this.#getEventDefs(eventTypeId);
|
|
20
20
|
const flatMembers = eventDefs.every((d) => d.fields.length === 0);
|
|
21
|
-
defTypeOut += (0,
|
|
21
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s events`);
|
|
22
22
|
defTypeOut += `${(0, util_1.stringCamelCase)(pallet.name)}: {
|
|
23
23
|
${eventDefs
|
|
24
24
|
.map((def) => {
|
|
@@ -32,21 +32,21 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
32
32
|
}
|
|
33
33
|
return { ...def, genericParts };
|
|
34
34
|
})
|
|
35
|
-
.map(({ name, docs, fields, genericParts }) => `${(0,
|
|
35
|
+
.map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, util_1.stringPascalCase)(name)}: GenericPalletEvent<${genericParts.join(', ')}>`)
|
|
36
36
|
.join(',\n')}
|
|
37
37
|
|
|
38
|
-
${(0,
|
|
38
|
+
${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent,
|
|
39
39
|
},`;
|
|
40
40
|
}
|
|
41
41
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
42
|
-
const template = (0,
|
|
43
|
-
return (0,
|
|
42
|
+
const template = (0, utils_js_1.compileTemplate)('events.hbs');
|
|
43
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
44
44
|
}
|
|
45
45
|
#getEventDefs(typeId) {
|
|
46
46
|
const def = this.metadata.types[typeId];
|
|
47
|
-
(0,
|
|
47
|
+
(0, utils_1.assert)(def, `Event def not found for id ${typeId}`);
|
|
48
48
|
const { tag, value } = def.type;
|
|
49
|
-
(0,
|
|
49
|
+
(0, utils_1.assert)(tag === 'Enum', 'Invalid pallet event type!');
|
|
50
50
|
return value.members;
|
|
51
51
|
}
|
|
52
52
|
#generateMemberDefType(fields) {
|
|
@@ -57,7 +57,7 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
57
57
|
return fields.length === 1
|
|
58
58
|
? this.typesGen.generateType(fields[0].typeId, 1, true)
|
|
59
59
|
: `[${fields
|
|
60
|
-
.map(({ typeId, docs }) => `${(0,
|
|
60
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.typesGen.generateType(typeId, 1, true)}`)
|
|
61
61
|
.join(', ')}]`;
|
|
62
62
|
}
|
|
63
63
|
else {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.IndexGen = void 0;
|
|
4
|
-
const
|
|
4
|
+
const utils_js_1 = require("./utils.js");
|
|
5
5
|
const util_1 = require("@polkadot/util");
|
|
6
6
|
class IndexGen {
|
|
7
7
|
networkInfo;
|
|
@@ -11,8 +11,8 @@ class IndexGen {
|
|
|
11
11
|
async generate() {
|
|
12
12
|
const { chain } = this.networkInfo;
|
|
13
13
|
const interfaceName = (0, util_1.stringPascalCase)(chain);
|
|
14
|
-
const template = (0,
|
|
15
|
-
return (0,
|
|
14
|
+
const template = (0, utils_js_1.compileTemplate)('index.hbs');
|
|
15
|
+
return (0, utils_js_1.beautifySourceCode)(template({ interfaceName }));
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
exports.IndexGen = IndexGen;
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.QueryGen = void 0;
|
|
4
4
|
const util_1 = require("@polkadot/util");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
class QueryGen extends
|
|
6
|
+
const index_js_1 = require("../generator/index.js");
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
class QueryGen extends index_js_1.ApiGen {
|
|
9
9
|
generate() {
|
|
10
10
|
const { pallets } = this.metadata;
|
|
11
11
|
this.typesGen.clearCache();
|
|
@@ -16,17 +16,17 @@ class QueryGen extends generator_1.ApiGen {
|
|
|
16
16
|
if (!storage)
|
|
17
17
|
continue;
|
|
18
18
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
19
|
-
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${(0,
|
|
20
|
-
defTypeOut += (0,
|
|
19
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<(${keyType}) => ${valueType}>`);
|
|
20
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
21
21
|
defTypeOut += `${(0, util_1.stringLowerFirst)(pallet.name)}: {
|
|
22
22
|
${queryDefs.join(',\n')}
|
|
23
23
|
|
|
24
|
-
${(0,
|
|
24
|
+
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery;
|
|
25
25
|
},`;
|
|
26
26
|
}
|
|
27
27
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
28
|
-
const template = (0,
|
|
29
|
-
return (0,
|
|
28
|
+
const template = (0, utils_js_1.compileTemplate)('query.hbs');
|
|
29
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
30
30
|
}
|
|
31
31
|
#generateEntry(entry) {
|
|
32
32
|
const { name, type, docs, modifier } = entry;
|
package/cjs/generator/RpcGen.js
CHANGED
|
@@ -3,13 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RpcGen = void 0;
|
|
4
4
|
const specs_1 = require("@dedot/specs");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
6
|
+
const index_js_1 = require("../generator/index.js");
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
8
9
|
const HIDDEN_RPCS = [
|
|
9
10
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
10
11
|
'rpc_methods',
|
|
11
12
|
];
|
|
12
|
-
class RpcGen extends
|
|
13
|
+
class RpcGen extends index_js_1.ApiGen {
|
|
13
14
|
typesGen;
|
|
14
15
|
rpcMethods;
|
|
15
16
|
constructor(typesGen, rpcMethods) {
|
|
@@ -62,8 +63,8 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
62
63
|
});
|
|
63
64
|
// TODO include & define external types
|
|
64
65
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
65
|
-
const template = (0,
|
|
66
|
-
return (0,
|
|
66
|
+
const template = (0, utils_js_1.compileTemplate)('rpc.hbs');
|
|
67
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
67
68
|
}
|
|
68
69
|
#generateMethodDef(spec) {
|
|
69
70
|
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
@@ -73,7 +74,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
73
74
|
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
74
75
|
}
|
|
75
76
|
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
76
|
-
return `${(0,
|
|
77
|
+
return `${(0, utils_js_1.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
77
78
|
}
|
|
78
79
|
this.addTypeImport(type, false);
|
|
79
80
|
params.forEach(({ type, isScale }) => {
|
|
@@ -91,10 +92,10 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
91
92
|
defaultDocs.shift();
|
|
92
93
|
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
93
94
|
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
94
|
-
return `${(0,
|
|
95
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
95
96
|
}
|
|
96
97
|
else {
|
|
97
|
-
return `${(0,
|
|
98
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
101
|
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
@@ -108,11 +109,11 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
108
109
|
return;
|
|
109
110
|
}
|
|
110
111
|
// Handle generic wrapper types
|
|
111
|
-
const matchArray = type.match(
|
|
112
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
112
113
|
if (matchArray) {
|
|
113
114
|
const [_, $1, $2] = matchArray;
|
|
114
115
|
this.addTypeImport($1, toTypeIn);
|
|
115
|
-
if ($2.match(
|
|
116
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
116
117
|
this.addTypeImport($2, toTypeIn);
|
|
117
118
|
}
|
|
118
119
|
else {
|
|
@@ -121,7 +122,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
121
122
|
return;
|
|
122
123
|
}
|
|
123
124
|
// Check tuple type
|
|
124
|
-
if (type.match(
|
|
125
|
+
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
125
126
|
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
126
127
|
return;
|
|
127
128
|
}
|
|
@@ -141,11 +142,11 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
141
142
|
}
|
|
142
143
|
getGeneratedTypeName(type, toTypeIn = true) {
|
|
143
144
|
try {
|
|
144
|
-
const matchArray = type.match(
|
|
145
|
+
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
145
146
|
if (matchArray) {
|
|
146
147
|
const [_, $1, $2] = matchArray;
|
|
147
148
|
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
148
|
-
if ($2.match(
|
|
149
|
+
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
149
150
|
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
150
151
|
}
|
|
151
152
|
const innerTypeNames = $2
|
|
@@ -154,7 +155,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
154
155
|
.join(', ');
|
|
155
156
|
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
156
157
|
}
|
|
157
|
-
else if (type.match(
|
|
158
|
+
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
158
159
|
const innerTypeNames = type
|
|
159
160
|
.slice(1, -1)
|
|
160
161
|
.split(',')
|
|
@@ -168,7 +169,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
168
169
|
return type;
|
|
169
170
|
}
|
|
170
171
|
#getCodecType(type, toTypeIn = true) {
|
|
171
|
-
const { typeIn, typeOut } =
|
|
172
|
+
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
172
173
|
return toTypeIn ? typeIn : typeOut;
|
|
173
174
|
}
|
|
174
175
|
}
|
|
@@ -2,11 +2,11 @@
|
|
|
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
|
|
5
|
+
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const utils_1 = require("@dedot/utils");
|
|
7
|
+
const RpcGen_js_1 = require("./RpcGen.js");
|
|
8
8
|
const util_1 = require("@polkadot/util");
|
|
9
|
-
class RuntimeApisGen extends
|
|
9
|
+
class RuntimeApisGen extends RpcGen_js_1.RpcGen {
|
|
10
10
|
typesGen;
|
|
11
11
|
runtimeApis;
|
|
12
12
|
constructor(typesGen, runtimeApis) {
|
|
@@ -21,18 +21,18 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
21
21
|
if (this.metadata.apis.length > 0) {
|
|
22
22
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
23
23
|
const { name: runtimeApiName, methods } = runtimeApi;
|
|
24
|
-
runtimeCallsOut += (0,
|
|
24
|
+
runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${(0, utils_1.calculateRuntimeApiHash)(runtimeApiName)}`);
|
|
25
25
|
runtimeCallsOut += `${(0, util_1.stringCamelCase)(runtimeApiName)}: {
|
|
26
26
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
27
27
|
|
|
28
|
-
${(0,
|
|
28
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
29
29
|
}`;
|
|
30
30
|
});
|
|
31
31
|
}
|
|
32
32
|
else {
|
|
33
33
|
const specs = this.#targetRuntimeApiSpecs();
|
|
34
34
|
specs.forEach(({ methods, runtimeApiName, runtimeApiHash, version }) => {
|
|
35
|
-
runtimeCallsOut += (0,
|
|
35
|
+
runtimeCallsOut += (0, utils_js_1.commentBlock)(`@runtimeapi: ${runtimeApiName} - ${runtimeApiHash}`, `@version: ${version}`);
|
|
36
36
|
runtimeCallsOut += `${(0, util_1.stringCamelCase)(runtimeApiName)}: {
|
|
37
37
|
${Object.keys(methods)
|
|
38
38
|
.map((methodName) => this.#generateMethodDefFromSpec({
|
|
@@ -42,13 +42,13 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
42
42
|
}))
|
|
43
43
|
.join('\n')}
|
|
44
44
|
|
|
45
|
-
${(0,
|
|
45
|
+
${(0, utils_js_1.commentBlock)('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
46
46
|
}`;
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
49
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
-
const template = (0,
|
|
51
|
-
return (0,
|
|
50
|
+
const template = (0, utils_js_1.compileTemplate)('runtime.hbs');
|
|
51
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, runtimeCallsOut }));
|
|
52
52
|
}
|
|
53
53
|
#isOptionalType(type) {
|
|
54
54
|
return type.startsWith('Option<') || type.endsWith('| undefined');
|
|
@@ -58,7 +58,7 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
58
58
|
}
|
|
59
59
|
#generateMethodDefFromSpec(spec) {
|
|
60
60
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
61
|
-
const callName = `${runtimeApiName}_${(0,
|
|
61
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
62
62
|
const defaultDocs = [`@callname: ${callName}`];
|
|
63
63
|
this.addTypeImport(type, false);
|
|
64
64
|
params.forEach(({ type }) => this.addTypeImport(type));
|
|
@@ -71,11 +71,11 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
71
71
|
.map(({ name, isOptional, plainType }) => `${(0, util_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
72
72
|
.join(', ');
|
|
73
73
|
const typeOut = this.getGeneratedTypeName(type, false);
|
|
74
|
-
return `${(0,
|
|
74
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
75
75
|
}
|
|
76
76
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
77
77
|
const { name: methodName, inputs, output, docs } = methodDef;
|
|
78
|
-
const callName = `${runtimeApiName}_${(0,
|
|
78
|
+
const callName = `${runtimeApiName}_${(0, utils_1.stringSnakeCase)(methodName)}`;
|
|
79
79
|
const defaultDocs = [`@callname: ${callName}`];
|
|
80
80
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
81
81
|
this.addTypeImport(typeOut, false);
|
|
@@ -92,11 +92,11 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
92
92
|
const paramsOut = typedInputs
|
|
93
93
|
.map(({ name, type, isOptional }) => `${(0, util_1.stringCamelCase)(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
94
94
|
.join(', ');
|
|
95
|
-
return `${(0,
|
|
95
|
+
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${(0, util_1.stringCamelCase)(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
96
96
|
}
|
|
97
97
|
#targetRuntimeApiSpecs() {
|
|
98
98
|
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
99
|
-
const runtimeApiSpec =
|
|
99
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
100
100
|
if (!runtimeApiSpec)
|
|
101
101
|
return;
|
|
102
102
|
return {
|
|
@@ -111,5 +111,9 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
111
111
|
return [...o, spec];
|
|
112
112
|
}, []);
|
|
113
113
|
}
|
|
114
|
+
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
115
|
+
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_1.calculateRuntimeApiHash)(one) === runtimeApiHash);
|
|
116
|
+
return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
117
|
+
};
|
|
114
118
|
}
|
|
115
119
|
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
|
|
4
|
+
const index_js_1 = require("../generator/index.js");
|
|
5
5
|
const util_1 = require("@polkadot/util");
|
|
6
|
-
const
|
|
7
|
-
class TxGen extends
|
|
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();
|
|
@@ -41,13 +41,13 @@ class TxGen extends generator_1.ApiGen {
|
|
|
41
41
|
callInput: this.#generateCallInput((0, util_1.stringPascalCase)(pallet.name), (0, util_1.stringPascalCase)(f.functionName), !isFlatEnum ? f.params.map((p) => `${p.name}: ${p.type}`) : undefined),
|
|
42
42
|
};
|
|
43
43
|
});
|
|
44
|
-
txDefsOut += (0,
|
|
44
|
+
txDefsOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s transaction calls`);
|
|
45
45
|
txDefsOut += `${(0, util_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
66
|
name = (0, util_1.stringCamelCase)(name);
|
|
67
|
-
return (0,
|
|
67
|
+
return (0, utils_js_1.isReservedWord)(name) ? `${name}_` : name;
|
|
68
68
|
}
|
|
69
69
|
#generateCallInput(palletName, callName, params) {
|
|
70
70
|
if (!params) {
|
|
@@ -4,9 +4,10 @@ exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
|
4
4
|
const util_1 = require("@polkadot/util");
|
|
5
5
|
const codecs_1 = require("@dedot/codecs");
|
|
6
6
|
const utils_1 = require("@dedot/utils");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
const known_types_js_1 = require("./known-types.js");
|
|
9
|
+
const TypeImports_js_1 = require("./TypeImports.js");
|
|
10
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
10
11
|
// Skip generate types for these
|
|
11
12
|
// as we do have native types for them
|
|
12
13
|
const SKIP_TYPES = [
|
|
@@ -40,9 +41,9 @@ class TypesGen {
|
|
|
40
41
|
typeImports;
|
|
41
42
|
constructor(metadata) {
|
|
42
43
|
this.metadata = metadata;
|
|
43
|
-
this.registry = new codecs_1.
|
|
44
|
+
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
44
45
|
this.includedTypes = this.#includedTypes();
|
|
45
|
-
this.typeImports = new
|
|
46
|
+
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
46
47
|
}
|
|
47
48
|
generate() {
|
|
48
49
|
this.clearCache();
|
|
@@ -50,14 +51,14 @@ class TypesGen {
|
|
|
50
51
|
Object.values(this.includedTypes)
|
|
51
52
|
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
52
53
|
.forEach(({ name, nameOut, id, docs }) => {
|
|
53
|
-
defTypeOut += `${(0,
|
|
54
|
+
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
54
55
|
if (this.#shouldGenerateTypeIn(id)) {
|
|
55
56
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
56
57
|
}
|
|
57
58
|
});
|
|
58
59
|
const importTypes = this.typeImports.toImports('./types');
|
|
59
|
-
const template = (0,
|
|
60
|
-
return (0,
|
|
60
|
+
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
61
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
61
62
|
}
|
|
62
63
|
typeCache = {};
|
|
63
64
|
clearCache() {
|
|
@@ -102,7 +103,7 @@ class TypesGen {
|
|
|
102
103
|
const { tag, value } = type;
|
|
103
104
|
switch (tag) {
|
|
104
105
|
case 'Primitive':
|
|
105
|
-
const $codec =
|
|
106
|
+
const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
|
|
106
107
|
if ($codec.nativeType) {
|
|
107
108
|
return $codec.nativeType;
|
|
108
109
|
}
|
|
@@ -150,7 +151,7 @@ class TypesGen {
|
|
|
150
151
|
return 'null';
|
|
151
152
|
}
|
|
152
153
|
else if (members.every((x) => x.fields.length === 0)) {
|
|
153
|
-
return members.map(({ name, docs }) => `${(0,
|
|
154
|
+
return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, util_1.stringPascalCase)(name)}'`).join(' | ');
|
|
154
155
|
}
|
|
155
156
|
else {
|
|
156
157
|
const membersType = [];
|
|
@@ -163,7 +164,7 @@ class TypesGen {
|
|
|
163
164
|
const valueType = fields.length === 1
|
|
164
165
|
? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
|
|
165
166
|
: `[${fields
|
|
166
|
-
.map(({ typeId, docs }) => `${(0,
|
|
167
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
|
|
167
168
|
.join(', ')}]`;
|
|
168
169
|
membersType.push([keyName, valueType, docs]);
|
|
169
170
|
}
|
|
@@ -171,14 +172,14 @@ class TypesGen {
|
|
|
171
172
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
172
173
|
}
|
|
173
174
|
}
|
|
174
|
-
const { tagKey, valueKey } = this.registry.
|
|
175
|
+
const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
|
|
175
176
|
return membersType
|
|
176
177
|
.map(([keyName, valueType, docs]) => ({
|
|
177
178
|
tag: `${tagKey}: '${keyName}'`,
|
|
178
179
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
179
180
|
docs,
|
|
180
181
|
}))
|
|
181
|
-
.map(({ tag, value, docs }) => `${(0,
|
|
182
|
+
.map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
|
|
182
183
|
.join(' | ');
|
|
183
184
|
}
|
|
184
185
|
}
|
|
@@ -225,7 +226,7 @@ class TypesGen {
|
|
|
225
226
|
};
|
|
226
227
|
});
|
|
227
228
|
return `{${props
|
|
228
|
-
.map(({ name, type, optional, docs }) => `${(0,
|
|
229
|
+
.map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
|
|
229
230
|
.join(',\n')}}`;
|
|
230
231
|
}
|
|
231
232
|
#isOptionalType(type) {
|
|
@@ -266,8 +267,8 @@ class TypesGen {
|
|
|
266
267
|
const suffix = typeSuffixes.get(id) || '';
|
|
267
268
|
let knownType = false;
|
|
268
269
|
let name, nameOut;
|
|
269
|
-
if (
|
|
270
|
-
const codecType =
|
|
270
|
+
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
271
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
271
272
|
name = codecType.typeIn;
|
|
272
273
|
nameOut = codecType.typeOut;
|
|
273
274
|
knownType = true;
|
|
@@ -320,7 +321,7 @@ class TypesGen {
|
|
|
320
321
|
}
|
|
321
322
|
#shouldGenerateTypeIn(id) {
|
|
322
323
|
const { callTypeId } = this.metadata.extrinsic;
|
|
323
|
-
const palletCallTypeIds = this.registry.
|
|
324
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
324
325
|
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
325
326
|
}
|
|
326
327
|
eqlCache = new Map();
|
|
@@ -425,7 +426,7 @@ class TypesGen {
|
|
|
425
426
|
this.typeImports.addCodecType(typeName);
|
|
426
427
|
return;
|
|
427
428
|
}
|
|
428
|
-
if (
|
|
429
|
+
if (known_types_js_1.knownTypes.includes(typeName)) {
|
|
429
430
|
this.typeImports.addKnownType(typeName);
|
|
430
431
|
}
|
|
431
432
|
else {
|