@dedot/codegen 0.0.1-alpha.4 → 0.0.1-alpha.40
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 +15 -15
- package/chaintypes/generator/ConstsGen.d.ts +4 -0
- package/{generator → chaintypes/generator}/ConstsGen.js +8 -9
- package/chaintypes/generator/ErrorsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/ErrorsGen.js +11 -12
- package/chaintypes/generator/EventsGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/EventsGen.js +11 -12
- package/chaintypes/generator/IndexGen.d.ts +5 -0
- package/chaintypes/generator/IndexGen.js +17 -0
- package/chaintypes/generator/JsonRpcGen.d.ts +8 -0
- package/chaintypes/generator/JsonRpcGen.js +55 -0
- package/chaintypes/generator/QueryGen.d.ts +5 -0
- package/chaintypes/generator/QueryGen.js +63 -0
- package/chaintypes/generator/RuntimeApisGen.d.ts +9 -0
- package/chaintypes/generator/RuntimeApisGen.js +189 -0
- package/chaintypes/generator/TxGen.d.ts +5 -0
- package/{generator → chaintypes/generator}/TxGen.js +18 -16
- package/chaintypes/generator/TypesGen.d.ts +11 -0
- package/chaintypes/generator/TypesGen.js +55 -0
- package/chaintypes/generator/index.d.ts +10 -0
- package/chaintypes/generator/index.js +10 -0
- package/chaintypes/index.d.ts +3 -0
- package/chaintypes/index.js +49 -0
- package/chaintypes/templates/consts.hbs +7 -0
- package/chaintypes/templates/errors.hbs +7 -0
- package/chaintypes/templates/events.hbs +7 -0
- package/chaintypes/templates/index.hbs +27 -0
- package/chaintypes/templates/json-rpc.hbs +5 -0
- package/chaintypes/templates/query.hbs +7 -0
- package/chaintypes/templates/runtime.hbs +8 -0
- package/chaintypes/templates/tx.hbs +9 -0
- package/chaintypes/templates/types.hbs +5 -0
- package/cjs/{generator → chaintypes/generator}/ConstsGen.js +12 -13
- package/cjs/chaintypes/generator/ErrorsGen.js +40 -0
- package/cjs/{generator → chaintypes/generator}/EventsGen.js +17 -18
- package/cjs/chaintypes/generator/IndexGen.js +21 -0
- package/cjs/chaintypes/generator/JsonRpcGen.js +59 -0
- package/cjs/chaintypes/generator/QueryGen.js +67 -0
- package/cjs/chaintypes/generator/RuntimeApisGen.js +193 -0
- package/cjs/{generator → chaintypes/generator}/TxGen.js +27 -25
- package/cjs/chaintypes/generator/TypesGen.js +59 -0
- package/cjs/chaintypes/generator/index.js +26 -0
- package/cjs/chaintypes/index.js +77 -0
- package/cjs/chaintypes/templates/consts.hbs +7 -0
- package/cjs/chaintypes/templates/errors.hbs +7 -0
- package/cjs/chaintypes/templates/events.hbs +7 -0
- package/cjs/chaintypes/templates/index.hbs +27 -0
- package/cjs/chaintypes/templates/json-rpc.hbs +5 -0
- package/cjs/chaintypes/templates/query.hbs +7 -0
- package/cjs/chaintypes/templates/runtime.hbs +8 -0
- package/cjs/chaintypes/templates/tx.hbs +9 -0
- package/cjs/chaintypes/templates/types.hbs +5 -0
- package/cjs/{types.js → dirname.js} +3 -0
- package/cjs/index.js +4 -62
- package/cjs/{generator/TypesGen.js → shared/BaseTypesGen.js} +123 -177
- package/cjs/{generator → shared}/TypeImports.js +29 -3
- package/cjs/{generator → shared}/index.js +3 -10
- package/cjs/shared/known-codecs.js +170 -0
- 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} +35 -11
- package/dirname.d.ts +1 -0
- package/dirname.js +6 -0
- package/index.d.ts +2 -4
- package/index.js +2 -48
- package/package.json +18 -19
- package/{generator/TypesGen.d.ts → shared/BaseTypesGen.d.ts} +13 -12
- package/{generator/TypesGen.js → shared/BaseTypesGen.js} +112 -166
- package/{generator → shared}/TypeImports.d.ts +12 -1
- package/{generator → shared}/TypeImports.js +29 -3
- package/shared/index.d.ts +3 -0
- package/shared/index.js +3 -0
- package/shared/known-codecs.d.ts +23 -0
- package/shared/known-codecs.js +139 -0
- 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} +6 -2
- package/{generator/utils.js → utils.js} +33 -9
- package/cjs/genSupportedChainTypes.js +0 -79
- package/cjs/generator/ErrorsGen.js +0 -41
- package/cjs/generator/IndexGen.js +0 -18
- package/cjs/generator/QueryGen.js +0 -59
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/generator/RuntimeApisGen.js +0 -120
- package/cjs/packageInfo.js +0 -5
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -74
- package/generator/ConstsGen.d.ts +0 -4
- package/generator/ErrorsGen.d.ts +0 -5
- package/generator/EventsGen.d.ts +0 -5
- package/generator/IndexGen.d.ts +0 -6
- package/generator/IndexGen.js +0 -14
- package/generator/QueryGen.d.ts +0 -5
- package/generator/QueryGen.js +0 -55
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- package/generator/RuntimeApisGen.d.ts +0 -9
- package/generator/RuntimeApisGen.js +0 -116
- package/generator/TxGen.d.ts +0 -5
- package/generator/index.d.ts +0 -10
- package/generator/index.js +0 -10
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/types.d.ts +0 -6
- package/types.js +0 -1
- /package/{generator → chaintypes/generator}/ApiGen.js +0 -0
- /package/cjs/{generator → chaintypes/generator}/ApiGen.js +0 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from '../../utils.js';
|
|
3
|
+
import { ApiGen } from '../generator/index.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
|
-
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainTx', 'GenericTxCall', 'ISubmittableExtrinsic', 'ISubmittableResult', 'IRuntimeTxCall', 'RpcVersion', 'RpcV2', 'ISubmittableExtrinsicLegacy', 'TransactionEvent');
|
|
9
9
|
const { callTypeId, addressTypeId, signatureTypeId } = this.metadata.extrinsic;
|
|
10
10
|
const callTypeIn = this.typesGen.generateType(callTypeId, 1);
|
|
11
11
|
const addressTypeIn = this.typesGen.generateType(addressTypeId, 1);
|
|
@@ -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) => ({
|
|
@@ -41,22 +41,24 @@ export class TxGen extends ApiGen {
|
|
|
41
41
|
txDefsOut += commentBlock(`Pallet \`${pallet.name}\`'s transaction calls`);
|
|
42
42
|
txDefsOut += `${stringCamelCase(pallet.name)}: {
|
|
43
43
|
${typedTxs
|
|
44
|
-
.map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<(${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic
|
|
44
|
+
.map(({ functionName, params, docs, callInput }) => `${commentBlock(docs, '\n', params.map((p) => `@param {${p.type}} ${p.normalizedName} ${p.docs}`))}${functionName}: GenericTxCall<Rv, (${params.map((p) => `${p.normalizedName}: ${p.type}`).join(', ')}) => ChainSubmittableExtrinsic<Rv, ${callInput}>>`)
|
|
45
45
|
.join(',\n')}
|
|
46
46
|
|
|
47
|
-
${commentBlock('Generic pallet tx call')}[callName: string]: GenericTxCall<TxCall
|
|
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
|
-
export type ChainSubmittableExtrinsic<T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
53
|
+
export type ChainSubmittableExtrinsic<Rv extends RpcVersion, T extends IRuntimeTxCall = ${callTypeIn}> =
|
|
54
54
|
Extrinsic<${addressTypeIn}, T, ${signatureTypeIn}, any[]> &
|
|
55
|
-
|
|
55
|
+
(Rv extends RpcV2
|
|
56
|
+
? ISubmittableExtrinsic<ISubmittableResult<FrameSystemEventRecord, TransactionEvent>>
|
|
57
|
+
: ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionEvent>>)
|
|
56
58
|
|
|
57
|
-
export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic
|
|
59
|
+
export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
|
|
58
60
|
`;
|
|
59
|
-
const template = compileTemplate('tx.hbs');
|
|
61
|
+
const template = compileTemplate('chaintypes/templates/tx.hbs');
|
|
60
62
|
return beautifySourceCode(template({ importTypes, defTypes, txDefsOut }));
|
|
61
63
|
}
|
|
62
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,10 @@
|
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './TypesGen.js';
|
|
2
|
+
export * from './ApiGen.js';
|
|
3
|
+
export * from './ConstsGen.js';
|
|
4
|
+
export * from './QueryGen.js';
|
|
5
|
+
export * from './JsonRpcGen.js';
|
|
6
|
+
export * from './IndexGen.js';
|
|
7
|
+
export * from './ErrorsGen.js';
|
|
8
|
+
export * from './EventsGen.js';
|
|
9
|
+
export * from './TxGen.js';
|
|
10
|
+
export * from './RuntimeApisGen.js';
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// Generated by dedot cli
|
|
2
|
+
|
|
3
|
+
{{{importTypes}}}
|
|
4
|
+
import { ChainConsts } from './consts';
|
|
5
|
+
import { ChainStorage } from './query';
|
|
6
|
+
import { ChainJsonRpcApis } from './json-rpc';
|
|
7
|
+
import { ChainErrors } from './errors';
|
|
8
|
+
import { ChainEvents } from './events';
|
|
9
|
+
import { RuntimeApis } from './runtime';
|
|
10
|
+
import { ChainTx } from './tx';
|
|
11
|
+
|
|
12
|
+
export * from './types';
|
|
13
|
+
|
|
14
|
+
export interface Versioned{{{interfaceName}}}Api<Rv extends RpcVersion> extends GenericSubstrateApi<Rv> {
|
|
15
|
+
rpc: ChainJsonRpcApis<Rv>;
|
|
16
|
+
consts: ChainConsts<Rv>;
|
|
17
|
+
query: ChainStorage<Rv>;
|
|
18
|
+
errors: ChainErrors<Rv>;
|
|
19
|
+
events: ChainEvents<Rv>;
|
|
20
|
+
call: RuntimeApis<Rv>;
|
|
21
|
+
tx: ChainTx<Rv>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface {{{interfaceName}}}Api {
|
|
25
|
+
legacy: Versioned{{{interfaceName}}}Api<RpcLegacy>;
|
|
26
|
+
v2: Versioned{{{interfaceName}}}Api<RpcV2>;
|
|
27
|
+
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ConstsGen = void 0;
|
|
4
|
-
const util_1 = require("@polkadot/util");
|
|
5
4
|
const utils_1 = require("@dedot/utils");
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
class ConstsGen extends
|
|
9
|
-
generate() {
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
+
class ConstsGen extends ApiGen_js_1.ApiGen {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
12
|
-
this.typesGen.typeImports.addKnownType('GenericChainConsts');
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
|
|
13
12
|
let defTypeOut = '';
|
|
14
13
|
for (let pallet of pallets) {
|
|
15
14
|
const typedConstants = pallet.constants.map((one) => ({
|
|
@@ -17,16 +16,16 @@ class ConstsGen extends generator_1.ApiGen {
|
|
|
17
16
|
type: this.typesGen.generateType(one.typeId, 1, true),
|
|
18
17
|
docs: one.docs,
|
|
19
18
|
}));
|
|
20
|
-
defTypeOut += (0,
|
|
21
|
-
defTypeOut += `${(0,
|
|
22
|
-
${typedConstants.map(({ name, type, docs }) => `${(0,
|
|
19
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s constants`);
|
|
20
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
21
|
+
${typedConstants.map(({ name, type, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${name}: ${type}`).join(',\n')}
|
|
23
22
|
|
|
24
|
-
${(0,
|
|
23
|
+
${(0, utils_js_1.commentBlock)('Generic pallet constant')}[name: string]: any,
|
|
25
24
|
},`;
|
|
26
25
|
}
|
|
27
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
28
|
-
const template = (0,
|
|
29
|
-
return (0,
|
|
26
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
27
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/consts.hbs');
|
|
28
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
30
29
|
}
|
|
31
30
|
}
|
|
32
31
|
exports.ConstsGen = ConstsGen;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ErrorsGen = void 0;
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
+
class ErrorsGen extends ApiGen_js_1.ApiGen {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
9
|
+
const { pallets } = this.metadata;
|
|
10
|
+
this.typesGen.clearCache();
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
|
|
12
|
+
let defTypeOut = '';
|
|
13
|
+
for (let pallet of pallets) {
|
|
14
|
+
const errorTypeId = pallet.error;
|
|
15
|
+
if (!errorTypeId) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const errorDefs = this.#getErrorDefs(errorTypeId);
|
|
19
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s errors`);
|
|
20
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
21
|
+
${errorDefs
|
|
22
|
+
.map(({ name, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletError<Rv>`)
|
|
23
|
+
.join(',\n')}
|
|
24
|
+
|
|
25
|
+
${(0, utils_js_1.commentBlock)('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
|
|
26
|
+
},`;
|
|
27
|
+
}
|
|
28
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
29
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/errors.hbs');
|
|
30
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
31
|
+
}
|
|
32
|
+
#getErrorDefs(errorTypeId) {
|
|
33
|
+
const def = this.metadata.types[errorTypeId];
|
|
34
|
+
(0, utils_1.assert)(def, `Error def not found for id ${errorTypeId}`);
|
|
35
|
+
const { type, value } = def.typeDef;
|
|
36
|
+
(0, utils_1.assert)(type === 'Enum', 'Invalid pallet error type!');
|
|
37
|
+
return value.members;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.ErrorsGen = ErrorsGen;
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.EventsGen = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
generate() {
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
+
class EventsGen extends ApiGen_js_1.ApiGen {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
10
9
|
const { pallets } = this.metadata;
|
|
11
10
|
this.typesGen.clearCache();
|
|
12
|
-
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
|
|
13
12
|
let defTypeOut = '';
|
|
14
13
|
for (let pallet of pallets) {
|
|
15
14
|
const eventTypeId = pallet.event;
|
|
@@ -18,8 +17,8 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
18
17
|
}
|
|
19
18
|
const eventDefs = this.#getEventDefs(eventTypeId);
|
|
20
19
|
const flatMembers = eventDefs.every((d) => d.fields.length === 0);
|
|
21
|
-
defTypeOut += (0,
|
|
22
|
-
defTypeOut += `${(0,
|
|
20
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s events`);
|
|
21
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
23
22
|
${eventDefs
|
|
24
23
|
.map((def) => {
|
|
25
24
|
const genericParts = [`'${pallet.name}'`, `'${def.name}'`];
|
|
@@ -32,21 +31,21 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
32
31
|
}
|
|
33
32
|
return { ...def, genericParts };
|
|
34
33
|
})
|
|
35
|
-
.map(({ name, docs, fields, genericParts }) => `${(0,
|
|
34
|
+
.map(({ name, docs, fields, genericParts }) => `${(0, utils_js_1.commentBlock)(docs)}${(0, utils_1.stringPascalCase)(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
|
|
36
35
|
.join(',\n')}
|
|
37
36
|
|
|
38
|
-
${(0,
|
|
37
|
+
${(0, utils_js_1.commentBlock)('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
|
|
39
38
|
},`;
|
|
40
39
|
}
|
|
41
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
42
|
-
const template = (0,
|
|
43
|
-
return (0,
|
|
40
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
41
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/events.hbs');
|
|
42
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
44
43
|
}
|
|
45
44
|
#getEventDefs(typeId) {
|
|
46
45
|
const def = this.metadata.types[typeId];
|
|
47
|
-
(0,
|
|
48
|
-
const {
|
|
49
|
-
(0,
|
|
46
|
+
(0, utils_1.assert)(def, `Event def not found for id ${typeId}`);
|
|
47
|
+
const { type, value } = def.typeDef;
|
|
48
|
+
(0, utils_1.assert)(type === 'Enum', 'Invalid pallet event type!');
|
|
50
49
|
return value.members;
|
|
51
50
|
}
|
|
52
51
|
#generateMemberDefType(fields) {
|
|
@@ -57,7 +56,7 @@ class EventsGen extends ApiGen_1.ApiGen {
|
|
|
57
56
|
return fields.length === 1
|
|
58
57
|
? this.typesGen.generateType(fields[0].typeId, 1, true)
|
|
59
58
|
: `[${fields
|
|
60
|
-
.map(({ typeId, docs }) => `${(0,
|
|
59
|
+
.map(({ typeId, docs }) => `${(0, utils_js_1.commentBlock)(docs)}${this.typesGen.generateType(typeId, 1, true)}`)
|
|
61
60
|
.join(', ')}]`;
|
|
62
61
|
}
|
|
63
62
|
else {
|
|
@@ -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;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonRpcGen = void 0;
|
|
4
|
+
const api_1 = require("@dedot/api");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
+
const HIDDEN_RPCS = [
|
|
8
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
9
|
+
'rpc_methods',
|
|
10
|
+
];
|
|
11
|
+
const ALIAS_RPCS = [
|
|
12
|
+
// TODO include these into the specs
|
|
13
|
+
'account_nextIndex',
|
|
14
|
+
'system_dryRunAt',
|
|
15
|
+
'state_callAt',
|
|
16
|
+
'state_getKeysPagedAt',
|
|
17
|
+
'state_getStorageAt',
|
|
18
|
+
'state_getStorageHashAt',
|
|
19
|
+
'state_getStorageSizeAt',
|
|
20
|
+
'chain_getRuntimeVersion',
|
|
21
|
+
'chain_getHead',
|
|
22
|
+
'chain_getFinalisedHead',
|
|
23
|
+
'childstate_getKeysPagedAt',
|
|
24
|
+
// Subscription pairs
|
|
25
|
+
'chain_subscribeRuntimeVersion',
|
|
26
|
+
'chain_unsubscribeRuntimeVersion',
|
|
27
|
+
'chain_unsubscribeNewHead',
|
|
28
|
+
'chain_subscribeNewHead',
|
|
29
|
+
'subscribe_newHead',
|
|
30
|
+
'unsubscribe_newHead',
|
|
31
|
+
'chain_subscribeFinalisedHeads',
|
|
32
|
+
'chain_unsubscribeFinalisedHeads',
|
|
33
|
+
];
|
|
34
|
+
class JsonRpcGen extends ApiGen_js_1.ApiGen {
|
|
35
|
+
typesGen;
|
|
36
|
+
rpcMethods;
|
|
37
|
+
constructor(typesGen, rpcMethods) {
|
|
38
|
+
super(typesGen);
|
|
39
|
+
this.typesGen = typesGen;
|
|
40
|
+
this.rpcMethods = rpcMethods;
|
|
41
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
42
|
+
rpcMethods.sort();
|
|
43
|
+
}
|
|
44
|
+
generate(useSubPaths = false) {
|
|
45
|
+
this.typesGen.clearCache();
|
|
46
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
|
|
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
|
+
const jsonRpcMethods = this.rpcMethods
|
|
52
|
+
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
53
|
+
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
54
|
+
.map((one) => `'${one}'`)
|
|
55
|
+
.join(' | ');
|
|
56
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, jsonRpcMethods }));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.JsonRpcGen = JsonRpcGen;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryGen = void 0;
|
|
4
|
+
const utils_1 = require("@dedot/utils");
|
|
5
|
+
const utils_js_1 = require("../../utils.js");
|
|
6
|
+
const ApiGen_js_1 = require("./ApiGen.js");
|
|
7
|
+
class QueryGen extends ApiGen_js_1.ApiGen {
|
|
8
|
+
generate(useSubPaths = false) {
|
|
9
|
+
const { pallets } = this.metadata;
|
|
10
|
+
this.typesGen.clearCache();
|
|
11
|
+
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
|
|
12
|
+
let defTypeOut = '';
|
|
13
|
+
for (let pallet of pallets) {
|
|
14
|
+
const storage = pallet.storage;
|
|
15
|
+
if (!storage)
|
|
16
|
+
continue;
|
|
17
|
+
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
18
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
|
|
19
|
+
if (keyTypeOut) {
|
|
20
|
+
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}, ${keyTypeOut}>`;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return `${(0, utils_js_1.commentBlock)(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}>`;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
defTypeOut += (0, utils_js_1.commentBlock)(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
27
|
+
defTypeOut += `${(0, utils_1.stringCamelCase)(pallet.name)}: {
|
|
28
|
+
${queryDefs.join(',\n')}
|
|
29
|
+
|
|
30
|
+
${(0, utils_js_1.commentBlock)('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
|
|
31
|
+
},`;
|
|
32
|
+
}
|
|
33
|
+
const importTypes = this.typesGen.typeImports.toImports({ useSubPaths });
|
|
34
|
+
const template = (0, utils_js_1.compileTemplate)('chaintypes/templates/query.hbs');
|
|
35
|
+
return (0, utils_js_1.beautifySourceCode)(template({ importTypes, defTypeOut }));
|
|
36
|
+
}
|
|
37
|
+
#generateEntry(entry) {
|
|
38
|
+
const { name, storageType, docs, modifier } = entry;
|
|
39
|
+
let valueType, keyTypeOut, keyTypeIn;
|
|
40
|
+
if (storageType.type === 'Plain') {
|
|
41
|
+
valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
|
|
42
|
+
}
|
|
43
|
+
else if (storageType.type === 'Map') {
|
|
44
|
+
valueType = this.typesGen.generateType(storageType.value.valueTypeId, 1, true);
|
|
45
|
+
keyTypeOut = this.typesGen.generateType(storageType.value.keyTypeId, 1, true);
|
|
46
|
+
keyTypeIn = this.typesGen.generateType(storageType.value.keyTypeId, 1);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw Error('Invalid entry type!');
|
|
50
|
+
}
|
|
51
|
+
const isOptional = modifier === 'Optional';
|
|
52
|
+
valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
|
|
53
|
+
docs.push('\n');
|
|
54
|
+
if (keyTypeIn) {
|
|
55
|
+
docs.push(`@param {${keyTypeIn}} arg`);
|
|
56
|
+
}
|
|
57
|
+
docs.push(`@param {Callback<${valueType}> =} callback`);
|
|
58
|
+
return {
|
|
59
|
+
name: (0, utils_1.normalizeName)(name),
|
|
60
|
+
valueType,
|
|
61
|
+
keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
|
|
62
|
+
keyTypeOut,
|
|
63
|
+
docs,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
exports.QueryGen = QueryGen;
|