@pezkuwi/typegen 16.5.5 → 16.5.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.
Files changed (193) hide show
  1. package/LICENSE +201 -0
  2. package/augment.js +2 -0
  3. package/bundle.js +2 -0
  4. package/cjs/augment.d.ts +2 -0
  5. package/cjs/augment.js +4 -0
  6. package/cjs/bundle.d.ts +2 -0
  7. package/cjs/bundle.js +7 -0
  8. package/cjs/extractChain.js +44 -0
  9. package/cjs/fromChain.js +95 -0
  10. package/cjs/fromDefs.js +82 -0
  11. package/cjs/generate/consts.js +91 -0
  12. package/cjs/generate/errors.js +59 -0
  13. package/cjs/generate/events.js +139 -0
  14. package/cjs/generate/index.js +23 -0
  15. package/cjs/generate/interfaceRegistry.js +64 -0
  16. package/cjs/generate/lookup.js +240 -0
  17. package/cjs/generate/query.js +140 -0
  18. package/cjs/generate/rpc.js +115 -0
  19. package/cjs/generate/runtime.js +233 -0
  20. package/{build → cjs}/generate/tsDef.d.ts +1 -1
  21. package/cjs/generate/tsDef.js +254 -0
  22. package/cjs/generate/tx.js +121 -0
  23. package/cjs/generate/types.js +16 -0
  24. package/cjs/index.js +5 -0
  25. package/cjs/interfacesTs.js +28 -0
  26. package/cjs/metadataMd.js +652 -0
  27. package/cjs/package.json +3 -0
  28. package/cjs/packageDetect.js +8 -0
  29. package/cjs/packageInfo.js +4 -0
  30. package/cjs/util/assert.js +15 -0
  31. package/cjs/util/derived.js +128 -0
  32. package/cjs/util/docs.js +10 -0
  33. package/cjs/util/file.js +33 -0
  34. package/{build → cjs}/util/formatting.d.ts +1 -1
  35. package/cjs/util/formatting.js +237 -0
  36. package/cjs/util/imports.js +126 -0
  37. package/{build → cjs}/util/index.d.ts +1 -0
  38. package/cjs/util/index.js +39 -0
  39. package/cjs/util/initMeta.js +22 -0
  40. package/cjs/util/register.js +10 -0
  41. package/cjs/util/wsMeta.js +60 -0
  42. package/extractChain.d.ts +1 -0
  43. package/extractChain.js +40 -0
  44. package/fromChain.d.ts +1 -0
  45. package/fromChain.js +91 -0
  46. package/fromDefs.d.ts +1 -0
  47. package/fromDefs.js +78 -0
  48. package/generate/consts.d.ts +5 -0
  49. package/generate/consts.js +87 -0
  50. package/generate/errors.d.ts +4 -0
  51. package/generate/errors.js +55 -0
  52. package/generate/events.d.ts +5 -0
  53. package/generate/events.js +135 -0
  54. package/{src/generate/index.ts → generate/index.d.ts} +0 -3
  55. package/generate/index.js +10 -0
  56. package/generate/interfaceRegistry.d.ts +4 -0
  57. package/generate/interfaceRegistry.js +59 -0
  58. package/generate/lookup.d.ts +4 -0
  59. package/generate/lookup.js +235 -0
  60. package/generate/query.d.ts +5 -0
  61. package/generate/query.js +136 -0
  62. package/generate/rpc.d.ts +6 -0
  63. package/generate/rpc.js +110 -0
  64. package/generate/runtime.d.ts +7 -0
  65. package/generate/runtime.js +228 -0
  66. package/generate/tsDef.d.ts +16 -0
  67. package/generate/tsDef.js +246 -0
  68. package/generate/tx.d.ts +5 -0
  69. package/generate/tx.js +117 -0
  70. package/generate/types.d.ts +12 -0
  71. package/generate/types.js +13 -0
  72. package/index.d.ts +2 -0
  73. package/index.js +2 -0
  74. package/interfacesTs.d.ts +1 -0
  75. package/interfacesTs.js +24 -0
  76. package/metadataMd.d.ts +1 -0
  77. package/metadataMd.js +648 -0
  78. package/package.json +520 -24
  79. package/packageDetect.d.ts +1 -0
  80. package/{src/packageDetect.ts → packageDetect.js} +0 -8
  81. package/packageInfo.d.ts +6 -0
  82. package/packageInfo.js +1 -0
  83. package/scripts/{polkadot-types-chain-info.mjs → pezkuwi-types-chain-info.mjs} +1 -1
  84. package/scripts/{polkadot-types-from-chain.mjs → pezkuwi-types-from-chain.mjs} +1 -1
  85. package/scripts/{polkadot-types-from-defs.mjs → pezkuwi-types-from-defs.mjs} +1 -1
  86. package/{build/scripts/polkadot-types-internal-interfaces.mjs → scripts/pezkuwi-types-internal-interfaces.mjs} +1 -1
  87. package/{build/scripts/polkadot-types-internal-metadata.mjs → scripts/pezkuwi-types-internal-metadata.mjs} +1 -1
  88. package/{src/templates → templates}/calls.hbs +2 -2
  89. package/{src/templates → templates}/consts.hbs +2 -2
  90. package/{src/templates → templates}/errors.hbs +2 -2
  91. package/{src/templates → templates}/events.hbs +2 -2
  92. package/{src/templates → templates}/interfaceRegistry.hbs +2 -2
  93. package/{src/templates → templates}/lookup/types.hbs +2 -2
  94. package/{src/templates → templates}/query.hbs +2 -2
  95. package/{src/templates → templates}/rpc.hbs +2 -2
  96. package/{src/templates → templates}/tx.hbs +2 -2
  97. package/util/assert.d.ts +2 -0
  98. package/util/assert.js +10 -0
  99. package/util/derived.d.ts +4 -0
  100. package/util/derived.js +125 -0
  101. package/util/docs.d.ts +1 -0
  102. package/util/docs.js +6 -0
  103. package/util/file.d.ts +2 -0
  104. package/util/file.js +28 -0
  105. package/util/formatting.d.ts +11 -0
  106. package/util/formatting.js +230 -0
  107. package/util/imports.d.ts +23 -0
  108. package/util/imports.js +121 -0
  109. package/{src/util/index.ts → util/index.d.ts} +6 -8
  110. package/util/index.js +34 -0
  111. package/util/initMeta.d.ts +12 -0
  112. package/util/initMeta.js +19 -0
  113. package/util/register.d.ts +4 -0
  114. package/util/register.js +7 -0
  115. package/util/wsMeta.d.ts +4 -0
  116. package/util/wsMeta.js +55 -0
  117. package/build/scripts/polkadot-types-chain-info.mjs +0 -7
  118. package/build/scripts/polkadot-types-from-chain.mjs +0 -7
  119. package/build/scripts/polkadot-types-from-defs.mjs +0 -7
  120. package/scripts/polkadot-types-internal-interfaces.mjs +0 -7
  121. package/scripts/polkadot-types-internal-metadata.mjs +0 -7
  122. package/src/augment.ts +0 -5
  123. package/src/bundle.ts +0 -5
  124. package/src/extractChain.ts +0 -54
  125. package/src/fromChain.ts +0 -123
  126. package/src/fromDefs.ts +0 -106
  127. package/src/generate/consts.ts +0 -112
  128. package/src/generate/errors.ts +0 -75
  129. package/src/generate/events.ts +0 -165
  130. package/src/generate/interfaceRegistry.ts +0 -85
  131. package/src/generate/lookup.ts +0 -294
  132. package/src/generate/query.ts +0 -169
  133. package/src/generate/rpc.ts +0 -158
  134. package/src/generate/runtime.ts +0 -284
  135. package/src/generate/tsDef.ts +0 -321
  136. package/src/generate/tx.ts +0 -152
  137. package/src/generate/types.ts +0 -26
  138. package/src/index.ts +0 -6
  139. package/src/interfacesTs.ts +0 -35
  140. package/src/metadataMd.ts +0 -844
  141. package/src/packageInfo.ts +0 -6
  142. package/src/util/assert.ts +0 -18
  143. package/src/util/derived.spec.ts +0 -58
  144. package/src/util/derived.ts +0 -133
  145. package/src/util/docs.ts +0 -13
  146. package/src/util/file.ts +0 -42
  147. package/src/util/formatting.spec.ts +0 -30
  148. package/src/util/formatting.ts +0 -295
  149. package/src/util/imports.ts +0 -164
  150. package/src/util/initMeta.ts +0 -37
  151. package/src/util/register.ts +0 -12
  152. package/src/util/wsMeta.ts +0 -70
  153. package/tsconfig.build.json +0 -28
  154. package/tsconfig.build.tsbuildinfo +0 -1
  155. package/tsconfig.scripts.json +0 -19
  156. package/tsconfig.spec.json +0 -17
  157. /package/{build/augment.d.ts → augment.d.ts} +0 -0
  158. /package/{build/bundle.d.ts → bundle.d.ts} +0 -0
  159. /package/{build → cjs}/extractChain.d.ts +0 -0
  160. /package/{build → cjs}/fromChain.d.ts +0 -0
  161. /package/{build → cjs}/fromDefs.d.ts +0 -0
  162. /package/{build → cjs}/generate/consts.d.ts +0 -0
  163. /package/{build → cjs}/generate/errors.d.ts +0 -0
  164. /package/{build → cjs}/generate/events.d.ts +0 -0
  165. /package/{build → cjs}/generate/index.d.ts +0 -0
  166. /package/{build → cjs}/generate/interfaceRegistry.d.ts +0 -0
  167. /package/{build → cjs}/generate/lookup.d.ts +0 -0
  168. /package/{build → cjs}/generate/query.d.ts +0 -0
  169. /package/{build → cjs}/generate/rpc.d.ts +0 -0
  170. /package/{build → cjs}/generate/runtime.d.ts +0 -0
  171. /package/{build → cjs}/generate/tx.d.ts +0 -0
  172. /package/{build → cjs}/generate/types.d.ts +0 -0
  173. /package/{build → cjs}/index.d.ts +0 -0
  174. /package/{build → cjs}/interfacesTs.d.ts +0 -0
  175. /package/{build → cjs}/metadataMd.d.ts +0 -0
  176. /package/{build → cjs}/packageDetect.d.ts +0 -0
  177. /package/{build → cjs}/packageInfo.d.ts +0 -0
  178. /package/{build → cjs}/util/assert.d.ts +0 -0
  179. /package/{build → cjs}/util/derived.d.ts +0 -0
  180. /package/{build → cjs}/util/docs.d.ts +0 -0
  181. /package/{build → cjs}/util/file.d.ts +0 -0
  182. /package/{build → cjs}/util/imports.d.ts +0 -0
  183. /package/{build → cjs}/util/initMeta.d.ts +0 -0
  184. /package/{build → cjs}/util/register.d.ts +0 -0
  185. /package/{build → cjs}/util/wsMeta.d.ts +0 -0
  186. /package/{src/templates → templates}/docs.hbs +0 -0
  187. /package/{src/templates → templates}/header.hbs +0 -0
  188. /package/{src/templates → templates}/lookup/defs-named.hbs +0 -0
  189. /package/{src/templates → templates}/lookup/defs.hbs +0 -0
  190. /package/{src/templates → templates}/lookup/index.hbs +0 -0
  191. /package/{src/templates → templates}/tsDef/index.hbs +0 -0
  192. /package/{src/templates → templates}/tsDef/moduleTypes.hbs +0 -0
  193. /package/{src/templates → templates}/tsDef/types.hbs +0 -0
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertDir = assertDir;
4
+ exports.assertFile = assertFile;
5
+ const tslib_1 = require("tslib");
6
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
7
+ const util_1 = require("@pezkuwi/util");
8
+ function assertDir(path) {
9
+ (0, util_1.assert)(node_fs_1.default.existsSync(path) && node_fs_1.default.lstatSync(path).isDirectory(), `${path} is not a directory`);
10
+ return path;
11
+ }
12
+ function assertFile(path) {
13
+ (0, util_1.assert)(node_fs_1.default.existsSync(path) && node_fs_1.default.lstatSync(path).isFile(), `${path} is not a file`);
14
+ return path;
15
+ }
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSimilarTypes = getSimilarTypes;
4
+ const generic_1 = require("@pezkuwi/types/generic");
5
+ const definitions_1 = require("@pezkuwi/types/interfaces/democracy/definitions");
6
+ const types_codec_1 = require("@pezkuwi/types-codec");
7
+ const types_create_1 = require("@pezkuwi/types-create");
8
+ const util_1 = require("@pezkuwi/util");
9
+ const formatting_js_1 = require("./formatting.js");
10
+ const imports_js_1 = require("./imports.js");
11
+ function arrayToStrType(arr) {
12
+ return `${arr.map((c) => `'${c}'`).join(' | ')}`;
13
+ }
14
+ const voteConvictions = arrayToStrType(definitions_1.AllConvictions);
15
+ /** @internal */
16
+ function getSimilarTypes(registry, definitions, _type, imports) {
17
+ const typeParts = _type.split('::');
18
+ const type = typeParts[typeParts.length - 1];
19
+ const possibleTypes = [(0, formatting_js_1.formatType)(registry, definitions, type, imports)];
20
+ if (type === 'Extrinsic') {
21
+ (0, imports_js_1.setImports)(definitions, imports, ['IExtrinsic']);
22
+ return ['Extrinsic', 'IExtrinsic', 'string', 'Uint8Array'];
23
+ }
24
+ else if (type === 'Keys') {
25
+ // This one is weird... surely it should popup as a Tuple? (but either way better as defined hex)
26
+ return ['Keys', 'string', 'Uint8Array'];
27
+ }
28
+ else if (type === 'StorageKey') {
29
+ // TODO We can do better
30
+ return ['StorageKey', 'string', 'Uint8Array', 'any'];
31
+ }
32
+ else if (type === '()') {
33
+ return ['null'];
34
+ }
35
+ const Clazz = registry.createClass(type);
36
+ if ((0, util_1.isChildClass)(types_codec_1.Vec, Clazz)) {
37
+ const vecDef = (0, types_create_1.getTypeDef)(type);
38
+ const subDef = (vecDef.sub);
39
+ // this could be that we define a Vec type and refer to it by name
40
+ if (subDef) {
41
+ if (subDef.info === types_create_1.TypeDefInfo.Plain) {
42
+ possibleTypes.push(`(${getSimilarTypes(registry, definitions, subDef.type, imports).join(' | ')})[]`);
43
+ }
44
+ else if (subDef.info === types_create_1.TypeDefInfo.Tuple) {
45
+ const subs = subDef.sub.map(({ type }) => getSimilarTypes(registry, definitions, type, imports).join(' | '));
46
+ possibleTypes.push(`([${subs.join(', ')}])[]`);
47
+ }
48
+ else if (subDef.info === types_create_1.TypeDefInfo.Option || subDef.info === types_create_1.TypeDefInfo.Vec || subDef.info === types_create_1.TypeDefInfo.VecFixed) {
49
+ // TODO: Add possibleTypes so imports work
50
+ }
51
+ else if (subDef.info === types_create_1.TypeDefInfo.Struct) {
52
+ // TODO: Add possibleTypes so imports work
53
+ }
54
+ else {
55
+ throw new Error(`Unhandled subtype in Vec, ${(0, util_1.stringify)(subDef)}`);
56
+ }
57
+ }
58
+ }
59
+ else if ((0, util_1.isChildClass)(types_codec_1.Enum, Clazz)) {
60
+ const { defKeys, isBasic } = new Clazz(registry);
61
+ const keys = defKeys.filter((v) => !v.startsWith('__Unused'));
62
+ if (isBasic) {
63
+ possibleTypes.push(arrayToStrType(keys), 'number');
64
+ }
65
+ else {
66
+ // TODO We don't really want any here, these should be expanded
67
+ possibleTypes.push(...keys.map((k) => `{ ${k}: any }`), 'string');
68
+ }
69
+ possibleTypes.push('Uint8Array');
70
+ }
71
+ else if ((0, util_1.isChildClass)(types_codec_1.AbstractInt, Clazz) || (0, util_1.isChildClass)(types_codec_1.Compact, Clazz)) {
72
+ possibleTypes.push('AnyNumber', 'Uint8Array');
73
+ }
74
+ else if ((0, util_1.isChildClass)(generic_1.GenericLookupSource, Clazz)) {
75
+ possibleTypes.push('Address', 'AccountId', 'AccountIndex', 'LookupSource', 'string', 'Uint8Array');
76
+ }
77
+ else if ((0, util_1.isChildClass)(generic_1.GenericAccountId, Clazz)) {
78
+ possibleTypes.push('string', 'Uint8Array');
79
+ }
80
+ else if ((0, util_1.isChildClass)(generic_1.GenericCall, Clazz)) {
81
+ possibleTypes.push('IMethod', 'string', 'Uint8Array');
82
+ }
83
+ else if ((0, util_1.isChildClass)(types_codec_1.bool, Clazz)) {
84
+ possibleTypes.push('boolean', 'Uint8Array');
85
+ }
86
+ else if ((0, util_1.isChildClass)(types_codec_1.Null, Clazz)) {
87
+ possibleTypes.push('null');
88
+ }
89
+ else if ((0, util_1.isChildClass)(types_codec_1.Struct, Clazz)) {
90
+ const s = new Clazz(registry);
91
+ const obj = s.defKeys.map((key) => `${key}?: any`).join('; ');
92
+ possibleTypes.push(`{ ${obj} }`, 'string', 'Uint8Array');
93
+ }
94
+ else if ((0, util_1.isChildClass)(types_codec_1.Option, Clazz)) {
95
+ possibleTypes.push('null', 'Uint8Array');
96
+ const optDef = (0, types_create_1.getTypeDef)(type);
97
+ const subDef = (optDef.sub);
98
+ if (subDef) {
99
+ possibleTypes.push(...getSimilarTypes(registry, definitions, subDef.type, imports));
100
+ }
101
+ else {
102
+ possibleTypes.push('object', 'string');
103
+ }
104
+ }
105
+ else if ((0, util_1.isChildClass)(generic_1.GenericVote, Clazz)) {
106
+ possibleTypes.push(`{ aye: boolean; conviction?: ${voteConvictions} | number }`, 'boolean', 'string', 'Uint8Array');
107
+ }
108
+ else if ((0, util_1.isChildClass)(types_codec_1.WrapperKeepOpaque, Clazz) || (0, util_1.isChildClass)(types_codec_1.WrapperOpaque, Clazz)) {
109
+ // TODO inspect container
110
+ possibleTypes.push('object', 'string', 'Uint8Array');
111
+ }
112
+ else if ((0, util_1.isChildClass)(Uint8Array, Clazz)) {
113
+ possibleTypes.push('string', 'Uint8Array');
114
+ }
115
+ else if ((0, util_1.isChildClass)(String, Clazz)) {
116
+ possibleTypes.push('string');
117
+ }
118
+ else if ((0, util_1.isChildClass)(types_codec_1.Tuple, Clazz)) {
119
+ const tupDef = (0, types_create_1.getTypeDef)(type);
120
+ const subDef = tupDef.sub;
121
+ // this could be that we define a Tuple type and refer to it by name
122
+ if (Array.isArray(subDef)) {
123
+ const subs = subDef.map(({ type }) => getSimilarTypes(registry, definitions, type, imports).join(' | '));
124
+ possibleTypes.push(`[${subs.join(', ')}]`);
125
+ }
126
+ }
127
+ return [...new Set(possibleTypes)];
128
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.__TYPEGEN_DUMMY_DOCS = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const handlebars_1 = tslib_1.__importDefault(require("handlebars"));
6
+ const file_js_1 = require("./file.js");
7
+ handlebars_1.default.registerPartial({
8
+ docs: handlebars_1.default.compile((0, file_js_1.readTemplate)('docs'))
9
+ });
10
+ exports.__TYPEGEN_DUMMY_DOCS = 'DUMMY_DOCS';
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeFile = writeFile;
4
+ exports.readTemplate = readTemplate;
5
+ const tslib_1 = require("tslib");
6
+ const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
7
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
8
+ const node_process_1 = tslib_1.__importDefault(require("node:process"));
9
+ const packageInfo_js_1 = require("../packageInfo.js");
10
+ function writeFile(dest, generator, noLog) {
11
+ !noLog && console.log(`${dest}\n\tGenerating`);
12
+ let generated = generator();
13
+ while (generated.includes('\n\n\n')) {
14
+ generated = generated.replace(/\n\n\n/g, '\n\n');
15
+ }
16
+ !noLog && console.log('\tWriting');
17
+ node_fs_1.default.writeFileSync(dest, generated, { flag: 'w' });
18
+ !noLog && console.log('');
19
+ }
20
+ function readTemplate(template) {
21
+ // Inside the api repo itself, it will be 'auto'
22
+ const rootDir = packageInfo_js_1.packageInfo.path === 'auto'
23
+ ? node_path_1.default.join(node_process_1.default.cwd(), 'packages/typegen/src')
24
+ : packageInfo_js_1.packageInfo.path;
25
+ // NOTE With cjs in a subdir, search one lower as well
26
+ const file = ['./templates', '../templates']
27
+ .map((p) => node_path_1.default.join(rootDir, p, `${template}.hbs`))
28
+ .find((p) => node_fs_1.default.existsSync(p));
29
+ if (!file) {
30
+ throw new Error(`Unable to locate ${template}.hbs from ${rootDir}`);
31
+ }
32
+ return node_fs_1.default.readFileSync(file).toString();
33
+ }
@@ -3,7 +3,7 @@ import type { TypeDef } from '@pezkuwi/types-create/types';
3
3
  import type { ModuleTypes, TypeImports } from './imports.js';
4
4
  export declare const HEADER: (type: "chain" | "defs") => string;
5
5
  /** @internal */
6
- export declare function exportInterface(lookupIndex: number, name: string, base: string, body?: string, withShortcut?: boolean): string;
6
+ export declare function exportInterface(lookupIndex: number | undefined, name: string | undefined, base: string, body?: string, withShortcut?: boolean): string;
7
7
  /**
8
8
  * Correctly format a given type
9
9
  */
@@ -0,0 +1,237 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HEADER = void 0;
4
+ exports.exportInterface = exportInterface;
5
+ exports.formatType = formatType;
6
+ const tslib_1 = require("tslib");
7
+ const handlebars_1 = tslib_1.__importDefault(require("handlebars"));
8
+ const typesCodec = tslib_1.__importStar(require("@pezkuwi/types-codec"));
9
+ const types_create_1 = require("@pezkuwi/types-create");
10
+ const util_1 = require("@pezkuwi/util");
11
+ const file_js_1 = require("./file.js");
12
+ const imports_js_1 = require("./imports.js");
13
+ function rebrandTypeName(typeName) {
14
+ return typeName
15
+ // Main projects
16
+ .replace(/Polkadot/g, 'Pezkuwi')
17
+ .replace(/Substrate/g, 'Bizinikiwi')
18
+ .replace(/Cumulus/g, 'Pezcumulus')
19
+ // Networks
20
+ .replace(/Kusama/g, 'Dicle')
21
+ .replace(/Westend/g, 'Zagros')
22
+ .replace(/Rococo/g, 'Pezkuwichain')
23
+ // Chain types
24
+ .replace(/Parachain/g, 'Teyrchain')
25
+ // Module prefixes (in PascalCase type names)
26
+ .replace(/Frame(?=[A-Z])/g, 'Pezframe')
27
+ .replace(/Pallet(?=[A-Z])/g, 'Pezpallet')
28
+ // Primitives prefixes (sp_ becomes Sp in type names)
29
+ .replace(/^Sp(?=[A-Z])/g, 'Pezsp')
30
+ .replace(/([^a-zA-Z])Sp(?=[A-Z])/g, '$1Pezsp')
31
+ // Client prefixes (sc_ becomes Sc in type names)
32
+ .replace(/^Sc(?=[A-Z])/g, 'Pezsc')
33
+ .replace(/([^a-zA-Z])Sc(?=[A-Z])/g, '$1Pezsc');
34
+ }
35
+ const NO_CODEC = ['Tuple', 'VecFixed'];
36
+ const ON_CODEC = Object.keys(typesCodec);
37
+ const ON_CODEC_TYPES = ['Codec', 'AnyJson', 'AnyFunction', 'AnyNumber', 'AnyString', 'AnyTuple', 'AnyU8a', 'ICompact', 'IEnum', 'IMap', 'INumber', 'IOption', 'IResult', 'ISet', 'IStruct', 'ITuple', 'IU8a', 'IVec', 'IMethod'];
38
+ const HEADER = (type) => `// Auto-generated via \`yarn pezkuwi-types-from-${type}\`, do not edit\n/* eslint-disable */\n\n`;
39
+ exports.HEADER = HEADER;
40
+ function extractImports({ imports, types }) {
41
+ const toplevel = [
42
+ ...Object.keys(imports.codecTypes),
43
+ ...Object.keys(imports.extrinsicTypes),
44
+ ...Object.keys(imports.genericTypes),
45
+ ...Object.keys(imports.metadataTypes),
46
+ ...Object.keys(imports.primitiveTypes)
47
+ ];
48
+ return [
49
+ {
50
+ file: '@pezkuwi/types',
51
+ types: toplevel.filter((n) => !NO_CODEC.includes(n) && !ON_CODEC.includes(n))
52
+ },
53
+ {
54
+ file: '@pezkuwi/types/lookup',
55
+ types: Object.keys(imports.lookupTypes)
56
+ },
57
+ {
58
+ file: '@pezkuwi/types/types',
59
+ types: Object.keys(imports.typesTypes).filter((n) => !ON_CODEC_TYPES.includes(n))
60
+ },
61
+ {
62
+ file: '@pezkuwi/types-codec',
63
+ types: toplevel.filter((n) => !NO_CODEC.includes(n) && ON_CODEC.includes(n))
64
+ },
65
+ {
66
+ file: '@pezkuwi/types-codec/types',
67
+ types: Object.keys(imports.typesTypes).filter((n) => ON_CODEC_TYPES.includes(n))
68
+ },
69
+ ...types
70
+ ]
71
+ .filter(({ types }) => types.length)
72
+ .sort(({ file }, b) => file.localeCompare(b.file))
73
+ .map(({ file, types }) => `import type { ${types.sort().join(', ')} } from '${file}';`);
74
+ }
75
+ handlebars_1.default.registerPartial({
76
+ header: handlebars_1.default.compile((0, file_js_1.readTemplate)('header'))
77
+ });
78
+ handlebars_1.default.registerHelper({
79
+ importsAll() {
80
+ return extractImports(this)
81
+ .join('\n');
82
+ },
83
+ importsPackage() {
84
+ return extractImports(this)
85
+ .filter((l) => !l.includes("from '."))
86
+ .join('\n ');
87
+ },
88
+ importsRelative() {
89
+ return extractImports(this)
90
+ .filter((l) => l.includes("from '."))
91
+ .join('\n');
92
+ },
93
+ trim(options) {
94
+ return options.fn(this).trim();
95
+ },
96
+ upper(options) {
97
+ return options.fn(this).toUpperCase();
98
+ }
99
+ });
100
+ /** @internal */
101
+ function exportInterface(lookupIndex = -1, name = '', base, body = '', withShortcut = false) {
102
+ // * @description extends [[${base}]]
103
+ const doc = withShortcut
104
+ ? ''
105
+ : `/** @name ${name}${lookupIndex !== -1 ? ` (${lookupIndex})` : ''} */\n`;
106
+ return `${doc}${withShortcut ? '' : `export interface ${name} extends ${base} `}{${body.length ? '\n' : ''}${body}${withShortcut ? ' ' : ''}}`;
107
+ }
108
+ function singleParamNotation(registry, wrapper, typeDef, definitions, imports, withShortcut) {
109
+ const sub = typeDef.sub;
110
+ const lookupName = rebrandTypeName(sub.lookupName || '');
111
+ (0, imports_js_1.setImports)(definitions, imports, [wrapper, lookupName]);
112
+ return (0, types_create_1.paramsNotation)(wrapper, lookupName || formatType(registry, definitions, sub.type, imports, withShortcut));
113
+ }
114
+ function dualParamsNotation(registry, wrapper, typeDef, definitions, imports, withShortcut) {
115
+ const [a, b] = typeDef.sub;
116
+ const aLookupName = rebrandTypeName(a.lookupName || '');
117
+ const bLookupName = rebrandTypeName(b.lookupName || '');
118
+ (0, imports_js_1.setImports)(definitions, imports, [wrapper, aLookupName, bLookupName]);
119
+ return (0, types_create_1.paramsNotation)(wrapper, [
120
+ aLookupName || formatType(registry, definitions, a.type, imports, withShortcut),
121
+ bLookupName || formatType(registry, definitions, b.type, imports, withShortcut)
122
+ ]);
123
+ }
124
+ const formatters = {
125
+ [types_create_1.TypeDefInfo.Compact]: (registry, typeDef, definitions, imports, withShortcut) => {
126
+ return singleParamNotation(registry, 'Compact', typeDef, definitions, imports, withShortcut);
127
+ },
128
+ [types_create_1.TypeDefInfo.DoNotConstruct]: (_registry, _typeDef, definitions, imports, _withShortcut) => {
129
+ (0, imports_js_1.setImports)(definitions, imports, ['DoNotConstruct']);
130
+ return 'DoNotConstruct';
131
+ },
132
+ [types_create_1.TypeDefInfo.Enum]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
133
+ if (typeDef.lookupName) {
134
+ return rebrandTypeName(typeDef.lookupName);
135
+ }
136
+ throw new Error(`TypeDefInfo.Enum: Parameter formatting not implemented on ${(0, util_1.stringify)(typeDef)}`);
137
+ },
138
+ [types_create_1.TypeDefInfo.Int]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
139
+ throw new Error(`TypeDefInfo.Int: Parameter formatting not implemented on ${(0, util_1.stringify)(typeDef)}`);
140
+ },
141
+ [types_create_1.TypeDefInfo.UInt]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
142
+ throw new Error(`TypeDefInfo.UInt: Parameter formatting not implemented on ${(0, util_1.stringify)(typeDef)}`);
143
+ },
144
+ [types_create_1.TypeDefInfo.Null]: (_registry, _typeDef, definitions, imports, _withShortcut) => {
145
+ (0, imports_js_1.setImports)(definitions, imports, ['Null']);
146
+ return 'Null';
147
+ },
148
+ [types_create_1.TypeDefInfo.Option]: (registry, typeDef, definitions, imports, withShortcut) => {
149
+ return singleParamNotation(registry, 'Option', typeDef, definitions, imports, withShortcut);
150
+ },
151
+ [types_create_1.TypeDefInfo.Plain]: (_registry, typeDef, definitions, imports, _withShortcut) => {
152
+ (0, imports_js_1.setImports)(definitions, imports, [rebrandTypeName(typeDef.type)]);
153
+ return rebrandTypeName(typeDef.type);
154
+ },
155
+ [types_create_1.TypeDefInfo.Range]: (registry, typeDef, definitions, imports, withShortcut) => {
156
+ return singleParamNotation(registry, 'Range', typeDef, definitions, imports, withShortcut);
157
+ },
158
+ [types_create_1.TypeDefInfo.RangeInclusive]: (registry, typeDef, definitions, imports, withShortcut) => {
159
+ return singleParamNotation(registry, 'RangeInclusive', typeDef, definitions, imports, withShortcut);
160
+ },
161
+ [types_create_1.TypeDefInfo.Set]: (_registry, typeDef, _definitions, _imports, _withShortcut) => {
162
+ throw new Error(`TypeDefInfo.Set: Parameter formatting not implemented on ${(0, util_1.stringify)(typeDef)}`);
163
+ },
164
+ [types_create_1.TypeDefInfo.Si]: (registry, typeDef, definitions, imports, withShortcut) => {
165
+ return formatType(registry, definitions, registry.lookup.getTypeDef(typeDef.type), imports, withShortcut);
166
+ },
167
+ [types_create_1.TypeDefInfo.Struct]: (registry, typeDef, definitions, imports, withShortcut) => {
168
+ if (typeDef.lookupName) {
169
+ return rebrandTypeName(typeDef.lookupName);
170
+ }
171
+ const sub = typeDef.sub;
172
+ (0, imports_js_1.setImports)(definitions, imports, ['Struct', ...sub.map(({ lookupName }) => rebrandTypeName(lookupName || ''))]);
173
+ return `{${withShortcut ? ' ' : '\n'}${sub.map(({ lookupName, name, type }, index) => [
174
+ name || `unknown${index}`,
175
+ rebrandTypeName(lookupName || '') || formatType(registry, definitions, type, imports, withShortcut)
176
+ ]).map(([k, t]) => `${withShortcut ? '' : ' readonly '}${k}: ${t};`).join(withShortcut ? ' ' : '\n')}${withShortcut ? ' ' : '\n '}} & Struct`;
177
+ },
178
+ [types_create_1.TypeDefInfo.Tuple]: (registry, typeDef, definitions, imports, withShortcut) => {
179
+ const sub = typeDef.sub;
180
+ (0, imports_js_1.setImports)(definitions, imports, ['ITuple', ...sub.map(({ lookupName }) => rebrandTypeName(lookupName || ''))]);
181
+ // `(a,b)` gets transformed into `ITuple<[a, b]>`
182
+ return (0, types_create_1.paramsNotation)('ITuple', `[${sub.map(({ lookupName, type }) => rebrandTypeName(lookupName || '') || formatType(registry, definitions, type, imports, withShortcut)).join(', ')}]`);
183
+ },
184
+ [types_create_1.TypeDefInfo.Vec]: (registry, typeDef, definitions, imports, withShortcut) => {
185
+ return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
186
+ },
187
+ [types_create_1.TypeDefInfo.VecFixed]: (registry, typeDef, definitions, imports, withShortcut) => {
188
+ const sub = typeDef.sub;
189
+ if (sub.type === 'u8') {
190
+ (0, imports_js_1.setImports)(definitions, imports, ['U8aFixed']);
191
+ return 'U8aFixed';
192
+ }
193
+ return singleParamNotation(registry, 'Vec', typeDef, definitions, imports, withShortcut);
194
+ },
195
+ [types_create_1.TypeDefInfo.BTreeMap]: (registry, typeDef, definitions, imports, withShortcut) => {
196
+ return dualParamsNotation(registry, 'BTreeMap', typeDef, definitions, imports, withShortcut);
197
+ },
198
+ [types_create_1.TypeDefInfo.BTreeSet]: (registry, typeDef, definitions, imports, withShortcut) => {
199
+ return singleParamNotation(registry, 'BTreeSet', typeDef, definitions, imports, withShortcut);
200
+ },
201
+ [types_create_1.TypeDefInfo.HashMap]: (registry, typeDef, definitions, imports, withShortcut) => {
202
+ return dualParamsNotation(registry, 'HashMap', typeDef, definitions, imports, withShortcut);
203
+ },
204
+ [types_create_1.TypeDefInfo.Linkage]: (registry, typeDef, definitions, imports, withShortcut) => {
205
+ return singleParamNotation(registry, 'Linkage', typeDef, definitions, imports, withShortcut);
206
+ },
207
+ [types_create_1.TypeDefInfo.Result]: (registry, typeDef, definitions, imports, withShortcut) => {
208
+ return dualParamsNotation(registry, 'Result', typeDef, definitions, imports, withShortcut);
209
+ },
210
+ [types_create_1.TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef, definitions, imports, withShortcut) => {
211
+ return singleParamNotation(registry, 'WrapperKeepOpaque', typeDef, definitions, imports, withShortcut);
212
+ },
213
+ [types_create_1.TypeDefInfo.WrapperOpaque]: (registry, typeDef, definitions, imports, withShortcut) => {
214
+ return singleParamNotation(registry, 'WrapperOpaque', typeDef, definitions, imports, withShortcut);
215
+ }
216
+ };
217
+ /**
218
+ * Correctly format a given type
219
+ */
220
+ /** @internal */
221
+ function formatType(registry, definitions, type, imports, withShortcut = false) {
222
+ let typeDef;
223
+ if ((0, util_1.isString)(type)) {
224
+ const _type = type.toString();
225
+ // If type is "unorthodox" (i.e. `{ something: any }` for an Enum input or `[a | b | c, d | e | f]` for a Tuple's similar types),
226
+ // we return it as-is
227
+ if (withShortcut && /(^{.+:.+})|^\([^,]+\)|^\(.+\)\[\]|^\[.+\]/.exec(_type) && !/\[\w+;\w+\]/.exec(_type)) {
228
+ return rebrandTypeName(_type);
229
+ }
230
+ typeDef = (0, types_create_1.getTypeDef)(type);
231
+ }
232
+ else {
233
+ typeDef = type;
234
+ }
235
+ (0, imports_js_1.setImports)(definitions, imports, [rebrandTypeName(typeDef.lookupName || typeDef.type)]);
236
+ return rebrandTypeName(formatters[typeDef.info](registry, typeDef, definitions, imports, withShortcut));
237
+ }
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setImports = setImports;
4
+ exports.createImports = createImports;
5
+ const tslib_1 = require("tslib");
6
+ const codecClasses = tslib_1.__importStar(require("@pezkuwi/types/codec"));
7
+ const extrinsicClasses = tslib_1.__importStar(require("@pezkuwi/types/extrinsic"));
8
+ const genericClasses = tslib_1.__importStar(require("@pezkuwi/types/generic"));
9
+ const primitiveClasses = tslib_1.__importStar(require("@pezkuwi/types/primitive"));
10
+ const types_create_1 = require("@pezkuwi/types-create");
11
+ function splitAlternatives(type) {
12
+ const alternatives = [];
13
+ let beginOfAlternative = 1;
14
+ let level = 0;
15
+ // we assume that the string starts with '['
16
+ for (let i = 1, count = type.length; i < count; i++) {
17
+ if (level === 0) {
18
+ switch (type[i]) {
19
+ case ']':
20
+ case ',':
21
+ case '|':
22
+ alternatives.push(type.substring(beginOfAlternative, i).trim());
23
+ beginOfAlternative = i + 1;
24
+ break;
25
+ }
26
+ }
27
+ switch (type[i]) {
28
+ case '[':
29
+ case '(':
30
+ case '<':
31
+ level++;
32
+ break;
33
+ case ']':
34
+ case ')':
35
+ case '>':
36
+ level--;
37
+ break;
38
+ }
39
+ }
40
+ return alternatives;
41
+ }
42
+ /** @internal */
43
+ function setImports(allDefs, imports, types) {
44
+ const { codecTypes, extrinsicTypes, genericTypes, ignoredTypes, localTypes, metadataTypes, primitiveTypes, typesTypes } = imports;
45
+ types.filter((t) => !!t).forEach((type) => {
46
+ if (ignoredTypes.includes(type)) {
47
+ // do nothing
48
+ }
49
+ else if (['AnyNumber', 'CallFunction', 'Codec', 'IExtrinsic', 'IMethod', 'ITuple'].includes(type)) {
50
+ typesTypes[type] = true;
51
+ }
52
+ else if (['Metadata', 'PortableRegistry'].includes(type)) {
53
+ metadataTypes[type] = true;
54
+ }
55
+ else if (codecClasses[type]) {
56
+ codecTypes[type] = true;
57
+ }
58
+ else if (extrinsicClasses[type]) {
59
+ extrinsicTypes[type] = true;
60
+ }
61
+ else if (genericClasses[type]) {
62
+ genericTypes[type] = true;
63
+ }
64
+ else if (primitiveClasses[type]) {
65
+ primitiveTypes[type] = true;
66
+ }
67
+ else if (type.startsWith('[') && type.includes('|')) {
68
+ const splitTypes = splitAlternatives(type);
69
+ setImports(allDefs, imports, splitTypes);
70
+ }
71
+ else if (type.includes('<') || type.includes('(') || type.includes('[')) {
72
+ // If the type is a bit special (tuple, fixed u8, nested type...), then we
73
+ // need to parse it with `getTypeDef`.
74
+ const typeDef = (0, types_create_1.getTypeDef)(type);
75
+ setImports(allDefs, imports, [types_create_1.TypeDefInfo[typeDef.info]]);
76
+ // TypeDef.sub is a `TypeDef | TypeDef[]`
77
+ if (Array.isArray(typeDef.sub)) {
78
+ typeDef.sub.forEach((subType) => setImports(allDefs, imports, [subType.lookupName || subType.type]));
79
+ }
80
+ else if (typeDef.sub && (typeDef.info !== types_create_1.TypeDefInfo.VecFixed || typeDef.sub.type !== 'u8')) {
81
+ // typeDef.sub is a TypeDef in this case
82
+ setImports(allDefs, imports, [typeDef.sub.lookupName || typeDef.sub.type]);
83
+ }
84
+ }
85
+ else {
86
+ // find this module inside the exports from the rest
87
+ const [moduleName] = Object.entries(allDefs).find(([, { types }]) => Object.keys(types).includes(type)) || [null];
88
+ if (moduleName) {
89
+ localTypes[moduleName][type] = true;
90
+ }
91
+ }
92
+ });
93
+ }
94
+ /** @internal */
95
+ function createImports(importDefinitions, { types } = { types: {} }) {
96
+ const definitions = {};
97
+ const typeToModule = {};
98
+ Object.entries(importDefinitions).forEach(([packagePath, packageDef]) => {
99
+ Object.entries(packageDef).forEach(([name, moduleDef]) => {
100
+ const fullName = `${packagePath}/${name}`;
101
+ definitions[fullName] = moduleDef;
102
+ Object.keys(moduleDef.types).forEach((type) => {
103
+ if (typeToModule[type]) {
104
+ console.warn(`\t\tWARN: Overwriting duplicated type '${type}' ${typeToModule[type]} -> ${fullName}`);
105
+ }
106
+ typeToModule[type] = fullName;
107
+ });
108
+ });
109
+ });
110
+ return {
111
+ codecTypes: {},
112
+ definitions,
113
+ extrinsicTypes: {},
114
+ genericTypes: {},
115
+ ignoredTypes: Object.keys(types),
116
+ localTypes: Object.keys(definitions).reduce((local, mod) => {
117
+ local[mod] = {};
118
+ return local;
119
+ }, {}),
120
+ lookupTypes: {},
121
+ metadataTypes: {},
122
+ primitiveTypes: {},
123
+ typeToModule,
124
+ typesTypes: {}
125
+ };
126
+ }
@@ -13,3 +13,4 @@ interface Cmp {
13
13
  };
14
14
  }
15
15
  export declare function compareName(a: Cmp, b: Cmp): number;
16
+ export declare function rebrandTypeName(typeName: string): string;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compareName = compareName;
4
+ exports.rebrandTypeName = rebrandTypeName;
5
+ const tslib_1 = require("tslib");
6
+ tslib_1.__exportStar(require("./assert.js"), exports);
7
+ tslib_1.__exportStar(require("./derived.js"), exports);
8
+ tslib_1.__exportStar(require("./docs.js"), exports);
9
+ tslib_1.__exportStar(require("./file.js"), exports);
10
+ tslib_1.__exportStar(require("./formatting.js"), exports);
11
+ tslib_1.__exportStar(require("./imports.js"), exports);
12
+ tslib_1.__exportStar(require("./initMeta.js"), exports);
13
+ tslib_1.__exportStar(require("./register.js"), exports);
14
+ tslib_1.__exportStar(require("./wsMeta.js"), exports);
15
+ function compareName(a, b) {
16
+ return a.name.toString().localeCompare(b.name.toString());
17
+ }
18
+ function rebrandTypeName(typeName) {
19
+ return typeName
20
+ // Main projects
21
+ .replace(/Polkadot/g, 'Pezkuwi')
22
+ .replace(/Substrate/g, 'Bizinikiwi')
23
+ .replace(/Cumulus/g, 'Pezcumulus')
24
+ // Networks
25
+ .replace(/Kusama/g, 'Dicle')
26
+ .replace(/Westend/g, 'Zagros')
27
+ .replace(/Rococo/g, 'Pezkuwichain')
28
+ // Chain types
29
+ .replace(/Parachain/g, 'Teyrchain')
30
+ // Module prefixes (in PascalCase type names)
31
+ .replace(/Frame(?=[A-Z])/g, 'Pezframe')
32
+ .replace(/Pallet(?=[A-Z])/g, 'Pezpallet')
33
+ // Primitives prefixes (sp_ becomes Sp in type names)
34
+ .replace(/^Sp(?=[A-Z])/g, 'Pezsp')
35
+ .replace(/([^a-zA-Z])Sp(?=[A-Z])/g, '$1Pezsp')
36
+ // Client prefixes (sc_ becomes Sc in type names)
37
+ .replace(/^Sc(?=[A-Z])/g, 'Pezsc')
38
+ .replace(/([^a-zA-Z])Sc(?=[A-Z])/g, '$1Pezsc');
39
+ }
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initMeta = initMeta;
4
+ const types_1 = require("@pezkuwi/types");
5
+ const register_js_1 = require("./register.js");
6
+ /**
7
+ * This helper method has been transitioned to work with V14, V15 and up.
8
+ */
9
+ function initMeta(staticMeta, extraTypes = {}) {
10
+ const registry = new types_1.TypeRegistry();
11
+ (0, register_js_1.registerDefinitions)(registry, extraTypes);
12
+ let metadata;
13
+ try {
14
+ const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', staticMeta).toU8a()).unwrap();
15
+ metadata = new types_1.Metadata(registry, opaqueMetadata.toHex());
16
+ }
17
+ catch {
18
+ metadata = new types_1.Metadata(registry, staticMeta);
19
+ }
20
+ registry.setMetadata(metadata);
21
+ return { metadata, registry };
22
+ }
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerDefinitions = registerDefinitions;
4
+ function registerDefinitions(registry, extras) {
5
+ Object.values(extras).forEach((def) => {
6
+ Object.values(def).forEach(({ types }) => {
7
+ registry.register(types);
8
+ });
9
+ });
10
+ }