@polkadot/types-codec 6.12.2-10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/abstract/AbstractArray.cjs +208 -0
- package/abstract/AbstractArray.d.ts +84 -0
- package/abstract/AbstractArray.js +197 -0
- package/abstract/AbstractInt.cjs +254 -0
- package/abstract/AbstractInt.d.ts +75 -0
- package/abstract/AbstractInt.js +231 -0
- package/abstract/index.cjs +21 -0
- package/abstract/index.d.ts +2 -0
- package/abstract/index.js +4 -0
- package/base/Base.cjs +137 -0
- package/base/Base.d.ts +56 -0
- package/base/Base.js +125 -0
- package/base/Compact.cjs +201 -0
- package/base/Compact.d.ts +83 -0
- package/base/Compact.js +186 -0
- package/base/DoNotConstruct.cjs +101 -0
- package/base/DoNotConstruct.d.ts +33 -0
- package/base/DoNotConstruct.js +88 -0
- package/base/Enum.cjs +445 -0
- package/base/Enum.d.ts +105 -0
- package/base/Enum.js +421 -0
- package/base/Int.cjs +45 -0
- package/base/Int.d.ts +16 -0
- package/base/Int.js +33 -0
- package/base/Null.cjs +111 -0
- package/base/Null.d.ts +54 -0
- package/base/Null.js +101 -0
- package/base/Option.cjs +256 -0
- package/base/Option.d.ts +88 -0
- package/base/Option.js +240 -0
- package/base/Result.cjs +109 -0
- package/base/Result.d.ts +46 -0
- package/base/Result.js +98 -0
- package/base/Tuple.cjs +132 -0
- package/base/Tuple.d.ts +39 -0
- package/base/Tuple.js +116 -0
- package/base/UInt.cjs +39 -0
- package/base/UInt.d.ts +15 -0
- package/base/UInt.js +29 -0
- package/base/Vec.cjs +126 -0
- package/base/Vec.d.ts +28 -0
- package/base/Vec.js +104 -0
- package/base/VecAny.cjs +31 -0
- package/base/VecAny.d.ts +15 -0
- package/base/VecAny.js +21 -0
- package/base/VecFixed.cjs +111 -0
- package/base/VecFixed.d.ts +26 -0
- package/base/VecFixed.js +93 -0
- package/base/index.cjs +109 -0
- package/base/index.d.ts +13 -0
- package/base/index.js +15 -0
- package/bundle.cjs +100 -0
- package/bundle.d.ts +7 -0
- package/bundle.js +9 -0
- package/detectPackage.cjs +9 -0
- package/detectPackage.d.ts +1 -0
- package/detectPackage.js +5 -0
- package/extended/BTreeMap.cjs +24 -0
- package/extended/BTreeMap.d.ts +5 -0
- package/extended/BTreeMap.js +14 -0
- package/extended/BTreeSet.cjs +227 -0
- package/extended/BTreeSet.d.ts +55 -0
- package/extended/BTreeSet.js +212 -0
- package/extended/BitVec.cjs +97 -0
- package/extended/BitVec.d.ts +26 -0
- package/extended/BitVec.js +82 -0
- package/extended/Bytes.cjs +86 -0
- package/extended/Bytes.d.ts +25 -0
- package/extended/Bytes.js +75 -0
- package/extended/HashMap.cjs +24 -0
- package/extended/HashMap.d.ts +5 -0
- package/extended/HashMap.js +14 -0
- package/extended/Linkage.cjs +89 -0
- package/extended/Linkage.d.ts +31 -0
- package/extended/Linkage.js +71 -0
- package/extended/Map.cjs +244 -0
- package/extended/Map.d.ts +50 -0
- package/extended/Map.js +225 -0
- package/extended/Range.cjs +92 -0
- package/extended/Range.d.ts +30 -0
- package/extended/Range.js +75 -0
- package/extended/Type.cjs +39 -0
- package/extended/Type.d.ts +16 -0
- package/extended/Type.js +27 -0
- package/extended/U8aFixed.cjs +66 -0
- package/extended/U8aFixed.d.ts +16 -0
- package/extended/U8aFixed.js +53 -0
- package/extended/WrapperOpaque.cjs +60 -0
- package/extended/WrapperOpaque.d.ts +14 -0
- package/extended/WrapperOpaque.js +49 -0
- package/extended/index.cjs +105 -0
- package/extended/index.d.ts +11 -0
- package/extended/index.js +13 -0
- package/index.cjs +20 -0
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/native/Bool.cjs +141 -0
- package/native/Bool.d.ts +62 -0
- package/native/Bool.js +130 -0
- package/native/Date.cjs +170 -0
- package/native/Date.d.ts +76 -0
- package/native/Date.js +158 -0
- package/native/Json.cjs +137 -0
- package/native/Json.d.ts +56 -0
- package/native/Json.js +121 -0
- package/native/Raw.cjs +192 -0
- package/native/Raw.d.ts +90 -0
- package/native/Raw.js +182 -0
- package/native/Set.cjs +268 -0
- package/native/Set.d.ts +76 -0
- package/native/Set.js +251 -0
- package/native/Struct.cjs +302 -0
- package/native/Struct.d.ts +93 -0
- package/native/Struct.js +286 -0
- package/native/Text.cjs +193 -0
- package/native/Text.d.ts +71 -0
- package/native/Text.js +178 -0
- package/native/index.cjs +79 -0
- package/native/index.d.ts +7 -0
- package/native/index.js +9 -0
- package/package.json +387 -0
- package/packageInfo.cjs +14 -0
- package/packageInfo.d.ts +4 -0
- package/packageInfo.js +7 -0
- package/primitive/I128.cjs +26 -0
- package/primitive/I128.d.ts +11 -0
- package/primitive/I128.js +16 -0
- package/primitive/I16.cjs +26 -0
- package/primitive/I16.d.ts +11 -0
- package/primitive/I16.js +16 -0
- package/primitive/I256.cjs +26 -0
- package/primitive/I256.d.ts +11 -0
- package/primitive/I256.js +16 -0
- package/primitive/I32.cjs +26 -0
- package/primitive/I32.d.ts +11 -0
- package/primitive/I32.js +16 -0
- package/primitive/I64.cjs +26 -0
- package/primitive/I64.d.ts +11 -0
- package/primitive/I64.js +16 -0
- package/primitive/I8.cjs +26 -0
- package/primitive/I8.d.ts +11 -0
- package/primitive/I8.js +16 -0
- package/primitive/U128.cjs +26 -0
- package/primitive/U128.d.ts +11 -0
- package/primitive/U128.js +16 -0
- package/primitive/U16.cjs +26 -0
- package/primitive/U16.d.ts +11 -0
- package/primitive/U16.js +16 -0
- package/primitive/U256.cjs +26 -0
- package/primitive/U256.d.ts +11 -0
- package/primitive/U256.js +16 -0
- package/primitive/U32.cjs +26 -0
- package/primitive/U32.d.ts +11 -0
- package/primitive/U32.js +16 -0
- package/primitive/U64.cjs +26 -0
- package/primitive/U64.d.ts +11 -0
- package/primitive/U64.js +16 -0
- package/primitive/U8.cjs +26 -0
- package/primitive/U8.d.ts +11 -0
- package/primitive/U8.js +16 -0
- package/primitive/USize.cjs +28 -0
- package/primitive/USize.d.ts +12 -0
- package/primitive/USize.js +18 -0
- package/primitive/index.cjs +187 -0
- package/primitive/index.d.ts +13 -0
- package/primitive/index.js +15 -0
- package/types/codec.cjs +1 -0
- package/types/codec.d.ts +77 -0
- package/types/codec.js +1 -0
- package/types/helpers.cjs +1 -0
- package/types/helpers.d.ts +14 -0
- package/types/helpers.js +1 -0
- package/types/index.cjs +57 -0
- package/types/index.d.ts +4 -0
- package/types/index.js +6 -0
- package/types/interfaces.cjs +1 -0
- package/types/interfaces.d.ts +69 -0
- package/types/interfaces.js +1 -0
- package/types/registry.cjs +1 -0
- package/types/registry.d.ts +61 -0
- package/types/registry.js +1 -0
- package/utils/compareArray.cjs +22 -0
- package/utils/compareArray.d.ts +1 -0
- package/utils/compareArray.js +13 -0
- package/utils/compareMap.cjs +39 -0
- package/utils/compareMap.d.ts +1 -0
- package/utils/compareMap.js +31 -0
- package/utils/compareSet.cjs +29 -0
- package/utils/compareSet.d.ts +1 -0
- package/utils/compareSet.js +22 -0
- package/utils/decodeU8a.cjs +69 -0
- package/utils/decodeU8a.d.ts +13 -0
- package/utils/decodeU8a.js +60 -0
- package/utils/index.cjs +95 -0
- package/utils/index.d.ts +9 -0
- package/utils/index.js +11 -0
- package/utils/mapToTypeMap.cjs +25 -0
- package/utils/mapToTypeMap.d.ts +5 -0
- package/utils/mapToTypeMap.js +17 -0
- package/utils/sanitize.cjs +242 -0
- package/utils/sanitize.d.ts +16 -0
- package/utils/sanitize.js +215 -0
- package/utils/sortValues.cjs +89 -0
- package/utils/sortValues.d.ts +13 -0
- package/utils/sortValues.js +73 -0
- package/utils/typeToConstructor.cjs +14 -0
- package/utils/typeToConstructor.d.ts +2 -0
- package/utils/typeToConstructor.js +6 -0
- package/utils/typesToMap.cjs +20 -0
- package/utils/typesToMap.d.ts +2 -0
- package/utils/typesToMap.js +13 -0
- package/utils/util.cjs +15 -0
- package/utils/util.d.ts +3 -0
- 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");
|