@dedot/codegen 0.0.1-next.020a0293.9 → 0.0.1-next.16e37feb.8
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 +8 -4
- package/cjs/generator/ConstsGen.js +8 -8
- package/cjs/generator/ErrorsGen.js +11 -11
- package/cjs/generator/EventsGen.js +12 -12
- package/cjs/generator/IndexGen.js +3 -3
- package/cjs/generator/QueryGen.js +8 -8
- package/cjs/generator/RpcGen.js +16 -15
- package/cjs/generator/RuntimeApisGen.js +19 -15
- package/cjs/generator/TxGen.js +9 -9
- package/cjs/generator/TypesGen.js +19 -18
- package/cjs/generator/index.js +10 -10
- package/cjs/generator/known-codecs.js +151 -0
- package/cjs/generator/known-types.js +33 -0
- package/cjs/index.js +10 -10
- package/cjs/packageInfo.js +1 -1
- package/genSupportedChainTypes.js +7 -3
- package/generator/ApiGen.d.ts +3 -3
- package/generator/ConstsGen.d.ts +1 -1
- package/generator/ConstsGen.js +2 -2
- package/generator/ErrorsGen.d.ts +1 -1
- package/generator/ErrorsGen.js +2 -2
- package/generator/EventsGen.d.ts +1 -1
- package/generator/EventsGen.js +2 -2
- package/generator/IndexGen.d.ts +1 -1
- package/generator/IndexGen.js +1 -1
- package/generator/QueryGen.d.ts +1 -1
- package/generator/QueryGen.js +2 -2
- package/generator/RpcGen.d.ts +1 -1
- package/generator/RpcGen.js +4 -3
- package/generator/RuntimeApisGen.d.ts +2 -2
- package/generator/RuntimeApisGen.js +8 -4
- package/generator/TxGen.d.ts +1 -1
- package/generator/TxGen.js +2 -2
- package/generator/TypesGen.d.ts +3 -3
- package/generator/TypesGen.js +12 -11
- package/generator/index.d.ts +10 -10
- package/generator/index.js +10 -10
- 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/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +7 -7
- package/packageInfo.js +1 -1
|
@@ -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/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { MetadataLatest } from '@dedot/codecs';
|
|
2
|
-
import { NetworkInfo } from './types';
|
|
2
|
+
import { NetworkInfo } from './types.js';
|
|
3
3
|
export declare function generateTypesFromChain(network: NetworkInfo, endpoint: string, outDir: string): Promise<void>;
|
|
4
4
|
export declare function generateTypes(network: NetworkInfo, metadata: MetadataLatest, rpcMethods: string[], runtimeApis: any[], outDir?: string): Promise<void>;
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Dedot } from 'dedot';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
3
|
import * as path from 'path';
|
|
4
|
-
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, RpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator';
|
|
4
|
+
import { ConstsGen, ErrorsGen, EventsGen, IndexGen, QueryGen, RpcGen, RuntimeApisGen, TxGen, TypesGen, } from './generator/index.js';
|
|
5
5
|
import { stringCamelCase } from '@polkadot/util';
|
|
6
6
|
export async function generateTypesFromChain(network, endpoint, outDir) {
|
|
7
7
|
const api = await Dedot.create(endpoint);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dedot/codegen",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.16e37feb.8+16e37fe",
|
|
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",
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"copy": "cp -R ./src/templates ./dist && cp -R ./src/templates ./dist/cjs"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@dedot/codecs": "0.0.1-next.
|
|
22
|
-
"@dedot/shape": "0.0.1-next.
|
|
23
|
-
"@dedot/specs": "0.0.1-next.
|
|
24
|
-
"@dedot/utils": "0.0.1-next.
|
|
21
|
+
"@dedot/codecs": "0.0.1-next.16e37feb.8+16e37fe",
|
|
22
|
+
"@dedot/shape": "0.0.1-next.16e37feb.8+16e37fe",
|
|
23
|
+
"@dedot/specs": "0.0.1-next.16e37feb.8+16e37fe",
|
|
24
|
+
"@dedot/utils": "0.0.1-next.16e37feb.8+16e37fe",
|
|
25
25
|
"@polkadot/util": "^12.6.2",
|
|
26
|
-
"dedot": "0.0.1-next.
|
|
26
|
+
"dedot": "0.0.1-next.16e37feb.8+16e37fe",
|
|
27
27
|
"handlebars": "^4.7.8",
|
|
28
28
|
"prettier": "^3.0.3"
|
|
29
29
|
},
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"directory": "dist"
|
|
33
33
|
},
|
|
34
34
|
"license": "Apache-2.0",
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "16e37feb35200c80b3ffcba5791154050da36443",
|
|
36
36
|
"module": "./index.js",
|
|
37
37
|
"types": "./index.d.ts",
|
|
38
38
|
"exports": {
|
package/packageInfo.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
// THIS FILE IS AUTO-GENERATED, DO NOT EDIT!
|
|
2
|
-
export const packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.
|
|
2
|
+
export const packageInfo = { name: '@dedot/codegen', version: '0.0.1-alpha.28' };
|