@polkadot/types-codec 9.14.2 → 10.1.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 (229) hide show
  1. package/abstract/Array.d.ts +1 -1
  2. package/abstract/Array.js +136 -155
  3. package/abstract/Base.d.ts +1 -1
  4. package/abstract/Base.js +95 -113
  5. package/abstract/Int.d.ts +1 -1
  6. package/abstract/Int.js +209 -220
  7. package/abstract/Object.d.ts +1 -1
  8. package/abstract/Object.js +29 -45
  9. package/abstract/index.d.ts +3 -3
  10. package/abstract/index.js +3 -6
  11. package/base/Compact.d.ts +1 -1
  12. package/base/Compact.js +149 -166
  13. package/base/DoNotConstruct.d.ts +1 -1
  14. package/base/DoNotConstruct.js +62 -65
  15. package/base/Enum.d.ts +1 -1
  16. package/base/Enum.js +347 -383
  17. package/base/Int.d.ts +2 -2
  18. package/base/Int.js +14 -18
  19. package/base/Null.d.ts +1 -1
  20. package/base/Null.js +68 -82
  21. package/base/Option.d.ts +1 -1
  22. package/base/Option.js +217 -227
  23. package/base/Result.d.ts +2 -2
  24. package/base/Result.js +54 -67
  25. package/base/Tuple.d.ts +2 -2
  26. package/base/Tuple.js +100 -102
  27. package/base/UInt.d.ts +2 -2
  28. package/base/UInt.js +11 -15
  29. package/base/Vec.d.ts +2 -2
  30. package/base/Vec.js +88 -90
  31. package/base/VecAny.d.ts +2 -2
  32. package/base/VecAny.js +8 -12
  33. package/base/VecFixed.d.ts +2 -2
  34. package/base/VecFixed.js +63 -71
  35. package/base/index.d.ts +12 -12
  36. package/base/index.js +12 -15
  37. package/bundle.d.ts +7 -7
  38. package/bundle.js +7 -10
  39. package/cjs/abstract/Array.js +138 -159
  40. package/cjs/abstract/Base.js +96 -117
  41. package/cjs/abstract/Int.js +215 -238
  42. package/cjs/abstract/Object.js +30 -49
  43. package/cjs/abstract/index.js +8 -25
  44. package/cjs/base/Compact.js +151 -173
  45. package/cjs/base/DoNotConstruct.js +63 -70
  46. package/cjs/base/Enum.js +350 -396
  47. package/cjs/base/Int.js +16 -24
  48. package/cjs/base/Null.js +70 -86
  49. package/cjs/base/Option.js +220 -234
  50. package/cjs/base/Result.js +56 -71
  51. package/cjs/base/Tuple.js +103 -109
  52. package/cjs/base/UInt.js +13 -19
  53. package/cjs/base/Vec.js +94 -101
  54. package/cjs/base/VecAny.js +10 -16
  55. package/cjs/base/VecFixed.js +66 -79
  56. package/cjs/base/index.js +26 -88
  57. package/cjs/bundle.js +11 -86
  58. package/cjs/detectOther.js +2 -10
  59. package/cjs/detectPackage.js +6 -11
  60. package/cjs/extended/BTreeMap.js +10 -16
  61. package/cjs/extended/BTreeSet.js +162 -178
  62. package/cjs/extended/BitVec.js +70 -77
  63. package/cjs/extended/Bytes.js +59 -62
  64. package/cjs/extended/HashMap.js +10 -16
  65. package/cjs/extended/Linkage.js +50 -60
  66. package/cjs/extended/Map.js +176 -181
  67. package/cjs/extended/OptionBool.js +65 -68
  68. package/cjs/extended/Range.js +37 -46
  69. package/cjs/extended/RangeInclusive.js +13 -21
  70. package/cjs/extended/Type.js +14 -22
  71. package/cjs/extended/U8aFixed.js +33 -43
  72. package/cjs/extended/WrapperKeepOpaque.js +99 -99
  73. package/cjs/extended/WrapperOpaque.js +19 -28
  74. package/cjs/extended/index.js +31 -108
  75. package/cjs/index.js +4 -16
  76. package/cjs/native/Bool.js +101 -116
  77. package/cjs/native/Date.js +125 -151
  78. package/cjs/native/Float.js +95 -114
  79. package/cjs/native/Json.js +102 -124
  80. package/cjs/native/Raw.js +128 -150
  81. package/cjs/native/Set.js +200 -220
  82. package/cjs/native/Struct.data.js +2 -9
  83. package/cjs/native/Struct.js +238 -251
  84. package/cjs/native/Text.js +137 -149
  85. package/cjs/native/index.js +21 -78
  86. package/cjs/packageInfo.js +2 -16
  87. package/cjs/primitive/F32.js +8 -11
  88. package/cjs/primitive/F64.js +8 -11
  89. package/cjs/primitive/I128.js +8 -11
  90. package/cjs/primitive/I16.js +8 -11
  91. package/cjs/primitive/I256.js +8 -11
  92. package/cjs/primitive/I32.js +8 -11
  93. package/cjs/primitive/I64.js +8 -11
  94. package/cjs/primitive/I8.js +8 -11
  95. package/cjs/primitive/U128.js +8 -11
  96. package/cjs/primitive/U16.js +8 -11
  97. package/cjs/primitive/U256.js +8 -11
  98. package/cjs/primitive/U32.js +8 -11
  99. package/cjs/primitive/U64.js +8 -11
  100. package/cjs/primitive/U8.js +8 -11
  101. package/cjs/primitive/USize.js +7 -13
  102. package/cjs/primitive/index.js +47 -199
  103. package/cjs/types/codec.js +2 -1
  104. package/cjs/types/helpers.js +2 -1
  105. package/cjs/types/index.js +6 -48
  106. package/cjs/types/interfaces.js +2 -1
  107. package/cjs/types/registry.js +2 -1
  108. package/cjs/utils/compareArray.js +12 -17
  109. package/cjs/utils/compareMap.js +23 -26
  110. package/cjs/utils/compareSet.js +17 -23
  111. package/cjs/utils/decodeU8a.js +63 -74
  112. package/cjs/utils/index.js +24 -91
  113. package/cjs/utils/mapToTypeMap.js +13 -18
  114. package/cjs/utils/sanitize.js +198 -204
  115. package/cjs/utils/sortValues.js +39 -49
  116. package/cjs/utils/typeToConstructor.js +8 -11
  117. package/cjs/utils/typesToMap.js +10 -16
  118. package/cjs/utils/util.js +6 -11
  119. package/detectOther.js +0 -5
  120. package/detectPackage.js +2 -7
  121. package/extended/BTreeMap.d.ts +2 -2
  122. package/extended/BTreeMap.js +8 -11
  123. package/extended/BTreeSet.d.ts +1 -1
  124. package/extended/BTreeSet.js +159 -172
  125. package/extended/BitVec.d.ts +2 -2
  126. package/extended/BitVec.js +67 -71
  127. package/extended/Bytes.d.ts +2 -2
  128. package/extended/Bytes.js +56 -57
  129. package/extended/HashMap.d.ts +2 -2
  130. package/extended/HashMap.js +8 -11
  131. package/extended/Linkage.d.ts +4 -4
  132. package/extended/Linkage.js +46 -51
  133. package/extended/Map.d.ts +1 -1
  134. package/extended/Map.js +173 -174
  135. package/extended/OptionBool.d.ts +3 -3
  136. package/extended/OptionBool.js +62 -62
  137. package/extended/Range.d.ts +2 -2
  138. package/extended/Range.js +35 -40
  139. package/extended/RangeInclusive.d.ts +2 -2
  140. package/extended/RangeInclusive.js +11 -16
  141. package/extended/Type.d.ts +2 -2
  142. package/extended/Type.js +12 -17
  143. package/extended/U8aFixed.d.ts +2 -2
  144. package/extended/U8aFixed.js +30 -36
  145. package/extended/WrapperKeepOpaque.d.ts +2 -2
  146. package/extended/WrapperKeepOpaque.js +96 -92
  147. package/extended/WrapperOpaque.d.ts +2 -2
  148. package/extended/WrapperOpaque.js +17 -23
  149. package/extended/index.d.ts +14 -14
  150. package/extended/index.js +14 -17
  151. package/index.d.ts +2 -2
  152. package/index.js +2 -5
  153. package/native/Bool.d.ts +1 -1
  154. package/native/Bool.js +99 -111
  155. package/native/Date.d.ts +1 -1
  156. package/native/Date.js +123 -144
  157. package/native/Float.d.ts +1 -1
  158. package/native/Float.js +93 -108
  159. package/native/Json.d.ts +1 -1
  160. package/native/Json.js +100 -114
  161. package/native/Raw.d.ts +1 -1
  162. package/native/Raw.js +126 -146
  163. package/native/Set.d.ts +1 -1
  164. package/native/Set.js +197 -211
  165. package/native/Struct.d.ts +1 -1
  166. package/native/Struct.data.js +0 -3
  167. package/native/Struct.js +235 -242
  168. package/native/Text.d.ts +1 -1
  169. package/native/Text.js +135 -144
  170. package/native/index.d.ts +8 -8
  171. package/native/index.js +8 -11
  172. package/package.json +8 -5
  173. package/packageInfo.js +1 -11
  174. package/primitive/F32.d.ts +2 -2
  175. package/primitive/F32.js +6 -7
  176. package/primitive/F64.d.ts +2 -2
  177. package/primitive/F64.js +6 -7
  178. package/primitive/I128.d.ts +2 -2
  179. package/primitive/I128.js +6 -7
  180. package/primitive/I16.d.ts +2 -2
  181. package/primitive/I16.js +6 -7
  182. package/primitive/I256.d.ts +2 -2
  183. package/primitive/I256.js +6 -7
  184. package/primitive/I32.d.ts +2 -2
  185. package/primitive/I32.js +6 -7
  186. package/primitive/I64.d.ts +2 -2
  187. package/primitive/I64.js +6 -7
  188. package/primitive/I8.d.ts +2 -2
  189. package/primitive/I8.js +6 -7
  190. package/primitive/U128.d.ts +2 -2
  191. package/primitive/U128.js +6 -7
  192. package/primitive/U16.d.ts +2 -2
  193. package/primitive/U16.js +6 -7
  194. package/primitive/U256.d.ts +2 -2
  195. package/primitive/U256.js +6 -7
  196. package/primitive/U32.d.ts +2 -2
  197. package/primitive/U32.js +6 -7
  198. package/primitive/U64.d.ts +2 -2
  199. package/primitive/U64.js +6 -7
  200. package/primitive/U8.d.ts +2 -2
  201. package/primitive/U8.js +6 -7
  202. package/primitive/USize.d.ts +2 -2
  203. package/primitive/USize.js +5 -9
  204. package/primitive/index.d.ts +15 -15
  205. package/primitive/index.js +15 -18
  206. package/types/codec.d.ts +3 -3
  207. package/types/helpers.d.ts +1 -1
  208. package/types/index.d.ts +4 -4
  209. package/types/index.js +4 -7
  210. package/types/interfaces.d.ts +2 -2
  211. package/types/registry.d.ts +3 -3
  212. package/utils/compareArray.js +7 -11
  213. package/utils/compareMap.js +18 -19
  214. package/utils/compareSet.js +12 -16
  215. package/utils/decodeU8a.d.ts +1 -1
  216. package/utils/decodeU8a.js +54 -61
  217. package/utils/index.d.ts +9 -9
  218. package/utils/index.js +9 -12
  219. package/utils/mapToTypeMap.d.ts +1 -1
  220. package/utils/mapToTypeMap.js +9 -13
  221. package/utils/sanitize.d.ts +1 -1
  222. package/utils/sanitize.js +182 -185
  223. package/utils/sortValues.d.ts +1 -1
  224. package/utils/sortValues.js +32 -36
  225. package/utils/typeToConstructor.d.ts +1 -1
  226. package/utils/typeToConstructor.js +3 -4
  227. package/utils/typesToMap.d.ts +1 -1
  228. package/utils/typesToMap.js +5 -8
  229. 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_js_1 = require("./Array.js");
5
+ Object.defineProperty(exports, "AbstractArray", { enumerable: true, get: function () { return Array_js_1.AbstractArray; } });
6
+ var Base_js_1 = require("./Base.js");
7
+ Object.defineProperty(exports, "AbstractBase", { enumerable: true, get: function () { return Base_js_1.AbstractBase; } });
8
+ var Int_js_1 = require("./Int.js");
9
+ Object.defineProperty(exports, "AbstractInt", { enumerable: true, get: function () { return Int_js_1.AbstractInt; } });