@dedot/codegen 0.0.1-next.714802d9.27 → 0.0.1-next.7a746a39.2
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 +16 -16
- 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 +6 -6
- package/{generator → chaintypes/generator}/EventsGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/EventsGen.js +6 -6
- 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 +11 -11
- package/{generator → chaintypes/generator}/RuntimeApisGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/RuntimeApisGen.js +6 -6
- package/{generator → chaintypes/generator}/TxGen.d.ts +1 -1
- package/{generator → chaintypes/generator}/TxGen.js +8 -8
- 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 +6 -6
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +6 -6
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/{generator → chaintypes/generator}/JsonRpcGen.js +9 -9
- package/cjs/{generator → chaintypes/generator}/QueryGen.js +11 -11
- package/cjs/{generator → chaintypes/generator}/RuntimeApisGen.js +9 -9
- package/cjs/{generator → chaintypes/generator}/TxGen.js +8 -8
- 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} +111 -159
- 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 +28 -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} +4 -4
- package/index.d.ts +2 -3
- package/index.js +2 -48
- package/package.json +11 -9
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +12 -11
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +108 -156
- 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 +7 -0
- package/typink/generator/TypesGen.js +24 -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} +4 -4
- 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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TypesGen } from '
|
|
1
|
+
import { TypesGen } from './TypesGen.js';
|
|
2
2
|
export declare abstract class ApiGen {
|
|
3
3
|
readonly typesGen: TypesGen;
|
|
4
4
|
constructor(typesGen: TypesGen);
|
|
@@ -10,49 +10,49 @@ export declare abstract class ApiGen {
|
|
|
10
10
|
name: string;
|
|
11
11
|
typeId: number | undefined;
|
|
12
12
|
}[];
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
typeDef: {
|
|
14
|
+
type: "Struct";
|
|
15
15
|
value: {
|
|
16
|
-
fields: import("dedot").Field[];
|
|
16
|
+
fields: import("@dedot/codecs").Field[];
|
|
17
17
|
};
|
|
18
18
|
} | {
|
|
19
|
-
|
|
19
|
+
type: "Enum";
|
|
20
20
|
value: {
|
|
21
21
|
members: {
|
|
22
22
|
name: string;
|
|
23
|
-
fields: import("dedot").Field[];
|
|
23
|
+
fields: import("@dedot/codecs").Field[];
|
|
24
24
|
index: number;
|
|
25
25
|
docs: string[];
|
|
26
26
|
}[];
|
|
27
27
|
};
|
|
28
28
|
} | {
|
|
29
|
-
|
|
29
|
+
type: "Sequence";
|
|
30
30
|
value: {
|
|
31
31
|
typeParam: number;
|
|
32
32
|
};
|
|
33
33
|
} | {
|
|
34
|
-
|
|
34
|
+
type: "SizedVec";
|
|
35
35
|
value: {
|
|
36
36
|
len: number;
|
|
37
37
|
typeParam: number;
|
|
38
38
|
};
|
|
39
39
|
} | {
|
|
40
|
-
|
|
40
|
+
type: "Tuple";
|
|
41
41
|
value: {
|
|
42
42
|
fields: number[];
|
|
43
43
|
};
|
|
44
44
|
} | {
|
|
45
|
-
|
|
45
|
+
type: "Primitive";
|
|
46
46
|
value: {
|
|
47
47
|
kind: "bool" | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "u256" | "i256" | "str" | "char";
|
|
48
48
|
};
|
|
49
49
|
} | {
|
|
50
|
-
|
|
50
|
+
type: "Compact";
|
|
51
51
|
value: {
|
|
52
52
|
typeParam: number;
|
|
53
53
|
};
|
|
54
54
|
} | {
|
|
55
|
-
|
|
55
|
+
type: "BitSequence";
|
|
56
56
|
value: {
|
|
57
57
|
bitOrderType: number;
|
|
58
58
|
bitStoreType: number;
|
|
@@ -67,13 +67,13 @@ export declare abstract class ApiGen {
|
|
|
67
67
|
entries: {
|
|
68
68
|
name: string;
|
|
69
69
|
modifier: string;
|
|
70
|
-
|
|
71
|
-
|
|
70
|
+
storageType: {
|
|
71
|
+
type: "Plain";
|
|
72
72
|
value: {
|
|
73
73
|
valueTypeId: number;
|
|
74
74
|
};
|
|
75
75
|
} | {
|
|
76
|
-
|
|
76
|
+
type: "Map";
|
|
77
77
|
value: {
|
|
78
78
|
hashers: ("identity" | "blake2_128" | "blake2_256" | "blake2_128Concat" | "twox128" | "twox256" | "twox64Concat")[];
|
|
79
79
|
keyTypeId: number;
|
|
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
|
|
|
134
134
|
}>;
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
|
-
get registry(): import("dedot").PortableRegistry;
|
|
137
|
+
get registry(): import("@dedot/codecs").PortableRegistry;
|
|
138
138
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
2
3
|
import { ApiGen } from './ApiGen.js';
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class ConstsGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
8
|
this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
|
|
@@ -20,8 +20,8 @@ export class ConstsGen extends ApiGen {
|
|
|
20
20
|
${commentBlock('Generic pallet constant')}[name: string]: any,
|
|
21
21
|
},`;
|
|
22
22
|
}
|
|
23
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
24
|
-
const template = compileTemplate('consts.hbs');
|
|
23
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
24
|
+
const template = compileTemplate('chaintypes/templates/consts.hbs');
|
|
25
25
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
2
3
|
import { ApiGen } from './ApiGen.js';
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class ErrorsGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
8
|
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
|
|
@@ -22,15 +22,15 @@ export class ErrorsGen extends ApiGen {
|
|
|
22
22
|
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
|
|
23
23
|
},`;
|
|
24
24
|
}
|
|
25
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
26
|
-
const template = compileTemplate('errors.hbs');
|
|
25
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
26
|
+
const template = compileTemplate('chaintypes/templates/errors.hbs');
|
|
27
27
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
28
28
|
}
|
|
29
29
|
#getErrorDefs(errorTypeId) {
|
|
30
30
|
const def = this.metadata.types[errorTypeId];
|
|
31
31
|
assert(def, `Error def not found for id ${errorTypeId}`);
|
|
32
|
-
const {
|
|
33
|
-
assert(
|
|
32
|
+
const { type, value } = def.typeDef;
|
|
33
|
+
assert(type === 'Enum', 'Invalid pallet error type!');
|
|
34
34
|
return value.members;
|
|
35
35
|
}
|
|
36
36
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
2
3
|
import { ApiGen } from './ApiGen.js';
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class EventsGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
8
|
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
|
|
@@ -34,15 +34,15 @@ export class EventsGen extends ApiGen {
|
|
|
34
34
|
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
|
|
35
35
|
},`;
|
|
36
36
|
}
|
|
37
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
38
|
-
const template = compileTemplate('events.hbs');
|
|
37
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
38
|
+
const template = compileTemplate('chaintypes/templates/events.hbs');
|
|
39
39
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
40
40
|
}
|
|
41
41
|
#getEventDefs(typeId) {
|
|
42
42
|
const def = this.metadata.types[typeId];
|
|
43
43
|
assert(def, `Event def not found for id ${typeId}`);
|
|
44
|
-
const {
|
|
45
|
-
assert(
|
|
44
|
+
const { type, value } = def.typeDef;
|
|
45
|
+
assert(type === 'Enum', 'Invalid pallet event type!');
|
|
46
46
|
return value.members;
|
|
47
47
|
}
|
|
48
48
|
#generateMemberDefType(fields) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { TypeImports } from '../../shared/index.js';
|
|
3
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
4
|
+
export class IndexGen {
|
|
5
|
+
chain;
|
|
6
|
+
constructor(chain) {
|
|
7
|
+
this.chain = chain;
|
|
8
|
+
}
|
|
9
|
+
async generate(useSubPaths = false) {
|
|
10
|
+
const interfaceName = stringPascalCase(this.chain);
|
|
11
|
+
const typeImports = new TypeImports();
|
|
12
|
+
typeImports.addKnownType('GenericSubstrateApi', 'RpcLegacy', 'RpcV2', 'RpcVersion');
|
|
13
|
+
const importTypes = typeImports.toImports({ useSubPaths });
|
|
14
|
+
const template = compileTemplate('chaintypes/templates/index.hbs');
|
|
15
|
+
return beautifySourceCode(template({ importTypes, interfaceName }));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { ApiGen
|
|
1
|
+
import { ApiGen } from './ApiGen.js';
|
|
2
|
+
import { TypesGen } from './TypesGen.js';
|
|
2
3
|
export declare class JsonRpcGen extends ApiGen {
|
|
3
4
|
readonly typesGen: TypesGen;
|
|
4
5
|
readonly rpcMethods: string[];
|
|
5
6
|
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
6
|
-
generate(): Promise<string>;
|
|
7
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
7
8
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { subscriptionsInfo } from '@dedot/
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { subscriptionsInfo } from '@dedot/api';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from '../../utils.js';
|
|
3
|
+
import { ApiGen } from './ApiGen.js';
|
|
4
4
|
const HIDDEN_RPCS = [
|
|
5
5
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
6
6
|
'rpc_methods',
|
|
@@ -38,13 +38,13 @@ export class JsonRpcGen extends ApiGen {
|
|
|
38
38
|
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
39
39
|
rpcMethods.sort();
|
|
40
40
|
}
|
|
41
|
-
generate() {
|
|
41
|
+
generate(useSubPaths = false) {
|
|
42
42
|
this.typesGen.clearCache();
|
|
43
43
|
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
|
|
44
|
-
this.typesGen.typeImports.
|
|
44
|
+
this.typesGen.typeImports.addKnownJsonRpcType('JsonRpcApis');
|
|
45
45
|
const toExclude = Object.values(subscriptionsInfo).flat();
|
|
46
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
47
|
-
const template = compileTemplate('json-rpc.hbs');
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
47
|
+
const template = compileTemplate('chaintypes/templates/json-rpc.hbs');
|
|
48
48
|
const jsonRpcMethods = this.rpcMethods
|
|
49
49
|
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
50
50
|
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
2
3
|
import { ApiGen } from './ApiGen.js';
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class QueryGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
8
|
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
|
|
@@ -27,20 +27,20 @@ export class QueryGen extends ApiGen {
|
|
|
27
27
|
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
|
|
28
28
|
},`;
|
|
29
29
|
}
|
|
30
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
31
|
-
const template = compileTemplate('query.hbs');
|
|
30
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
31
|
+
const template = compileTemplate('chaintypes/templates/query.hbs');
|
|
32
32
|
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
33
33
|
}
|
|
34
34
|
#generateEntry(entry) {
|
|
35
|
-
const { name,
|
|
35
|
+
const { name, storageType, docs, modifier } = entry;
|
|
36
36
|
let valueType, keyTypeOut, keyTypeIn;
|
|
37
|
-
if (type
|
|
38
|
-
valueType = this.typesGen.generateType(
|
|
37
|
+
if (storageType.type === 'Plain') {
|
|
38
|
+
valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
|
|
39
39
|
}
|
|
40
|
-
else if (type
|
|
41
|
-
valueType = this.typesGen.generateType(
|
|
42
|
-
keyTypeOut = this.typesGen.generateType(
|
|
43
|
-
keyTypeIn = this.typesGen.generateType(
|
|
40
|
+
else if (storageType.type === 'Map') {
|
|
41
|
+
valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
|
|
42
|
+
keyTypeOut = this.typesGen.generateType(storageType.value.keyTypeId, 1, true);
|
|
43
|
+
keyTypeIn = this.typesGen.generateType(storageType.value.keyTypeId, 1);
|
|
44
44
|
}
|
|
45
45
|
else {
|
|
46
46
|
throw Error('Invalid entry type!');
|
|
@@ -5,5 +5,5 @@ export declare class RuntimeApisGen extends ApiGen {
|
|
|
5
5
|
readonly typesGen: TypesGen;
|
|
6
6
|
readonly runtimeApis: Record<string, number>;
|
|
7
7
|
constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
|
|
8
|
-
generate(): Promise<string>;
|
|
8
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
9
9
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
1
|
+
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/runtime-specs';
|
|
2
2
|
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
3
|
+
import { findKnownCodecType } from '../../shared/index.js';
|
|
4
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from '../../utils.js';
|
|
3
5
|
import { ApiGen } from './ApiGen.js';
|
|
4
|
-
import { findKnownCodecType } from './known-codecs.js';
|
|
5
|
-
import { beautifySourceCode, commentBlock, compileTemplate, isNativeType, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX, } from './utils.js';
|
|
6
6
|
export class RuntimeApisGen extends ApiGen {
|
|
7
7
|
typesGen;
|
|
8
8
|
runtimeApis;
|
|
@@ -11,7 +11,7 @@ export class RuntimeApisGen extends ApiGen {
|
|
|
11
11
|
this.typesGen = typesGen;
|
|
12
12
|
this.runtimeApis = runtimeApis;
|
|
13
13
|
}
|
|
14
|
-
generate() {
|
|
14
|
+
generate(useSubPaths = false) {
|
|
15
15
|
this.typesGen.clearCache();
|
|
16
16
|
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
|
|
17
17
|
let runtimeCallsOut = '';
|
|
@@ -43,8 +43,8 @@ export class RuntimeApisGen extends ApiGen {
|
|
|
43
43
|
}`;
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
47
|
-
const template = compileTemplate('runtime.hbs');
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
47
|
+
const template = compileTemplate('chaintypes/templates/runtime.hbs');
|
|
48
48
|
return beautifySourceCode(template({ importTypes, runtimeCallsOut }));
|
|
49
49
|
}
|
|
50
50
|
#isOptionalType(type) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from '../../utils.js';
|
|
2
3
|
import { ApiGen } from '../generator/index.js';
|
|
3
|
-
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
|
|
4
4
|
export class TxGen extends ApiGen {
|
|
5
|
-
generate() {
|
|
5
|
+
generate(useSubPaths = false) {
|
|
6
6
|
const { pallets, types } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
8
|
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionStatusLegacy', 'TransactionStatusV2');
|
|
@@ -17,11 +17,11 @@ export class TxGen extends ApiGen {
|
|
|
17
17
|
for (let pallet of pallets) {
|
|
18
18
|
if (pallet.calls === undefined)
|
|
19
19
|
continue;
|
|
20
|
-
const {
|
|
21
|
-
if (type
|
|
20
|
+
const { typeDef } = types[pallet.calls];
|
|
21
|
+
if (typeDef.type !== 'Enum')
|
|
22
22
|
continue;
|
|
23
|
-
const isFlatEnum =
|
|
24
|
-
const typedTxs =
|
|
23
|
+
const isFlatEnum = typeDef.value.members.every((m) => m.fields.length === 0);
|
|
24
|
+
const typedTxs = typeDef.value.members
|
|
25
25
|
.map((one) => ({
|
|
26
26
|
functionName: stringCamelCase(one.name),
|
|
27
27
|
params: one.fields.map((f) => ({
|
|
@@ -47,7 +47,7 @@ export class TxGen extends ApiGen {
|
|
|
47
47
|
${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<Rv, TxCall<Rv>>,
|
|
48
48
|
},`;
|
|
49
49
|
}
|
|
50
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
51
51
|
// TODO make explicit separate type for Extra
|
|
52
52
|
const defTypes = `
|
|
53
53
|
export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
@@ -58,7 +58,7 @@ export class TxGen extends ApiGen {
|
|
|
58
58
|
|
|
59
59
|
export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
|
|
60
60
|
`;
|
|
61
|
-
const template = compileTemplate('tx.hbs');
|
|
61
|
+
const template = compileTemplate('chaintypes/templates/tx.hbs');
|
|
62
62
|
return beautifySourceCode(template({ importTypes, defTypes, txDefsOut }));
|
|
63
63
|
}
|
|
64
64
|
#normalizeParamName(name) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { MetadataLatest, PortableRegistry, TypeId } from '@dedot/codecs';
|
|
2
|
+
import { EnumOptions } from '@dedot/shape';
|
|
3
|
+
import { BaseTypesGen } from '../../shared/index.js';
|
|
4
|
+
export declare class TypesGen extends BaseTypesGen {
|
|
5
|
+
metadata: MetadataLatest;
|
|
6
|
+
registry: PortableRegistry;
|
|
7
|
+
constructor(metadata: MetadataLatest);
|
|
8
|
+
generate(useSubPaths?: boolean): Promise<string>;
|
|
9
|
+
shouldGenerateTypeIn(id: TypeId): boolean;
|
|
10
|
+
getEnumOptions(typeId: TypeId): EnumOptions;
|
|
11
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { PortableRegistry } from '@dedot/codecs';
|
|
2
|
+
import { BaseTypesGen } from '../../shared/index.js';
|
|
3
|
+
import { beautifySourceCode, commentBlock, compileTemplate } from '../../utils.js';
|
|
4
|
+
// Skip generate types for these
|
|
5
|
+
// as we do have native types for them
|
|
6
|
+
const SKIP_TYPES = [
|
|
7
|
+
'BoundedBTreeMap',
|
|
8
|
+
'BoundedBTreeSet',
|
|
9
|
+
'BoundedVec',
|
|
10
|
+
'Box',
|
|
11
|
+
'BTreeMap',
|
|
12
|
+
'BTreeSet',
|
|
13
|
+
'Cow',
|
|
14
|
+
'Option',
|
|
15
|
+
'Range',
|
|
16
|
+
'RangeInclusive',
|
|
17
|
+
'Result',
|
|
18
|
+
'WeakBoundedVec',
|
|
19
|
+
'WrapperKeepOpaque',
|
|
20
|
+
'WrapperOpaque',
|
|
21
|
+
];
|
|
22
|
+
export class TypesGen extends BaseTypesGen {
|
|
23
|
+
metadata;
|
|
24
|
+
registry;
|
|
25
|
+
constructor(metadata) {
|
|
26
|
+
super(metadata.types);
|
|
27
|
+
this.metadata = metadata;
|
|
28
|
+
this.skipTypes = SKIP_TYPES;
|
|
29
|
+
this.registry = new PortableRegistry(this.metadata);
|
|
30
|
+
this.includedTypes = this.includeTypes();
|
|
31
|
+
}
|
|
32
|
+
generate(useSubPaths = false) {
|
|
33
|
+
this.clearCache();
|
|
34
|
+
let defTypeOut = '';
|
|
35
|
+
Object.values(this.includedTypes)
|
|
36
|
+
.filter(({ skip, knownType }) => !(skip || knownType))
|
|
37
|
+
.forEach(({ name, nameOut, id, docs }) => {
|
|
38
|
+
defTypeOut += `${commentBlock(docs)}export type ${nameOut} = ${this.generateType(id, 0, true)};\n\n`;
|
|
39
|
+
if (this.shouldGenerateTypeIn(id)) {
|
|
40
|
+
defTypeOut += `export type ${name} = ${this.generateType(id)};\n\n`;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
const importTypes = this.typeImports.toImports({ excludeModules: ['./types'], useSubPaths });
|
|
44
|
+
const template = compileTemplate('chaintypes/templates/types.hbs');
|
|
45
|
+
return beautifySourceCode(template({ importTypes, defTypeOut }));
|
|
46
|
+
}
|
|
47
|
+
shouldGenerateTypeIn(id) {
|
|
48
|
+
const { callTypeId } = this.metadata.extrinsic;
|
|
49
|
+
const palletCallTypeIds = this.registry.getFieldTypeIdsFromEnum(callTypeId);
|
|
50
|
+
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
51
|
+
}
|
|
52
|
+
getEnumOptions(typeId) {
|
|
53
|
+
return this.registry.getEnumOptions(typeId);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
+
export declare function generateTypesFromEndpoint(chain: string, endpoint: string, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
|
|
3
|
+
export declare function generateTypes(chain: string, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: Record<string, number>, outDir?: string, extension?: string, useSubPaths?: boolean): Promise<void>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { LegacyClient } from '@dedot/api';
|
|
2
|
+
import { WsProvider } from '@dedot/providers';
|
|
3
|
+
import { stringCamelCase } from '@dedot/utils';
|
|
4
|
+
import * as fs from 'fs';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, JsonRpcGen, QueryGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
7
|
+
export async function generateTypesFromEndpoint(chain, endpoint, outDir, extension = 'd.ts', useSubPaths = false) {
|
|
8
|
+
const api = await LegacyClient.new(new WsProvider(endpoint));
|
|
9
|
+
const { methods } = await api.rpc.rpc_methods();
|
|
10
|
+
const apis = api.runtimeVersion.apis || {};
|
|
11
|
+
if (!chain) {
|
|
12
|
+
chain = stringCamelCase(api.runtimeVersion.specName || 'local');
|
|
13
|
+
}
|
|
14
|
+
await generateTypes(chain, api.metadata.latest, methods, apis, outDir, extension, useSubPaths);
|
|
15
|
+
await api.disconnect();
|
|
16
|
+
}
|
|
17
|
+
export async function generateTypes(chain, metadata, rpcMethods, runtimeApis, outDir = '.', extension = 'd.ts', useSubPaths = false) {
|
|
18
|
+
const dirPath = path.resolve(outDir, chain);
|
|
19
|
+
const defTypesFileName = path.join(dirPath, `types.${extension}`);
|
|
20
|
+
const constsTypesFileName = path.join(dirPath, `consts.${extension}`);
|
|
21
|
+
const queryTypesFileName = path.join(dirPath, `query.${extension}`);
|
|
22
|
+
const jsonRpcFileName = path.join(dirPath, `json-rpc.${extension}`);
|
|
23
|
+
const indexFileName = path.join(dirPath, `index.${extension}`);
|
|
24
|
+
const errorsFileName = path.join(dirPath, `errors.${extension}`);
|
|
25
|
+
const eventsFileName = path.join(dirPath, `events.${extension}`);
|
|
26
|
+
const runtimeApisFileName = path.join(dirPath, `runtime.${extension}`);
|
|
27
|
+
const txFileName = path.join(dirPath, `tx.${extension}`);
|
|
28
|
+
if (!fs.existsSync(dirPath)) {
|
|
29
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
const typesGen = new TypesGen(metadata);
|
|
32
|
+
const constsGen = new ConstsGen(typesGen);
|
|
33
|
+
const queryGen = new QueryGen(typesGen);
|
|
34
|
+
const jsonRpcGen = new JsonRpcGen(typesGen, rpcMethods);
|
|
35
|
+
const indexGen = new IndexGen(chain);
|
|
36
|
+
const errorsGen = new ErrorsGen(typesGen);
|
|
37
|
+
const eventsGen = new EventsGen(typesGen);
|
|
38
|
+
const runtimeApisGen = new RuntimeApisGen(typesGen, runtimeApis);
|
|
39
|
+
const txGen = new TxGen(typesGen);
|
|
40
|
+
fs.writeFileSync(defTypesFileName, await typesGen.generate(useSubPaths));
|
|
41
|
+
fs.writeFileSync(errorsFileName, await errorsGen.generate(useSubPaths));
|
|
42
|
+
fs.writeFileSync(eventsFileName, await eventsGen.generate(useSubPaths));
|
|
43
|
+
fs.writeFileSync(jsonRpcFileName, await jsonRpcGen.generate(useSubPaths));
|
|
44
|
+
fs.writeFileSync(queryTypesFileName, await queryGen.generate(useSubPaths));
|
|
45
|
+
fs.writeFileSync(constsTypesFileName, await constsGen.generate(useSubPaths));
|
|
46
|
+
fs.writeFileSync(txFileName, await txGen.generate(useSubPaths));
|
|
47
|
+
fs.writeFileSync(indexFileName, await indexGen.generate(useSubPaths));
|
|
48
|
+
fs.writeFileSync(runtimeApisFileName, await runtimeApisGen.generate(useSubPaths));
|
|
49
|
+
}
|
|
@@ -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';
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConstsGen = void 0;
|
|
4
4
|
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
5
6
|
const ApiGen_js_1 = require("./ApiGen.js");
|
|
6
|
-
const utils_js_1 = require("./utils.js");
|
|
7
7
|
class ConstsGen extends ApiGen_js_1.ApiGen {
|
|
8
|
-
generate() {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
9
9
|
const { pallets } = this.metadata;
|
|
10
10
|
this.typesGen.clearCache();
|
|
11
11
|
this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
|
|
@@ -23,8 +23,8 @@ class ConstsGen extends ApiGen_js_1.ApiGen {
|
|
|
23
23
|
${(0, utils_js_1.commentBlock)('Generic pallet constant')}[name: string]: any,
|
|
24
24
|
},`;
|
|
25
25
|
}
|
|
26
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
27
|
-
const template = (0, utils_js_1.compileTemplate)('consts.hbs');
|
|
26
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
27
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/consts.hbs');
|
|
28
28
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
29
29
|
}
|
|
30
30
|
}
|