@dedot/codegen 0.0.1-next.8d06d348.17 → 0.0.1-next.8f4b2c24.6
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 +9 -10
- package/cjs/generator/ErrorsGen.js +12 -13
- package/cjs/generator/EventsGen.js +13 -14
- package/cjs/generator/IndexGen.js +8 -9
- package/cjs/generator/JsonRpcGen.js +59 -0
- package/cjs/generator/QueryGen.js +23 -15
- package/cjs/generator/RuntimeApisGen.js +102 -28
- package/cjs/generator/TxGen.js +15 -15
- package/cjs/generator/TypeImports.js +8 -0
- package/cjs/generator/TypesGen.js +23 -32
- package/cjs/generator/index.js +10 -10
- package/cjs/{helpers/KnownCodecRegistry.js → generator/known-codecs.js} +56 -68
- package/cjs/generator/utils.js +30 -1
- package/cjs/index.js +32 -32
- package/cjs/templates/index.hbs +2 -2
- package/cjs/templates/json-rpc.hbs +5 -0
- package/generator/ApiGen.d.ts +4 -4
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +4 -5
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +3 -4
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +3 -4
- package/generator/IndexGen.d.ts +2 -3
- package/generator/IndexGen.js +6 -7
- package/generator/JsonRpcGen.d.ts +7 -0
- package/generator/JsonRpcGen.js +55 -0
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +19 -11
- package/generator/RuntimeApisGen.d.ts +5 -5
- package/generator/RuntimeApisGen.js +89 -15
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +3 -3
- package/generator/TypeImports.d.ts +2 -0
- package/generator/TypeImports.js +8 -0
- package/generator/TypesGen.d.ts +3 -5
- package/generator/TypesGen.js +12 -21
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- package/{helpers/KnownCodecRegistry.d.ts → generator/known-codecs.d.ts} +11 -10
- package/generator/known-codecs.js +120 -0
- package/generator/utils.d.ts +5 -0
- package/generator/utils.js +28 -0
- package/index.d.ts +2 -3
- package/index.js +23 -23
- package/package.json +7 -8
- package/templates/index.hbs +2 -2
- package/templates/json-rpc.hbs +5 -0
- package/cjs/genSupportedChainTypes.js +0 -79
- package/cjs/generator/RpcGen.js +0 -175
- package/cjs/helpers/index.js +0 -17
- 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 -74
- package/generator/RpcGen.d.ts +0 -10
- package/generator/RpcGen.js +0 -171
- package/helpers/KnownCodecRegistry.js +0 -135
- package/helpers/index.d.ts +0 -1
- package/helpers/index.js +0 -1
- package/packageInfo.d.ts +0 -4
- package/packageInfo.js +0 -2
- package/templates/rpc.hbs +0 -7
- package/types.d.ts +0 -14
- package/types.js +0 -1
package/generator/RpcGen.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { ApiGen, TypesGen } from '../generator';
|
|
2
|
-
export declare class RpcGen extends ApiGen {
|
|
3
|
-
#private;
|
|
4
|
-
readonly typesGen: TypesGen;
|
|
5
|
-
readonly rpcMethods: string[];
|
|
6
|
-
constructor(typesGen: TypesGen, rpcMethods: string[]);
|
|
7
|
-
generate(): Promise<string>;
|
|
8
|
-
addTypeImport(type: string | string[], toTypeIn?: boolean): void;
|
|
9
|
-
getGeneratedTypeName(type: string, toTypeIn?: boolean): string;
|
|
10
|
-
}
|
package/generator/RpcGen.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs';
|
|
2
|
-
import { isNativeType } from '@dedot/utils';
|
|
3
|
-
import { ApiGen } from '../generator';
|
|
4
|
-
import { beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX } from './utils';
|
|
5
|
-
const HIDDEN_RPCS = [
|
|
6
|
-
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
7
|
-
'rpc_methods',
|
|
8
|
-
];
|
|
9
|
-
export class RpcGen extends ApiGen {
|
|
10
|
-
typesGen;
|
|
11
|
-
rpcMethods;
|
|
12
|
-
constructor(typesGen, rpcMethods) {
|
|
13
|
-
super(typesGen);
|
|
14
|
-
this.typesGen = typesGen;
|
|
15
|
-
this.rpcMethods = rpcMethods;
|
|
16
|
-
HIDDEN_RPCS.filter((one) => !rpcMethods.includes(one)).forEach((one) => rpcMethods.push(one));
|
|
17
|
-
rpcMethods.sort();
|
|
18
|
-
}
|
|
19
|
-
generate() {
|
|
20
|
-
this.typesGen.clearCache();
|
|
21
|
-
this.typesGen.typeImports.addKnownType('GenericRpcCalls', 'Unsub', 'Callback', 'GenericRpcCall');
|
|
22
|
-
const specsByModule = this.rpcMethods
|
|
23
|
-
.filter((one) => !findAliasRpcSpec(one)) // we'll ignore alias rpc for now if defined in the specs! TODO should we generate alias rpc as well?
|
|
24
|
-
.filter((one) => !isUnsubscribeMethod(one)) // we'll ignore unsubscribe method as well
|
|
25
|
-
.map((one) => {
|
|
26
|
-
const spec = findRpcSpec(one);
|
|
27
|
-
if (spec) {
|
|
28
|
-
return spec;
|
|
29
|
-
}
|
|
30
|
-
const [module, ...methodParts] = one.split('_');
|
|
31
|
-
const method = methodParts.join('_');
|
|
32
|
-
return {
|
|
33
|
-
params: [],
|
|
34
|
-
type: 'GenericRpcCall',
|
|
35
|
-
module,
|
|
36
|
-
method,
|
|
37
|
-
};
|
|
38
|
-
})
|
|
39
|
-
.reduce((o, spec) => {
|
|
40
|
-
const { module, method } = spec;
|
|
41
|
-
// ignore if rpc name does not confront with the general convention
|
|
42
|
-
if (!module || !method) {
|
|
43
|
-
return o;
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
...o,
|
|
47
|
-
[module]: o[module] ? [...o[module], spec] : [spec],
|
|
48
|
-
};
|
|
49
|
-
}, {});
|
|
50
|
-
let rpcCallsOut = '';
|
|
51
|
-
Object.keys(specsByModule).forEach((module) => {
|
|
52
|
-
const specs = specsByModule[module];
|
|
53
|
-
// TODO add alias info to docs block!
|
|
54
|
-
rpcCallsOut += `${module}: {
|
|
55
|
-
${specs.map((spec) => this.#generateMethodDef(spec)).join(',\n')},
|
|
56
|
-
|
|
57
|
-
[method: string]: GenericRpcCall,
|
|
58
|
-
},`;
|
|
59
|
-
});
|
|
60
|
-
// TODO include & define external types
|
|
61
|
-
const importTypes = this.typesGen.typeImports.toImports();
|
|
62
|
-
const template = compileTemplate('rpc.hbs');
|
|
63
|
-
return beautifySourceCode(template({ importTypes, rpcCallsOut }));
|
|
64
|
-
}
|
|
65
|
-
#generateMethodDef(spec) {
|
|
66
|
-
const { name, type, module, method, docs = [], params, pubsub, deprecated } = spec;
|
|
67
|
-
const rpcName = name || `${module}_${method}`;
|
|
68
|
-
let defaultDocs = [`@rpcname: ${rpcName}`];
|
|
69
|
-
if (deprecated) {
|
|
70
|
-
defaultDocs.push(`@deprecated: ${deprecated}`);
|
|
71
|
-
}
|
|
72
|
-
if (type === 'GenericRpcCall' && params.length === 0) {
|
|
73
|
-
return `${commentBlock(defaultDocs)}${method}: GenericRpcCall`;
|
|
74
|
-
}
|
|
75
|
-
this.addTypeImport(type, false);
|
|
76
|
-
params.forEach(({ type, isScale }) => {
|
|
77
|
-
this.addTypeImport(type, !!isScale);
|
|
78
|
-
});
|
|
79
|
-
const typedParams = params.map((param) => ({
|
|
80
|
-
...param,
|
|
81
|
-
plainType: this.getGeneratedTypeName(param.type, !!param.isScale),
|
|
82
|
-
}));
|
|
83
|
-
const isSubscription = !!pubsub;
|
|
84
|
-
const paramsOut = typedParams.map(({ name, type, isOptional, isScale, plainType }) => `${name}${isOptional ? '?' : ''}: ${plainType}`);
|
|
85
|
-
const paramsDoc = typedParams.map(({ name, plainType }) => `@param {${plainType}} ${name}`);
|
|
86
|
-
const typeOut = this.getGeneratedTypeName(type, false);
|
|
87
|
-
if (isSubscription) {
|
|
88
|
-
defaultDocs.shift();
|
|
89
|
-
defaultDocs.unshift(`@pubsub: ${pubsub?.join(', ')}`);
|
|
90
|
-
paramsOut.push(`callback: Callback<${typeOut}>`);
|
|
91
|
-
return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<Unsub>>`;
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
return `${commentBlock(docs, '\n', defaultDocs, paramsDoc)}${method}: GenericRpcCall<(${paramsOut.join(', ')}) => Promise<${typeOut}>>`;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
// TODO check typeIn, typeOut if param type, or rpc type isScale
|
|
98
|
-
addTypeImport(type, toTypeIn = true) {
|
|
99
|
-
if (Array.isArray(type)) {
|
|
100
|
-
type.forEach((one) => this.addTypeImport(one, toTypeIn));
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
type = type.trim();
|
|
104
|
-
if (isNativeType(type)) {
|
|
105
|
-
return;
|
|
106
|
-
}
|
|
107
|
-
// Handle generic wrapper types
|
|
108
|
-
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
109
|
-
if (matchArray) {
|
|
110
|
-
const [_, $1, $2] = matchArray;
|
|
111
|
-
this.addTypeImport($1, toTypeIn);
|
|
112
|
-
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
113
|
-
this.addTypeImport($2, toTypeIn);
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
this.addTypeImport($2.split(','), toTypeIn);
|
|
117
|
-
}
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
// Check tuple type
|
|
121
|
-
if (type.match(TUPLE_TYPE_REGEX)) {
|
|
122
|
-
this.addTypeImport(type.slice(1, -1).split(','), toTypeIn);
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
if (type.includes(' | ')) {
|
|
126
|
-
this.addTypeImport(type.split(' | ').map((one) => one.trim()), toTypeIn);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
try {
|
|
130
|
-
const codecType = this.#getCodecType(type, toTypeIn);
|
|
131
|
-
if (isNativeType(codecType))
|
|
132
|
-
return;
|
|
133
|
-
this.typesGen.typeImports.addCodecType(codecType);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
catch (e) { }
|
|
137
|
-
this.typesGen.addTypeImport(type);
|
|
138
|
-
}
|
|
139
|
-
getGeneratedTypeName(type, toTypeIn = true) {
|
|
140
|
-
try {
|
|
141
|
-
const matchArray = type.match(WRAPPER_TYPE_REGEX);
|
|
142
|
-
if (matchArray) {
|
|
143
|
-
const [_, $1, $2] = matchArray;
|
|
144
|
-
const wrapperTypeName = this.#getCodecType($1, toTypeIn);
|
|
145
|
-
if ($2.match(WRAPPER_TYPE_REGEX) || $2.match(TUPLE_TYPE_REGEX)) {
|
|
146
|
-
return `${wrapperTypeName}<${this.getGeneratedTypeName($2, toTypeIn)}>`;
|
|
147
|
-
}
|
|
148
|
-
const innerTypeNames = $2
|
|
149
|
-
.split(',')
|
|
150
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
151
|
-
.join(', ');
|
|
152
|
-
return `${wrapperTypeName}<${innerTypeNames}>`;
|
|
153
|
-
}
|
|
154
|
-
else if (type.match(TUPLE_TYPE_REGEX)) {
|
|
155
|
-
const innerTypeNames = type
|
|
156
|
-
.slice(1, -1)
|
|
157
|
-
.split(',')
|
|
158
|
-
.map((one) => this.getGeneratedTypeName(one.trim(), toTypeIn))
|
|
159
|
-
.join(', ');
|
|
160
|
-
return `[${innerTypeNames}]`;
|
|
161
|
-
}
|
|
162
|
-
return this.#getCodecType(type, toTypeIn);
|
|
163
|
-
}
|
|
164
|
-
catch (e) { }
|
|
165
|
-
return type;
|
|
166
|
-
}
|
|
167
|
-
#getCodecType(type, toTypeIn = true) {
|
|
168
|
-
const { typeIn, typeOut } = this.typesGen.knownCodecRegistry.findCodecType(type);
|
|
169
|
-
return toTypeIn ? typeIn : typeOut;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import * as $ from '@dedot/shape';
|
|
2
|
-
import * as Codecs from '@dedot/codecs';
|
|
3
|
-
import { $AccountId20, $AccountId32, $Bytes, $ConsensusEngineId, $Era, $EthereumAddress, $MultiAddress, $OpaqueExtrinsic, $PrefixedStorageKey, $RawBytes, $StorageData, $StorageKey, $UncheckedExtrinsic, } from '@dedot/codecs';
|
|
4
|
-
export const normalizeCodecName = (name) => {
|
|
5
|
-
return name.startsWith('$') ? name : `$${name}`;
|
|
6
|
-
};
|
|
7
|
-
// Known paths for codecs (primitives) that are shared between
|
|
8
|
-
// different substrate-based blockchains
|
|
9
|
-
const KNOWN_PATHS = [
|
|
10
|
-
'sp_core::crypto::AccountId32',
|
|
11
|
-
'sp_runtime::generic::era::Era',
|
|
12
|
-
'sp_runtime::multiaddress::MultiAddress',
|
|
13
|
-
/^sp_runtime::DispatchError$/,
|
|
14
|
-
'sp_runtime::ModuleError',
|
|
15
|
-
'sp_runtime::TokenError',
|
|
16
|
-
'sp_arithmetic::ArithmeticError',
|
|
17
|
-
'sp_runtime::TransactionalError',
|
|
18
|
-
'frame_support::dispatch::DispatchInfo',
|
|
19
|
-
'frame_system::Phase',
|
|
20
|
-
'sp_version::RuntimeVersion',
|
|
21
|
-
'fp_account::AccountId20',
|
|
22
|
-
'account::AccountId20',
|
|
23
|
-
'polkadot_runtime_common::claims::EthereumAddress',
|
|
24
|
-
'pallet_identity::types::Data',
|
|
25
|
-
'sp_runtime::generic::digest::Digest',
|
|
26
|
-
'sp_runtime::generic::digest::DigestItem',
|
|
27
|
-
'sp_runtime::generic::header::Header',
|
|
28
|
-
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
29
|
-
/^primitive_types::\w+$/,
|
|
30
|
-
/^sp_arithmetic::per_things::\w+$/,
|
|
31
|
-
/^sp_arithmetic::fixed_point::\w+$/,
|
|
32
|
-
];
|
|
33
|
-
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
34
|
-
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
35
|
-
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
36
|
-
/**
|
|
37
|
-
* Collection of codec types with loose input types
|
|
38
|
-
*
|
|
39
|
-
* Loose codecs are codecs with different typeIn & typeOut,
|
|
40
|
-
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
41
|
-
*
|
|
42
|
-
* This registry keep track the list of codecs which follow this convention
|
|
43
|
-
*/
|
|
44
|
-
export const looseTypeCodecs = {
|
|
45
|
-
$AccountId20,
|
|
46
|
-
$EthereumAddress,
|
|
47
|
-
$AccountId32,
|
|
48
|
-
$ConsensusEngineId,
|
|
49
|
-
$StorageKey,
|
|
50
|
-
$StorageData,
|
|
51
|
-
$PrefixedStorageKey,
|
|
52
|
-
$Bytes,
|
|
53
|
-
$RawBytes,
|
|
54
|
-
$MultiAddress,
|
|
55
|
-
$OpaqueExtrinsic,
|
|
56
|
-
$UncheckedExtrinsic,
|
|
57
|
-
$Era,
|
|
58
|
-
};
|
|
59
|
-
/**
|
|
60
|
-
* A codec registry for both known and portable codecs,
|
|
61
|
-
*/
|
|
62
|
-
export class KnownCodecRegistry {
|
|
63
|
-
findCodec(name) {
|
|
64
|
-
return this.#findKnownCodec(name);
|
|
65
|
-
}
|
|
66
|
-
findCodecType(name) {
|
|
67
|
-
const normalizedName = normalizeCodecName(name);
|
|
68
|
-
const $knownCodec = looseTypeCodecs[normalizedName];
|
|
69
|
-
if ($knownCodec) {
|
|
70
|
-
return {
|
|
71
|
-
name: normalizedName,
|
|
72
|
-
$codec: $knownCodec,
|
|
73
|
-
typeIn: `${name}Like`,
|
|
74
|
-
typeOut: name,
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
const $codec = this.findCodec(name);
|
|
78
|
-
if ($codec.nativeType && $[name]) {
|
|
79
|
-
return {
|
|
80
|
-
name: normalizedName,
|
|
81
|
-
$codec,
|
|
82
|
-
typeIn: $codec.nativeType,
|
|
83
|
-
typeOut: $codec.nativeType,
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
name: normalizedName,
|
|
88
|
-
$codec,
|
|
89
|
-
typeIn: name,
|
|
90
|
-
typeOut: name,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
#findKnownCodec(typeName) {
|
|
94
|
-
// @ts-ignore
|
|
95
|
-
const $codec = this.#findKnownWrapperCodec(typeName) || Codecs[normalizeCodecName(typeName)] || $[typeName];
|
|
96
|
-
if (!$codec) {
|
|
97
|
-
throw new Error(`Unsupported codec - ${typeName}`);
|
|
98
|
-
}
|
|
99
|
-
// This only works with top-level $.deferred codecs
|
|
100
|
-
// We should make it work for nested $.deferred codecs as well
|
|
101
|
-
if ($codec.metadata && $codec.metadata[0].name === '$.deferred') {
|
|
102
|
-
const getShape = $codec.metadata[0].args[0];
|
|
103
|
-
return $.deferred(() => getShape(this));
|
|
104
|
-
}
|
|
105
|
-
return $codec;
|
|
106
|
-
}
|
|
107
|
-
#findKnownWrapperCodec(typeName) {
|
|
108
|
-
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
109
|
-
if (matchNames) {
|
|
110
|
-
const [_, wrapper, inner] = matchNames;
|
|
111
|
-
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
112
|
-
// @ts-ignore
|
|
113
|
-
const $Wrapper = $[wrapper];
|
|
114
|
-
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
115
|
-
return $Wrapper(this.#findKnownWrapperCodec(inner));
|
|
116
|
-
}
|
|
117
|
-
const $inners = inner.split(',').map((one) => this.#findKnownCodec(one.trim()));
|
|
118
|
-
return $Wrapper(...$inners);
|
|
119
|
-
}
|
|
120
|
-
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
121
|
-
}
|
|
122
|
-
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
123
|
-
const $inner = typeName
|
|
124
|
-
.slice(1, -1)
|
|
125
|
-
.split(',')
|
|
126
|
-
.filter((x) => x)
|
|
127
|
-
.map((one) => this.#findKnownCodec(one.trim()));
|
|
128
|
-
return $.Tuple(...$inner);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
isKnownType(path) {
|
|
132
|
-
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
133
|
-
return KNOWN_PATHS.some((one) => joinedPath.match(one));
|
|
134
|
-
}
|
|
135
|
-
}
|
package/helpers/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './KnownCodecRegistry';
|
package/helpers/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './KnownCodecRegistry';
|
package/packageInfo.d.ts
DELETED
package/packageInfo.js
DELETED
package/templates/rpc.hbs
DELETED
package/types.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AnyShape } from '@dedot/shape';
|
|
2
|
-
export type NetworkInfo = {
|
|
3
|
-
chain: string;
|
|
4
|
-
endpoint?: string;
|
|
5
|
-
metadataHex?: string;
|
|
6
|
-
rpcMethods?: string[];
|
|
7
|
-
};
|
|
8
|
-
export type CodecName = `$${string}`;
|
|
9
|
-
export interface CodecType {
|
|
10
|
-
name: CodecName;
|
|
11
|
-
$codec: AnyShape;
|
|
12
|
-
typeIn: string;
|
|
13
|
-
typeOut: string;
|
|
14
|
-
}
|
package/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|