@chainflip/utils 0.4.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/dist/assertion.js +2 -2
  2. package/dist/async.cjs +1 -1
  3. package/dist/async.d.cts +1 -1
  4. package/dist/async.d.ts +1 -1
  5. package/dist/async.js +1 -1
  6. package/dist/base58.js +4 -4
  7. package/dist/bytes.js +3 -3
  8. package/dist/chainflip.cjs +147 -0
  9. package/dist/chainflip.d.cts +99 -0
  10. package/dist/chainflip.d.ts +99 -0
  11. package/dist/chainflip.js +22 -0
  12. package/dist/{chunk-D7RIA7SA.js → chunk-2KA626OL.js} +1 -1
  13. package/dist/{chunk-UPA7KLTI.js → chunk-CGMSQQDO.js} +1 -1
  14. package/dist/{chunk-CZNX6EUV.js → chunk-HBIFE4XN.js} +7 -1
  15. package/dist/chunk-WGS2RGVR.js +1348 -0
  16. package/dist/chunk-XFHVSV76.js +115 -0
  17. package/dist/{chunk-MYP3UYWE.js → chunk-ZHIKNZLU.js} +1 -1
  18. package/dist/consts.cjs +146 -0
  19. package/dist/consts.d.cts +148 -0
  20. package/dist/consts.d.ts +148 -0
  21. package/dist/consts.js +113 -0
  22. package/dist/date.cjs +497 -0
  23. package/dist/date.d.cts +16 -0
  24. package/dist/date.d.ts +16 -0
  25. package/dist/date.js +455 -0
  26. package/dist/guard.cjs +9 -0
  27. package/dist/guard.d.cts +11 -8
  28. package/dist/guard.d.ts +11 -8
  29. package/dist/guard.js +7 -1
  30. package/dist/number.cjs +1386 -0
  31. package/dist/number.d.cts +6 -1
  32. package/dist/number.d.ts +6 -1
  33. package/dist/number.js +33 -0
  34. package/dist/ss58.cjs +23 -23
  35. package/dist/ss58.js +10 -10
  36. package/dist/string.cjs +21 -0
  37. package/dist/string.d.cts +7 -2
  38. package/dist/string.d.ts +7 -2
  39. package/dist/string.js +18 -0
  40. package/dist/tickMath.cjs +1502 -0
  41. package/dist/tickMath.d.cts +15 -0
  42. package/dist/tickMath.d.ts +15 -0
  43. package/dist/tickMath.js +35 -0
  44. package/package.json +9 -2
@@ -0,0 +1,1502 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/tickMath.ts
21
+ var tickMath_exports = {};
22
+ __export(tickMath_exports, {
23
+ FULL_TICK_RANGE: () => FULL_TICK_RANGE,
24
+ MAX_TICK: () => MAX_TICK,
25
+ MIN_TICK: () => MIN_TICK,
26
+ priceX128ToPrice: () => priceX128ToPrice,
27
+ rateToTick: () => rateToTick,
28
+ sqrtPriceX96ToPrice: () => sqrtPriceX96ToPrice,
29
+ tickToRate: () => tickToRate
30
+ });
31
+ module.exports = __toCommonJS(tickMath_exports);
32
+
33
+ // ../../node_modules/.pnpm/bignumber.js@9.1.2/node_modules/bignumber.js/bignumber.mjs
34
+ var isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i;
35
+ var mathceil = Math.ceil;
36
+ var mathfloor = Math.floor;
37
+ var bignumberError = "[BigNumber Error] ";
38
+ var tooManyDigits = bignumberError + "Number primitive has more than 15 significant digits: ";
39
+ var BASE = 1e14;
40
+ var LOG_BASE = 14;
41
+ var MAX_SAFE_INTEGER = 9007199254740991;
42
+ var POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13];
43
+ var SQRT_BASE = 1e7;
44
+ var MAX = 1e9;
45
+ function clone(configObject) {
46
+ var div, convertBase, parseNumeric, P = BigNumber2.prototype = { constructor: BigNumber2, toString: null, valueOf: null }, ONE = new BigNumber2(1), DECIMAL_PLACES = 20, ROUNDING_MODE = 4, TO_EXP_NEG = -7, TO_EXP_POS = 21, MIN_EXP = -1e7, MAX_EXP = 1e7, CRYPTO = false, MODULO_MODE = 1, POW_PRECISION = 0, FORMAT = {
47
+ prefix: "",
48
+ groupSize: 3,
49
+ secondaryGroupSize: 0,
50
+ groupSeparator: ",",
51
+ decimalSeparator: ".",
52
+ fractionGroupSize: 0,
53
+ fractionGroupSeparator: "\xA0",
54
+ // non-breaking space
55
+ suffix: ""
56
+ }, ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz", alphabetHasNormalDecimalDigits = true;
57
+ function BigNumber2(v, b) {
58
+ var alphabet, c, caseChanged, e, i, isNum, len, str, x = this;
59
+ if (!(x instanceof BigNumber2)) return new BigNumber2(v, b);
60
+ if (b == null) {
61
+ if (v && v._isBigNumber === true) {
62
+ x.s = v.s;
63
+ if (!v.c || v.e > MAX_EXP) {
64
+ x.c = x.e = null;
65
+ } else if (v.e < MIN_EXP) {
66
+ x.c = [x.e = 0];
67
+ } else {
68
+ x.e = v.e;
69
+ x.c = v.c.slice();
70
+ }
71
+ return;
72
+ }
73
+ if ((isNum = typeof v == "number") && v * 0 == 0) {
74
+ x.s = 1 / v < 0 ? (v = -v, -1) : 1;
75
+ if (v === ~~v) {
76
+ for (e = 0, i = v; i >= 10; i /= 10, e++) ;
77
+ if (e > MAX_EXP) {
78
+ x.c = x.e = null;
79
+ } else {
80
+ x.e = e;
81
+ x.c = [v];
82
+ }
83
+ return;
84
+ }
85
+ str = String(v);
86
+ } else {
87
+ if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);
88
+ x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
89
+ }
90
+ if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
91
+ if ((i = str.search(/e/i)) > 0) {
92
+ if (e < 0) e = i;
93
+ e += +str.slice(i + 1);
94
+ str = str.substring(0, i);
95
+ } else if (e < 0) {
96
+ e = str.length;
97
+ }
98
+ } else {
99
+ intCheck(b, 2, ALPHABET.length, "Base");
100
+ if (b == 10 && alphabetHasNormalDecimalDigits) {
101
+ x = new BigNumber2(v);
102
+ return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
103
+ }
104
+ str = String(v);
105
+ if (isNum = typeof v == "number") {
106
+ if (v * 0 != 0) return parseNumeric(x, str, isNum, b);
107
+ x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
108
+ if (BigNumber2.DEBUG && str.replace(/^0\.0*|\./, "").length > 15) {
109
+ throw Error(tooManyDigits + v);
110
+ }
111
+ } else {
112
+ x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
113
+ }
114
+ alphabet = ALPHABET.slice(0, b);
115
+ e = i = 0;
116
+ for (len = str.length; i < len; i++) {
117
+ if (alphabet.indexOf(c = str.charAt(i)) < 0) {
118
+ if (c == ".") {
119
+ if (i > e) {
120
+ e = len;
121
+ continue;
122
+ }
123
+ } else if (!caseChanged) {
124
+ if (str == str.toUpperCase() && (str = str.toLowerCase()) || str == str.toLowerCase() && (str = str.toUpperCase())) {
125
+ caseChanged = true;
126
+ i = -1;
127
+ e = 0;
128
+ continue;
129
+ }
130
+ }
131
+ return parseNumeric(x, String(v), isNum, b);
132
+ }
133
+ }
134
+ isNum = false;
135
+ str = convertBase(str, b, 10, x.s);
136
+ if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
137
+ else e = str.length;
138
+ }
139
+ for (i = 0; str.charCodeAt(i) === 48; i++) ;
140
+ for (len = str.length; str.charCodeAt(--len) === 48; ) ;
141
+ if (str = str.slice(i, ++len)) {
142
+ len -= i;
143
+ if (isNum && BigNumber2.DEBUG && len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
144
+ throw Error(tooManyDigits + x.s * v);
145
+ }
146
+ if ((e = e - i - 1) > MAX_EXP) {
147
+ x.c = x.e = null;
148
+ } else if (e < MIN_EXP) {
149
+ x.c = [x.e = 0];
150
+ } else {
151
+ x.e = e;
152
+ x.c = [];
153
+ i = (e + 1) % LOG_BASE;
154
+ if (e < 0) i += LOG_BASE;
155
+ if (i < len) {
156
+ if (i) x.c.push(+str.slice(0, i));
157
+ for (len -= LOG_BASE; i < len; ) {
158
+ x.c.push(+str.slice(i, i += LOG_BASE));
159
+ }
160
+ i = LOG_BASE - (str = str.slice(i)).length;
161
+ } else {
162
+ i -= len;
163
+ }
164
+ for (; i--; str += "0") ;
165
+ x.c.push(+str);
166
+ }
167
+ } else {
168
+ x.c = [x.e = 0];
169
+ }
170
+ }
171
+ BigNumber2.clone = clone;
172
+ BigNumber2.ROUND_UP = 0;
173
+ BigNumber2.ROUND_DOWN = 1;
174
+ BigNumber2.ROUND_CEIL = 2;
175
+ BigNumber2.ROUND_FLOOR = 3;
176
+ BigNumber2.ROUND_HALF_UP = 4;
177
+ BigNumber2.ROUND_HALF_DOWN = 5;
178
+ BigNumber2.ROUND_HALF_EVEN = 6;
179
+ BigNumber2.ROUND_HALF_CEIL = 7;
180
+ BigNumber2.ROUND_HALF_FLOOR = 8;
181
+ BigNumber2.EUCLID = 9;
182
+ BigNumber2.config = BigNumber2.set = function(obj) {
183
+ var p, v;
184
+ if (obj != null) {
185
+ if (typeof obj == "object") {
186
+ if (obj.hasOwnProperty(p = "DECIMAL_PLACES")) {
187
+ v = obj[p];
188
+ intCheck(v, 0, MAX, p);
189
+ DECIMAL_PLACES = v;
190
+ }
191
+ if (obj.hasOwnProperty(p = "ROUNDING_MODE")) {
192
+ v = obj[p];
193
+ intCheck(v, 0, 8, p);
194
+ ROUNDING_MODE = v;
195
+ }
196
+ if (obj.hasOwnProperty(p = "EXPONENTIAL_AT")) {
197
+ v = obj[p];
198
+ if (v && v.pop) {
199
+ intCheck(v[0], -MAX, 0, p);
200
+ intCheck(v[1], 0, MAX, p);
201
+ TO_EXP_NEG = v[0];
202
+ TO_EXP_POS = v[1];
203
+ } else {
204
+ intCheck(v, -MAX, MAX, p);
205
+ TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
206
+ }
207
+ }
208
+ if (obj.hasOwnProperty(p = "RANGE")) {
209
+ v = obj[p];
210
+ if (v && v.pop) {
211
+ intCheck(v[0], -MAX, -1, p);
212
+ intCheck(v[1], 1, MAX, p);
213
+ MIN_EXP = v[0];
214
+ MAX_EXP = v[1];
215
+ } else {
216
+ intCheck(v, -MAX, MAX, p);
217
+ if (v) {
218
+ MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
219
+ } else {
220
+ throw Error(bignumberError + p + " cannot be zero: " + v);
221
+ }
222
+ }
223
+ }
224
+ if (obj.hasOwnProperty(p = "CRYPTO")) {
225
+ v = obj[p];
226
+ if (v === !!v) {
227
+ if (v) {
228
+ if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
229
+ CRYPTO = v;
230
+ } else {
231
+ CRYPTO = !v;
232
+ throw Error(bignumberError + "crypto unavailable");
233
+ }
234
+ } else {
235
+ CRYPTO = v;
236
+ }
237
+ } else {
238
+ throw Error(bignumberError + p + " not true or false: " + v);
239
+ }
240
+ }
241
+ if (obj.hasOwnProperty(p = "MODULO_MODE")) {
242
+ v = obj[p];
243
+ intCheck(v, 0, 9, p);
244
+ MODULO_MODE = v;
245
+ }
246
+ if (obj.hasOwnProperty(p = "POW_PRECISION")) {
247
+ v = obj[p];
248
+ intCheck(v, 0, MAX, p);
249
+ POW_PRECISION = v;
250
+ }
251
+ if (obj.hasOwnProperty(p = "FORMAT")) {
252
+ v = obj[p];
253
+ if (typeof v == "object") FORMAT = v;
254
+ else throw Error(bignumberError + p + " not an object: " + v);
255
+ }
256
+ if (obj.hasOwnProperty(p = "ALPHABET")) {
257
+ v = obj[p];
258
+ if (typeof v == "string" && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
259
+ alphabetHasNormalDecimalDigits = v.slice(0, 10) == "0123456789";
260
+ ALPHABET = v;
261
+ } else {
262
+ throw Error(bignumberError + p + " invalid: " + v);
263
+ }
264
+ }
265
+ } else {
266
+ throw Error(bignumberError + "Object expected: " + obj);
267
+ }
268
+ }
269
+ return {
270
+ DECIMAL_PLACES,
271
+ ROUNDING_MODE,
272
+ EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
273
+ RANGE: [MIN_EXP, MAX_EXP],
274
+ CRYPTO,
275
+ MODULO_MODE,
276
+ POW_PRECISION,
277
+ FORMAT,
278
+ ALPHABET
279
+ };
280
+ };
281
+ BigNumber2.isBigNumber = function(v) {
282
+ if (!v || v._isBigNumber !== true) return false;
283
+ if (!BigNumber2.DEBUG) return true;
284
+ var i, n, c = v.c, e = v.e, s = v.s;
285
+ out: if ({}.toString.call(c) == "[object Array]") {
286
+ if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
287
+ if (c[0] === 0) {
288
+ if (e === 0 && c.length === 1) return true;
289
+ break out;
290
+ }
291
+ i = (e + 1) % LOG_BASE;
292
+ if (i < 1) i += LOG_BASE;
293
+ if (String(c[0]).length == i) {
294
+ for (i = 0; i < c.length; i++) {
295
+ n = c[i];
296
+ if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
297
+ }
298
+ if (n !== 0) return true;
299
+ }
300
+ }
301
+ } else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
302
+ return true;
303
+ }
304
+ throw Error(bignumberError + "Invalid BigNumber: " + v);
305
+ };
306
+ BigNumber2.maximum = BigNumber2.max = function() {
307
+ return maxOrMin(arguments, -1);
308
+ };
309
+ BigNumber2.minimum = BigNumber2.min = function() {
310
+ return maxOrMin(arguments, 1);
311
+ };
312
+ BigNumber2.random = function() {
313
+ var pow2_53 = 9007199254740992;
314
+ var random53bitInt = Math.random() * pow2_53 & 2097151 ? function() {
315
+ return mathfloor(Math.random() * pow2_53);
316
+ } : function() {
317
+ return (Math.random() * 1073741824 | 0) * 8388608 + (Math.random() * 8388608 | 0);
318
+ };
319
+ return function(dp) {
320
+ var a, b, e, k, v, i = 0, c = [], rand = new BigNumber2(ONE);
321
+ if (dp == null) dp = DECIMAL_PLACES;
322
+ else intCheck(dp, 0, MAX);
323
+ k = mathceil(dp / LOG_BASE);
324
+ if (CRYPTO) {
325
+ if (crypto.getRandomValues) {
326
+ a = crypto.getRandomValues(new Uint32Array(k *= 2));
327
+ for (; i < k; ) {
328
+ v = a[i] * 131072 + (a[i + 1] >>> 11);
329
+ if (v >= 9e15) {
330
+ b = crypto.getRandomValues(new Uint32Array(2));
331
+ a[i] = b[0];
332
+ a[i + 1] = b[1];
333
+ } else {
334
+ c.push(v % 1e14);
335
+ i += 2;
336
+ }
337
+ }
338
+ i = k / 2;
339
+ } else if (crypto.randomBytes) {
340
+ a = crypto.randomBytes(k *= 7);
341
+ for (; i < k; ) {
342
+ v = (a[i] & 31) * 281474976710656 + a[i + 1] * 1099511627776 + a[i + 2] * 4294967296 + a[i + 3] * 16777216 + (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
343
+ if (v >= 9e15) {
344
+ crypto.randomBytes(7).copy(a, i);
345
+ } else {
346
+ c.push(v % 1e14);
347
+ i += 7;
348
+ }
349
+ }
350
+ i = k / 7;
351
+ } else {
352
+ CRYPTO = false;
353
+ throw Error(bignumberError + "crypto unavailable");
354
+ }
355
+ }
356
+ if (!CRYPTO) {
357
+ for (; i < k; ) {
358
+ v = random53bitInt();
359
+ if (v < 9e15) c[i++] = v % 1e14;
360
+ }
361
+ }
362
+ k = c[--i];
363
+ dp %= LOG_BASE;
364
+ if (k && dp) {
365
+ v = POWS_TEN[LOG_BASE - dp];
366
+ c[i] = mathfloor(k / v) * v;
367
+ }
368
+ for (; c[i] === 0; c.pop(), i--) ;
369
+ if (i < 0) {
370
+ c = [e = 0];
371
+ } else {
372
+ for (e = -1; c[0] === 0; c.splice(0, 1), e -= LOG_BASE) ;
373
+ for (i = 1, v = c[0]; v >= 10; v /= 10, i++) ;
374
+ if (i < LOG_BASE) e -= LOG_BASE - i;
375
+ }
376
+ rand.e = e;
377
+ rand.c = c;
378
+ return rand;
379
+ };
380
+ }();
381
+ BigNumber2.sum = function() {
382
+ var i = 1, args = arguments, sum = new BigNumber2(args[0]);
383
+ for (; i < args.length; ) sum = sum.plus(args[i++]);
384
+ return sum;
385
+ };
386
+ convertBase = /* @__PURE__ */ function() {
387
+ var decimal = "0123456789";
388
+ function toBaseOut(str, baseIn, baseOut, alphabet) {
389
+ var j, arr = [0], arrL, i = 0, len = str.length;
390
+ for (; i < len; ) {
391
+ for (arrL = arr.length; arrL--; arr[arrL] *= baseIn) ;
392
+ arr[0] += alphabet.indexOf(str.charAt(i++));
393
+ for (j = 0; j < arr.length; j++) {
394
+ if (arr[j] > baseOut - 1) {
395
+ if (arr[j + 1] == null) arr[j + 1] = 0;
396
+ arr[j + 1] += arr[j] / baseOut | 0;
397
+ arr[j] %= baseOut;
398
+ }
399
+ }
400
+ }
401
+ return arr.reverse();
402
+ }
403
+ return function(str, baseIn, baseOut, sign, callerIsToString) {
404
+ var alphabet, d, e, k, r, x, xc, y, i = str.indexOf("."), dp = DECIMAL_PLACES, rm = ROUNDING_MODE;
405
+ if (i >= 0) {
406
+ k = POW_PRECISION;
407
+ POW_PRECISION = 0;
408
+ str = str.replace(".", "");
409
+ y = new BigNumber2(baseIn);
410
+ x = y.pow(str.length - i);
411
+ POW_PRECISION = k;
412
+ y.c = toBaseOut(
413
+ toFixedPoint(coeffToString(x.c), x.e, "0"),
414
+ 10,
415
+ baseOut,
416
+ decimal
417
+ );
418
+ y.e = y.c.length;
419
+ }
420
+ xc = toBaseOut(str, baseIn, baseOut, callerIsToString ? (alphabet = ALPHABET, decimal) : (alphabet = decimal, ALPHABET));
421
+ e = k = xc.length;
422
+ for (; xc[--k] == 0; xc.pop()) ;
423
+ if (!xc[0]) return alphabet.charAt(0);
424
+ if (i < 0) {
425
+ --e;
426
+ } else {
427
+ x.c = xc;
428
+ x.e = e;
429
+ x.s = sign;
430
+ x = div(x, y, dp, rm, baseOut);
431
+ xc = x.c;
432
+ r = x.r;
433
+ e = x.e;
434
+ }
435
+ d = e + dp + 1;
436
+ i = xc[d];
437
+ k = baseOut / 2;
438
+ r = r || d < 0 || xc[d + 1] != null;
439
+ r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : i > k || i == k && (rm == 4 || r || rm == 6 && xc[d - 1] & 1 || rm == (x.s < 0 ? 8 : 7));
440
+ if (d < 1 || !xc[0]) {
441
+ str = r ? toFixedPoint(alphabet.charAt(1), -dp, alphabet.charAt(0)) : alphabet.charAt(0);
442
+ } else {
443
+ xc.length = d;
444
+ if (r) {
445
+ for (--baseOut; ++xc[--d] > baseOut; ) {
446
+ xc[d] = 0;
447
+ if (!d) {
448
+ ++e;
449
+ xc = [1].concat(xc);
450
+ }
451
+ }
452
+ }
453
+ for (k = xc.length; !xc[--k]; ) ;
454
+ for (i = 0, str = ""; i <= k; str += alphabet.charAt(xc[i++])) ;
455
+ str = toFixedPoint(str, e, alphabet.charAt(0));
456
+ }
457
+ return str;
458
+ };
459
+ }();
460
+ div = /* @__PURE__ */ function() {
461
+ function multiply(x, k, base) {
462
+ var m, temp, xlo, xhi, carry = 0, i = x.length, klo = k % SQRT_BASE, khi = k / SQRT_BASE | 0;
463
+ for (x = x.slice(); i--; ) {
464
+ xlo = x[i] % SQRT_BASE;
465
+ xhi = x[i] / SQRT_BASE | 0;
466
+ m = khi * xlo + xhi * klo;
467
+ temp = klo * xlo + m % SQRT_BASE * SQRT_BASE + carry;
468
+ carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
469
+ x[i] = temp % base;
470
+ }
471
+ if (carry) x = [carry].concat(x);
472
+ return x;
473
+ }
474
+ function compare2(a, b, aL, bL) {
475
+ var i, cmp;
476
+ if (aL != bL) {
477
+ cmp = aL > bL ? 1 : -1;
478
+ } else {
479
+ for (i = cmp = 0; i < aL; i++) {
480
+ if (a[i] != b[i]) {
481
+ cmp = a[i] > b[i] ? 1 : -1;
482
+ break;
483
+ }
484
+ }
485
+ }
486
+ return cmp;
487
+ }
488
+ function subtract(a, b, aL, base) {
489
+ var i = 0;
490
+ for (; aL--; ) {
491
+ a[aL] -= i;
492
+ i = a[aL] < b[aL] ? 1 : 0;
493
+ a[aL] = i * base + a[aL] - b[aL];
494
+ }
495
+ for (; !a[0] && a.length > 1; a.splice(0, 1)) ;
496
+ }
497
+ return function(x, y, dp, rm, base) {
498
+ var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0, yL, yz, s = x.s == y.s ? 1 : -1, xc = x.c, yc = y.c;
499
+ if (!xc || !xc[0] || !yc || !yc[0]) {
500
+ return new BigNumber2(
501
+ // Return NaN if either NaN, or both Infinity or 0.
502
+ !x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN : (
503
+ // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
504
+ xc && xc[0] == 0 || !yc ? s * 0 : s / 0
505
+ )
506
+ );
507
+ }
508
+ q = new BigNumber2(s);
509
+ qc = q.c = [];
510
+ e = x.e - y.e;
511
+ s = dp + e + 1;
512
+ if (!base) {
513
+ base = BASE;
514
+ e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
515
+ s = s / LOG_BASE | 0;
516
+ }
517
+ for (i = 0; yc[i] == (xc[i] || 0); i++) ;
518
+ if (yc[i] > (xc[i] || 0)) e--;
519
+ if (s < 0) {
520
+ qc.push(1);
521
+ more = true;
522
+ } else {
523
+ xL = xc.length;
524
+ yL = yc.length;
525
+ i = 0;
526
+ s += 2;
527
+ n = mathfloor(base / (yc[0] + 1));
528
+ if (n > 1) {
529
+ yc = multiply(yc, n, base);
530
+ xc = multiply(xc, n, base);
531
+ yL = yc.length;
532
+ xL = xc.length;
533
+ }
534
+ xi = yL;
535
+ rem = xc.slice(0, yL);
536
+ remL = rem.length;
537
+ for (; remL < yL; rem[remL++] = 0) ;
538
+ yz = yc.slice();
539
+ yz = [0].concat(yz);
540
+ yc0 = yc[0];
541
+ if (yc[1] >= base / 2) yc0++;
542
+ do {
543
+ n = 0;
544
+ cmp = compare2(yc, rem, yL, remL);
545
+ if (cmp < 0) {
546
+ rem0 = rem[0];
547
+ if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
548
+ n = mathfloor(rem0 / yc0);
549
+ if (n > 1) {
550
+ if (n >= base) n = base - 1;
551
+ prod = multiply(yc, n, base);
552
+ prodL = prod.length;
553
+ remL = rem.length;
554
+ while (compare2(prod, rem, prodL, remL) == 1) {
555
+ n--;
556
+ subtract(prod, yL < prodL ? yz : yc, prodL, base);
557
+ prodL = prod.length;
558
+ cmp = 1;
559
+ }
560
+ } else {
561
+ if (n == 0) {
562
+ cmp = n = 1;
563
+ }
564
+ prod = yc.slice();
565
+ prodL = prod.length;
566
+ }
567
+ if (prodL < remL) prod = [0].concat(prod);
568
+ subtract(rem, prod, remL, base);
569
+ remL = rem.length;
570
+ if (cmp == -1) {
571
+ while (compare2(yc, rem, yL, remL) < 1) {
572
+ n++;
573
+ subtract(rem, yL < remL ? yz : yc, remL, base);
574
+ remL = rem.length;
575
+ }
576
+ }
577
+ } else if (cmp === 0) {
578
+ n++;
579
+ rem = [0];
580
+ }
581
+ qc[i++] = n;
582
+ if (rem[0]) {
583
+ rem[remL++] = xc[xi] || 0;
584
+ } else {
585
+ rem = [xc[xi]];
586
+ remL = 1;
587
+ }
588
+ } while ((xi++ < xL || rem[0] != null) && s--);
589
+ more = rem[0] != null;
590
+ if (!qc[0]) qc.splice(0, 1);
591
+ }
592
+ if (base == BASE) {
593
+ for (i = 1, s = qc[0]; s >= 10; s /= 10, i++) ;
594
+ round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more);
595
+ } else {
596
+ q.e = e;
597
+ q.r = +more;
598
+ }
599
+ return q;
600
+ };
601
+ }();
602
+ function format(n, i, rm, id) {
603
+ var c0, e, ne, len, str;
604
+ if (rm == null) rm = ROUNDING_MODE;
605
+ else intCheck(rm, 0, 8);
606
+ if (!n.c) return n.toString();
607
+ c0 = n.c[0];
608
+ ne = n.e;
609
+ if (i == null) {
610
+ str = coeffToString(n.c);
611
+ str = id == 1 || id == 2 && (ne <= TO_EXP_NEG || ne >= TO_EXP_POS) ? toExponential(str, ne) : toFixedPoint(str, ne, "0");
612
+ } else {
613
+ n = round(new BigNumber2(n), i, rm);
614
+ e = n.e;
615
+ str = coeffToString(n.c);
616
+ len = str.length;
617
+ if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
618
+ for (; len < i; str += "0", len++) ;
619
+ str = toExponential(str, e);
620
+ } else {
621
+ i -= ne;
622
+ str = toFixedPoint(str, e, "0");
623
+ if (e + 1 > len) {
624
+ if (--i > 0) for (str += "."; i--; str += "0") ;
625
+ } else {
626
+ i += e - len;
627
+ if (i > 0) {
628
+ if (e + 1 == len) str += ".";
629
+ for (; i--; str += "0") ;
630
+ }
631
+ }
632
+ }
633
+ }
634
+ return n.s < 0 && c0 ? "-" + str : str;
635
+ }
636
+ function maxOrMin(args, n) {
637
+ var k, y, i = 1, x = new BigNumber2(args[0]);
638
+ for (; i < args.length; i++) {
639
+ y = new BigNumber2(args[i]);
640
+ if (!y.s || (k = compare(x, y)) === n || k === 0 && x.s === n) {
641
+ x = y;
642
+ }
643
+ }
644
+ return x;
645
+ }
646
+ function normalise(n, c, e) {
647
+ var i = 1, j = c.length;
648
+ for (; !c[--j]; c.pop()) ;
649
+ for (j = c[0]; j >= 10; j /= 10, i++) ;
650
+ if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
651
+ n.c = n.e = null;
652
+ } else if (e < MIN_EXP) {
653
+ n.c = [n.e = 0];
654
+ } else {
655
+ n.e = e;
656
+ n.c = c;
657
+ }
658
+ return n;
659
+ }
660
+ parseNumeric = /* @__PURE__ */ function() {
661
+ var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, dotAfter = /^([^.]+)\.$/, dotBefore = /^\.([^.]+)$/, isInfinityOrNaN = /^-?(Infinity|NaN)$/, whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
662
+ return function(x, str, isNum, b) {
663
+ var base, s = isNum ? str : str.replace(whitespaceOrPlus, "");
664
+ if (isInfinityOrNaN.test(s)) {
665
+ x.s = isNaN(s) ? null : s < 0 ? -1 : 1;
666
+ } else {
667
+ if (!isNum) {
668
+ s = s.replace(basePrefix, function(m, p1, p2) {
669
+ base = (p2 = p2.toLowerCase()) == "x" ? 16 : p2 == "b" ? 2 : 8;
670
+ return !b || b == base ? p1 : m;
671
+ });
672
+ if (b) {
673
+ base = b;
674
+ s = s.replace(dotAfter, "$1").replace(dotBefore, "0.$1");
675
+ }
676
+ if (str != s) return new BigNumber2(s, base);
677
+ }
678
+ if (BigNumber2.DEBUG) {
679
+ throw Error(bignumberError + "Not a" + (b ? " base " + b : "") + " number: " + str);
680
+ }
681
+ x.s = null;
682
+ }
683
+ x.c = x.e = null;
684
+ };
685
+ }();
686
+ function round(x, sd, rm, r) {
687
+ var d, i, j, k, n, ni, rd, xc = x.c, pows10 = POWS_TEN;
688
+ if (xc) {
689
+ out: {
690
+ for (d = 1, k = xc[0]; k >= 10; k /= 10, d++) ;
691
+ i = sd - d;
692
+ if (i < 0) {
693
+ i += LOG_BASE;
694
+ j = sd;
695
+ n = xc[ni = 0];
696
+ rd = mathfloor(n / pows10[d - j - 1] % 10);
697
+ } else {
698
+ ni = mathceil((i + 1) / LOG_BASE);
699
+ if (ni >= xc.length) {
700
+ if (r) {
701
+ for (; xc.length <= ni; xc.push(0)) ;
702
+ n = rd = 0;
703
+ d = 1;
704
+ i %= LOG_BASE;
705
+ j = i - LOG_BASE + 1;
706
+ } else {
707
+ break out;
708
+ }
709
+ } else {
710
+ n = k = xc[ni];
711
+ for (d = 1; k >= 10; k /= 10, d++) ;
712
+ i %= LOG_BASE;
713
+ j = i - LOG_BASE + d;
714
+ rd = j < 0 ? 0 : mathfloor(n / pows10[d - j - 1] % 10);
715
+ }
716
+ }
717
+ r = r || sd < 0 || // Are there any non-zero digits after the rounding digit?
718
+ // The expression n % pows10[d - j - 1] returns all digits of n to the right
719
+ // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
720
+ xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
721
+ r = rm < 4 ? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 && // Check whether the digit to the left of the rounding digit is odd.
722
+ (i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10 & 1 || rm == (x.s < 0 ? 8 : 7));
723
+ if (sd < 1 || !xc[0]) {
724
+ xc.length = 0;
725
+ if (r) {
726
+ sd -= x.e + 1;
727
+ xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
728
+ x.e = -sd || 0;
729
+ } else {
730
+ xc[0] = x.e = 0;
731
+ }
732
+ return x;
733
+ }
734
+ if (i == 0) {
735
+ xc.length = ni;
736
+ k = 1;
737
+ ni--;
738
+ } else {
739
+ xc.length = ni + 1;
740
+ k = pows10[LOG_BASE - i];
741
+ xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0;
742
+ }
743
+ if (r) {
744
+ for (; ; ) {
745
+ if (ni == 0) {
746
+ for (i = 1, j = xc[0]; j >= 10; j /= 10, i++) ;
747
+ j = xc[0] += k;
748
+ for (k = 1; j >= 10; j /= 10, k++) ;
749
+ if (i != k) {
750
+ x.e++;
751
+ if (xc[0] == BASE) xc[0] = 1;
752
+ }
753
+ break;
754
+ } else {
755
+ xc[ni] += k;
756
+ if (xc[ni] != BASE) break;
757
+ xc[ni--] = 0;
758
+ k = 1;
759
+ }
760
+ }
761
+ }
762
+ for (i = xc.length; xc[--i] === 0; xc.pop()) ;
763
+ }
764
+ if (x.e > MAX_EXP) {
765
+ x.c = x.e = null;
766
+ } else if (x.e < MIN_EXP) {
767
+ x.c = [x.e = 0];
768
+ }
769
+ }
770
+ return x;
771
+ }
772
+ function valueOf(n) {
773
+ var str, e = n.e;
774
+ if (e === null) return n.toString();
775
+ str = coeffToString(n.c);
776
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(str, e) : toFixedPoint(str, e, "0");
777
+ return n.s < 0 ? "-" + str : str;
778
+ }
779
+ P.absoluteValue = P.abs = function() {
780
+ var x = new BigNumber2(this);
781
+ if (x.s < 0) x.s = 1;
782
+ return x;
783
+ };
784
+ P.comparedTo = function(y, b) {
785
+ return compare(this, new BigNumber2(y, b));
786
+ };
787
+ P.decimalPlaces = P.dp = function(dp, rm) {
788
+ var c, n, v, x = this;
789
+ if (dp != null) {
790
+ intCheck(dp, 0, MAX);
791
+ if (rm == null) rm = ROUNDING_MODE;
792
+ else intCheck(rm, 0, 8);
793
+ return round(new BigNumber2(x), dp + x.e + 1, rm);
794
+ }
795
+ if (!(c = x.c)) return null;
796
+ n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
797
+ if (v = c[v]) for (; v % 10 == 0; v /= 10, n--) ;
798
+ if (n < 0) n = 0;
799
+ return n;
800
+ };
801
+ P.dividedBy = P.div = function(y, b) {
802
+ return div(this, new BigNumber2(y, b), DECIMAL_PLACES, ROUNDING_MODE);
803
+ };
804
+ P.dividedToIntegerBy = P.idiv = function(y, b) {
805
+ return div(this, new BigNumber2(y, b), 0, 1);
806
+ };
807
+ P.exponentiatedBy = P.pow = function(n, m) {
808
+ var half, isModExp, i, k, more, nIsBig, nIsNeg, nIsOdd, y, x = this;
809
+ n = new BigNumber2(n);
810
+ if (n.c && !n.isInteger()) {
811
+ throw Error(bignumberError + "Exponent not an integer: " + valueOf(n));
812
+ }
813
+ if (m != null) m = new BigNumber2(m);
814
+ nIsBig = n.e > 14;
815
+ if (!x.c || !x.c[0] || x.c[0] == 1 && !x.e && x.c.length == 1 || !n.c || !n.c[0]) {
816
+ y = new BigNumber2(Math.pow(+valueOf(x), nIsBig ? n.s * (2 - isOdd(n)) : +valueOf(n)));
817
+ return m ? y.mod(m) : y;
818
+ }
819
+ nIsNeg = n.s < 0;
820
+ if (m) {
821
+ if (m.c ? !m.c[0] : !m.s) return new BigNumber2(NaN);
822
+ isModExp = !nIsNeg && x.isInteger() && m.isInteger();
823
+ if (isModExp) x = x.mod(m);
824
+ } else if (n.e > 9 && (x.e > 0 || x.e < -1 || (x.e == 0 ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7 : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) {
825
+ k = x.s < 0 && isOdd(n) ? -0 : 0;
826
+ if (x.e > -1) k = 1 / k;
827
+ return new BigNumber2(nIsNeg ? 1 / k : k);
828
+ } else if (POW_PRECISION) {
829
+ k = mathceil(POW_PRECISION / LOG_BASE + 2);
830
+ }
831
+ if (nIsBig) {
832
+ half = new BigNumber2(0.5);
833
+ if (nIsNeg) n.s = 1;
834
+ nIsOdd = isOdd(n);
835
+ } else {
836
+ i = Math.abs(+valueOf(n));
837
+ nIsOdd = i % 2;
838
+ }
839
+ y = new BigNumber2(ONE);
840
+ for (; ; ) {
841
+ if (nIsOdd) {
842
+ y = y.times(x);
843
+ if (!y.c) break;
844
+ if (k) {
845
+ if (y.c.length > k) y.c.length = k;
846
+ } else if (isModExp) {
847
+ y = y.mod(m);
848
+ }
849
+ }
850
+ if (i) {
851
+ i = mathfloor(i / 2);
852
+ if (i === 0) break;
853
+ nIsOdd = i % 2;
854
+ } else {
855
+ n = n.times(half);
856
+ round(n, n.e + 1, 1);
857
+ if (n.e > 14) {
858
+ nIsOdd = isOdd(n);
859
+ } else {
860
+ i = +valueOf(n);
861
+ if (i === 0) break;
862
+ nIsOdd = i % 2;
863
+ }
864
+ }
865
+ x = x.times(x);
866
+ if (k) {
867
+ if (x.c && x.c.length > k) x.c.length = k;
868
+ } else if (isModExp) {
869
+ x = x.mod(m);
870
+ }
871
+ }
872
+ if (isModExp) return y;
873
+ if (nIsNeg) y = ONE.div(y);
874
+ return m ? y.mod(m) : k ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
875
+ };
876
+ P.integerValue = function(rm) {
877
+ var n = new BigNumber2(this);
878
+ if (rm == null) rm = ROUNDING_MODE;
879
+ else intCheck(rm, 0, 8);
880
+ return round(n, n.e + 1, rm);
881
+ };
882
+ P.isEqualTo = P.eq = function(y, b) {
883
+ return compare(this, new BigNumber2(y, b)) === 0;
884
+ };
885
+ P.isFinite = function() {
886
+ return !!this.c;
887
+ };
888
+ P.isGreaterThan = P.gt = function(y, b) {
889
+ return compare(this, new BigNumber2(y, b)) > 0;
890
+ };
891
+ P.isGreaterThanOrEqualTo = P.gte = function(y, b) {
892
+ return (b = compare(this, new BigNumber2(y, b))) === 1 || b === 0;
893
+ };
894
+ P.isInteger = function() {
895
+ return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
896
+ };
897
+ P.isLessThan = P.lt = function(y, b) {
898
+ return compare(this, new BigNumber2(y, b)) < 0;
899
+ };
900
+ P.isLessThanOrEqualTo = P.lte = function(y, b) {
901
+ return (b = compare(this, new BigNumber2(y, b))) === -1 || b === 0;
902
+ };
903
+ P.isNaN = function() {
904
+ return !this.s;
905
+ };
906
+ P.isNegative = function() {
907
+ return this.s < 0;
908
+ };
909
+ P.isPositive = function() {
910
+ return this.s > 0;
911
+ };
912
+ P.isZero = function() {
913
+ return !!this.c && this.c[0] == 0;
914
+ };
915
+ P.minus = function(y, b) {
916
+ var i, j, t, xLTy, x = this, a = x.s;
917
+ y = new BigNumber2(y, b);
918
+ b = y.s;
919
+ if (!a || !b) return new BigNumber2(NaN);
920
+ if (a != b) {
921
+ y.s = -b;
922
+ return x.plus(y);
923
+ }
924
+ var xe = x.e / LOG_BASE, ye = y.e / LOG_BASE, xc = x.c, yc = y.c;
925
+ if (!xe || !ye) {
926
+ if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber2(yc ? x : NaN);
927
+ if (!xc[0] || !yc[0]) {
928
+ return yc[0] ? (y.s = -b, y) : new BigNumber2(xc[0] ? x : (
929
+ // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
930
+ ROUNDING_MODE == 3 ? -0 : 0
931
+ ));
932
+ }
933
+ }
934
+ xe = bitFloor(xe);
935
+ ye = bitFloor(ye);
936
+ xc = xc.slice();
937
+ if (a = xe - ye) {
938
+ if (xLTy = a < 0) {
939
+ a = -a;
940
+ t = xc;
941
+ } else {
942
+ ye = xe;
943
+ t = yc;
944
+ }
945
+ t.reverse();
946
+ for (b = a; b--; t.push(0)) ;
947
+ t.reverse();
948
+ } else {
949
+ j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b;
950
+ for (a = b = 0; b < j; b++) {
951
+ if (xc[b] != yc[b]) {
952
+ xLTy = xc[b] < yc[b];
953
+ break;
954
+ }
955
+ }
956
+ }
957
+ if (xLTy) {
958
+ t = xc;
959
+ xc = yc;
960
+ yc = t;
961
+ y.s = -y.s;
962
+ }
963
+ b = (j = yc.length) - (i = xc.length);
964
+ if (b > 0) for (; b--; xc[i++] = 0) ;
965
+ b = BASE - 1;
966
+ for (; j > a; ) {
967
+ if (xc[--j] < yc[j]) {
968
+ for (i = j; i && !xc[--i]; xc[i] = b) ;
969
+ --xc[i];
970
+ xc[j] += BASE;
971
+ }
972
+ xc[j] -= yc[j];
973
+ }
974
+ for (; xc[0] == 0; xc.splice(0, 1), --ye) ;
975
+ if (!xc[0]) {
976
+ y.s = ROUNDING_MODE == 3 ? -1 : 1;
977
+ y.c = [y.e = 0];
978
+ return y;
979
+ }
980
+ return normalise(y, xc, ye);
981
+ };
982
+ P.modulo = P.mod = function(y, b) {
983
+ var q, s, x = this;
984
+ y = new BigNumber2(y, b);
985
+ if (!x.c || !y.s || y.c && !y.c[0]) {
986
+ return new BigNumber2(NaN);
987
+ } else if (!y.c || x.c && !x.c[0]) {
988
+ return new BigNumber2(x);
989
+ }
990
+ if (MODULO_MODE == 9) {
991
+ s = y.s;
992
+ y.s = 1;
993
+ q = div(x, y, 0, 3);
994
+ y.s = s;
995
+ q.s *= s;
996
+ } else {
997
+ q = div(x, y, 0, MODULO_MODE);
998
+ }
999
+ y = x.minus(q.times(y));
1000
+ if (!y.c[0] && MODULO_MODE == 1) y.s = x.s;
1001
+ return y;
1002
+ };
1003
+ P.multipliedBy = P.times = function(y, b) {
1004
+ var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, base, sqrtBase, x = this, xc = x.c, yc = (y = new BigNumber2(y, b)).c;
1005
+ if (!xc || !yc || !xc[0] || !yc[0]) {
1006
+ if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
1007
+ y.c = y.e = y.s = null;
1008
+ } else {
1009
+ y.s *= x.s;
1010
+ if (!xc || !yc) {
1011
+ y.c = y.e = null;
1012
+ } else {
1013
+ y.c = [0];
1014
+ y.e = 0;
1015
+ }
1016
+ }
1017
+ return y;
1018
+ }
1019
+ e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
1020
+ y.s *= x.s;
1021
+ xcL = xc.length;
1022
+ ycL = yc.length;
1023
+ if (xcL < ycL) {
1024
+ zc = xc;
1025
+ xc = yc;
1026
+ yc = zc;
1027
+ i = xcL;
1028
+ xcL = ycL;
1029
+ ycL = i;
1030
+ }
1031
+ for (i = xcL + ycL, zc = []; i--; zc.push(0)) ;
1032
+ base = BASE;
1033
+ sqrtBase = SQRT_BASE;
1034
+ for (i = ycL; --i >= 0; ) {
1035
+ c = 0;
1036
+ ylo = yc[i] % sqrtBase;
1037
+ yhi = yc[i] / sqrtBase | 0;
1038
+ for (k = xcL, j = i + k; j > i; ) {
1039
+ xlo = xc[--k] % sqrtBase;
1040
+ xhi = xc[k] / sqrtBase | 0;
1041
+ m = yhi * xlo + xhi * ylo;
1042
+ xlo = ylo * xlo + m % sqrtBase * sqrtBase + zc[j] + c;
1043
+ c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
1044
+ zc[j--] = xlo % base;
1045
+ }
1046
+ zc[j] = c;
1047
+ }
1048
+ if (c) {
1049
+ ++e;
1050
+ } else {
1051
+ zc.splice(0, 1);
1052
+ }
1053
+ return normalise(y, zc, e);
1054
+ };
1055
+ P.negated = function() {
1056
+ var x = new BigNumber2(this);
1057
+ x.s = -x.s || null;
1058
+ return x;
1059
+ };
1060
+ P.plus = function(y, b) {
1061
+ var t, x = this, a = x.s;
1062
+ y = new BigNumber2(y, b);
1063
+ b = y.s;
1064
+ if (!a || !b) return new BigNumber2(NaN);
1065
+ if (a != b) {
1066
+ y.s = -b;
1067
+ return x.minus(y);
1068
+ }
1069
+ var xe = x.e / LOG_BASE, ye = y.e / LOG_BASE, xc = x.c, yc = y.c;
1070
+ if (!xe || !ye) {
1071
+ if (!xc || !yc) return new BigNumber2(a / 0);
1072
+ if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber2(xc[0] ? x : a * 0);
1073
+ }
1074
+ xe = bitFloor(xe);
1075
+ ye = bitFloor(ye);
1076
+ xc = xc.slice();
1077
+ if (a = xe - ye) {
1078
+ if (a > 0) {
1079
+ ye = xe;
1080
+ t = yc;
1081
+ } else {
1082
+ a = -a;
1083
+ t = xc;
1084
+ }
1085
+ t.reverse();
1086
+ for (; a--; t.push(0)) ;
1087
+ t.reverse();
1088
+ }
1089
+ a = xc.length;
1090
+ b = yc.length;
1091
+ if (a - b < 0) {
1092
+ t = yc;
1093
+ yc = xc;
1094
+ xc = t;
1095
+ b = a;
1096
+ }
1097
+ for (a = 0; b; ) {
1098
+ a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0;
1099
+ xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE;
1100
+ }
1101
+ if (a) {
1102
+ xc = [a].concat(xc);
1103
+ ++ye;
1104
+ }
1105
+ return normalise(y, xc, ye);
1106
+ };
1107
+ P.precision = P.sd = function(sd, rm) {
1108
+ var c, n, v, x = this;
1109
+ if (sd != null && sd !== !!sd) {
1110
+ intCheck(sd, 1, MAX);
1111
+ if (rm == null) rm = ROUNDING_MODE;
1112
+ else intCheck(rm, 0, 8);
1113
+ return round(new BigNumber2(x), sd, rm);
1114
+ }
1115
+ if (!(c = x.c)) return null;
1116
+ v = c.length - 1;
1117
+ n = v * LOG_BASE + 1;
1118
+ if (v = c[v]) {
1119
+ for (; v % 10 == 0; v /= 10, n--) ;
1120
+ for (v = c[0]; v >= 10; v /= 10, n++) ;
1121
+ }
1122
+ if (sd && x.e + 1 > n) n = x.e + 1;
1123
+ return n;
1124
+ };
1125
+ P.shiftedBy = function(k) {
1126
+ intCheck(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
1127
+ return this.times("1e" + k);
1128
+ };
1129
+ P.squareRoot = P.sqrt = function() {
1130
+ var m, n, r, rep, t, x = this, c = x.c, s = x.s, e = x.e, dp = DECIMAL_PLACES + 4, half = new BigNumber2("0.5");
1131
+ if (s !== 1 || !c || !c[0]) {
1132
+ return new BigNumber2(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0);
1133
+ }
1134
+ s = Math.sqrt(+valueOf(x));
1135
+ if (s == 0 || s == 1 / 0) {
1136
+ n = coeffToString(c);
1137
+ if ((n.length + e) % 2 == 0) n += "0";
1138
+ s = Math.sqrt(+n);
1139
+ e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
1140
+ if (s == 1 / 0) {
1141
+ n = "5e" + e;
1142
+ } else {
1143
+ n = s.toExponential();
1144
+ n = n.slice(0, n.indexOf("e") + 1) + e;
1145
+ }
1146
+ r = new BigNumber2(n);
1147
+ } else {
1148
+ r = new BigNumber2(s + "");
1149
+ }
1150
+ if (r.c[0]) {
1151
+ e = r.e;
1152
+ s = e + dp;
1153
+ if (s < 3) s = 0;
1154
+ for (; ; ) {
1155
+ t = r;
1156
+ r = half.times(t.plus(div(x, t, dp, 1)));
1157
+ if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
1158
+ if (r.e < e) --s;
1159
+ n = n.slice(s - 3, s + 1);
1160
+ if (n == "9999" || !rep && n == "4999") {
1161
+ if (!rep) {
1162
+ round(t, t.e + DECIMAL_PLACES + 2, 0);
1163
+ if (t.times(t).eq(x)) {
1164
+ r = t;
1165
+ break;
1166
+ }
1167
+ }
1168
+ dp += 4;
1169
+ s += 4;
1170
+ rep = 1;
1171
+ } else {
1172
+ if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
1173
+ round(r, r.e + DECIMAL_PLACES + 2, 1);
1174
+ m = !r.times(r).eq(x);
1175
+ }
1176
+ break;
1177
+ }
1178
+ }
1179
+ }
1180
+ }
1181
+ return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
1182
+ };
1183
+ P.toExponential = function(dp, rm) {
1184
+ if (dp != null) {
1185
+ intCheck(dp, 0, MAX);
1186
+ dp++;
1187
+ }
1188
+ return format(this, dp, rm, 1);
1189
+ };
1190
+ P.toFixed = function(dp, rm) {
1191
+ if (dp != null) {
1192
+ intCheck(dp, 0, MAX);
1193
+ dp = dp + this.e + 1;
1194
+ }
1195
+ return format(this, dp, rm);
1196
+ };
1197
+ P.toFormat = function(dp, rm, format2) {
1198
+ var str, x = this;
1199
+ if (format2 == null) {
1200
+ if (dp != null && rm && typeof rm == "object") {
1201
+ format2 = rm;
1202
+ rm = null;
1203
+ } else if (dp && typeof dp == "object") {
1204
+ format2 = dp;
1205
+ dp = rm = null;
1206
+ } else {
1207
+ format2 = FORMAT;
1208
+ }
1209
+ } else if (typeof format2 != "object") {
1210
+ throw Error(bignumberError + "Argument not an object: " + format2);
1211
+ }
1212
+ str = x.toFixed(dp, rm);
1213
+ if (x.c) {
1214
+ var i, arr = str.split("."), g1 = +format2.groupSize, g2 = +format2.secondaryGroupSize, groupSeparator = format2.groupSeparator || "", intPart = arr[0], fractionPart = arr[1], isNeg = x.s < 0, intDigits = isNeg ? intPart.slice(1) : intPart, len = intDigits.length;
1215
+ if (g2) {
1216
+ i = g1;
1217
+ g1 = g2;
1218
+ g2 = i;
1219
+ len -= i;
1220
+ }
1221
+ if (g1 > 0 && len > 0) {
1222
+ i = len % g1 || g1;
1223
+ intPart = intDigits.substr(0, i);
1224
+ for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
1225
+ if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
1226
+ if (isNeg) intPart = "-" + intPart;
1227
+ }
1228
+ str = fractionPart ? intPart + (format2.decimalSeparator || "") + ((g2 = +format2.fractionGroupSize) ? fractionPart.replace(
1229
+ new RegExp("\\d{" + g2 + "}\\B", "g"),
1230
+ "$&" + (format2.fractionGroupSeparator || "")
1231
+ ) : fractionPart) : intPart;
1232
+ }
1233
+ return (format2.prefix || "") + str + (format2.suffix || "");
1234
+ };
1235
+ P.toFraction = function(md) {
1236
+ var d, d0, d1, d2, e, exp, n, n0, n1, q, r, s, x = this, xc = x.c;
1237
+ if (md != null) {
1238
+ n = new BigNumber2(md);
1239
+ if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
1240
+ throw Error(bignumberError + "Argument " + (n.isInteger() ? "out of range: " : "not an integer: ") + valueOf(n));
1241
+ }
1242
+ }
1243
+ if (!xc) return new BigNumber2(x);
1244
+ d = new BigNumber2(ONE);
1245
+ n1 = d0 = new BigNumber2(ONE);
1246
+ d1 = n0 = new BigNumber2(ONE);
1247
+ s = coeffToString(xc);
1248
+ e = d.e = s.length - x.e - 1;
1249
+ d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
1250
+ md = !md || n.comparedTo(d) > 0 ? e > 0 ? d : n1 : n;
1251
+ exp = MAX_EXP;
1252
+ MAX_EXP = 1 / 0;
1253
+ n = new BigNumber2(s);
1254
+ n0.c[0] = 0;
1255
+ for (; ; ) {
1256
+ q = div(n, d, 0, 1);
1257
+ d2 = d0.plus(q.times(d1));
1258
+ if (d2.comparedTo(md) == 1) break;
1259
+ d0 = d1;
1260
+ d1 = d2;
1261
+ n1 = n0.plus(q.times(d2 = n1));
1262
+ n0 = d2;
1263
+ d = n.minus(q.times(d2 = d));
1264
+ n = d2;
1265
+ }
1266
+ d2 = div(md.minus(d0), d1, 0, 1);
1267
+ n0 = n0.plus(d2.times(n1));
1268
+ d0 = d0.plus(d2.times(d1));
1269
+ n0.s = n1.s = x.s;
1270
+ e = e * 2;
1271
+ r = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().comparedTo(
1272
+ div(n0, d0, e, ROUNDING_MODE).minus(x).abs()
1273
+ ) < 1 ? [n1, d1] : [n0, d0];
1274
+ MAX_EXP = exp;
1275
+ return r;
1276
+ };
1277
+ P.toNumber = function() {
1278
+ return +valueOf(this);
1279
+ };
1280
+ P.toPrecision = function(sd, rm) {
1281
+ if (sd != null) intCheck(sd, 1, MAX);
1282
+ return format(this, sd, rm, 2);
1283
+ };
1284
+ P.toString = function(b) {
1285
+ var str, n = this, s = n.s, e = n.e;
1286
+ if (e === null) {
1287
+ if (s) {
1288
+ str = "Infinity";
1289
+ if (s < 0) str = "-" + str;
1290
+ } else {
1291
+ str = "NaN";
1292
+ }
1293
+ } else {
1294
+ if (b == null) {
1295
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(coeffToString(n.c), e) : toFixedPoint(coeffToString(n.c), e, "0");
1296
+ } else if (b === 10 && alphabetHasNormalDecimalDigits) {
1297
+ n = round(new BigNumber2(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
1298
+ str = toFixedPoint(coeffToString(n.c), n.e, "0");
1299
+ } else {
1300
+ intCheck(b, 2, ALPHABET.length, "Base");
1301
+ str = convertBase(toFixedPoint(coeffToString(n.c), e, "0"), 10, b, s, true);
1302
+ }
1303
+ if (s < 0 && n.c[0]) str = "-" + str;
1304
+ }
1305
+ return str;
1306
+ };
1307
+ P.valueOf = P.toJSON = function() {
1308
+ return valueOf(this);
1309
+ };
1310
+ P._isBigNumber = true;
1311
+ P[Symbol.toStringTag] = "BigNumber";
1312
+ P[Symbol.for("nodejs.util.inspect.custom")] = P.valueOf;
1313
+ if (configObject != null) BigNumber2.set(configObject);
1314
+ return BigNumber2;
1315
+ }
1316
+ function bitFloor(n) {
1317
+ var i = n | 0;
1318
+ return n > 0 || n === i ? i : i - 1;
1319
+ }
1320
+ function coeffToString(a) {
1321
+ var s, z, i = 1, j = a.length, r = a[0] + "";
1322
+ for (; i < j; ) {
1323
+ s = a[i++] + "";
1324
+ z = LOG_BASE - s.length;
1325
+ for (; z--; s = "0" + s) ;
1326
+ r += s;
1327
+ }
1328
+ for (j = r.length; r.charCodeAt(--j) === 48; ) ;
1329
+ return r.slice(0, j + 1 || 1);
1330
+ }
1331
+ function compare(x, y) {
1332
+ var a, b, xc = x.c, yc = y.c, i = x.s, j = y.s, k = x.e, l = y.e;
1333
+ if (!i || !j) return null;
1334
+ a = xc && !xc[0];
1335
+ b = yc && !yc[0];
1336
+ if (a || b) return a ? b ? 0 : -j : i;
1337
+ if (i != j) return i;
1338
+ a = i < 0;
1339
+ b = k == l;
1340
+ if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1;
1341
+ if (!b) return k > l ^ a ? 1 : -1;
1342
+ j = (k = xc.length) < (l = yc.length) ? k : l;
1343
+ for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
1344
+ return k == l ? 0 : k > l ^ a ? 1 : -1;
1345
+ }
1346
+ function intCheck(n, min, max, name) {
1347
+ if (n < min || n > max || n !== mathfloor(n)) {
1348
+ throw Error(bignumberError + (name || "Argument") + (typeof n == "number" ? n < min || n > max ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(n));
1349
+ }
1350
+ }
1351
+ function isOdd(n) {
1352
+ var k = n.c.length - 1;
1353
+ return bitFloor(n.e / LOG_BASE) == k && n.c[k] % 2 != 0;
1354
+ }
1355
+ function toExponential(str, e) {
1356
+ return (str.length > 1 ? str.charAt(0) + "." + str.slice(1) : str) + (e < 0 ? "e" : "e+") + e;
1357
+ }
1358
+ function toFixedPoint(str, e, z) {
1359
+ var len, zs;
1360
+ if (e < 0) {
1361
+ for (zs = z + "."; ++e; zs += z) ;
1362
+ str = zs + str;
1363
+ } else {
1364
+ len = str.length;
1365
+ if (++e > len) {
1366
+ for (zs = z, e -= len; --e; zs += z) ;
1367
+ str += zs;
1368
+ } else if (e < len) {
1369
+ str = str.slice(0, e) + "." + str.slice(e);
1370
+ }
1371
+ }
1372
+ return str;
1373
+ }
1374
+ var BigNumber = clone();
1375
+
1376
+ // src/chainflip.ts
1377
+ var chainflipAssets = [
1378
+ "Usdc",
1379
+ "Usdt",
1380
+ "Flip",
1381
+ "Dot",
1382
+ "Eth",
1383
+ "Btc",
1384
+ "ArbUsdc",
1385
+ "ArbEth",
1386
+ "Sol",
1387
+ "SolUsdc"
1388
+ ];
1389
+ var baseChainflipAssets = chainflipAssets.filter(
1390
+ (asset) => asset !== "Usdc"
1391
+ );
1392
+ var chainflipEvmChains = ["Ethereum", "Arbitrum"];
1393
+ var chainflipChains = [...chainflipEvmChains, "Bitcoin", "Polkadot", "Solana"];
1394
+ var assetConstants = {
1395
+ Eth: {
1396
+ chain: "Ethereum",
1397
+ rpcAsset: "ETH",
1398
+ decimals: 18
1399
+ },
1400
+ Flip: {
1401
+ chain: "Ethereum",
1402
+ rpcAsset: "FLIP",
1403
+ decimals: 18
1404
+ },
1405
+ Usdc: {
1406
+ chain: "Ethereum",
1407
+ rpcAsset: "USDC",
1408
+ decimals: 6
1409
+ },
1410
+ Usdt: {
1411
+ chain: "Ethereum",
1412
+ rpcAsset: "USDT",
1413
+ decimals: 6
1414
+ },
1415
+ Dot: {
1416
+ chain: "Polkadot",
1417
+ rpcAsset: "DOT",
1418
+ decimals: 10
1419
+ },
1420
+ Btc: {
1421
+ chain: "Bitcoin",
1422
+ rpcAsset: "BTC",
1423
+ decimals: 8
1424
+ },
1425
+ ArbUsdc: {
1426
+ chain: "Arbitrum",
1427
+ rpcAsset: "USDC",
1428
+ decimals: 6
1429
+ },
1430
+ ArbEth: {
1431
+ chain: "Arbitrum",
1432
+ rpcAsset: "ETH",
1433
+ decimals: 18
1434
+ },
1435
+ Sol: {
1436
+ chain: "Solana",
1437
+ rpcAsset: "SOL",
1438
+ decimals: 9
1439
+ },
1440
+ SolUsdc: {
1441
+ chain: "Solana",
1442
+ rpcAsset: "USDC",
1443
+ decimals: 6
1444
+ }
1445
+ };
1446
+ var chainConstants = {
1447
+ Ethereum: {
1448
+ assets: ["Eth", "Flip", "Usdc", "Usdt"],
1449
+ gasAsset: "Eth",
1450
+ blockTimeSeconds: 12
1451
+ },
1452
+ Polkadot: {
1453
+ assets: ["Dot"],
1454
+ gasAsset: "Dot",
1455
+ blockTimeSeconds: 6
1456
+ },
1457
+ Bitcoin: {
1458
+ assets: ["Btc"],
1459
+ gasAsset: "Btc",
1460
+ blockTimeSeconds: 10 * 60
1461
+ },
1462
+ Arbitrum: {
1463
+ assets: ["ArbUsdc", "ArbEth"],
1464
+ gasAsset: "ArbEth",
1465
+ blockTimeSeconds: 0.26
1466
+ },
1467
+ Solana: {
1468
+ assets: ["Sol", "SolUsdc"],
1469
+ gasAsset: "Sol",
1470
+ blockTimeSeconds: 0.8
1471
+ }
1472
+ };
1473
+
1474
+ // src/tickMath.ts
1475
+ var MIN_TICK = -887272;
1476
+ var MAX_TICK = -MIN_TICK;
1477
+ var FULL_TICK_RANGE = { start: MIN_TICK, end: MAX_TICK };
1478
+ var tickToRate = (tick, baseAsset) => {
1479
+ const baseRate = 1.0001 ** tick;
1480
+ const rateDecimals = assetConstants.Usdc.decimals - assetConstants[baseAsset].decimals;
1481
+ return baseRate / 10 ** rateDecimals;
1482
+ };
1483
+ var rateToTick = (rate, baseAsset) => {
1484
+ const rateDecimals = assetConstants.Usdc.decimals - assetConstants[baseAsset].decimals;
1485
+ const rawRate = new BigNumber(rate).shiftedBy(rateDecimals);
1486
+ let tick = Math.log(rawRate.toNumber()) / Math.log(1.0001);
1487
+ tick = Math.round(tick * 1e6) / 1e6;
1488
+ tick = Math.floor(tick);
1489
+ return Math.max(MIN_TICK, Math.min(tick, MAX_TICK));
1490
+ };
1491
+ var sqrtPriceX96ToPrice = (amount) => new BigNumber(amount).div(2 ** 96).pow(2);
1492
+ var priceX128ToPrice = (amount) => new BigNumber(amount).div(2 ** 128);
1493
+ // Annotate the CommonJS export names for ESM import in node:
1494
+ 0 && (module.exports = {
1495
+ FULL_TICK_RANGE,
1496
+ MAX_TICK,
1497
+ MIN_TICK,
1498
+ priceX128ToPrice,
1499
+ rateToTick,
1500
+ sqrtPriceX96ToPrice,
1501
+ tickToRate
1502
+ });