@dedot/codegen 0.0.1-next.3ec72a37.8 → 0.0.1-next.41a5fa17.16
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 +9 -10
- package/cjs/generator/ErrorsGen.js +12 -13
- package/cjs/generator/EventsGen.js +13 -14
- package/cjs/generator/IndexGen.js +5 -5
- package/cjs/generator/QueryGen.js +9 -10
- package/cjs/generator/RpcGen.js +18 -18
- package/cjs/generator/RuntimeApisGen.js +23 -20
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypesGen.js +24 -24
- 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 +30 -1
- package/cjs/index.js +12 -12
- 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 +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 +1 -1
- package/generator/IndexGen.js +2 -2
- 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 +10 -7
- 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/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 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +1 -1
- package/index.js +2 -2
- package/package.json +7 -8
- 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,
|
|
@@ -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,8 +1,8 @@
|
|
|
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
7
|
networkInfo;
|
|
8
8
|
constructor(networkInfo) {
|
|
@@ -10,9 +10,9 @@ class IndexGen {
|
|
|
10
10
|
}
|
|
11
11
|
async generate() {
|
|
12
12
|
const { chain } = this.networkInfo;
|
|
13
|
-
const interfaceName = (0,
|
|
14
|
-
const template = (0,
|
|
15
|
-
return (0,
|
|
13
|
+
const interfaceName = (0, utils_1.stringPascalCase)(chain);
|
|
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;
|
|
@@ -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,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,13 +89,13 @@ 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]) => {
|
|
99
|
-
const runtimeApiSpec =
|
|
98
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
100
99
|
if (!runtimeApiSpec)
|
|
101
100
|
return;
|
|
102
101
|
return {
|
|
@@ -111,5 +110,9 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
111
110
|
return [...o, spec];
|
|
112
111
|
}, []);
|
|
113
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
|
+
};
|
|
114
117
|
}
|
|
115
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) {
|