@dedot/codegen 0.0.1-next.f1aed4d8.4 → 0.0.1-next.fecbe32d.13
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 +3 -4
- package/cjs/generator/ErrorsGen.js +3 -4
- package/cjs/generator/EventsGen.js +3 -4
- package/cjs/generator/IndexGen.js +5 -6
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +18 -10
- package/cjs/generator/RuntimeApisGen.js +93 -19
- package/cjs/generator/TxGen.js +6 -6
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +8 -15
- package/cjs/generator/index.js +1 -1
- package/cjs/generator/known-codecs.js +1 -1
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +24 -24
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +3 -4
- package/generator/ErrorsGen.js +1 -2
- package/generator/EventsGen.js +1 -2
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +5 -6
- package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +18 -10
- package/generator/RuntimeApisGen.d.ts +4 -4
- package/generator/RuntimeApisGen.js +89 -15
- package/generator/TxGen.js +1 -1
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +3 -10
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/generator/known-codecs.js +1 -1
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +23 -23
- package/package.json +7 -8
- package/templates/index.hbs +2 -2
- package/templates/json-rpc.hbs +5 -0
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/RpcGen.js +0 -176
- package/cjs/generator/known-types.js +0 -33
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/generator/RpcGen.js +0 -172
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -6
- package/types.js +0 -1
package/generator/utils.d.ts
CHANGED
|
@@ -8,3 +8,8 @@ export declare const compileTemplate: (templateFileName: string) => HandlebarsTe
|
|
|
8
8
|
* @param word
|
|
9
9
|
*/
|
|
10
10
|
export declare const isReservedWord: (word: string) => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Check if a type is native JS/TS type
|
|
13
|
+
* @param type
|
|
14
|
+
*/
|
|
15
|
+
export declare const isNativeType: (type: string) => boolean;
|
package/generator/utils.js
CHANGED
|
@@ -33,3 +33,31 @@ const TS_RESERVED_WORDS = ['new', 'class'];
|
|
|
33
33
|
* @param word
|
|
34
34
|
*/
|
|
35
35
|
export const isReservedWord = (word) => TS_RESERVED_WORDS.includes(word);
|
|
36
|
+
const TS_PRIMITIVE_TYPES = [
|
|
37
|
+
'void',
|
|
38
|
+
'undefined',
|
|
39
|
+
'null',
|
|
40
|
+
'number',
|
|
41
|
+
'boolean',
|
|
42
|
+
'bigint',
|
|
43
|
+
'Map',
|
|
44
|
+
'Set',
|
|
45
|
+
'string',
|
|
46
|
+
'any',
|
|
47
|
+
'Array',
|
|
48
|
+
'Record',
|
|
49
|
+
];
|
|
50
|
+
/**
|
|
51
|
+
* Check if a type is native JS/TS type
|
|
52
|
+
* @param type
|
|
53
|
+
*/
|
|
54
|
+
export const isNativeType = (type) => {
|
|
55
|
+
return TS_PRIMITIVE_TYPES.some((one) => {
|
|
56
|
+
if (typeof one === 'string') {
|
|
57
|
+
return one === type;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
return type.match(one);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
-
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function generateTypes(network: NetworkInfo, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
|
|
2
|
+
export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string): Promise<void>;
|
|
3
|
+
export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
1
2
|
import { Dedot } from 'dedot';
|
|
2
3
|
import * as fs from 'fs';
|
|
3
4
|
import * as path from 'path';
|
|
4
|
-
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
network.chain = stringCamelCase(api.runtimeVersion?.specName || api.runtimeChain || 'local');
|
|
5
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, JsonRpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
6
|
+
export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts') {
|
|
7
|
+
const api = await Dedot.new(endpoint);
|
|
8
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
9
|
+
const apis = api.runtimeVersion.apis || {};
|
|
10
|
+
if (!chain) {
|
|
11
|
+
chain = stringCamelCase(api.runtimeVersion.specName || 'local');
|
|
12
12
|
}
|
|
13
|
-
await generateTypes(
|
|
13
|
+
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension);
|
|
14
14
|
await api.disconnect();
|
|
15
15
|
}
|
|
16
|
-
export async function generateTypes(
|
|
17
|
-
const dirPath = path.resolve(outDir,
|
|
18
|
-
const defTypesFileName = path.join(dirPath, `types
|
|
19
|
-
const constsTypesFileName = path.join(dirPath, `consts
|
|
20
|
-
const queryTypesFileName = path.join(dirPath, `query
|
|
21
|
-
const
|
|
22
|
-
const indexFileName = path.join(dirPath, `index
|
|
23
|
-
const errorsFileName = path.join(dirPath, `errors
|
|
24
|
-
const eventsFileName = path.join(dirPath, `events
|
|
25
|
-
const runtimeApisFileName = path.join(dirPath, `runtime
|
|
26
|
-
const txFileName = path.join(dirPath, `tx
|
|
16
|
+
export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts') {
|
|
17
|
+
const dirPath = path.resolve(outDir, chain);
|
|
18
|
+
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
19
|
+
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
20
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
21
|
+
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
22
|
+
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
23
|
+
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
24
|
+
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
25
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
26
|
+
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
27
27
|
if (!fs.existsSync(dirPath)) {
|
|
28
28
|
fs.mkdirSync(dirPath, { recursive: true });
|
|
29
29
|
}
|
|
30
30
|
const typesGen = new TypesGen(metadata);
|
|
31
31
|
const constsGen = new ConstsGen(typesGen);
|
|
32
32
|
const queryGen = new QueryGen(typesGen);
|
|
33
|
-
const
|
|
34
|
-
const indexGen = new IndexGen(
|
|
33
|
+
const jsonRpcGen = new JsonRpcGen(typesGen, rpcMethods);
|
|
34
|
+
const indexGen = new IndexGen(chain);
|
|
35
35
|
const errorsGen = new ErrorsGen(typesGen);
|
|
36
36
|
const eventsGen = new EventsGen(typesGen);
|
|
37
37
|
const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeApis);
|
|
@@ -39,7 +39,7 @@ export async function generateTypes(network, metadata, rpcMethods, runtimeApis,
|
|
|
39
39
|
fs.writeFileSync(defTypesFileName, await typesGen.generate());
|
|
40
40
|
fs.writeFileSync(errorsFileName, await errorsGen.generate());
|
|
41
41
|
fs.writeFileSync(eventsFileName, await eventsGen.generate());
|
|
42
|
-
fs.writeFileSync(
|
|
42
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate());
|
|
43
43
|
fs.writeFileSync(queryTypesFileName, await queryGen.generate());
|
|
44
44
|
fs.writeFileSync(constsTypesFileName, await constsGen.generate());
|
|
45
45
|
fs.writeFileSync(txFileName, await txGen.generate());
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/codegen",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
4
4
|
"description": "Generate types",
|
|
5
5
|
"author": "Thang X. Vu <thang@coongcrafts.io>",
|
|
6
6
|
"homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
|
|
@@ -18,12 +18,11 @@
|
|
|
18
18
|
"copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dedot/codecs": "0.0.1-next.
|
|
22
|
-
"@dedot/shape": "0.0.1-next.
|
|
23
|
-
"@dedot/specs": "0.0.1-next.
|
|
24
|
-
"@dedot/utils": "0.0.1-next.
|
|
25
|
-
"
|
|
26
|
-
"dedot": "0.0.1-next.f1aed4d8.4+f1aed4d",
|
|
21
|
+
"@dedot/codecs": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
22
|
+
"@dedot/shape": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
23
|
+
"@dedot/specs": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
24
|
+
"@dedot/utils": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
25
|
+
"dedot": "0.0.1-next.fecbe32d.13+fecbe32",
|
|
27
26
|
"handlebars": "^4.7.8",
|
|
28
27
|
"prettier": "^3.0.3"
|
|
29
28
|
},
|
|
@@ -32,7 +31,7 @@
|
|
|
32
31
|
"directory": "dist"
|
|
33
32
|
},
|
|
34
33
|
"license": "Apache-2.0",
|
|
35
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "fecbe32d9d4b04cdb6802bf0ac604222f54b8331",
|
|
36
35
|
"module": "./index.js",
|
|
37
36
|
"types": "./index.d.ts",
|
|
38
37
|
"exports": {
|
package/templates/index.hbs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { GenericSubstrateApi } from '@dedot/types';
|
|
4
4
|
import { ChainConsts } from './consts';
|
|
5
5
|
import { ChainStorage } from './query';
|
|
6
|
-
import {
|
|
6
|
+
import { ChainJsonRpcApis } from './json-rpc';
|
|
7
7
|
import { ChainErrors } from './errors';
|
|
8
8
|
import { ChainEvents } from './events';
|
|
9
9
|
import { RuntimeApis } from './runtime';
|
|
@@ -12,7 +12,7 @@ import { ChainTx } from './tx';
|
|
|
12
12
|
export * from './types';
|
|
13
13
|
|
|
14
14
|
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
-
rpc:
|
|
15
|
+
rpc: ChainJsonRpcApis;
|
|
16
16
|
consts: ChainConsts;
|
|
17
17
|
query: ChainStorage;
|
|
18
18
|
errors: ChainErrors;
|
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const index_js_1 = require("./index.js");
|
|
7
|
-
const static_substrate_1 = require("@polkadot/types-support/metadata/static-substrate");
|
|
8
|
-
const substrate_hex_1 = __importDefault(require("@polkadot/types-support/metadata/v15/substrate-hex"));
|
|
9
|
-
const codecs_1 = require("@dedot/codecs");
|
|
10
|
-
const dedot_1 = require("dedot");
|
|
11
|
-
const NETWORKS = [
|
|
12
|
-
{
|
|
13
|
-
chain: 'substrate',
|
|
14
|
-
metadataHex: substrate_hex_1.default,
|
|
15
|
-
rpcMethods: static_substrate_1.rpc.methods,
|
|
16
|
-
},
|
|
17
|
-
{
|
|
18
|
-
chain: 'polkadot',
|
|
19
|
-
endpoint: 'wss://rpc.polkadot.io',
|
|
20
|
-
},
|
|
21
|
-
{
|
|
22
|
-
chain: 'kusama',
|
|
23
|
-
endpoint: 'wss://kusama-rpc.polkadot.io',
|
|
24
|
-
},
|
|
25
|
-
{
|
|
26
|
-
chain: 'astar',
|
|
27
|
-
endpoint: 'wss://rpc.astar.network',
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
chain: 'moonbeam',
|
|
31
|
-
endpoint: 'wss://moonbeam.api.onfinality.io/public-ws',
|
|
32
|
-
},
|
|
33
|
-
{
|
|
34
|
-
chain: 'polkadotAssetHub',
|
|
35
|
-
endpoint: 'wss://polkadot-asset-hub-rpc.polkadot.io/',
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
chain: 'kusamaAssetHub',
|
|
39
|
-
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
chain: 'rococo',
|
|
43
|
-
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
44
|
-
},
|
|
45
|
-
{
|
|
46
|
-
chain: 'rococoAssetHub',
|
|
47
|
-
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
chain: 'aleph',
|
|
51
|
-
endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
|
|
52
|
-
},
|
|
53
|
-
{
|
|
54
|
-
chain: 'westendAssetHub',
|
|
55
|
-
endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
|
|
56
|
-
},
|
|
57
|
-
];
|
|
58
|
-
const OUT_DIR = 'packages/chaintypes/src';
|
|
59
|
-
async function run() {
|
|
60
|
-
for (const network of NETWORKS) {
|
|
61
|
-
const { chain, endpoint, metadataHex, rpcMethods } = network;
|
|
62
|
-
if (endpoint) {
|
|
63
|
-
console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
|
|
64
|
-
await (0, index_js_1.generateTypesFromChain)(network, endpoint, OUT_DIR);
|
|
65
|
-
}
|
|
66
|
-
else if (metadataHex && rpcMethods) {
|
|
67
|
-
console.log(`Generate types for ${chain} via raw data`);
|
|
68
|
-
const metadata = codecs_1.$Metadata.tryDecode(metadataHex);
|
|
69
|
-
const runtimeVersion = getRuntimeVersion(metadata);
|
|
70
|
-
await (0, index_js_1.generateTypes)(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
console.log('DONE!');
|
|
74
|
-
}
|
|
75
|
-
const getRuntimeVersion = (metadata) => {
|
|
76
|
-
const registry = new codecs_1.PortableRegistry(metadata.latest);
|
|
77
|
-
const executor = new dedot_1.ConstantExecutor({
|
|
78
|
-
registry,
|
|
79
|
-
metadataLatest: metadata.latest,
|
|
80
|
-
});
|
|
81
|
-
return executor.execute('system', 'version');
|
|
82
|
-
};
|
|
83
|
-
run().catch(console.log);
|
package/cjs/generator/RpcGen.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RpcGen = void 0;
|
|
4
|
-
const specs_1 = require("@dedot/specs");
|
|
5
|
-
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const index_js_1 = require("../generator/index.js");
|
|
7
|
-
const utils_js_1 = require("./utils.js");
|
|
8
|
-
const known_codecs_js_1 = require("./known-codecs.js");
|
|
9
|
-
const HIDDEN_RPCS = [
|
|
10
|
-
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
11
|
-
'rpc_methods',
|
|
12
|
-
];
|
|
13
|
-
class RpcGen extends index_js_1.ApiGen {
|
|
14
|
-
typesGen;
|
|
15
|
-
rpcMethods;
|
|
16
|
-
constructor(typesGen, rpcMethods) {
|
|
17
|
-
super(typesGen);
|
|
18
|
-
this.typesGen = typesGen;
|
|
19
|
-
this.rpcMethods = rpcMethods;
|
|
20
|
-
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
21
|
-
rpcMethods.sort();
|
|
22
|
-
}
|
|
23
|
-
generate() {
|
|
24
|
-
this.typesGen.clearCache();
|
|
25
|
-
this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
|
|
26
|
-
const specsByModule = this.rpcMethods
|
|
27
|
-
.filter((one) => !(0, specs_1.findAliasRpcSpec)(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
|
|
28
|
-
.filter((one) => !(0, specs_1.isUnsubscribeMethod)(one)) // we'll ignore unsubscribe method as well
|
|
29
|
-
.map((one) => {
|
|
30
|
-
const spec = (0, specs_1.findRpcSpec)(one);
|
|
31
|
-
if (spec) {
|
|
32
|
-
return spec;
|
|
33
|
-
}
|
|
34
|
-
const [module, ...methodParts] = one.split('_');
|
|
35
|
-
const method = methodParts.join('_');
|
|
36
|
-
return {
|
|
37
|
-
params: [],
|
|
38
|
-
type: 'GenericRpcCall',
|
|
39
|
-
module,
|
|
40
|
-
method,
|
|
41
|
-
};
|
|
42
|
-
})
|
|
43
|
-
.reduce((o, spec) => {
|
|
44
|
-
const { module, method } = spec;
|
|
45
|
-
// ignore if rpc name does not confront with the general convention
|
|
46
|
-
if (!module || !method) {
|
|
47
|
-
return o;
|
|
48
|
-
}
|
|
49
|
-
return {
|
|
50
|
-
...o,
|
|
51
|
-
[module]: o[module] ? [...o[module], spec] : [spec],
|
|
52
|
-
};
|
|
53
|
-
}, {});
|
|
54
|
-
let rpcCallsOut = '';
|
|
55
|
-
Object.keys(specsByModule).forEach((module) => {
|
|
56
|
-
const specs = specsByModule[module];
|
|
57
|
-
// TODO add alias info to docs block!
|
|
58
|
-
rpcCallsOut += `${module}: {
|
|
59
|
-
${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
|
|
60
|
-
|
|
61
|
-
[method: string]: GenericRpcCall,
|
|
62
|
-
},`;
|
|
63
|
-
});
|
|
64
|
-
// TODO include & define external types
|
|
65
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
66
|
-
const template = (0, utils_js_1.compileTemplate)('rpc.hbs');
|
|
67
|
-
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, rpcCallsOut }));
|
|
68
|
-
}
|
|
69
|
-
#generateMethodDef(spec) {
|
|
70
|
-
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
71
|
-
const rpcName = name || `${module}_${method}`;
|
|
72
|
-
let defaultDocs = [`@rpcname: ${rpcName}`];
|
|
73
|
-
if (deprecated) {
|
|
74
|
-
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
75
|
-
}
|
|
76
|
-
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
77
|
-
return `${(0, utils_js_1.commentBlock)(defaultDocs)}${method}: GenericRpcCall`;
|
|
78
|
-
}
|
|
79
|
-
this.addTypeImport(type, false);
|
|
80
|
-
params.forEach(({ type, isScale }) => {
|
|
81
|
-
this.addTypeImport(type, !!isScale);
|
|
82
|
-
});
|
|
83
|
-
const typedParams = params.map((param) => ({
|
|
84
|
-
...param,
|
|
85
|
-
plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
|
|
86
|
-
}));
|
|
87
|
-
const isSubscription = !!pubsub;
|
|
88
|
-
const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
|
|
89
|
-
const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
|
|
90
|
-
const typeOut = this.getGeneratedTypeName(type, false);
|
|
91
|
-
if (isSubscription) {
|
|
92
|
-
defaultDocs.shift();
|
|
93
|
-
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
94
|
-
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
95
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
96
|
-
}
|
|
97
|
-
else {
|
|
98
|
-
return `${(0, utils_js_1.commentBlock)(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
102
|
-
addTypeImport(type, toTypeIn = true) {
|
|
103
|
-
if (Array.isArray(type)) {
|
|
104
|
-
type.forEach((one) => this.addTypeImport(one, toTypeIn));
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
type = type.trim();
|
|
108
|
-
if ((0, utils_1.isNativeType)(type)) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
// Handle generic wrapper types
|
|
112
|
-
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
113
|
-
if (matchArray) {
|
|
114
|
-
const [_, $1, $2] = matchArray;
|
|
115
|
-
this.addTypeImport($1, toTypeIn);
|
|
116
|
-
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
117
|
-
this.addTypeImport($2, toTypeIn);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
this.addTypeImport($2.split(','), toTypeIn);
|
|
121
|
-
}
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
// Check tuple type
|
|
125
|
-
if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
126
|
-
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
if (type.includes(' | ')) {
|
|
130
|
-
this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
try {
|
|
134
|
-
const codecType = this.#getCodecType(type, toTypeIn);
|
|
135
|
-
if ((0, utils_1.isNativeType)(codecType))
|
|
136
|
-
return;
|
|
137
|
-
this.typesGen.typeImports.addCodecType(codecType);
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
catch (e) { }
|
|
141
|
-
this.typesGen.addTypeImport(type);
|
|
142
|
-
}
|
|
143
|
-
getGeneratedTypeName(type, toTypeIn = true) {
|
|
144
|
-
try {
|
|
145
|
-
const matchArray = type.match(utils_js_1.WRAPPER_TYPE_REGEX);
|
|
146
|
-
if (matchArray) {
|
|
147
|
-
const [_, $1, $2] = matchArray;
|
|
148
|
-
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
149
|
-
if ($2.match(utils_js_1.WRAPPER_TYPE_REGEX) || $2.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
150
|
-
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
151
|
-
}
|
|
152
|
-
const innerTypeNames = $2
|
|
153
|
-
.split(',')
|
|
154
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
155
|
-
.join(', ');
|
|
156
|
-
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
157
|
-
}
|
|
158
|
-
else if (type.match(utils_js_1.TUPLE_TYPE_REGEX)) {
|
|
159
|
-
const innerTypeNames = type
|
|
160
|
-
.slice(1, -1)
|
|
161
|
-
.split(',')
|
|
162
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
163
|
-
.join(', ');
|
|
164
|
-
return `[${innerTypeNames}]`;
|
|
165
|
-
}
|
|
166
|
-
return this.#getCodecType(type, toTypeIn);
|
|
167
|
-
}
|
|
168
|
-
catch (e) { }
|
|
169
|
-
return type;
|
|
170
|
-
}
|
|
171
|
-
#getCodecType(type, toTypeIn = true) {
|
|
172
|
-
const { typeIn, typeOut } = (0, known_codecs_js_1.findKnownCodecType)(type);
|
|
173
|
-
return toTypeIn ? typeIn : typeOut;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
exports.RpcGen = RpcGen;
|
|
@@ -1,33 +0,0 @@
|
|
|
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/packageInfo.js
DELETED
package/cjs/templates/rpc.hbs
DELETED
package/cjs/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import { generateTypes, generateTypesFromChain } from './index.js';
|
|
2
|
-
import { rpc } from '@polkadot/types-support/metadata/static-substrate';
|
|
3
|
-
import staticSubstrate from '@polkadot/types-support/metadata/v15/substrate-hex';
|
|
4
|
-
import { $Metadata, PortableRegistry } from '@dedot/codecs';
|
|
5
|
-
import { ConstantExecutor } from 'dedot';
|
|
6
|
-
const NETWORKS = [
|
|
7
|
-
{
|
|
8
|
-
chain: 'substrate',
|
|
9
|
-
metadataHex: staticSubstrate,
|
|
10
|
-
rpcMethods: rpc.methods,
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
chain: 'polkadot',
|
|
14
|
-
endpoint: 'wss://rpc.polkadot.io',
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
chain: 'kusama',
|
|
18
|
-
endpoint: 'wss://kusama-rpc.polkadot.io',
|
|
19
|
-
},
|
|
20
|
-
{
|
|
21
|
-
chain: 'astar',
|
|
22
|
-
endpoint: 'wss://rpc.astar.network',
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
chain: 'moonbeam',
|
|
26
|
-
endpoint: 'wss://moonbeam.api.onfinality.io/public-ws',
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
chain: 'polkadotAssetHub',
|
|
30
|
-
endpoint: 'wss://polkadot-asset-hub-rpc.polkadot.io/',
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
chain: 'kusamaAssetHub',
|
|
34
|
-
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
chain: 'rococo',
|
|
38
|
-
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
chain: 'rococoAssetHub',
|
|
42
|
-
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
43
|
-
},
|
|
44
|
-
{
|
|
45
|
-
chain: 'aleph',
|
|
46
|
-
endpoint: 'wss://aleph-zero.api.onfinality.io/public-ws',
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
chain: 'westendAssetHub',
|
|
50
|
-
endpoint: 'wss://westend-asset-hub-rpc.polkadot.io',
|
|
51
|
-
},
|
|
52
|
-
];
|
|
53
|
-
const OUT_DIR = 'packages/chaintypes/src';
|
|
54
|
-
async function run() {
|
|
55
|
-
for (const network of NETWORKS) {
|
|
56
|
-
const { chain, endpoint, metadataHex, rpcMethods } = network;
|
|
57
|
-
if (endpoint) {
|
|
58
|
-
console.log(`Generate types for ${chain} via endpoint ${endpoint}`);
|
|
59
|
-
await generateTypesFromChain(network, endpoint, OUT_DIR);
|
|
60
|
-
}
|
|
61
|
-
else if (metadataHex && rpcMethods) {
|
|
62
|
-
console.log(`Generate types for ${chain} via raw data`);
|
|
63
|
-
const metadata = $Metadata.tryDecode(metadataHex);
|
|
64
|
-
const runtimeVersion = getRuntimeVersion(metadata);
|
|
65
|
-
await generateTypes(network, metadata.latest, rpcMethods, runtimeVersion.apis, OUT_DIR);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
console.log('DONE!');
|
|
69
|
-
}
|
|
70
|
-
const getRuntimeVersion = (metadata) => {
|
|
71
|
-
const registry = new PortableRegistry(metadata.latest);
|
|
72
|
-
const executor = new ConstantExecutor({
|
|
73
|
-
registry,
|
|
74
|
-
metadataLatest: metadata.latest,
|
|
75
|
-
});
|
|
76
|
-
return executor.execute('system', 'version');
|
|
77
|
-
};
|
|
78
|
-
run().catch(console.log);
|