@sovryn-zero/lib-base 0.1.0 → 0.2.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.
- package/dist/index.d.ts +13 -13
- package/dist/index.js +25 -25
- package/dist/src/Decimal.d.ts +88 -88
- package/dist/src/Decimal.js +360 -360
- package/dist/src/Fees.d.ts +81 -81
- package/dist/src/Fees.js +122 -122
- package/dist/src/LiquityStore.d.ts +208 -208
- package/dist/src/LiquityStore.js +208 -208
- package/dist/src/ObservableLiquity.d.ts +14 -14
- package/dist/src/ObservableLiquity.js +2 -2
- package/dist/src/PopulatableLiquity.d.ts +124 -124
- package/dist/src/PopulatableLiquity.js +2 -2
- package/dist/src/ReadableLiquity.d.ts +155 -155
- package/dist/src/ReadableLiquity.js +2 -2
- package/dist/src/SendableLiquity.d.ts +155 -155
- package/dist/src/SendableLiquity.js +19 -19
- package/dist/src/StabilityDeposit.d.ts +58 -58
- package/dist/src/StabilityDeposit.js +79 -79
- package/dist/src/TransactableLiquity.d.ts +413 -413
- package/dist/src/TransactableLiquity.js +17 -17
- package/dist/src/Trove.d.ts +366 -366
- package/dist/src/Trove.js +422 -422
- package/dist/src/ZEROStake.d.ts +51 -51
- package/dist/src/ZEROStake.js +73 -73
- package/dist/src/_CachedReadableLiquity.d.ts +54 -54
- package/dist/src/_CachedReadableLiquity.js +92 -92
- package/dist/src/constants.d.ts +60 -60
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +63 -63
- package/dist/src/constants.js.map +1 -1
- package/package.json +2 -2
- package/src/constants.ts +3 -3
package/dist/src/Decimal.js
CHANGED
@@ -1,361 +1,361 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.Percent = exports.Difference = exports.Decimal = void 0;
|
7
|
-
const assert_1 = __importDefault(require("assert"));
|
8
|
-
const bignumber_1 = require("@ethersproject/bignumber");
|
9
|
-
const getDigits = (numDigits) => TEN.pow(numDigits);
|
10
|
-
const MAX_UINT_256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
11
|
-
const PRECISION = 18;
|
12
|
-
const ONE = bignumber_1.BigNumber.from(1);
|
13
|
-
const TEN = bignumber_1.BigNumber.from(10);
|
14
|
-
const DIGITS = getDigits(PRECISION);
|
15
|
-
const stringRepresentationFormat = /^[0-9]*(\.[0-9]*)?(e[-+]?[0-9]+)?$/;
|
16
|
-
const trailingZeros = /0*$/;
|
17
|
-
const magnitudes = ["", "K", "M", "B", "T"];
|
18
|
-
const roundedMul = (x, y) => x.mul(y).add(Decimal.HALF.hex).div(DIGITS);
|
19
|
-
/**
|
20
|
-
* Fixed-point decimal bignumber with 18 digits of precision.
|
21
|
-
*
|
22
|
-
* @remarks
|
23
|
-
* Used by Zero libraries to precisely represent native currency (e.g. Ether), ZUSD and ZERO
|
24
|
-
* amounts, as well as derived metrics like collateral ratios.
|
25
|
-
*
|
26
|
-
* @public
|
27
|
-
*/
|
28
|
-
class Decimal {
|
29
|
-
constructor(bigNumber) {
|
30
|
-
if (bigNumber.isNegative()) {
|
31
|
-
throw new Error("negatives not supported by Decimal");
|
32
|
-
}
|
33
|
-
this._bigNumber = bigNumber;
|
34
|
-
}
|
35
|
-
/** @internal */
|
36
|
-
get hex() {
|
37
|
-
return this._bigNumber.toHexString();
|
38
|
-
}
|
39
|
-
/** @internal */
|
40
|
-
get bigNumber() {
|
41
|
-
return this._bigNumber.toString();
|
42
|
-
}
|
43
|
-
static fromBigNumberString(bigNumberString) {
|
44
|
-
return new Decimal(bignumber_1.BigNumber.from(bigNumberString));
|
45
|
-
}
|
46
|
-
static _fromString(representation) {
|
47
|
-
if (!representation || !representation.match(stringRepresentationFormat)) {
|
48
|
-
throw new Error(`bad decimal format: "${representation}"`);
|
49
|
-
}
|
50
|
-
if (representation.includes("e")) {
|
51
|
-
// eslint-disable-next-line prefer-const
|
52
|
-
let [coefficient, exponent] = representation.split("e");
|
53
|
-
if (exponent.startsWith("-")) {
|
54
|
-
return new Decimal(Decimal._fromString(coefficient)._bigNumber.div(TEN.pow(bignumber_1.BigNumber.from(exponent.substr(1)))));
|
55
|
-
}
|
56
|
-
if (exponent.startsWith("+")) {
|
57
|
-
exponent = exponent.substr(1);
|
58
|
-
}
|
59
|
-
return new Decimal(Decimal._fromString(coefficient)._bigNumber.mul(TEN.pow(bignumber_1.BigNumber.from(exponent))));
|
60
|
-
}
|
61
|
-
if (!representation.includes(".")) {
|
62
|
-
return new Decimal(bignumber_1.BigNumber.from(representation).mul(DIGITS));
|
63
|
-
}
|
64
|
-
// eslint-disable-next-line prefer-const
|
65
|
-
let [characteristic, mantissa] = representation.split(".");
|
66
|
-
if (mantissa.length < PRECISION) {
|
67
|
-
mantissa += "0".repeat(PRECISION - mantissa.length);
|
68
|
-
}
|
69
|
-
else {
|
70
|
-
mantissa = mantissa.substr(0, PRECISION);
|
71
|
-
}
|
72
|
-
return new Decimal(bignumber_1.BigNumber.from(characteristic || 0)
|
73
|
-
.mul(DIGITS)
|
74
|
-
.add(mantissa));
|
75
|
-
}
|
76
|
-
static from(decimalish) {
|
77
|
-
switch (typeof decimalish) {
|
78
|
-
case "object":
|
79
|
-
if (decimalish instanceof Decimal) {
|
80
|
-
return decimalish;
|
81
|
-
}
|
82
|
-
else {
|
83
|
-
throw new Error("invalid Decimalish value");
|
84
|
-
}
|
85
|
-
case "string":
|
86
|
-
return Decimal._fromString(decimalish);
|
87
|
-
case "number":
|
88
|
-
return Decimal._fromString(decimalish.toString());
|
89
|
-
default:
|
90
|
-
throw new Error("invalid Decimalish value");
|
91
|
-
}
|
92
|
-
}
|
93
|
-
_toStringWithAutomaticPrecision() {
|
94
|
-
const characteristic = this._bigNumber.div(DIGITS);
|
95
|
-
const mantissa = this._bigNumber.mod(DIGITS);
|
96
|
-
if (mantissa.isZero()) {
|
97
|
-
return characteristic.toString();
|
98
|
-
}
|
99
|
-
else {
|
100
|
-
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
101
|
-
const trimmedMantissa = paddedMantissa.replace(trailingZeros, "");
|
102
|
-
return characteristic.toString() + "." + trimmedMantissa;
|
103
|
-
}
|
104
|
-
}
|
105
|
-
_roundUp(precision) {
|
106
|
-
const halfDigit = getDigits(PRECISION - 1 - precision).mul(5);
|
107
|
-
return this._bigNumber.add(halfDigit);
|
108
|
-
}
|
109
|
-
_toStringWithPrecision(precision) {
|
110
|
-
if (precision < 0) {
|
111
|
-
throw new Error("precision must not be negative");
|
112
|
-
}
|
113
|
-
const value = precision < PRECISION ? this._roundUp(precision) : this._bigNumber;
|
114
|
-
const characteristic = value.div(DIGITS);
|
115
|
-
const mantissa = value.mod(DIGITS);
|
116
|
-
if (precision === 0) {
|
117
|
-
return characteristic.toString();
|
118
|
-
}
|
119
|
-
else {
|
120
|
-
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
121
|
-
const trimmedMantissa = paddedMantissa.substr(0, precision);
|
122
|
-
return characteristic.toString() + "." + trimmedMantissa;
|
123
|
-
}
|
124
|
-
}
|
125
|
-
toString(precision) {
|
126
|
-
if (this.infinite) {
|
127
|
-
return "∞";
|
128
|
-
}
|
129
|
-
else if (precision !== undefined) {
|
130
|
-
return this._toStringWithPrecision(precision);
|
131
|
-
}
|
132
|
-
else {
|
133
|
-
return this._toStringWithAutomaticPrecision();
|
134
|
-
}
|
135
|
-
}
|
136
|
-
prettify(precision = 2) {
|
137
|
-
const [characteristic, mantissa] = parseFloat(this.toString(precision)).toString().split(".");
|
138
|
-
const prettyCharacteristic = characteristic.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
|
139
|
-
return mantissa !== undefined ? prettyCharacteristic + "." + mantissa : prettyCharacteristic;
|
140
|
-
}
|
141
|
-
shorten() {
|
142
|
-
const characteristicLength = this.toString(0).length;
|
143
|
-
const magnitude = Math.min(Math.floor((characteristicLength - 1) / 3), magnitudes.length - 1);
|
144
|
-
const precision = Math.max(3 * (magnitude + 1) - characteristicLength, 0);
|
145
|
-
const normalized = this.div(new Decimal(getDigits(PRECISION + 3 * magnitude)));
|
146
|
-
return normalized.prettify(precision) + magnitudes[magnitude];
|
147
|
-
}
|
148
|
-
add(addend) {
|
149
|
-
return new Decimal(this._bigNumber.add(Decimal.from(addend)._bigNumber));
|
150
|
-
}
|
151
|
-
sub(subtrahend) {
|
152
|
-
return new Decimal(this._bigNumber.sub(Decimal.from(subtrahend)._bigNumber));
|
153
|
-
}
|
154
|
-
mul(multiplier) {
|
155
|
-
return new Decimal(this._bigNumber.mul(Decimal.from(multiplier)._bigNumber).div(DIGITS));
|
156
|
-
}
|
157
|
-
div(divider) {
|
158
|
-
divider = Decimal.from(divider);
|
159
|
-
if (divider.isZero) {
|
160
|
-
return Decimal.INFINITY;
|
161
|
-
}
|
162
|
-
return new Decimal(this._bigNumber.mul(DIGITS).div(divider._bigNumber));
|
163
|
-
}
|
164
|
-
/** @internal */
|
165
|
-
_divCeil(divider) {
|
166
|
-
divider = Decimal.from(divider);
|
167
|
-
if (divider.isZero) {
|
168
|
-
return Decimal.INFINITY;
|
169
|
-
}
|
170
|
-
return new Decimal(this._bigNumber.mul(DIGITS).add(divider._bigNumber.sub(ONE)).div(divider._bigNumber));
|
171
|
-
}
|
172
|
-
mulDiv(multiplier, divider) {
|
173
|
-
multiplier = Decimal.from(multiplier);
|
174
|
-
divider = Decimal.from(divider);
|
175
|
-
if (divider.isZero) {
|
176
|
-
return Decimal.INFINITY;
|
177
|
-
}
|
178
|
-
return new Decimal(this._bigNumber.mul(multiplier._bigNumber).div(divider._bigNumber));
|
179
|
-
}
|
180
|
-
pow(exponent) {
|
181
|
-
assert_1.default(Number.isInteger(exponent));
|
182
|
-
assert_1.default(0 <= exponent && exponent <= 0xffffffff); // Ensure we're safe to use bitwise ops
|
183
|
-
if (exponent === 0) {
|
184
|
-
return Decimal.ONE;
|
185
|
-
}
|
186
|
-
if (exponent === 1) {
|
187
|
-
return this;
|
188
|
-
}
|
189
|
-
let x = this._bigNumber;
|
190
|
-
let y = DIGITS;
|
191
|
-
for (; exponent > 1; exponent >>>= 1) {
|
192
|
-
if (exponent & 1) {
|
193
|
-
y = roundedMul(x, y);
|
194
|
-
}
|
195
|
-
x = roundedMul(x, x);
|
196
|
-
}
|
197
|
-
return new Decimal(roundedMul(x, y));
|
198
|
-
}
|
199
|
-
get isZero() {
|
200
|
-
return this._bigNumber.isZero();
|
201
|
-
}
|
202
|
-
get zero() {
|
203
|
-
if (this.isZero) {
|
204
|
-
return this;
|
205
|
-
}
|
206
|
-
}
|
207
|
-
get nonZero() {
|
208
|
-
if (!this.isZero) {
|
209
|
-
return this;
|
210
|
-
}
|
211
|
-
}
|
212
|
-
get infinite() {
|
213
|
-
if (this.eq(Decimal.INFINITY)) {
|
214
|
-
return this;
|
215
|
-
}
|
216
|
-
}
|
217
|
-
get finite() {
|
218
|
-
if (!this.eq(Decimal.INFINITY)) {
|
219
|
-
return this;
|
220
|
-
}
|
221
|
-
}
|
222
|
-
/** @internal */
|
223
|
-
get absoluteValue() {
|
224
|
-
return this;
|
225
|
-
}
|
226
|
-
lt(that) {
|
227
|
-
return this._bigNumber.lt(Decimal.from(that)._bigNumber);
|
228
|
-
}
|
229
|
-
eq(that) {
|
230
|
-
return this._bigNumber.eq(Decimal.from(that)._bigNumber);
|
231
|
-
}
|
232
|
-
gt(that) {
|
233
|
-
return this._bigNumber.gt(Decimal.from(that)._bigNumber);
|
234
|
-
}
|
235
|
-
gte(that) {
|
236
|
-
return this._bigNumber.gte(Decimal.from(that)._bigNumber);
|
237
|
-
}
|
238
|
-
lte(that) {
|
239
|
-
return this._bigNumber.lte(Decimal.from(that)._bigNumber);
|
240
|
-
}
|
241
|
-
static min(a, b) {
|
242
|
-
a = Decimal.from(a);
|
243
|
-
b = Decimal.from(b);
|
244
|
-
return a.lt(b) ? a : b;
|
245
|
-
}
|
246
|
-
static max(a, b) {
|
247
|
-
a = Decimal.from(a);
|
248
|
-
b = Decimal.from(b);
|
249
|
-
return a.gt(b) ? a : b;
|
250
|
-
}
|
251
|
-
}
|
252
|
-
exports.Decimal = Decimal;
|
253
|
-
Decimal.INFINITY = Decimal.fromBigNumberString(MAX_UINT_256);
|
254
|
-
Decimal.ZERO = Decimal.from(0);
|
255
|
-
Decimal.HALF = Decimal.from(0.5);
|
256
|
-
Decimal.ONE = Decimal.from(1);
|
257
|
-
/** @alpha */
|
258
|
-
class Difference {
|
259
|
-
constructor(number) {
|
260
|
-
this._number = number;
|
261
|
-
}
|
262
|
-
static between(d1, d2) {
|
263
|
-
if (d1 === undefined || d2 === undefined) {
|
264
|
-
return new Difference(undefined);
|
265
|
-
}
|
266
|
-
d1 = Decimal.from(d1);
|
267
|
-
d2 = Decimal.from(d2);
|
268
|
-
if (d1.infinite && d2.infinite) {
|
269
|
-
return new Difference(undefined);
|
270
|
-
}
|
271
|
-
else if (d1.infinite) {
|
272
|
-
return new Difference({ sign: "+", absoluteValue: d1 });
|
273
|
-
}
|
274
|
-
else if (d2.infinite) {
|
275
|
-
return new Difference({ sign: "-", absoluteValue: d2 });
|
276
|
-
}
|
277
|
-
else if (d1.gt(d2)) {
|
278
|
-
return new Difference({ sign: "+", absoluteValue: Decimal.from(d1).sub(d2) });
|
279
|
-
}
|
280
|
-
else if (d2.gt(d1)) {
|
281
|
-
return new Difference({ sign: "-", absoluteValue: Decimal.from(d2).sub(d1) });
|
282
|
-
}
|
283
|
-
else {
|
284
|
-
return new Difference({ sign: "", absoluteValue: Decimal.ZERO });
|
285
|
-
}
|
286
|
-
}
|
287
|
-
toString(precision) {
|
288
|
-
if (!this._number) {
|
289
|
-
return "N/A";
|
290
|
-
}
|
291
|
-
return this._number.sign + this._number.absoluteValue.toString(precision);
|
292
|
-
}
|
293
|
-
prettify(precision) {
|
294
|
-
if (!this._number) {
|
295
|
-
return this.toString();
|
296
|
-
}
|
297
|
-
return this._number.sign + this._number.absoluteValue.prettify(precision);
|
298
|
-
}
|
299
|
-
mul(multiplier) {
|
300
|
-
return new Difference(this._number && {
|
301
|
-
sign: this._number.sign,
|
302
|
-
absoluteValue: this._number.absoluteValue.mul(multiplier)
|
303
|
-
});
|
304
|
-
}
|
305
|
-
get nonZero() {
|
306
|
-
var _a;
|
307
|
-
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.nonZero) && this;
|
308
|
-
}
|
309
|
-
get positive() {
|
310
|
-
var _a;
|
311
|
-
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.sign) === "+" ? this : undefined;
|
312
|
-
}
|
313
|
-
get negative() {
|
314
|
-
var _a;
|
315
|
-
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.sign) === "-" ? this : undefined;
|
316
|
-
}
|
317
|
-
get absoluteValue() {
|
318
|
-
var _a;
|
319
|
-
return (_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue;
|
320
|
-
}
|
321
|
-
get infinite() {
|
322
|
-
var _a;
|
323
|
-
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.infinite) && this;
|
324
|
-
}
|
325
|
-
get finite() {
|
326
|
-
var _a;
|
327
|
-
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.finite) && this;
|
328
|
-
}
|
329
|
-
}
|
330
|
-
exports.Difference = Difference;
|
331
|
-
/** @alpha */
|
332
|
-
class Percent {
|
333
|
-
constructor(ratio) {
|
334
|
-
this._percent = ratio.infinite || (ratio.mul && ratio.mul(100)) || ratio;
|
335
|
-
}
|
336
|
-
nonZeroish(precision) {
|
337
|
-
var _a;
|
338
|
-
const zeroish = `0.${"0".repeat(precision)}5`;
|
339
|
-
if ((_a = this._percent.absoluteValue) === null || _a === void 0 ? void 0 : _a.gte(zeroish)) {
|
340
|
-
return this;
|
341
|
-
}
|
342
|
-
}
|
343
|
-
toString(precision) {
|
344
|
-
return (this._percent.toString(precision) +
|
345
|
-
(this._percent.absoluteValue && !this._percent.infinite ? "%" : ""));
|
346
|
-
}
|
347
|
-
prettify() {
|
348
|
-
var _a, _b;
|
349
|
-
if ((_a = this._percent.absoluteValue) === null || _a === void 0 ? void 0 : _a.gte("1000")) {
|
350
|
-
return this.toString(0);
|
351
|
-
}
|
352
|
-
else if ((_b = this._percent.absoluteValue) === null || _b === void 0 ? void 0 : _b.gte("10")) {
|
353
|
-
return this.toString(1);
|
354
|
-
}
|
355
|
-
else {
|
356
|
-
return this.toString(2);
|
357
|
-
}
|
358
|
-
}
|
359
|
-
}
|
360
|
-
exports.Percent = Percent;
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.Percent = exports.Difference = exports.Decimal = void 0;
|
7
|
+
const assert_1 = __importDefault(require("assert"));
|
8
|
+
const bignumber_1 = require("@ethersproject/bignumber");
|
9
|
+
const getDigits = (numDigits) => TEN.pow(numDigits);
|
10
|
+
const MAX_UINT_256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
|
11
|
+
const PRECISION = 18;
|
12
|
+
const ONE = bignumber_1.BigNumber.from(1);
|
13
|
+
const TEN = bignumber_1.BigNumber.from(10);
|
14
|
+
const DIGITS = getDigits(PRECISION);
|
15
|
+
const stringRepresentationFormat = /^[0-9]*(\.[0-9]*)?(e[-+]?[0-9]+)?$/;
|
16
|
+
const trailingZeros = /0*$/;
|
17
|
+
const magnitudes = ["", "K", "M", "B", "T"];
|
18
|
+
const roundedMul = (x, y) => x.mul(y).add(Decimal.HALF.hex).div(DIGITS);
|
19
|
+
/**
|
20
|
+
* Fixed-point decimal bignumber with 18 digits of precision.
|
21
|
+
*
|
22
|
+
* @remarks
|
23
|
+
* Used by Zero libraries to precisely represent native currency (e.g. Ether), ZUSD and ZERO
|
24
|
+
* amounts, as well as derived metrics like collateral ratios.
|
25
|
+
*
|
26
|
+
* @public
|
27
|
+
*/
|
28
|
+
class Decimal {
|
29
|
+
constructor(bigNumber) {
|
30
|
+
if (bigNumber.isNegative()) {
|
31
|
+
throw new Error("negatives not supported by Decimal");
|
32
|
+
}
|
33
|
+
this._bigNumber = bigNumber;
|
34
|
+
}
|
35
|
+
/** @internal */
|
36
|
+
get hex() {
|
37
|
+
return this._bigNumber.toHexString();
|
38
|
+
}
|
39
|
+
/** @internal */
|
40
|
+
get bigNumber() {
|
41
|
+
return this._bigNumber.toString();
|
42
|
+
}
|
43
|
+
static fromBigNumberString(bigNumberString) {
|
44
|
+
return new Decimal(bignumber_1.BigNumber.from(bigNumberString));
|
45
|
+
}
|
46
|
+
static _fromString(representation) {
|
47
|
+
if (!representation || !representation.match(stringRepresentationFormat)) {
|
48
|
+
throw new Error(`bad decimal format: "${representation}"`);
|
49
|
+
}
|
50
|
+
if (representation.includes("e")) {
|
51
|
+
// eslint-disable-next-line prefer-const
|
52
|
+
let [coefficient, exponent] = representation.split("e");
|
53
|
+
if (exponent.startsWith("-")) {
|
54
|
+
return new Decimal(Decimal._fromString(coefficient)._bigNumber.div(TEN.pow(bignumber_1.BigNumber.from(exponent.substr(1)))));
|
55
|
+
}
|
56
|
+
if (exponent.startsWith("+")) {
|
57
|
+
exponent = exponent.substr(1);
|
58
|
+
}
|
59
|
+
return new Decimal(Decimal._fromString(coefficient)._bigNumber.mul(TEN.pow(bignumber_1.BigNumber.from(exponent))));
|
60
|
+
}
|
61
|
+
if (!representation.includes(".")) {
|
62
|
+
return new Decimal(bignumber_1.BigNumber.from(representation).mul(DIGITS));
|
63
|
+
}
|
64
|
+
// eslint-disable-next-line prefer-const
|
65
|
+
let [characteristic, mantissa] = representation.split(".");
|
66
|
+
if (mantissa.length < PRECISION) {
|
67
|
+
mantissa += "0".repeat(PRECISION - mantissa.length);
|
68
|
+
}
|
69
|
+
else {
|
70
|
+
mantissa = mantissa.substr(0, PRECISION);
|
71
|
+
}
|
72
|
+
return new Decimal(bignumber_1.BigNumber.from(characteristic || 0)
|
73
|
+
.mul(DIGITS)
|
74
|
+
.add(mantissa));
|
75
|
+
}
|
76
|
+
static from(decimalish) {
|
77
|
+
switch (typeof decimalish) {
|
78
|
+
case "object":
|
79
|
+
if (decimalish instanceof Decimal) {
|
80
|
+
return decimalish;
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
throw new Error("invalid Decimalish value");
|
84
|
+
}
|
85
|
+
case "string":
|
86
|
+
return Decimal._fromString(decimalish);
|
87
|
+
case "number":
|
88
|
+
return Decimal._fromString(decimalish.toString());
|
89
|
+
default:
|
90
|
+
throw new Error("invalid Decimalish value");
|
91
|
+
}
|
92
|
+
}
|
93
|
+
_toStringWithAutomaticPrecision() {
|
94
|
+
const characteristic = this._bigNumber.div(DIGITS);
|
95
|
+
const mantissa = this._bigNumber.mod(DIGITS);
|
96
|
+
if (mantissa.isZero()) {
|
97
|
+
return characteristic.toString();
|
98
|
+
}
|
99
|
+
else {
|
100
|
+
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
101
|
+
const trimmedMantissa = paddedMantissa.replace(trailingZeros, "");
|
102
|
+
return characteristic.toString() + "." + trimmedMantissa;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
_roundUp(precision) {
|
106
|
+
const halfDigit = getDigits(PRECISION - 1 - precision).mul(5);
|
107
|
+
return this._bigNumber.add(halfDigit);
|
108
|
+
}
|
109
|
+
_toStringWithPrecision(precision) {
|
110
|
+
if (precision < 0) {
|
111
|
+
throw new Error("precision must not be negative");
|
112
|
+
}
|
113
|
+
const value = precision < PRECISION ? this._roundUp(precision) : this._bigNumber;
|
114
|
+
const characteristic = value.div(DIGITS);
|
115
|
+
const mantissa = value.mod(DIGITS);
|
116
|
+
if (precision === 0) {
|
117
|
+
return characteristic.toString();
|
118
|
+
}
|
119
|
+
else {
|
120
|
+
const paddedMantissa = mantissa.toString().padStart(PRECISION, "0");
|
121
|
+
const trimmedMantissa = paddedMantissa.substr(0, precision);
|
122
|
+
return characteristic.toString() + "." + trimmedMantissa;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
toString(precision) {
|
126
|
+
if (this.infinite) {
|
127
|
+
return "∞";
|
128
|
+
}
|
129
|
+
else if (precision !== undefined) {
|
130
|
+
return this._toStringWithPrecision(precision);
|
131
|
+
}
|
132
|
+
else {
|
133
|
+
return this._toStringWithAutomaticPrecision();
|
134
|
+
}
|
135
|
+
}
|
136
|
+
prettify(precision = 2) {
|
137
|
+
const [characteristic, mantissa] = parseFloat(this.toString(precision)).toString().split(".");
|
138
|
+
const prettyCharacteristic = characteristic.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
|
139
|
+
return mantissa !== undefined ? prettyCharacteristic + "." + mantissa : prettyCharacteristic;
|
140
|
+
}
|
141
|
+
shorten() {
|
142
|
+
const characteristicLength = this.toString(0).length;
|
143
|
+
const magnitude = Math.min(Math.floor((characteristicLength - 1) / 3), magnitudes.length - 1);
|
144
|
+
const precision = Math.max(3 * (magnitude + 1) - characteristicLength, 0);
|
145
|
+
const normalized = this.div(new Decimal(getDigits(PRECISION + 3 * magnitude)));
|
146
|
+
return normalized.prettify(precision) + magnitudes[magnitude];
|
147
|
+
}
|
148
|
+
add(addend) {
|
149
|
+
return new Decimal(this._bigNumber.add(Decimal.from(addend)._bigNumber));
|
150
|
+
}
|
151
|
+
sub(subtrahend) {
|
152
|
+
return new Decimal(this._bigNumber.sub(Decimal.from(subtrahend)._bigNumber));
|
153
|
+
}
|
154
|
+
mul(multiplier) {
|
155
|
+
return new Decimal(this._bigNumber.mul(Decimal.from(multiplier)._bigNumber).div(DIGITS));
|
156
|
+
}
|
157
|
+
div(divider) {
|
158
|
+
divider = Decimal.from(divider);
|
159
|
+
if (divider.isZero) {
|
160
|
+
return Decimal.INFINITY;
|
161
|
+
}
|
162
|
+
return new Decimal(this._bigNumber.mul(DIGITS).div(divider._bigNumber));
|
163
|
+
}
|
164
|
+
/** @internal */
|
165
|
+
_divCeil(divider) {
|
166
|
+
divider = Decimal.from(divider);
|
167
|
+
if (divider.isZero) {
|
168
|
+
return Decimal.INFINITY;
|
169
|
+
}
|
170
|
+
return new Decimal(this._bigNumber.mul(DIGITS).add(divider._bigNumber.sub(ONE)).div(divider._bigNumber));
|
171
|
+
}
|
172
|
+
mulDiv(multiplier, divider) {
|
173
|
+
multiplier = Decimal.from(multiplier);
|
174
|
+
divider = Decimal.from(divider);
|
175
|
+
if (divider.isZero) {
|
176
|
+
return Decimal.INFINITY;
|
177
|
+
}
|
178
|
+
return new Decimal(this._bigNumber.mul(multiplier._bigNumber).div(divider._bigNumber));
|
179
|
+
}
|
180
|
+
pow(exponent) {
|
181
|
+
assert_1.default(Number.isInteger(exponent));
|
182
|
+
assert_1.default(0 <= exponent && exponent <= 0xffffffff); // Ensure we're safe to use bitwise ops
|
183
|
+
if (exponent === 0) {
|
184
|
+
return Decimal.ONE;
|
185
|
+
}
|
186
|
+
if (exponent === 1) {
|
187
|
+
return this;
|
188
|
+
}
|
189
|
+
let x = this._bigNumber;
|
190
|
+
let y = DIGITS;
|
191
|
+
for (; exponent > 1; exponent >>>= 1) {
|
192
|
+
if (exponent & 1) {
|
193
|
+
y = roundedMul(x, y);
|
194
|
+
}
|
195
|
+
x = roundedMul(x, x);
|
196
|
+
}
|
197
|
+
return new Decimal(roundedMul(x, y));
|
198
|
+
}
|
199
|
+
get isZero() {
|
200
|
+
return this._bigNumber.isZero();
|
201
|
+
}
|
202
|
+
get zero() {
|
203
|
+
if (this.isZero) {
|
204
|
+
return this;
|
205
|
+
}
|
206
|
+
}
|
207
|
+
get nonZero() {
|
208
|
+
if (!this.isZero) {
|
209
|
+
return this;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
get infinite() {
|
213
|
+
if (this.eq(Decimal.INFINITY)) {
|
214
|
+
return this;
|
215
|
+
}
|
216
|
+
}
|
217
|
+
get finite() {
|
218
|
+
if (!this.eq(Decimal.INFINITY)) {
|
219
|
+
return this;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
/** @internal */
|
223
|
+
get absoluteValue() {
|
224
|
+
return this;
|
225
|
+
}
|
226
|
+
lt(that) {
|
227
|
+
return this._bigNumber.lt(Decimal.from(that)._bigNumber);
|
228
|
+
}
|
229
|
+
eq(that) {
|
230
|
+
return this._bigNumber.eq(Decimal.from(that)._bigNumber);
|
231
|
+
}
|
232
|
+
gt(that) {
|
233
|
+
return this._bigNumber.gt(Decimal.from(that)._bigNumber);
|
234
|
+
}
|
235
|
+
gte(that) {
|
236
|
+
return this._bigNumber.gte(Decimal.from(that)._bigNumber);
|
237
|
+
}
|
238
|
+
lte(that) {
|
239
|
+
return this._bigNumber.lte(Decimal.from(that)._bigNumber);
|
240
|
+
}
|
241
|
+
static min(a, b) {
|
242
|
+
a = Decimal.from(a);
|
243
|
+
b = Decimal.from(b);
|
244
|
+
return a.lt(b) ? a : b;
|
245
|
+
}
|
246
|
+
static max(a, b) {
|
247
|
+
a = Decimal.from(a);
|
248
|
+
b = Decimal.from(b);
|
249
|
+
return a.gt(b) ? a : b;
|
250
|
+
}
|
251
|
+
}
|
252
|
+
exports.Decimal = Decimal;
|
253
|
+
Decimal.INFINITY = Decimal.fromBigNumberString(MAX_UINT_256);
|
254
|
+
Decimal.ZERO = Decimal.from(0);
|
255
|
+
Decimal.HALF = Decimal.from(0.5);
|
256
|
+
Decimal.ONE = Decimal.from(1);
|
257
|
+
/** @alpha */
|
258
|
+
class Difference {
|
259
|
+
constructor(number) {
|
260
|
+
this._number = number;
|
261
|
+
}
|
262
|
+
static between(d1, d2) {
|
263
|
+
if (d1 === undefined || d2 === undefined) {
|
264
|
+
return new Difference(undefined);
|
265
|
+
}
|
266
|
+
d1 = Decimal.from(d1);
|
267
|
+
d2 = Decimal.from(d2);
|
268
|
+
if (d1.infinite && d2.infinite) {
|
269
|
+
return new Difference(undefined);
|
270
|
+
}
|
271
|
+
else if (d1.infinite) {
|
272
|
+
return new Difference({ sign: "+", absoluteValue: d1 });
|
273
|
+
}
|
274
|
+
else if (d2.infinite) {
|
275
|
+
return new Difference({ sign: "-", absoluteValue: d2 });
|
276
|
+
}
|
277
|
+
else if (d1.gt(d2)) {
|
278
|
+
return new Difference({ sign: "+", absoluteValue: Decimal.from(d1).sub(d2) });
|
279
|
+
}
|
280
|
+
else if (d2.gt(d1)) {
|
281
|
+
return new Difference({ sign: "-", absoluteValue: Decimal.from(d2).sub(d1) });
|
282
|
+
}
|
283
|
+
else {
|
284
|
+
return new Difference({ sign: "", absoluteValue: Decimal.ZERO });
|
285
|
+
}
|
286
|
+
}
|
287
|
+
toString(precision) {
|
288
|
+
if (!this._number) {
|
289
|
+
return "N/A";
|
290
|
+
}
|
291
|
+
return this._number.sign + this._number.absoluteValue.toString(precision);
|
292
|
+
}
|
293
|
+
prettify(precision) {
|
294
|
+
if (!this._number) {
|
295
|
+
return this.toString();
|
296
|
+
}
|
297
|
+
return this._number.sign + this._number.absoluteValue.prettify(precision);
|
298
|
+
}
|
299
|
+
mul(multiplier) {
|
300
|
+
return new Difference(this._number && {
|
301
|
+
sign: this._number.sign,
|
302
|
+
absoluteValue: this._number.absoluteValue.mul(multiplier)
|
303
|
+
});
|
304
|
+
}
|
305
|
+
get nonZero() {
|
306
|
+
var _a;
|
307
|
+
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.nonZero) && this;
|
308
|
+
}
|
309
|
+
get positive() {
|
310
|
+
var _a;
|
311
|
+
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.sign) === "+" ? this : undefined;
|
312
|
+
}
|
313
|
+
get negative() {
|
314
|
+
var _a;
|
315
|
+
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.sign) === "-" ? this : undefined;
|
316
|
+
}
|
317
|
+
get absoluteValue() {
|
318
|
+
var _a;
|
319
|
+
return (_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue;
|
320
|
+
}
|
321
|
+
get infinite() {
|
322
|
+
var _a;
|
323
|
+
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.infinite) && this;
|
324
|
+
}
|
325
|
+
get finite() {
|
326
|
+
var _a;
|
327
|
+
return ((_a = this._number) === null || _a === void 0 ? void 0 : _a.absoluteValue.finite) && this;
|
328
|
+
}
|
329
|
+
}
|
330
|
+
exports.Difference = Difference;
|
331
|
+
/** @alpha */
|
332
|
+
class Percent {
|
333
|
+
constructor(ratio) {
|
334
|
+
this._percent = ratio.infinite || (ratio.mul && ratio.mul(100)) || ratio;
|
335
|
+
}
|
336
|
+
nonZeroish(precision) {
|
337
|
+
var _a;
|
338
|
+
const zeroish = `0.${"0".repeat(precision)}5`;
|
339
|
+
if ((_a = this._percent.absoluteValue) === null || _a === void 0 ? void 0 : _a.gte(zeroish)) {
|
340
|
+
return this;
|
341
|
+
}
|
342
|
+
}
|
343
|
+
toString(precision) {
|
344
|
+
return (this._percent.toString(precision) +
|
345
|
+
(this._percent.absoluteValue && !this._percent.infinite ? "%" : ""));
|
346
|
+
}
|
347
|
+
prettify() {
|
348
|
+
var _a, _b;
|
349
|
+
if ((_a = this._percent.absoluteValue) === null || _a === void 0 ? void 0 : _a.gte("1000")) {
|
350
|
+
return this.toString(0);
|
351
|
+
}
|
352
|
+
else if ((_b = this._percent.absoluteValue) === null || _b === void 0 ? void 0 : _b.gte("10")) {
|
353
|
+
return this.toString(1);
|
354
|
+
}
|
355
|
+
else {
|
356
|
+
return this.toString(2);
|
357
|
+
}
|
358
|
+
}
|
359
|
+
}
|
360
|
+
exports.Percent = Percent;
|
361
361
|
//# sourceMappingURL=Decimal.js.map
|