@pezkuwi/typegen 16.5.5
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/README.md +3 -0
- package/build/augment.d.ts +2 -0
- package/build/bundle.d.ts +2 -0
- package/build/extractChain.d.ts +1 -0
- package/build/fromChain.d.ts +1 -0
- package/build/fromDefs.d.ts +1 -0
- package/build/generate/consts.d.ts +5 -0
- package/build/generate/errors.d.ts +4 -0
- package/build/generate/events.d.ts +5 -0
- package/build/generate/index.d.ts +10 -0
- package/build/generate/interfaceRegistry.d.ts +4 -0
- package/build/generate/lookup.d.ts +4 -0
- package/build/generate/query.d.ts +5 -0
- package/build/generate/rpc.d.ts +6 -0
- package/build/generate/runtime.d.ts +7 -0
- package/build/generate/tsDef.d.ts +16 -0
- package/build/generate/tx.d.ts +5 -0
- package/build/generate/types.d.ts +12 -0
- package/build/index.d.ts +2 -0
- package/build/interfacesTs.d.ts +1 -0
- package/build/metadataMd.d.ts +1 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/scripts/polkadot-types-chain-info.mjs +7 -0
- package/build/scripts/polkadot-types-from-chain.mjs +7 -0
- package/build/scripts/polkadot-types-from-defs.mjs +7 -0
- package/build/scripts/polkadot-types-internal-interfaces.mjs +7 -0
- package/build/scripts/polkadot-types-internal-metadata.mjs +7 -0
- package/build/util/assert.d.ts +2 -0
- package/build/util/derived.d.ts +4 -0
- package/build/util/docs.d.ts +1 -0
- package/build/util/file.d.ts +2 -0
- package/build/util/formatting.d.ts +11 -0
- package/build/util/imports.d.ts +23 -0
- package/build/util/index.d.ts +15 -0
- package/build/util/initMeta.d.ts +12 -0
- package/build/util/register.d.ts +4 -0
- package/build/util/wsMeta.d.ts +4 -0
- package/package.json +52 -0
- package/scripts/polkadot-types-chain-info.mjs +7 -0
- package/scripts/polkadot-types-from-chain.mjs +7 -0
- package/scripts/polkadot-types-from-defs.mjs +7 -0
- package/scripts/polkadot-types-internal-interfaces.mjs +7 -0
- package/scripts/polkadot-types-internal-metadata.mjs +7 -0
- package/src/augment.ts +5 -0
- package/src/bundle.ts +5 -0
- package/src/extractChain.ts +54 -0
- package/src/fromChain.ts +123 -0
- package/src/fromDefs.ts +106 -0
- package/src/generate/consts.ts +112 -0
- package/src/generate/errors.ts +75 -0
- package/src/generate/events.ts +165 -0
- package/src/generate/index.ts +13 -0
- package/src/generate/interfaceRegistry.ts +85 -0
- package/src/generate/lookup.ts +294 -0
- package/src/generate/query.ts +169 -0
- package/src/generate/rpc.ts +158 -0
- package/src/generate/runtime.ts +284 -0
- package/src/generate/tsDef.ts +321 -0
- package/src/generate/tx.ts +152 -0
- package/src/generate/types.ts +26 -0
- package/src/index.ts +6 -0
- package/src/interfacesTs.ts +35 -0
- package/src/metadataMd.ts +844 -0
- package/src/packageDetect.ts +14 -0
- package/src/packageInfo.ts +6 -0
- package/src/templates/calls.hbs +30 -0
- package/src/templates/consts.hbs +28 -0
- package/src/templates/docs.hbs +7 -0
- package/src/templates/errors.hbs +28 -0
- package/src/templates/events.hbs +28 -0
- package/src/templates/header.hbs +2 -0
- package/src/templates/interfaceRegistry.hbs +15 -0
- package/src/templates/lookup/defs-named.hbs +12 -0
- package/src/templates/lookup/defs.hbs +15 -0
- package/src/templates/lookup/index.hbs +3 -0
- package/src/templates/lookup/types.hbs +14 -0
- package/src/templates/query.hbs +29 -0
- package/src/templates/rpc.hbs +22 -0
- package/src/templates/tsDef/index.hbs +3 -0
- package/src/templates/tsDef/moduleTypes.hbs +10 -0
- package/src/templates/tsDef/types.hbs +7 -0
- package/src/templates/tx.hbs +30 -0
- package/src/util/assert.ts +18 -0
- package/src/util/derived.spec.ts +58 -0
- package/src/util/derived.ts +133 -0
- package/src/util/docs.ts +13 -0
- package/src/util/file.ts +42 -0
- package/src/util/formatting.spec.ts +30 -0
- package/src/util/formatting.ts +295 -0
- package/src/util/imports.ts +164 -0
- package/src/util/index.ts +18 -0
- package/src/util/initMeta.ts +37 -0
- package/src/util/register.ts +12 -0
- package/src/util/wsMeta.ts +70 -0
- package/tsconfig.build.json +28 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.scripts.json +19 -0
- package/tsconfig.spec.json +17 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
// (packageInfo imports will be kept as-is, user-editable)
|
|
6
|
+
|
|
7
|
+
import { packageInfo as apiInfo } from '@pezkuwi/api/packageInfo';
|
|
8
|
+
import { packageInfo as providerInfo } from '@pezkuwi/rpc-provider/packageInfo';
|
|
9
|
+
import { packageInfo as typesInfo } from '@pezkuwi/types/packageInfo';
|
|
10
|
+
import { detectPackage } from '@pezkuwi/util';
|
|
11
|
+
|
|
12
|
+
import { packageInfo } from './packageInfo.js';
|
|
13
|
+
|
|
14
|
+
detectPackage(packageInfo, null, [apiInfo, providerInfo, typesInfo]);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/calls';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedCall<ApiType extends ApiTypes> = AugmentedCall<ApiType>;
|
|
10
|
+
export type __DecoratedCallBase<ApiType extends ApiTypes> = DecoratedCallBase<ApiType>;
|
|
11
|
+
|
|
12
|
+
declare module '@polkadot/api-base/types/calls' {
|
|
13
|
+
interface AugmentedCalls<ApiType extends ApiTypes> {
|
|
14
|
+
{{#each modules}}
|
|
15
|
+
/** {{{sectionHash}}}/{{{sectionVersion}}} */
|
|
16
|
+
{{{name}}}: {
|
|
17
|
+
{{#each items}}
|
|
18
|
+
{{> docs}}
|
|
19
|
+
{{{name}}}: AugmentedCall<ApiType, ({{{args}}}) => Observable<{{{type}}}>>;
|
|
20
|
+
{{/each}}
|
|
21
|
+
{{#unless @root.isStrict}}
|
|
22
|
+
/**
|
|
23
|
+
* Generic call
|
|
24
|
+
**/
|
|
25
|
+
[key: string]: DecoratedCallBase<ApiType>;
|
|
26
|
+
{{/unless}}
|
|
27
|
+
};
|
|
28
|
+
{{/each}}
|
|
29
|
+
} // AugmentedCalls
|
|
30
|
+
} // declare module
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/consts';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedConst<ApiType extends ApiTypes> = AugmentedConst<ApiType>;
|
|
10
|
+
|
|
11
|
+
declare module '@polkadot/api-base/types/consts' {
|
|
12
|
+
interface AugmentedConsts<ApiType extends ApiTypes> {
|
|
13
|
+
{{#each modules}}
|
|
14
|
+
{{{name}}}: {
|
|
15
|
+
{{#each items}}
|
|
16
|
+
{{> docs}}
|
|
17
|
+
{{{name}}}: {{{type}}} & AugmentedConst<ApiType>;
|
|
18
|
+
{{/each}}
|
|
19
|
+
{{#unless @root.isStrict}}
|
|
20
|
+
/**
|
|
21
|
+
* Generic const
|
|
22
|
+
**/
|
|
23
|
+
[key: string]: Codec;
|
|
24
|
+
{{/unless}}
|
|
25
|
+
};
|
|
26
|
+
{{/each}}
|
|
27
|
+
} // AugmentedConsts
|
|
28
|
+
} // declare module
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/errors';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedError<ApiType extends ApiTypes> = AugmentedError<ApiType>;
|
|
10
|
+
|
|
11
|
+
declare module '@polkadot/api-base/types/errors' {
|
|
12
|
+
interface AugmentedErrors<ApiType extends ApiTypes> {
|
|
13
|
+
{{#each modules}}
|
|
14
|
+
{{{name}}}: {
|
|
15
|
+
{{#each items}}
|
|
16
|
+
{{> docs}}
|
|
17
|
+
{{{name}}}: AugmentedError<ApiType>;
|
|
18
|
+
{{/each}}
|
|
19
|
+
{{#unless @root.isStrict}}
|
|
20
|
+
/**
|
|
21
|
+
* Generic error
|
|
22
|
+
**/
|
|
23
|
+
[key: string]: AugmentedError<ApiType>;
|
|
24
|
+
{{/unless}}
|
|
25
|
+
};
|
|
26
|
+
{{/each}}
|
|
27
|
+
} // AugmentedErrors
|
|
28
|
+
} // declare module
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/events';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedEvent<ApiType extends ApiTypes> = AugmentedEvent<ApiType>;
|
|
10
|
+
|
|
11
|
+
declare module '@polkadot/api-base/types/events' {
|
|
12
|
+
interface AugmentedEvents<ApiType extends ApiTypes> {
|
|
13
|
+
{{#each modules}}
|
|
14
|
+
{{{name}}}: {
|
|
15
|
+
{{#each items}}
|
|
16
|
+
{{> docs}}
|
|
17
|
+
{{{name}}}: AugmentedEvent<ApiType, {{{type}}}>;
|
|
18
|
+
{{/each}}
|
|
19
|
+
{{#unless @root.isStrict}}
|
|
20
|
+
/**
|
|
21
|
+
* Generic event
|
|
22
|
+
**/
|
|
23
|
+
[key: string]: AugmentedEvent<ApiType>;
|
|
24
|
+
{{/unless}}
|
|
25
|
+
};
|
|
26
|
+
{{/each}}
|
|
27
|
+
} // AugmentedEvents
|
|
28
|
+
} // declare module
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/types/types/registry';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
declare module '@polkadot/types/types/registry' {
|
|
10
|
+
interface InterfaceTypes {
|
|
11
|
+
{{#each items}}
|
|
12
|
+
{{{this}}}: {{{this}}};
|
|
13
|
+
{{/each}}
|
|
14
|
+
} // InterfaceTypes
|
|
15
|
+
} // declare module
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/types/lookup';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
declare module '@polkadot/types/lookup' {
|
|
10
|
+
{{#each items}}
|
|
11
|
+
{{{this}}}
|
|
12
|
+
|
|
13
|
+
{{/each}}
|
|
14
|
+
} // declare module
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/storage';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedQuery<ApiType extends ApiTypes> = AugmentedQuery<ApiType, () => unknown>;
|
|
10
|
+
export type __QueryableStorageEntry<ApiType extends ApiTypes> = QueryableStorageEntry<ApiType>;
|
|
11
|
+
|
|
12
|
+
declare module '@polkadot/api-base/types/storage' {
|
|
13
|
+
interface AugmentedQueries<ApiType extends ApiTypes> {
|
|
14
|
+
{{#each modules}}
|
|
15
|
+
{{{name}}}: {
|
|
16
|
+
{{#each items}}
|
|
17
|
+
{{> docs}}
|
|
18
|
+
{{{name}}}: {{{entryType}}}<ApiType, ({{{params}}}) => Observable<{{{returnType}}}>, [{{{args}}}]>{{#unless @root.isStrict}} & QueryableStorageEntry<ApiType, [{{{args}}}]>{{/unless}};
|
|
19
|
+
{{/each}}
|
|
20
|
+
{{#unless @root.isStrict}}
|
|
21
|
+
/**
|
|
22
|
+
* Generic query
|
|
23
|
+
**/
|
|
24
|
+
[key: string]: QueryableStorageEntry<ApiType>;
|
|
25
|
+
{{/unless}}
|
|
26
|
+
};
|
|
27
|
+
{{/each}}
|
|
28
|
+
} // AugmentedQueries
|
|
29
|
+
} // declare module
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/rpc-core/types/jsonrpc';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedRpc = AugmentedRpc<() => unknown>;
|
|
10
|
+
|
|
11
|
+
declare module '@polkadot/rpc-core/types/jsonrpc' {
|
|
12
|
+
interface RpcInterface {
|
|
13
|
+
{{#each modules}}
|
|
14
|
+
{{{name}}}: {
|
|
15
|
+
{{#each items}}
|
|
16
|
+
{{> docs}}
|
|
17
|
+
{{{name}}}: AugmentedRpc<{{#if generic}}<{{{generic}}}>{{/if}}({{{args}}}) => Observable<{{{type}}}>>;
|
|
18
|
+
{{/each}}
|
|
19
|
+
};
|
|
20
|
+
{{/each}}
|
|
21
|
+
} // RpcInterface
|
|
22
|
+
} // declare module
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{{> header }}
|
|
2
|
+
|
|
3
|
+
// import type lookup before we augment - in some environments
|
|
4
|
+
// this is required to allow for ambient/previous definitions
|
|
5
|
+
import '@polkadot/api-base/types/submittable';
|
|
6
|
+
|
|
7
|
+
{{{ importsAll }}}
|
|
8
|
+
|
|
9
|
+
export type __AugmentedSubmittable = AugmentedSubmittable<() => unknown>;
|
|
10
|
+
export type __SubmittableExtrinsic<ApiType extends ApiTypes> = SubmittableExtrinsic<ApiType>;
|
|
11
|
+
export type __SubmittableExtrinsicFunction<ApiType extends ApiTypes> = SubmittableExtrinsicFunction<ApiType>;
|
|
12
|
+
|
|
13
|
+
declare module '@polkadot/api-base/types/submittable' {
|
|
14
|
+
interface AugmentedSubmittables<ApiType extends ApiTypes> {
|
|
15
|
+
{{#each modules}}
|
|
16
|
+
{{{name}}}: {
|
|
17
|
+
{{#each items}}
|
|
18
|
+
{{> docs}}
|
|
19
|
+
{{{name}}}: AugmentedSubmittable<({{{params}}}) => SubmittableExtrinsic<ApiType>, [{{{args}}}]>;
|
|
20
|
+
{{/each}}
|
|
21
|
+
{{#unless @root.isStrict}}
|
|
22
|
+
/**
|
|
23
|
+
* Generic tx
|
|
24
|
+
**/
|
|
25
|
+
[key: string]: SubmittableExtrinsicFunction<ApiType>;
|
|
26
|
+
{{/unless}}
|
|
27
|
+
};
|
|
28
|
+
{{/each}}
|
|
29
|
+
} // AugmentedSubmittables
|
|
30
|
+
} // declare module
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
|
|
6
|
+
import { assert } from '@pezkuwi/util';
|
|
7
|
+
|
|
8
|
+
export function assertDir (path: string): string {
|
|
9
|
+
assert(fs.existsSync(path) && fs.lstatSync(path).isDirectory(), `${path} is not a directory`);
|
|
10
|
+
|
|
11
|
+
return path;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function assertFile (path: string): string {
|
|
15
|
+
assert(fs.existsSync(path) && fs.lstatSync(path).isFile(), `${path} is not a file`);
|
|
16
|
+
|
|
17
|
+
return path;
|
|
18
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { TypeRegistry } from '@pezkuwi/types/create';
|
|
7
|
+
|
|
8
|
+
import { getSimilarTypes } from './derived.js';
|
|
9
|
+
|
|
10
|
+
describe('getSimilarTypes', (): void => {
|
|
11
|
+
const registry = new TypeRegistry();
|
|
12
|
+
const mockImports = {
|
|
13
|
+
codecTypes: {},
|
|
14
|
+
definitions: {},
|
|
15
|
+
extrinsicTypes: {},
|
|
16
|
+
genericTypes: {},
|
|
17
|
+
ignoredTypes: [],
|
|
18
|
+
localTypes: {},
|
|
19
|
+
lookupTypes: {},
|
|
20
|
+
metadataTypes: {},
|
|
21
|
+
primitiveTypes: {},
|
|
22
|
+
typeToModule: {},
|
|
23
|
+
typesTypes: {}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
it('handles nested Tuples', (): void => {
|
|
27
|
+
expect(getSimilarTypes(registry, {}, '(AccountId, (Balance, u32), u64)', mockImports)).toEqual([
|
|
28
|
+
'ITuple<[AccountId, ITuple<[Balance, u32]>, u64]>',
|
|
29
|
+
'[AccountId | string | Uint8Array, ITuple<[Balance, u32]> | [Balance | AnyNumber | Uint8Array, u32 | AnyNumber | Uint8Array], u64 | AnyNumber | Uint8Array]'
|
|
30
|
+
]);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('handles vectors of slices', (): void => {
|
|
34
|
+
expect(getSimilarTypes(registry, {}, 'Vec<[u8;4]>', mockImports)).toEqual([
|
|
35
|
+
'Vec<U8aFixed>'
|
|
36
|
+
]);
|
|
37
|
+
expect(getSimilarTypes(registry, {}, 'Vec<[Balance;8]>', mockImports)).toEqual([
|
|
38
|
+
'Vec<Vec<Balance>>'
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('handles structs', (): void => {
|
|
43
|
+
expect(getSimilarTypes(registry, {}, '{ "a": "u8", "b": "Vec<u8>" }', mockImports)).toEqual([
|
|
44
|
+
`{
|
|
45
|
+
readonly a: u8;
|
|
46
|
+
readonly b: Bytes;
|
|
47
|
+
} & Struct`, '{ a?: any; b?: any }', 'string', 'Uint8Array'
|
|
48
|
+
]);
|
|
49
|
+
});
|
|
50
|
+
it('handles vectors of structs', (): void => {
|
|
51
|
+
expect(getSimilarTypes(registry, {}, 'Vec<{ "a": "H256", "b": "Vec<H256>" }>', mockImports)).toEqual([
|
|
52
|
+
`Vec<{
|
|
53
|
+
readonly a: H256;
|
|
54
|
+
readonly b: Vec<H256>;
|
|
55
|
+
} & Struct>`
|
|
56
|
+
]);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/types authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { CodecClass, Registry } from '@pezkuwi/types/types';
|
|
5
|
+
import type { UInt } from '@pezkuwi/types-codec';
|
|
6
|
+
import type { TypeDef } from '@pezkuwi/types-create/types';
|
|
7
|
+
import type { ModuleTypes, TypeImports } from './imports.js';
|
|
8
|
+
|
|
9
|
+
import { GenericAccountId, GenericCall, GenericLookupSource, GenericVote } from '@pezkuwi/types/generic';
|
|
10
|
+
import { AllConvictions } from '@pezkuwi/types/interfaces/democracy/definitions';
|
|
11
|
+
import { AbstractInt, bool, Compact, Enum, Null, Option, Struct, Tuple, Vec, WrapperKeepOpaque, WrapperOpaque } from '@pezkuwi/types-codec';
|
|
12
|
+
import { getTypeDef, TypeDefInfo } from '@pezkuwi/types-create';
|
|
13
|
+
import { isChildClass, stringify } from '@pezkuwi/util';
|
|
14
|
+
|
|
15
|
+
import { formatType } from './formatting.js';
|
|
16
|
+
import { setImports } from './imports.js';
|
|
17
|
+
|
|
18
|
+
function arrayToStrType (arr: string[]): string {
|
|
19
|
+
return `${arr.map((c) => `'${c}'`).join(' | ')}`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const voteConvictions = arrayToStrType(AllConvictions);
|
|
23
|
+
|
|
24
|
+
// Make types a little bit more flexible
|
|
25
|
+
// - if param instanceof AbstractInt, then param: u64 | Uint8array | AnyNumber
|
|
26
|
+
// etc
|
|
27
|
+
/** @internal */
|
|
28
|
+
export function getSimilarTypes (registry: Registry, definitions: Record<string, ModuleTypes>, _type: string, imports: TypeImports): string[] {
|
|
29
|
+
const typeParts = _type.split('::');
|
|
30
|
+
const type = typeParts[typeParts.length - 1];
|
|
31
|
+
const possibleTypes = [formatType(registry, definitions, type, imports)];
|
|
32
|
+
|
|
33
|
+
if (type === 'Extrinsic') {
|
|
34
|
+
setImports(definitions, imports, ['IExtrinsic']);
|
|
35
|
+
|
|
36
|
+
return ['Extrinsic', 'IExtrinsic', 'string', 'Uint8Array'];
|
|
37
|
+
} else if (type === 'Keys') {
|
|
38
|
+
// This one is weird... surely it should popup as a Tuple? (but either way better as defined hex)
|
|
39
|
+
return ['Keys', 'string', 'Uint8Array'];
|
|
40
|
+
} else if (type === 'StorageKey') {
|
|
41
|
+
// TODO We can do better
|
|
42
|
+
return ['StorageKey', 'string', 'Uint8Array', 'any'];
|
|
43
|
+
} else if (type === '()') {
|
|
44
|
+
return ['null'];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const Clazz = registry.createClass(type);
|
|
48
|
+
|
|
49
|
+
if (isChildClass(Vec, Clazz)) {
|
|
50
|
+
const vecDef = getTypeDef(type);
|
|
51
|
+
const subDef = (vecDef.sub) as TypeDef;
|
|
52
|
+
|
|
53
|
+
// this could be that we define a Vec type and refer to it by name
|
|
54
|
+
if (subDef) {
|
|
55
|
+
if (subDef.info === TypeDefInfo.Plain) {
|
|
56
|
+
possibleTypes.push(`(${getSimilarTypes(registry, definitions, subDef.type, imports).join(' | ')})[]`);
|
|
57
|
+
} else if (subDef.info === TypeDefInfo.Tuple) {
|
|
58
|
+
const subs = (subDef.sub as TypeDef[]).map(({ type }): string =>
|
|
59
|
+
getSimilarTypes(registry, definitions, type, imports).join(' | ')
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
possibleTypes.push(`([${subs.join(', ')}])[]`);
|
|
63
|
+
} else if (subDef.info === TypeDefInfo.Option || subDef.info === TypeDefInfo.Vec || subDef.info === TypeDefInfo.VecFixed) {
|
|
64
|
+
// TODO: Add possibleTypes so imports work
|
|
65
|
+
} else if (subDef.info === TypeDefInfo.Struct) {
|
|
66
|
+
// TODO: Add possibleTypes so imports work
|
|
67
|
+
} else {
|
|
68
|
+
throw new Error(`Unhandled subtype in Vec, ${stringify(subDef)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
} else if (isChildClass(Enum, Clazz)) {
|
|
72
|
+
const { defKeys, isBasic } = new (Clazz as CodecClass)(registry) as Enum;
|
|
73
|
+
const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
|
|
74
|
+
|
|
75
|
+
if (isBasic) {
|
|
76
|
+
possibleTypes.push(arrayToStrType(keys), 'number');
|
|
77
|
+
} else {
|
|
78
|
+
// TODO We don't really want any here, these should be expanded
|
|
79
|
+
possibleTypes.push(...keys.map((k) => `{ ${k}: any }`), 'string');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
possibleTypes.push('Uint8Array');
|
|
83
|
+
} else if (isChildClass(AbstractInt as unknown as CodecClass<UInt>, Clazz) || isChildClass(Compact, Clazz)) {
|
|
84
|
+
possibleTypes.push('AnyNumber', 'Uint8Array');
|
|
85
|
+
} else if (isChildClass(GenericLookupSource, Clazz)) {
|
|
86
|
+
possibleTypes.push('Address', 'AccountId', 'AccountIndex', 'LookupSource', 'string', 'Uint8Array');
|
|
87
|
+
} else if (isChildClass(GenericAccountId, Clazz)) {
|
|
88
|
+
possibleTypes.push('string', 'Uint8Array');
|
|
89
|
+
} else if (isChildClass(GenericCall, Clazz)) {
|
|
90
|
+
possibleTypes.push('IMethod', 'string', 'Uint8Array');
|
|
91
|
+
} else if (isChildClass(bool, Clazz)) {
|
|
92
|
+
possibleTypes.push('boolean', 'Uint8Array');
|
|
93
|
+
} else if (isChildClass(Null, Clazz)) {
|
|
94
|
+
possibleTypes.push('null');
|
|
95
|
+
} else if (isChildClass(Struct, Clazz)) {
|
|
96
|
+
const s = new (Clazz as CodecClass)(registry) as Struct;
|
|
97
|
+
const obj = s.defKeys.map((key): string => `${key}?: any`).join('; ');
|
|
98
|
+
|
|
99
|
+
possibleTypes.push(`{ ${obj} }`, 'string', 'Uint8Array');
|
|
100
|
+
} else if (isChildClass(Option, Clazz)) {
|
|
101
|
+
possibleTypes.push('null', 'Uint8Array');
|
|
102
|
+
|
|
103
|
+
const optDef = getTypeDef(type);
|
|
104
|
+
const subDef = (optDef.sub) as TypeDef;
|
|
105
|
+
|
|
106
|
+
if (subDef) {
|
|
107
|
+
possibleTypes.push(...getSimilarTypes(registry, definitions, subDef.type, imports));
|
|
108
|
+
} else {
|
|
109
|
+
possibleTypes.push('object', 'string');
|
|
110
|
+
}
|
|
111
|
+
} else if (isChildClass(GenericVote, Clazz)) {
|
|
112
|
+
possibleTypes.push(`{ aye: boolean; conviction?: ${voteConvictions} | number }`, 'boolean', 'string', 'Uint8Array');
|
|
113
|
+
} else if (isChildClass(WrapperKeepOpaque, Clazz) || isChildClass(WrapperOpaque, Clazz)) {
|
|
114
|
+
// TODO inspect container
|
|
115
|
+
possibleTypes.push('object', 'string', 'Uint8Array');
|
|
116
|
+
} else if (isChildClass(Uint8Array, Clazz)) {
|
|
117
|
+
possibleTypes.push('string', 'Uint8Array');
|
|
118
|
+
} else if (isChildClass(String, Clazz)) {
|
|
119
|
+
possibleTypes.push('string');
|
|
120
|
+
} else if (isChildClass(Tuple, Clazz)) {
|
|
121
|
+
const tupDef = getTypeDef(type);
|
|
122
|
+
const subDef = tupDef.sub;
|
|
123
|
+
|
|
124
|
+
// this could be that we define a Tuple type and refer to it by name
|
|
125
|
+
if (Array.isArray(subDef)) {
|
|
126
|
+
const subs = subDef.map(({ type }) => getSimilarTypes(registry, definitions, type, imports).join(' | '));
|
|
127
|
+
|
|
128
|
+
possibleTypes.push(`[${subs.join(', ')}]`);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return [...new Set(possibleTypes)];
|
|
133
|
+
}
|
package/src/util/docs.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import Handlebars from 'handlebars';
|
|
5
|
+
|
|
6
|
+
import { readTemplate } from './file.js';
|
|
7
|
+
|
|
8
|
+
Handlebars.registerPartial({
|
|
9
|
+
docs: Handlebars.compile(readTemplate('docs'))
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// empty export
|
|
13
|
+
export const __TYPEGEN_DUMMY_DOCS = 'DUMMY_DOCS';
|
package/src/util/file.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import fs from 'node:fs';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import process from 'node:process';
|
|
7
|
+
|
|
8
|
+
import { packageInfo } from '../packageInfo.js';
|
|
9
|
+
|
|
10
|
+
export function writeFile (dest: string, generator: () => string, noLog?: boolean): void {
|
|
11
|
+
!noLog && console.log(`${dest}\n\tGenerating`);
|
|
12
|
+
|
|
13
|
+
let generated = generator();
|
|
14
|
+
|
|
15
|
+
while (generated.includes('\n\n\n')) {
|
|
16
|
+
generated = generated.replace(/\n\n\n/g, '\n\n');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
!noLog && console.log('\tWriting');
|
|
20
|
+
|
|
21
|
+
fs.writeFileSync(dest, generated, { flag: 'w' });
|
|
22
|
+
|
|
23
|
+
!noLog && console.log('');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function readTemplate (template: string): string {
|
|
27
|
+
// Inside the api repo itself, it will be 'auto'
|
|
28
|
+
const rootDir = packageInfo.path === 'auto'
|
|
29
|
+
? path.join(process.cwd(), 'packages/typegen/src')
|
|
30
|
+
: packageInfo.path;
|
|
31
|
+
|
|
32
|
+
// NOTE With cjs in a subdir, search one lower as well
|
|
33
|
+
const file = ['./templates', '../templates']
|
|
34
|
+
.map((p) => path.join(rootDir, p, `${template}.hbs`))
|
|
35
|
+
.find((p) => fs.existsSync(p));
|
|
36
|
+
|
|
37
|
+
if (!file) {
|
|
38
|
+
throw new Error(`Unable to locate ${template}.hbs from ${rootDir}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return fs.readFileSync(file).toString();
|
|
42
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2017-2025 @polkadot/typegen authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
/// <reference types="@pezkuwi/dev-test/globals.d.ts" />
|
|
5
|
+
|
|
6
|
+
import { TypeRegistry } from '@pezkuwi/types';
|
|
7
|
+
|
|
8
|
+
import { formatType } from './formatting.js';
|
|
9
|
+
|
|
10
|
+
describe('formatType', (): void => {
|
|
11
|
+
const registry = new TypeRegistry();
|
|
12
|
+
|
|
13
|
+
it('handles nested Tuples', (): void => {
|
|
14
|
+
expect(
|
|
15
|
+
formatType(registry, {}, '(AccountId, (Balance, u32), u64)', {
|
|
16
|
+
codecTypes: {},
|
|
17
|
+
definitions: {},
|
|
18
|
+
extrinsicTypes: {},
|
|
19
|
+
genericTypes: {},
|
|
20
|
+
ignoredTypes: [],
|
|
21
|
+
localTypes: {},
|
|
22
|
+
lookupTypes: {},
|
|
23
|
+
metadataTypes: {},
|
|
24
|
+
primitiveTypes: {},
|
|
25
|
+
typeToModule: {},
|
|
26
|
+
typesTypes: {}
|
|
27
|
+
})
|
|
28
|
+
).toEqual('ITuple<[AccountId, ITuple<[Balance, u32]>, u64]>');
|
|
29
|
+
});
|
|
30
|
+
});
|