@dedot/codegen 0.0.1-next.41a5fa17.16 → 0.0.1-next.4deddfff.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/generator/ConstsGen.js +3 -3
- package/cjs/generator/ErrorsGen.js +4 -4
- package/cjs/generator/EventsGen.js +4 -4
- package/cjs/generator/IndexGen.js +5 -6
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +19 -10
- package/cjs/generator/RuntimeApisGen.js +91 -16
- package/cjs/generator/TxGen.js +9 -7
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +2 -8
- package/cjs/generator/index.js +1 -1
- package/cjs/generator/known-codecs.js +1 -1
- package/cjs/index.js +24 -24
- package/cjs/templates/consts.hbs +1 -1
- package/cjs/templates/errors.hbs +1 -1
- package/cjs/templates/events.hbs +1 -1
- package/cjs/templates/index.hbs +15 -10
- package/cjs/templates/json-rpc.hbs +5 -0
- package/cjs/templates/query.hbs +1 -1
- package/cjs/templates/runtime.hbs +1 -1
- package/cjs/templates/tx.hbs +1 -1
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +2 -2
- package/generator/ErrorsGen.js +4 -4
- package/generator/EventsGen.js +4 -4
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +5 -6
- package/generator/{RpcGen.d.ts → JsonRpcGen.d.ts} +1 -4
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +18 -9
- package/generator/RuntimeApisGen.d.ts +4 -4
- package/generator/RuntimeApisGen.js +91 -16
- package/generator/TxGen.js +9 -7
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +1 -1
- package/generator/TypesGen.js +2 -8
- package/generator/index.d.ts +1 -1
- package/generator/index.js +1 -1
- package/generator/known-codecs.js +1 -1
- package/index.d.ts +2 -3
- package/index.js +23 -23
- package/package.json +11 -15
- package/templates/consts.hbs +1 -1
- package/templates/errors.hbs +1 -1
- package/templates/events.hbs +1 -1
- package/templates/index.hbs +15 -10
- package/templates/json-rpc.hbs +5 -0
- package/templates/query.hbs +1 -1
- package/templates/runtime.hbs +1 -1
- package/templates/tx.hbs +1 -1
- package/cjs/genSupportedChainTypes.js +0 -83
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/generator/known-types.js +0 -33
- package/cjs/packageInfo.js +0 -5
- package/cjs/templates/rpc.hbs +0 -7
- package/cjs/types.js +0 -2
- package/genSupportedChainTypes.d.ts +0 -1
- package/genSupportedChainTypes.js +0 -78
- package/generator/RpcGen.js +0 -171
- package/generator/known-types.d.ts +0 -6
- package/generator/known-types.js +0 -30
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -6
- package/types.js +0 -1
package/cjs/templates/events.hbs
CHANGED
package/cjs/templates/index.hbs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// Generated by @dedot/codegen
|
|
2
2
|
|
|
3
|
-
import { GenericSubstrateApi } from '@dedot/types';
|
|
3
|
+
import { GenericSubstrateApi, RpcLegacy, RpcV2, RpcVersion } from '@dedot/types';
|
|
4
4
|
import { ChainConsts } from './consts';
|
|
5
5
|
import { ChainStorage } from './query';
|
|
6
|
-
import {
|
|
6
|
+
import { ChainJsonRpcApis } from './json-rpc';
|
|
7
7
|
import { ChainErrors } from './errors';
|
|
8
8
|
import { ChainEvents } from './events';
|
|
9
9
|
import { RuntimeApis } from './runtime';
|
|
@@ -11,12 +11,17 @@ import { ChainTx } from './tx';
|
|
|
11
11
|
|
|
12
12
|
export * from './types';
|
|
13
13
|
|
|
14
|
-
export interface {{{interfaceName}}}Api extends GenericSubstrateApi {
|
|
15
|
-
rpc:
|
|
16
|
-
consts: ChainConsts
|
|
17
|
-
query: ChainStorage
|
|
18
|
-
errors: ChainErrors
|
|
19
|
-
events: ChainEvents
|
|
20
|
-
call: RuntimeApis
|
|
21
|
-
tx: ChainTx
|
|
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>;
|
|
22
27
|
}
|
package/cjs/templates/query.hbs
CHANGED
package/cjs/templates/tx.hbs
CHANGED
package/generator/ApiGen.d.ts
CHANGED
|
@@ -13,14 +13,14 @@ export declare abstract class ApiGen {
|
|
|
13
13
|
type: {
|
|
14
14
|
tag: "Struct";
|
|
15
15
|
value: {
|
|
16
|
-
fields: import("
|
|
16
|
+
fields: import("dedot").Field[];
|
|
17
17
|
};
|
|
18
18
|
} | {
|
|
19
19
|
tag: "Enum";
|
|
20
20
|
value: {
|
|
21
21
|
members: {
|
|
22
22
|
name: string;
|
|
23
|
-
fields: import("
|
|
23
|
+
fields: import("dedot").Field[];
|
|
24
24
|
index: number;
|
|
25
25
|
docs: string[];
|
|
26
26
|
}[];
|
|
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
|
|
|
134
134
|
}>;
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
|
-
get registry(): import("
|
|
137
|
+
get registry(): import("dedot").PortableRegistry;
|
|
138
138
|
}
|
package/generator/ConstsGen.d.ts
CHANGED
package/generator/ConstsGen.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
-
import { ApiGen } from '
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class ConstsGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
|
-
this.typesGen.typeImports.addKnownType('GenericChainConsts');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainConsts', 'RpcVersion');
|
|
9
9
|
let defTypeOut = '';
|
|
10
10
|
for (let pallet of pallets) {
|
|
11
11
|
const typedConstants = pallet.constants.map((one) => ({
|
package/generator/ErrorsGen.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
1
2
|
import { ApiGen } from './ApiGen.js';
|
|
2
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
-
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
4
4
|
export class ErrorsGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
|
-
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainErrors', 'GenericPalletError', 'RpcVersion');
|
|
9
9
|
let defTypeOut = '';
|
|
10
10
|
for (let pallet of pallets) {
|
|
11
11
|
const errorTypeId = pallet.error;
|
|
@@ -16,10 +16,10 @@ export class ErrorsGen extends ApiGen {
|
|
|
16
16
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s errors`);
|
|
17
17
|
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
18
18
|
${errorDefs
|
|
19
|
-
.map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError
|
|
19
|
+
.map(({ name, docs }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletError<Rv>`)
|
|
20
20
|
.join(',\n')}
|
|
21
21
|
|
|
22
|
-
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError
|
|
22
|
+
${commentBlock('Generic pallet error')}[error: string]: GenericPalletError<Rv>,
|
|
23
23
|
},`;
|
|
24
24
|
}
|
|
25
25
|
const importTypes = this.typesGen.typeImports.toImports();
|
package/generator/EventsGen.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
+
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
1
2
|
import { ApiGen } from './ApiGen.js';
|
|
2
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
|
-
import { assert, stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
4
4
|
export class EventsGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
|
-
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainEvents', 'GenericPalletEvent', 'RpcVersion');
|
|
9
9
|
let defTypeOut = '';
|
|
10
10
|
for (let pallet of pallets) {
|
|
11
11
|
const eventTypeId = pallet.event;
|
|
@@ -28,10 +28,10 @@ export class EventsGen extends ApiGen {
|
|
|
28
28
|
}
|
|
29
29
|
return { ...def, genericParts };
|
|
30
30
|
})
|
|
31
|
-
.map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent
|
|
31
|
+
.map(({ name, docs, fields, genericParts }) => `${commentBlock(docs)}${stringPascalCase(name)}: GenericPalletEvent<Rv, ${genericParts.join(', ')}>`)
|
|
32
32
|
.join(',\n')}
|
|
33
33
|
|
|
34
|
-
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent
|
|
34
|
+
${commentBlock('Generic pallet event')}[prop: string]: GenericPalletEvent<Rv>,
|
|
35
35
|
},`;
|
|
36
36
|
}
|
|
37
37
|
const importTypes = this.typesGen.typeImports.toImports();
|
package/generator/IndexGen.d.ts
CHANGED
package/generator/IndexGen.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
2
1
|
import { stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
3
3
|
export class IndexGen {
|
|
4
|
-
|
|
5
|
-
constructor(
|
|
6
|
-
this.
|
|
4
|
+
chain;
|
|
5
|
+
constructor(chain) {
|
|
6
|
+
this.chain = chain;
|
|
7
7
|
}
|
|
8
8
|
async generate() {
|
|
9
|
-
const
|
|
10
|
-
const interfaceName = stringPascalCase(chain);
|
|
9
|
+
const interfaceName = stringPascalCase(this.chain);
|
|
11
10
|
const template = compileTemplate('index.hbs');
|
|
12
11
|
return beautifySourceCode(template({ interfaceName }));
|
|
13
12
|
}
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { ApiGen, TypesGen } from '../generator/index.js';
|
|
2
|
-
export declare class
|
|
3
|
-
#private;
|
|
2
|
+
export declare class JsonRpcGen extends ApiGen {
|
|
4
3
|
readonly typesGen: TypesGen;
|
|
5
4
|
readonly rpcMethods: string[];
|
|
6
5
|
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
7
6
|
generate(): Promise<string>;
|
|
8
|
-
addTypeImport(type: string | string[], toTypeIn?: boolean): void;
|
|
9
|
-
getGeneratedTypeName(type: string, toTypeIn?: boolean): string;
|
|
10
7
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { subscriptionsInfo } from '@dedot/specs';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
3
|
+
import { beautifySourceCode, compileTemplate } from './utils.js';
|
|
4
|
+
const HIDDEN_RPCS = [
|
|
5
|
+
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
6
|
+
'rpc_methods',
|
|
7
|
+
];
|
|
8
|
+
const ALIAS_RPCS = [
|
|
9
|
+
// TODO include these into the specs
|
|
10
|
+
'account_nextIndex',
|
|
11
|
+
'system_dryRunAt',
|
|
12
|
+
'state_callAt',
|
|
13
|
+
'state_getKeysPagedAt',
|
|
14
|
+
'state_getStorageAt',
|
|
15
|
+
'state_getStorageHashAt',
|
|
16
|
+
'state_getStorageSizeAt',
|
|
17
|
+
'chain_getRuntimeVersion',
|
|
18
|
+
'chain_getHead',
|
|
19
|
+
'chain_getFinalisedHead',
|
|
20
|
+
'childstate_getKeysPagedAt',
|
|
21
|
+
// Subscription pairs
|
|
22
|
+
'chain_subscribeRuntimeVersion',
|
|
23
|
+
'chain_unsubscribeRuntimeVersion',
|
|
24
|
+
'chain_unsubscribeNewHead',
|
|
25
|
+
'chain_subscribeNewHead',
|
|
26
|
+
'subscribe_newHead',
|
|
27
|
+
'unsubscribe_newHead',
|
|
28
|
+
'chain_subscribeFinalisedHeads',
|
|
29
|
+
'chain_unsubscribeFinalisedHeads',
|
|
30
|
+
];
|
|
31
|
+
export class JsonRpcGen extends ApiGen {
|
|
32
|
+
typesGen;
|
|
33
|
+
rpcMethods;
|
|
34
|
+
constructor(typesGen, rpcMethods) {
|
|
35
|
+
super(typesGen);
|
|
36
|
+
this.typesGen = typesGen;
|
|
37
|
+
this.rpcMethods = rpcMethods;
|
|
38
|
+
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
39
|
+
rpcMethods.sort();
|
|
40
|
+
}
|
|
41
|
+
generate() {
|
|
42
|
+
this.typesGen.clearCache();
|
|
43
|
+
this.typesGen.typeImports.addKnownType('GenericJsonRpcApis', 'RpcVersion');
|
|
44
|
+
this.typesGen.typeImports.addSpecType('JsonRpcApis');
|
|
45
|
+
const toExclude = Object.values(subscriptionsInfo).flat();
|
|
46
|
+
const importTypes = this.typesGen.typeImports.toImports();
|
|
47
|
+
const template = compileTemplate('json-rpc.hbs');
|
|
48
|
+
const jsonRpcMethods = this.rpcMethods
|
|
49
|
+
.filter((one) => !ALIAS_RPCS.includes(one)) // exclude alias rpcs
|
|
50
|
+
.filter((one) => !toExclude.includes(one)) // exclude unsubscribe methods
|
|
51
|
+
.map((one) => `'${one}'`)
|
|
52
|
+
.join(' | ');
|
|
53
|
+
return beautifySourceCode(template({ importTypes, jsonRpcMethods }));
|
|
54
|
+
}
|
|
55
|
+
}
|
package/generator/QueryGen.d.ts
CHANGED
package/generator/QueryGen.js
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
import { normalizeName, stringCamelCase } from '@dedot/utils';
|
|
2
|
-
import { ApiGen } from '
|
|
2
|
+
import { ApiGen } from './ApiGen.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
4
4
|
export class QueryGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
6
6
|
const { pallets } = this.metadata;
|
|
7
7
|
this.typesGen.clearCache();
|
|
8
|
-
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback');
|
|
8
|
+
this.typesGen.typeImports.addKnownType('GenericChainStorage', 'GenericStorageQuery', 'Callback', 'RpcVersion');
|
|
9
9
|
let defTypeOut = '';
|
|
10
10
|
for (let pallet of pallets) {
|
|
11
11
|
const storage = pallet.storage;
|
|
12
12
|
if (!storage)
|
|
13
13
|
continue;
|
|
14
14
|
const queries = storage.entries.map((one) => this.#generateEntry(one));
|
|
15
|
-
const queryDefs = queries.map(({ name, valueType, keyType, docs
|
|
15
|
+
const queryDefs = queries.map(({ name, valueType, keyType, docs, keyTypeOut }) => {
|
|
16
|
+
if (keyTypeOut) {
|
|
17
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}, ${keyTypeOut}>`;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
return `${commentBlock(docs)}${name}: GenericStorageQuery<Rv, (${keyType}) => ${valueType}>`;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
16
23
|
defTypeOut += commentBlock(`Pallet \`${pallet.name}\`'s storage queries`);
|
|
17
24
|
defTypeOut += `${stringCamelCase(pallet.name)}: {
|
|
18
25
|
${queryDefs.join(',\n')}
|
|
19
26
|
|
|
20
|
-
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery
|
|
27
|
+
${commentBlock('Generic pallet storage query')}[storage: string]: GenericStorageQuery<Rv>;
|
|
21
28
|
},`;
|
|
22
29
|
}
|
|
23
30
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
@@ -26,13 +33,14 @@ export class QueryGen extends ApiGen {
|
|
|
26
33
|
}
|
|
27
34
|
#generateEntry(entry) {
|
|
28
35
|
const { name, type, docs, modifier } = entry;
|
|
29
|
-
let valueType,
|
|
36
|
+
let valueType, keyTypeOut, keyTypeIn;
|
|
30
37
|
if (type.tag === 'Plain') {
|
|
31
38
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
32
39
|
}
|
|
33
40
|
else if (type.tag === 'Map') {
|
|
34
41
|
valueType = this.typesGen.generateType(type.value.valueTypeId, 1, true);
|
|
35
|
-
|
|
42
|
+
keyTypeOut = this.typesGen.generateType(type.value.keyTypeId, 1, true);
|
|
43
|
+
keyTypeIn = this.typesGen.generateType(type.value.keyTypeId, 1);
|
|
36
44
|
}
|
|
37
45
|
else {
|
|
38
46
|
throw Error('Invalid entry type!');
|
|
@@ -40,14 +48,15 @@ export class QueryGen extends ApiGen {
|
|
|
40
48
|
const isOptional = modifier === 'Optional';
|
|
41
49
|
valueType = `${valueType}${isOptional ? ' | undefined' : ''}`;
|
|
42
50
|
docs.push('\n');
|
|
43
|
-
if (
|
|
44
|
-
docs.push(`@param {${
|
|
51
|
+
if (keyTypeIn) {
|
|
52
|
+
docs.push(`@param {${keyTypeIn}} arg`);
|
|
45
53
|
}
|
|
46
54
|
docs.push(`@param {Callback<${valueType}> =} callback`);
|
|
47
55
|
return {
|
|
48
56
|
name: normalizeName(name),
|
|
49
57
|
valueType,
|
|
50
|
-
keyType:
|
|
58
|
+
keyType: keyTypeIn ? `arg: ${keyTypeIn}` : '',
|
|
59
|
+
keyTypeOut,
|
|
51
60
|
docs,
|
|
52
61
|
};
|
|
53
62
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { ApiGen } from './ApiGen.js';
|
|
1
2
|
import { TypesGen } from './TypesGen.js';
|
|
2
|
-
|
|
3
|
-
export declare class RuntimeApisGen extends RpcGen {
|
|
3
|
+
export declare class RuntimeApisGen extends ApiGen {
|
|
4
4
|
#private;
|
|
5
5
|
readonly typesGen: TypesGen;
|
|
6
|
-
readonly runtimeApis:
|
|
7
|
-
constructor(typesGen: TypesGen, runtimeApis:
|
|
6
|
+
readonly runtimeApis: Record<string, number>;
|
|
7
|
+
constructor(typesGen: TypesGen, runtimeApis: Record<string, number>);
|
|
8
8
|
generate(): Promise<string>;
|
|
9
9
|
}
|
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
|
-
import { beautifySourceCode, commentBlock, compileTemplate } from './utils.js';
|
|
3
2
|
import { calcRuntimeApiHash, stringSnakeCase, stringCamelCase } from '@dedot/utils';
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
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
|
+
export class RuntimeApisGen extends ApiGen {
|
|
6
7
|
typesGen;
|
|
7
8
|
runtimeApis;
|
|
8
9
|
constructor(typesGen, runtimeApis) {
|
|
9
|
-
super(typesGen
|
|
10
|
+
super(typesGen);
|
|
10
11
|
this.typesGen = typesGen;
|
|
11
12
|
this.runtimeApis = runtimeApis;
|
|
12
13
|
}
|
|
13
14
|
generate() {
|
|
14
15
|
this.typesGen.clearCache();
|
|
15
|
-
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod');
|
|
16
|
+
this.typesGen.typeImports.addKnownType('GenericRuntimeApis', 'GenericRuntimeApiMethod', 'RpcVersion');
|
|
16
17
|
let runtimeCallsOut = '';
|
|
17
18
|
if (this.metadata.apis.length > 0) {
|
|
18
19
|
this.metadata.apis.forEach((runtimeApi) => {
|
|
@@ -21,7 +22,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
21
22
|
runtimeCallsOut += `${stringCamelCase(runtimeApiName)}: {
|
|
22
23
|
${methods.map((method) => this.#generateMethodDef(runtimeApiName, method)).join('\n')}
|
|
23
24
|
|
|
24
|
-
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
25
|
+
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
|
|
25
26
|
}`;
|
|
26
27
|
});
|
|
27
28
|
}
|
|
@@ -38,7 +39,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
38
39
|
}))
|
|
39
40
|
.join('\n')}
|
|
40
41
|
|
|
41
|
-
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod
|
|
42
|
+
${commentBlock('Generic runtime api call')}[method: string]: GenericRuntimeApiMethod<Rv>
|
|
42
43
|
}`;
|
|
43
44
|
});
|
|
44
45
|
}
|
|
@@ -56,25 +57,25 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
56
57
|
const { docs = [], params, type, runtimeApiName, methodName } = spec;
|
|
57
58
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
58
59
|
const defaultDocs = [`@callname: ${callName}`];
|
|
59
|
-
this
|
|
60
|
-
params.forEach(({ type }) => this
|
|
60
|
+
this.#addTypeImport(type, false);
|
|
61
|
+
params.forEach(({ type }) => this.#addTypeImport(type));
|
|
61
62
|
const typedParams = params.map((param, idx) => ({
|
|
62
63
|
...param,
|
|
63
64
|
isOptional: this.#isOptionalParam(params, param.type, idx),
|
|
64
|
-
plainType: this
|
|
65
|
+
plainType: this.#getGeneratedTypeName(param.type),
|
|
65
66
|
}));
|
|
66
67
|
const paramsOut = typedParams
|
|
67
68
|
.map(({ name, isOptional, plainType }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${plainType}`)
|
|
68
69
|
.join(', ');
|
|
69
|
-
const typeOut = this
|
|
70
|
-
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
70
|
+
const typeOut = this.#getGeneratedTypeName(type, false);
|
|
71
|
+
return `${commentBlock(docs, '\n', defaultDocs, typedParams.map(({ plainType, name }) => `@param {${plainType}} ${name}`))}${methodName}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
|
|
71
72
|
}
|
|
72
73
|
#generateMethodDef(runtimeApiName, methodDef) {
|
|
73
74
|
const { name: methodName, inputs, output, docs } = methodDef;
|
|
74
75
|
const callName = `${runtimeApiName}_${stringSnakeCase(methodName)}`;
|
|
75
76
|
const defaultDocs = [`@callname: ${callName}`];
|
|
76
77
|
const typeOut = this.typesGen.generateType(output, 1, true);
|
|
77
|
-
this
|
|
78
|
+
this.#addTypeImport(typeOut, false);
|
|
78
79
|
const typedInputs = inputs
|
|
79
80
|
.map((input, idx) => ({
|
|
80
81
|
...input,
|
|
@@ -84,14 +85,14 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
84
85
|
...input,
|
|
85
86
|
isOptional: this.#isOptionalParam(inputs, input.type, idx),
|
|
86
87
|
}));
|
|
87
|
-
this
|
|
88
|
+
this.#addTypeImport(typedInputs.map((t) => t.type));
|
|
88
89
|
const paramsOut = typedInputs
|
|
89
90
|
.map(({ name, type, isOptional }) => `${stringCamelCase(name)}${isOptional ? '?' : ''}: ${type}`)
|
|
90
91
|
.join(', ');
|
|
91
|
-
return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<(${paramsOut}) => Promise<${typeOut}>>`;
|
|
92
|
+
return `${commentBlock(docs, '\n', defaultDocs, typedInputs.map(({ type, name }) => `@param {${type}} ${name}`))}${stringCamelCase(methodName)}: GenericRuntimeApiMethod<Rv, (${paramsOut}) => Promise<${typeOut}>>`;
|
|
92
93
|
}
|
|
93
94
|
#targetRuntimeApiSpecs() {
|
|
94
|
-
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
95
|
+
const specs = Object.entries(this.runtimeApis).map(([runtimeApiHash, version]) => {
|
|
95
96
|
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
96
97
|
if (!runtimeApiSpec)
|
|
97
98
|
return;
|
|
@@ -111,4 +112,78 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
111
112
|
const runtimeApiName = getRuntimeApiNames().find((one) => calcRuntimeApiHash(one) === runtimeApiHash);
|
|
112
113
|
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
113
114
|
};
|
|
115
|
+
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
116
|
+
#addTypeImport(type, toTypeIn = true) {
|
|
117
|
+
if (Array.isArray(type)) {
|
|
118
|
+
type.forEach((one) => this.#addTypeImport(one, toTypeIn));
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
type = type.trim();
|
|
122
|
+
if (isNativeType(type)) {
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
// Handle generic wrapper types
|
|
126
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
127
|
+
if (matchArray) {
|
|
128
|
+
const [_, $1, $2] = matchArray;
|
|
129
|
+
this.#addTypeImport($1, toTypeIn);
|
|
130
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
131
|
+
this.#addTypeImport($2, toTypeIn);
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
this.#addTypeImport($2.split(','), toTypeIn);
|
|
135
|
+
}
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
// Check tuple type
|
|
139
|
+
if (type.match(TUPLE_TYPE_REGEX)) {
|
|
140
|
+
this.#addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (type.includes(' | ')) {
|
|
144
|
+
this.#addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const codecType = this.#getCodecType(type, toTypeIn);
|
|
149
|
+
if (isNativeType(codecType))
|
|
150
|
+
return;
|
|
151
|
+
this.typesGen.typeImports.addCodecType(codecType);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
catch (e) { }
|
|
155
|
+
this.typesGen.addTypeImport(type);
|
|
156
|
+
}
|
|
157
|
+
#getGeneratedTypeName(type, toTypeIn = true) {
|
|
158
|
+
try {
|
|
159
|
+
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
160
|
+
if (matchArray) {
|
|
161
|
+
const [_, $1, $2] = matchArray;
|
|
162
|
+
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
163
|
+
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
164
|
+
return `${wrapperTypeName}<${this.#getGeneratedTypeName($2, toTypeIn)}>`;
|
|
165
|
+
}
|
|
166
|
+
const innerTypeNames = $2
|
|
167
|
+
.split(',')
|
|
168
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
169
|
+
.join(', ');
|
|
170
|
+
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
171
|
+
}
|
|
172
|
+
else if (type.match(TUPLE_TYPE_REGEX)) {
|
|
173
|
+
const innerTypeNames = type
|
|
174
|
+
.slice(1, -1)
|
|
175
|
+
.split(',')
|
|
176
|
+
.map((one) => this.#getGeneratedTypeName(one.trim(), toTypeIn))
|
|
177
|
+
.join(', ');
|
|
178
|
+
return `[${innerTypeNames}]`;
|
|
179
|
+
}
|
|
180
|
+
return this.#getCodecType(type, toTypeIn);
|
|
181
|
+
}
|
|
182
|
+
catch (e) { }
|
|
183
|
+
return type;
|
|
184
|
+
}
|
|
185
|
+
#getCodecType(type, toTypeIn = true) {
|
|
186
|
+
const { typeIn, typeOut } = findKnownCodecType(type);
|
|
187
|
+
return toTypeIn ? typeIn : typeOut;
|
|
188
|
+
}
|
|
114
189
|
}
|
package/generator/TxGen.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ApiGen } from '../generator/index.js';
|
|
2
1
|
import { stringCamelCase, stringPascalCase } from '@dedot/utils';
|
|
2
|
+
import { ApiGen } from '../generator/index.js';
|
|
3
3
|
import { beautifySourceCode, commentBlock, compileTemplate, isReservedWord } from './utils.js';
|
|
4
4
|
export class TxGen extends ApiGen {
|
|
5
5
|
generate() {
|
|
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', 'TransactionStatusLegacy', 'TransactionStatusV2');
|
|
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);
|
|
@@ -41,20 +41,22 @@ 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
50
|
const importTypes = this.typesGen.typeImports.toImports();
|
|
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, TransactionStatusV2>>
|
|
57
|
+
: ISubmittableExtrinsicLegacy<ISubmittableResult<FrameSystemEventRecord, TransactionStatusLegacy>>)
|
|
56
58
|
|
|
57
|
-
export type TxCall = (...args: any[]) => ChainSubmittableExtrinsic
|
|
59
|
+
export type TxCall<Rv extends RpcVersion> = (...args: any[]) => ChainSubmittableExtrinsic<Rv>;
|
|
58
60
|
`;
|
|
59
61
|
const template = compileTemplate('tx.hbs');
|
|
60
62
|
return beautifySourceCode(template({ importTypes, defTypes, txDefsOut }));
|
|
@@ -3,6 +3,7 @@ export declare class TypeImports {
|
|
|
3
3
|
portableTypes: Set<string>;
|
|
4
4
|
codecTypes: Set<string>;
|
|
5
5
|
knownTypes: Set<string>;
|
|
6
|
+
specTypes: Set<string>;
|
|
6
7
|
outTypes: Set<string>;
|
|
7
8
|
constructor();
|
|
8
9
|
clear(): void;
|
|
@@ -10,5 +11,6 @@ export declare class TypeImports {
|
|
|
10
11
|
addPortableType(...types: string[]): void;
|
|
11
12
|
addCodecType(...types: string[]): void;
|
|
12
13
|
addKnownType(...types: string[]): void;
|
|
14
|
+
addSpecType(...types: string[]): void;
|
|
13
15
|
addOutType(...types: string[]): void;
|
|
14
16
|
}
|
package/generator/TypeImports.js
CHANGED
|
@@ -5,30 +5,35 @@ export class TypeImports {
|
|
|
5
5
|
codecTypes;
|
|
6
6
|
// Known types that're not codecs or chain/portable types defined in @dedot/types
|
|
7
7
|
knownTypes;
|
|
8
|
+
specTypes;
|
|
8
9
|
// External types to define explicitly
|
|
9
10
|
outTypes;
|
|
10
11
|
constructor() {
|
|
11
12
|
this.portableTypes = new Set();
|
|
12
13
|
this.codecTypes = new Set();
|
|
13
14
|
this.knownTypes = new Set();
|
|
15
|
+
this.specTypes = new Set();
|
|
14
16
|
this.outTypes = new Set();
|
|
15
17
|
}
|
|
16
18
|
clear() {
|
|
17
19
|
this.portableTypes.clear();
|
|
18
20
|
this.codecTypes.clear();
|
|
19
21
|
this.knownTypes.clear();
|
|
22
|
+
this.specTypes.clear();
|
|
20
23
|
this.outTypes.clear();
|
|
21
24
|
}
|
|
22
25
|
toImports(...excludeModules) {
|
|
23
26
|
// TODO generate outTypes!
|
|
24
27
|
const toImports = [
|
|
25
28
|
[this.knownTypes, '@dedot/types'],
|
|
29
|
+
[this.specTypes, '@dedot/specs'],
|
|
26
30
|
[this.codecTypes, '@dedot/codecs'],
|
|
27
31
|
[this.portableTypes, './types'],
|
|
28
32
|
];
|
|
29
33
|
return toImports
|
|
30
34
|
.filter(([_, module]) => !excludeModules.includes(module))
|
|
31
35
|
.map(([types, module]) => this.#toImportLine(types, module))
|
|
36
|
+
.filter((line) => line.length > 0)
|
|
32
37
|
.join('\n');
|
|
33
38
|
}
|
|
34
39
|
#toImportLine(types, module) {
|
|
@@ -46,6 +51,9 @@ export class TypeImports {
|
|
|
46
51
|
addKnownType(...types) {
|
|
47
52
|
types.forEach((one) => this.knownTypes.add(one));
|
|
48
53
|
}
|
|
54
|
+
addSpecType(...types) {
|
|
55
|
+
types.forEach((one) => this.specTypes.add(one));
|
|
56
|
+
}
|
|
49
57
|
addOutType(...types) {
|
|
50
58
|
types.forEach((one) => this.outTypes.add(one));
|
|
51
59
|
}
|