@polkadot/types-create 6.12.2-3

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/package.json ADDED
@@ -0,0 +1,92 @@
1
+ {
2
+ "author": "Jaco Greeff <jacogr@gmail.com>",
3
+ "bugs": "https://github.com/polkadot-js/api/issues",
4
+ "contributors": [],
5
+ "description": "Type creator helpers",
6
+ "engines": {
7
+ "node": ">=14.0.0"
8
+ },
9
+ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/types-create#readme",
10
+ "license": "Apache-2.0",
11
+ "maintainers": [],
12
+ "name": "@polkadot/types-create",
13
+ "repository": {
14
+ "directory": "packages/types-create",
15
+ "type": "git",
16
+ "url": "https://github.com/polkadot-js/api.git"
17
+ },
18
+ "sideEffects": [
19
+ "./detectPackage.js",
20
+ "./detectPackage.cjs"
21
+ ],
22
+ "type": "module",
23
+ "version": "6.12.2-3",
24
+ "main": "index.js",
25
+ "dependencies": {
26
+ "@babel/runtime": "^7.16.3",
27
+ "@polkadot/util": "^8.1.2"
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "require": "./index.cjs",
32
+ "default": "./index.js"
33
+ },
34
+ "./bundle": {
35
+ "require": "./bundle.cjs",
36
+ "default": "./bundle.js"
37
+ },
38
+ "./bundle.d.ts": "./bundle.d.ts",
39
+ "./create": {
40
+ "require": "./create/index.cjs",
41
+ "default": "./create/index.js"
42
+ },
43
+ "./create/class": {
44
+ "require": "./create/class.cjs",
45
+ "default": "./create/class.js"
46
+ },
47
+ "./create/class.d.ts": "./create/class.d.ts",
48
+ "./create/index.d.ts": "./create/index.d.ts",
49
+ "./create/type": {
50
+ "require": "./create/type.cjs",
51
+ "default": "./create/type.js"
52
+ },
53
+ "./create/type.d.ts": "./create/type.d.ts",
54
+ "./detectPackage": {
55
+ "require": "./detectPackage.cjs",
56
+ "default": "./detectPackage.js"
57
+ },
58
+ "./detectPackage.d.ts": "./detectPackage.d.ts",
59
+ "./index.d.ts": "./index.d.ts",
60
+ "./package.json": "./package.json",
61
+ "./packageInfo": {
62
+ "require": "./packageInfo.cjs",
63
+ "default": "./packageInfo.js"
64
+ },
65
+ "./packageInfo.d.ts": "./packageInfo.d.ts",
66
+ "./types": {
67
+ "require": "./types.cjs",
68
+ "default": "./types.js"
69
+ },
70
+ "./types.d.ts": "./types.d.ts",
71
+ "./util": {
72
+ "require": "./util/index.cjs",
73
+ "default": "./util/index.js"
74
+ },
75
+ "./util/encodeTypes": {
76
+ "require": "./util/encodeTypes.cjs",
77
+ "default": "./util/encodeTypes.js"
78
+ },
79
+ "./util/encodeTypes.d.ts": "./util/encodeTypes.d.ts",
80
+ "./util/getTypeDef": {
81
+ "require": "./util/getTypeDef.cjs",
82
+ "default": "./util/getTypeDef.js"
83
+ },
84
+ "./util/getTypeDef.d.ts": "./util/getTypeDef.d.ts",
85
+ "./util/index.d.ts": "./util/index.d.ts",
86
+ "./util/typeSplit": {
87
+ "require": "./util/typeSplit.cjs",
88
+ "default": "./util/typeSplit.js"
89
+ },
90
+ "./util/typeSplit.d.ts": "./util/typeSplit.d.ts"
91
+ }
92
+ }
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.packageInfo = void 0;
7
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
8
+ // SPDX-License-Identifier: Apache-2.0
9
+ // Auto-generated by @polkadot/dev, do not edit
10
+ const packageInfo = {
11
+ name: '@polkadot/types-create',
12
+ version: '6.12.2-3'
13
+ };
14
+ exports.packageInfo = packageInfo;
@@ -0,0 +1,4 @@
1
+ export declare const packageInfo: {
2
+ name: string;
3
+ version: string;
4
+ };
package/packageInfo.js ADDED
@@ -0,0 +1,7 @@
1
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Auto-generated by @polkadot/dev, do not edit
4
+ export const packageInfo = {
5
+ name: '@polkadot/types-create',
6
+ version: '6.12.2-3'
7
+ };
package/types.cjs ADDED
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.TypeDefInfo = void 0;
7
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
8
+ // SPDX-License-Identifier: Apache-2.0
9
+ let TypeDefInfo;
10
+ exports.TypeDefInfo = TypeDefInfo;
11
+
12
+ (function (TypeDefInfo) {
13
+ TypeDefInfo[TypeDefInfo["BTreeMap"] = 0] = "BTreeMap";
14
+ TypeDefInfo[TypeDefInfo["BTreeSet"] = 1] = "BTreeSet";
15
+ TypeDefInfo[TypeDefInfo["Compact"] = 2] = "Compact";
16
+ TypeDefInfo[TypeDefInfo["DoNotConstruct"] = 3] = "DoNotConstruct";
17
+ TypeDefInfo[TypeDefInfo["Enum"] = 4] = "Enum";
18
+ TypeDefInfo[TypeDefInfo["HashMap"] = 5] = "HashMap";
19
+ TypeDefInfo[TypeDefInfo["Int"] = 6] = "Int";
20
+ TypeDefInfo[TypeDefInfo["Linkage"] = 7] = "Linkage";
21
+ TypeDefInfo[TypeDefInfo["Null"] = 8] = "Null";
22
+ TypeDefInfo[TypeDefInfo["Option"] = 9] = "Option";
23
+ TypeDefInfo[TypeDefInfo["Plain"] = 10] = "Plain";
24
+ TypeDefInfo[TypeDefInfo["Range"] = 11] = "Range";
25
+ TypeDefInfo[TypeDefInfo["Result"] = 12] = "Result";
26
+ TypeDefInfo[TypeDefInfo["Set"] = 13] = "Set";
27
+ TypeDefInfo[TypeDefInfo["Si"] = 14] = "Si";
28
+ TypeDefInfo[TypeDefInfo["Struct"] = 15] = "Struct";
29
+ TypeDefInfo[TypeDefInfo["Tuple"] = 16] = "Tuple";
30
+ TypeDefInfo[TypeDefInfo["UInt"] = 17] = "UInt";
31
+ TypeDefInfo[TypeDefInfo["Vec"] = 18] = "Vec";
32
+ TypeDefInfo[TypeDefInfo["VecFixed"] = 19] = "VecFixed";
33
+ TypeDefInfo[TypeDefInfo["WrapperOpaque"] = 20] = "WrapperOpaque";
34
+ })(TypeDefInfo || (exports.TypeDefInfo = TypeDefInfo = {}));
package/types.d.ts ADDED
@@ -0,0 +1,57 @@
1
+ import type { Codec, CodecClass, CodecRegistry, ICompact, INumber } from '@polkadot/types-codec/types';
2
+ export type { CodecCreateOptions as CreateOptions } from '@polkadot/types-codec/types';
3
+ export declare enum TypeDefInfo {
4
+ BTreeMap = 0,
5
+ BTreeSet = 1,
6
+ Compact = 2,
7
+ DoNotConstruct = 3,
8
+ Enum = 4,
9
+ HashMap = 5,
10
+ Int = 6,
11
+ Linkage = 7,
12
+ Null = 8,
13
+ Option = 9,
14
+ Plain = 10,
15
+ Range = 11,
16
+ Result = 12,
17
+ Set = 13,
18
+ Si = 14,
19
+ Struct = 15,
20
+ Tuple = 16,
21
+ UInt = 17,
22
+ Vec = 18,
23
+ VecFixed = 19,
24
+ WrapperOpaque = 20
25
+ }
26
+ export interface TypeDef {
27
+ alias?: Map<string, string>;
28
+ displayName?: string;
29
+ docs?: string[];
30
+ fallbackType?: string;
31
+ info: TypeDefInfo;
32
+ index?: number;
33
+ isFromSi?: boolean;
34
+ length?: number;
35
+ lookupIndex?: number;
36
+ lookupName?: string;
37
+ lookupNameRoot?: string;
38
+ name?: string;
39
+ namespace?: string;
40
+ sub?: TypeDef | TypeDef[];
41
+ type: string;
42
+ typeName?: string;
43
+ }
44
+ export interface ILookup {
45
+ getSiType(lookupId: ICompact<INumber> | string | number): {
46
+ def: {
47
+ asTuple: ICompact<INumber>[];
48
+ };
49
+ };
50
+ getTypeDef(lookupId: ICompact<INumber> | string | number): TypeDef;
51
+ }
52
+ export interface CreateRegistry extends CodecRegistry {
53
+ lookup: ILookup;
54
+ createLookupType(lookupId: ICompact<INumber> | number): string;
55
+ setLookup(lookup: ILookup): void;
56
+ getUnsafe<T extends Codec = Codec, K extends string = string>(name: K, withUnknown?: boolean, knownTypeDef?: TypeDef): CodecClass<T> | undefined;
57
+ }
package/types.js ADDED
@@ -0,0 +1,27 @@
1
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export let TypeDefInfo;
4
+
5
+ (function (TypeDefInfo) {
6
+ TypeDefInfo[TypeDefInfo["BTreeMap"] = 0] = "BTreeMap";
7
+ TypeDefInfo[TypeDefInfo["BTreeSet"] = 1] = "BTreeSet";
8
+ TypeDefInfo[TypeDefInfo["Compact"] = 2] = "Compact";
9
+ TypeDefInfo[TypeDefInfo["DoNotConstruct"] = 3] = "DoNotConstruct";
10
+ TypeDefInfo[TypeDefInfo["Enum"] = 4] = "Enum";
11
+ TypeDefInfo[TypeDefInfo["HashMap"] = 5] = "HashMap";
12
+ TypeDefInfo[TypeDefInfo["Int"] = 6] = "Int";
13
+ TypeDefInfo[TypeDefInfo["Linkage"] = 7] = "Linkage";
14
+ TypeDefInfo[TypeDefInfo["Null"] = 8] = "Null";
15
+ TypeDefInfo[TypeDefInfo["Option"] = 9] = "Option";
16
+ TypeDefInfo[TypeDefInfo["Plain"] = 10] = "Plain";
17
+ TypeDefInfo[TypeDefInfo["Range"] = 11] = "Range";
18
+ TypeDefInfo[TypeDefInfo["Result"] = 12] = "Result";
19
+ TypeDefInfo[TypeDefInfo["Set"] = 13] = "Set";
20
+ TypeDefInfo[TypeDefInfo["Si"] = 14] = "Si";
21
+ TypeDefInfo[TypeDefInfo["Struct"] = 15] = "Struct";
22
+ TypeDefInfo[TypeDefInfo["Tuple"] = 16] = "Tuple";
23
+ TypeDefInfo[TypeDefInfo["UInt"] = 17] = "UInt";
24
+ TypeDefInfo[TypeDefInfo["Vec"] = 18] = "Vec";
25
+ TypeDefInfo[TypeDefInfo["VecFixed"] = 19] = "VecFixed";
26
+ TypeDefInfo[TypeDefInfo["WrapperOpaque"] = 20] = "WrapperOpaque";
27
+ })(TypeDefInfo || (TypeDefInfo = {}));
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.encodeTypeDef = encodeTypeDef;
7
+ exports.paramsNotation = paramsNotation;
8
+ exports.withTypeString = withTypeString;
9
+
10
+ var _util = require("@polkadot/util");
11
+
12
+ var _types = require("../types.cjs");
13
+
14
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
15
+ // SPDX-License-Identifier: Apache-2.0
16
+ const stringIdentity = value => value.toString();
17
+
18
+ const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
19
+
20
+ function paramsNotation(outer, inner) {
21
+ let transform = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : stringIdentity;
22
+ return `${outer}${inner ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` : ''}`;
23
+ }
24
+
25
+ function encodeWithParams(CodecRegistry, typeDef, outer) {
26
+ const {
27
+ info,
28
+ sub
29
+ } = typeDef;
30
+
31
+ switch (info) {
32
+ case _types.TypeDefInfo.BTreeMap:
33
+ case _types.TypeDefInfo.BTreeSet:
34
+ case _types.TypeDefInfo.Compact:
35
+ case _types.TypeDefInfo.HashMap:
36
+ case _types.TypeDefInfo.Linkage:
37
+ case _types.TypeDefInfo.Option:
38
+ case _types.TypeDefInfo.Result:
39
+ case _types.TypeDefInfo.Vec:
40
+ case _types.TypeDefInfo.WrapperOpaque:
41
+ return paramsNotation(outer, sub, p => encodeTypeDef(CodecRegistry, p));
42
+ }
43
+
44
+ throw new Error(`Unable to encode ${(0, _util.stringify)(typeDef)} with params`);
45
+ }
46
+
47
+ function encodeSubTypes(CodecRegistry, sub, asEnum, extra) {
48
+ const names = sub.map(_ref => {
49
+ let {
50
+ name
51
+ } = _ref;
52
+ return name;
53
+ });
54
+ (0, _util.assert)(names.every(n => !!n), () => `Subtypes does not have consistent names, ${names.join(', ')}`);
55
+ const inner = (0, _util.objectSpread)({}, extra);
56
+
57
+ for (let i = 0; i < sub.length; i++) {
58
+ const def = sub[i];
59
+ inner[def.name] = encodeTypeDef(CodecRegistry, def);
60
+ }
61
+
62
+ return (0, _util.stringify)(asEnum ? {
63
+ _enum: inner
64
+ } : inner);
65
+ } // We setup a record here to ensure we have comprehensive coverage (any item not covered will result
66
+ // in a compile-time error with the missing index)
67
+
68
+
69
+ const encoders = {
70
+ [_types.TypeDefInfo.BTreeMap]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'BTreeMap'),
71
+ [_types.TypeDefInfo.BTreeSet]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'BTreeSet'),
72
+ [_types.TypeDefInfo.Compact]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Compact'),
73
+ [_types.TypeDefInfo.DoNotConstruct]: (CodecRegistry, _ref2) => {
74
+ let {
75
+ displayName,
76
+ lookupIndex,
77
+ lookupName
78
+ } = _ref2;
79
+ return `DoNotConstruct<${lookupName || displayName || ((0, _util.isUndefined)(lookupIndex) ? 'Unknown' : CodecRegistry.createLookupType(lookupIndex))}>`;
80
+ },
81
+ [_types.TypeDefInfo.Enum]: (CodecRegistry, _ref3) => {
82
+ let {
83
+ sub
84
+ } = _ref3;
85
+ (0, _util.assert)(sub && Array.isArray(sub), 'Unable to encode Enum type'); // c-like enums have all Null entries
86
+ // TODO We need to take the disciminant into account and auto-add empty entries
87
+
88
+ return sub.every(_ref4 => {
89
+ let {
90
+ type
91
+ } = _ref4;
92
+ return type === 'Null';
93
+ }) ? (0, _util.stringify)({
94
+ _enum: sub.map((_ref5, index) => {
95
+ let {
96
+ name
97
+ } = _ref5;
98
+ return `${name || `Empty${index}`}`;
99
+ })
100
+ }) : encodeSubTypes(CodecRegistry, sub, true);
101
+ },
102
+ [_types.TypeDefInfo.HashMap]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'HashMap'),
103
+ [_types.TypeDefInfo.Int]: (CodecRegistry, _ref6) => {
104
+ let {
105
+ length = 32
106
+ } = _ref6;
107
+ return `Int<${length}>`;
108
+ },
109
+ [_types.TypeDefInfo.Linkage]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Linkage'),
110
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
111
+ [_types.TypeDefInfo.Null]: (CodecRegistry, typeDef) => 'Null',
112
+ [_types.TypeDefInfo.Option]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Option'),
113
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
114
+ [_types.TypeDefInfo.Plain]: (CodecRegistry, _ref7) => {
115
+ let {
116
+ displayName,
117
+ type
118
+ } = _ref7;
119
+ return displayName || type;
120
+ },
121
+ [_types.TypeDefInfo.Range]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, typeDef.type.includes('RangeInclusive') ? 'RangeInclusive' : 'Range'),
122
+ [_types.TypeDefInfo.Result]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Result'),
123
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
124
+ [_types.TypeDefInfo.Set]: (CodecRegistry, _ref8) => {
125
+ let {
126
+ length = 8,
127
+ sub
128
+ } = _ref8;
129
+ (0, _util.assert)(sub && Array.isArray(sub), 'Unable to encode Set type');
130
+ return (0, _util.stringify)({
131
+ _set: sub.reduce((all, _ref9, count) => {
132
+ let {
133
+ index,
134
+ name
135
+ } = _ref9;
136
+ return (0, _util.objectSpread)(all, {
137
+ [`${name || `Unknown${index || count}`}`]: index || count
138
+ });
139
+ }, {
140
+ _bitLength: length || 8
141
+ })
142
+ });
143
+ },
144
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
145
+ [_types.TypeDefInfo.Si]: (CodecRegistry, _ref10) => {
146
+ let {
147
+ lookupName,
148
+ type
149
+ } = _ref10;
150
+ return lookupName || type;
151
+ },
152
+ [_types.TypeDefInfo.Struct]: (CodecRegistry, _ref11) => {
153
+ let {
154
+ alias,
155
+ sub
156
+ } = _ref11;
157
+ (0, _util.assert)(sub && Array.isArray(sub), 'Unable to encode Struct type');
158
+ return encodeSubTypes(CodecRegistry, sub, false, alias ? {
159
+ _alias: [...alias.entries()].reduce((all, _ref12) => {
160
+ let [k, v] = _ref12;
161
+ return (0, _util.objectSpread)(all, {
162
+ [k]: v
163
+ });
164
+ }, {})
165
+ } : {});
166
+ },
167
+ [_types.TypeDefInfo.Tuple]: (CodecRegistry, _ref13) => {
168
+ let {
169
+ sub
170
+ } = _ref13;
171
+ (0, _util.assert)(sub && Array.isArray(sub), 'Unable to encode Tuple type');
172
+ return `(${sub.map(type => encodeTypeDef(CodecRegistry, type)).join(',')})`;
173
+ },
174
+ [_types.TypeDefInfo.UInt]: (CodecRegistry, _ref14) => {
175
+ let {
176
+ length = 32
177
+ } = _ref14;
178
+ return `UInt<${length}>`;
179
+ },
180
+ [_types.TypeDefInfo.Vec]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Vec'),
181
+ [_types.TypeDefInfo.VecFixed]: (CodecRegistry, _ref15) => {
182
+ let {
183
+ length,
184
+ sub
185
+ } = _ref15;
186
+ (0, _util.assert)((0, _util.isNumber)(length) && !(0, _util.isUndefined)(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type');
187
+ return `[${sub.type};${length}]`;
188
+ },
189
+ [_types.TypeDefInfo.WrapperOpaque]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'WrapperOpaque')
190
+ };
191
+
192
+ function encodeType(CodecRegistry, typeDef) {
193
+ let withLookup = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
194
+ return withLookup && typeDef.lookupName ? typeDef.lookupName : encoders[typeDef.info](CodecRegistry, typeDef);
195
+ }
196
+
197
+ function encodeTypeDef(CodecRegistry, typeDef) {
198
+ // In the case of contracts we do have the unfortunate situation where the displayName would
199
+ // refer to "Option" when it is an option. For these, string it out, only using when actually
200
+ // not a top-level element to be used
201
+ return typeDef.displayName && !INFO_WRAP.some(i => typeDef.displayName === i) ? typeDef.displayName : encodeType(CodecRegistry, typeDef);
202
+ }
203
+
204
+ function withTypeString(CodecRegistry, typeDef) {
205
+ return (0, _util.objectSpread)({}, typeDef, {
206
+ type: encodeType(CodecRegistry, typeDef, false)
207
+ });
208
+ }
@@ -0,0 +1,5 @@
1
+ import type { CodecRegistry } from '@polkadot/types-codec/types';
2
+ import type { TypeDef } from '@polkadot/types-create/types';
3
+ export declare function paramsNotation<T>(outer: string, inner?: T | T[], transform?: (_: T) => string): string;
4
+ export declare function encodeTypeDef(CodecRegistry: CodecRegistry, typeDef: TypeDef): string;
5
+ export declare function withTypeString(CodecRegistry: CodecRegistry, typeDef: Omit<TypeDef, 'type'>): TypeDef;
@@ -0,0 +1,159 @@
1
+ // Copyright 2017-2021 @polkadot/types-create authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { assert, isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
4
+ import { TypeDefInfo } from "../types.js";
5
+
6
+ const stringIdentity = value => value.toString();
7
+
8
+ const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
9
+ export function paramsNotation(outer, inner, transform = stringIdentity) {
10
+ return `${outer}${inner ? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>` : ''}`;
11
+ }
12
+
13
+ function encodeWithParams(CodecRegistry, typeDef, outer) {
14
+ const {
15
+ info,
16
+ sub
17
+ } = typeDef;
18
+
19
+ switch (info) {
20
+ case TypeDefInfo.BTreeMap:
21
+ case TypeDefInfo.BTreeSet:
22
+ case TypeDefInfo.Compact:
23
+ case TypeDefInfo.HashMap:
24
+ case TypeDefInfo.Linkage:
25
+ case TypeDefInfo.Option:
26
+ case TypeDefInfo.Result:
27
+ case TypeDefInfo.Vec:
28
+ case TypeDefInfo.WrapperOpaque:
29
+ return paramsNotation(outer, sub, p => encodeTypeDef(CodecRegistry, p));
30
+ }
31
+
32
+ throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
33
+ }
34
+
35
+ function encodeSubTypes(CodecRegistry, sub, asEnum, extra) {
36
+ const names = sub.map(({
37
+ name
38
+ }) => name);
39
+ assert(names.every(n => !!n), () => `Subtypes does not have consistent names, ${names.join(', ')}`);
40
+ const inner = objectSpread({}, extra);
41
+
42
+ for (let i = 0; i < sub.length; i++) {
43
+ const def = sub[i];
44
+ inner[def.name] = encodeTypeDef(CodecRegistry, def);
45
+ }
46
+
47
+ return stringify(asEnum ? {
48
+ _enum: inner
49
+ } : inner);
50
+ } // We setup a record here to ensure we have comprehensive coverage (any item not covered will result
51
+ // in a compile-time error with the missing index)
52
+
53
+
54
+ const encoders = {
55
+ [TypeDefInfo.BTreeMap]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'BTreeMap'),
56
+ [TypeDefInfo.BTreeSet]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'BTreeSet'),
57
+ [TypeDefInfo.Compact]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Compact'),
58
+ [TypeDefInfo.DoNotConstruct]: (CodecRegistry, {
59
+ displayName,
60
+ lookupIndex,
61
+ lookupName
62
+ }) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : CodecRegistry.createLookupType(lookupIndex))}>`,
63
+ [TypeDefInfo.Enum]: (CodecRegistry, {
64
+ sub
65
+ }) => {
66
+ assert(sub && Array.isArray(sub), 'Unable to encode Enum type'); // c-like enums have all Null entries
67
+ // TODO We need to take the disciminant into account and auto-add empty entries
68
+
69
+ return sub.every(({
70
+ type
71
+ }) => type === 'Null') ? stringify({
72
+ _enum: sub.map(({
73
+ name
74
+ }, index) => `${name || `Empty${index}`}`)
75
+ }) : encodeSubTypes(CodecRegistry, sub, true);
76
+ },
77
+ [TypeDefInfo.HashMap]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'HashMap'),
78
+ [TypeDefInfo.Int]: (CodecRegistry, {
79
+ length = 32
80
+ }) => `Int<${length}>`,
81
+ [TypeDefInfo.Linkage]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Linkage'),
82
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
83
+ [TypeDefInfo.Null]: (CodecRegistry, typeDef) => 'Null',
84
+ [TypeDefInfo.Option]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Option'),
85
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
86
+ [TypeDefInfo.Plain]: (CodecRegistry, {
87
+ displayName,
88
+ type
89
+ }) => displayName || type,
90
+ [TypeDefInfo.Range]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, typeDef.type.includes('RangeInclusive') ? 'RangeInclusive' : 'Range'),
91
+ [TypeDefInfo.Result]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Result'),
92
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
93
+ [TypeDefInfo.Set]: (CodecRegistry, {
94
+ length = 8,
95
+ sub
96
+ }) => {
97
+ assert(sub && Array.isArray(sub), 'Unable to encode Set type');
98
+ return stringify({
99
+ _set: sub.reduce((all, {
100
+ index,
101
+ name
102
+ }, count) => objectSpread(all, {
103
+ [`${name || `Unknown${index || count}`}`]: index || count
104
+ }), {
105
+ _bitLength: length || 8
106
+ })
107
+ });
108
+ },
109
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
110
+ [TypeDefInfo.Si]: (CodecRegistry, {
111
+ lookupName,
112
+ type
113
+ }) => lookupName || type,
114
+ [TypeDefInfo.Struct]: (CodecRegistry, {
115
+ alias,
116
+ sub
117
+ }) => {
118
+ assert(sub && Array.isArray(sub), 'Unable to encode Struct type');
119
+ return encodeSubTypes(CodecRegistry, sub, false, alias ? {
120
+ _alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, {
121
+ [k]: v
122
+ }), {})
123
+ } : {});
124
+ },
125
+ [TypeDefInfo.Tuple]: (CodecRegistry, {
126
+ sub
127
+ }) => {
128
+ assert(sub && Array.isArray(sub), 'Unable to encode Tuple type');
129
+ return `(${sub.map(type => encodeTypeDef(CodecRegistry, type)).join(',')})`;
130
+ },
131
+ [TypeDefInfo.UInt]: (CodecRegistry, {
132
+ length = 32
133
+ }) => `UInt<${length}>`,
134
+ [TypeDefInfo.Vec]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'Vec'),
135
+ [TypeDefInfo.VecFixed]: (CodecRegistry, {
136
+ length,
137
+ sub
138
+ }) => {
139
+ assert(isNumber(length) && !isUndefined(sub) && !Array.isArray(sub), 'Unable to encode VecFixed type');
140
+ return `[${sub.type};${length}]`;
141
+ },
142
+ [TypeDefInfo.WrapperOpaque]: (CodecRegistry, typeDef) => encodeWithParams(CodecRegistry, typeDef, 'WrapperOpaque')
143
+ };
144
+
145
+ function encodeType(CodecRegistry, typeDef, withLookup = true) {
146
+ return withLookup && typeDef.lookupName ? typeDef.lookupName : encoders[typeDef.info](CodecRegistry, typeDef);
147
+ }
148
+
149
+ export function encodeTypeDef(CodecRegistry, typeDef) {
150
+ // In the case of contracts we do have the unfortunate situation where the displayName would
151
+ // refer to "Option" when it is an option. For these, string it out, only using when actually
152
+ // not a top-level element to be used
153
+ return typeDef.displayName && !INFO_WRAP.some(i => typeDef.displayName === i) ? typeDef.displayName : encodeType(CodecRegistry, typeDef);
154
+ }
155
+ export function withTypeString(CodecRegistry, typeDef) {
156
+ return objectSpread({}, typeDef, {
157
+ type: encodeType(CodecRegistry, typeDef, false)
158
+ });
159
+ }