@dedot/codegen 0.0.1-alpha.3 → 0.0.1-alpha.31
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,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
5
4
|
const codecs_1 = require("@dedot/codecs");
|
|
6
5
|
const utils_1 = require("@dedot/utils");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
6
|
+
const utils_js_1 = require("./utils.js");
|
|
7
|
+
const known_types_js_1 = require("./known-types.js");
|
|
8
|
+
const TypeImports_js_1 = require("./TypeImports.js");
|
|
9
|
+
const known_codecs_js_1 = require("./known-codecs.js");
|
|
10
10
|
// Skip generate types for these
|
|
11
11
|
// as we do have native types for them
|
|
12
12
|
const SKIP_TYPES = [
|
|
@@ -40,9 +40,9 @@ class TypesGen {
|
|
|
40
40
|
typeImports;
|
|
41
41
|
constructor(metadata) {
|
|
42
42
|
this.metadata = metadata;
|
|
43
|
-
this.registry = new codecs_1.
|
|
43
|
+
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
44
44
|
this.includedTypes = this.#includedTypes();
|
|
45
|
-
this.typeImports = new
|
|
45
|
+
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
46
46
|
}
|
|
47
47
|
generate() {
|
|
48
48
|
this.clearCache();
|
|
@@ -50,14 +50,14 @@ class TypesGen {
|
|
|
50
50
|
Object.values(this.includedTypes)
|
|
51
51
|
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
52
52
|
.forEach(({ name, nameOut, id, docs }) => {
|
|
53
|
-
defTypeOut += `${(0,
|
|
53
|
+
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
54
54
|
if (this.#shouldGenerateTypeIn(id)) {
|
|
55
55
|
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
56
56
|
}
|
|
57
57
|
});
|
|
58
58
|
const importTypes = this.typeImports.toImports('./types');
|
|
59
|
-
const template = (0,
|
|
60
|
-
return (0,
|
|
59
|
+
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
60
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
61
61
|
}
|
|
62
62
|
typeCache = {};
|
|
63
63
|
clearCache() {
|
|
@@ -102,7 +102,7 @@ class TypesGen {
|
|
|
102
102
|
const { tag, value } = type;
|
|
103
103
|
switch (tag) {
|
|
104
104
|
case 'Primitive':
|
|
105
|
-
const $codec =
|
|
105
|
+
const $codec = (0, known_codecs_js_1.findKnownCodec)(value.kind);
|
|
106
106
|
if ($codec.nativeType) {
|
|
107
107
|
return $codec.nativeType;
|
|
108
108
|
}
|
|
@@ -150,12 +150,12 @@ class TypesGen {
|
|
|
150
150
|
return 'null';
|
|
151
151
|
}
|
|
152
152
|
else if (members.every((x) => x.fields.length === 0)) {
|
|
153
|
-
return members.map(({ name, docs }) => `${(0,
|
|
153
|
+
return members.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}'${(0, utils_1.stringPascalCase)(name)}'`).join(' | ');
|
|
154
154
|
}
|
|
155
155
|
else {
|
|
156
156
|
const membersType = [];
|
|
157
157
|
for (const { fields, name, docs } of members) {
|
|
158
|
-
const keyName = (0,
|
|
158
|
+
const keyName = (0, utils_1.stringPascalCase)(name);
|
|
159
159
|
if (fields.length === 0) {
|
|
160
160
|
membersType.push([keyName, null, docs]);
|
|
161
161
|
}
|
|
@@ -163,7 +163,7 @@ class TypesGen {
|
|
|
163
163
|
const valueType = fields.length === 1
|
|
164
164
|
? this.generateType(fields[0].typeId, nestedLevel + 1, typeOut)
|
|
165
165
|
: `[${fields
|
|
166
|
-
.map(({ typeId, docs }) => `${(0,
|
|
166
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.generateType(typeId, nestedLevel + 1, typeOut)}`)
|
|
167
167
|
.join(', ')}]`;
|
|
168
168
|
membersType.push([keyName, valueType, docs]);
|
|
169
169
|
}
|
|
@@ -171,14 +171,14 @@ class TypesGen {
|
|
|
171
171
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
|
-
const { tagKey, valueKey } = this.registry.
|
|
174
|
+
const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
|
|
175
175
|
return membersType
|
|
176
176
|
.map(([keyName, valueType, docs]) => ({
|
|
177
177
|
tag: `${tagKey}: '${keyName}'`,
|
|
178
178
|
value: valueType ? `, ${valueKey}${this.#isOptionalType(valueType) ? '?' : ''}: ${valueType} ` : '',
|
|
179
179
|
docs,
|
|
180
180
|
}))
|
|
181
|
-
.map(({ tag, value, docs }) => `${(0,
|
|
181
|
+
.map(({ tag, value, docs }) => `${(0, utils_js_1.commentBlock)(docs)}{ ${tag}${value} }`)
|
|
182
182
|
.join(' | ');
|
|
183
183
|
}
|
|
184
184
|
}
|
|
@@ -225,7 +225,7 @@ class TypesGen {
|
|
|
225
225
|
};
|
|
226
226
|
});
|
|
227
227
|
return `{${props
|
|
228
|
-
.map(({ name, type, optional, docs }) => `${(0,
|
|
228
|
+
.map(({ name, type, optional, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}${optional ? '?' : ''}: ${type}`)
|
|
229
229
|
.join(',\n')}}`;
|
|
230
230
|
}
|
|
231
231
|
#isOptionalType(type) {
|
|
@@ -266,8 +266,8 @@ class TypesGen {
|
|
|
266
266
|
const suffix = typeSuffixes.get(id) || '';
|
|
267
267
|
let knownType = false;
|
|
268
268
|
let name, nameOut;
|
|
269
|
-
if (
|
|
270
|
-
const codecType =
|
|
269
|
+
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
270
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
271
271
|
name = codecType.typeIn;
|
|
272
272
|
nameOut = codecType.typeOut;
|
|
273
273
|
knownType = true;
|
|
@@ -314,13 +314,13 @@ class TypesGen {
|
|
|
314
314
|
*/
|
|
315
315
|
#cleanPath(path) {
|
|
316
316
|
return path
|
|
317
|
-
.map((one) => (0,
|
|
317
|
+
.map((one) => (0, utils_1.stringPascalCase)(one))
|
|
318
318
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
319
319
|
.join('');
|
|
320
320
|
}
|
|
321
321
|
#shouldGenerateTypeIn(id) {
|
|
322
322
|
const { callTypeId } = this.metadata.extrinsic;
|
|
323
|
-
const palletCallTypeIds = this.registry.
|
|
323
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
324
324
|
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
325
325
|
}
|
|
326
326
|
eqlCache = new Map();
|
|
@@ -389,10 +389,10 @@ class TypesGen {
|
|
|
389
389
|
if (diffParam?.typeId) {
|
|
390
390
|
const diffType = types[diffParam.typeId];
|
|
391
391
|
if (diffType.path.length > 0) {
|
|
392
|
-
return (0,
|
|
392
|
+
return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
|
|
393
393
|
}
|
|
394
394
|
else if (diffType.type.tag === 'Primitive') {
|
|
395
|
-
return (0,
|
|
395
|
+
return (0, utils_1.stringPascalCase)(diffType.type.value.kind);
|
|
396
396
|
}
|
|
397
397
|
}
|
|
398
398
|
// Last resort!
|
|
@@ -403,7 +403,7 @@ class TypesGen {
|
|
|
403
403
|
typeName.forEach((one) => this.addTypeImport(one));
|
|
404
404
|
return;
|
|
405
405
|
}
|
|
406
|
-
if ((0,
|
|
406
|
+
if ((0, utils_js_1.isNativeType)(typeName)) {
|
|
407
407
|
return;
|
|
408
408
|
}
|
|
409
409
|
for (let type of Object.values(this.includedTypes)) {
|
|
@@ -425,7 +425,7 @@ class TypesGen {
|
|
|
425
425
|
this.typeImports.addCodecType(typeName);
|
|
426
426
|
return;
|
|
427
427
|
}
|
|
428
|
-
if (
|
|
428
|
+
if (known_types_js_1.knownTypes.includes(typeName)) {
|
|
429
429
|
this.typeImports.addKnownType(typeName);
|
|
430
430
|
}
|
|
431
431
|
else {
|
package/cjs/generator/index.js
CHANGED
|
@@ -14,13 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./TypesGen"), exports);
|
|
18
|
-
__exportStar(require("./ApiGen"), exports);
|
|
19
|
-
__exportStar(require("./ConstsGen"), exports);
|
|
20
|
-
__exportStar(require("./QueryGen"), exports);
|
|
21
|
-
__exportStar(require("./RpcGen"), exports);
|
|
22
|
-
__exportStar(require("./IndexGen"), exports);
|
|
23
|
-
__exportStar(require("./ErrorsGen"), exports);
|
|
24
|
-
__exportStar(require("./EventsGen"), exports);
|
|
25
|
-
__exportStar(require("./TxGen"), exports);
|
|
26
|
-
__exportStar(require("./RuntimeApisGen"), exports);
|
|
17
|
+
__exportStar(require("./TypesGen.js"), exports);
|
|
18
|
+
__exportStar(require("./ApiGen.js"), exports);
|
|
19
|
+
__exportStar(require("./ConstsGen.js"), exports);
|
|
20
|
+
__exportStar(require("./QueryGen.js"), exports);
|
|
21
|
+
__exportStar(require("./RpcGen.js"), exports);
|
|
22
|
+
__exportStar(require("./IndexGen.js"), exports);
|
|
23
|
+
__exportStar(require("./ErrorsGen.js"), exports);
|
|
24
|
+
__exportStar(require("./EventsGen.js"), exports);
|
|
25
|
+
__exportStar(require("./TxGen.js"), exports);
|
|
26
|
+
__exportStar(require("./RuntimeApisGen.js"), exports);
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
|
|
27
|
+
const $ = __importStar(require("@dedot/shape"));
|
|
28
|
+
const Codecs = __importStar(require("@dedot/codecs"));
|
|
29
|
+
const codecs_1 = require("@dedot/codecs");
|
|
30
|
+
const utils_1 = require("@dedot/utils");
|
|
31
|
+
const normalizeCodecName = (name) => {
|
|
32
|
+
return name.startsWith('$') ? name : `$${name}`;
|
|
33
|
+
};
|
|
34
|
+
exports.normalizeCodecName = normalizeCodecName;
|
|
35
|
+
// Known paths for codecs (primitives) that are shared between
|
|
36
|
+
// different substrate-based blockchains
|
|
37
|
+
const KNOWN_PATHS = [
|
|
38
|
+
'sp_core::crypto::AccountId32',
|
|
39
|
+
'sp_runtime::generic::era::Era',
|
|
40
|
+
'sp_runtime::multiaddress::MultiAddress',
|
|
41
|
+
/^sp_runtime::DispatchError$/,
|
|
42
|
+
'sp_runtime::ModuleError',
|
|
43
|
+
'sp_runtime::TokenError',
|
|
44
|
+
'sp_arithmetic::ArithmeticError',
|
|
45
|
+
'sp_runtime::TransactionalError',
|
|
46
|
+
'frame_support::dispatch::DispatchInfo',
|
|
47
|
+
'frame_system::Phase',
|
|
48
|
+
'sp_version::RuntimeVersion',
|
|
49
|
+
'fp_account::AccountId20',
|
|
50
|
+
'account::AccountId20',
|
|
51
|
+
'polkadot_runtime_common::claims::EthereumAddress',
|
|
52
|
+
'pallet_identity::types::Data',
|
|
53
|
+
'sp_runtime::generic::digest::Digest',
|
|
54
|
+
'sp_runtime::generic::digest::DigestItem',
|
|
55
|
+
'sp_runtime::generic::header::Header',
|
|
56
|
+
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
57
|
+
/^primitive_types::\w+$/,
|
|
58
|
+
/^sp_arithmetic::per_things::\w+$/,
|
|
59
|
+
/^sp_arithmetic::fixed_point::\w+$/,
|
|
60
|
+
];
|
|
61
|
+
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
62
|
+
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
63
|
+
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
64
|
+
/**
|
|
65
|
+
* Collection of codec types with loose input types
|
|
66
|
+
*
|
|
67
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
68
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
69
|
+
*
|
|
70
|
+
* This registry keep track the list of codecs which follow this convention
|
|
71
|
+
*/
|
|
72
|
+
exports.looseTypeCodecs = {
|
|
73
|
+
$AccountId20: codecs_1.$AccountId20,
|
|
74
|
+
$EthereumAddress: codecs_1.$EthereumAddress,
|
|
75
|
+
$AccountId32: codecs_1.$AccountId32,
|
|
76
|
+
$ConsensusEngineId: codecs_1.$ConsensusEngineId,
|
|
77
|
+
$StorageKey: codecs_1.$StorageKey,
|
|
78
|
+
$StorageData: codecs_1.$StorageData,
|
|
79
|
+
$PrefixedStorageKey: codecs_1.$PrefixedStorageKey,
|
|
80
|
+
$Bytes: codecs_1.$Bytes,
|
|
81
|
+
$RawBytes: codecs_1.$RawBytes,
|
|
82
|
+
$MultiAddress: codecs_1.$MultiAddress,
|
|
83
|
+
$OpaqueExtrinsic: codecs_1.$OpaqueExtrinsic,
|
|
84
|
+
$UncheckedExtrinsic: codecs_1.$UncheckedExtrinsic,
|
|
85
|
+
$Era: codecs_1.$Era,
|
|
86
|
+
};
|
|
87
|
+
function findKnownCodecType(name) {
|
|
88
|
+
const normalizedName = (0, exports.normalizeCodecName)(name);
|
|
89
|
+
const $knownCodec = exports.looseTypeCodecs[normalizedName];
|
|
90
|
+
if ($knownCodec) {
|
|
91
|
+
return {
|
|
92
|
+
name: normalizedName,
|
|
93
|
+
$codec: $knownCodec,
|
|
94
|
+
typeIn: `${name}Like`,
|
|
95
|
+
typeOut: name,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const $codec = findKnownCodec(name);
|
|
99
|
+
if ($codec.nativeType && $[name]) {
|
|
100
|
+
return {
|
|
101
|
+
name: normalizedName,
|
|
102
|
+
$codec,
|
|
103
|
+
typeIn: $codec.nativeType,
|
|
104
|
+
typeOut: $codec.nativeType,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
name: normalizedName,
|
|
109
|
+
$codec,
|
|
110
|
+
typeIn: name,
|
|
111
|
+
typeOut: name,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
exports.findKnownCodecType = findKnownCodecType;
|
|
115
|
+
function findKnownCodec(typeName) {
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] || $[typeName];
|
|
118
|
+
(0, utils_1.assert)($codec, `Known codec not found - ${typeName}`);
|
|
119
|
+
return $codec;
|
|
120
|
+
}
|
|
121
|
+
exports.findKnownCodec = findKnownCodec;
|
|
122
|
+
function findKnownWrapperCodec(typeName) {
|
|
123
|
+
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
124
|
+
if (matchNames) {
|
|
125
|
+
const [_, wrapper, inner] = matchNames;
|
|
126
|
+
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
const $Wrapper = $[wrapper];
|
|
129
|
+
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
130
|
+
return $Wrapper(findKnownWrapperCodec(inner));
|
|
131
|
+
}
|
|
132
|
+
const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
|
|
133
|
+
return $Wrapper(...$inners);
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
136
|
+
}
|
|
137
|
+
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
138
|
+
const $inner = typeName
|
|
139
|
+
.slice(1, -1)
|
|
140
|
+
.split(',')
|
|
141
|
+
.filter((x) => x)
|
|
142
|
+
.map((one) => findKnownCodec(one.trim()));
|
|
143
|
+
return $.Tuple(...$inner);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.findKnownWrapperCodec = findKnownWrapperCodec;
|
|
147
|
+
function isKnownCodecType(path) {
|
|
148
|
+
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
149
|
+
return KNOWN_PATHS.some((one) => joinedPath.match(one));
|
|
150
|
+
}
|
|
151
|
+
exports.isKnownCodecType = isKnownCodecType;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.knownTypes = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Known type names registered in @dedot/types
|
|
6
|
+
* Since TS interfaces are trim-off when compiling,
|
|
7
|
+
* So we need to register them explicitly here for RPC codegen process
|
|
8
|
+
*/
|
|
9
|
+
exports.knownTypes = [
|
|
10
|
+
'ExtrinsicOrHash',
|
|
11
|
+
'EpochAuthorship',
|
|
12
|
+
'BlockStats',
|
|
13
|
+
'Prevotes',
|
|
14
|
+
'Precommits',
|
|
15
|
+
'RoundState',
|
|
16
|
+
'ReportedRoundStates',
|
|
17
|
+
'JustificationNotification',
|
|
18
|
+
'EncodedFinalityProofs',
|
|
19
|
+
'LeavesProof',
|
|
20
|
+
'StorageKind',
|
|
21
|
+
'RpcMethods',
|
|
22
|
+
'ReadProof',
|
|
23
|
+
'StorageChangeSet',
|
|
24
|
+
'TraceBlockResponse',
|
|
25
|
+
'MigrationStatusResult',
|
|
26
|
+
'ChainType',
|
|
27
|
+
'ChainProperties',
|
|
28
|
+
'Health',
|
|
29
|
+
'SyncState',
|
|
30
|
+
'PeerInfo',
|
|
31
|
+
'NodeRole',
|
|
32
|
+
'NetworkState',
|
|
33
|
+
];
|
package/cjs/generator/utils.js
CHANGED
|
@@ -26,12 +26,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
26
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.
|
|
29
|
+
exports.isNativeType = exports.isReservedWord = exports.compileTemplate = exports.beautifySourceCode = exports.commentBlock = exports.TUPLE_TYPE_REGEX = exports.WRAPPER_TYPE_REGEX = void 0;
|
|
30
30
|
const fs = __importStar(require("fs"));
|
|
31
31
|
const handlebars_1 = __importDefault(require("handlebars"));
|
|
32
32
|
const path = __importStar(require("path"));
|
|
33
|
-
const process = __importStar(require("process"));
|
|
34
33
|
const prettier = __importStar(require("prettier"));
|
|
34
|
+
const dirname_1 = require("./dirname");
|
|
35
35
|
exports.WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
36
36
|
exports.TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
37
37
|
const commentBlock = (...docs) => {
|
|
@@ -48,18 +48,13 @@ ${flatLines.map((line) => `* ${line.replaceAll(/\s+/g, ' ').trim()}`).join('\n')
|
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
50
|
exports.commentBlock = commentBlock;
|
|
51
|
-
const resolveFilePath = (relativePath) => {
|
|
52
|
-
relativePath = Array.isArray(relativePath) ? relativePath : [relativePath];
|
|
53
|
-
return path.resolve(process.cwd(), ...relativePath);
|
|
54
|
-
};
|
|
55
|
-
exports.resolveFilePath = resolveFilePath;
|
|
56
51
|
const beautifySourceCode = async (source) => {
|
|
57
|
-
const prettierOptions = await prettier.resolveConfig((0,
|
|
52
|
+
const prettierOptions = await prettier.resolveConfig(path.resolve((0, dirname_1.currentDirname)(), '../../../../.prettierrc.js'));
|
|
58
53
|
return prettier.format(source, { parser: 'babel-ts', ...prettierOptions });
|
|
59
54
|
};
|
|
60
55
|
exports.beautifySourceCode = beautifySourceCode;
|
|
61
56
|
const compileTemplate = (templateFileName) => {
|
|
62
|
-
const templateFilePath = (0,
|
|
57
|
+
const templateFilePath = path.resolve((0, dirname_1.currentDirname)(), `../templates/${templateFileName}`);
|
|
63
58
|
return handlebars_1.default.compile(fs.readFileSync(templateFilePath, 'utf8'));
|
|
64
59
|
};
|
|
65
60
|
exports.compileTemplate = compileTemplate;
|
|
@@ -71,3 +66,32 @@ const TS_RESERVED_WORDS = ['new', 'class'];
|
|
|
71
66
|
*/
|
|
72
67
|
const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
|
|
73
68
|
exports.isReservedWord = isReservedWord;
|
|
69
|
+
const TS_PRIMITIVE_TYPES = [
|
|
70
|
+
'void',
|
|
71
|
+
'undefined',
|
|
72
|
+
'null',
|
|
73
|
+
'number',
|
|
74
|
+
'boolean',
|
|
75
|
+
'bigint',
|
|
76
|
+
'Map',
|
|
77
|
+
'Set',
|
|
78
|
+
'string',
|
|
79
|
+
'any',
|
|
80
|
+
'Array',
|
|
81
|
+
'Record',
|
|
82
|
+
];
|
|
83
|
+
/**
|
|
84
|
+
* Check if a type is native JS/TS type
|
|
85
|
+
* @param type
|
|
86
|
+
*/
|
|
87
|
+
const isNativeType = (type) => {
|
|
88
|
+
return TS_PRIMITIVE_TYPES.some((one) => {
|
|
89
|
+
if (typeof one === 'string') {
|
|
90
|
+
return one === type;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
return type.match(one);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
exports.isNativeType = isNativeType;
|
package/cjs/index.js
CHANGED
|
@@ -23,46 +23,46 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.generateTypes = exports.
|
|
26
|
+
exports.generateTypes = exports.generateTypesFromEndpoint = void 0;
|
|
27
27
|
const dedot_1 = require("dedot");
|
|
28
28
|
const fs = __importStar(require("fs"));
|
|
29
29
|
const path = __importStar(require("path"));
|
|
30
|
-
const
|
|
31
|
-
const
|
|
32
|
-
async function
|
|
33
|
-
const api = await dedot_1.Dedot.
|
|
30
|
+
const index_js_1 = require("./generator/index.js");
|
|
31
|
+
const utils_1 = require("@dedot/utils");
|
|
32
|
+
async function generateTypesFromEndpoint(chain, endpoint, outDir) {
|
|
33
|
+
const api = await dedot_1.Dedot.new(endpoint);
|
|
34
34
|
const { methods } = await api.rpc.rpc.methods();
|
|
35
35
|
const apis = api.runtimeVersion?.apis || [];
|
|
36
|
-
if (!
|
|
37
|
-
|
|
36
|
+
if (!chain) {
|
|
37
|
+
chain = (0, utils_1.stringCamelCase)(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
38
38
|
}
|
|
39
|
-
await generateTypes(
|
|
39
|
+
await generateTypes(chain, api.metadataLatest, methods, apis, outDir);
|
|
40
40
|
await api.disconnect();
|
|
41
41
|
}
|
|
42
|
-
exports.
|
|
43
|
-
async function generateTypes(
|
|
44
|
-
const dirPath = path.resolve(outDir,
|
|
45
|
-
const defTypesFileName = path.join(dirPath, `types.ts`);
|
|
46
|
-
const constsTypesFileName = path.join(dirPath, `consts.ts`);
|
|
47
|
-
const queryTypesFileName = path.join(dirPath, `query.ts`);
|
|
48
|
-
const rpcCallsFileName = path.join(dirPath, `rpc.ts`);
|
|
49
|
-
const indexFileName = path.join(dirPath, `index.ts`);
|
|
50
|
-
const errorsFileName = path.join(dirPath, `errors.ts`);
|
|
51
|
-
const eventsFileName = path.join(dirPath, `events.ts`);
|
|
52
|
-
const runtimeApisFileName = path.join(dirPath, `runtime.ts`);
|
|
53
|
-
const txFileName = path.join(dirPath, `tx.ts`);
|
|
42
|
+
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
43
|
+
async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.') {
|
|
44
|
+
const dirPath = path.resolve(outDir, chain);
|
|
45
|
+
const defTypesFileName = path.join(dirPath, `types.d.ts`);
|
|
46
|
+
const constsTypesFileName = path.join(dirPath, `consts.d.ts`);
|
|
47
|
+
const queryTypesFileName = path.join(dirPath, `query.d.ts`);
|
|
48
|
+
const rpcCallsFileName = path.join(dirPath, `rpc.d.ts`);
|
|
49
|
+
const indexFileName = path.join(dirPath, `index.d.ts`);
|
|
50
|
+
const errorsFileName = path.join(dirPath, `errors.d.ts`);
|
|
51
|
+
const eventsFileName = path.join(dirPath, `events.d.ts`);
|
|
52
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.d.ts`);
|
|
53
|
+
const txFileName = path.join(dirPath, `tx.d.ts`);
|
|
54
54
|
if (!fs.existsSync(dirPath)) {
|
|
55
55
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
56
56
|
}
|
|
57
|
-
const typesGen = new
|
|
58
|
-
const constsGen = new
|
|
59
|
-
const queryGen = new
|
|
60
|
-
const rpcGen = new
|
|
61
|
-
const indexGen = new
|
|
62
|
-
const errorsGen = new
|
|
63
|
-
const eventsGen = new
|
|
64
|
-
const runtimeApisGen = new
|
|
65
|
-
const txGen = new
|
|
57
|
+
const typesGen = new index_js_1.TypesGen(metadata);
|
|
58
|
+
const constsGen = new index_js_1.ConstsGen(typesGen);
|
|
59
|
+
const queryGen = new index_js_1.QueryGen(typesGen);
|
|
60
|
+
const rpcGen = new index_js_1.RpcGen(typesGen, rpcMethods);
|
|
61
|
+
const indexGen = new index_js_1.IndexGen(chain);
|
|
62
|
+
const errorsGen = new index_js_1.ErrorsGen(typesGen);
|
|
63
|
+
const eventsGen = new index_js_1.EventsGen(typesGen);
|
|
64
|
+
const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
|
|
65
|
+
const txGen = new index_js_1.TxGen(typesGen);
|
|
66
66
|
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
67
67
|
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
68
68
|
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Generated by @dedot/codegen
|
|
2
|
+
|
|
3
|
+
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
|
+
import { ChainConsts } from './consts';
|
|
5
|
+
import { ChainStorage } from './query';
|
|
6
|
+
import { RpcCalls } from './rpc';
|
|
7
|
+
import { ChainErrors } from './errors';
|
|
8
|
+
import { ChainEvents } from './events';
|
|
9
|
+
import { RuntimeApis } from './runtime';
|
|
10
|
+
import { ChainTx } from './tx';
|
|
11
|
+
|
|
12
|
+
export * from './types';
|
|
13
|
+
|
|
14
|
+
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
+
rpc: RpcCalls;
|
|
16
|
+
consts: ChainConsts;
|
|
17
|
+
query: ChainStorage;
|
|
18
|
+
errors: ChainErrors;
|
|
19
|
+
events: ChainEvents;
|
|
20
|
+
call: RuntimeApis;
|
|
21
|
+
tx: ChainTx;
|
|
22
|
+
}
|
package/generator/ApiGen.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypesGen } from '../generator';
|
|
1
|
+
import { TypesGen } from '../generator/index.js';
|
|
2
2
|
export declare abstract class ApiGen {
|
|
3
3
|
readonly typesGen: TypesGen;
|
|
4
4
|
constructor(typesGen: TypesGen);
|
|
@@ -44,7 +44,7 @@ export declare abstract class ApiGen {
|
|
|
44
44
|
} | {
|
|
45
45
|
tag: "Primitive";
|
|
46
46
|
value: {
|
|
47
|
-
kind: "bool" | "
|
|
47
|
+
kind: "bool" | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "u256" | "i256" | "str" | "char";
|
|
48
48
|
};
|
|
49
49
|
} | {
|
|
50
50
|
tag: "Compact";
|
|
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
|
|
|
134
134
|
}>;
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
|
-
get registry(): import("@dedot/codecs").
|
|
137
|
+
get registry(): import("@dedot/codecs").PortableRegistry;
|
|
138
138
|
}
|
package/generator/ConstsGen.d.ts
CHANGED