@polkadot/types-codec 6.12.2-2

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