@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
|
@@ -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,8 +22,8 @@ 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) {
|
|
@@ -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,8 +34,8 @@ 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) {
|
|
@@ -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,8 +27,8 @@ 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) {
|
|
@@ -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
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');
|
|
@@ -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.getPalletCallTypeIds();
|
|
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
|
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ErrorsGen = 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 ErrorsGen 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('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
|
|
@@ -25,8 +25,8 @@ class ErrorsGen extends ApiGen_js_1.ApiGen {
|
|
|
25
25
|
${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
|
|
26
26
|
},`;
|
|
27
27
|
}
|
|
28
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
29
|
-
const template = (0, utils_js_1.compileTemplate)('errors.hbs');
|
|
28
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
29
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/errors.hbs');
|
|
30
30
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
31
31
|
}
|
|
32
32
|
#getErrorDefs(errorTypeId) {
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventsGen = 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 EventsGen 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('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
|
|
@@ -37,8 +37,8 @@ class EventsGen extends ApiGen_js_1.ApiGen {
|
|
|
37
37
|
${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
|
|
38
38
|
},`;
|
|
39
39
|
}
|
|
40
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
41
|
-
const template = (0, utils_js_1.compileTemplate)('events.hbs');
|
|
40
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
41
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/events.hbs');
|
|
42
42
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
43
43
|
}
|
|
44
44
|
#getEventDefs(typeId) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IndexGen = void 0;
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const index_js_1 = require("../../shared/index.js");
|
|
6
|
+
const utils_js_1 = require("../../utils.js");
|
|
7
|
+
class IndexGen {
|
|
8
|
+
chain;
|
|
9
|
+
constructor(chain) {
|
|
10
|
+
this.chain = chain;
|
|
11
|
+
}
|
|
12
|
+
async generate(useSubPaths = false) {
|
|
13
|
+
const interfaceName = (0, utils_1.stringPascalCase)(this.chain);
|
|
14
|
+
const typeImports = new index_js_1.TypeImports();
|
|
15
|
+
typeImports.addKnownType('GenericSubstrateApi', 'RpcLegacy', 'RpcV2', 'RpcVersion');
|
|
16
|
+
const importTypes = typeImports.toImports({ useSubPaths });
|
|
17
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/index.hbs');
|
|
18
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, interfaceName }));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.IndexGen = IndexGen;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.JsonRpcGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
4
|
+
const api_1 = require("@dedot/api");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
7
|
const HIDDEN_RPCS = [
|
|
8
8
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
9
9
|
'rpc_methods',
|
|
@@ -31,7 +31,7 @@ const ALIAS_RPCS = [
|
|
|
31
31
|
'chain_subscribeFinalisedHeads',
|
|
32
32
|
'chain_unsubscribeFinalisedHeads',
|
|
33
33
|
];
|
|
34
|
-
class JsonRpcGen extends
|
|
34
|
+
class JsonRpcGen extends ApiGen_js_1.ApiGen {
|
|
35
35
|
typesGen;
|
|
36
36
|
rpcMethods;
|
|
37
37
|
constructor(typesGen, rpcMethods) {
|
|
@@ -41,13 +41,13 @@ class JsonRpcGen extends index_js_1.ApiGen {
|
|
|
41
41
|
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
42
42
|
rpcMethods.sort();
|
|
43
43
|
}
|
|
44
|
-
generate() {
|
|
44
|
+
generate(useSubPaths = false) {
|
|
45
45
|
this.typesGen.clearCache();
|
|
46
46
|
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
|
|
47
|
-
this.typesGen.typeImports.
|
|
48
|
-
const toExclude = Object.values(
|
|
49
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
50
|
-
const template = (0, utils_js_1.compileTemplate)('json-rpc.hbs');
|
|
47
|
+
this.typesGen.typeImports.addKnownJsonRpcType('JsonRpcApis');
|
|
48
|
+
const toExclude = Object.values(api_1.subscriptionsInfo).flat();
|
|
49
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
50
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/json-rpc.hbs');
|
|
51
51
|
const jsonRpcMethods = this.rpcMethods
|
|
52
52
|
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
53
53
|
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.QueryGen = 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 QueryGen 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('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
|
|
@@ -30,8 +30,8 @@ class QueryGen extends ApiGen_js_1.ApiGen {
|
|
|
30
30
|
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
|
|
31
31
|
},`;
|
|
32
32
|
}
|
|
33
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
34
|
-
const template = (0, utils_js_1.compileTemplate)('query.hbs');
|
|
33
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
34
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/query.hbs');
|
|
35
35
|
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
36
36
|
}
|
|
37
37
|
#generateEntry(entry) {
|