@dedot/codegen 0.0.1-alpha.25 → 0.0.1-alpha.27
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/genSupportedChainTypes.js +5 -1
- package/cjs/generator/RpcGen.js +2 -1
- package/cjs/generator/RuntimeApisGen.js +5 -1
- package/cjs/generator/TypesGen.js +9 -8
- package/cjs/generator/known-codecs.js +151 -0
- package/cjs/generator/known-types.js +33 -0
- package/cjs/packageInfo.js +1 -1
- package/genSupportedChainTypes.js +6 -2
- package/generator/ApiGen.d.ts +2 -2
- package/generator/RpcGen.js +2 -1
- package/generator/RuntimeApisGen.js +6 -2
- package/generator/TypesGen.d.ts +2 -2
- package/generator/TypesGen.js +10 -9
- package/generator/known-codecs.d.ts +23 -0
- package/generator/known-codecs.js +120 -0
- package/generator/known-types.d.ts +6 -0
- package/generator/known-types.js +30 -0
- package/package.json +8 -7
- package/packageInfo.js +1 -1
|
@@ -38,6 +38,10 @@ const NETWORKS = [
|
|
|
38
38
|
chain: 'kusamaAssetHub',
|
|
39
39
|
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
40
40
|
},
|
|
41
|
+
{
|
|
42
|
+
chain: 'rococo',
|
|
43
|
+
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
44
|
+
},
|
|
41
45
|
{
|
|
42
46
|
chain: 'rococoAssetHub',
|
|
43
47
|
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
@@ -69,7 +73,7 @@ async function run() {
|
|
|
69
73
|
console.log('DONE!');
|
|
70
74
|
}
|
|
71
75
|
const getRuntimeVersion = (metadata) => {
|
|
72
|
-
const registry = new codecs_1.
|
|
76
|
+
const registry = new codecs_1.PortableRegistry(metadata.latest);
|
|
73
77
|
const executor = new dedot_1.ConstantExecutor({
|
|
74
78
|
registry,
|
|
75
79
|
metadataLatest: metadata.latest,
|
package/cjs/generator/RpcGen.js
CHANGED
|
@@ -5,6 +5,7 @@ const specs_1 = require("@dedot/specs");
|
|
|
5
5
|
const utils_1 = require("@dedot/utils");
|
|
6
6
|
const generator_1 = require("../generator");
|
|
7
7
|
const utils_2 = require("./utils");
|
|
8
|
+
const known_codecs_1 = require("./known-codecs");
|
|
8
9
|
const HIDDEN_RPCS = [
|
|
9
10
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
10
11
|
'rpc_methods',
|
|
@@ -168,7 +169,7 @@ class RpcGen extends generator_1.ApiGen {
|
|
|
168
169
|
return type;
|
|
169
170
|
}
|
|
170
171
|
#getCodecType(type, toTypeIn = true) {
|
|
171
|
-
const { typeIn, typeOut } =
|
|
172
|
+
const { typeIn, typeOut } = (0, known_codecs_1.findKnownCodecType)(type);
|
|
172
173
|
return toTypeIn ? typeIn : typeOut;
|
|
173
174
|
}
|
|
174
175
|
}
|
|
@@ -96,7 +96,7 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
96
96
|
}
|
|
97
97
|
#targetRuntimeApiSpecs() {
|
|
98
98
|
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
99
|
-
const runtimeApiSpec =
|
|
99
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
100
100
|
if (!runtimeApiSpec)
|
|
101
101
|
return;
|
|
102
102
|
return {
|
|
@@ -111,5 +111,9 @@ class RuntimeApisGen extends RpcGen_1.RpcGen {
|
|
|
111
111
|
return [...o, spec];
|
|
112
112
|
}, []);
|
|
113
113
|
}
|
|
114
|
+
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
115
|
+
const runtimeApiName = (0, specs_1.getRuntimeApiNames)().find((one) => (0, utils_2.calculateRuntimeApiHash)(one) === runtimeApiHash);
|
|
116
|
+
return (0, specs_1.getRuntimeApiSpecs)().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
117
|
+
};
|
|
114
118
|
}
|
|
115
119
|
exports.RuntimeApisGen = RuntimeApisGen;
|
|
@@ -5,8 +5,9 @@ const util_1 = require("@polkadot/util");
|
|
|
5
5
|
const codecs_1 = require("@dedot/codecs");
|
|
6
6
|
const utils_1 = require("@dedot/utils");
|
|
7
7
|
const utils_2 = require("./utils");
|
|
8
|
-
const
|
|
8
|
+
const known_types_1 = require("./known-types");
|
|
9
9
|
const TypeImports_1 = require("./TypeImports");
|
|
10
|
+
const known_codecs_1 = require("./known-codecs");
|
|
10
11
|
// Skip generate types for these
|
|
11
12
|
// as we do have native types for them
|
|
12
13
|
const SKIP_TYPES = [
|
|
@@ -40,7 +41,7 @@ class TypesGen {
|
|
|
40
41
|
typeImports;
|
|
41
42
|
constructor(metadata) {
|
|
42
43
|
this.metadata = metadata;
|
|
43
|
-
this.registry = new codecs_1.
|
|
44
|
+
this.registry = new codecs_1.PortableRegistry(this.metadata);
|
|
44
45
|
this.includedTypes = this.#includedTypes();
|
|
45
46
|
this.typeImports = new TypeImports_1.TypeImports();
|
|
46
47
|
}
|
|
@@ -102,7 +103,7 @@ class TypesGen {
|
|
|
102
103
|
const { tag, value } = type;
|
|
103
104
|
switch (tag) {
|
|
104
105
|
case 'Primitive':
|
|
105
|
-
const $codec =
|
|
106
|
+
const $codec = (0, known_codecs_1.findKnownCodec)(value.kind);
|
|
106
107
|
if ($codec.nativeType) {
|
|
107
108
|
return $codec.nativeType;
|
|
108
109
|
}
|
|
@@ -171,7 +172,7 @@ class TypesGen {
|
|
|
171
172
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
172
173
|
}
|
|
173
174
|
}
|
|
174
|
-
const { tagKey, valueKey } = this.registry.
|
|
175
|
+
const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
|
|
175
176
|
return membersType
|
|
176
177
|
.map(([keyName, valueType, docs]) => ({
|
|
177
178
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -266,8 +267,8 @@ class TypesGen {
|
|
|
266
267
|
const suffix = typeSuffixes.get(id) || '';
|
|
267
268
|
let knownType = false;
|
|
268
269
|
let name, nameOut;
|
|
269
|
-
if (
|
|
270
|
-
const codecType =
|
|
270
|
+
if ((0, known_codecs_1.isKnownCodecType)(joinedPath)) {
|
|
271
|
+
const codecType = (0, known_codecs_1.findKnownCodecType)(path.at(-1));
|
|
271
272
|
name = codecType.typeIn;
|
|
272
273
|
nameOut = codecType.typeOut;
|
|
273
274
|
knownType = true;
|
|
@@ -320,7 +321,7 @@ class TypesGen {
|
|
|
320
321
|
}
|
|
321
322
|
#shouldGenerateTypeIn(id) {
|
|
322
323
|
const { callTypeId } = this.metadata.extrinsic;
|
|
323
|
-
const palletCallTypeIds = this.registry.
|
|
324
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
324
325
|
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
325
326
|
}
|
|
326
327
|
eqlCache = new Map();
|
|
@@ -425,7 +426,7 @@ class TypesGen {
|
|
|
425
426
|
this.typeImports.addCodecType(typeName);
|
|
426
427
|
return;
|
|
427
428
|
}
|
|
428
|
-
if (
|
|
429
|
+
if (known_types_1.knownTypes.includes(typeName)) {
|
|
429
430
|
this.typeImports.addKnownType(typeName);
|
|
430
431
|
}
|
|
431
432
|
else {
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.isKnownCodecType = exports.findKnownWrapperCodec = exports.findKnownCodec = exports.findKnownCodecType = exports.looseTypeCodecs = exports.normalizeCodecName = void 0;
|
|
27
|
+
const $ = __importStar(require("@dedot/shape"));
|
|
28
|
+
const Codecs = __importStar(require("@dedot/codecs"));
|
|
29
|
+
const codecs_1 = require("@dedot/codecs");
|
|
30
|
+
const utils_1 = require("@dedot/utils");
|
|
31
|
+
const normalizeCodecName = (name) => {
|
|
32
|
+
return name.startsWith('$') ? name : `$${name}`;
|
|
33
|
+
};
|
|
34
|
+
exports.normalizeCodecName = normalizeCodecName;
|
|
35
|
+
// Known paths for codecs (primitives) that are shared between
|
|
36
|
+
// different substrate-based blockchains
|
|
37
|
+
const KNOWN_PATHS = [
|
|
38
|
+
'sp_core::crypto::AccountId32',
|
|
39
|
+
'sp_runtime::generic::era::Era',
|
|
40
|
+
'sp_runtime::multiaddress::MultiAddress',
|
|
41
|
+
/^sp_runtime::DispatchError$/,
|
|
42
|
+
'sp_runtime::ModuleError',
|
|
43
|
+
'sp_runtime::TokenError',
|
|
44
|
+
'sp_arithmetic::ArithmeticError',
|
|
45
|
+
'sp_runtime::TransactionalError',
|
|
46
|
+
'frame_support::dispatch::DispatchInfo',
|
|
47
|
+
'frame_system::Phase',
|
|
48
|
+
'sp_version::RuntimeVersion',
|
|
49
|
+
'fp_account::AccountId20',
|
|
50
|
+
'account::AccountId20',
|
|
51
|
+
'polkadot_runtime_common::claims::EthereumAddress',
|
|
52
|
+
'pallet_identity::types::Data',
|
|
53
|
+
'sp_runtime::generic::digest::Digest',
|
|
54
|
+
'sp_runtime::generic::digest::DigestItem',
|
|
55
|
+
'sp_runtime::generic::header::Header',
|
|
56
|
+
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
57
|
+
/^primitive_types::\w+$/,
|
|
58
|
+
/^sp_arithmetic::per_things::\w+$/,
|
|
59
|
+
/^sp_arithmetic::fixed_point::\w+$/,
|
|
60
|
+
];
|
|
61
|
+
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
62
|
+
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
63
|
+
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
64
|
+
/**
|
|
65
|
+
* Collection of codec types with loose input types
|
|
66
|
+
*
|
|
67
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
68
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
69
|
+
*
|
|
70
|
+
* This registry keep track the list of codecs which follow this convention
|
|
71
|
+
*/
|
|
72
|
+
exports.looseTypeCodecs = {
|
|
73
|
+
$AccountId20: codecs_1.$AccountId20,
|
|
74
|
+
$EthereumAddress: codecs_1.$EthereumAddress,
|
|
75
|
+
$AccountId32: codecs_1.$AccountId32,
|
|
76
|
+
$ConsensusEngineId: codecs_1.$ConsensusEngineId,
|
|
77
|
+
$StorageKey: codecs_1.$StorageKey,
|
|
78
|
+
$StorageData: codecs_1.$StorageData,
|
|
79
|
+
$PrefixedStorageKey: codecs_1.$PrefixedStorageKey,
|
|
80
|
+
$Bytes: codecs_1.$Bytes,
|
|
81
|
+
$RawBytes: codecs_1.$RawBytes,
|
|
82
|
+
$MultiAddress: codecs_1.$MultiAddress,
|
|
83
|
+
$OpaqueExtrinsic: codecs_1.$OpaqueExtrinsic,
|
|
84
|
+
$UncheckedExtrinsic: codecs_1.$UncheckedExtrinsic,
|
|
85
|
+
$Era: codecs_1.$Era,
|
|
86
|
+
};
|
|
87
|
+
function findKnownCodecType(name) {
|
|
88
|
+
const normalizedName = (0, exports.normalizeCodecName)(name);
|
|
89
|
+
const $knownCodec = exports.looseTypeCodecs[normalizedName];
|
|
90
|
+
if ($knownCodec) {
|
|
91
|
+
return {
|
|
92
|
+
name: normalizedName,
|
|
93
|
+
$codec: $knownCodec,
|
|
94
|
+
typeIn: `${name}Like`,
|
|
95
|
+
typeOut: name,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
const $codec = findKnownCodec(name);
|
|
99
|
+
if ($codec.nativeType && $[name]) {
|
|
100
|
+
return {
|
|
101
|
+
name: normalizedName,
|
|
102
|
+
$codec,
|
|
103
|
+
typeIn: $codec.nativeType,
|
|
104
|
+
typeOut: $codec.nativeType,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
name: normalizedName,
|
|
109
|
+
$codec,
|
|
110
|
+
typeIn: name,
|
|
111
|
+
typeOut: name,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
exports.findKnownCodecType = findKnownCodecType;
|
|
115
|
+
function findKnownCodec(typeName) {
|
|
116
|
+
// @ts-ignore
|
|
117
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[(0, exports.normalizeCodecName)(typeName)] || $[typeName];
|
|
118
|
+
(0, utils_1.assert)($codec, `Known codec not found - ${typeName}`);
|
|
119
|
+
return $codec;
|
|
120
|
+
}
|
|
121
|
+
exports.findKnownCodec = findKnownCodec;
|
|
122
|
+
function findKnownWrapperCodec(typeName) {
|
|
123
|
+
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
124
|
+
if (matchNames) {
|
|
125
|
+
const [_, wrapper, inner] = matchNames;
|
|
126
|
+
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
127
|
+
// @ts-ignore
|
|
128
|
+
const $Wrapper = $[wrapper];
|
|
129
|
+
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
130
|
+
return $Wrapper(findKnownWrapperCodec(inner));
|
|
131
|
+
}
|
|
132
|
+
const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
|
|
133
|
+
return $Wrapper(...$inners);
|
|
134
|
+
}
|
|
135
|
+
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
136
|
+
}
|
|
137
|
+
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
138
|
+
const $inner = typeName
|
|
139
|
+
.slice(1, -1)
|
|
140
|
+
.split(',')
|
|
141
|
+
.filter((x) => x)
|
|
142
|
+
.map((one) => findKnownCodec(one.trim()));
|
|
143
|
+
return $.Tuple(...$inner);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.findKnownWrapperCodec = findKnownWrapperCodec;
|
|
147
|
+
function isKnownCodecType(path) {
|
|
148
|
+
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
149
|
+
return KNOWN_PATHS.some((one) => joinedPath.match(one));
|
|
150
|
+
}
|
|
151
|
+
exports.isKnownCodecType = isKnownCodecType;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.knownTypes = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Known type names registered in @dedot/types
|
|
6
|
+
* Since TS interfaces are trim-off when compiling,
|
|
7
|
+
* So we need to register them explicitly here for RPC codegen process
|
|
8
|
+
*/
|
|
9
|
+
exports.knownTypes = [
|
|
10
|
+
'ExtrinsicOrHash',
|
|
11
|
+
'EpochAuthorship',
|
|
12
|
+
'BlockStats',
|
|
13
|
+
'Prevotes',
|
|
14
|
+
'Precommits',
|
|
15
|
+
'RoundState',
|
|
16
|
+
'ReportedRoundStates',
|
|
17
|
+
'JustificationNotification',
|
|
18
|
+
'EncodedFinalityProofs',
|
|
19
|
+
'LeavesProof',
|
|
20
|
+
'StorageKind',
|
|
21
|
+
'RpcMethods',
|
|
22
|
+
'ReadProof',
|
|
23
|
+
'StorageChangeSet',
|
|
24
|
+
'TraceBlockResponse',
|
|
25
|
+
'MigrationStatusResult',
|
|
26
|
+
'ChainType',
|
|
27
|
+
'ChainProperties',
|
|
28
|
+
'Health',
|
|
29
|
+
'SyncState',
|
|
30
|
+
'PeerInfo',
|
|
31
|
+
'NodeRole',
|
|
32
|
+
'NetworkState',
|
|
33
|
+
];
|
package/cjs/packageInfo.js
CHANGED
|
@@ -2,4 +2,4 @@
|
|
|
2
2
|
// THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
4
|
exports.packageInfo = void 0;
|
|
5
|
-
exports.packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.
|
|
5
|
+
exports.packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.26' };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { generateTypes, generateTypesFromChain } from './index';
|
|
2
2
|
import { rpc } from '@polkadot/types-support/metadata/static-substrate';
|
|
3
3
|
import staticSubstrate from '@polkadot/types-support/metadata/v15/substrate-hex';
|
|
4
|
-
import { $Metadata,
|
|
4
|
+
import { $Metadata, PortableRegistry } from '@dedot/codecs';
|
|
5
5
|
import { ConstantExecutor } from 'dedot';
|
|
6
6
|
const NETWORKS = [
|
|
7
7
|
{
|
|
@@ -33,6 +33,10 @@ const NETWORKS = [
|
|
|
33
33
|
chain: 'kusamaAssetHub',
|
|
34
34
|
endpoint: 'wss://kusama-asset-hub-rpc.polkadot.io/',
|
|
35
35
|
},
|
|
36
|
+
{
|
|
37
|
+
chain: 'rococo',
|
|
38
|
+
endpoint: 'wss://rococo-rpc.polkadot.io/',
|
|
39
|
+
},
|
|
36
40
|
{
|
|
37
41
|
chain: 'rococoAssetHub',
|
|
38
42
|
endpoint: 'wss://rococo-asset-hub-rpc.polkadot.io/',
|
|
@@ -64,7 +68,7 @@ async function run() {
|
|
|
64
68
|
console.log('DONE!');
|
|
65
69
|
}
|
|
66
70
|
const getRuntimeVersion = (metadata) => {
|
|
67
|
-
const registry = new
|
|
71
|
+
const registry = new PortableRegistry(metadata.latest);
|
|
68
72
|
const executor = new ConstantExecutor({
|
|
69
73
|
registry,
|
|
70
74
|
metadataLatest: metadata.latest,
|
package/generator/ApiGen.d.ts
CHANGED
|
@@ -44,7 +44,7 @@ export declare abstract class ApiGen {
|
|
|
44
44
|
} | {
|
|
45
45
|
tag: "Primitive";
|
|
46
46
|
value: {
|
|
47
|
-
kind: "bool" | "
|
|
47
|
+
kind: "bool" | "u8" | "i8" | "u16" | "i16" | "u32" | "i32" | "u64" | "i64" | "u128" | "i128" | "u256" | "i256" | "str" | "char";
|
|
48
48
|
};
|
|
49
49
|
} | {
|
|
50
50
|
tag: "Compact";
|
|
@@ -134,5 +134,5 @@ export declare abstract class ApiGen {
|
|
|
134
134
|
}>;
|
|
135
135
|
};
|
|
136
136
|
};
|
|
137
|
-
get registry(): import("@dedot/codecs").
|
|
137
|
+
get registry(): import("@dedot/codecs").PortableRegistry;
|
|
138
138
|
}
|
package/generator/RpcGen.js
CHANGED
|
@@ -2,6 +2,7 @@ import { findAliasRpcSpec, findRpcSpec, isUnsubscribeMethod } from '@dedot/specs
|
|
|
2
2
|
import { isNativeType } from '@dedot/utils';
|
|
3
3
|
import { ApiGen } from '../generator';
|
|
4
4
|
import { beautifySourceCode, commentBlock, compileTemplate, TUPLE_TYPE_REGEX, WRAPPER_TYPE_REGEX } from './utils';
|
|
5
|
+
import { findKnownCodecType } from './known-codecs';
|
|
5
6
|
const HIDDEN_RPCS = [
|
|
6
7
|
// Ref: https://github.com/paritytech/polkadot-sdk/blob/43415ef58c143b985e09015cd000dbd65f6d3997/substrate/client/rpc-servers/src/lib.rs#L152C9-L158
|
|
7
8
|
'rpc_methods',
|
|
@@ -165,7 +166,7 @@ export class RpcGen extends ApiGen {
|
|
|
165
166
|
return type;
|
|
166
167
|
}
|
|
167
168
|
#getCodecType(type, toTypeIn = true) {
|
|
168
|
-
const { typeIn, typeOut } =
|
|
169
|
+
const { typeIn, typeOut } = findKnownCodecType(type);
|
|
169
170
|
return toTypeIn ? typeIn : typeOut;
|
|
170
171
|
}
|
|
171
172
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getRuntimeApiNames, getRuntimeApiSpecs } from '@dedot/specs';
|
|
2
2
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
3
3
|
import { calculateRuntimeApiHash, stringSnakeCase } from '@dedot/utils';
|
|
4
4
|
import { RpcGen } from './RpcGen';
|
|
@@ -93,7 +93,7 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
93
93
|
}
|
|
94
94
|
#targetRuntimeApiSpecs() {
|
|
95
95
|
const specs = this.runtimeApis.map(([runtimeApiHash, version]) => {
|
|
96
|
-
const runtimeApiSpec = findRuntimeApiSpec(runtimeApiHash, version);
|
|
96
|
+
const runtimeApiSpec = this.#findRuntimeApiSpec(runtimeApiHash, version);
|
|
97
97
|
if (!runtimeApiSpec)
|
|
98
98
|
return;
|
|
99
99
|
return {
|
|
@@ -108,4 +108,8 @@ export class RuntimeApisGen extends RpcGen {
|
|
|
108
108
|
return [...o, spec];
|
|
109
109
|
}, []);
|
|
110
110
|
}
|
|
111
|
+
#findRuntimeApiSpec = (runtimeApiHash, version) => {
|
|
112
|
+
const runtimeApiName = getRuntimeApiNames().find((one) => calculateRuntimeApiHash(one) === runtimeApiHash);
|
|
113
|
+
return getRuntimeApiSpecs().find((one) => one.runtimeApiName === runtimeApiName && one.version === version);
|
|
114
|
+
};
|
|
111
115
|
}
|
package/generator/TypesGen.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PortableRegistry, Field, MetadataLatest, PortableType, TypeId } from '@dedot/codecs';
|
|
2
2
|
import { TypeImports } from './TypeImports';
|
|
3
3
|
interface NamedType extends PortableType {
|
|
4
4
|
name: string;
|
|
@@ -15,7 +15,7 @@ export declare class TypesGen {
|
|
|
15
15
|
* Types will be generated its definition out.
|
|
16
16
|
*/
|
|
17
17
|
includedTypes: Record<TypeId, NamedType>;
|
|
18
|
-
registry:
|
|
18
|
+
registry: PortableRegistry;
|
|
19
19
|
typeImports: TypeImports;
|
|
20
20
|
constructor(metadata: MetadataLatest);
|
|
21
21
|
generate(): Promise<string>;
|
package/generator/TypesGen.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { stringPascalCase } from '@polkadot/util';
|
|
2
|
-
import {
|
|
2
|
+
import { PortableRegistry } from '@dedot/codecs';
|
|
3
3
|
import { isNativeType, normalizeName } from '@dedot/utils';
|
|
4
4
|
import { beautifySourceCode, commentBlock, compileTemplate } from './utils';
|
|
5
|
-
import {
|
|
5
|
+
import { knownTypes } from './known-types';
|
|
6
6
|
import { TypeImports } from './TypeImports';
|
|
7
|
+
import { findKnownCodec, findKnownCodecType, isKnownCodecType } from './known-codecs';
|
|
7
8
|
// Skip generate types for these
|
|
8
9
|
// as we do have native types for them
|
|
9
10
|
const SKIP_TYPES = [
|
|
@@ -37,7 +38,7 @@ export class TypesGen {
|
|
|
37
38
|
typeImports;
|
|
38
39
|
constructor(metadata) {
|
|
39
40
|
this.metadata = metadata;
|
|
40
|
-
this.registry = new
|
|
41
|
+
this.registry = new PortableRegistry(this.metadata);
|
|
41
42
|
this.includedTypes = this.#includedTypes();
|
|
42
43
|
this.typeImports = new TypeImports();
|
|
43
44
|
}
|
|
@@ -99,7 +100,7 @@ export class TypesGen {
|
|
|
99
100
|
const { tag, value } = type;
|
|
100
101
|
switch (tag) {
|
|
101
102
|
case 'Primitive':
|
|
102
|
-
const $codec =
|
|
103
|
+
const $codec = findKnownCodec(value.kind);
|
|
103
104
|
if ($codec.nativeType) {
|
|
104
105
|
return $codec.nativeType;
|
|
105
106
|
}
|
|
@@ -168,7 +169,7 @@ export class TypesGen {
|
|
|
168
169
|
membersType.push([keyName, this.generateObjectType(fields, nestedLevel + 1, typeOut), docs]);
|
|
169
170
|
}
|
|
170
171
|
}
|
|
171
|
-
const { tagKey, valueKey } = this.registry.
|
|
172
|
+
const { tagKey, valueKey } = this.registry.getEnumOptions(typeId);
|
|
172
173
|
return membersType
|
|
173
174
|
.map(([keyName, valueType, docs]) => ({
|
|
174
175
|
tag: `${tagKey}: '${keyName}'`,
|
|
@@ -263,8 +264,8 @@ export class TypesGen {
|
|
|
263
264
|
const suffix = typeSuffixes.get(id) || '';
|
|
264
265
|
let knownType = false;
|
|
265
266
|
let name, nameOut;
|
|
266
|
-
if (
|
|
267
|
-
const codecType =
|
|
267
|
+
if (isKnownCodecType(joinedPath)) {
|
|
268
|
+
const codecType = findKnownCodecType(path.at(-1));
|
|
268
269
|
name = codecType.typeIn;
|
|
269
270
|
nameOut = codecType.typeOut;
|
|
270
271
|
knownType = true;
|
|
@@ -317,7 +318,7 @@ export class TypesGen {
|
|
|
317
318
|
}
|
|
318
319
|
#shouldGenerateTypeIn(id) {
|
|
319
320
|
const { callTypeId } = this.metadata.extrinsic;
|
|
320
|
-
const palletCallTypeIds = this.registry.
|
|
321
|
+
const palletCallTypeIds = this.registry.getPalletCallTypeIds();
|
|
321
322
|
return callTypeId === id || palletCallTypeIds.includes(id);
|
|
322
323
|
}
|
|
323
324
|
eqlCache = new Map();
|
|
@@ -422,7 +423,7 @@ export class TypesGen {
|
|
|
422
423
|
this.typeImports.addCodecType(typeName);
|
|
423
424
|
return;
|
|
424
425
|
}
|
|
425
|
-
if (
|
|
426
|
+
if (knownTypes.includes(typeName)) {
|
|
426
427
|
this.typeImports.addKnownType(typeName);
|
|
427
428
|
}
|
|
428
429
|
else {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import * as $ from '@dedot/shape';
|
|
2
|
+
import { AnyShape } from '@dedot/shape';
|
|
3
|
+
export type CodecName = `$${string}`;
|
|
4
|
+
export interface CodecType {
|
|
5
|
+
name: CodecName;
|
|
6
|
+
$codec: AnyShape;
|
|
7
|
+
typeIn: string;
|
|
8
|
+
typeOut: string;
|
|
9
|
+
}
|
|
10
|
+
export declare const normalizeCodecName: (name: string | CodecName) => CodecName;
|
|
11
|
+
/**
|
|
12
|
+
* Collection of codec types with loose input types
|
|
13
|
+
*
|
|
14
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
15
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
16
|
+
*
|
|
17
|
+
* This registry keep track the list of codecs which follow this convention
|
|
18
|
+
*/
|
|
19
|
+
export declare const looseTypeCodecs: Record<string, AnyShape>;
|
|
20
|
+
export declare function findKnownCodecType(name: string): CodecType;
|
|
21
|
+
export declare function findKnownCodec<I = unknown, O = I>(typeName: string): $.Shape<I, O>;
|
|
22
|
+
export declare function findKnownWrapperCodec(typeName: string): $.AnyShape | undefined;
|
|
23
|
+
export declare function isKnownCodecType(path: string | string[]): boolean;
|
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
import { assert } from '@dedot/utils';
|
|
5
|
+
export const normalizeCodecName = (name) => {
|
|
6
|
+
return name.startsWith('$') ? name : `$${name}`;
|
|
7
|
+
};
|
|
8
|
+
// Known paths for codecs (primitives) that are shared between
|
|
9
|
+
// different substrate-based blockchains
|
|
10
|
+
const KNOWN_PATHS = [
|
|
11
|
+
'sp_core::crypto::AccountId32',
|
|
12
|
+
'sp_runtime::generic::era::Era',
|
|
13
|
+
'sp_runtime::multiaddress::MultiAddress',
|
|
14
|
+
/^sp_runtime::DispatchError$/,
|
|
15
|
+
'sp_runtime::ModuleError',
|
|
16
|
+
'sp_runtime::TokenError',
|
|
17
|
+
'sp_arithmetic::ArithmeticError',
|
|
18
|
+
'sp_runtime::TransactionalError',
|
|
19
|
+
'frame_support::dispatch::DispatchInfo',
|
|
20
|
+
'frame_system::Phase',
|
|
21
|
+
'sp_version::RuntimeVersion',
|
|
22
|
+
'fp_account::AccountId20',
|
|
23
|
+
'account::AccountId20',
|
|
24
|
+
'polkadot_runtime_common::claims::EthereumAddress',
|
|
25
|
+
'pallet_identity::types::Data',
|
|
26
|
+
'sp_runtime::generic::digest::Digest',
|
|
27
|
+
'sp_runtime::generic::digest::DigestItem',
|
|
28
|
+
'sp_runtime::generic::header::Header',
|
|
29
|
+
'sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic',
|
|
30
|
+
/^primitive_types::\w+$/,
|
|
31
|
+
/^sp_arithmetic::per_things::\w+$/,
|
|
32
|
+
/^sp_arithmetic::fixed_point::\w+$/,
|
|
33
|
+
];
|
|
34
|
+
const WRAPPER_TYPE_REGEX = /^(\w+)<(.*)>$/;
|
|
35
|
+
const TUPLE_TYPE_REGEX = /^\[(.*)]$/;
|
|
36
|
+
const KNOWN_WRAPPER_TYPES = ['Option', 'Vec', 'Result', 'Array'];
|
|
37
|
+
/**
|
|
38
|
+
* Collection of codec types with loose input types
|
|
39
|
+
*
|
|
40
|
+
* Loose codecs are codecs with different typeIn & typeOut,
|
|
41
|
+
* E.g: Codec `$AccountId32`, we have its typeIn is `AccountId32Like` & typeOut is `AccountId32`
|
|
42
|
+
*
|
|
43
|
+
* This registry keep track the list of codecs which follow this convention
|
|
44
|
+
*/
|
|
45
|
+
export const looseTypeCodecs = {
|
|
46
|
+
$AccountId20,
|
|
47
|
+
$EthereumAddress,
|
|
48
|
+
$AccountId32,
|
|
49
|
+
$ConsensusEngineId,
|
|
50
|
+
$StorageKey,
|
|
51
|
+
$StorageData,
|
|
52
|
+
$PrefixedStorageKey,
|
|
53
|
+
$Bytes,
|
|
54
|
+
$RawBytes,
|
|
55
|
+
$MultiAddress,
|
|
56
|
+
$OpaqueExtrinsic,
|
|
57
|
+
$UncheckedExtrinsic,
|
|
58
|
+
$Era,
|
|
59
|
+
};
|
|
60
|
+
export function findKnownCodecType(name) {
|
|
61
|
+
const normalizedName = normalizeCodecName(name);
|
|
62
|
+
const $knownCodec = looseTypeCodecs[normalizedName];
|
|
63
|
+
if ($knownCodec) {
|
|
64
|
+
return {
|
|
65
|
+
name: normalizedName,
|
|
66
|
+
$codec: $knownCodec,
|
|
67
|
+
typeIn: `${name}Like`,
|
|
68
|
+
typeOut: name,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const $codec = findKnownCodec(name);
|
|
72
|
+
if ($codec.nativeType && $[name]) {
|
|
73
|
+
return {
|
|
74
|
+
name: normalizedName,
|
|
75
|
+
$codec,
|
|
76
|
+
typeIn: $codec.nativeType,
|
|
77
|
+
typeOut: $codec.nativeType,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
name: normalizedName,
|
|
82
|
+
$codec,
|
|
83
|
+
typeIn: name,
|
|
84
|
+
typeOut: name,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export function findKnownCodec(typeName) {
|
|
88
|
+
// @ts-ignore
|
|
89
|
+
const $codec = findKnownWrapperCodec(typeName) || Codecs[normalizeCodecName(typeName)] || $[typeName];
|
|
90
|
+
assert($codec, `Known codec not found - ${typeName}`);
|
|
91
|
+
return $codec;
|
|
92
|
+
}
|
|
93
|
+
export function findKnownWrapperCodec(typeName) {
|
|
94
|
+
const matchNames = typeName.match(WRAPPER_TYPE_REGEX);
|
|
95
|
+
if (matchNames) {
|
|
96
|
+
const [_, wrapper, inner] = matchNames;
|
|
97
|
+
if (KNOWN_WRAPPER_TYPES.includes(wrapper)) {
|
|
98
|
+
// @ts-ignore
|
|
99
|
+
const $Wrapper = $[wrapper];
|
|
100
|
+
if (inner.match(TUPLE_TYPE_REGEX) || inner.match(WRAPPER_TYPE_REGEX)) {
|
|
101
|
+
return $Wrapper(findKnownWrapperCodec(inner));
|
|
102
|
+
}
|
|
103
|
+
const $inners = inner.split(',').map((one) => findKnownCodec(one.trim()));
|
|
104
|
+
return $Wrapper(...$inners);
|
|
105
|
+
}
|
|
106
|
+
throw new Error(`Unknown wrapper type ${wrapper} from ${typeName}`);
|
|
107
|
+
}
|
|
108
|
+
else if (typeName.match(TUPLE_TYPE_REGEX)) {
|
|
109
|
+
const $inner = typeName
|
|
110
|
+
.slice(1, -1)
|
|
111
|
+
.split(',')
|
|
112
|
+
.filter((x) => x)
|
|
113
|
+
.map((one) => findKnownCodec(one.trim()));
|
|
114
|
+
return $.Tuple(...$inner);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export function isKnownCodecType(path) {
|
|
118
|
+
const joinedPath = Array.isArray(path) ? path.join('::') : path;
|
|
119
|
+
return KNOWN_PATHS.some((one) => joinedPath.match(one));
|
|
120
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Known type names registered in @dedot/types
|
|
3
|
+
* Since TS interfaces are trim-off when compiling,
|
|
4
|
+
* So we need to register them explicitly here for RPC codegen process
|
|
5
|
+
*/
|
|
6
|
+
export const knownTypes = [
|
|
7
|
+
'ExtrinsicOrHash',
|
|
8
|
+
'EpochAuthorship',
|
|
9
|
+
'BlockStats',
|
|
10
|
+
'Prevotes',
|
|
11
|
+
'Precommits',
|
|
12
|
+
'RoundState',
|
|
13
|
+
'ReportedRoundStates',
|
|
14
|
+
'JustificationNotification',
|
|
15
|
+
'EncodedFinalityProofs',
|
|
16
|
+
'LeavesProof',
|
|
17
|
+
'StorageKind',
|
|
18
|
+
'RpcMethods',
|
|
19
|
+
'ReadProof',
|
|
20
|
+
'StorageChangeSet',
|
|
21
|
+
'TraceBlockResponse',
|
|
22
|
+
'MigrationStatusResult',
|
|
23
|
+
'ChainType',
|
|
24
|
+
'ChainProperties',
|
|
25
|
+
'Health',
|
|
26
|
+
'SyncState',
|
|
27
|
+
'PeerInfo',
|
|
28
|
+
'NodeRole',
|
|
29
|
+
'NetworkState',
|
|
30
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/codegen",
|
|
3
|
-
"version": "0.0.1-alpha.
|
|
3
|
+
"version": "0.0.1-alpha.27",
|
|
4
4
|
"description": "Generate types",
|
|
5
5
|
"author": "Thang X. Vu <thang@coongcrafts.io>",
|
|
6
6
|
"homepage": "https://github.com/dedotdev/dedot/tree/main/packages/codegen",
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"url": "https://github.com/dedotdev/dedot.git"
|
|
11
11
|
},
|
|
12
12
|
"main": "./cjs/index.js",
|
|
13
|
+
"sideEffects": false,
|
|
13
14
|
"type": "module",
|
|
14
15
|
"scripts": {
|
|
15
16
|
"build": "tsc --project tsconfig.build.json && tsc --project tsconfig.build.cjs.json && yarn copy",
|
|
@@ -17,12 +18,12 @@
|
|
|
17
18
|
"copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
|
|
18
19
|
},
|
|
19
20
|
"dependencies": {
|
|
20
|
-
"@dedot/codecs": "0.0.1-alpha.
|
|
21
|
-
"@dedot/shape": "0.0.1-alpha.
|
|
22
|
-
"@dedot/specs": "0.0.1-alpha.
|
|
23
|
-
"@dedot/utils": "0.0.1-alpha.
|
|
21
|
+
"@dedot/codecs": "0.0.1-alpha.27",
|
|
22
|
+
"@dedot/shape": "0.0.1-alpha.27",
|
|
23
|
+
"@dedot/specs": "0.0.1-alpha.27",
|
|
24
|
+
"@dedot/utils": "0.0.1-alpha.27",
|
|
24
25
|
"@polkadot/util": "^12.6.2",
|
|
25
|
-
"dedot": "0.0.1-alpha.
|
|
26
|
+
"dedot": "0.0.1-alpha.27",
|
|
26
27
|
"handlebars": "^4.7.8",
|
|
27
28
|
"prettier": "^3.0.3"
|
|
28
29
|
},
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
"directory": "dist"
|
|
32
33
|
},
|
|
33
34
|
"license": "Apache-2.0",
|
|
34
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "0a597de3c59c70bf39a56e1569e76b61dce9af8f",
|
|
35
36
|
"module": "./index.js",
|
|
36
37
|
"types": "./index.d.ts",
|
|
37
38
|
"exports": {
|
package/packageInfo.js
CHANGED