@xchainjs/xchain-thorchain 0.24.1 → 0.25.2-alpha.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/lib/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { cosmosclient, proto } from '@cosmos-client/core';
1
+ import { cosmosclient, proto, rest } from '@cosmos-client/core';
2
2
  import { Network, TxType, singleFee, FeeType, BaseXChainClient } from '@xchainjs/xchain-client';
3
3
  import { CosmosSDKClient } from '@xchainjs/xchain-cosmos';
4
4
  import { assetToString, AssetRuneNative, isSynthAsset, assetFromString, baseAmount, assetToBase, assetAmount, Chain } from '@xchainjs/xchain-util';
@@ -36,6 +36,4230 @@ function createCommonjsModule(fn, module) {
36
36
  return module = { exports: {} }, fn(module, module.exports), module.exports;
37
37
  }
38
38
 
39
+ var bignumber = createCommonjsModule(function (module) {
40
+ (function (globalObject) {
41
+
42
+ /*
43
+ * bignumber.js v9.0.1
44
+ * A JavaScript library for arbitrary-precision arithmetic.
45
+ * https://github.com/MikeMcl/bignumber.js
46
+ * Copyright (c) 2020 Michael Mclaughlin <M8ch88l@gmail.com>
47
+ * MIT Licensed.
48
+ *
49
+ * BigNumber.prototype methods | BigNumber methods
50
+ * |
51
+ * absoluteValue abs | clone
52
+ * comparedTo | config set
53
+ * decimalPlaces dp | DECIMAL_PLACES
54
+ * dividedBy div | ROUNDING_MODE
55
+ * dividedToIntegerBy idiv | EXPONENTIAL_AT
56
+ * exponentiatedBy pow | RANGE
57
+ * integerValue | CRYPTO
58
+ * isEqualTo eq | MODULO_MODE
59
+ * isFinite | POW_PRECISION
60
+ * isGreaterThan gt | FORMAT
61
+ * isGreaterThanOrEqualTo gte | ALPHABET
62
+ * isInteger | isBigNumber
63
+ * isLessThan lt | maximum max
64
+ * isLessThanOrEqualTo lte | minimum min
65
+ * isNaN | random
66
+ * isNegative | sum
67
+ * isPositive |
68
+ * isZero |
69
+ * minus |
70
+ * modulo mod |
71
+ * multipliedBy times |
72
+ * negated |
73
+ * plus |
74
+ * precision sd |
75
+ * shiftedBy |
76
+ * squareRoot sqrt |
77
+ * toExponential |
78
+ * toFixed |
79
+ * toFormat |
80
+ * toFraction |
81
+ * toJSON |
82
+ * toNumber |
83
+ * toPrecision |
84
+ * toString |
85
+ * valueOf |
86
+ *
87
+ */
88
+
89
+
90
+ var BigNumber,
91
+ isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,
92
+ mathceil = Math.ceil,
93
+ mathfloor = Math.floor,
94
+
95
+ bignumberError = '[BigNumber Error] ',
96
+ tooManyDigits = bignumberError + 'Number primitive has more than 15 significant digits: ',
97
+
98
+ BASE = 1e14,
99
+ LOG_BASE = 14,
100
+ MAX_SAFE_INTEGER = 0x1fffffffffffff, // 2^53 - 1
101
+ // MAX_INT32 = 0x7fffffff, // 2^31 - 1
102
+ POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13],
103
+ SQRT_BASE = 1e7,
104
+
105
+ // EDITABLE
106
+ // The limit on the value of DECIMAL_PLACES, TO_EXP_NEG, TO_EXP_POS, MIN_EXP, MAX_EXP, and
107
+ // the arguments to toExponential, toFixed, toFormat, and toPrecision.
108
+ MAX = 1E9; // 0 to MAX_INT32
109
+
110
+
111
+ /*
112
+ * Create and return a BigNumber constructor.
113
+ */
114
+ function clone(configObject) {
115
+ var div, convertBase, parseNumeric,
116
+ P = BigNumber.prototype = { constructor: BigNumber, toString: null, valueOf: null },
117
+ ONE = new BigNumber(1),
118
+
119
+
120
+ //----------------------------- EDITABLE CONFIG DEFAULTS -------------------------------
121
+
122
+
123
+ // The default values below must be integers within the inclusive ranges stated.
124
+ // The values can also be changed at run-time using BigNumber.set.
125
+
126
+ // The maximum number of decimal places for operations involving division.
127
+ DECIMAL_PLACES = 20, // 0 to MAX
128
+
129
+ // The rounding mode used when rounding to the above decimal places, and when using
130
+ // toExponential, toFixed, toFormat and toPrecision, and round (default value).
131
+ // UP 0 Away from zero.
132
+ // DOWN 1 Towards zero.
133
+ // CEIL 2 Towards +Infinity.
134
+ // FLOOR 3 Towards -Infinity.
135
+ // HALF_UP 4 Towards nearest neighbour. If equidistant, up.
136
+ // HALF_DOWN 5 Towards nearest neighbour. If equidistant, down.
137
+ // HALF_EVEN 6 Towards nearest neighbour. If equidistant, towards even neighbour.
138
+ // HALF_CEIL 7 Towards nearest neighbour. If equidistant, towards +Infinity.
139
+ // HALF_FLOOR 8 Towards nearest neighbour. If equidistant, towards -Infinity.
140
+ ROUNDING_MODE = 4, // 0 to 8
141
+
142
+ // EXPONENTIAL_AT : [TO_EXP_NEG , TO_EXP_POS]
143
+
144
+ // The exponent value at and beneath which toString returns exponential notation.
145
+ // Number type: -7
146
+ TO_EXP_NEG = -7, // 0 to -MAX
147
+
148
+ // The exponent value at and above which toString returns exponential notation.
149
+ // Number type: 21
150
+ TO_EXP_POS = 21, // 0 to MAX
151
+
152
+ // RANGE : [MIN_EXP, MAX_EXP]
153
+
154
+ // The minimum exponent value, beneath which underflow to zero occurs.
155
+ // Number type: -324 (5e-324)
156
+ MIN_EXP = -1e7, // -1 to -MAX
157
+
158
+ // The maximum exponent value, above which overflow to Infinity occurs.
159
+ // Number type: 308 (1.7976931348623157e+308)
160
+ // For MAX_EXP > 1e7, e.g. new BigNumber('1e100000000').plus(1) may be slow.
161
+ MAX_EXP = 1e7, // 1 to MAX
162
+
163
+ // Whether to use cryptographically-secure random number generation, if available.
164
+ CRYPTO = false, // true or false
165
+
166
+ // The modulo mode used when calculating the modulus: a mod n.
167
+ // The quotient (q = a / n) is calculated according to the corresponding rounding mode.
168
+ // The remainder (r) is calculated as: r = a - n * q.
169
+ //
170
+ // UP 0 The remainder is positive if the dividend is negative, else is negative.
171
+ // DOWN 1 The remainder has the same sign as the dividend.
172
+ // This modulo mode is commonly known as 'truncated division' and is
173
+ // equivalent to (a % n) in JavaScript.
174
+ // FLOOR 3 The remainder has the same sign as the divisor (Python %).
175
+ // HALF_EVEN 6 This modulo mode implements the IEEE 754 remainder function.
176
+ // EUCLID 9 Euclidian division. q = sign(n) * floor(a / abs(n)).
177
+ // The remainder is always positive.
178
+ //
179
+ // The truncated division, floored division, Euclidian division and IEEE 754 remainder
180
+ // modes are commonly used for the modulus operation.
181
+ // Although the other rounding modes can also be used, they may not give useful results.
182
+ MODULO_MODE = 1, // 0 to 9
183
+
184
+ // The maximum number of significant digits of the result of the exponentiatedBy operation.
185
+ // If POW_PRECISION is 0, there will be unlimited significant digits.
186
+ POW_PRECISION = 0, // 0 to MAX
187
+
188
+ // The format specification used by the BigNumber.prototype.toFormat method.
189
+ FORMAT = {
190
+ prefix: '',
191
+ groupSize: 3,
192
+ secondaryGroupSize: 0,
193
+ groupSeparator: ',',
194
+ decimalSeparator: '.',
195
+ fractionGroupSize: 0,
196
+ fractionGroupSeparator: '\xA0', // non-breaking space
197
+ suffix: ''
198
+ },
199
+
200
+ // The alphabet used for base conversion. It must be at least 2 characters long, with no '+',
201
+ // '-', '.', whitespace, or repeated character.
202
+ // '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_'
203
+ ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz';
204
+
205
+
206
+ //------------------------------------------------------------------------------------------
207
+
208
+
209
+ // CONSTRUCTOR
210
+
211
+
212
+ /*
213
+ * The BigNumber constructor and exported function.
214
+ * Create and return a new instance of a BigNumber object.
215
+ *
216
+ * v {number|string|BigNumber} A numeric value.
217
+ * [b] {number} The base of v. Integer, 2 to ALPHABET.length inclusive.
218
+ */
219
+ function BigNumber(v, b) {
220
+ var alphabet, c, caseChanged, e, i, isNum, len, str,
221
+ x = this;
222
+
223
+ // Enable constructor call without `new`.
224
+ if (!(x instanceof BigNumber)) return new BigNumber(v, b);
225
+
226
+ if (b == null) {
227
+
228
+ if (v && v._isBigNumber === true) {
229
+ x.s = v.s;
230
+
231
+ if (!v.c || v.e > MAX_EXP) {
232
+ x.c = x.e = null;
233
+ } else if (v.e < MIN_EXP) {
234
+ x.c = [x.e = 0];
235
+ } else {
236
+ x.e = v.e;
237
+ x.c = v.c.slice();
238
+ }
239
+
240
+ return;
241
+ }
242
+
243
+ if ((isNum = typeof v == 'number') && v * 0 == 0) {
244
+
245
+ // Use `1 / n` to handle minus zero also.
246
+ x.s = 1 / v < 0 ? (v = -v, -1) : 1;
247
+
248
+ // Fast path for integers, where n < 2147483648 (2**31).
249
+ if (v === ~~v) {
250
+ for (e = 0, i = v; i >= 10; i /= 10, e++);
251
+
252
+ if (e > MAX_EXP) {
253
+ x.c = x.e = null;
254
+ } else {
255
+ x.e = e;
256
+ x.c = [v];
257
+ }
258
+
259
+ return;
260
+ }
261
+
262
+ str = String(v);
263
+ } else {
264
+
265
+ if (!isNumeric.test(str = String(v))) return parseNumeric(x, str, isNum);
266
+
267
+ x.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
268
+ }
269
+
270
+ // Decimal point?
271
+ if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
272
+
273
+ // Exponential form?
274
+ if ((i = str.search(/e/i)) > 0) {
275
+
276
+ // Determine exponent.
277
+ if (e < 0) e = i;
278
+ e += +str.slice(i + 1);
279
+ str = str.substring(0, i);
280
+ } else if (e < 0) {
281
+
282
+ // Integer.
283
+ e = str.length;
284
+ }
285
+
286
+ } else {
287
+
288
+ // '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
289
+ intCheck(b, 2, ALPHABET.length, 'Base');
290
+
291
+ // Allow exponential notation to be used with base 10 argument, while
292
+ // also rounding to DECIMAL_PLACES as with other bases.
293
+ if (b == 10) {
294
+ x = new BigNumber(v);
295
+ return round(x, DECIMAL_PLACES + x.e + 1, ROUNDING_MODE);
296
+ }
297
+
298
+ str = String(v);
299
+
300
+ if (isNum = typeof v == 'number') {
301
+
302
+ // Avoid potential interpretation of Infinity and NaN as base 44+ values.
303
+ if (v * 0 != 0) return parseNumeric(x, str, isNum, b);
304
+
305
+ x.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
306
+
307
+ // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
308
+ if (BigNumber.DEBUG && str.replace(/^0\.0*|\./, '').length > 15) {
309
+ throw Error
310
+ (tooManyDigits + v);
311
+ }
312
+ } else {
313
+ x.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
314
+ }
315
+
316
+ alphabet = ALPHABET.slice(0, b);
317
+ e = i = 0;
318
+
319
+ // Check that str is a valid base b number.
320
+ // Don't use RegExp, so alphabet can contain special characters.
321
+ for (len = str.length; i < len; i++) {
322
+ if (alphabet.indexOf(c = str.charAt(i)) < 0) {
323
+ if (c == '.') {
324
+
325
+ // If '.' is not the first character and it has not be found before.
326
+ if (i > e) {
327
+ e = len;
328
+ continue;
329
+ }
330
+ } else if (!caseChanged) {
331
+
332
+ // Allow e.g. hexadecimal 'FF' as well as 'ff'.
333
+ if (str == str.toUpperCase() && (str = str.toLowerCase()) ||
334
+ str == str.toLowerCase() && (str = str.toUpperCase())) {
335
+ caseChanged = true;
336
+ i = -1;
337
+ e = 0;
338
+ continue;
339
+ }
340
+ }
341
+
342
+ return parseNumeric(x, String(v), isNum, b);
343
+ }
344
+ }
345
+
346
+ // Prevent later check for length on converted number.
347
+ isNum = false;
348
+ str = convertBase(str, b, 10, x.s);
349
+
350
+ // Decimal point?
351
+ if ((e = str.indexOf('.')) > -1) str = str.replace('.', '');
352
+ else e = str.length;
353
+ }
354
+
355
+ // Determine leading zeros.
356
+ for (i = 0; str.charCodeAt(i) === 48; i++);
357
+
358
+ // Determine trailing zeros.
359
+ for (len = str.length; str.charCodeAt(--len) === 48;);
360
+
361
+ if (str = str.slice(i, ++len)) {
362
+ len -= i;
363
+
364
+ // '[BigNumber Error] Number primitive has more than 15 significant digits: {n}'
365
+ if (isNum && BigNumber.DEBUG &&
366
+ len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
367
+ throw Error
368
+ (tooManyDigits + (x.s * v));
369
+ }
370
+
371
+ // Overflow?
372
+ if ((e = e - i - 1) > MAX_EXP) {
373
+
374
+ // Infinity.
375
+ x.c = x.e = null;
376
+
377
+ // Underflow?
378
+ } else if (e < MIN_EXP) {
379
+
380
+ // Zero.
381
+ x.c = [x.e = 0];
382
+ } else {
383
+ x.e = e;
384
+ x.c = [];
385
+
386
+ // Transform base
387
+
388
+ // e is the base 10 exponent.
389
+ // i is where to slice str to get the first element of the coefficient array.
390
+ i = (e + 1) % LOG_BASE;
391
+ if (e < 0) i += LOG_BASE; // i < 1
392
+
393
+ if (i < len) {
394
+ if (i) x.c.push(+str.slice(0, i));
395
+
396
+ for (len -= LOG_BASE; i < len;) {
397
+ x.c.push(+str.slice(i, i += LOG_BASE));
398
+ }
399
+
400
+ i = LOG_BASE - (str = str.slice(i)).length;
401
+ } else {
402
+ i -= len;
403
+ }
404
+
405
+ for (; i--; str += '0');
406
+ x.c.push(+str);
407
+ }
408
+ } else {
409
+
410
+ // Zero.
411
+ x.c = [x.e = 0];
412
+ }
413
+ }
414
+
415
+
416
+ // CONSTRUCTOR PROPERTIES
417
+
418
+
419
+ BigNumber.clone = clone;
420
+
421
+ BigNumber.ROUND_UP = 0;
422
+ BigNumber.ROUND_DOWN = 1;
423
+ BigNumber.ROUND_CEIL = 2;
424
+ BigNumber.ROUND_FLOOR = 3;
425
+ BigNumber.ROUND_HALF_UP = 4;
426
+ BigNumber.ROUND_HALF_DOWN = 5;
427
+ BigNumber.ROUND_HALF_EVEN = 6;
428
+ BigNumber.ROUND_HALF_CEIL = 7;
429
+ BigNumber.ROUND_HALF_FLOOR = 8;
430
+ BigNumber.EUCLID = 9;
431
+
432
+
433
+ /*
434
+ * Configure infrequently-changing library-wide settings.
435
+ *
436
+ * Accept an object with the following optional properties (if the value of a property is
437
+ * a number, it must be an integer within the inclusive range stated):
438
+ *
439
+ * DECIMAL_PLACES {number} 0 to MAX
440
+ * ROUNDING_MODE {number} 0 to 8
441
+ * EXPONENTIAL_AT {number|number[]} -MAX to MAX or [-MAX to 0, 0 to MAX]
442
+ * RANGE {number|number[]} -MAX to MAX (not zero) or [-MAX to -1, 1 to MAX]
443
+ * CRYPTO {boolean} true or false
444
+ * MODULO_MODE {number} 0 to 9
445
+ * POW_PRECISION {number} 0 to MAX
446
+ * ALPHABET {string} A string of two or more unique characters which does
447
+ * not contain '.'.
448
+ * FORMAT {object} An object with some of the following properties:
449
+ * prefix {string}
450
+ * groupSize {number}
451
+ * secondaryGroupSize {number}
452
+ * groupSeparator {string}
453
+ * decimalSeparator {string}
454
+ * fractionGroupSize {number}
455
+ * fractionGroupSeparator {string}
456
+ * suffix {string}
457
+ *
458
+ * (The values assigned to the above FORMAT object properties are not checked for validity.)
459
+ *
460
+ * E.g.
461
+ * BigNumber.config({ DECIMAL_PLACES : 20, ROUNDING_MODE : 4 })
462
+ *
463
+ * Ignore properties/parameters set to null or undefined, except for ALPHABET.
464
+ *
465
+ * Return an object with the properties current values.
466
+ */
467
+ BigNumber.config = BigNumber.set = function (obj) {
468
+ var p, v;
469
+
470
+ if (obj != null) {
471
+
472
+ if (typeof obj == 'object') {
473
+
474
+ // DECIMAL_PLACES {number} Integer, 0 to MAX inclusive.
475
+ // '[BigNumber Error] DECIMAL_PLACES {not a primitive number|not an integer|out of range}: {v}'
476
+ if (obj.hasOwnProperty(p = 'DECIMAL_PLACES')) {
477
+ v = obj[p];
478
+ intCheck(v, 0, MAX, p);
479
+ DECIMAL_PLACES = v;
480
+ }
481
+
482
+ // ROUNDING_MODE {number} Integer, 0 to 8 inclusive.
483
+ // '[BigNumber Error] ROUNDING_MODE {not a primitive number|not an integer|out of range}: {v}'
484
+ if (obj.hasOwnProperty(p = 'ROUNDING_MODE')) {
485
+ v = obj[p];
486
+ intCheck(v, 0, 8, p);
487
+ ROUNDING_MODE = v;
488
+ }
489
+
490
+ // EXPONENTIAL_AT {number|number[]}
491
+ // Integer, -MAX to MAX inclusive or
492
+ // [integer -MAX to 0 inclusive, 0 to MAX inclusive].
493
+ // '[BigNumber Error] EXPONENTIAL_AT {not a primitive number|not an integer|out of range}: {v}'
494
+ if (obj.hasOwnProperty(p = 'EXPONENTIAL_AT')) {
495
+ v = obj[p];
496
+ if (v && v.pop) {
497
+ intCheck(v[0], -MAX, 0, p);
498
+ intCheck(v[1], 0, MAX, p);
499
+ TO_EXP_NEG = v[0];
500
+ TO_EXP_POS = v[1];
501
+ } else {
502
+ intCheck(v, -MAX, MAX, p);
503
+ TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
504
+ }
505
+ }
506
+
507
+ // RANGE {number|number[]} Non-zero integer, -MAX to MAX inclusive or
508
+ // [integer -MAX to -1 inclusive, integer 1 to MAX inclusive].
509
+ // '[BigNumber Error] RANGE {not a primitive number|not an integer|out of range|cannot be zero}: {v}'
510
+ if (obj.hasOwnProperty(p = 'RANGE')) {
511
+ v = obj[p];
512
+ if (v && v.pop) {
513
+ intCheck(v[0], -MAX, -1, p);
514
+ intCheck(v[1], 1, MAX, p);
515
+ MIN_EXP = v[0];
516
+ MAX_EXP = v[1];
517
+ } else {
518
+ intCheck(v, -MAX, MAX, p);
519
+ if (v) {
520
+ MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
521
+ } else {
522
+ throw Error
523
+ (bignumberError + p + ' cannot be zero: ' + v);
524
+ }
525
+ }
526
+ }
527
+
528
+ // CRYPTO {boolean} true or false.
529
+ // '[BigNumber Error] CRYPTO not true or false: {v}'
530
+ // '[BigNumber Error] crypto unavailable'
531
+ if (obj.hasOwnProperty(p = 'CRYPTO')) {
532
+ v = obj[p];
533
+ if (v === !!v) {
534
+ if (v) {
535
+ if (typeof crypto != 'undefined' && crypto &&
536
+ (crypto.getRandomValues || crypto.randomBytes)) {
537
+ CRYPTO = v;
538
+ } else {
539
+ CRYPTO = !v;
540
+ throw Error
541
+ (bignumberError + 'crypto unavailable');
542
+ }
543
+ } else {
544
+ CRYPTO = v;
545
+ }
546
+ } else {
547
+ throw Error
548
+ (bignumberError + p + ' not true or false: ' + v);
549
+ }
550
+ }
551
+
552
+ // MODULO_MODE {number} Integer, 0 to 9 inclusive.
553
+ // '[BigNumber Error] MODULO_MODE {not a primitive number|not an integer|out of range}: {v}'
554
+ if (obj.hasOwnProperty(p = 'MODULO_MODE')) {
555
+ v = obj[p];
556
+ intCheck(v, 0, 9, p);
557
+ MODULO_MODE = v;
558
+ }
559
+
560
+ // POW_PRECISION {number} Integer, 0 to MAX inclusive.
561
+ // '[BigNumber Error] POW_PRECISION {not a primitive number|not an integer|out of range}: {v}'
562
+ if (obj.hasOwnProperty(p = 'POW_PRECISION')) {
563
+ v = obj[p];
564
+ intCheck(v, 0, MAX, p);
565
+ POW_PRECISION = v;
566
+ }
567
+
568
+ // FORMAT {object}
569
+ // '[BigNumber Error] FORMAT not an object: {v}'
570
+ if (obj.hasOwnProperty(p = 'FORMAT')) {
571
+ v = obj[p];
572
+ if (typeof v == 'object') FORMAT = v;
573
+ else throw Error
574
+ (bignumberError + p + ' not an object: ' + v);
575
+ }
576
+
577
+ // ALPHABET {string}
578
+ // '[BigNumber Error] ALPHABET invalid: {v}'
579
+ if (obj.hasOwnProperty(p = 'ALPHABET')) {
580
+ v = obj[p];
581
+
582
+ // Disallow if less than two characters,
583
+ // or if it contains '+', '-', '.', whitespace, or a repeated character.
584
+ if (typeof v == 'string' && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
585
+ ALPHABET = v;
586
+ } else {
587
+ throw Error
588
+ (bignumberError + p + ' invalid: ' + v);
589
+ }
590
+ }
591
+
592
+ } else {
593
+
594
+ // '[BigNumber Error] Object expected: {v}'
595
+ throw Error
596
+ (bignumberError + 'Object expected: ' + obj);
597
+ }
598
+ }
599
+
600
+ return {
601
+ DECIMAL_PLACES: DECIMAL_PLACES,
602
+ ROUNDING_MODE: ROUNDING_MODE,
603
+ EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
604
+ RANGE: [MIN_EXP, MAX_EXP],
605
+ CRYPTO: CRYPTO,
606
+ MODULO_MODE: MODULO_MODE,
607
+ POW_PRECISION: POW_PRECISION,
608
+ FORMAT: FORMAT,
609
+ ALPHABET: ALPHABET
610
+ };
611
+ };
612
+
613
+
614
+ /*
615
+ * Return true if v is a BigNumber instance, otherwise return false.
616
+ *
617
+ * If BigNumber.DEBUG is true, throw if a BigNumber instance is not well-formed.
618
+ *
619
+ * v {any}
620
+ *
621
+ * '[BigNumber Error] Invalid BigNumber: {v}'
622
+ */
623
+ BigNumber.isBigNumber = function (v) {
624
+ if (!v || v._isBigNumber !== true) return false;
625
+ if (!BigNumber.DEBUG) return true;
626
+
627
+ var i, n,
628
+ c = v.c,
629
+ e = v.e,
630
+ s = v.s;
631
+
632
+ out: if ({}.toString.call(c) == '[object Array]') {
633
+
634
+ if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
635
+
636
+ // If the first element is zero, the BigNumber value must be zero.
637
+ if (c[0] === 0) {
638
+ if (e === 0 && c.length === 1) return true;
639
+ break out;
640
+ }
641
+
642
+ // Calculate number of digits that c[0] should have, based on the exponent.
643
+ i = (e + 1) % LOG_BASE;
644
+ if (i < 1) i += LOG_BASE;
645
+
646
+ // Calculate number of digits of c[0].
647
+ //if (Math.ceil(Math.log(c[0] + 1) / Math.LN10) == i) {
648
+ if (String(c[0]).length == i) {
649
+
650
+ for (i = 0; i < c.length; i++) {
651
+ n = c[i];
652
+ if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
653
+ }
654
+
655
+ // Last element cannot be zero, unless it is the only element.
656
+ if (n !== 0) return true;
657
+ }
658
+ }
659
+
660
+ // Infinity/NaN
661
+ } else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
662
+ return true;
663
+ }
664
+
665
+ throw Error
666
+ (bignumberError + 'Invalid BigNumber: ' + v);
667
+ };
668
+
669
+
670
+ /*
671
+ * Return a new BigNumber whose value is the maximum of the arguments.
672
+ *
673
+ * arguments {number|string|BigNumber}
674
+ */
675
+ BigNumber.maximum = BigNumber.max = function () {
676
+ return maxOrMin(arguments, P.lt);
677
+ };
678
+
679
+
680
+ /*
681
+ * Return a new BigNumber whose value is the minimum of the arguments.
682
+ *
683
+ * arguments {number|string|BigNumber}
684
+ */
685
+ BigNumber.minimum = BigNumber.min = function () {
686
+ return maxOrMin(arguments, P.gt);
687
+ };
688
+
689
+
690
+ /*
691
+ * Return a new BigNumber with a random value equal to or greater than 0 and less than 1,
692
+ * and with dp, or DECIMAL_PLACES if dp is omitted, decimal places (or less if trailing
693
+ * zeros are produced).
694
+ *
695
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
696
+ *
697
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp}'
698
+ * '[BigNumber Error] crypto unavailable'
699
+ */
700
+ BigNumber.random = (function () {
701
+ var pow2_53 = 0x20000000000000;
702
+
703
+ // Return a 53 bit integer n, where 0 <= n < 9007199254740992.
704
+ // Check if Math.random() produces more than 32 bits of randomness.
705
+ // If it does, assume at least 53 bits are produced, otherwise assume at least 30 bits.
706
+ // 0x40000000 is 2^30, 0x800000 is 2^23, 0x1fffff is 2^21 - 1.
707
+ var random53bitInt = (Math.random() * pow2_53) & 0x1fffff
708
+ ? function () { return mathfloor(Math.random() * pow2_53); }
709
+ : function () { return ((Math.random() * 0x40000000 | 0) * 0x800000) +
710
+ (Math.random() * 0x800000 | 0); };
711
+
712
+ return function (dp) {
713
+ var a, b, e, k, v,
714
+ i = 0,
715
+ c = [],
716
+ rand = new BigNumber(ONE);
717
+
718
+ if (dp == null) dp = DECIMAL_PLACES;
719
+ else intCheck(dp, 0, MAX);
720
+
721
+ k = mathceil(dp / LOG_BASE);
722
+
723
+ if (CRYPTO) {
724
+
725
+ // Browsers supporting crypto.getRandomValues.
726
+ if (crypto.getRandomValues) {
727
+
728
+ a = crypto.getRandomValues(new Uint32Array(k *= 2));
729
+
730
+ for (; i < k;) {
731
+
732
+ // 53 bits:
733
+ // ((Math.pow(2, 32) - 1) * Math.pow(2, 21)).toString(2)
734
+ // 11111 11111111 11111111 11111111 11100000 00000000 00000000
735
+ // ((Math.pow(2, 32) - 1) >>> 11).toString(2)
736
+ // 11111 11111111 11111111
737
+ // 0x20000 is 2^21.
738
+ v = a[i] * 0x20000 + (a[i + 1] >>> 11);
739
+
740
+ // Rejection sampling:
741
+ // 0 <= v < 9007199254740992
742
+ // Probability that v >= 9e15, is
743
+ // 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251
744
+ if (v >= 9e15) {
745
+ b = crypto.getRandomValues(new Uint32Array(2));
746
+ a[i] = b[0];
747
+ a[i + 1] = b[1];
748
+ } else {
749
+
750
+ // 0 <= v <= 8999999999999999
751
+ // 0 <= (v % 1e14) <= 99999999999999
752
+ c.push(v % 1e14);
753
+ i += 2;
754
+ }
755
+ }
756
+ i = k / 2;
757
+
758
+ // Node.js supporting crypto.randomBytes.
759
+ } else if (crypto.randomBytes) {
760
+
761
+ // buffer
762
+ a = crypto.randomBytes(k *= 7);
763
+
764
+ for (; i < k;) {
765
+
766
+ // 0x1000000000000 is 2^48, 0x10000000000 is 2^40
767
+ // 0x100000000 is 2^32, 0x1000000 is 2^24
768
+ // 11111 11111111 11111111 11111111 11111111 11111111 11111111
769
+ // 0 <= v < 9007199254740992
770
+ v = ((a[i] & 31) * 0x1000000000000) + (a[i + 1] * 0x10000000000) +
771
+ (a[i + 2] * 0x100000000) + (a[i + 3] * 0x1000000) +
772
+ (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
773
+
774
+ if (v >= 9e15) {
775
+ crypto.randomBytes(7).copy(a, i);
776
+ } else {
777
+
778
+ // 0 <= (v % 1e14) <= 99999999999999
779
+ c.push(v % 1e14);
780
+ i += 7;
781
+ }
782
+ }
783
+ i = k / 7;
784
+ } else {
785
+ CRYPTO = false;
786
+ throw Error
787
+ (bignumberError + 'crypto unavailable');
788
+ }
789
+ }
790
+
791
+ // Use Math.random.
792
+ if (!CRYPTO) {
793
+
794
+ for (; i < k;) {
795
+ v = random53bitInt();
796
+ if (v < 9e15) c[i++] = v % 1e14;
797
+ }
798
+ }
799
+
800
+ k = c[--i];
801
+ dp %= LOG_BASE;
802
+
803
+ // Convert trailing digits to zeros according to dp.
804
+ if (k && dp) {
805
+ v = POWS_TEN[LOG_BASE - dp];
806
+ c[i] = mathfloor(k / v) * v;
807
+ }
808
+
809
+ // Remove trailing elements which are zero.
810
+ for (; c[i] === 0; c.pop(), i--);
811
+
812
+ // Zero?
813
+ if (i < 0) {
814
+ c = [e = 0];
815
+ } else {
816
+
817
+ // Remove leading elements which are zero and adjust exponent accordingly.
818
+ for (e = -1 ; c[0] === 0; c.splice(0, 1), e -= LOG_BASE);
819
+
820
+ // Count the digits of the first element of c to determine leading zeros, and...
821
+ for (i = 1, v = c[0]; v >= 10; v /= 10, i++);
822
+
823
+ // adjust the exponent accordingly.
824
+ if (i < LOG_BASE) e -= LOG_BASE - i;
825
+ }
826
+
827
+ rand.e = e;
828
+ rand.c = c;
829
+ return rand;
830
+ };
831
+ })();
832
+
833
+
834
+ /*
835
+ * Return a BigNumber whose value is the sum of the arguments.
836
+ *
837
+ * arguments {number|string|BigNumber}
838
+ */
839
+ BigNumber.sum = function () {
840
+ var i = 1,
841
+ args = arguments,
842
+ sum = new BigNumber(args[0]);
843
+ for (; i < args.length;) sum = sum.plus(args[i++]);
844
+ return sum;
845
+ };
846
+
847
+
848
+ // PRIVATE FUNCTIONS
849
+
850
+
851
+ // Called by BigNumber and BigNumber.prototype.toString.
852
+ convertBase = (function () {
853
+ var decimal = '0123456789';
854
+
855
+ /*
856
+ * Convert string of baseIn to an array of numbers of baseOut.
857
+ * Eg. toBaseOut('255', 10, 16) returns [15, 15].
858
+ * Eg. toBaseOut('ff', 16, 10) returns [2, 5, 5].
859
+ */
860
+ function toBaseOut(str, baseIn, baseOut, alphabet) {
861
+ var j,
862
+ arr = [0],
863
+ arrL,
864
+ i = 0,
865
+ len = str.length;
866
+
867
+ for (; i < len;) {
868
+ for (arrL = arr.length; arrL--; arr[arrL] *= baseIn);
869
+
870
+ arr[0] += alphabet.indexOf(str.charAt(i++));
871
+
872
+ for (j = 0; j < arr.length; j++) {
873
+
874
+ if (arr[j] > baseOut - 1) {
875
+ if (arr[j + 1] == null) arr[j + 1] = 0;
876
+ arr[j + 1] += arr[j] / baseOut | 0;
877
+ arr[j] %= baseOut;
878
+ }
879
+ }
880
+ }
881
+
882
+ return arr.reverse();
883
+ }
884
+
885
+ // Convert a numeric string of baseIn to a numeric string of baseOut.
886
+ // If the caller is toString, we are converting from base 10 to baseOut.
887
+ // If the caller is BigNumber, we are converting from baseIn to base 10.
888
+ return function (str, baseIn, baseOut, sign, callerIsToString) {
889
+ var alphabet, d, e, k, r, x, xc, y,
890
+ i = str.indexOf('.'),
891
+ dp = DECIMAL_PLACES,
892
+ rm = ROUNDING_MODE;
893
+
894
+ // Non-integer.
895
+ if (i >= 0) {
896
+ k = POW_PRECISION;
897
+
898
+ // Unlimited precision.
899
+ POW_PRECISION = 0;
900
+ str = str.replace('.', '');
901
+ y = new BigNumber(baseIn);
902
+ x = y.pow(str.length - i);
903
+ POW_PRECISION = k;
904
+
905
+ // Convert str as if an integer, then restore the fraction part by dividing the
906
+ // result by its base raised to a power.
907
+
908
+ y.c = toBaseOut(toFixedPoint(coeffToString(x.c), x.e, '0'),
909
+ 10, baseOut, decimal);
910
+ y.e = y.c.length;
911
+ }
912
+
913
+ // Convert the number as integer.
914
+
915
+ xc = toBaseOut(str, baseIn, baseOut, callerIsToString
916
+ ? (alphabet = ALPHABET, decimal)
917
+ : (alphabet = decimal, ALPHABET));
918
+
919
+ // xc now represents str as an integer and converted to baseOut. e is the exponent.
920
+ e = k = xc.length;
921
+
922
+ // Remove trailing zeros.
923
+ for (; xc[--k] == 0; xc.pop());
924
+
925
+ // Zero?
926
+ if (!xc[0]) return alphabet.charAt(0);
927
+
928
+ // Does str represent an integer? If so, no need for the division.
929
+ if (i < 0) {
930
+ --e;
931
+ } else {
932
+ x.c = xc;
933
+ x.e = e;
934
+
935
+ // The sign is needed for correct rounding.
936
+ x.s = sign;
937
+ x = div(x, y, dp, rm, baseOut);
938
+ xc = x.c;
939
+ r = x.r;
940
+ e = x.e;
941
+ }
942
+
943
+ // xc now represents str converted to baseOut.
944
+
945
+ // THe index of the rounding digit.
946
+ d = e + dp + 1;
947
+
948
+ // The rounding digit: the digit to the right of the digit that may be rounded up.
949
+ i = xc[d];
950
+
951
+ // Look at the rounding digits and mode to determine whether to round up.
952
+
953
+ k = baseOut / 2;
954
+ r = r || d < 0 || xc[d + 1] != null;
955
+
956
+ r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
957
+ : i > k || i == k &&(rm == 4 || r || rm == 6 && xc[d - 1] & 1 ||
958
+ rm == (x.s < 0 ? 8 : 7));
959
+
960
+ // If the index of the rounding digit is not greater than zero, or xc represents
961
+ // zero, then the result of the base conversion is zero or, if rounding up, a value
962
+ // such as 0.00001.
963
+ if (d < 1 || !xc[0]) {
964
+
965
+ // 1^-dp or 0
966
+ str = r ? toFixedPoint(alphabet.charAt(1), -dp, alphabet.charAt(0)) : alphabet.charAt(0);
967
+ } else {
968
+
969
+ // Truncate xc to the required number of decimal places.
970
+ xc.length = d;
971
+
972
+ // Round up?
973
+ if (r) {
974
+
975
+ // Rounding up may mean the previous digit has to be rounded up and so on.
976
+ for (--baseOut; ++xc[--d] > baseOut;) {
977
+ xc[d] = 0;
978
+
979
+ if (!d) {
980
+ ++e;
981
+ xc = [1].concat(xc);
982
+ }
983
+ }
984
+ }
985
+
986
+ // Determine trailing zeros.
987
+ for (k = xc.length; !xc[--k];);
988
+
989
+ // E.g. [4, 11, 15] becomes 4bf.
990
+ for (i = 0, str = ''; i <= k; str += alphabet.charAt(xc[i++]));
991
+
992
+ // Add leading zeros, decimal point and trailing zeros as required.
993
+ str = toFixedPoint(str, e, alphabet.charAt(0));
994
+ }
995
+
996
+ // The caller will add the sign.
997
+ return str;
998
+ };
999
+ })();
1000
+
1001
+
1002
+ // Perform division in the specified base. Called by div and convertBase.
1003
+ div = (function () {
1004
+
1005
+ // Assume non-zero x and k.
1006
+ function multiply(x, k, base) {
1007
+ var m, temp, xlo, xhi,
1008
+ carry = 0,
1009
+ i = x.length,
1010
+ klo = k % SQRT_BASE,
1011
+ khi = k / SQRT_BASE | 0;
1012
+
1013
+ for (x = x.slice(); i--;) {
1014
+ xlo = x[i] % SQRT_BASE;
1015
+ xhi = x[i] / SQRT_BASE | 0;
1016
+ m = khi * xlo + xhi * klo;
1017
+ temp = klo * xlo + ((m % SQRT_BASE) * SQRT_BASE) + carry;
1018
+ carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
1019
+ x[i] = temp % base;
1020
+ }
1021
+
1022
+ if (carry) x = [carry].concat(x);
1023
+
1024
+ return x;
1025
+ }
1026
+
1027
+ function compare(a, b, aL, bL) {
1028
+ var i, cmp;
1029
+
1030
+ if (aL != bL) {
1031
+ cmp = aL > bL ? 1 : -1;
1032
+ } else {
1033
+
1034
+ for (i = cmp = 0; i < aL; i++) {
1035
+
1036
+ if (a[i] != b[i]) {
1037
+ cmp = a[i] > b[i] ? 1 : -1;
1038
+ break;
1039
+ }
1040
+ }
1041
+ }
1042
+
1043
+ return cmp;
1044
+ }
1045
+
1046
+ function subtract(a, b, aL, base) {
1047
+ var i = 0;
1048
+
1049
+ // Subtract b from a.
1050
+ for (; aL--;) {
1051
+ a[aL] -= i;
1052
+ i = a[aL] < b[aL] ? 1 : 0;
1053
+ a[aL] = i * base + a[aL] - b[aL];
1054
+ }
1055
+
1056
+ // Remove leading zeros.
1057
+ for (; !a[0] && a.length > 1; a.splice(0, 1));
1058
+ }
1059
+
1060
+ // x: dividend, y: divisor.
1061
+ return function (x, y, dp, rm, base) {
1062
+ var cmp, e, i, more, n, prod, prodL, q, qc, rem, remL, rem0, xi, xL, yc0,
1063
+ yL, yz,
1064
+ s = x.s == y.s ? 1 : -1,
1065
+ xc = x.c,
1066
+ yc = y.c;
1067
+
1068
+ // Either NaN, Infinity or 0?
1069
+ if (!xc || !xc[0] || !yc || !yc[0]) {
1070
+
1071
+ return new BigNumber(
1072
+
1073
+ // Return NaN if either NaN, or both Infinity or 0.
1074
+ !x.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN :
1075
+
1076
+ // Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
1077
+ xc && xc[0] == 0 || !yc ? s * 0 : s / 0
1078
+ );
1079
+ }
1080
+
1081
+ q = new BigNumber(s);
1082
+ qc = q.c = [];
1083
+ e = x.e - y.e;
1084
+ s = dp + e + 1;
1085
+
1086
+ if (!base) {
1087
+ base = BASE;
1088
+ e = bitFloor(x.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
1089
+ s = s / LOG_BASE | 0;
1090
+ }
1091
+
1092
+ // Result exponent may be one less then the current value of e.
1093
+ // The coefficients of the BigNumbers from convertBase may have trailing zeros.
1094
+ for (i = 0; yc[i] == (xc[i] || 0); i++);
1095
+
1096
+ if (yc[i] > (xc[i] || 0)) e--;
1097
+
1098
+ if (s < 0) {
1099
+ qc.push(1);
1100
+ more = true;
1101
+ } else {
1102
+ xL = xc.length;
1103
+ yL = yc.length;
1104
+ i = 0;
1105
+ s += 2;
1106
+
1107
+ // Normalise xc and yc so highest order digit of yc is >= base / 2.
1108
+
1109
+ n = mathfloor(base / (yc[0] + 1));
1110
+
1111
+ // Not necessary, but to handle odd bases where yc[0] == (base / 2) - 1.
1112
+ // if (n > 1 || n++ == 1 && yc[0] < base / 2) {
1113
+ if (n > 1) {
1114
+ yc = multiply(yc, n, base);
1115
+ xc = multiply(xc, n, base);
1116
+ yL = yc.length;
1117
+ xL = xc.length;
1118
+ }
1119
+
1120
+ xi = yL;
1121
+ rem = xc.slice(0, yL);
1122
+ remL = rem.length;
1123
+
1124
+ // Add zeros to make remainder as long as divisor.
1125
+ for (; remL < yL; rem[remL++] = 0);
1126
+ yz = yc.slice();
1127
+ yz = [0].concat(yz);
1128
+ yc0 = yc[0];
1129
+ if (yc[1] >= base / 2) yc0++;
1130
+ // Not necessary, but to prevent trial digit n > base, when using base 3.
1131
+ // else if (base == 3 && yc0 == 1) yc0 = 1 + 1e-15;
1132
+
1133
+ do {
1134
+ n = 0;
1135
+
1136
+ // Compare divisor and remainder.
1137
+ cmp = compare(yc, rem, yL, remL);
1138
+
1139
+ // If divisor < remainder.
1140
+ if (cmp < 0) {
1141
+
1142
+ // Calculate trial digit, n.
1143
+
1144
+ rem0 = rem[0];
1145
+ if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
1146
+
1147
+ // n is how many times the divisor goes into the current remainder.
1148
+ n = mathfloor(rem0 / yc0);
1149
+
1150
+ // Algorithm:
1151
+ // product = divisor multiplied by trial digit (n).
1152
+ // Compare product and remainder.
1153
+ // If product is greater than remainder:
1154
+ // Subtract divisor from product, decrement trial digit.
1155
+ // Subtract product from remainder.
1156
+ // If product was less than remainder at the last compare:
1157
+ // Compare new remainder and divisor.
1158
+ // If remainder is greater than divisor:
1159
+ // Subtract divisor from remainder, increment trial digit.
1160
+
1161
+ if (n > 1) {
1162
+
1163
+ // n may be > base only when base is 3.
1164
+ if (n >= base) n = base - 1;
1165
+
1166
+ // product = divisor * trial digit.
1167
+ prod = multiply(yc, n, base);
1168
+ prodL = prod.length;
1169
+ remL = rem.length;
1170
+
1171
+ // Compare product and remainder.
1172
+ // If product > remainder then trial digit n too high.
1173
+ // n is 1 too high about 5% of the time, and is not known to have
1174
+ // ever been more than 1 too high.
1175
+ while (compare(prod, rem, prodL, remL) == 1) {
1176
+ n--;
1177
+
1178
+ // Subtract divisor from product.
1179
+ subtract(prod, yL < prodL ? yz : yc, prodL, base);
1180
+ prodL = prod.length;
1181
+ cmp = 1;
1182
+ }
1183
+ } else {
1184
+
1185
+ // n is 0 or 1, cmp is -1.
1186
+ // If n is 0, there is no need to compare yc and rem again below,
1187
+ // so change cmp to 1 to avoid it.
1188
+ // If n is 1, leave cmp as -1, so yc and rem are compared again.
1189
+ if (n == 0) {
1190
+
1191
+ // divisor < remainder, so n must be at least 1.
1192
+ cmp = n = 1;
1193
+ }
1194
+
1195
+ // product = divisor
1196
+ prod = yc.slice();
1197
+ prodL = prod.length;
1198
+ }
1199
+
1200
+ if (prodL < remL) prod = [0].concat(prod);
1201
+
1202
+ // Subtract product from remainder.
1203
+ subtract(rem, prod, remL, base);
1204
+ remL = rem.length;
1205
+
1206
+ // If product was < remainder.
1207
+ if (cmp == -1) {
1208
+
1209
+ // Compare divisor and new remainder.
1210
+ // If divisor < new remainder, subtract divisor from remainder.
1211
+ // Trial digit n too low.
1212
+ // n is 1 too low about 5% of the time, and very rarely 2 too low.
1213
+ while (compare(yc, rem, yL, remL) < 1) {
1214
+ n++;
1215
+
1216
+ // Subtract divisor from remainder.
1217
+ subtract(rem, yL < remL ? yz : yc, remL, base);
1218
+ remL = rem.length;
1219
+ }
1220
+ }
1221
+ } else if (cmp === 0) {
1222
+ n++;
1223
+ rem = [0];
1224
+ } // else cmp === 1 and n will be 0
1225
+
1226
+ // Add the next digit, n, to the result array.
1227
+ qc[i++] = n;
1228
+
1229
+ // Update the remainder.
1230
+ if (rem[0]) {
1231
+ rem[remL++] = xc[xi] || 0;
1232
+ } else {
1233
+ rem = [xc[xi]];
1234
+ remL = 1;
1235
+ }
1236
+ } while ((xi++ < xL || rem[0] != null) && s--);
1237
+
1238
+ more = rem[0] != null;
1239
+
1240
+ // Leading zero?
1241
+ if (!qc[0]) qc.splice(0, 1);
1242
+ }
1243
+
1244
+ if (base == BASE) {
1245
+
1246
+ // To calculate q.e, first get the number of digits of qc[0].
1247
+ for (i = 1, s = qc[0]; s >= 10; s /= 10, i++);
1248
+
1249
+ round(q, dp + (q.e = i + e * LOG_BASE - 1) + 1, rm, more);
1250
+
1251
+ // Caller is convertBase.
1252
+ } else {
1253
+ q.e = e;
1254
+ q.r = +more;
1255
+ }
1256
+
1257
+ return q;
1258
+ };
1259
+ })();
1260
+
1261
+
1262
+ /*
1263
+ * Return a string representing the value of BigNumber n in fixed-point or exponential
1264
+ * notation rounded to the specified decimal places or significant digits.
1265
+ *
1266
+ * n: a BigNumber.
1267
+ * i: the index of the last digit required (i.e. the digit that may be rounded up).
1268
+ * rm: the rounding mode.
1269
+ * id: 1 (toExponential) or 2 (toPrecision).
1270
+ */
1271
+ function format(n, i, rm, id) {
1272
+ var c0, e, ne, len, str;
1273
+
1274
+ if (rm == null) rm = ROUNDING_MODE;
1275
+ else intCheck(rm, 0, 8);
1276
+
1277
+ if (!n.c) return n.toString();
1278
+
1279
+ c0 = n.c[0];
1280
+ ne = n.e;
1281
+
1282
+ if (i == null) {
1283
+ str = coeffToString(n.c);
1284
+ str = id == 1 || id == 2 && (ne <= TO_EXP_NEG || ne >= TO_EXP_POS)
1285
+ ? toExponential(str, ne)
1286
+ : toFixedPoint(str, ne, '0');
1287
+ } else {
1288
+ n = round(new BigNumber(n), i, rm);
1289
+
1290
+ // n.e may have changed if the value was rounded up.
1291
+ e = n.e;
1292
+
1293
+ str = coeffToString(n.c);
1294
+ len = str.length;
1295
+
1296
+ // toPrecision returns exponential notation if the number of significant digits
1297
+ // specified is less than the number of digits necessary to represent the integer
1298
+ // part of the value in fixed-point notation.
1299
+
1300
+ // Exponential notation.
1301
+ if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
1302
+
1303
+ // Append zeros?
1304
+ for (; len < i; str += '0', len++);
1305
+ str = toExponential(str, e);
1306
+
1307
+ // Fixed-point notation.
1308
+ } else {
1309
+ i -= ne;
1310
+ str = toFixedPoint(str, e, '0');
1311
+
1312
+ // Append zeros?
1313
+ if (e + 1 > len) {
1314
+ if (--i > 0) for (str += '.'; i--; str += '0');
1315
+ } else {
1316
+ i += e - len;
1317
+ if (i > 0) {
1318
+ if (e + 1 == len) str += '.';
1319
+ for (; i--; str += '0');
1320
+ }
1321
+ }
1322
+ }
1323
+ }
1324
+
1325
+ return n.s < 0 && c0 ? '-' + str : str;
1326
+ }
1327
+
1328
+
1329
+ // Handle BigNumber.max and BigNumber.min.
1330
+ function maxOrMin(args, method) {
1331
+ var n,
1332
+ i = 1,
1333
+ m = new BigNumber(args[0]);
1334
+
1335
+ for (; i < args.length; i++) {
1336
+ n = new BigNumber(args[i]);
1337
+
1338
+ // If any number is NaN, return NaN.
1339
+ if (!n.s) {
1340
+ m = n;
1341
+ break;
1342
+ } else if (method.call(m, n)) {
1343
+ m = n;
1344
+ }
1345
+ }
1346
+
1347
+ return m;
1348
+ }
1349
+
1350
+
1351
+ /*
1352
+ * Strip trailing zeros, calculate base 10 exponent and check against MIN_EXP and MAX_EXP.
1353
+ * Called by minus, plus and times.
1354
+ */
1355
+ function normalise(n, c, e) {
1356
+ var i = 1,
1357
+ j = c.length;
1358
+
1359
+ // Remove trailing zeros.
1360
+ for (; !c[--j]; c.pop());
1361
+
1362
+ // Calculate the base 10 exponent. First get the number of digits of c[0].
1363
+ for (j = c[0]; j >= 10; j /= 10, i++);
1364
+
1365
+ // Overflow?
1366
+ if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
1367
+
1368
+ // Infinity.
1369
+ n.c = n.e = null;
1370
+
1371
+ // Underflow?
1372
+ } else if (e < MIN_EXP) {
1373
+
1374
+ // Zero.
1375
+ n.c = [n.e = 0];
1376
+ } else {
1377
+ n.e = e;
1378
+ n.c = c;
1379
+ }
1380
+
1381
+ return n;
1382
+ }
1383
+
1384
+
1385
+ // Handle values that fail the validity test in BigNumber.
1386
+ parseNumeric = (function () {
1387
+ var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i,
1388
+ dotAfter = /^([^.]+)\.$/,
1389
+ dotBefore = /^\.([^.]+)$/,
1390
+ isInfinityOrNaN = /^-?(Infinity|NaN)$/,
1391
+ whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
1392
+
1393
+ return function (x, str, isNum, b) {
1394
+ var base,
1395
+ s = isNum ? str : str.replace(whitespaceOrPlus, '');
1396
+
1397
+ // No exception on ±Infinity or NaN.
1398
+ if (isInfinityOrNaN.test(s)) {
1399
+ x.s = isNaN(s) ? null : s < 0 ? -1 : 1;
1400
+ } else {
1401
+ if (!isNum) {
1402
+
1403
+ // basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i
1404
+ s = s.replace(basePrefix, function (m, p1, p2) {
1405
+ base = (p2 = p2.toLowerCase()) == 'x' ? 16 : p2 == 'b' ? 2 : 8;
1406
+ return !b || b == base ? p1 : m;
1407
+ });
1408
+
1409
+ if (b) {
1410
+ base = b;
1411
+
1412
+ // E.g. '1.' to '1', '.1' to '0.1'
1413
+ s = s.replace(dotAfter, '$1').replace(dotBefore, '0.$1');
1414
+ }
1415
+
1416
+ if (str != s) return new BigNumber(s, base);
1417
+ }
1418
+
1419
+ // '[BigNumber Error] Not a number: {n}'
1420
+ // '[BigNumber Error] Not a base {b} number: {n}'
1421
+ if (BigNumber.DEBUG) {
1422
+ throw Error
1423
+ (bignumberError + 'Not a' + (b ? ' base ' + b : '') + ' number: ' + str);
1424
+ }
1425
+
1426
+ // NaN
1427
+ x.s = null;
1428
+ }
1429
+
1430
+ x.c = x.e = null;
1431
+ }
1432
+ })();
1433
+
1434
+
1435
+ /*
1436
+ * Round x to sd significant digits using rounding mode rm. Check for over/under-flow.
1437
+ * If r is truthy, it is known that there are more digits after the rounding digit.
1438
+ */
1439
+ function round(x, sd, rm, r) {
1440
+ var d, i, j, k, n, ni, rd,
1441
+ xc = x.c,
1442
+ pows10 = POWS_TEN;
1443
+
1444
+ // if x is not Infinity or NaN...
1445
+ if (xc) {
1446
+
1447
+ // rd is the rounding digit, i.e. the digit after the digit that may be rounded up.
1448
+ // n is a base 1e14 number, the value of the element of array x.c containing rd.
1449
+ // ni is the index of n within x.c.
1450
+ // d is the number of digits of n.
1451
+ // i is the index of rd within n including leading zeros.
1452
+ // j is the actual index of rd within n (if < 0, rd is a leading zero).
1453
+ out: {
1454
+
1455
+ // Get the number of digits of the first element of xc.
1456
+ for (d = 1, k = xc[0]; k >= 10; k /= 10, d++);
1457
+ i = sd - d;
1458
+
1459
+ // If the rounding digit is in the first element of xc...
1460
+ if (i < 0) {
1461
+ i += LOG_BASE;
1462
+ j = sd;
1463
+ n = xc[ni = 0];
1464
+
1465
+ // Get the rounding digit at index j of n.
1466
+ rd = n / pows10[d - j - 1] % 10 | 0;
1467
+ } else {
1468
+ ni = mathceil((i + 1) / LOG_BASE);
1469
+
1470
+ if (ni >= xc.length) {
1471
+
1472
+ if (r) {
1473
+
1474
+ // Needed by sqrt.
1475
+ for (; xc.length <= ni; xc.push(0));
1476
+ n = rd = 0;
1477
+ d = 1;
1478
+ i %= LOG_BASE;
1479
+ j = i - LOG_BASE + 1;
1480
+ } else {
1481
+ break out;
1482
+ }
1483
+ } else {
1484
+ n = k = xc[ni];
1485
+
1486
+ // Get the number of digits of n.
1487
+ for (d = 1; k >= 10; k /= 10, d++);
1488
+
1489
+ // Get the index of rd within n.
1490
+ i %= LOG_BASE;
1491
+
1492
+ // Get the index of rd within n, adjusted for leading zeros.
1493
+ // The number of leading zeros of n is given by LOG_BASE - d.
1494
+ j = i - LOG_BASE + d;
1495
+
1496
+ // Get the rounding digit at index j of n.
1497
+ rd = j < 0 ? 0 : n / pows10[d - j - 1] % 10 | 0;
1498
+ }
1499
+ }
1500
+
1501
+ r = r || sd < 0 ||
1502
+
1503
+ // Are there any non-zero digits after the rounding digit?
1504
+ // The expression n % pows10[d - j - 1] returns all digits of n to the right
1505
+ // of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
1506
+ xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
1507
+
1508
+ r = rm < 4
1509
+ ? (rd || r) && (rm == 0 || rm == (x.s < 0 ? 3 : 2))
1510
+ : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 &&
1511
+
1512
+ // Check whether the digit to the left of the rounding digit is odd.
1513
+ ((i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10) & 1 ||
1514
+ rm == (x.s < 0 ? 8 : 7));
1515
+
1516
+ if (sd < 1 || !xc[0]) {
1517
+ xc.length = 0;
1518
+
1519
+ if (r) {
1520
+
1521
+ // Convert sd to decimal places.
1522
+ sd -= x.e + 1;
1523
+
1524
+ // 1, 0.1, 0.01, 0.001, 0.0001 etc.
1525
+ xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
1526
+ x.e = -sd || 0;
1527
+ } else {
1528
+
1529
+ // Zero.
1530
+ xc[0] = x.e = 0;
1531
+ }
1532
+
1533
+ return x;
1534
+ }
1535
+
1536
+ // Remove excess digits.
1537
+ if (i == 0) {
1538
+ xc.length = ni;
1539
+ k = 1;
1540
+ ni--;
1541
+ } else {
1542
+ xc.length = ni + 1;
1543
+ k = pows10[LOG_BASE - i];
1544
+
1545
+ // E.g. 56700 becomes 56000 if 7 is the rounding digit.
1546
+ // j > 0 means i > number of leading zeros of n.
1547
+ xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k : 0;
1548
+ }
1549
+
1550
+ // Round up?
1551
+ if (r) {
1552
+
1553
+ for (; ;) {
1554
+
1555
+ // If the digit to be rounded up is in the first element of xc...
1556
+ if (ni == 0) {
1557
+
1558
+ // i will be the length of xc[0] before k is added.
1559
+ for (i = 1, j = xc[0]; j >= 10; j /= 10, i++);
1560
+ j = xc[0] += k;
1561
+ for (k = 1; j >= 10; j /= 10, k++);
1562
+
1563
+ // if i != k the length has increased.
1564
+ if (i != k) {
1565
+ x.e++;
1566
+ if (xc[0] == BASE) xc[0] = 1;
1567
+ }
1568
+
1569
+ break;
1570
+ } else {
1571
+ xc[ni] += k;
1572
+ if (xc[ni] != BASE) break;
1573
+ xc[ni--] = 0;
1574
+ k = 1;
1575
+ }
1576
+ }
1577
+ }
1578
+
1579
+ // Remove trailing zeros.
1580
+ for (i = xc.length; xc[--i] === 0; xc.pop());
1581
+ }
1582
+
1583
+ // Overflow? Infinity.
1584
+ if (x.e > MAX_EXP) {
1585
+ x.c = x.e = null;
1586
+
1587
+ // Underflow? Zero.
1588
+ } else if (x.e < MIN_EXP) {
1589
+ x.c = [x.e = 0];
1590
+ }
1591
+ }
1592
+
1593
+ return x;
1594
+ }
1595
+
1596
+
1597
+ function valueOf(n) {
1598
+ var str,
1599
+ e = n.e;
1600
+
1601
+ if (e === null) return n.toString();
1602
+
1603
+ str = coeffToString(n.c);
1604
+
1605
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS
1606
+ ? toExponential(str, e)
1607
+ : toFixedPoint(str, e, '0');
1608
+
1609
+ return n.s < 0 ? '-' + str : str;
1610
+ }
1611
+
1612
+
1613
+ // PROTOTYPE/INSTANCE METHODS
1614
+
1615
+
1616
+ /*
1617
+ * Return a new BigNumber whose value is the absolute value of this BigNumber.
1618
+ */
1619
+ P.absoluteValue = P.abs = function () {
1620
+ var x = new BigNumber(this);
1621
+ if (x.s < 0) x.s = 1;
1622
+ return x;
1623
+ };
1624
+
1625
+
1626
+ /*
1627
+ * Return
1628
+ * 1 if the value of this BigNumber is greater than the value of BigNumber(y, b),
1629
+ * -1 if the value of this BigNumber is less than the value of BigNumber(y, b),
1630
+ * 0 if they have the same value,
1631
+ * or null if the value of either is NaN.
1632
+ */
1633
+ P.comparedTo = function (y, b) {
1634
+ return compare(this, new BigNumber(y, b));
1635
+ };
1636
+
1637
+
1638
+ /*
1639
+ * If dp is undefined or null or true or false, return the number of decimal places of the
1640
+ * value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
1641
+ *
1642
+ * Otherwise, if dp is a number, return a new BigNumber whose value is the value of this
1643
+ * BigNumber rounded to a maximum of dp decimal places using rounding mode rm, or
1644
+ * ROUNDING_MODE if rm is omitted.
1645
+ *
1646
+ * [dp] {number} Decimal places: integer, 0 to MAX inclusive.
1647
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
1648
+ *
1649
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
1650
+ */
1651
+ P.decimalPlaces = P.dp = function (dp, rm) {
1652
+ var c, n, v,
1653
+ x = this;
1654
+
1655
+ if (dp != null) {
1656
+ intCheck(dp, 0, MAX);
1657
+ if (rm == null) rm = ROUNDING_MODE;
1658
+ else intCheck(rm, 0, 8);
1659
+
1660
+ return round(new BigNumber(x), dp + x.e + 1, rm);
1661
+ }
1662
+
1663
+ if (!(c = x.c)) return null;
1664
+ n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
1665
+
1666
+ // Subtract the number of trailing zeros of the last number.
1667
+ if (v = c[v]) for (; v % 10 == 0; v /= 10, n--);
1668
+ if (n < 0) n = 0;
1669
+
1670
+ return n;
1671
+ };
1672
+
1673
+
1674
+ /*
1675
+ * n / 0 = I
1676
+ * n / N = N
1677
+ * n / I = 0
1678
+ * 0 / n = 0
1679
+ * 0 / 0 = N
1680
+ * 0 / N = N
1681
+ * 0 / I = 0
1682
+ * N / n = N
1683
+ * N / 0 = N
1684
+ * N / N = N
1685
+ * N / I = N
1686
+ * I / n = I
1687
+ * I / 0 = I
1688
+ * I / N = N
1689
+ * I / I = N
1690
+ *
1691
+ * Return a new BigNumber whose value is the value of this BigNumber divided by the value of
1692
+ * BigNumber(y, b), rounded according to DECIMAL_PLACES and ROUNDING_MODE.
1693
+ */
1694
+ P.dividedBy = P.div = function (y, b) {
1695
+ return div(this, new BigNumber(y, b), DECIMAL_PLACES, ROUNDING_MODE);
1696
+ };
1697
+
1698
+
1699
+ /*
1700
+ * Return a new BigNumber whose value is the integer part of dividing the value of this
1701
+ * BigNumber by the value of BigNumber(y, b).
1702
+ */
1703
+ P.dividedToIntegerBy = P.idiv = function (y, b) {
1704
+ return div(this, new BigNumber(y, b), 0, 1);
1705
+ };
1706
+
1707
+
1708
+ /*
1709
+ * Return a BigNumber whose value is the value of this BigNumber exponentiated by n.
1710
+ *
1711
+ * If m is present, return the result modulo m.
1712
+ * If n is negative round according to DECIMAL_PLACES and ROUNDING_MODE.
1713
+ * If POW_PRECISION is non-zero and m is not present, round to POW_PRECISION using ROUNDING_MODE.
1714
+ *
1715
+ * The modular power operation works efficiently when x, n, and m are integers, otherwise it
1716
+ * is equivalent to calculating x.exponentiatedBy(n).modulo(m) with a POW_PRECISION of 0.
1717
+ *
1718
+ * n {number|string|BigNumber} The exponent. An integer.
1719
+ * [m] {number|string|BigNumber} The modulus.
1720
+ *
1721
+ * '[BigNumber Error] Exponent not an integer: {n}'
1722
+ */
1723
+ P.exponentiatedBy = P.pow = function (n, m) {
1724
+ var half, isModExp, i, k, more, nIsBig, nIsNeg, nIsOdd, y,
1725
+ x = this;
1726
+
1727
+ n = new BigNumber(n);
1728
+
1729
+ // Allow NaN and ±Infinity, but not other non-integers.
1730
+ if (n.c && !n.isInteger()) {
1731
+ throw Error
1732
+ (bignumberError + 'Exponent not an integer: ' + valueOf(n));
1733
+ }
1734
+
1735
+ if (m != null) m = new BigNumber(m);
1736
+
1737
+ // Exponent of MAX_SAFE_INTEGER is 15.
1738
+ nIsBig = n.e > 14;
1739
+
1740
+ // If x is NaN, ±Infinity, ±0 or ±1, or n is ±Infinity, NaN or ±0.
1741
+ if (!x.c || !x.c[0] || x.c[0] == 1 && !x.e && x.c.length == 1 || !n.c || !n.c[0]) {
1742
+
1743
+ // The sign of the result of pow when x is negative depends on the evenness of n.
1744
+ // If +n overflows to ±Infinity, the evenness of n would be not be known.
1745
+ y = new BigNumber(Math.pow(+valueOf(x), nIsBig ? 2 - isOdd(n) : +valueOf(n)));
1746
+ return m ? y.mod(m) : y;
1747
+ }
1748
+
1749
+ nIsNeg = n.s < 0;
1750
+
1751
+ if (m) {
1752
+
1753
+ // x % m returns NaN if abs(m) is zero, or m is NaN.
1754
+ if (m.c ? !m.c[0] : !m.s) return new BigNumber(NaN);
1755
+
1756
+ isModExp = !nIsNeg && x.isInteger() && m.isInteger();
1757
+
1758
+ if (isModExp) x = x.mod(m);
1759
+
1760
+ // Overflow to ±Infinity: >=2**1e10 or >=1.0000024**1e15.
1761
+ // Underflow to ±0: <=0.79**1e10 or <=0.9999975**1e15.
1762
+ } else if (n.e > 9 && (x.e > 0 || x.e < -1 || (x.e == 0
1763
+ // [1, 240000000]
1764
+ ? x.c[0] > 1 || nIsBig && x.c[1] >= 24e7
1765
+ // [80000000000000] [99999750000000]
1766
+ : x.c[0] < 8e13 || nIsBig && x.c[0] <= 9999975e7))) {
1767
+
1768
+ // If x is negative and n is odd, k = -0, else k = 0.
1769
+ k = x.s < 0 && isOdd(n) ? -0 : 0;
1770
+
1771
+ // If x >= 1, k = ±Infinity.
1772
+ if (x.e > -1) k = 1 / k;
1773
+
1774
+ // If n is negative return ±0, else return ±Infinity.
1775
+ return new BigNumber(nIsNeg ? 1 / k : k);
1776
+
1777
+ } else if (POW_PRECISION) {
1778
+
1779
+ // Truncating each coefficient array to a length of k after each multiplication
1780
+ // equates to truncating significant digits to POW_PRECISION + [28, 41],
1781
+ // i.e. there will be a minimum of 28 guard digits retained.
1782
+ k = mathceil(POW_PRECISION / LOG_BASE + 2);
1783
+ }
1784
+
1785
+ if (nIsBig) {
1786
+ half = new BigNumber(0.5);
1787
+ if (nIsNeg) n.s = 1;
1788
+ nIsOdd = isOdd(n);
1789
+ } else {
1790
+ i = Math.abs(+valueOf(n));
1791
+ nIsOdd = i % 2;
1792
+ }
1793
+
1794
+ y = new BigNumber(ONE);
1795
+
1796
+ // Performs 54 loop iterations for n of 9007199254740991.
1797
+ for (; ;) {
1798
+
1799
+ if (nIsOdd) {
1800
+ y = y.times(x);
1801
+ if (!y.c) break;
1802
+
1803
+ if (k) {
1804
+ if (y.c.length > k) y.c.length = k;
1805
+ } else if (isModExp) {
1806
+ y = y.mod(m); //y = y.minus(div(y, m, 0, MODULO_MODE).times(m));
1807
+ }
1808
+ }
1809
+
1810
+ if (i) {
1811
+ i = mathfloor(i / 2);
1812
+ if (i === 0) break;
1813
+ nIsOdd = i % 2;
1814
+ } else {
1815
+ n = n.times(half);
1816
+ round(n, n.e + 1, 1);
1817
+
1818
+ if (n.e > 14) {
1819
+ nIsOdd = isOdd(n);
1820
+ } else {
1821
+ i = +valueOf(n);
1822
+ if (i === 0) break;
1823
+ nIsOdd = i % 2;
1824
+ }
1825
+ }
1826
+
1827
+ x = x.times(x);
1828
+
1829
+ if (k) {
1830
+ if (x.c && x.c.length > k) x.c.length = k;
1831
+ } else if (isModExp) {
1832
+ x = x.mod(m); //x = x.minus(div(x, m, 0, MODULO_MODE).times(m));
1833
+ }
1834
+ }
1835
+
1836
+ if (isModExp) return y;
1837
+ if (nIsNeg) y = ONE.div(y);
1838
+
1839
+ return m ? y.mod(m) : k ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
1840
+ };
1841
+
1842
+
1843
+ /*
1844
+ * Return a new BigNumber whose value is the value of this BigNumber rounded to an integer
1845
+ * using rounding mode rm, or ROUNDING_MODE if rm is omitted.
1846
+ *
1847
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
1848
+ *
1849
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {rm}'
1850
+ */
1851
+ P.integerValue = function (rm) {
1852
+ var n = new BigNumber(this);
1853
+ if (rm == null) rm = ROUNDING_MODE;
1854
+ else intCheck(rm, 0, 8);
1855
+ return round(n, n.e + 1, rm);
1856
+ };
1857
+
1858
+
1859
+ /*
1860
+ * Return true if the value of this BigNumber is equal to the value of BigNumber(y, b),
1861
+ * otherwise return false.
1862
+ */
1863
+ P.isEqualTo = P.eq = function (y, b) {
1864
+ return compare(this, new BigNumber(y, b)) === 0;
1865
+ };
1866
+
1867
+
1868
+ /*
1869
+ * Return true if the value of this BigNumber is a finite number, otherwise return false.
1870
+ */
1871
+ P.isFinite = function () {
1872
+ return !!this.c;
1873
+ };
1874
+
1875
+
1876
+ /*
1877
+ * Return true if the value of this BigNumber is greater than the value of BigNumber(y, b),
1878
+ * otherwise return false.
1879
+ */
1880
+ P.isGreaterThan = P.gt = function (y, b) {
1881
+ return compare(this, new BigNumber(y, b)) > 0;
1882
+ };
1883
+
1884
+
1885
+ /*
1886
+ * Return true if the value of this BigNumber is greater than or equal to the value of
1887
+ * BigNumber(y, b), otherwise return false.
1888
+ */
1889
+ P.isGreaterThanOrEqualTo = P.gte = function (y, b) {
1890
+ return (b = compare(this, new BigNumber(y, b))) === 1 || b === 0;
1891
+
1892
+ };
1893
+
1894
+
1895
+ /*
1896
+ * Return true if the value of this BigNumber is an integer, otherwise return false.
1897
+ */
1898
+ P.isInteger = function () {
1899
+ return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
1900
+ };
1901
+
1902
+
1903
+ /*
1904
+ * Return true if the value of this BigNumber is less than the value of BigNumber(y, b),
1905
+ * otherwise return false.
1906
+ */
1907
+ P.isLessThan = P.lt = function (y, b) {
1908
+ return compare(this, new BigNumber(y, b)) < 0;
1909
+ };
1910
+
1911
+
1912
+ /*
1913
+ * Return true if the value of this BigNumber is less than or equal to the value of
1914
+ * BigNumber(y, b), otherwise return false.
1915
+ */
1916
+ P.isLessThanOrEqualTo = P.lte = function (y, b) {
1917
+ return (b = compare(this, new BigNumber(y, b))) === -1 || b === 0;
1918
+ };
1919
+
1920
+
1921
+ /*
1922
+ * Return true if the value of this BigNumber is NaN, otherwise return false.
1923
+ */
1924
+ P.isNaN = function () {
1925
+ return !this.s;
1926
+ };
1927
+
1928
+
1929
+ /*
1930
+ * Return true if the value of this BigNumber is negative, otherwise return false.
1931
+ */
1932
+ P.isNegative = function () {
1933
+ return this.s < 0;
1934
+ };
1935
+
1936
+
1937
+ /*
1938
+ * Return true if the value of this BigNumber is positive, otherwise return false.
1939
+ */
1940
+ P.isPositive = function () {
1941
+ return this.s > 0;
1942
+ };
1943
+
1944
+
1945
+ /*
1946
+ * Return true if the value of this BigNumber is 0 or -0, otherwise return false.
1947
+ */
1948
+ P.isZero = function () {
1949
+ return !!this.c && this.c[0] == 0;
1950
+ };
1951
+
1952
+
1953
+ /*
1954
+ * n - 0 = n
1955
+ * n - N = N
1956
+ * n - I = -I
1957
+ * 0 - n = -n
1958
+ * 0 - 0 = 0
1959
+ * 0 - N = N
1960
+ * 0 - I = -I
1961
+ * N - n = N
1962
+ * N - 0 = N
1963
+ * N - N = N
1964
+ * N - I = N
1965
+ * I - n = I
1966
+ * I - 0 = I
1967
+ * I - N = N
1968
+ * I - I = N
1969
+ *
1970
+ * Return a new BigNumber whose value is the value of this BigNumber minus the value of
1971
+ * BigNumber(y, b).
1972
+ */
1973
+ P.minus = function (y, b) {
1974
+ var i, j, t, xLTy,
1975
+ x = this,
1976
+ a = x.s;
1977
+
1978
+ y = new BigNumber(y, b);
1979
+ b = y.s;
1980
+
1981
+ // Either NaN?
1982
+ if (!a || !b) return new BigNumber(NaN);
1983
+
1984
+ // Signs differ?
1985
+ if (a != b) {
1986
+ y.s = -b;
1987
+ return x.plus(y);
1988
+ }
1989
+
1990
+ var xe = x.e / LOG_BASE,
1991
+ ye = y.e / LOG_BASE,
1992
+ xc = x.c,
1993
+ yc = y.c;
1994
+
1995
+ if (!xe || !ye) {
1996
+
1997
+ // Either Infinity?
1998
+ if (!xc || !yc) return xc ? (y.s = -b, y) : new BigNumber(yc ? x : NaN);
1999
+
2000
+ // Either zero?
2001
+ if (!xc[0] || !yc[0]) {
2002
+
2003
+ // Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
2004
+ return yc[0] ? (y.s = -b, y) : new BigNumber(xc[0] ? x :
2005
+
2006
+ // IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
2007
+ ROUNDING_MODE == 3 ? -0 : 0);
2008
+ }
2009
+ }
2010
+
2011
+ xe = bitFloor(xe);
2012
+ ye = bitFloor(ye);
2013
+ xc = xc.slice();
2014
+
2015
+ // Determine which is the bigger number.
2016
+ if (a = xe - ye) {
2017
+
2018
+ if (xLTy = a < 0) {
2019
+ a = -a;
2020
+ t = xc;
2021
+ } else {
2022
+ ye = xe;
2023
+ t = yc;
2024
+ }
2025
+
2026
+ t.reverse();
2027
+
2028
+ // Prepend zeros to equalise exponents.
2029
+ for (b = a; b--; t.push(0));
2030
+ t.reverse();
2031
+ } else {
2032
+
2033
+ // Exponents equal. Check digit by digit.
2034
+ j = (xLTy = (a = xc.length) < (b = yc.length)) ? a : b;
2035
+
2036
+ for (a = b = 0; b < j; b++) {
2037
+
2038
+ if (xc[b] != yc[b]) {
2039
+ xLTy = xc[b] < yc[b];
2040
+ break;
2041
+ }
2042
+ }
2043
+ }
2044
+
2045
+ // x < y? Point xc to the array of the bigger number.
2046
+ if (xLTy) t = xc, xc = yc, yc = t, y.s = -y.s;
2047
+
2048
+ b = (j = yc.length) - (i = xc.length);
2049
+
2050
+ // Append zeros to xc if shorter.
2051
+ // No need to add zeros to yc if shorter as subtract only needs to start at yc.length.
2052
+ if (b > 0) for (; b--; xc[i++] = 0);
2053
+ b = BASE - 1;
2054
+
2055
+ // Subtract yc from xc.
2056
+ for (; j > a;) {
2057
+
2058
+ if (xc[--j] < yc[j]) {
2059
+ for (i = j; i && !xc[--i]; xc[i] = b);
2060
+ --xc[i];
2061
+ xc[j] += BASE;
2062
+ }
2063
+
2064
+ xc[j] -= yc[j];
2065
+ }
2066
+
2067
+ // Remove leading zeros and adjust exponent accordingly.
2068
+ for (; xc[0] == 0; xc.splice(0, 1), --ye);
2069
+
2070
+ // Zero?
2071
+ if (!xc[0]) {
2072
+
2073
+ // Following IEEE 754 (2008) 6.3,
2074
+ // n - n = +0 but n - n = -0 when rounding towards -Infinity.
2075
+ y.s = ROUNDING_MODE == 3 ? -1 : 1;
2076
+ y.c = [y.e = 0];
2077
+ return y;
2078
+ }
2079
+
2080
+ // No need to check for Infinity as +x - +y != Infinity && -x - -y != Infinity
2081
+ // for finite x and y.
2082
+ return normalise(y, xc, ye);
2083
+ };
2084
+
2085
+
2086
+ /*
2087
+ * n % 0 = N
2088
+ * n % N = N
2089
+ * n % I = n
2090
+ * 0 % n = 0
2091
+ * -0 % n = -0
2092
+ * 0 % 0 = N
2093
+ * 0 % N = N
2094
+ * 0 % I = 0
2095
+ * N % n = N
2096
+ * N % 0 = N
2097
+ * N % N = N
2098
+ * N % I = N
2099
+ * I % n = N
2100
+ * I % 0 = N
2101
+ * I % N = N
2102
+ * I % I = N
2103
+ *
2104
+ * Return a new BigNumber whose value is the value of this BigNumber modulo the value of
2105
+ * BigNumber(y, b). The result depends on the value of MODULO_MODE.
2106
+ */
2107
+ P.modulo = P.mod = function (y, b) {
2108
+ var q, s,
2109
+ x = this;
2110
+
2111
+ y = new BigNumber(y, b);
2112
+
2113
+ // Return NaN if x is Infinity or NaN, or y is NaN or zero.
2114
+ if (!x.c || !y.s || y.c && !y.c[0]) {
2115
+ return new BigNumber(NaN);
2116
+
2117
+ // Return x if y is Infinity or x is zero.
2118
+ } else if (!y.c || x.c && !x.c[0]) {
2119
+ return new BigNumber(x);
2120
+ }
2121
+
2122
+ if (MODULO_MODE == 9) {
2123
+
2124
+ // Euclidian division: q = sign(y) * floor(x / abs(y))
2125
+ // r = x - qy where 0 <= r < abs(y)
2126
+ s = y.s;
2127
+ y.s = 1;
2128
+ q = div(x, y, 0, 3);
2129
+ y.s = s;
2130
+ q.s *= s;
2131
+ } else {
2132
+ q = div(x, y, 0, MODULO_MODE);
2133
+ }
2134
+
2135
+ y = x.minus(q.times(y));
2136
+
2137
+ // To match JavaScript %, ensure sign of zero is sign of dividend.
2138
+ if (!y.c[0] && MODULO_MODE == 1) y.s = x.s;
2139
+
2140
+ return y;
2141
+ };
2142
+
2143
+
2144
+ /*
2145
+ * n * 0 = 0
2146
+ * n * N = N
2147
+ * n * I = I
2148
+ * 0 * n = 0
2149
+ * 0 * 0 = 0
2150
+ * 0 * N = N
2151
+ * 0 * I = N
2152
+ * N * n = N
2153
+ * N * 0 = N
2154
+ * N * N = N
2155
+ * N * I = N
2156
+ * I * n = I
2157
+ * I * 0 = N
2158
+ * I * N = N
2159
+ * I * I = I
2160
+ *
2161
+ * Return a new BigNumber whose value is the value of this BigNumber multiplied by the value
2162
+ * of BigNumber(y, b).
2163
+ */
2164
+ P.multipliedBy = P.times = function (y, b) {
2165
+ var c, e, i, j, k, m, xcL, xlo, xhi, ycL, ylo, yhi, zc,
2166
+ base, sqrtBase,
2167
+ x = this,
2168
+ xc = x.c,
2169
+ yc = (y = new BigNumber(y, b)).c;
2170
+
2171
+ // Either NaN, ±Infinity or ±0?
2172
+ if (!xc || !yc || !xc[0] || !yc[0]) {
2173
+
2174
+ // Return NaN if either is NaN, or one is 0 and the other is Infinity.
2175
+ if (!x.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
2176
+ y.c = y.e = y.s = null;
2177
+ } else {
2178
+ y.s *= x.s;
2179
+
2180
+ // Return ±Infinity if either is ±Infinity.
2181
+ if (!xc || !yc) {
2182
+ y.c = y.e = null;
2183
+
2184
+ // Return ±0 if either is ±0.
2185
+ } else {
2186
+ y.c = [0];
2187
+ y.e = 0;
2188
+ }
2189
+ }
2190
+
2191
+ return y;
2192
+ }
2193
+
2194
+ e = bitFloor(x.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
2195
+ y.s *= x.s;
2196
+ xcL = xc.length;
2197
+ ycL = yc.length;
2198
+
2199
+ // Ensure xc points to longer array and xcL to its length.
2200
+ if (xcL < ycL) zc = xc, xc = yc, yc = zc, i = xcL, xcL = ycL, ycL = i;
2201
+
2202
+ // Initialise the result array with zeros.
2203
+ for (i = xcL + ycL, zc = []; i--; zc.push(0));
2204
+
2205
+ base = BASE;
2206
+ sqrtBase = SQRT_BASE;
2207
+
2208
+ for (i = ycL; --i >= 0;) {
2209
+ c = 0;
2210
+ ylo = yc[i] % sqrtBase;
2211
+ yhi = yc[i] / sqrtBase | 0;
2212
+
2213
+ for (k = xcL, j = i + k; j > i;) {
2214
+ xlo = xc[--k] % sqrtBase;
2215
+ xhi = xc[k] / sqrtBase | 0;
2216
+ m = yhi * xlo + xhi * ylo;
2217
+ xlo = ylo * xlo + ((m % sqrtBase) * sqrtBase) + zc[j] + c;
2218
+ c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
2219
+ zc[j--] = xlo % base;
2220
+ }
2221
+
2222
+ zc[j] = c;
2223
+ }
2224
+
2225
+ if (c) {
2226
+ ++e;
2227
+ } else {
2228
+ zc.splice(0, 1);
2229
+ }
2230
+
2231
+ return normalise(y, zc, e);
2232
+ };
2233
+
2234
+
2235
+ /*
2236
+ * Return a new BigNumber whose value is the value of this BigNumber negated,
2237
+ * i.e. multiplied by -1.
2238
+ */
2239
+ P.negated = function () {
2240
+ var x = new BigNumber(this);
2241
+ x.s = -x.s || null;
2242
+ return x;
2243
+ };
2244
+
2245
+
2246
+ /*
2247
+ * n + 0 = n
2248
+ * n + N = N
2249
+ * n + I = I
2250
+ * 0 + n = n
2251
+ * 0 + 0 = 0
2252
+ * 0 + N = N
2253
+ * 0 + I = I
2254
+ * N + n = N
2255
+ * N + 0 = N
2256
+ * N + N = N
2257
+ * N + I = N
2258
+ * I + n = I
2259
+ * I + 0 = I
2260
+ * I + N = N
2261
+ * I + I = I
2262
+ *
2263
+ * Return a new BigNumber whose value is the value of this BigNumber plus the value of
2264
+ * BigNumber(y, b).
2265
+ */
2266
+ P.plus = function (y, b) {
2267
+ var t,
2268
+ x = this,
2269
+ a = x.s;
2270
+
2271
+ y = new BigNumber(y, b);
2272
+ b = y.s;
2273
+
2274
+ // Either NaN?
2275
+ if (!a || !b) return new BigNumber(NaN);
2276
+
2277
+ // Signs differ?
2278
+ if (a != b) {
2279
+ y.s = -b;
2280
+ return x.minus(y);
2281
+ }
2282
+
2283
+ var xe = x.e / LOG_BASE,
2284
+ ye = y.e / LOG_BASE,
2285
+ xc = x.c,
2286
+ yc = y.c;
2287
+
2288
+ if (!xe || !ye) {
2289
+
2290
+ // Return ±Infinity if either ±Infinity.
2291
+ if (!xc || !yc) return new BigNumber(a / 0);
2292
+
2293
+ // Either zero?
2294
+ // Return y if y is non-zero, x if x is non-zero, or zero if both are zero.
2295
+ if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber(xc[0] ? x : a * 0);
2296
+ }
2297
+
2298
+ xe = bitFloor(xe);
2299
+ ye = bitFloor(ye);
2300
+ xc = xc.slice();
2301
+
2302
+ // Prepend zeros to equalise exponents. Faster to use reverse then do unshifts.
2303
+ if (a = xe - ye) {
2304
+ if (a > 0) {
2305
+ ye = xe;
2306
+ t = yc;
2307
+ } else {
2308
+ a = -a;
2309
+ t = xc;
2310
+ }
2311
+
2312
+ t.reverse();
2313
+ for (; a--; t.push(0));
2314
+ t.reverse();
2315
+ }
2316
+
2317
+ a = xc.length;
2318
+ b = yc.length;
2319
+
2320
+ // Point xc to the longer array, and b to the shorter length.
2321
+ if (a - b < 0) t = yc, yc = xc, xc = t, b = a;
2322
+
2323
+ // Only start adding at yc.length - 1 as the further digits of xc can be ignored.
2324
+ for (a = 0; b;) {
2325
+ a = (xc[--b] = xc[b] + yc[b] + a) / BASE | 0;
2326
+ xc[b] = BASE === xc[b] ? 0 : xc[b] % BASE;
2327
+ }
2328
+
2329
+ if (a) {
2330
+ xc = [a].concat(xc);
2331
+ ++ye;
2332
+ }
2333
+
2334
+ // No need to check for zero, as +x + +y != 0 && -x + -y != 0
2335
+ // ye = MAX_EXP + 1 possible
2336
+ return normalise(y, xc, ye);
2337
+ };
2338
+
2339
+
2340
+ /*
2341
+ * If sd is undefined or null or true or false, return the number of significant digits of
2342
+ * the value of this BigNumber, or null if the value of this BigNumber is ±Infinity or NaN.
2343
+ * If sd is true include integer-part trailing zeros in the count.
2344
+ *
2345
+ * Otherwise, if sd is a number, return a new BigNumber whose value is the value of this
2346
+ * BigNumber rounded to a maximum of sd significant digits using rounding mode rm, or
2347
+ * ROUNDING_MODE if rm is omitted.
2348
+ *
2349
+ * sd {number|boolean} number: significant digits: integer, 1 to MAX inclusive.
2350
+ * boolean: whether to count integer-part trailing zeros: true or false.
2351
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2352
+ *
2353
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
2354
+ */
2355
+ P.precision = P.sd = function (sd, rm) {
2356
+ var c, n, v,
2357
+ x = this;
2358
+
2359
+ if (sd != null && sd !== !!sd) {
2360
+ intCheck(sd, 1, MAX);
2361
+ if (rm == null) rm = ROUNDING_MODE;
2362
+ else intCheck(rm, 0, 8);
2363
+
2364
+ return round(new BigNumber(x), sd, rm);
2365
+ }
2366
+
2367
+ if (!(c = x.c)) return null;
2368
+ v = c.length - 1;
2369
+ n = v * LOG_BASE + 1;
2370
+
2371
+ if (v = c[v]) {
2372
+
2373
+ // Subtract the number of trailing zeros of the last element.
2374
+ for (; v % 10 == 0; v /= 10, n--);
2375
+
2376
+ // Add the number of digits of the first element.
2377
+ for (v = c[0]; v >= 10; v /= 10, n++);
2378
+ }
2379
+
2380
+ if (sd && x.e + 1 > n) n = x.e + 1;
2381
+
2382
+ return n;
2383
+ };
2384
+
2385
+
2386
+ /*
2387
+ * Return a new BigNumber whose value is the value of this BigNumber shifted by k places
2388
+ * (powers of 10). Shift to the right if n > 0, and to the left if n < 0.
2389
+ *
2390
+ * k {number} Integer, -MAX_SAFE_INTEGER to MAX_SAFE_INTEGER inclusive.
2391
+ *
2392
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {k}'
2393
+ */
2394
+ P.shiftedBy = function (k) {
2395
+ intCheck(k, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
2396
+ return this.times('1e' + k);
2397
+ };
2398
+
2399
+
2400
+ /*
2401
+ * sqrt(-n) = N
2402
+ * sqrt(N) = N
2403
+ * sqrt(-I) = N
2404
+ * sqrt(I) = I
2405
+ * sqrt(0) = 0
2406
+ * sqrt(-0) = -0
2407
+ *
2408
+ * Return a new BigNumber whose value is the square root of the value of this BigNumber,
2409
+ * rounded according to DECIMAL_PLACES and ROUNDING_MODE.
2410
+ */
2411
+ P.squareRoot = P.sqrt = function () {
2412
+ var m, n, r, rep, t,
2413
+ x = this,
2414
+ c = x.c,
2415
+ s = x.s,
2416
+ e = x.e,
2417
+ dp = DECIMAL_PLACES + 4,
2418
+ half = new BigNumber('0.5');
2419
+
2420
+ // Negative/NaN/Infinity/zero?
2421
+ if (s !== 1 || !c || !c[0]) {
2422
+ return new BigNumber(!s || s < 0 && (!c || c[0]) ? NaN : c ? x : 1 / 0);
2423
+ }
2424
+
2425
+ // Initial estimate.
2426
+ s = Math.sqrt(+valueOf(x));
2427
+
2428
+ // Math.sqrt underflow/overflow?
2429
+ // Pass x to Math.sqrt as integer, then adjust the exponent of the result.
2430
+ if (s == 0 || s == 1 / 0) {
2431
+ n = coeffToString(c);
2432
+ if ((n.length + e) % 2 == 0) n += '0';
2433
+ s = Math.sqrt(+n);
2434
+ e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
2435
+
2436
+ if (s == 1 / 0) {
2437
+ n = '5e' + e;
2438
+ } else {
2439
+ n = s.toExponential();
2440
+ n = n.slice(0, n.indexOf('e') + 1) + e;
2441
+ }
2442
+
2443
+ r = new BigNumber(n);
2444
+ } else {
2445
+ r = new BigNumber(s + '');
2446
+ }
2447
+
2448
+ // Check for zero.
2449
+ // r could be zero if MIN_EXP is changed after the this value was created.
2450
+ // This would cause a division by zero (x/t) and hence Infinity below, which would cause
2451
+ // coeffToString to throw.
2452
+ if (r.c[0]) {
2453
+ e = r.e;
2454
+ s = e + dp;
2455
+ if (s < 3) s = 0;
2456
+
2457
+ // Newton-Raphson iteration.
2458
+ for (; ;) {
2459
+ t = r;
2460
+ r = half.times(t.plus(div(x, t, dp, 1)));
2461
+
2462
+ if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
2463
+
2464
+ // The exponent of r may here be one less than the final result exponent,
2465
+ // e.g 0.0009999 (e-4) --> 0.001 (e-3), so adjust s so the rounding digits
2466
+ // are indexed correctly.
2467
+ if (r.e < e) --s;
2468
+ n = n.slice(s - 3, s + 1);
2469
+
2470
+ // The 4th rounding digit may be in error by -1 so if the 4 rounding digits
2471
+ // are 9999 or 4999 (i.e. approaching a rounding boundary) continue the
2472
+ // iteration.
2473
+ if (n == '9999' || !rep && n == '4999') {
2474
+
2475
+ // On the first iteration only, check to see if rounding up gives the
2476
+ // exact result as the nines may infinitely repeat.
2477
+ if (!rep) {
2478
+ round(t, t.e + DECIMAL_PLACES + 2, 0);
2479
+
2480
+ if (t.times(t).eq(x)) {
2481
+ r = t;
2482
+ break;
2483
+ }
2484
+ }
2485
+
2486
+ dp += 4;
2487
+ s += 4;
2488
+ rep = 1;
2489
+ } else {
2490
+
2491
+ // If rounding digits are null, 0{0,4} or 50{0,3}, check for exact
2492
+ // result. If not, then there are further digits and m will be truthy.
2493
+ if (!+n || !+n.slice(1) && n.charAt(0) == '5') {
2494
+
2495
+ // Truncate to the first rounding digit.
2496
+ round(r, r.e + DECIMAL_PLACES + 2, 1);
2497
+ m = !r.times(r).eq(x);
2498
+ }
2499
+
2500
+ break;
2501
+ }
2502
+ }
2503
+ }
2504
+ }
2505
+
2506
+ return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
2507
+ };
2508
+
2509
+
2510
+ /*
2511
+ * Return a string representing the value of this BigNumber in exponential notation and
2512
+ * rounded using ROUNDING_MODE to dp fixed decimal places.
2513
+ *
2514
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
2515
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2516
+ *
2517
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
2518
+ */
2519
+ P.toExponential = function (dp, rm) {
2520
+ if (dp != null) {
2521
+ intCheck(dp, 0, MAX);
2522
+ dp++;
2523
+ }
2524
+ return format(this, dp, rm, 1);
2525
+ };
2526
+
2527
+
2528
+ /*
2529
+ * Return a string representing the value of this BigNumber in fixed-point notation rounding
2530
+ * to dp fixed decimal places using rounding mode rm, or ROUNDING_MODE if rm is omitted.
2531
+ *
2532
+ * Note: as with JavaScript's number type, (-0).toFixed(0) is '0',
2533
+ * but e.g. (-0.00001).toFixed(0) is '-0'.
2534
+ *
2535
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
2536
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2537
+ *
2538
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
2539
+ */
2540
+ P.toFixed = function (dp, rm) {
2541
+ if (dp != null) {
2542
+ intCheck(dp, 0, MAX);
2543
+ dp = dp + this.e + 1;
2544
+ }
2545
+ return format(this, dp, rm);
2546
+ };
2547
+
2548
+
2549
+ /*
2550
+ * Return a string representing the value of this BigNumber in fixed-point notation rounded
2551
+ * using rm or ROUNDING_MODE to dp decimal places, and formatted according to the properties
2552
+ * of the format or FORMAT object (see BigNumber.set).
2553
+ *
2554
+ * The formatting object may contain some or all of the properties shown below.
2555
+ *
2556
+ * FORMAT = {
2557
+ * prefix: '',
2558
+ * groupSize: 3,
2559
+ * secondaryGroupSize: 0,
2560
+ * groupSeparator: ',',
2561
+ * decimalSeparator: '.',
2562
+ * fractionGroupSize: 0,
2563
+ * fractionGroupSeparator: '\xA0', // non-breaking space
2564
+ * suffix: ''
2565
+ * };
2566
+ *
2567
+ * [dp] {number} Decimal places. Integer, 0 to MAX inclusive.
2568
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2569
+ * [format] {object} Formatting options. See FORMAT pbject above.
2570
+ *
2571
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {dp|rm}'
2572
+ * '[BigNumber Error] Argument not an object: {format}'
2573
+ */
2574
+ P.toFormat = function (dp, rm, format) {
2575
+ var str,
2576
+ x = this;
2577
+
2578
+ if (format == null) {
2579
+ if (dp != null && rm && typeof rm == 'object') {
2580
+ format = rm;
2581
+ rm = null;
2582
+ } else if (dp && typeof dp == 'object') {
2583
+ format = dp;
2584
+ dp = rm = null;
2585
+ } else {
2586
+ format = FORMAT;
2587
+ }
2588
+ } else if (typeof format != 'object') {
2589
+ throw Error
2590
+ (bignumberError + 'Argument not an object: ' + format);
2591
+ }
2592
+
2593
+ str = x.toFixed(dp, rm);
2594
+
2595
+ if (x.c) {
2596
+ var i,
2597
+ arr = str.split('.'),
2598
+ g1 = +format.groupSize,
2599
+ g2 = +format.secondaryGroupSize,
2600
+ groupSeparator = format.groupSeparator || '',
2601
+ intPart = arr[0],
2602
+ fractionPart = arr[1],
2603
+ isNeg = x.s < 0,
2604
+ intDigits = isNeg ? intPart.slice(1) : intPart,
2605
+ len = intDigits.length;
2606
+
2607
+ if (g2) i = g1, g1 = g2, g2 = i, len -= i;
2608
+
2609
+ if (g1 > 0 && len > 0) {
2610
+ i = len % g1 || g1;
2611
+ intPart = intDigits.substr(0, i);
2612
+ for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
2613
+ if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
2614
+ if (isNeg) intPart = '-' + intPart;
2615
+ }
2616
+
2617
+ str = fractionPart
2618
+ ? intPart + (format.decimalSeparator || '') + ((g2 = +format.fractionGroupSize)
2619
+ ? fractionPart.replace(new RegExp('\\d{' + g2 + '}\\B', 'g'),
2620
+ '$&' + (format.fractionGroupSeparator || ''))
2621
+ : fractionPart)
2622
+ : intPart;
2623
+ }
2624
+
2625
+ return (format.prefix || '') + str + (format.suffix || '');
2626
+ };
2627
+
2628
+
2629
+ /*
2630
+ * Return an array of two BigNumbers representing the value of this BigNumber as a simple
2631
+ * fraction with an integer numerator and an integer denominator.
2632
+ * The denominator will be a positive non-zero value less than or equal to the specified
2633
+ * maximum denominator. If a maximum denominator is not specified, the denominator will be
2634
+ * the lowest value necessary to represent the number exactly.
2635
+ *
2636
+ * [md] {number|string|BigNumber} Integer >= 1, or Infinity. The maximum denominator.
2637
+ *
2638
+ * '[BigNumber Error] Argument {not an integer|out of range} : {md}'
2639
+ */
2640
+ P.toFraction = function (md) {
2641
+ var d, d0, d1, d2, e, exp, n, n0, n1, q, r, s,
2642
+ x = this,
2643
+ xc = x.c;
2644
+
2645
+ if (md != null) {
2646
+ n = new BigNumber(md);
2647
+
2648
+ // Throw if md is less than one or is not an integer, unless it is Infinity.
2649
+ if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
2650
+ throw Error
2651
+ (bignumberError + 'Argument ' +
2652
+ (n.isInteger() ? 'out of range: ' : 'not an integer: ') + valueOf(n));
2653
+ }
2654
+ }
2655
+
2656
+ if (!xc) return new BigNumber(x);
2657
+
2658
+ d = new BigNumber(ONE);
2659
+ n1 = d0 = new BigNumber(ONE);
2660
+ d1 = n0 = new BigNumber(ONE);
2661
+ s = coeffToString(xc);
2662
+
2663
+ // Determine initial denominator.
2664
+ // d is a power of 10 and the minimum max denominator that specifies the value exactly.
2665
+ e = d.e = s.length - x.e - 1;
2666
+ d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
2667
+ md = !md || n.comparedTo(d) > 0 ? (e > 0 ? d : n1) : n;
2668
+
2669
+ exp = MAX_EXP;
2670
+ MAX_EXP = 1 / 0;
2671
+ n = new BigNumber(s);
2672
+
2673
+ // n0 = d1 = 0
2674
+ n0.c[0] = 0;
2675
+
2676
+ for (; ;) {
2677
+ q = div(n, d, 0, 1);
2678
+ d2 = d0.plus(q.times(d1));
2679
+ if (d2.comparedTo(md) == 1) break;
2680
+ d0 = d1;
2681
+ d1 = d2;
2682
+ n1 = n0.plus(q.times(d2 = n1));
2683
+ n0 = d2;
2684
+ d = n.minus(q.times(d2 = d));
2685
+ n = d2;
2686
+ }
2687
+
2688
+ d2 = div(md.minus(d0), d1, 0, 1);
2689
+ n0 = n0.plus(d2.times(n1));
2690
+ d0 = d0.plus(d2.times(d1));
2691
+ n0.s = n1.s = x.s;
2692
+ e = e * 2;
2693
+
2694
+ // Determine which fraction is closer to x, n0/d0 or n1/d1
2695
+ r = div(n1, d1, e, ROUNDING_MODE).minus(x).abs().comparedTo(
2696
+ div(n0, d0, e, ROUNDING_MODE).minus(x).abs()) < 1 ? [n1, d1] : [n0, d0];
2697
+
2698
+ MAX_EXP = exp;
2699
+
2700
+ return r;
2701
+ };
2702
+
2703
+
2704
+ /*
2705
+ * Return the value of this BigNumber converted to a number primitive.
2706
+ */
2707
+ P.toNumber = function () {
2708
+ return +valueOf(this);
2709
+ };
2710
+
2711
+
2712
+ /*
2713
+ * Return a string representing the value of this BigNumber rounded to sd significant digits
2714
+ * using rounding mode rm or ROUNDING_MODE. If sd is less than the number of digits
2715
+ * necessary to represent the integer part of the value in fixed-point notation, then use
2716
+ * exponential notation.
2717
+ *
2718
+ * [sd] {number} Significant digits. Integer, 1 to MAX inclusive.
2719
+ * [rm] {number} Rounding mode. Integer, 0 to 8 inclusive.
2720
+ *
2721
+ * '[BigNumber Error] Argument {not a primitive number|not an integer|out of range}: {sd|rm}'
2722
+ */
2723
+ P.toPrecision = function (sd, rm) {
2724
+ if (sd != null) intCheck(sd, 1, MAX);
2725
+ return format(this, sd, rm, 2);
2726
+ };
2727
+
2728
+
2729
+ /*
2730
+ * Return a string representing the value of this BigNumber in base b, or base 10 if b is
2731
+ * omitted. If a base is specified, including base 10, round according to DECIMAL_PLACES and
2732
+ * ROUNDING_MODE. If a base is not specified, and this BigNumber has a positive exponent
2733
+ * that is equal to or greater than TO_EXP_POS, or a negative exponent equal to or less than
2734
+ * TO_EXP_NEG, return exponential notation.
2735
+ *
2736
+ * [b] {number} Integer, 2 to ALPHABET.length inclusive.
2737
+ *
2738
+ * '[BigNumber Error] Base {not a primitive number|not an integer|out of range}: {b}'
2739
+ */
2740
+ P.toString = function (b) {
2741
+ var str,
2742
+ n = this,
2743
+ s = n.s,
2744
+ e = n.e;
2745
+
2746
+ // Infinity or NaN?
2747
+ if (e === null) {
2748
+ if (s) {
2749
+ str = 'Infinity';
2750
+ if (s < 0) str = '-' + str;
2751
+ } else {
2752
+ str = 'NaN';
2753
+ }
2754
+ } else {
2755
+ if (b == null) {
2756
+ str = e <= TO_EXP_NEG || e >= TO_EXP_POS
2757
+ ? toExponential(coeffToString(n.c), e)
2758
+ : toFixedPoint(coeffToString(n.c), e, '0');
2759
+ } else if (b === 10) {
2760
+ n = round(new BigNumber(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
2761
+ str = toFixedPoint(coeffToString(n.c), n.e, '0');
2762
+ } else {
2763
+ intCheck(b, 2, ALPHABET.length, 'Base');
2764
+ str = convertBase(toFixedPoint(coeffToString(n.c), e, '0'), 10, b, s, true);
2765
+ }
2766
+
2767
+ if (s < 0 && n.c[0]) str = '-' + str;
2768
+ }
2769
+
2770
+ return str;
2771
+ };
2772
+
2773
+
2774
+ /*
2775
+ * Return as toString, but do not accept a base argument, and include the minus sign for
2776
+ * negative zero.
2777
+ */
2778
+ P.valueOf = P.toJSON = function () {
2779
+ return valueOf(this);
2780
+ };
2781
+
2782
+
2783
+ P._isBigNumber = true;
2784
+
2785
+ if (configObject != null) BigNumber.set(configObject);
2786
+
2787
+ return BigNumber;
2788
+ }
2789
+
2790
+
2791
+ // PRIVATE HELPER FUNCTIONS
2792
+
2793
+ // These functions don't need access to variables,
2794
+ // e.g. DECIMAL_PLACES, in the scope of the `clone` function above.
2795
+
2796
+
2797
+ function bitFloor(n) {
2798
+ var i = n | 0;
2799
+ return n > 0 || n === i ? i : i - 1;
2800
+ }
2801
+
2802
+
2803
+ // Return a coefficient array as a string of base 10 digits.
2804
+ function coeffToString(a) {
2805
+ var s, z,
2806
+ i = 1,
2807
+ j = a.length,
2808
+ r = a[0] + '';
2809
+
2810
+ for (; i < j;) {
2811
+ s = a[i++] + '';
2812
+ z = LOG_BASE - s.length;
2813
+ for (; z--; s = '0' + s);
2814
+ r += s;
2815
+ }
2816
+
2817
+ // Determine trailing zeros.
2818
+ for (j = r.length; r.charCodeAt(--j) === 48;);
2819
+
2820
+ return r.slice(0, j + 1 || 1);
2821
+ }
2822
+
2823
+
2824
+ // Compare the value of BigNumbers x and y.
2825
+ function compare(x, y) {
2826
+ var a, b,
2827
+ xc = x.c,
2828
+ yc = y.c,
2829
+ i = x.s,
2830
+ j = y.s,
2831
+ k = x.e,
2832
+ l = y.e;
2833
+
2834
+ // Either NaN?
2835
+ if (!i || !j) return null;
2836
+
2837
+ a = xc && !xc[0];
2838
+ b = yc && !yc[0];
2839
+
2840
+ // Either zero?
2841
+ if (a || b) return a ? b ? 0 : -j : i;
2842
+
2843
+ // Signs differ?
2844
+ if (i != j) return i;
2845
+
2846
+ a = i < 0;
2847
+ b = k == l;
2848
+
2849
+ // Either Infinity?
2850
+ if (!xc || !yc) return b ? 0 : !xc ^ a ? 1 : -1;
2851
+
2852
+ // Compare exponents.
2853
+ if (!b) return k > l ^ a ? 1 : -1;
2854
+
2855
+ j = (k = xc.length) < (l = yc.length) ? k : l;
2856
+
2857
+ // Compare digit by digit.
2858
+ for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
2859
+
2860
+ // Compare lengths.
2861
+ return k == l ? 0 : k > l ^ a ? 1 : -1;
2862
+ }
2863
+
2864
+
2865
+ /*
2866
+ * Check that n is a primitive number, an integer, and in range, otherwise throw.
2867
+ */
2868
+ function intCheck(n, min, max, name) {
2869
+ if (n < min || n > max || n !== mathfloor(n)) {
2870
+ throw Error
2871
+ (bignumberError + (name || 'Argument') + (typeof n == 'number'
2872
+ ? n < min || n > max ? ' out of range: ' : ' not an integer: '
2873
+ : ' not a primitive number: ') + String(n));
2874
+ }
2875
+ }
2876
+
2877
+
2878
+ // Assumes finite n.
2879
+ function isOdd(n) {
2880
+ var k = n.c.length - 1;
2881
+ return bitFloor(n.e / LOG_BASE) == k && n.c[k] % 2 != 0;
2882
+ }
2883
+
2884
+
2885
+ function toExponential(str, e) {
2886
+ return (str.length > 1 ? str.charAt(0) + '.' + str.slice(1) : str) +
2887
+ (e < 0 ? 'e' : 'e+') + e;
2888
+ }
2889
+
2890
+
2891
+ function toFixedPoint(str, e, z) {
2892
+ var len, zs;
2893
+
2894
+ // Negative exponent?
2895
+ if (e < 0) {
2896
+
2897
+ // Prepend zeros.
2898
+ for (zs = z + '.'; ++e; zs += z);
2899
+ str = zs + str;
2900
+
2901
+ // Positive exponent
2902
+ } else {
2903
+ len = str.length;
2904
+
2905
+ // Append zeros.
2906
+ if (++e > len) {
2907
+ for (zs = z, e -= len; --e; zs += z);
2908
+ str += zs;
2909
+ } else if (e < len) {
2910
+ str = str.slice(0, e) + '.' + str.slice(e);
2911
+ }
2912
+ }
2913
+
2914
+ return str;
2915
+ }
2916
+
2917
+
2918
+ // EXPORT
2919
+
2920
+
2921
+ BigNumber = clone();
2922
+ BigNumber['default'] = BigNumber.BigNumber = BigNumber;
2923
+
2924
+ // AMD.
2925
+ if ( module.exports) {
2926
+ module.exports = BigNumber;
2927
+
2928
+ // Browser.
2929
+ } else {
2930
+ if (!globalObject) {
2931
+ globalObject = typeof self != 'undefined' && self ? self : window;
2932
+ }
2933
+
2934
+ globalObject.BigNumber = BigNumber;
2935
+ }
2936
+ })(commonjsGlobal);
2937
+ });
2938
+
2939
+ var long_1 = Long;
2940
+
2941
+ /**
2942
+ * wasm optimizations, to do native i64 multiplication and divide
2943
+ */
2944
+ var wasm = null;
2945
+
2946
+ try {
2947
+ wasm = new WebAssembly.Instance(new WebAssembly.Module(new Uint8Array([
2948
+ 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127, 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11, 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5, 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114, 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0, 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11
2949
+ ])), {}).exports;
2950
+ } catch (e) {
2951
+ // no wasm support :(
2952
+ }
2953
+
2954
+ /**
2955
+ * Constructs a 64 bit two's-complement integer, given its low and high 32 bit values as *signed* integers.
2956
+ * See the from* functions below for more convenient ways of constructing Longs.
2957
+ * @exports Long
2958
+ * @class A Long class for representing a 64 bit two's-complement integer value.
2959
+ * @param {number} low The low (signed) 32 bits of the long
2960
+ * @param {number} high The high (signed) 32 bits of the long
2961
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
2962
+ * @constructor
2963
+ */
2964
+ function Long(low, high, unsigned) {
2965
+
2966
+ /**
2967
+ * The low 32 bits as a signed value.
2968
+ * @type {number}
2969
+ */
2970
+ this.low = low | 0;
2971
+
2972
+ /**
2973
+ * The high 32 bits as a signed value.
2974
+ * @type {number}
2975
+ */
2976
+ this.high = high | 0;
2977
+
2978
+ /**
2979
+ * Whether unsigned or not.
2980
+ * @type {boolean}
2981
+ */
2982
+ this.unsigned = !!unsigned;
2983
+ }
2984
+
2985
+ // The internal representation of a long is the two given signed, 32-bit values.
2986
+ // We use 32-bit pieces because these are the size of integers on which
2987
+ // Javascript performs bit-operations. For operations like addition and
2988
+ // multiplication, we split each number into 16 bit pieces, which can easily be
2989
+ // multiplied within Javascript's floating-point representation without overflow
2990
+ // or change in sign.
2991
+ //
2992
+ // In the algorithms below, we frequently reduce the negative case to the
2993
+ // positive case by negating the input(s) and then post-processing the result.
2994
+ // Note that we must ALWAYS check specially whether those values are MIN_VALUE
2995
+ // (-2^63) because -MIN_VALUE == MIN_VALUE (since 2^63 cannot be represented as
2996
+ // a positive number, it overflows back into a negative). Not handling this
2997
+ // case would often result in infinite recursion.
2998
+ //
2999
+ // Common constant values ZERO, ONE, NEG_ONE, etc. are defined below the from*
3000
+ // methods on which they depend.
3001
+
3002
+ /**
3003
+ * An indicator used to reliably determine if an object is a Long or not.
3004
+ * @type {boolean}
3005
+ * @const
3006
+ * @private
3007
+ */
3008
+ Long.prototype.__isLong__;
3009
+
3010
+ Object.defineProperty(Long.prototype, "__isLong__", { value: true });
3011
+
3012
+ /**
3013
+ * @function
3014
+ * @param {*} obj Object
3015
+ * @returns {boolean}
3016
+ * @inner
3017
+ */
3018
+ function isLong(obj) {
3019
+ return (obj && obj["__isLong__"]) === true;
3020
+ }
3021
+
3022
+ /**
3023
+ * Tests if the specified object is a Long.
3024
+ * @function
3025
+ * @param {*} obj Object
3026
+ * @returns {boolean}
3027
+ */
3028
+ Long.isLong = isLong;
3029
+
3030
+ /**
3031
+ * A cache of the Long representations of small integer values.
3032
+ * @type {!Object}
3033
+ * @inner
3034
+ */
3035
+ var INT_CACHE = {};
3036
+
3037
+ /**
3038
+ * A cache of the Long representations of small unsigned integer values.
3039
+ * @type {!Object}
3040
+ * @inner
3041
+ */
3042
+ var UINT_CACHE = {};
3043
+
3044
+ /**
3045
+ * @param {number} value
3046
+ * @param {boolean=} unsigned
3047
+ * @returns {!Long}
3048
+ * @inner
3049
+ */
3050
+ function fromInt(value, unsigned) {
3051
+ var obj, cachedObj, cache;
3052
+ if (unsigned) {
3053
+ value >>>= 0;
3054
+ if (cache = (0 <= value && value < 256)) {
3055
+ cachedObj = UINT_CACHE[value];
3056
+ if (cachedObj)
3057
+ return cachedObj;
3058
+ }
3059
+ obj = fromBits(value, (value | 0) < 0 ? -1 : 0, true);
3060
+ if (cache)
3061
+ UINT_CACHE[value] = obj;
3062
+ return obj;
3063
+ } else {
3064
+ value |= 0;
3065
+ if (cache = (-128 <= value && value < 128)) {
3066
+ cachedObj = INT_CACHE[value];
3067
+ if (cachedObj)
3068
+ return cachedObj;
3069
+ }
3070
+ obj = fromBits(value, value < 0 ? -1 : 0, false);
3071
+ if (cache)
3072
+ INT_CACHE[value] = obj;
3073
+ return obj;
3074
+ }
3075
+ }
3076
+
3077
+ /**
3078
+ * Returns a Long representing the given 32 bit integer value.
3079
+ * @function
3080
+ * @param {number} value The 32 bit integer in question
3081
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3082
+ * @returns {!Long} The corresponding Long value
3083
+ */
3084
+ Long.fromInt = fromInt;
3085
+
3086
+ /**
3087
+ * @param {number} value
3088
+ * @param {boolean=} unsigned
3089
+ * @returns {!Long}
3090
+ * @inner
3091
+ */
3092
+ function fromNumber(value, unsigned) {
3093
+ if (isNaN(value))
3094
+ return unsigned ? UZERO : ZERO;
3095
+ if (unsigned) {
3096
+ if (value < 0)
3097
+ return UZERO;
3098
+ if (value >= TWO_PWR_64_DBL)
3099
+ return MAX_UNSIGNED_VALUE;
3100
+ } else {
3101
+ if (value <= -TWO_PWR_63_DBL)
3102
+ return MIN_VALUE;
3103
+ if (value + 1 >= TWO_PWR_63_DBL)
3104
+ return MAX_VALUE;
3105
+ }
3106
+ if (value < 0)
3107
+ return fromNumber(-value, unsigned).neg();
3108
+ return fromBits((value % TWO_PWR_32_DBL) | 0, (value / TWO_PWR_32_DBL) | 0, unsigned);
3109
+ }
3110
+
3111
+ /**
3112
+ * Returns a Long representing the given value, provided that it is a finite number. Otherwise, zero is returned.
3113
+ * @function
3114
+ * @param {number} value The number in question
3115
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3116
+ * @returns {!Long} The corresponding Long value
3117
+ */
3118
+ Long.fromNumber = fromNumber;
3119
+
3120
+ /**
3121
+ * @param {number} lowBits
3122
+ * @param {number} highBits
3123
+ * @param {boolean=} unsigned
3124
+ * @returns {!Long}
3125
+ * @inner
3126
+ */
3127
+ function fromBits(lowBits, highBits, unsigned) {
3128
+ return new Long(lowBits, highBits, unsigned);
3129
+ }
3130
+
3131
+ /**
3132
+ * Returns a Long representing the 64 bit integer that comes by concatenating the given low and high bits. Each is
3133
+ * assumed to use 32 bits.
3134
+ * @function
3135
+ * @param {number} lowBits The low 32 bits
3136
+ * @param {number} highBits The high 32 bits
3137
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3138
+ * @returns {!Long} The corresponding Long value
3139
+ */
3140
+ Long.fromBits = fromBits;
3141
+
3142
+ /**
3143
+ * @function
3144
+ * @param {number} base
3145
+ * @param {number} exponent
3146
+ * @returns {number}
3147
+ * @inner
3148
+ */
3149
+ var pow_dbl = Math.pow; // Used 4 times (4*8 to 15+4)
3150
+
3151
+ /**
3152
+ * @param {string} str
3153
+ * @param {(boolean|number)=} unsigned
3154
+ * @param {number=} radix
3155
+ * @returns {!Long}
3156
+ * @inner
3157
+ */
3158
+ function fromString(str, unsigned, radix) {
3159
+ if (str.length === 0)
3160
+ throw Error('empty string');
3161
+ if (str === "NaN" || str === "Infinity" || str === "+Infinity" || str === "-Infinity")
3162
+ return ZERO;
3163
+ if (typeof unsigned === 'number') {
3164
+ // For goog.math.long compatibility
3165
+ radix = unsigned,
3166
+ unsigned = false;
3167
+ } else {
3168
+ unsigned = !! unsigned;
3169
+ }
3170
+ radix = radix || 10;
3171
+ if (radix < 2 || 36 < radix)
3172
+ throw RangeError('radix');
3173
+
3174
+ var p;
3175
+ if ((p = str.indexOf('-')) > 0)
3176
+ throw Error('interior hyphen');
3177
+ else if (p === 0) {
3178
+ return fromString(str.substring(1), unsigned, radix).neg();
3179
+ }
3180
+
3181
+ // Do several (8) digits each time through the loop, so as to
3182
+ // minimize the calls to the very expensive emulated div.
3183
+ var radixToPower = fromNumber(pow_dbl(radix, 8));
3184
+
3185
+ var result = ZERO;
3186
+ for (var i = 0; i < str.length; i += 8) {
3187
+ var size = Math.min(8, str.length - i),
3188
+ value = parseInt(str.substring(i, i + size), radix);
3189
+ if (size < 8) {
3190
+ var power = fromNumber(pow_dbl(radix, size));
3191
+ result = result.mul(power).add(fromNumber(value));
3192
+ } else {
3193
+ result = result.mul(radixToPower);
3194
+ result = result.add(fromNumber(value));
3195
+ }
3196
+ }
3197
+ result.unsigned = unsigned;
3198
+ return result;
3199
+ }
3200
+
3201
+ /**
3202
+ * Returns a Long representation of the given string, written using the specified radix.
3203
+ * @function
3204
+ * @param {string} str The textual representation of the Long
3205
+ * @param {(boolean|number)=} unsigned Whether unsigned or not, defaults to signed
3206
+ * @param {number=} radix The radix in which the text is written (2-36), defaults to 10
3207
+ * @returns {!Long} The corresponding Long value
3208
+ */
3209
+ Long.fromString = fromString;
3210
+
3211
+ /**
3212
+ * @function
3213
+ * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val
3214
+ * @param {boolean=} unsigned
3215
+ * @returns {!Long}
3216
+ * @inner
3217
+ */
3218
+ function fromValue(val, unsigned) {
3219
+ if (typeof val === 'number')
3220
+ return fromNumber(val, unsigned);
3221
+ if (typeof val === 'string')
3222
+ return fromString(val, unsigned);
3223
+ // Throws for non-objects, converts non-instanceof Long:
3224
+ return fromBits(val.low, val.high, typeof unsigned === 'boolean' ? unsigned : val.unsigned);
3225
+ }
3226
+
3227
+ /**
3228
+ * Converts the specified value to a Long using the appropriate from* function for its type.
3229
+ * @function
3230
+ * @param {!Long|number|string|!{low: number, high: number, unsigned: boolean}} val Value
3231
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
3232
+ * @returns {!Long}
3233
+ */
3234
+ Long.fromValue = fromValue;
3235
+
3236
+ // NOTE: the compiler should inline these constant values below and then remove these variables, so there should be
3237
+ // no runtime penalty for these.
3238
+
3239
+ /**
3240
+ * @type {number}
3241
+ * @const
3242
+ * @inner
3243
+ */
3244
+ var TWO_PWR_16_DBL = 1 << 16;
3245
+
3246
+ /**
3247
+ * @type {number}
3248
+ * @const
3249
+ * @inner
3250
+ */
3251
+ var TWO_PWR_24_DBL = 1 << 24;
3252
+
3253
+ /**
3254
+ * @type {number}
3255
+ * @const
3256
+ * @inner
3257
+ */
3258
+ var TWO_PWR_32_DBL = TWO_PWR_16_DBL * TWO_PWR_16_DBL;
3259
+
3260
+ /**
3261
+ * @type {number}
3262
+ * @const
3263
+ * @inner
3264
+ */
3265
+ var TWO_PWR_64_DBL = TWO_PWR_32_DBL * TWO_PWR_32_DBL;
3266
+
3267
+ /**
3268
+ * @type {number}
3269
+ * @const
3270
+ * @inner
3271
+ */
3272
+ var TWO_PWR_63_DBL = TWO_PWR_64_DBL / 2;
3273
+
3274
+ /**
3275
+ * @type {!Long}
3276
+ * @const
3277
+ * @inner
3278
+ */
3279
+ var TWO_PWR_24 = fromInt(TWO_PWR_24_DBL);
3280
+
3281
+ /**
3282
+ * @type {!Long}
3283
+ * @inner
3284
+ */
3285
+ var ZERO = fromInt(0);
3286
+
3287
+ /**
3288
+ * Signed zero.
3289
+ * @type {!Long}
3290
+ */
3291
+ Long.ZERO = ZERO;
3292
+
3293
+ /**
3294
+ * @type {!Long}
3295
+ * @inner
3296
+ */
3297
+ var UZERO = fromInt(0, true);
3298
+
3299
+ /**
3300
+ * Unsigned zero.
3301
+ * @type {!Long}
3302
+ */
3303
+ Long.UZERO = UZERO;
3304
+
3305
+ /**
3306
+ * @type {!Long}
3307
+ * @inner
3308
+ */
3309
+ var ONE = fromInt(1);
3310
+
3311
+ /**
3312
+ * Signed one.
3313
+ * @type {!Long}
3314
+ */
3315
+ Long.ONE = ONE;
3316
+
3317
+ /**
3318
+ * @type {!Long}
3319
+ * @inner
3320
+ */
3321
+ var UONE = fromInt(1, true);
3322
+
3323
+ /**
3324
+ * Unsigned one.
3325
+ * @type {!Long}
3326
+ */
3327
+ Long.UONE = UONE;
3328
+
3329
+ /**
3330
+ * @type {!Long}
3331
+ * @inner
3332
+ */
3333
+ var NEG_ONE = fromInt(-1);
3334
+
3335
+ /**
3336
+ * Signed negative one.
3337
+ * @type {!Long}
3338
+ */
3339
+ Long.NEG_ONE = NEG_ONE;
3340
+
3341
+ /**
3342
+ * @type {!Long}
3343
+ * @inner
3344
+ */
3345
+ var MAX_VALUE = fromBits(0xFFFFFFFF|0, 0x7FFFFFFF|0, false);
3346
+
3347
+ /**
3348
+ * Maximum signed value.
3349
+ * @type {!Long}
3350
+ */
3351
+ Long.MAX_VALUE = MAX_VALUE;
3352
+
3353
+ /**
3354
+ * @type {!Long}
3355
+ * @inner
3356
+ */
3357
+ var MAX_UNSIGNED_VALUE = fromBits(0xFFFFFFFF|0, 0xFFFFFFFF|0, true);
3358
+
3359
+ /**
3360
+ * Maximum unsigned value.
3361
+ * @type {!Long}
3362
+ */
3363
+ Long.MAX_UNSIGNED_VALUE = MAX_UNSIGNED_VALUE;
3364
+
3365
+ /**
3366
+ * @type {!Long}
3367
+ * @inner
3368
+ */
3369
+ var MIN_VALUE = fromBits(0, 0x80000000|0, false);
3370
+
3371
+ /**
3372
+ * Minimum signed value.
3373
+ * @type {!Long}
3374
+ */
3375
+ Long.MIN_VALUE = MIN_VALUE;
3376
+
3377
+ /**
3378
+ * @alias Long.prototype
3379
+ * @inner
3380
+ */
3381
+ var LongPrototype = Long.prototype;
3382
+
3383
+ /**
3384
+ * Converts the Long to a 32 bit integer, assuming it is a 32 bit integer.
3385
+ * @returns {number}
3386
+ */
3387
+ LongPrototype.toInt = function toInt() {
3388
+ return this.unsigned ? this.low >>> 0 : this.low;
3389
+ };
3390
+
3391
+ /**
3392
+ * Converts the Long to a the nearest floating-point representation of this value (double, 53 bit mantissa).
3393
+ * @returns {number}
3394
+ */
3395
+ LongPrototype.toNumber = function toNumber() {
3396
+ if (this.unsigned)
3397
+ return ((this.high >>> 0) * TWO_PWR_32_DBL) + (this.low >>> 0);
3398
+ return this.high * TWO_PWR_32_DBL + (this.low >>> 0);
3399
+ };
3400
+
3401
+ /**
3402
+ * Converts the Long to a string written in the specified radix.
3403
+ * @param {number=} radix Radix (2-36), defaults to 10
3404
+ * @returns {string}
3405
+ * @override
3406
+ * @throws {RangeError} If `radix` is out of range
3407
+ */
3408
+ LongPrototype.toString = function toString(radix) {
3409
+ radix = radix || 10;
3410
+ if (radix < 2 || 36 < radix)
3411
+ throw RangeError('radix');
3412
+ if (this.isZero())
3413
+ return '0';
3414
+ if (this.isNegative()) { // Unsigned Longs are never negative
3415
+ if (this.eq(MIN_VALUE)) {
3416
+ // We need to change the Long value before it can be negated, so we remove
3417
+ // the bottom-most digit in this base and then recurse to do the rest.
3418
+ var radixLong = fromNumber(radix),
3419
+ div = this.div(radixLong),
3420
+ rem1 = div.mul(radixLong).sub(this);
3421
+ return div.toString(radix) + rem1.toInt().toString(radix);
3422
+ } else
3423
+ return '-' + this.neg().toString(radix);
3424
+ }
3425
+
3426
+ // Do several (6) digits each time through the loop, so as to
3427
+ // minimize the calls to the very expensive emulated div.
3428
+ var radixToPower = fromNumber(pow_dbl(radix, 6), this.unsigned),
3429
+ rem = this;
3430
+ var result = '';
3431
+ while (true) {
3432
+ var remDiv = rem.div(radixToPower),
3433
+ intval = rem.sub(remDiv.mul(radixToPower)).toInt() >>> 0,
3434
+ digits = intval.toString(radix);
3435
+ rem = remDiv;
3436
+ if (rem.isZero())
3437
+ return digits + result;
3438
+ else {
3439
+ while (digits.length < 6)
3440
+ digits = '0' + digits;
3441
+ result = '' + digits + result;
3442
+ }
3443
+ }
3444
+ };
3445
+
3446
+ /**
3447
+ * Gets the high 32 bits as a signed integer.
3448
+ * @returns {number} Signed high bits
3449
+ */
3450
+ LongPrototype.getHighBits = function getHighBits() {
3451
+ return this.high;
3452
+ };
3453
+
3454
+ /**
3455
+ * Gets the high 32 bits as an unsigned integer.
3456
+ * @returns {number} Unsigned high bits
3457
+ */
3458
+ LongPrototype.getHighBitsUnsigned = function getHighBitsUnsigned() {
3459
+ return this.high >>> 0;
3460
+ };
3461
+
3462
+ /**
3463
+ * Gets the low 32 bits as a signed integer.
3464
+ * @returns {number} Signed low bits
3465
+ */
3466
+ LongPrototype.getLowBits = function getLowBits() {
3467
+ return this.low;
3468
+ };
3469
+
3470
+ /**
3471
+ * Gets the low 32 bits as an unsigned integer.
3472
+ * @returns {number} Unsigned low bits
3473
+ */
3474
+ LongPrototype.getLowBitsUnsigned = function getLowBitsUnsigned() {
3475
+ return this.low >>> 0;
3476
+ };
3477
+
3478
+ /**
3479
+ * Gets the number of bits needed to represent the absolute value of this Long.
3480
+ * @returns {number}
3481
+ */
3482
+ LongPrototype.getNumBitsAbs = function getNumBitsAbs() {
3483
+ if (this.isNegative()) // Unsigned Longs are never negative
3484
+ return this.eq(MIN_VALUE) ? 64 : this.neg().getNumBitsAbs();
3485
+ var val = this.high != 0 ? this.high : this.low;
3486
+ for (var bit = 31; bit > 0; bit--)
3487
+ if ((val & (1 << bit)) != 0)
3488
+ break;
3489
+ return this.high != 0 ? bit + 33 : bit + 1;
3490
+ };
3491
+
3492
+ /**
3493
+ * Tests if this Long's value equals zero.
3494
+ * @returns {boolean}
3495
+ */
3496
+ LongPrototype.isZero = function isZero() {
3497
+ return this.high === 0 && this.low === 0;
3498
+ };
3499
+
3500
+ /**
3501
+ * Tests if this Long's value equals zero. This is an alias of {@link Long#isZero}.
3502
+ * @returns {boolean}
3503
+ */
3504
+ LongPrototype.eqz = LongPrototype.isZero;
3505
+
3506
+ /**
3507
+ * Tests if this Long's value is negative.
3508
+ * @returns {boolean}
3509
+ */
3510
+ LongPrototype.isNegative = function isNegative() {
3511
+ return !this.unsigned && this.high < 0;
3512
+ };
3513
+
3514
+ /**
3515
+ * Tests if this Long's value is positive.
3516
+ * @returns {boolean}
3517
+ */
3518
+ LongPrototype.isPositive = function isPositive() {
3519
+ return this.unsigned || this.high >= 0;
3520
+ };
3521
+
3522
+ /**
3523
+ * Tests if this Long's value is odd.
3524
+ * @returns {boolean}
3525
+ */
3526
+ LongPrototype.isOdd = function isOdd() {
3527
+ return (this.low & 1) === 1;
3528
+ };
3529
+
3530
+ /**
3531
+ * Tests if this Long's value is even.
3532
+ * @returns {boolean}
3533
+ */
3534
+ LongPrototype.isEven = function isEven() {
3535
+ return (this.low & 1) === 0;
3536
+ };
3537
+
3538
+ /**
3539
+ * Tests if this Long's value equals the specified's.
3540
+ * @param {!Long|number|string} other Other value
3541
+ * @returns {boolean}
3542
+ */
3543
+ LongPrototype.equals = function equals(other) {
3544
+ if (!isLong(other))
3545
+ other = fromValue(other);
3546
+ if (this.unsigned !== other.unsigned && (this.high >>> 31) === 1 && (other.high >>> 31) === 1)
3547
+ return false;
3548
+ return this.high === other.high && this.low === other.low;
3549
+ };
3550
+
3551
+ /**
3552
+ * Tests if this Long's value equals the specified's. This is an alias of {@link Long#equals}.
3553
+ * @function
3554
+ * @param {!Long|number|string} other Other value
3555
+ * @returns {boolean}
3556
+ */
3557
+ LongPrototype.eq = LongPrototype.equals;
3558
+
3559
+ /**
3560
+ * Tests if this Long's value differs from the specified's.
3561
+ * @param {!Long|number|string} other Other value
3562
+ * @returns {boolean}
3563
+ */
3564
+ LongPrototype.notEquals = function notEquals(other) {
3565
+ return !this.eq(/* validates */ other);
3566
+ };
3567
+
3568
+ /**
3569
+ * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
3570
+ * @function
3571
+ * @param {!Long|number|string} other Other value
3572
+ * @returns {boolean}
3573
+ */
3574
+ LongPrototype.neq = LongPrototype.notEquals;
3575
+
3576
+ /**
3577
+ * Tests if this Long's value differs from the specified's. This is an alias of {@link Long#notEquals}.
3578
+ * @function
3579
+ * @param {!Long|number|string} other Other value
3580
+ * @returns {boolean}
3581
+ */
3582
+ LongPrototype.ne = LongPrototype.notEquals;
3583
+
3584
+ /**
3585
+ * Tests if this Long's value is less than the specified's.
3586
+ * @param {!Long|number|string} other Other value
3587
+ * @returns {boolean}
3588
+ */
3589
+ LongPrototype.lessThan = function lessThan(other) {
3590
+ return this.comp(/* validates */ other) < 0;
3591
+ };
3592
+
3593
+ /**
3594
+ * Tests if this Long's value is less than the specified's. This is an alias of {@link Long#lessThan}.
3595
+ * @function
3596
+ * @param {!Long|number|string} other Other value
3597
+ * @returns {boolean}
3598
+ */
3599
+ LongPrototype.lt = LongPrototype.lessThan;
3600
+
3601
+ /**
3602
+ * Tests if this Long's value is less than or equal the specified's.
3603
+ * @param {!Long|number|string} other Other value
3604
+ * @returns {boolean}
3605
+ */
3606
+ LongPrototype.lessThanOrEqual = function lessThanOrEqual(other) {
3607
+ return this.comp(/* validates */ other) <= 0;
3608
+ };
3609
+
3610
+ /**
3611
+ * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
3612
+ * @function
3613
+ * @param {!Long|number|string} other Other value
3614
+ * @returns {boolean}
3615
+ */
3616
+ LongPrototype.lte = LongPrototype.lessThanOrEqual;
3617
+
3618
+ /**
3619
+ * Tests if this Long's value is less than or equal the specified's. This is an alias of {@link Long#lessThanOrEqual}.
3620
+ * @function
3621
+ * @param {!Long|number|string} other Other value
3622
+ * @returns {boolean}
3623
+ */
3624
+ LongPrototype.le = LongPrototype.lessThanOrEqual;
3625
+
3626
+ /**
3627
+ * Tests if this Long's value is greater than the specified's.
3628
+ * @param {!Long|number|string} other Other value
3629
+ * @returns {boolean}
3630
+ */
3631
+ LongPrototype.greaterThan = function greaterThan(other) {
3632
+ return this.comp(/* validates */ other) > 0;
3633
+ };
3634
+
3635
+ /**
3636
+ * Tests if this Long's value is greater than the specified's. This is an alias of {@link Long#greaterThan}.
3637
+ * @function
3638
+ * @param {!Long|number|string} other Other value
3639
+ * @returns {boolean}
3640
+ */
3641
+ LongPrototype.gt = LongPrototype.greaterThan;
3642
+
3643
+ /**
3644
+ * Tests if this Long's value is greater than or equal the specified's.
3645
+ * @param {!Long|number|string} other Other value
3646
+ * @returns {boolean}
3647
+ */
3648
+ LongPrototype.greaterThanOrEqual = function greaterThanOrEqual(other) {
3649
+ return this.comp(/* validates */ other) >= 0;
3650
+ };
3651
+
3652
+ /**
3653
+ * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
3654
+ * @function
3655
+ * @param {!Long|number|string} other Other value
3656
+ * @returns {boolean}
3657
+ */
3658
+ LongPrototype.gte = LongPrototype.greaterThanOrEqual;
3659
+
3660
+ /**
3661
+ * Tests if this Long's value is greater than or equal the specified's. This is an alias of {@link Long#greaterThanOrEqual}.
3662
+ * @function
3663
+ * @param {!Long|number|string} other Other value
3664
+ * @returns {boolean}
3665
+ */
3666
+ LongPrototype.ge = LongPrototype.greaterThanOrEqual;
3667
+
3668
+ /**
3669
+ * Compares this Long's value with the specified's.
3670
+ * @param {!Long|number|string} other Other value
3671
+ * @returns {number} 0 if they are the same, 1 if the this is greater and -1
3672
+ * if the given one is greater
3673
+ */
3674
+ LongPrototype.compare = function compare(other) {
3675
+ if (!isLong(other))
3676
+ other = fromValue(other);
3677
+ if (this.eq(other))
3678
+ return 0;
3679
+ var thisNeg = this.isNegative(),
3680
+ otherNeg = other.isNegative();
3681
+ if (thisNeg && !otherNeg)
3682
+ return -1;
3683
+ if (!thisNeg && otherNeg)
3684
+ return 1;
3685
+ // At this point the sign bits are the same
3686
+ if (!this.unsigned)
3687
+ return this.sub(other).isNegative() ? -1 : 1;
3688
+ // Both are positive if at least one is unsigned
3689
+ return (other.high >>> 0) > (this.high >>> 0) || (other.high === this.high && (other.low >>> 0) > (this.low >>> 0)) ? -1 : 1;
3690
+ };
3691
+
3692
+ /**
3693
+ * Compares this Long's value with the specified's. This is an alias of {@link Long#compare}.
3694
+ * @function
3695
+ * @param {!Long|number|string} other Other value
3696
+ * @returns {number} 0 if they are the same, 1 if the this is greater and -1
3697
+ * if the given one is greater
3698
+ */
3699
+ LongPrototype.comp = LongPrototype.compare;
3700
+
3701
+ /**
3702
+ * Negates this Long's value.
3703
+ * @returns {!Long} Negated Long
3704
+ */
3705
+ LongPrototype.negate = function negate() {
3706
+ if (!this.unsigned && this.eq(MIN_VALUE))
3707
+ return MIN_VALUE;
3708
+ return this.not().add(ONE);
3709
+ };
3710
+
3711
+ /**
3712
+ * Negates this Long's value. This is an alias of {@link Long#negate}.
3713
+ * @function
3714
+ * @returns {!Long} Negated Long
3715
+ */
3716
+ LongPrototype.neg = LongPrototype.negate;
3717
+
3718
+ /**
3719
+ * Returns the sum of this and the specified Long.
3720
+ * @param {!Long|number|string} addend Addend
3721
+ * @returns {!Long} Sum
3722
+ */
3723
+ LongPrototype.add = function add(addend) {
3724
+ if (!isLong(addend))
3725
+ addend = fromValue(addend);
3726
+
3727
+ // Divide each number into 4 chunks of 16 bits, and then sum the chunks.
3728
+
3729
+ var a48 = this.high >>> 16;
3730
+ var a32 = this.high & 0xFFFF;
3731
+ var a16 = this.low >>> 16;
3732
+ var a00 = this.low & 0xFFFF;
3733
+
3734
+ var b48 = addend.high >>> 16;
3735
+ var b32 = addend.high & 0xFFFF;
3736
+ var b16 = addend.low >>> 16;
3737
+ var b00 = addend.low & 0xFFFF;
3738
+
3739
+ var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
3740
+ c00 += a00 + b00;
3741
+ c16 += c00 >>> 16;
3742
+ c00 &= 0xFFFF;
3743
+ c16 += a16 + b16;
3744
+ c32 += c16 >>> 16;
3745
+ c16 &= 0xFFFF;
3746
+ c32 += a32 + b32;
3747
+ c48 += c32 >>> 16;
3748
+ c32 &= 0xFFFF;
3749
+ c48 += a48 + b48;
3750
+ c48 &= 0xFFFF;
3751
+ return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
3752
+ };
3753
+
3754
+ /**
3755
+ * Returns the difference of this and the specified Long.
3756
+ * @param {!Long|number|string} subtrahend Subtrahend
3757
+ * @returns {!Long} Difference
3758
+ */
3759
+ LongPrototype.subtract = function subtract(subtrahend) {
3760
+ if (!isLong(subtrahend))
3761
+ subtrahend = fromValue(subtrahend);
3762
+ return this.add(subtrahend.neg());
3763
+ };
3764
+
3765
+ /**
3766
+ * Returns the difference of this and the specified Long. This is an alias of {@link Long#subtract}.
3767
+ * @function
3768
+ * @param {!Long|number|string} subtrahend Subtrahend
3769
+ * @returns {!Long} Difference
3770
+ */
3771
+ LongPrototype.sub = LongPrototype.subtract;
3772
+
3773
+ /**
3774
+ * Returns the product of this and the specified Long.
3775
+ * @param {!Long|number|string} multiplier Multiplier
3776
+ * @returns {!Long} Product
3777
+ */
3778
+ LongPrototype.multiply = function multiply(multiplier) {
3779
+ if (this.isZero())
3780
+ return ZERO;
3781
+ if (!isLong(multiplier))
3782
+ multiplier = fromValue(multiplier);
3783
+
3784
+ // use wasm support if present
3785
+ if (wasm) {
3786
+ var low = wasm.mul(this.low,
3787
+ this.high,
3788
+ multiplier.low,
3789
+ multiplier.high);
3790
+ return fromBits(low, wasm.get_high(), this.unsigned);
3791
+ }
3792
+
3793
+ if (multiplier.isZero())
3794
+ return ZERO;
3795
+ if (this.eq(MIN_VALUE))
3796
+ return multiplier.isOdd() ? MIN_VALUE : ZERO;
3797
+ if (multiplier.eq(MIN_VALUE))
3798
+ return this.isOdd() ? MIN_VALUE : ZERO;
3799
+
3800
+ if (this.isNegative()) {
3801
+ if (multiplier.isNegative())
3802
+ return this.neg().mul(multiplier.neg());
3803
+ else
3804
+ return this.neg().mul(multiplier).neg();
3805
+ } else if (multiplier.isNegative())
3806
+ return this.mul(multiplier.neg()).neg();
3807
+
3808
+ // If both longs are small, use float multiplication
3809
+ if (this.lt(TWO_PWR_24) && multiplier.lt(TWO_PWR_24))
3810
+ return fromNumber(this.toNumber() * multiplier.toNumber(), this.unsigned);
3811
+
3812
+ // Divide each long into 4 chunks of 16 bits, and then add up 4x4 products.
3813
+ // We can skip products that would overflow.
3814
+
3815
+ var a48 = this.high >>> 16;
3816
+ var a32 = this.high & 0xFFFF;
3817
+ var a16 = this.low >>> 16;
3818
+ var a00 = this.low & 0xFFFF;
3819
+
3820
+ var b48 = multiplier.high >>> 16;
3821
+ var b32 = multiplier.high & 0xFFFF;
3822
+ var b16 = multiplier.low >>> 16;
3823
+ var b00 = multiplier.low & 0xFFFF;
3824
+
3825
+ var c48 = 0, c32 = 0, c16 = 0, c00 = 0;
3826
+ c00 += a00 * b00;
3827
+ c16 += c00 >>> 16;
3828
+ c00 &= 0xFFFF;
3829
+ c16 += a16 * b00;
3830
+ c32 += c16 >>> 16;
3831
+ c16 &= 0xFFFF;
3832
+ c16 += a00 * b16;
3833
+ c32 += c16 >>> 16;
3834
+ c16 &= 0xFFFF;
3835
+ c32 += a32 * b00;
3836
+ c48 += c32 >>> 16;
3837
+ c32 &= 0xFFFF;
3838
+ c32 += a16 * b16;
3839
+ c48 += c32 >>> 16;
3840
+ c32 &= 0xFFFF;
3841
+ c32 += a00 * b32;
3842
+ c48 += c32 >>> 16;
3843
+ c32 &= 0xFFFF;
3844
+ c48 += a48 * b00 + a32 * b16 + a16 * b32 + a00 * b48;
3845
+ c48 &= 0xFFFF;
3846
+ return fromBits((c16 << 16) | c00, (c48 << 16) | c32, this.unsigned);
3847
+ };
3848
+
3849
+ /**
3850
+ * Returns the product of this and the specified Long. This is an alias of {@link Long#multiply}.
3851
+ * @function
3852
+ * @param {!Long|number|string} multiplier Multiplier
3853
+ * @returns {!Long} Product
3854
+ */
3855
+ LongPrototype.mul = LongPrototype.multiply;
3856
+
3857
+ /**
3858
+ * Returns this Long divided by the specified. The result is signed if this Long is signed or
3859
+ * unsigned if this Long is unsigned.
3860
+ * @param {!Long|number|string} divisor Divisor
3861
+ * @returns {!Long} Quotient
3862
+ */
3863
+ LongPrototype.divide = function divide(divisor) {
3864
+ if (!isLong(divisor))
3865
+ divisor = fromValue(divisor);
3866
+ if (divisor.isZero())
3867
+ throw Error('division by zero');
3868
+
3869
+ // use wasm support if present
3870
+ if (wasm) {
3871
+ // guard against signed division overflow: the largest
3872
+ // negative number / -1 would be 1 larger than the largest
3873
+ // positive number, due to two's complement.
3874
+ if (!this.unsigned &&
3875
+ this.high === -0x80000000 &&
3876
+ divisor.low === -1 && divisor.high === -1) {
3877
+ // be consistent with non-wasm code path
3878
+ return this;
3879
+ }
3880
+ var low = (this.unsigned ? wasm.div_u : wasm.div_s)(
3881
+ this.low,
3882
+ this.high,
3883
+ divisor.low,
3884
+ divisor.high
3885
+ );
3886
+ return fromBits(low, wasm.get_high(), this.unsigned);
3887
+ }
3888
+
3889
+ if (this.isZero())
3890
+ return this.unsigned ? UZERO : ZERO;
3891
+ var approx, rem, res;
3892
+ if (!this.unsigned) {
3893
+ // This section is only relevant for signed longs and is derived from the
3894
+ // closure library as a whole.
3895
+ if (this.eq(MIN_VALUE)) {
3896
+ if (divisor.eq(ONE) || divisor.eq(NEG_ONE))
3897
+ return MIN_VALUE; // recall that -MIN_VALUE == MIN_VALUE
3898
+ else if (divisor.eq(MIN_VALUE))
3899
+ return ONE;
3900
+ else {
3901
+ // At this point, we have |other| >= 2, so |this/other| < |MIN_VALUE|.
3902
+ var halfThis = this.shr(1);
3903
+ approx = halfThis.div(divisor).shl(1);
3904
+ if (approx.eq(ZERO)) {
3905
+ return divisor.isNegative() ? ONE : NEG_ONE;
3906
+ } else {
3907
+ rem = this.sub(divisor.mul(approx));
3908
+ res = approx.add(rem.div(divisor));
3909
+ return res;
3910
+ }
3911
+ }
3912
+ } else if (divisor.eq(MIN_VALUE))
3913
+ return this.unsigned ? UZERO : ZERO;
3914
+ if (this.isNegative()) {
3915
+ if (divisor.isNegative())
3916
+ return this.neg().div(divisor.neg());
3917
+ return this.neg().div(divisor).neg();
3918
+ } else if (divisor.isNegative())
3919
+ return this.div(divisor.neg()).neg();
3920
+ res = ZERO;
3921
+ } else {
3922
+ // The algorithm below has not been made for unsigned longs. It's therefore
3923
+ // required to take special care of the MSB prior to running it.
3924
+ if (!divisor.unsigned)
3925
+ divisor = divisor.toUnsigned();
3926
+ if (divisor.gt(this))
3927
+ return UZERO;
3928
+ if (divisor.gt(this.shru(1))) // 15 >>> 1 = 7 ; with divisor = 8 ; true
3929
+ return UONE;
3930
+ res = UZERO;
3931
+ }
3932
+
3933
+ // Repeat the following until the remainder is less than other: find a
3934
+ // floating-point that approximates remainder / other *from below*, add this
3935
+ // into the result, and subtract it from the remainder. It is critical that
3936
+ // the approximate value is less than or equal to the real value so that the
3937
+ // remainder never becomes negative.
3938
+ rem = this;
3939
+ while (rem.gte(divisor)) {
3940
+ // Approximate the result of division. This may be a little greater or
3941
+ // smaller than the actual value.
3942
+ approx = Math.max(1, Math.floor(rem.toNumber() / divisor.toNumber()));
3943
+
3944
+ // We will tweak the approximate result by changing it in the 48-th digit or
3945
+ // the smallest non-fractional digit, whichever is larger.
3946
+ var log2 = Math.ceil(Math.log(approx) / Math.LN2),
3947
+ delta = (log2 <= 48) ? 1 : pow_dbl(2, log2 - 48),
3948
+
3949
+ // Decrease the approximation until it is smaller than the remainder. Note
3950
+ // that if it is too large, the product overflows and is negative.
3951
+ approxRes = fromNumber(approx),
3952
+ approxRem = approxRes.mul(divisor);
3953
+ while (approxRem.isNegative() || approxRem.gt(rem)) {
3954
+ approx -= delta;
3955
+ approxRes = fromNumber(approx, this.unsigned);
3956
+ approxRem = approxRes.mul(divisor);
3957
+ }
3958
+
3959
+ // We know the answer can't be zero... and actually, zero would cause
3960
+ // infinite recursion since we would make no progress.
3961
+ if (approxRes.isZero())
3962
+ approxRes = ONE;
3963
+
3964
+ res = res.add(approxRes);
3965
+ rem = rem.sub(approxRem);
3966
+ }
3967
+ return res;
3968
+ };
3969
+
3970
+ /**
3971
+ * Returns this Long divided by the specified. This is an alias of {@link Long#divide}.
3972
+ * @function
3973
+ * @param {!Long|number|string} divisor Divisor
3974
+ * @returns {!Long} Quotient
3975
+ */
3976
+ LongPrototype.div = LongPrototype.divide;
3977
+
3978
+ /**
3979
+ * Returns this Long modulo the specified.
3980
+ * @param {!Long|number|string} divisor Divisor
3981
+ * @returns {!Long} Remainder
3982
+ */
3983
+ LongPrototype.modulo = function modulo(divisor) {
3984
+ if (!isLong(divisor))
3985
+ divisor = fromValue(divisor);
3986
+
3987
+ // use wasm support if present
3988
+ if (wasm) {
3989
+ var low = (this.unsigned ? wasm.rem_u : wasm.rem_s)(
3990
+ this.low,
3991
+ this.high,
3992
+ divisor.low,
3993
+ divisor.high
3994
+ );
3995
+ return fromBits(low, wasm.get_high(), this.unsigned);
3996
+ }
3997
+
3998
+ return this.sub(this.div(divisor).mul(divisor));
3999
+ };
4000
+
4001
+ /**
4002
+ * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
4003
+ * @function
4004
+ * @param {!Long|number|string} divisor Divisor
4005
+ * @returns {!Long} Remainder
4006
+ */
4007
+ LongPrototype.mod = LongPrototype.modulo;
4008
+
4009
+ /**
4010
+ * Returns this Long modulo the specified. This is an alias of {@link Long#modulo}.
4011
+ * @function
4012
+ * @param {!Long|number|string} divisor Divisor
4013
+ * @returns {!Long} Remainder
4014
+ */
4015
+ LongPrototype.rem = LongPrototype.modulo;
4016
+
4017
+ /**
4018
+ * Returns the bitwise NOT of this Long.
4019
+ * @returns {!Long}
4020
+ */
4021
+ LongPrototype.not = function not() {
4022
+ return fromBits(~this.low, ~this.high, this.unsigned);
4023
+ };
4024
+
4025
+ /**
4026
+ * Returns the bitwise AND of this Long and the specified.
4027
+ * @param {!Long|number|string} other Other Long
4028
+ * @returns {!Long}
4029
+ */
4030
+ LongPrototype.and = function and(other) {
4031
+ if (!isLong(other))
4032
+ other = fromValue(other);
4033
+ return fromBits(this.low & other.low, this.high & other.high, this.unsigned);
4034
+ };
4035
+
4036
+ /**
4037
+ * Returns the bitwise OR of this Long and the specified.
4038
+ * @param {!Long|number|string} other Other Long
4039
+ * @returns {!Long}
4040
+ */
4041
+ LongPrototype.or = function or(other) {
4042
+ if (!isLong(other))
4043
+ other = fromValue(other);
4044
+ return fromBits(this.low | other.low, this.high | other.high, this.unsigned);
4045
+ };
4046
+
4047
+ /**
4048
+ * Returns the bitwise XOR of this Long and the given one.
4049
+ * @param {!Long|number|string} other Other Long
4050
+ * @returns {!Long}
4051
+ */
4052
+ LongPrototype.xor = function xor(other) {
4053
+ if (!isLong(other))
4054
+ other = fromValue(other);
4055
+ return fromBits(this.low ^ other.low, this.high ^ other.high, this.unsigned);
4056
+ };
4057
+
4058
+ /**
4059
+ * Returns this Long with bits shifted to the left by the given amount.
4060
+ * @param {number|!Long} numBits Number of bits
4061
+ * @returns {!Long} Shifted Long
4062
+ */
4063
+ LongPrototype.shiftLeft = function shiftLeft(numBits) {
4064
+ if (isLong(numBits))
4065
+ numBits = numBits.toInt();
4066
+ if ((numBits &= 63) === 0)
4067
+ return this;
4068
+ else if (numBits < 32)
4069
+ return fromBits(this.low << numBits, (this.high << numBits) | (this.low >>> (32 - numBits)), this.unsigned);
4070
+ else
4071
+ return fromBits(0, this.low << (numBits - 32), this.unsigned);
4072
+ };
4073
+
4074
+ /**
4075
+ * Returns this Long with bits shifted to the left by the given amount. This is an alias of {@link Long#shiftLeft}.
4076
+ * @function
4077
+ * @param {number|!Long} numBits Number of bits
4078
+ * @returns {!Long} Shifted Long
4079
+ */
4080
+ LongPrototype.shl = LongPrototype.shiftLeft;
4081
+
4082
+ /**
4083
+ * Returns this Long with bits arithmetically shifted to the right by the given amount.
4084
+ * @param {number|!Long} numBits Number of bits
4085
+ * @returns {!Long} Shifted Long
4086
+ */
4087
+ LongPrototype.shiftRight = function shiftRight(numBits) {
4088
+ if (isLong(numBits))
4089
+ numBits = numBits.toInt();
4090
+ if ((numBits &= 63) === 0)
4091
+ return this;
4092
+ else if (numBits < 32)
4093
+ return fromBits((this.low >>> numBits) | (this.high << (32 - numBits)), this.high >> numBits, this.unsigned);
4094
+ else
4095
+ return fromBits(this.high >> (numBits - 32), this.high >= 0 ? 0 : -1, this.unsigned);
4096
+ };
4097
+
4098
+ /**
4099
+ * Returns this Long with bits arithmetically shifted to the right by the given amount. This is an alias of {@link Long#shiftRight}.
4100
+ * @function
4101
+ * @param {number|!Long} numBits Number of bits
4102
+ * @returns {!Long} Shifted Long
4103
+ */
4104
+ LongPrototype.shr = LongPrototype.shiftRight;
4105
+
4106
+ /**
4107
+ * Returns this Long with bits logically shifted to the right by the given amount.
4108
+ * @param {number|!Long} numBits Number of bits
4109
+ * @returns {!Long} Shifted Long
4110
+ */
4111
+ LongPrototype.shiftRightUnsigned = function shiftRightUnsigned(numBits) {
4112
+ if (isLong(numBits))
4113
+ numBits = numBits.toInt();
4114
+ numBits &= 63;
4115
+ if (numBits === 0)
4116
+ return this;
4117
+ else {
4118
+ var high = this.high;
4119
+ if (numBits < 32) {
4120
+ var low = this.low;
4121
+ return fromBits((low >>> numBits) | (high << (32 - numBits)), high >>> numBits, this.unsigned);
4122
+ } else if (numBits === 32)
4123
+ return fromBits(high, 0, this.unsigned);
4124
+ else
4125
+ return fromBits(high >>> (numBits - 32), 0, this.unsigned);
4126
+ }
4127
+ };
4128
+
4129
+ /**
4130
+ * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
4131
+ * @function
4132
+ * @param {number|!Long} numBits Number of bits
4133
+ * @returns {!Long} Shifted Long
4134
+ */
4135
+ LongPrototype.shru = LongPrototype.shiftRightUnsigned;
4136
+
4137
+ /**
4138
+ * Returns this Long with bits logically shifted to the right by the given amount. This is an alias of {@link Long#shiftRightUnsigned}.
4139
+ * @function
4140
+ * @param {number|!Long} numBits Number of bits
4141
+ * @returns {!Long} Shifted Long
4142
+ */
4143
+ LongPrototype.shr_u = LongPrototype.shiftRightUnsigned;
4144
+
4145
+ /**
4146
+ * Converts this Long to signed.
4147
+ * @returns {!Long} Signed long
4148
+ */
4149
+ LongPrototype.toSigned = function toSigned() {
4150
+ if (!this.unsigned)
4151
+ return this;
4152
+ return fromBits(this.low, this.high, false);
4153
+ };
4154
+
4155
+ /**
4156
+ * Converts this Long to unsigned.
4157
+ * @returns {!Long} Unsigned long
4158
+ */
4159
+ LongPrototype.toUnsigned = function toUnsigned() {
4160
+ if (this.unsigned)
4161
+ return this;
4162
+ return fromBits(this.low, this.high, true);
4163
+ };
4164
+
4165
+ /**
4166
+ * Converts this Long to its byte representation.
4167
+ * @param {boolean=} le Whether little or big endian, defaults to big endian
4168
+ * @returns {!Array.<number>} Byte representation
4169
+ */
4170
+ LongPrototype.toBytes = function toBytes(le) {
4171
+ return le ? this.toBytesLE() : this.toBytesBE();
4172
+ };
4173
+
4174
+ /**
4175
+ * Converts this Long to its little endian byte representation.
4176
+ * @returns {!Array.<number>} Little endian byte representation
4177
+ */
4178
+ LongPrototype.toBytesLE = function toBytesLE() {
4179
+ var hi = this.high,
4180
+ lo = this.low;
4181
+ return [
4182
+ lo & 0xff,
4183
+ lo >>> 8 & 0xff,
4184
+ lo >>> 16 & 0xff,
4185
+ lo >>> 24 ,
4186
+ hi & 0xff,
4187
+ hi >>> 8 & 0xff,
4188
+ hi >>> 16 & 0xff,
4189
+ hi >>> 24
4190
+ ];
4191
+ };
4192
+
4193
+ /**
4194
+ * Converts this Long to its big endian byte representation.
4195
+ * @returns {!Array.<number>} Big endian byte representation
4196
+ */
4197
+ LongPrototype.toBytesBE = function toBytesBE() {
4198
+ var hi = this.high,
4199
+ lo = this.low;
4200
+ return [
4201
+ hi >>> 24 ,
4202
+ hi >>> 16 & 0xff,
4203
+ hi >>> 8 & 0xff,
4204
+ hi & 0xff,
4205
+ lo >>> 24 ,
4206
+ lo >>> 16 & 0xff,
4207
+ lo >>> 8 & 0xff,
4208
+ lo & 0xff
4209
+ ];
4210
+ };
4211
+
4212
+ /**
4213
+ * Creates a Long from its byte representation.
4214
+ * @param {!Array.<number>} bytes Byte representation
4215
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4216
+ * @param {boolean=} le Whether little or big endian, defaults to big endian
4217
+ * @returns {Long} The corresponding Long value
4218
+ */
4219
+ Long.fromBytes = function fromBytes(bytes, unsigned, le) {
4220
+ return le ? Long.fromBytesLE(bytes, unsigned) : Long.fromBytesBE(bytes, unsigned);
4221
+ };
4222
+
4223
+ /**
4224
+ * Creates a Long from its little endian byte representation.
4225
+ * @param {!Array.<number>} bytes Little endian byte representation
4226
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4227
+ * @returns {Long} The corresponding Long value
4228
+ */
4229
+ Long.fromBytesLE = function fromBytesLE(bytes, unsigned) {
4230
+ return new Long(
4231
+ bytes[0] |
4232
+ bytes[1] << 8 |
4233
+ bytes[2] << 16 |
4234
+ bytes[3] << 24,
4235
+ bytes[4] |
4236
+ bytes[5] << 8 |
4237
+ bytes[6] << 16 |
4238
+ bytes[7] << 24,
4239
+ unsigned
4240
+ );
4241
+ };
4242
+
4243
+ /**
4244
+ * Creates a Long from its big endian byte representation.
4245
+ * @param {!Array.<number>} bytes Big endian byte representation
4246
+ * @param {boolean=} unsigned Whether unsigned or not, defaults to signed
4247
+ * @returns {Long} The corresponding Long value
4248
+ */
4249
+ Long.fromBytesBE = function fromBytesBE(bytes, unsigned) {
4250
+ return new Long(
4251
+ bytes[4] << 24 |
4252
+ bytes[5] << 16 |
4253
+ bytes[6] << 8 |
4254
+ bytes[7],
4255
+ bytes[0] << 24 |
4256
+ bytes[1] << 16 |
4257
+ bytes[2] << 8 |
4258
+ bytes[3],
4259
+ unsigned
4260
+ );
4261
+ };
4262
+
39
4263
  var aspromise = asPromise;
40
4264
 
41
4265
  /**
@@ -5376,8 +9600,9 @@ $root.cosmos = (function() {
5376
9600
  var MsgCompiled = $root;
5377
9601
 
5378
9602
  const DECIMAL = 8;
5379
- const DEFAULT_GAS_VALUE = '4000000';
5380
- const DEPOSIT_GAS_VALUE = '500000000';
9603
+ const DEFAULT_GAS_ADJUSTMENT = 2;
9604
+ const DEFAULT_GAS_LIMIT_VALUE = '4000000';
9605
+ const DEPOSIT_GAS_LIMIT_VALUE = '600000000';
5381
9606
  const MAX_TX_COUNT = 100;
5382
9607
  /**
5383
9608
  * Checks whether an asset is `AssetRuneNative`
@@ -5555,11 +9780,36 @@ const buildUnsignedTx = ({ cosmosSdk, txBody, signerPubkey, sequence, gasLimit,
5555
9780
  ],
5556
9781
  fee: {
5557
9782
  amount: null,
5558
- gas_limit: cosmosclient.Long.fromString(gasLimit),
9783
+ gas_limit: gasLimit || null,
5559
9784
  },
5560
9785
  });
5561
9786
  return new cosmosclient.TxBuilder(cosmosSdk, txBody, authInfo);
5562
9787
  };
9788
+ /**
9789
+ * Estimates usage of gas
9790
+ *
9791
+ * Note: Be careful by using this helper function,
9792
+ * it's still experimental and result might be incorrect.
9793
+ * Change `multiplier` to get a valid estimation of gas.
9794
+ */
9795
+ const getEstimatedGas = ({ cosmosSDKClient, txBody, privKey, accountNumber, accountSequence, multiplier, }) => __awaiter(void 0, void 0, void 0, function* () {
9796
+ var _b, _c, _d;
9797
+ const pubKey = privKey.pubKey();
9798
+ const txBuilder = buildUnsignedTx({
9799
+ cosmosSdk: cosmosSDKClient.sdk,
9800
+ txBody: txBody,
9801
+ signerPubkey: cosmosclient.codec.instanceToProtoAny(pubKey),
9802
+ sequence: accountSequence,
9803
+ });
9804
+ const signDocBytes = txBuilder.signDocBytes(accountNumber);
9805
+ txBuilder.addSignature(privKey.sign(signDocBytes));
9806
+ const resp = yield rest.tx.simulate(cosmosSDKClient.sdk, { tx_bytes: txBuilder.txBytes() });
9807
+ const estimatedGas = (_d = (_c = (_b = resp.data) === null || _b === void 0 ? void 0 : _b.gas_info) === null || _c === void 0 ? void 0 : _c.gas_used) !== null && _d !== void 0 ? _d : null;
9808
+ if (!estimatedGas) {
9809
+ throw new Error('Could not get data of estimated gas');
9810
+ }
9811
+ return long_1.fromString(estimatedGas).multiply(multiplier || DEFAULT_GAS_ADJUSTMENT);
9812
+ });
5563
9813
  /**
5564
9814
  * Structure a MsgDeposit
5565
9815
  *
@@ -5585,7 +9835,7 @@ const buildDepositTx = ({ msgNativeTx, nodeUrl, chainId, }) => __awaiter(void 0,
5585
9835
  };
5586
9836
  const depositMsg = MsgCompiled.types.MsgDeposit.fromObject(msgDepositObj);
5587
9837
  return new proto.cosmos.tx.v1beta1.TxBody({
5588
- messages: [cosmosclient.codec.packAny(depositMsg)],
9838
+ messages: [cosmosclient.codec.instanceToProtoAny(depositMsg)],
5589
9839
  memo: msgNativeTx.memo,
5590
9840
  });
5591
9841
  });
@@ -5619,7 +9869,7 @@ const buildTransferTx = ({ fromAddress, toAddress, assetAmount, assetDenom, memo
5619
9869
  };
5620
9870
  const transferMsg = MsgCompiled.types.MsgSend.fromObject(transferObj);
5621
9871
  return new proto.cosmos.tx.v1beta1.TxBody({
5622
- messages: [cosmosclient.codec.packAny(transferMsg)],
9872
+ messages: [cosmosclient.codec.instanceToProtoAny(transferMsg)],
5623
9873
  memo,
5624
9874
  });
5625
9875
  });
@@ -5831,8 +10081,15 @@ class Client extends BaseXChainClient {
5831
10081
  * Thrown if network has not been set before.
5832
10082
  */
5833
10083
  setNetwork(network) {
10084
+ // dirty check to avoid using and re-creation of same data
10085
+ if (network === this.network)
10086
+ return;
5834
10087
  super.setNetwork(network);
5835
- this.cosmosClient.updatePrefix(getPrefix(this.network));
10088
+ this.cosmosClient = new CosmosSDKClient({
10089
+ server: this.getClientUrl().node,
10090
+ chainId: this.getChainId(network),
10091
+ prefix: getPrefix(network),
10092
+ });
5836
10093
  }
5837
10094
  /**
5838
10095
  * Set/update the client URL.
@@ -6041,9 +10298,9 @@ class Client extends BaseXChainClient {
6041
10298
  * @returns {TxHash} The transaction hash.
6042
10299
  *
6043
10300
  * @throws {"insufficient funds"} Thrown if the wallet has insufficient funds.
6044
- * @throws {"failed to broadcast transaction"} Thrown if failed to broadcast transaction.
10301
+ * @throws {"Invalid transaction hash"} Thrown by missing tx hash
6045
10302
  */
6046
- deposit({ walletIndex = 0, asset = AssetRuneNative, amount, memo }) {
10303
+ deposit({ walletIndex = 0, asset = AssetRuneNative, amount, memo, gasLimit = new bignumber(DEPOSIT_GAS_LIMIT_VALUE), }) {
6047
10304
  var _a, _b, _c, _d;
6048
10305
  return __awaiter(this, void 0, void 0, function* () {
6049
10306
  const balances = yield this.getBalance(this.getAddress(walletIndex));
@@ -6084,11 +10341,14 @@ class Client extends BaseXChainClient {
6084
10341
  const txBuilder = buildUnsignedTx({
6085
10342
  cosmosSdk: this.getCosmosClient().sdk,
6086
10343
  txBody: depositTxBody,
6087
- signerPubkey: cosmosclient.codec.packAny(signerPubkey),
6088
- gasLimit: DEPOSIT_GAS_VALUE,
6089
- sequence: account.sequence || cosmosclient.Long.ZERO,
10344
+ signerPubkey: cosmosclient.codec.instanceToProtoAny(signerPubkey),
10345
+ gasLimit: long_1.fromString(gasLimit.toFixed(0)),
10346
+ sequence: account.sequence || long_1.ZERO,
6090
10347
  });
6091
- return (yield this.getCosmosClient().signAndBroadcast(txBuilder, privKey, account)) || '';
10348
+ const txHash = yield this.getCosmosClient().signAndBroadcast(txBuilder, privKey, account);
10349
+ if (!txHash)
10350
+ throw Error(`Invalid transaction hash: ${txHash}`);
10351
+ return txHash;
6092
10352
  });
6093
10353
  }
6094
10354
  /**
@@ -6096,8 +10356,11 @@ class Client extends BaseXChainClient {
6096
10356
  *
6097
10357
  * @param {TxParams} params The transfer options.
6098
10358
  * @returns {TxHash} The transaction hash.
10359
+ *
10360
+ * @throws {"insufficient funds"} Thrown if the wallet has insufficient funds.
10361
+ * @throws {"Invalid transaction hash"} Thrown by missing tx hash
6099
10362
  */
6100
- transfer({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo }) {
10363
+ transfer({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo, gasLimit = new bignumber(DEFAULT_GAS_LIMIT_VALUE), }) {
6101
10364
  var _a, _b, _c, _d;
6102
10365
  return __awaiter(this, void 0, void 0, function* () {
6103
10366
  const balances = yield this.getBalance(this.getAddress(walletIndex));
@@ -6131,14 +10394,18 @@ class Client extends BaseXChainClient {
6131
10394
  nodeUrl: this.getClientUrl().node,
6132
10395
  });
6133
10396
  const account = yield this.getCosmosClient().getAccount(accAddress);
10397
+ const accountSequence = account.sequence || long_1.ZERO;
6134
10398
  const txBuilder = buildUnsignedTx({
6135
10399
  cosmosSdk: this.getCosmosClient().sdk,
6136
10400
  txBody: txBody,
6137
- gasLimit: DEFAULT_GAS_VALUE,
6138
- signerPubkey: cosmosclient.codec.packAny(signerPubkey),
6139
- sequence: account.sequence || cosmosclient.Long.ZERO,
10401
+ gasLimit: long_1.fromString(gasLimit.toString()),
10402
+ signerPubkey: cosmosclient.codec.instanceToProtoAny(signerPubkey),
10403
+ sequence: accountSequence,
6140
10404
  });
6141
- return (yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, account)) || '';
10405
+ const txHash = yield this.cosmosClient.signAndBroadcast(txBuilder, privKey, account);
10406
+ if (!txHash)
10407
+ throw Error(`Invalid transaction hash: ${txHash}`);
10408
+ return txHash;
6142
10409
  });
6143
10410
  }
6144
10411
  /**
@@ -6147,7 +10414,7 @@ class Client extends BaseXChainClient {
6147
10414
  * @param {TxOfflineParams} params The transfer offline options.
6148
10415
  * @returns {string} The signed transaction bytes.
6149
10416
  */
6150
- transferOffline({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo, from_rune_balance, from_asset_balance = baseAmount(0, DECIMAL), from_account_number = '0', from_sequence = '0', }) {
10417
+ transferOffline({ walletIndex = 0, asset = AssetRuneNative, amount, recipient, memo, fromRuneBalance: from_rune_balance, fromAssetBalance: from_asset_balance = baseAmount(0, DECIMAL), fromAccountNumber = long_1.ZERO, fromSequence = long_1.ZERO, gasLimit = new bignumber(DEFAULT_GAS_LIMIT_VALUE), }) {
6151
10418
  return __awaiter(this, void 0, void 0, function* () {
6152
10419
  const fee = (yield this.getFees()).average;
6153
10420
  if (isAssetRuneNative(asset)) {
@@ -6175,11 +10442,11 @@ class Client extends BaseXChainClient {
6175
10442
  const txBuilder = buildUnsignedTx({
6176
10443
  cosmosSdk: this.getCosmosClient().sdk,
6177
10444
  txBody: txBody,
6178
- gasLimit: DEFAULT_GAS_VALUE,
6179
- signerPubkey: cosmosclient.codec.packAny(privKey.pubKey()),
6180
- sequence: cosmosclient.Long.fromString(from_sequence) || cosmosclient.Long.ZERO,
10445
+ gasLimit: long_1.fromString(gasLimit.toFixed(0)),
10446
+ signerPubkey: cosmosclient.codec.instanceToProtoAny(privKey.pubKey()),
10447
+ sequence: fromSequence,
6181
10448
  });
6182
- const signDocBytes = txBuilder.signDocBytes(cosmosclient.Long.fromString(from_account_number));
10449
+ const signDocBytes = txBuilder.signDocBytes(fromAccountNumber);
6183
10450
  txBuilder.addSignature(privKey.sign(signDocBytes));
6184
10451
  return txBuilder.txBytes();
6185
10452
  });
@@ -6222,4 +10489,4 @@ const msgNativeTxFromJson = (value) => {
6222
10489
  return new MsgNativeTx(value.coins, value.memo, cosmosclient.AccAddress.fromString(value.signer));
6223
10490
  };
6224
10491
 
6225
- export { Client, DECIMAL, DEFAULT_GAS_VALUE, DEPOSIT_GAS_VALUE, MAX_TX_COUNT, MsgNativeTx, assetFromDenom, buildDepositTx, buildTransferTx, buildUnsignedTx, getBalance, getChainId, getChainIds, getDefaultClientUrl, getDefaultExplorerUrls, getDefaultFees, getDenom, getDepositTxDataFromLogs, getExplorerAddressUrl, getExplorerTxUrl, getExplorerUrl, getPrefix, getTxType, isAssetRuneNative, isBroadcastSuccess, msgNativeTxFromJson, registerDepositCodecs, registerSendCodecs };
10492
+ export { Client, DECIMAL, DEFAULT_GAS_ADJUSTMENT, DEFAULT_GAS_LIMIT_VALUE, DEPOSIT_GAS_LIMIT_VALUE, MAX_TX_COUNT, MsgNativeTx, assetFromDenom, buildDepositTx, buildTransferTx, buildUnsignedTx, getBalance, getChainId, getChainIds, getDefaultClientUrl, getDefaultExplorerUrls, getDefaultFees, getDenom, getDepositTxDataFromLogs, getEstimatedGas, getExplorerAddressUrl, getExplorerTxUrl, getExplorerUrl, getPrefix, getTxType, isAssetRuneNative, isBroadcastSuccess, msgNativeTxFromJson, registerDepositCodecs, registerSendCodecs };