@polkadot/types-codec 9.14.1 → 10.0.1

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 (157) hide show
  1. package/abstract/Array.js +140 -155
  2. package/abstract/Base.js +95 -112
  3. package/abstract/Int.js +212 -220
  4. package/abstract/Object.js +34 -45
  5. package/abstract/index.js +3 -6
  6. package/base/Compact.js +149 -165
  7. package/base/DoNotConstruct.js +61 -64
  8. package/base/Enum.js +345 -382
  9. package/base/Int.js +14 -18
  10. package/base/Null.js +71 -82
  11. package/base/Option.js +217 -226
  12. package/base/Result.js +54 -67
  13. package/base/Tuple.js +96 -101
  14. package/base/UInt.js +11 -15
  15. package/base/Vec.js +84 -89
  16. package/base/VecAny.js +8 -12
  17. package/base/VecFixed.js +59 -70
  18. package/base/index.js +12 -15
  19. package/bundle.js +7 -10
  20. package/cjs/abstract/Array.js +138 -159
  21. package/cjs/abstract/Base.js +96 -117
  22. package/cjs/abstract/Int.js +215 -238
  23. package/cjs/abstract/Object.js +30 -49
  24. package/cjs/abstract/index.js +8 -25
  25. package/cjs/base/Compact.js +151 -173
  26. package/cjs/base/DoNotConstruct.js +63 -70
  27. package/cjs/base/Enum.js +350 -396
  28. package/cjs/base/Int.js +16 -24
  29. package/cjs/base/Null.js +70 -86
  30. package/cjs/base/Option.js +220 -234
  31. package/cjs/base/Result.js +56 -71
  32. package/cjs/base/Tuple.js +103 -109
  33. package/cjs/base/UInt.js +13 -19
  34. package/cjs/base/Vec.js +94 -101
  35. package/cjs/base/VecAny.js +10 -16
  36. package/cjs/base/VecFixed.js +66 -79
  37. package/cjs/base/index.js +26 -88
  38. package/cjs/bundle.js +11 -86
  39. package/cjs/detectOther.js +2 -10
  40. package/cjs/detectPackage.js +6 -11
  41. package/cjs/extended/BTreeMap.js +10 -16
  42. package/cjs/extended/BTreeSet.js +162 -178
  43. package/cjs/extended/BitVec.js +70 -77
  44. package/cjs/extended/Bytes.js +59 -62
  45. package/cjs/extended/HashMap.js +10 -16
  46. package/cjs/extended/Linkage.js +50 -60
  47. package/cjs/extended/Map.js +176 -181
  48. package/cjs/extended/OptionBool.js +65 -68
  49. package/cjs/extended/Range.js +37 -46
  50. package/cjs/extended/RangeInclusive.js +13 -21
  51. package/cjs/extended/Type.js +14 -22
  52. package/cjs/extended/U8aFixed.js +33 -43
  53. package/cjs/extended/WrapperKeepOpaque.js +99 -99
  54. package/cjs/extended/WrapperOpaque.js +19 -28
  55. package/cjs/extended/index.js +31 -108
  56. package/cjs/index.js +3 -15
  57. package/cjs/native/Bool.js +101 -116
  58. package/cjs/native/Date.js +125 -151
  59. package/cjs/native/Float.js +95 -114
  60. package/cjs/native/Json.js +102 -124
  61. package/cjs/native/Raw.js +128 -150
  62. package/cjs/native/Set.js +200 -220
  63. package/cjs/native/Struct.data.js +2 -9
  64. package/cjs/native/Struct.js +238 -251
  65. package/cjs/native/Text.js +137 -149
  66. package/cjs/native/index.js +21 -78
  67. package/cjs/packageInfo.js +2 -16
  68. package/cjs/primitive/F32.js +8 -11
  69. package/cjs/primitive/F64.js +8 -11
  70. package/cjs/primitive/I128.js +8 -11
  71. package/cjs/primitive/I16.js +8 -11
  72. package/cjs/primitive/I256.js +8 -11
  73. package/cjs/primitive/I32.js +8 -11
  74. package/cjs/primitive/I64.js +8 -11
  75. package/cjs/primitive/I8.js +8 -11
  76. package/cjs/primitive/U128.js +8 -11
  77. package/cjs/primitive/U16.js +8 -11
  78. package/cjs/primitive/U256.js +8 -11
  79. package/cjs/primitive/U32.js +8 -11
  80. package/cjs/primitive/U64.js +8 -11
  81. package/cjs/primitive/U8.js +8 -11
  82. package/cjs/primitive/USize.js +7 -13
  83. package/cjs/primitive/index.js +47 -199
  84. package/cjs/types/codec.js +2 -1
  85. package/cjs/types/helpers.js +2 -1
  86. package/cjs/types/index.js +6 -48
  87. package/cjs/types/interfaces.js +2 -1
  88. package/cjs/types/registry.js +2 -1
  89. package/cjs/utils/compareArray.js +12 -17
  90. package/cjs/utils/compareMap.js +23 -26
  91. package/cjs/utils/compareSet.js +17 -23
  92. package/cjs/utils/decodeU8a.js +63 -74
  93. package/cjs/utils/index.js +24 -91
  94. package/cjs/utils/mapToTypeMap.js +13 -18
  95. package/cjs/utils/sanitize.js +198 -204
  96. package/cjs/utils/sortValues.js +39 -49
  97. package/cjs/utils/typeToConstructor.js +8 -11
  98. package/cjs/utils/typesToMap.js +10 -16
  99. package/cjs/utils/util.js +6 -11
  100. package/detectOther.js +0 -5
  101. package/detectPackage.js +2 -7
  102. package/extended/BTreeMap.js +8 -11
  103. package/extended/BTreeSet.js +159 -171
  104. package/extended/BitVec.js +63 -70
  105. package/extended/Bytes.js +56 -57
  106. package/extended/HashMap.js +8 -11
  107. package/extended/Linkage.js +46 -51
  108. package/extended/Map.js +173 -173
  109. package/extended/OptionBool.js +62 -62
  110. package/extended/Range.js +31 -39
  111. package/extended/RangeInclusive.js +11 -16
  112. package/extended/Type.js +12 -17
  113. package/extended/U8aFixed.js +30 -36
  114. package/extended/WrapperKeepOpaque.js +92 -91
  115. package/extended/WrapperOpaque.js +17 -23
  116. package/extended/index.js +14 -17
  117. package/index.js +2 -5
  118. package/native/Bool.js +102 -111
  119. package/native/Date.js +126 -144
  120. package/native/Float.js +94 -107
  121. package/native/Json.js +104 -114
  122. package/native/Raw.js +130 -146
  123. package/native/Set.js +194 -209
  124. package/native/Struct.data.js +0 -3
  125. package/native/Struct.js +233 -241
  126. package/native/Text.js +135 -143
  127. package/native/index.js +8 -11
  128. package/package.json +8 -5
  129. package/packageInfo.js +1 -11
  130. package/primitive/F32.js +3 -7
  131. package/primitive/F64.js +3 -7
  132. package/primitive/I128.js +3 -7
  133. package/primitive/I16.js +3 -7
  134. package/primitive/I256.js +3 -7
  135. package/primitive/I32.js +3 -7
  136. package/primitive/I64.js +3 -7
  137. package/primitive/I8.js +3 -7
  138. package/primitive/U128.js +3 -7
  139. package/primitive/U16.js +3 -7
  140. package/primitive/U256.js +3 -7
  141. package/primitive/U32.js +3 -7
  142. package/primitive/U64.js +3 -7
  143. package/primitive/U8.js +3 -7
  144. package/primitive/USize.js +5 -9
  145. package/primitive/index.js +15 -18
  146. package/types/index.js +4 -7
  147. package/utils/compareArray.js +7 -11
  148. package/utils/compareMap.js +18 -19
  149. package/utils/compareSet.js +12 -16
  150. package/utils/decodeU8a.js +54 -61
  151. package/utils/index.js +9 -12
  152. package/utils/mapToTypeMap.js +9 -13
  153. package/utils/sanitize.js +182 -185
  154. package/utils/sortValues.js +32 -36
  155. package/utils/typeToConstructor.js +3 -4
  156. package/utils/typesToMap.js +5 -8
  157. package/utils/util.js +1 -4
@@ -1,253 +1,230 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DEFAULT_UINT_BITS = exports.AbstractInt = void 0;
7
- var _util = require("@polkadot/util");
8
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
9
- // SPDX-License-Identifier: Apache-2.0
10
-
11
- const DEFAULT_UINT_BITS = 64;
12
-
13
- // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
14
- // In this case however, we always print any >32 as hex
15
- exports.DEFAULT_UINT_BITS = DEFAULT_UINT_BITS;
2
+ var _AbstractInt_bitLength;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.AbstractInt = exports.DEFAULT_UINT_BITS = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const util_1 = require("@polkadot/util");
7
+ exports.DEFAULT_UINT_BITS = 64;
16
8
  const MAX_NUMBER_BITS = 52;
17
- const MUL_P = new _util.BN(10000);
18
- const FORMATTERS = [['Perquintill', _util.BN_QUINTILL], ['Perbill', _util.BN_BILLION], ['Permill', _util.BN_MILLION], ['Percent', _util.BN_HUNDRED]];
9
+ const MUL_P = new util_1.BN(10000);
10
+ const FORMATTERS = [
11
+ ['Perquintill', util_1.BN_QUINTILL],
12
+ ['Perbill', util_1.BN_BILLION],
13
+ ['Permill', util_1.BN_MILLION],
14
+ ['Percent', util_1.BN_HUNDRED]
15
+ ];
19
16
  function isToBn(value) {
20
- return (0, _util.isFunction)(value.toBn);
17
+ return (0, util_1.isFunction)(value.toBn);
21
18
  }
22
19
  function toPercentage(value, divisor) {
23
- return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
20
+ return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
24
21
  }
25
-
26
22
  /** @internal */
27
23
  function decodeAbstractInt(value, isNegative) {
28
- if ((0, _util.isNumber)(value)) {
29
- if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
30
- throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
31
- }
32
- return value;
33
- } else if ((0, _util.isString)(value)) {
34
- if ((0, _util.isHex)(value, -1, true)) {
35
- return (0, _util.hexToBn)(value, {
36
- isLe: false,
37
- isNegative
38
- }).toString();
39
- }
40
- if (value.includes('.') || value.includes(',') || value.includes('e')) {
41
- throw new Error('String should not contain decimal points or scientific notation');
42
- }
43
- return value;
44
- } else if ((0, _util.isBn)(value) || (0, _util.isBigInt)(value)) {
45
- return value.toString();
46
- } else if ((0, _util.isObject)(value)) {
47
- if (isToBn(value)) {
48
- return value.toBn().toString();
49
- }
50
-
51
- // Allow the construction from an object with a single top-level key. This means that
52
- // single key objects can be treated equivalently to numbers, assuming they meet the
53
- // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
54
- const keys = Object.keys(value);
55
- if (keys.length !== 1) {
56
- throw new Error('Unable to construct number from multi-key object');
57
- }
58
- return decodeAbstractInt(value[keys[0]], isNegative);
59
- } else if (!value) {
60
- return 0;
61
- }
62
- throw new Error(`Unable to create BN from unknown type ${typeof value}`);
24
+ if ((0, util_1.isNumber)(value)) {
25
+ if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
26
+ throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
27
+ }
28
+ return value;
29
+ }
30
+ else if ((0, util_1.isString)(value)) {
31
+ if ((0, util_1.isHex)(value, -1, true)) {
32
+ return (0, util_1.hexToBn)(value, { isLe: false, isNegative }).toString();
33
+ }
34
+ if (value.includes('.') || value.includes(',') || value.includes('e')) {
35
+ throw new Error('String should not contain decimal points or scientific notation');
36
+ }
37
+ return value;
38
+ }
39
+ else if ((0, util_1.isBn)(value) || (0, util_1.isBigInt)(value)) {
40
+ return value.toString();
41
+ }
42
+ else if ((0, util_1.isObject)(value)) {
43
+ if (isToBn(value)) {
44
+ return value.toBn().toString();
45
+ }
46
+ // Allow the construction from an object with a single top-level key. This means that
47
+ // single key objects can be treated equivalently to numbers, assuming they meet the
48
+ // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
49
+ const keys = Object.keys(value);
50
+ if (keys.length !== 1) {
51
+ throw new Error('Unable to construct number from multi-key object');
52
+ }
53
+ return decodeAbstractInt(value[keys[0]], isNegative);
54
+ }
55
+ else if (!value) {
56
+ return 0;
57
+ }
58
+ throw new Error(`Unable to create BN from unknown type ${typeof value}`);
63
59
  }
64
-
65
60
  /**
66
61
  * @name AbstractInt
67
62
  * @ignore
68
63
  * @noInheritDoc
69
64
  */
70
- class AbstractInt extends _util.BN {
71
- #bitLength;
72
- constructor(registry) {
73
- let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
74
- let bitLength = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : DEFAULT_UINT_BITS;
75
- let isSigned = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
76
- // Construct via a string/number, which will be passed in the BN constructor.
77
- // It would be ideal to actually return a BN, but there is an issue:
78
- // https://github.com/indutny/bn.js/issues/206
79
- super(
80
- // shortcut isU8a as used in SCALE decoding
81
- (0, _util.isU8a)(value) ? bitLength <= 48 ? (0, _util.u8aToNumber)(value.subarray(0, bitLength / 8), {
82
- isNegative: isSigned
83
- }) : (0, _util.u8aToBn)(value.subarray(0, bitLength / 8), {
84
- isLe: true,
85
- isNegative: isSigned
86
- }).toString() : decodeAbstractInt(value, isSigned));
87
- this.registry = registry;
88
- this.#bitLength = bitLength;
89
- this.encodedLength = this.#bitLength / 8;
90
- this.initialU8aLength = this.#bitLength / 8;
91
- this.isUnsigned = !isSigned;
92
- const isNegative = this.isNeg();
93
- const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
94
- if (isNegative && !isSigned) {
95
- throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
96
- } else if (super.bitLength() > maxBits) {
97
- throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
98
- }
99
- }
100
-
101
- /**
102
- * @description returns a hash of the contents
103
- */
104
- get hash() {
105
- return this.registry.hash(this.toU8a());
106
- }
107
-
108
- /**
109
- * @description Checks if the value is a zero value (align elsewhere)
110
- */
111
- get isEmpty() {
112
- return this.isZero();
113
- }
114
-
115
- /**
116
- * @description Returns the number of bits in the value
117
- */
118
- bitLength() {
119
- return this.#bitLength;
120
- }
121
-
122
- /**
123
- * @description Compares the value of the input to see if there is a match
124
- */
125
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
126
- eq(other) {
127
- // Here we are actually overriding the built-in .eq to take care of both
128
- // number and BN inputs (no `.eqn` needed) - numbers will be converted
129
- return super.eq((0, _util.isHex)(other) ? (0, _util.hexToBn)(other.toString(), {
130
- isLe: false,
131
- isNegative: !this.isUnsigned
132
- }) : (0, _util.bnToBn)(other));
133
- }
134
-
135
- /**
136
- * @description Returns a breakdown of the hex encoding for this Codec
137
- */
138
- inspect() {
139
- return {
140
- outer: [this.toU8a()]
141
- };
142
- }
143
-
144
- /**
145
- * @description True if this value is the max of the type
146
- */
147
- isMax() {
148
- const u8a = this.toU8a().filter(b => b === 0xff);
149
- return u8a.length === this.#bitLength / 8;
150
- }
151
-
152
- /**
153
- * @description Returns a BigInt representation of the number
154
- */
155
- toBigInt() {
156
- return BigInt(this.toString());
157
- }
158
-
159
- /**
160
- * @description Returns the BN representation of the number. (Compatibility)
161
- */
162
- toBn() {
163
- return this;
164
- }
165
-
166
- /**
167
- * @description Returns a hex string representation of the value
168
- */
169
- toHex() {
170
- let isLe = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
171
- // For display/JSON, this is BE, for compare, use isLe
172
- return (0, _util.bnToHex)(this, {
173
- bitLength: this.bitLength(),
174
- isLe,
175
- isNegative: !this.isUnsigned
176
- });
177
- }
178
-
179
- /**
180
- * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
181
- */
182
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
183
- toHuman(isExpanded) {
184
- const rawType = this.toRawType();
185
- if (rawType === 'Balance') {
186
- return this.isMax() ? 'everything'
187
- // FIXME In the case of multiples we need some way of detecting which instance this belongs
188
- // to. as it stands we will always format (incorrectly) against the first token defined
189
- : (0, _util.formatBalance)(this, {
190
- decimals: this.registry.chainDecimals[0],
191
- withSi: true,
192
- withUnit: this.registry.chainTokens[0]
193
- });
194
- }
195
- const [, divisor] = FORMATTERS.find(_ref => {
196
- let [type] = _ref;
197
- return type === rawType;
198
- }) || [];
199
- return divisor ? toPercentage(this, divisor) : (0, _util.formatNumber)(this);
200
- }
201
-
202
- /**
203
- * @description Converts the Object to JSON, typically used for RPC transfers
204
- */
205
- toJSON() {
206
- let onlyHex = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
207
- // FIXME this return type should by string | number, however BN returns string
208
- // Options here are
209
- // - super.bitLength() - the actual used bits
210
- // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
211
- return onlyHex || super.bitLength() > MAX_NUMBER_BITS ? this.toHex() : this.toNumber();
212
- }
213
-
214
- /**
215
- * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
216
- */
217
- toPrimitive() {
218
- return super.bitLength() > MAX_NUMBER_BITS ? this.toString() : this.toNumber();
219
- }
220
-
221
- /**
222
- * @description Returns the base runtime type name for this instance
223
- */
224
- toRawType() {
225
- // NOTE In the case of balances, which have a special meaning on the UI
226
- // and can be interpreted differently, return a specific value for it so
227
- // underlying it always matches (no matter which length it actually is)
228
- return this instanceof this.registry.createClassUnsafe('Balance') ? 'Balance' : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
229
- }
230
-
231
- /**
232
- * @description Returns the string representation of the value
233
- * @param base The base to use for the conversion
234
- */
235
- toString(base) {
236
- // only included here since we do not inherit docs
237
- return super.toString(base);
238
- }
239
-
240
- /**
241
- * @description Encodes the value as a Uint8Array as per the SCALE specifications
242
- * @param isBare true when the value has none of the type-specific prefixes (internal)
243
- */
244
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
245
- toU8a(isBare) {
246
- return (0, _util.bnToU8a)(this, {
247
- bitLength: this.bitLength(),
248
- isLe: true,
249
- isNegative: !this.isUnsigned
250
- });
251
- }
65
+ class AbstractInt extends util_1.BN {
66
+ constructor(registry, value = 0, bitLength = exports.DEFAULT_UINT_BITS, isSigned = false) {
67
+ // Construct via a string/number, which will be passed in the BN constructor.
68
+ // It would be ideal to actually return a BN, but there is an issue:
69
+ // https://github.com/indutny/bn.js/issues/206
70
+ super(
71
+ // shortcut isU8a as used in SCALE decoding
72
+ (0, util_1.isU8a)(value)
73
+ ? bitLength <= 48
74
+ ? (0, util_1.u8aToNumber)(value.subarray(0, bitLength / 8), { isNegative: isSigned })
75
+ : (0, util_1.u8aToBn)(value.subarray(0, bitLength / 8), { isLe: true, isNegative: isSigned }).toString()
76
+ : decodeAbstractInt(value, isSigned));
77
+ _AbstractInt_bitLength.set(this, void 0);
78
+ this.registry = registry;
79
+ tslib_1.__classPrivateFieldSet(this, _AbstractInt_bitLength, bitLength, "f");
80
+ this.encodedLength = tslib_1.__classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8;
81
+ this.initialU8aLength = tslib_1.__classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8;
82
+ this.isUnsigned = !isSigned;
83
+ const isNegative = this.isNeg();
84
+ const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
85
+ if (isNegative && !isSigned) {
86
+ throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
87
+ }
88
+ else if (super.bitLength() > maxBits) {
89
+ throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
90
+ }
91
+ }
92
+ /**
93
+ * @description returns a hash of the contents
94
+ */
95
+ get hash() {
96
+ return this.registry.hash(this.toU8a());
97
+ }
98
+ /**
99
+ * @description Checks if the value is a zero value (align elsewhere)
100
+ */
101
+ get isEmpty() {
102
+ return this.isZero();
103
+ }
104
+ /**
105
+ * @description Returns the number of bits in the value
106
+ */
107
+ bitLength() {
108
+ return tslib_1.__classPrivateFieldGet(this, _AbstractInt_bitLength, "f");
109
+ }
110
+ /**
111
+ * @description Compares the value of the input to see if there is a match
112
+ */
113
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
114
+ eq(other) {
115
+ // Here we are actually overriding the built-in .eq to take care of both
116
+ // number and BN inputs (no `.eqn` needed) - numbers will be converted
117
+ return super.eq((0, util_1.isHex)(other)
118
+ ? (0, util_1.hexToBn)(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
119
+ : (0, util_1.bnToBn)(other));
120
+ }
121
+ /**
122
+ * @description Returns a breakdown of the hex encoding for this Codec
123
+ */
124
+ inspect() {
125
+ return {
126
+ outer: [this.toU8a()]
127
+ };
128
+ }
129
+ /**
130
+ * @description True if this value is the max of the type
131
+ */
132
+ isMax() {
133
+ const u8a = this.toU8a().filter((b) => b === 0xff);
134
+ return u8a.length === (tslib_1.__classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8);
135
+ }
136
+ /**
137
+ * @description Returns a BigInt representation of the number
138
+ */
139
+ toBigInt() {
140
+ return BigInt(this.toString());
141
+ }
142
+ /**
143
+ * @description Returns the BN representation of the number. (Compatibility)
144
+ */
145
+ toBn() {
146
+ return this;
147
+ }
148
+ /**
149
+ * @description Returns a hex string representation of the value
150
+ */
151
+ toHex(isLe = false) {
152
+ // For display/JSON, this is BE, for compare, use isLe
153
+ return (0, util_1.bnToHex)(this, {
154
+ bitLength: this.bitLength(),
155
+ isLe,
156
+ isNegative: !this.isUnsigned
157
+ });
158
+ }
159
+ /**
160
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
161
+ */
162
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
163
+ toHuman(isExpanded) {
164
+ const rawType = this.toRawType();
165
+ if (rawType === 'Balance') {
166
+ return this.isMax()
167
+ ? 'everything'
168
+ // FIXME In the case of multiples we need some way of detecting which instance this belongs
169
+ // to. as it stands we will always format (incorrectly) against the first token defined
170
+ : (0, util_1.formatBalance)(this, { decimals: this.registry.chainDecimals[0], withSi: true, withUnit: this.registry.chainTokens[0] });
171
+ }
172
+ const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
173
+ return divisor
174
+ ? toPercentage(this, divisor)
175
+ : (0, util_1.formatNumber)(this);
176
+ }
177
+ /**
178
+ * @description Converts the Object to JSON, typically used for RPC transfers
179
+ */
180
+ toJSON(onlyHex = false) {
181
+ // FIXME this return type should by string | number, however BN returns string
182
+ // Options here are
183
+ // - super.bitLength() - the actual used bits
184
+ // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
185
+ return onlyHex || (super.bitLength() > MAX_NUMBER_BITS)
186
+ ? this.toHex()
187
+ : this.toNumber();
188
+ }
189
+ /**
190
+ * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
191
+ */
192
+ toPrimitive() {
193
+ return super.bitLength() > MAX_NUMBER_BITS
194
+ ? this.toString()
195
+ : this.toNumber();
196
+ }
197
+ /**
198
+ * @description Returns the base runtime type name for this instance
199
+ */
200
+ toRawType() {
201
+ // NOTE In the case of balances, which have a special meaning on the UI
202
+ // and can be interpreted differently, return a specific value for it so
203
+ // underlying it always matches (no matter which length it actually is)
204
+ return this instanceof this.registry.createClassUnsafe('Balance')
205
+ ? 'Balance'
206
+ : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
207
+ }
208
+ /**
209
+ * @description Returns the string representation of the value
210
+ * @param base The base to use for the conversion
211
+ */
212
+ toString(base) {
213
+ // only included here since we do not inherit docs
214
+ return super.toString(base);
215
+ }
216
+ /**
217
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
218
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
219
+ */
220
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
221
+ toU8a(isBare) {
222
+ return (0, util_1.bnToU8a)(this, {
223
+ bitLength: this.bitLength(),
224
+ isLe: true,
225
+ isNegative: !this.isUnsigned
226
+ });
227
+ }
252
228
  }
253
- exports.AbstractInt = AbstractInt;
229
+ exports.AbstractInt = AbstractInt;
230
+ _AbstractInt_bitLength = new WeakMap();
@@ -1,58 +1,39 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
2
+ Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.AbstractObject = void 0;
7
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
8
- // SPDX-License-Identifier: Apache-2.0
9
-
10
4
  /**
11
5
  * @name Object
12
6
  * @description A type extends the Base class, when it holds a value
13
7
  */
14
8
  class AbstractObject {
15
- constructor(registry, value, initialU8aLength) {
16
- this.$ = value;
17
- this.initialU8aLength = initialU8aLength;
18
- this.registry = registry;
19
- }
20
-
21
- /**
22
- * @description The length of the value when encoded as a Uint8Array
23
- */
24
- get encodedLength() {
25
- return this.toU8a().length;
26
- }
27
-
28
- /**
29
- * @description returns a hash of the contents
30
- */
31
- get hash() {
32
- return this.registry.hash(this.toU8a());
33
- }
34
-
35
- /**
36
- * @description Checks if the value is an empty value
37
- */
38
-
39
- /**
40
- * @description Returns the string representation of the value
41
- */
42
- toString() {
43
- return this.$.toString();
44
- }
45
-
46
- /**
47
- * @description Encodes the value as a Uint8Array as per the SCALE specifications
48
- * @param isBare true when the value has none of the type-specific prefixes (internal)
49
- */
50
-
51
- /**
52
- * @description Return the internal value (JS-aligned, same result as $)
53
- */
54
- valueOf() {
55
- return this.$;
56
- }
9
+ constructor(registry, value, initialU8aLength) {
10
+ this.$ = value;
11
+ this.initialU8aLength = initialU8aLength;
12
+ this.registry = registry;
13
+ }
14
+ /**
15
+ * @description The length of the value when encoded as a Uint8Array
16
+ */
17
+ get encodedLength() {
18
+ return this.toU8a().length;
19
+ }
20
+ /**
21
+ * @description returns a hash of the contents
22
+ */
23
+ get hash() {
24
+ return this.registry.hash(this.toU8a());
25
+ }
26
+ /**
27
+ * @description Returns the string representation of the value
28
+ */
29
+ toString() {
30
+ return this.$.toString();
31
+ }
32
+ /**
33
+ * @description Return the internal value (JS-aligned, same result as $)
34
+ */
35
+ valueOf() {
36
+ return this.$;
37
+ }
57
38
  }
58
39
  exports.AbstractObject = AbstractObject;
@@ -1,26 +1,9 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "AbstractArray", {
7
- enumerable: true,
8
- get: function () {
9
- return _Array.AbstractArray;
10
- }
11
- });
12
- Object.defineProperty(exports, "AbstractBase", {
13
- enumerable: true,
14
- get: function () {
15
- return _Base.AbstractBase;
16
- }
17
- });
18
- Object.defineProperty(exports, "AbstractInt", {
19
- enumerable: true,
20
- get: function () {
21
- return _Int.AbstractInt;
22
- }
23
- });
24
- var _Array = require("./Array");
25
- var _Base = require("./Base");
26
- var _Int = require("./Int");
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AbstractInt = exports.AbstractBase = exports.AbstractArray = void 0;
4
+ var Array_1 = require("./Array");
5
+ Object.defineProperty(exports, "AbstractArray", { enumerable: true, get: function () { return Array_1.AbstractArray; } });
6
+ var Base_1 = require("./Base");
7
+ Object.defineProperty(exports, "AbstractBase", { enumerable: true, get: function () { return Base_1.AbstractBase; } });
8
+ var Int_1 = require("./Int");
9
+ Object.defineProperty(exports, "AbstractInt", { enumerable: true, get: function () { return Int_1.AbstractInt; } });