@dedot/codegen 0.0.1-next.c26d3c25.42 → 0.0.1-next.cb041b89.17
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/{generator → chaintypes/generator}/ApiGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/ConstsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/ConstsGen.js +4 -4
- package/{generator → chaintypes/generator}/ErrorsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/ErrorsGen.js +4 -4
- package/{generator → chaintypes/generator}/EventsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/EventsGen.js +4 -4
- package/{generator → chaintypes/generator}/IndexGen.d.ts +1 -1
- package/chaintypes/generator/IndexGen.js +17 -0
- package/{generator → chaintypes/generator}/JsonRpcGen.d.ts +3 -2
- package/{generator → chaintypes/generator}/JsonRpcGen.js +7 -7
- package/{generator → chaintypes/generator}/QueryGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/QueryGen.js +4 -4
- package/{generator → chaintypes/generator}/RuntimeApisGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/RuntimeApisGen.js +5 -5
- package/{generator → chaintypes/generator}/TxGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/TxGen.js +4 -4
- package/chaintypes/generator/TypesGen.d.ts +11 -0
- package/chaintypes/generator/TypesGen.js +55 -0
- package/chaintypes/index.d.ts +3 -0
- package/chaintypes/index.js +49 -0
- package/{cjs → chaintypes}/templates/consts.hbs +1 -1
- package/{templates → chaintypes/templates}/errors.hbs +1 -1
- package/{templates → chaintypes/templates}/events.hbs +1 -1
- package/{templates → chaintypes/templates}/index.hbs +2 -2
- package/{templates → chaintypes/templates}/json-rpc.hbs +1 -1
- package/{templates → chaintypes/templates}/query.hbs +1 -1
- package/{templates → chaintypes/templates}/runtime.hbs +1 -1
- package/{cjs → chaintypes}/templates/tx.hbs +1 -1
- package/{cjs → chaintypes}/templates/types.hbs +1 -1
- package/cjs/{generator → chaintypes/generator}/ConstsGen.js +4 -4
- package/cjs/{generator → chaintypes/generator}/ErrorsGen.js +4 -4
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +4 -4
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/{generator → chaintypes/generator}/JsonRpcGen.js +9 -9
- package/cjs/{generator → chaintypes/generator}/QueryGen.js +4 -4
- package/cjs/{generator → chaintypes/generator}/RuntimeApisGen.js +6 -6
- package/cjs/{generator → chaintypes/generator}/TxGen.js +4 -4
- package/cjs/chaintypes/generator/TypesGen.js +59 -0
- package/cjs/chaintypes/index.js +77 -0
- package/{templates → cjs/chaintypes/templates}/consts.hbs +1 -1
- package/cjs/{templates → chaintypes/templates}/errors.hbs +1 -1
- package/cjs/{templates → chaintypes/templates}/events.hbs +1 -1
- package/cjs/{templates → chaintypes/templates}/index.hbs +2 -2
- package/cjs/{templates → chaintypes/templates}/json-rpc.hbs +1 -1
- package/cjs/{templates → chaintypes/templates}/query.hbs +1 -1
- package/cjs/{templates → chaintypes/templates}/runtime.hbs +1 -1
- package/{templates → cjs/chaintypes/templates}/tx.hbs +1 -1
- package/{templates → cjs/chaintypes/templates}/types.hbs +1 -1
- package/cjs/index.js +4 -62
- package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +88 -140
- package/cjs/{generator → shared}/TypeImports.js +27 -9
- package/cjs/shared/index.js +19 -0
- package/cjs/{generator → shared}/known-codecs.js +23 -4
- package/cjs/typink/generator/ConstructorQueryGen.js +24 -0
- package/cjs/typink/generator/ConstructorTxGen.js +24 -0
- package/cjs/typink/generator/IndexGen.js +24 -0
- package/cjs/typink/generator/QueryGen.js +72 -0
- package/cjs/typink/generator/TxGen.js +25 -0
- package/cjs/typink/generator/TypesGen.js +35 -0
- package/cjs/typink/generator/index.js +22 -0
- package/cjs/typink/index.js +37 -0
- package/cjs/typink/templates/constructor-query.hbs +7 -0
- package/cjs/typink/templates/constructor-tx.hbs +7 -0
- package/cjs/typink/templates/index.hbs +14 -0
- package/cjs/typink/templates/query.hbs +7 -0
- package/cjs/typink/templates/tx.hbs +7 -0
- package/cjs/typink/templates/types.hbs +5 -0
- package/cjs/{generator/utils.js → utils.js} +3 -3
- package/index.d.ts +2 -3
- package/index.js +2 -48
- package/package.json +10 -8
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +12 -11
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +85 -137
- package/{generator → shared}/TypeImports.d.ts +12 -3
- package/{generator → shared}/TypeImports.js +27 -9
- package/shared/index.d.ts +3 -0
- package/shared/index.js +3 -0
- package/{generator → shared}/known-codecs.d.ts +1 -1
- package/{generator → shared}/known-codecs.js +21 -2
- package/typink/generator/ConstructorQueryGen.d.ts +6 -0
- package/typink/generator/ConstructorQueryGen.js +20 -0
- package/typink/generator/ConstructorTxGen.d.ts +6 -0
- package/typink/generator/ConstructorTxGen.js +20 -0
- package/typink/generator/IndexGen.d.ts +6 -0
- package/typink/generator/IndexGen.js +20 -0
- package/typink/generator/QueryGen.d.ts +12 -0
- package/typink/generator/QueryGen.js +68 -0
- package/typink/generator/TxGen.d.ts +6 -0
- package/typink/generator/TxGen.js +21 -0
- package/typink/generator/TypesGen.d.ts +8 -0
- package/typink/generator/TypesGen.js +31 -0
- package/typink/generator/index.d.ts +6 -0
- package/typink/generator/index.js +6 -0
- package/typink/index.d.ts +2 -0
- package/typink/index.js +30 -0
- package/typink/templates/constructor-query.hbs +7 -0
- package/typink/templates/constructor-tx.hbs +7 -0
- package/typink/templates/index.hbs +14 -0
- package/typink/templates/query.hbs +7 -0
- package/typink/templates/tx.hbs +7 -0
- package/typink/templates/types.hbs +5 -0
- package/{generator/utils.d.ts → utils.d.ts} +1 -1
- package/{generator/utils.js → utils.js} +3 -3
- package/cjs/generator/IndexGen.js +0 -17
- package/generator/IndexGen.js +0 -13
- /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/{generator → chaintypes/generator}/index.d.ts +0 -0
- /package/{generator → chaintypes/generator}/index.js +0 -0
- /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/cjs/{generator → chaintypes/generator}/index.js +0 -0
- /package/cjs/{generator/dirname.js → dirname.js} +0 -0
- /package/{generator/dirname.d.ts → dirname.d.ts} +0 -0
- /package/{generator/dirname.js → dirname.js} +0 -0
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RuntimeApisGen = void 0;
|
|
4
4
|
const runtime_specs_1 = require("@dedot/runtime-specs");
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
|
+
const index_js_1 = require("../../shared/index.js");
|
|
7
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
8
|
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
-
const known_codecs_js_1 = require("./known-codecs.js");
|
|
8
|
-
const utils_js_1 = require("./utils.js");
|
|
9
9
|
class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
10
10
|
typesGen;
|
|
11
11
|
runtimeApis;
|
|
@@ -14,7 +14,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
14
14
|
this.typesGen = typesGen;
|
|
15
15
|
this.runtimeApis = runtimeApis;
|
|
16
16
|
}
|
|
17
|
-
generate() {
|
|
17
|
+
generate(useSubPaths = false) {
|
|
18
18
|
this.typesGen.clearCache();
|
|
19
19
|
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
|
|
20
20
|
let runtimeCallsOut = '';
|
|
@@ -46,8 +46,8 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
46
46
|
}`;
|
|
47
47
|
});
|
|
48
48
|
}
|
|
49
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
-
const template = (0, utils_js_1.compileTemplate)('runtime.hbs');
|
|
49
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
50
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/runtime.hbs');
|
|
51
51
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, runtimeCallsOut }));
|
|
52
52
|
}
|
|
53
53
|
#isOptionalType(type) {
|
|
@@ -186,7 +186,7 @@ class RuntimeApisGen extends ApiGen_js_1.ApiGen {
|
|
|
186
186
|
return type;
|
|
187
187
|
}
|
|
188
188
|
#getCodecType(type, toTypeIn = true) {
|
|
189
|
-
const { typeIn, typeOut } = (0,
|
|
189
|
+
const { typeIn, typeOut } = (0, index_js_1.findKnownCodecType)(type);
|
|
190
190
|
return toTypeIn ? typeIn : typeOut;
|
|
191
191
|
}
|
|
192
192
|
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TxGen = void 0;
|
|
4
4
|
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
5
6
|
const index_js_1 = require("../generator/index.js");
|
|
6
|
-
const utils_js_1 = require("./utils.js");
|
|
7
7
|
class TxGen extends index_js_1.ApiGen {
|
|
8
|
-
generate() {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
9
9
|
const { pallets, types } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
11
11
|
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionStatusLegacy', 'TransactionStatusV2');
|
|
@@ -50,7 +50,7 @@ class TxGen extends index_js_1.ApiGen {
|
|
|
50
50
|
${(0, utils_js_1.commentBlock)('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
|
|
51
51
|
},`;
|
|
52
52
|
}
|
|
53
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
53
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
54
54
|
// TODO make explicit separate type for Extra
|
|
55
55
|
const defTypes = `
|
|
56
56
|
export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
@@ -61,7 +61,7 @@ class TxGen extends index_js_1.ApiGen {
|
|
|
61
61
|
|
|
62
62
|
export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
|
|
63
63
|
`;
|
|
64
|
-
const template = (0, utils_js_1.compileTemplate)('tx.hbs');
|
|
64
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/tx.hbs');
|
|
65
65
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypes, txDefsOut }));
|
|
66
66
|
}
|
|
67
67
|
#normalizeParamName(name) {
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypesGen = void 0;
|
|
4
|
+
const codecs_1 = require("@dedot/codecs");
|
|
5
|
+
const index_js_1 = require("../../shared/index.js");
|
|
6
|
+
const utils_js_1 = require("../../utils.js");
|
|
7
|
+
// Skip generate types for these
|
|
8
|
+
// as we do have native types for them
|
|
9
|
+
const SKIP_TYPES = [
|
|
10
|
+
'BoundedBTreeMap',
|
|
11
|
+
'BoundedBTreeSet',
|
|
12
|
+
'BoundedVec',
|
|
13
|
+
'Box',
|
|
14
|
+
'BTreeMap',
|
|
15
|
+
'BTreeSet',
|
|
16
|
+
'Cow',
|
|
17
|
+
'Option',
|
|
18
|
+
'Range',
|
|
19
|
+
'RangeInclusive',
|
|
20
|
+
'Result',
|
|
21
|
+
'WeakBoundedVec',
|
|
22
|
+
'WrapperKeepOpaque',
|
|
23
|
+
'WrapperOpaque',
|
|
24
|
+
];
|
|
25
|
+
class TypesGen extends index_js_1.BaseTypesGen {
|
|
26
|
+
metadata;
|
|
27
|
+
registry;
|
|
28
|
+
constructor(metadata) {
|
|
29
|
+
super(metadata.types);
|
|
30
|
+
this.metadata = metadata;
|
|
31
|
+
this.skipTypes = SKIP_TYPES;
|
|
32
|
+
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
33
|
+
this.includedTypes = this.includeTypes();
|
|
34
|
+
}
|
|
35
|
+
generate(useSubPaths = false) {
|
|
36
|
+
this.clearCache();
|
|
37
|
+
let defTypeOut = '';
|
|
38
|
+
Object.values(this.includedTypes)
|
|
39
|
+
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
40
|
+
.forEach(({ name, nameOut, id, docs }) => {
|
|
41
|
+
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
42
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
43
|
+
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
const importTypes = this.typeImports.toImports({ excludeModules: ['./types'], useSubPaths });
|
|
47
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/types.hbs');
|
|
48
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
49
|
+
}
|
|
50
|
+
shouldGenerateTypeIn(id) {
|
|
51
|
+
const { callTypeId } = this.metadata.extrinsic;
|
|
52
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
53
|
+
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
54
|
+
}
|
|
55
|
+
getEnumOptions(typeId) {
|
|
56
|
+
return this.registry.getEnumOptions(typeId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.TypesGen = TypesGen;
|
|
@@ -0,0 +1,77 @@
|
|
|
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.generateTypes = exports.generateTypesFromEndpoint = void 0;
|
|
27
|
+
const api_1 = require("@dedot/api");
|
|
28
|
+
const providers_1 = require("@dedot/providers");
|
|
29
|
+
const utils_1 = require("@dedot/utils");
|
|
30
|
+
const fs = __importStar(require("fs"));
|
|
31
|
+
const path = __importStar(require("path"));
|
|
32
|
+
const index_js_1 = require("./generator/index.js");
|
|
33
|
+
async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
|
|
34
|
+
const api = await api_1.LegacyClient.new(new providers_1.WsProvider(endpoint));
|
|
35
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
36
|
+
const apis = api.runtimeVersion.apis || {};
|
|
37
|
+
if (!chain) {
|
|
38
|
+
chain = (0, utils_1.stringCamelCase)(api.runtimeVersion.specName || 'local');
|
|
39
|
+
}
|
|
40
|
+
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension, useSubPaths);
|
|
41
|
+
await api.disconnect();
|
|
42
|
+
}
|
|
43
|
+
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
44
|
+
async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts', useSubPaths = false) {
|
|
45
|
+
const dirPath = path.resolve(outDir, chain);
|
|
46
|
+
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
47
|
+
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
48
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
49
|
+
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
50
|
+
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
51
|
+
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
52
|
+
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
53
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
54
|
+
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
55
|
+
if (!fs.existsSync(dirPath)) {
|
|
56
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
57
|
+
}
|
|
58
|
+
const typesGen = new index_js_1.TypesGen(metadata);
|
|
59
|
+
const constsGen = new index_js_1.ConstsGen(typesGen);
|
|
60
|
+
const queryGen = new index_js_1.QueryGen(typesGen);
|
|
61
|
+
const jsonRpcGen = new index_js_1.JsonRpcGen(typesGen, rpcMethods);
|
|
62
|
+
const indexGen = new index_js_1.IndexGen(chain);
|
|
63
|
+
const errorsGen = new index_js_1.ErrorsGen(typesGen);
|
|
64
|
+
const eventsGen = new index_js_1.EventsGen(typesGen);
|
|
65
|
+
const runtimeApisGen = new index_js_1.RuntimeApisGen(typesGen, runtimeApis);
|
|
66
|
+
const txGen = new index_js_1.TxGen(typesGen);
|
|
67
|
+
fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
|
|
68
|
+
fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
|
|
69
|
+
fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
|
|
70
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate(useSubPaths));
|
|
71
|
+
fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
|
|
72
|
+
fs.writeFileSync(constsTypesFileName, await constsGen.generate(useSubPaths));
|
|
73
|
+
fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
|
|
74
|
+
fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
|
|
75
|
+
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
|
|
76
|
+
}
|
|
77
|
+
exports.generateTypes = generateTypes;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
// Generated by
|
|
1
|
+
// Generated by dedot cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
{{{importTypes}}}
|
|
4
4
|
import { ChainConsts } from './consts';
|
|
5
5
|
import { ChainStorage } from './query';
|
|
6
6
|
import { ChainJsonRpcApis } from './json-rpc';
|
package/cjs/index.js
CHANGED
|
@@ -10,67 +10,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
-
var
|
|
14
|
-
|
|
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;
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
24
15
|
};
|
|
25
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const dedot_1 = require("dedot");
|
|
29
|
-
const fs = __importStar(require("fs"));
|
|
30
|
-
const path = __importStar(require("path"));
|
|
31
|
-
const index_js_1 = require("./generator/index.js");
|
|
32
|
-
async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
|
|
33
|
-
const api = await dedot_1.Dedot.new(new dedot_1.WsProvider(endpoint));
|
|
34
|
-
const { methods } = await api.rpc.rpc_methods();
|
|
35
|
-
const apis = api.runtimeVersion.apis || {};
|
|
36
|
-
if (!chain) {
|
|
37
|
-
chain = (0, utils_1.stringCamelCase)(api.runtimeVersion.specName || 'local');
|
|
38
|
-
}
|
|
39
|
-
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
|
|
40
|
-
await api.disconnect();
|
|
41
|
-
}
|
|
42
|
-
exports.generateTypesFromEndpoint = generateTypesFromEndpoint;
|
|
43
|
-
async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
|
|
44
|
-
const dirPath = path.resolve(outDir, chain);
|
|
45
|
-
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
46
|
-
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
47
|
-
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
48
|
-
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
49
|
-
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
50
|
-
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
51
|
-
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
52
|
-
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
53
|
-
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
54
|
-
if (!fs.existsSync(dirPath)) {
|
|
55
|
-
fs.mkdirSync(dirPath, { recursive: true });
|
|
56
|
-
}
|
|
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 jsonRpcGen = new index_js_1.JsonRpcGen(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
|
-
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
67
|
-
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
68
|
-
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
69
|
-
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
|
|
70
|
-
fs.writeFileSync(queryTypesFileName, await queryGen.generate());
|
|
71
|
-
fs.writeFileSync(constsTypesFileName, await constsGen.generate());
|
|
72
|
-
fs.writeFileSync(txFileName, await txGen.generate());
|
|
73
|
-
fs.writeFileSync(indexFileName, await indexGen.generate());
|
|
74
|
-
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate());
|
|
75
|
-
}
|
|
76
|
-
exports.generateTypes = generateTypes;
|
|
17
|
+
__exportStar(require("./chaintypes/index.js"), exports);
|
|
18
|
+
__exportStar(require("./typink/index.js"), exports);
|
|
@@ -1,68 +1,93 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const codecs_1 = require("@dedot/codecs");
|
|
3
|
+
exports.BaseTypesGen = exports.BASIC_KNOWN_TYPES = void 0;
|
|
5
4
|
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../utils.js");
|
|
6
6
|
const TypeImports_js_1 = require("./TypeImports.js");
|
|
7
7
|
const known_codecs_js_1 = require("./known-codecs.js");
|
|
8
|
-
const utils_js_1 = require("./utils.js");
|
|
9
|
-
// Skip generate types for these
|
|
10
|
-
// as we do have native types for them
|
|
11
|
-
const SKIP_TYPES = [
|
|
12
|
-
'BoundedBTreeMap',
|
|
13
|
-
'BoundedBTreeSet',
|
|
14
|
-
'BoundedVec',
|
|
15
|
-
'Box',
|
|
16
|
-
'BTreeMap',
|
|
17
|
-
'BTreeSet',
|
|
18
|
-
'Cow',
|
|
19
|
-
'Option',
|
|
20
|
-
'Range',
|
|
21
|
-
'RangeInclusive',
|
|
22
|
-
'Result',
|
|
23
|
-
'WeakBoundedVec',
|
|
24
|
-
'WrapperKeepOpaque',
|
|
25
|
-
'WrapperOpaque',
|
|
26
|
-
];
|
|
27
8
|
// These are common & generic types, so we'll remove these from all paths at index 1
|
|
28
9
|
// This helps make the type name shorter
|
|
29
10
|
const PATH_RM_INDEX_1 = ['generic', 'misc', 'pallet', 'traits', 'types'];
|
|
30
11
|
exports.BASIC_KNOWN_TYPES = ['BitSequence', 'Bytes', 'BytesLike', 'FixedBytes', 'FixedArray', 'Result'];
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
metadata;
|
|
34
|
-
/**
|
|
35
|
-
* Types will be generated its definition out.
|
|
36
|
-
*/
|
|
12
|
+
class BaseTypesGen {
|
|
13
|
+
types;
|
|
37
14
|
includedTypes;
|
|
38
|
-
registry;
|
|
39
15
|
typeImports;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.
|
|
43
|
-
this.includedTypes =
|
|
16
|
+
skipTypes;
|
|
17
|
+
constructor(types) {
|
|
18
|
+
this.types = types;
|
|
19
|
+
this.includedTypes = {};
|
|
44
20
|
this.typeImports = new TypeImports_js_1.TypeImports();
|
|
45
|
-
|
|
46
|
-
generate() {
|
|
47
|
-
this.clearCache();
|
|
48
|
-
let defTypeOut = '';
|
|
49
|
-
Object.values(this.includedTypes)
|
|
50
|
-
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
51
|
-
.forEach(({ name, nameOut, id, docs }) => {
|
|
52
|
-
defTypeOut += `${(0, utils_js_1.commentBlock)(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
53
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
54
|
-
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
const importTypes = this.typeImports.toImports('./types');
|
|
58
|
-
const template = (0, utils_js_1.compileTemplate)('types.hbs');
|
|
59
|
-
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
21
|
+
this.skipTypes = [];
|
|
60
22
|
}
|
|
61
23
|
typeCache = {};
|
|
62
24
|
clearCache() {
|
|
63
25
|
this.typeCache = {};
|
|
64
26
|
this.typeImports.clear();
|
|
65
27
|
}
|
|
28
|
+
includeTypes() {
|
|
29
|
+
const pathsCount = new Map();
|
|
30
|
+
const typesWithPath = this.types.filter((one) => one.path.length > 0);
|
|
31
|
+
const skipIds = [];
|
|
32
|
+
const typeSuffixes = new Map();
|
|
33
|
+
typesWithPath.forEach(({ path, id }) => {
|
|
34
|
+
const joinedPath = path.join('::');
|
|
35
|
+
if (pathsCount.has(joinedPath)) {
|
|
36
|
+
// We compare 2 types with the same path here,
|
|
37
|
+
// if they are the same type -> skip the current one, keep the first occurrence
|
|
38
|
+
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
39
|
+
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
40
|
+
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
41
|
+
if (sameType) {
|
|
42
|
+
skipIds.push(id);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
pathsCount.get(joinedPath).push(id);
|
|
46
|
+
typeSuffixes.set(id, this.extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
pathsCount.set(joinedPath, [id]);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
return typesWithPath.reduce((o, type) => {
|
|
54
|
+
const { path, id } = type;
|
|
55
|
+
const joinedPath = path.join('::');
|
|
56
|
+
if (this.skipTypes.includes(joinedPath) || this.skipTypes.includes(path.at(-1))) {
|
|
57
|
+
return o;
|
|
58
|
+
}
|
|
59
|
+
const suffix = typeSuffixes.get(id) || '';
|
|
60
|
+
let knownType = false;
|
|
61
|
+
let name, nameOut;
|
|
62
|
+
const [isKnownCodecType, codecName] = (0, known_codecs_js_1.checkKnownCodecType)(joinedPath);
|
|
63
|
+
if (isKnownCodecType) {
|
|
64
|
+
const codecType = (0, known_codecs_js_1.findKnownCodecType)(codecName);
|
|
65
|
+
name = codecType.typeIn;
|
|
66
|
+
nameOut = codecType.typeOut;
|
|
67
|
+
knownType = true;
|
|
68
|
+
}
|
|
69
|
+
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
70
|
+
const newPath = path.slice();
|
|
71
|
+
newPath.splice(1, 1);
|
|
72
|
+
name = this.cleanPath(newPath);
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
name = this.cleanPath(path);
|
|
76
|
+
}
|
|
77
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
78
|
+
nameOut = name;
|
|
79
|
+
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
80
|
+
}
|
|
81
|
+
o[id] = {
|
|
82
|
+
name: `${name}${suffix}`,
|
|
83
|
+
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
84
|
+
knownType,
|
|
85
|
+
skip: skipIds.includes(id),
|
|
86
|
+
...type,
|
|
87
|
+
};
|
|
88
|
+
return o;
|
|
89
|
+
}, {});
|
|
90
|
+
}
|
|
66
91
|
generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
67
92
|
if (nestedLevel > 0) {
|
|
68
93
|
const includedDef = this.includedTypes[typeId];
|
|
@@ -93,7 +118,7 @@ class TypesGen {
|
|
|
93
118
|
return type;
|
|
94
119
|
}
|
|
95
120
|
#generateType(typeId, nestedLevel = 0, typeOut = false) {
|
|
96
|
-
const def = this.
|
|
121
|
+
const def = this.types[typeId];
|
|
97
122
|
if (!def) {
|
|
98
123
|
throw new Error(`Type def not found ${JSON.stringify(def)}`);
|
|
99
124
|
}
|
|
@@ -170,7 +195,7 @@ class TypesGen {
|
|
|
170
195
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
171
196
|
}
|
|
172
197
|
}
|
|
173
|
-
const { tagKey, valueKey } = this.
|
|
198
|
+
const { tagKey, valueKey } = this.getEnumOptions(typeId);
|
|
174
199
|
return membersType
|
|
175
200
|
.map(([keyName, valueType, docs]) => ({
|
|
176
201
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -200,7 +225,7 @@ class TypesGen {
|
|
|
200
225
|
case 'Sequence':
|
|
201
226
|
case 'SizedVec': {
|
|
202
227
|
const fixedSize = tag === 'SizedVec' ? `${value.len}` : null;
|
|
203
|
-
const $innerType = this.
|
|
228
|
+
const $innerType = this.types[value.typeParam].type;
|
|
204
229
|
if ($innerType.tag === 'Primitive' && $innerType.value.kind === 'u8') {
|
|
205
230
|
return fixedSize ? `FixedBytes<${fixedSize}>` : typeOut ? 'Bytes' : 'BytesLike';
|
|
206
231
|
}
|
|
@@ -230,98 +255,20 @@ class TypesGen {
|
|
|
230
255
|
#isOptionalType(type) {
|
|
231
256
|
return type.endsWith('| undefined');
|
|
232
257
|
}
|
|
233
|
-
#includedTypes() {
|
|
234
|
-
const { types } = this.metadata;
|
|
235
|
-
const pathsCount = new Map();
|
|
236
|
-
const typesWithPath = types.filter((one) => one.path.length > 0);
|
|
237
|
-
const skipIds = [];
|
|
238
|
-
const typeSuffixes = new Map();
|
|
239
|
-
typesWithPath.forEach(({ path, id }) => {
|
|
240
|
-
const joinedPath = path.join('::');
|
|
241
|
-
if (pathsCount.has(joinedPath)) {
|
|
242
|
-
// We compare 2 types with the same path here,
|
|
243
|
-
// if they are the same type -> skip the current one, keep the first occurrence
|
|
244
|
-
// if they are not the same type but has the same path -> we'll try to calculate & add a suffix for the current type name
|
|
245
|
-
const firstOccurrenceTypeId = pathsCount.get(joinedPath)[0];
|
|
246
|
-
const sameType = this.typeEql(firstOccurrenceTypeId, id);
|
|
247
|
-
if (sameType) {
|
|
248
|
-
skipIds.push(id);
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
pathsCount.get(joinedPath).push(id);
|
|
252
|
-
typeSuffixes.set(id, this.#extractDupTypeSuffix(id, firstOccurrenceTypeId, pathsCount.get(joinedPath).length));
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
else {
|
|
256
|
-
pathsCount.set(joinedPath, [id]);
|
|
257
|
-
}
|
|
258
|
-
});
|
|
259
|
-
return typesWithPath.reduce((o, type) => {
|
|
260
|
-
const { path, id } = type;
|
|
261
|
-
const joinedPath = path.join('::');
|
|
262
|
-
if (SKIP_TYPES.includes(joinedPath) || SKIP_TYPES.includes(path.at(-1))) {
|
|
263
|
-
return o;
|
|
264
|
-
}
|
|
265
|
-
const suffix = typeSuffixes.get(id) || '';
|
|
266
|
-
let knownType = false;
|
|
267
|
-
let name, nameOut;
|
|
268
|
-
if ((0, known_codecs_js_1.isKnownCodecType)(joinedPath)) {
|
|
269
|
-
const codecType = (0, known_codecs_js_1.findKnownCodecType)(path.at(-1));
|
|
270
|
-
name = codecType.typeIn;
|
|
271
|
-
nameOut = codecType.typeOut;
|
|
272
|
-
knownType = true;
|
|
273
|
-
}
|
|
274
|
-
else if (PATH_RM_INDEX_1.includes(path[1])) {
|
|
275
|
-
const newPath = path.slice();
|
|
276
|
-
newPath.splice(1, 1);
|
|
277
|
-
name = this.#cleanPath(newPath);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
name = this.#cleanPath(path);
|
|
281
|
-
}
|
|
282
|
-
if (this.#shouldGenerateTypeIn(id)) {
|
|
283
|
-
nameOut = name;
|
|
284
|
-
name = name.endsWith('Like') ? name : `${name}Like`;
|
|
285
|
-
}
|
|
286
|
-
o[id] = {
|
|
287
|
-
name: `${name}${suffix}`,
|
|
288
|
-
nameOut: nameOut ? `${nameOut}${suffix}` : `${name}${suffix}`,
|
|
289
|
-
knownType,
|
|
290
|
-
skip: skipIds.includes(id),
|
|
291
|
-
...type,
|
|
292
|
-
};
|
|
293
|
-
return o;
|
|
294
|
-
}, {});
|
|
295
|
-
}
|
|
296
258
|
#removeGenericPart(typeName) {
|
|
297
|
-
if (typeName.match(WRAPPER_TYPE_REGEX)) {
|
|
298
|
-
return typeName.replace(WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
259
|
+
if (typeName.match(utils_js_1.WRAPPER_TYPE_REGEX)) {
|
|
260
|
+
return typeName.replace(utils_js_1.WRAPPER_TYPE_REGEX, (_, $1) => $1);
|
|
299
261
|
}
|
|
300
262
|
else {
|
|
301
263
|
return typeName;
|
|
302
264
|
}
|
|
303
265
|
}
|
|
304
|
-
|
|
305
|
-
* @description Remove duplicated part of the path
|
|
306
|
-
*
|
|
307
|
-
* Example:
|
|
308
|
-
* ["pallet_staking", "pallet", "pallet", "Event"]
|
|
309
|
-
* => ["pallet_staking", "pallet", "Event"]
|
|
310
|
-
*
|
|
311
|
-
* @param path
|
|
312
|
-
* @private
|
|
313
|
-
*/
|
|
314
|
-
#cleanPath(path) {
|
|
266
|
+
cleanPath(path) {
|
|
315
267
|
return path
|
|
316
268
|
.map((one) => (0, utils_1.stringPascalCase)(one))
|
|
317
269
|
.filter((one, idx, currentPath) => idx === 0 || one !== currentPath[idx - 1])
|
|
318
270
|
.join('');
|
|
319
271
|
}
|
|
320
|
-
#shouldGenerateTypeIn(id) {
|
|
321
|
-
const { callTypeId } = this.metadata.extrinsic;
|
|
322
|
-
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
323
|
-
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
324
|
-
}
|
|
325
272
|
eqlCache = new Map();
|
|
326
273
|
typeEql(idA, idB, level = 0) {
|
|
327
274
|
const cacheKey = `${idA}==${idB}`;
|
|
@@ -334,9 +281,8 @@ class TypesGen {
|
|
|
334
281
|
#typeEql(idA, idB, lvl = 0) {
|
|
335
282
|
if (idA === idB)
|
|
336
283
|
return true;
|
|
337
|
-
const
|
|
338
|
-
const
|
|
339
|
-
const typeB = types[idB];
|
|
284
|
+
const typeA = this.types[idA];
|
|
285
|
+
const typeB = this.types[idB];
|
|
340
286
|
if (typeA.path.join('::') !== typeB.path.join('::'))
|
|
341
287
|
return false;
|
|
342
288
|
const { type: defA, params: paramsA } = typeA;
|
|
@@ -379,14 +325,13 @@ class TypesGen {
|
|
|
379
325
|
(param1.typeId === undefined) === (param2.typeId === undefined) &&
|
|
380
326
|
(param1.typeId === undefined || this.#typeEql(param1.typeId, param2.typeId)));
|
|
381
327
|
}
|
|
382
|
-
|
|
383
|
-
const
|
|
384
|
-
const
|
|
385
|
-
const dupTypeParams = types[dupTypeId].params;
|
|
328
|
+
extractDupTypeSuffix(dupTypeId, originalTypeId, dupCount) {
|
|
329
|
+
const originalTypeParams = this.types[originalTypeId].params;
|
|
330
|
+
const dupTypeParams = this.types[dupTypeId].params;
|
|
386
331
|
const diffParam = dupTypeParams.find((one, idx) => !this.#eqlTypeParam(one, originalTypeParams[idx]));
|
|
387
332
|
// TODO make sure these suffix is unique if a type is duplicated more than 2 times
|
|
388
333
|
if (diffParam?.typeId) {
|
|
389
|
-
const diffType = types[diffParam.typeId];
|
|
334
|
+
const diffType = this.types[diffParam.typeId];
|
|
390
335
|
if (diffType.path.length > 0) {
|
|
391
336
|
return (0, utils_1.stringPascalCase)(diffType.path.at(-1));
|
|
392
337
|
}
|
|
@@ -426,5 +371,8 @@ class TypesGen {
|
|
|
426
371
|
}
|
|
427
372
|
this.typeImports.addOutType(typeName);
|
|
428
373
|
}
|
|
374
|
+
getEnumOptions(_typeId) {
|
|
375
|
+
return { tagKey: 'tag', valueKey: 'value' };
|
|
376
|
+
}
|
|
429
377
|
}
|
|
430
|
-
exports.
|
|
378
|
+
exports.BaseTypesGen = BaseTypesGen;
|