@t2000/sdk 0.36.4 → 0.37.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/README.md +1 -1
- package/dist/adapters/index.cjs +4143 -130
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.js +4143 -130
- package/dist/adapters/index.js.map +1 -1
- package/dist/browser.d.cts +2 -2
- package/dist/browser.d.ts +2 -2
- package/dist/index.cjs +4261 -234
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4268 -241
- package/dist/index.js.map +1 -1
- package/dist/{token-registry-DGqUZaMz.d.cts → token-registry-DEZbGh2r.d.cts} +1 -1
- package/dist/{token-registry-9PgtYXoH.d.ts → token-registry-DkNy5t19.d.ts} +1 -1
- package/dist/{types-Clhh7dHT.d.cts → types-C6z5Pk_d.d.cts} +3 -1
- package/dist/{types-Clhh7dHT.d.ts → types-C6z5Pk_d.d.ts} +3 -1
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
var transactions = require('@mysten/sui/transactions');
|
|
4
4
|
var aggregatorSdk = require('@cetusprotocol/aggregator-sdk');
|
|
5
5
|
var eventemitter3 = require('eventemitter3');
|
|
6
|
+
var paymentKit = require('@mysten/payment-kit');
|
|
6
7
|
var jsonRpc = require('@mysten/sui/jsonRpc');
|
|
7
8
|
var utils = require('@mysten/sui/utils');
|
|
8
9
|
var ed25519 = require('@mysten/sui/keypairs/ed25519');
|
|
@@ -11,18 +12,48 @@ var crypto$1 = require('crypto');
|
|
|
11
12
|
var promises = require('fs/promises');
|
|
12
13
|
var path = require('path');
|
|
13
14
|
var os = require('os');
|
|
14
|
-
var
|
|
15
|
+
var pythSuiJs = require('@pythnetwork/pyth-sui-js');
|
|
16
|
+
var bcs$1 = require('@mysten/sui/bcs');
|
|
15
17
|
var fs = require('fs');
|
|
16
18
|
|
|
19
|
+
var __create = Object.create;
|
|
17
20
|
var __defProp = Object.defineProperty;
|
|
21
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
18
22
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
23
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
24
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
25
|
+
var __require = /* @__PURE__ */ ((x2) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x2, {
|
|
26
|
+
get: (a, b2) => (typeof require !== "undefined" ? require : a)[b2]
|
|
27
|
+
}) : x2)(function(x2) {
|
|
28
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
29
|
+
throw Error('Dynamic require of "' + x2 + '" is not supported');
|
|
30
|
+
});
|
|
19
31
|
var __esm = (fn, res) => function __init() {
|
|
20
32
|
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
21
33
|
};
|
|
34
|
+
var __commonJS = (cb, mod) => function __require2() {
|
|
35
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
36
|
+
};
|
|
22
37
|
var __export = (target, all) => {
|
|
23
38
|
for (var name in all)
|
|
24
39
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
40
|
};
|
|
41
|
+
var __copyProps = (to, from, except, desc) => {
|
|
42
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
43
|
+
for (let key of __getOwnPropNames(from))
|
|
44
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
45
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
46
|
+
}
|
|
47
|
+
return to;
|
|
48
|
+
};
|
|
49
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
50
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
51
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
52
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
53
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
54
|
+
!mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
55
|
+
mod
|
|
56
|
+
));
|
|
26
57
|
|
|
27
58
|
// src/errors.ts
|
|
28
59
|
var errors_exports = {};
|
|
@@ -187,12 +218,12 @@ var init_token_registry = __esm({
|
|
|
187
218
|
BY_TYPE.set(meta.type, meta);
|
|
188
219
|
}
|
|
189
220
|
exports.TOKEN_MAP = (() => {
|
|
190
|
-
const
|
|
221
|
+
const map2 = {};
|
|
191
222
|
for (const [name, meta] of Object.entries(exports.COIN_REGISTRY)) {
|
|
192
|
-
|
|
193
|
-
|
|
223
|
+
map2[name] = meta.type;
|
|
224
|
+
map2[name.toUpperCase()] = meta.type;
|
|
194
225
|
}
|
|
195
|
-
return
|
|
226
|
+
return map2;
|
|
196
227
|
})();
|
|
197
228
|
exports.SUI_TYPE = exports.COIN_REGISTRY.SUI.type;
|
|
198
229
|
exports.USDC_TYPE = exports.COIN_REGISTRY.USDC.type;
|
|
@@ -209,6 +240,377 @@ var init_token_registry = __esm({
|
|
|
209
240
|
}
|
|
210
241
|
});
|
|
211
242
|
|
|
243
|
+
// ../../node_modules/.pnpm/lodash.camelcase@4.3.0/node_modules/lodash.camelcase/index.js
|
|
244
|
+
var require_lodash = __commonJS({
|
|
245
|
+
"../../node_modules/.pnpm/lodash.camelcase@4.3.0/node_modules/lodash.camelcase/index.js"(exports$1, module) {
|
|
246
|
+
var symbolTag = "[object Symbol]";
|
|
247
|
+
var reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;
|
|
248
|
+
var reLatin = /[\xc0-\xd6\xd8-\xf6\xf8-\xff\u0100-\u017f]/g;
|
|
249
|
+
var rsAstralRange = "\\ud800-\\udfff";
|
|
250
|
+
var rsComboMarksRange = "\\u0300-\\u036f\\ufe20-\\ufe23";
|
|
251
|
+
var rsComboSymbolsRange = "\\u20d0-\\u20f0";
|
|
252
|
+
var rsDingbatRange = "\\u2700-\\u27bf";
|
|
253
|
+
var rsLowerRange = "a-z\\xdf-\\xf6\\xf8-\\xff";
|
|
254
|
+
var rsMathOpRange = "\\xac\\xb1\\xd7\\xf7";
|
|
255
|
+
var rsNonCharRange = "\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf";
|
|
256
|
+
var rsPunctuationRange = "\\u2000-\\u206f";
|
|
257
|
+
var rsSpaceRange = " \\t\\x0b\\f\\xa0\\ufeff\\n\\r\\u2028\\u2029\\u1680\\u180e\\u2000\\u2001\\u2002\\u2003\\u2004\\u2005\\u2006\\u2007\\u2008\\u2009\\u200a\\u202f\\u205f\\u3000";
|
|
258
|
+
var rsUpperRange = "A-Z\\xc0-\\xd6\\xd8-\\xde";
|
|
259
|
+
var rsVarRange = "\\ufe0e\\ufe0f";
|
|
260
|
+
var rsBreakRange = rsMathOpRange + rsNonCharRange + rsPunctuationRange + rsSpaceRange;
|
|
261
|
+
var rsApos = "['\u2019]";
|
|
262
|
+
var rsAstral = "[" + rsAstralRange + "]";
|
|
263
|
+
var rsBreak = "[" + rsBreakRange + "]";
|
|
264
|
+
var rsCombo = "[" + rsComboMarksRange + rsComboSymbolsRange + "]";
|
|
265
|
+
var rsDigits = "\\d+";
|
|
266
|
+
var rsDingbat = "[" + rsDingbatRange + "]";
|
|
267
|
+
var rsLower = "[" + rsLowerRange + "]";
|
|
268
|
+
var rsMisc = "[^" + rsAstralRange + rsBreakRange + rsDigits + rsDingbatRange + rsLowerRange + rsUpperRange + "]";
|
|
269
|
+
var rsFitz = "\\ud83c[\\udffb-\\udfff]";
|
|
270
|
+
var rsModifier = "(?:" + rsCombo + "|" + rsFitz + ")";
|
|
271
|
+
var rsNonAstral = "[^" + rsAstralRange + "]";
|
|
272
|
+
var rsRegional = "(?:\\ud83c[\\udde6-\\uddff]){2}";
|
|
273
|
+
var rsSurrPair = "[\\ud800-\\udbff][\\udc00-\\udfff]";
|
|
274
|
+
var rsUpper = "[" + rsUpperRange + "]";
|
|
275
|
+
var rsZWJ = "\\u200d";
|
|
276
|
+
var rsLowerMisc = "(?:" + rsLower + "|" + rsMisc + ")";
|
|
277
|
+
var rsUpperMisc = "(?:" + rsUpper + "|" + rsMisc + ")";
|
|
278
|
+
var rsOptLowerContr = "(?:" + rsApos + "(?:d|ll|m|re|s|t|ve))?";
|
|
279
|
+
var rsOptUpperContr = "(?:" + rsApos + "(?:D|LL|M|RE|S|T|VE))?";
|
|
280
|
+
var reOptMod = rsModifier + "?";
|
|
281
|
+
var rsOptVar = "[" + rsVarRange + "]?";
|
|
282
|
+
var rsOptJoin = "(?:" + rsZWJ + "(?:" + [rsNonAstral, rsRegional, rsSurrPair].join("|") + ")" + rsOptVar + reOptMod + ")*";
|
|
283
|
+
var rsSeq = rsOptVar + reOptMod + rsOptJoin;
|
|
284
|
+
var rsEmoji = "(?:" + [rsDingbat, rsRegional, rsSurrPair].join("|") + ")" + rsSeq;
|
|
285
|
+
var rsSymbol = "(?:" + [rsNonAstral + rsCombo + "?", rsCombo, rsRegional, rsSurrPair, rsAstral].join("|") + ")";
|
|
286
|
+
var reApos = RegExp(rsApos, "g");
|
|
287
|
+
var reComboMark = RegExp(rsCombo, "g");
|
|
288
|
+
var reUnicode = RegExp(rsFitz + "(?=" + rsFitz + ")|" + rsSymbol + rsSeq, "g");
|
|
289
|
+
var reUnicodeWord = RegExp([
|
|
290
|
+
rsUpper + "?" + rsLower + "+" + rsOptLowerContr + "(?=" + [rsBreak, rsUpper, "$"].join("|") + ")",
|
|
291
|
+
rsUpperMisc + "+" + rsOptUpperContr + "(?=" + [rsBreak, rsUpper + rsLowerMisc, "$"].join("|") + ")",
|
|
292
|
+
rsUpper + "?" + rsLowerMisc + "+" + rsOptLowerContr,
|
|
293
|
+
rsUpper + "+" + rsOptUpperContr,
|
|
294
|
+
rsDigits,
|
|
295
|
+
rsEmoji
|
|
296
|
+
].join("|"), "g");
|
|
297
|
+
var reHasUnicode = RegExp("[" + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + "]");
|
|
298
|
+
var reHasUnicodeWord = /[a-z][A-Z]|[A-Z]{2,}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/;
|
|
299
|
+
var deburredLetters = {
|
|
300
|
+
// Latin-1 Supplement block.
|
|
301
|
+
"\xC0": "A",
|
|
302
|
+
"\xC1": "A",
|
|
303
|
+
"\xC2": "A",
|
|
304
|
+
"\xC3": "A",
|
|
305
|
+
"\xC4": "A",
|
|
306
|
+
"\xC5": "A",
|
|
307
|
+
"\xE0": "a",
|
|
308
|
+
"\xE1": "a",
|
|
309
|
+
"\xE2": "a",
|
|
310
|
+
"\xE3": "a",
|
|
311
|
+
"\xE4": "a",
|
|
312
|
+
"\xE5": "a",
|
|
313
|
+
"\xC7": "C",
|
|
314
|
+
"\xE7": "c",
|
|
315
|
+
"\xD0": "D",
|
|
316
|
+
"\xF0": "d",
|
|
317
|
+
"\xC8": "E",
|
|
318
|
+
"\xC9": "E",
|
|
319
|
+
"\xCA": "E",
|
|
320
|
+
"\xCB": "E",
|
|
321
|
+
"\xE8": "e",
|
|
322
|
+
"\xE9": "e",
|
|
323
|
+
"\xEA": "e",
|
|
324
|
+
"\xEB": "e",
|
|
325
|
+
"\xCC": "I",
|
|
326
|
+
"\xCD": "I",
|
|
327
|
+
"\xCE": "I",
|
|
328
|
+
"\xCF": "I",
|
|
329
|
+
"\xEC": "i",
|
|
330
|
+
"\xED": "i",
|
|
331
|
+
"\xEE": "i",
|
|
332
|
+
"\xEF": "i",
|
|
333
|
+
"\xD1": "N",
|
|
334
|
+
"\xF1": "n",
|
|
335
|
+
"\xD2": "O",
|
|
336
|
+
"\xD3": "O",
|
|
337
|
+
"\xD4": "O",
|
|
338
|
+
"\xD5": "O",
|
|
339
|
+
"\xD6": "O",
|
|
340
|
+
"\xD8": "O",
|
|
341
|
+
"\xF2": "o",
|
|
342
|
+
"\xF3": "o",
|
|
343
|
+
"\xF4": "o",
|
|
344
|
+
"\xF5": "o",
|
|
345
|
+
"\xF6": "o",
|
|
346
|
+
"\xF8": "o",
|
|
347
|
+
"\xD9": "U",
|
|
348
|
+
"\xDA": "U",
|
|
349
|
+
"\xDB": "U",
|
|
350
|
+
"\xDC": "U",
|
|
351
|
+
"\xF9": "u",
|
|
352
|
+
"\xFA": "u",
|
|
353
|
+
"\xFB": "u",
|
|
354
|
+
"\xFC": "u",
|
|
355
|
+
"\xDD": "Y",
|
|
356
|
+
"\xFD": "y",
|
|
357
|
+
"\xFF": "y",
|
|
358
|
+
"\xC6": "Ae",
|
|
359
|
+
"\xE6": "ae",
|
|
360
|
+
"\xDE": "Th",
|
|
361
|
+
"\xFE": "th",
|
|
362
|
+
"\xDF": "ss",
|
|
363
|
+
// Latin Extended-A block.
|
|
364
|
+
"\u0100": "A",
|
|
365
|
+
"\u0102": "A",
|
|
366
|
+
"\u0104": "A",
|
|
367
|
+
"\u0101": "a",
|
|
368
|
+
"\u0103": "a",
|
|
369
|
+
"\u0105": "a",
|
|
370
|
+
"\u0106": "C",
|
|
371
|
+
"\u0108": "C",
|
|
372
|
+
"\u010A": "C",
|
|
373
|
+
"\u010C": "C",
|
|
374
|
+
"\u0107": "c",
|
|
375
|
+
"\u0109": "c",
|
|
376
|
+
"\u010B": "c",
|
|
377
|
+
"\u010D": "c",
|
|
378
|
+
"\u010E": "D",
|
|
379
|
+
"\u0110": "D",
|
|
380
|
+
"\u010F": "d",
|
|
381
|
+
"\u0111": "d",
|
|
382
|
+
"\u0112": "E",
|
|
383
|
+
"\u0114": "E",
|
|
384
|
+
"\u0116": "E",
|
|
385
|
+
"\u0118": "E",
|
|
386
|
+
"\u011A": "E",
|
|
387
|
+
"\u0113": "e",
|
|
388
|
+
"\u0115": "e",
|
|
389
|
+
"\u0117": "e",
|
|
390
|
+
"\u0119": "e",
|
|
391
|
+
"\u011B": "e",
|
|
392
|
+
"\u011C": "G",
|
|
393
|
+
"\u011E": "G",
|
|
394
|
+
"\u0120": "G",
|
|
395
|
+
"\u0122": "G",
|
|
396
|
+
"\u011D": "g",
|
|
397
|
+
"\u011F": "g",
|
|
398
|
+
"\u0121": "g",
|
|
399
|
+
"\u0123": "g",
|
|
400
|
+
"\u0124": "H",
|
|
401
|
+
"\u0126": "H",
|
|
402
|
+
"\u0125": "h",
|
|
403
|
+
"\u0127": "h",
|
|
404
|
+
"\u0128": "I",
|
|
405
|
+
"\u012A": "I",
|
|
406
|
+
"\u012C": "I",
|
|
407
|
+
"\u012E": "I",
|
|
408
|
+
"\u0130": "I",
|
|
409
|
+
"\u0129": "i",
|
|
410
|
+
"\u012B": "i",
|
|
411
|
+
"\u012D": "i",
|
|
412
|
+
"\u012F": "i",
|
|
413
|
+
"\u0131": "i",
|
|
414
|
+
"\u0134": "J",
|
|
415
|
+
"\u0135": "j",
|
|
416
|
+
"\u0136": "K",
|
|
417
|
+
"\u0137": "k",
|
|
418
|
+
"\u0138": "k",
|
|
419
|
+
"\u0139": "L",
|
|
420
|
+
"\u013B": "L",
|
|
421
|
+
"\u013D": "L",
|
|
422
|
+
"\u013F": "L",
|
|
423
|
+
"\u0141": "L",
|
|
424
|
+
"\u013A": "l",
|
|
425
|
+
"\u013C": "l",
|
|
426
|
+
"\u013E": "l",
|
|
427
|
+
"\u0140": "l",
|
|
428
|
+
"\u0142": "l",
|
|
429
|
+
"\u0143": "N",
|
|
430
|
+
"\u0145": "N",
|
|
431
|
+
"\u0147": "N",
|
|
432
|
+
"\u014A": "N",
|
|
433
|
+
"\u0144": "n",
|
|
434
|
+
"\u0146": "n",
|
|
435
|
+
"\u0148": "n",
|
|
436
|
+
"\u014B": "n",
|
|
437
|
+
"\u014C": "O",
|
|
438
|
+
"\u014E": "O",
|
|
439
|
+
"\u0150": "O",
|
|
440
|
+
"\u014D": "o",
|
|
441
|
+
"\u014F": "o",
|
|
442
|
+
"\u0151": "o",
|
|
443
|
+
"\u0154": "R",
|
|
444
|
+
"\u0156": "R",
|
|
445
|
+
"\u0158": "R",
|
|
446
|
+
"\u0155": "r",
|
|
447
|
+
"\u0157": "r",
|
|
448
|
+
"\u0159": "r",
|
|
449
|
+
"\u015A": "S",
|
|
450
|
+
"\u015C": "S",
|
|
451
|
+
"\u015E": "S",
|
|
452
|
+
"\u0160": "S",
|
|
453
|
+
"\u015B": "s",
|
|
454
|
+
"\u015D": "s",
|
|
455
|
+
"\u015F": "s",
|
|
456
|
+
"\u0161": "s",
|
|
457
|
+
"\u0162": "T",
|
|
458
|
+
"\u0164": "T",
|
|
459
|
+
"\u0166": "T",
|
|
460
|
+
"\u0163": "t",
|
|
461
|
+
"\u0165": "t",
|
|
462
|
+
"\u0167": "t",
|
|
463
|
+
"\u0168": "U",
|
|
464
|
+
"\u016A": "U",
|
|
465
|
+
"\u016C": "U",
|
|
466
|
+
"\u016E": "U",
|
|
467
|
+
"\u0170": "U",
|
|
468
|
+
"\u0172": "U",
|
|
469
|
+
"\u0169": "u",
|
|
470
|
+
"\u016B": "u",
|
|
471
|
+
"\u016D": "u",
|
|
472
|
+
"\u016F": "u",
|
|
473
|
+
"\u0171": "u",
|
|
474
|
+
"\u0173": "u",
|
|
475
|
+
"\u0174": "W",
|
|
476
|
+
"\u0175": "w",
|
|
477
|
+
"\u0176": "Y",
|
|
478
|
+
"\u0177": "y",
|
|
479
|
+
"\u0178": "Y",
|
|
480
|
+
"\u0179": "Z",
|
|
481
|
+
"\u017B": "Z",
|
|
482
|
+
"\u017D": "Z",
|
|
483
|
+
"\u017A": "z",
|
|
484
|
+
"\u017C": "z",
|
|
485
|
+
"\u017E": "z",
|
|
486
|
+
"\u0132": "IJ",
|
|
487
|
+
"\u0133": "ij",
|
|
488
|
+
"\u0152": "Oe",
|
|
489
|
+
"\u0153": "oe",
|
|
490
|
+
"\u0149": "'n",
|
|
491
|
+
"\u017F": "ss"
|
|
492
|
+
};
|
|
493
|
+
var freeGlobal = typeof global == "object" && global && global.Object === Object && global;
|
|
494
|
+
var freeSelf = typeof self == "object" && self && self.Object === Object && self;
|
|
495
|
+
var root = freeGlobal || freeSelf || Function("return this")();
|
|
496
|
+
function arrayReduce(array, iteratee, accumulator, initAccum) {
|
|
497
|
+
var index = -1, length = array ? array.length : 0;
|
|
498
|
+
while (++index < length) {
|
|
499
|
+
accumulator = iteratee(accumulator, array[index], index, array);
|
|
500
|
+
}
|
|
501
|
+
return accumulator;
|
|
502
|
+
}
|
|
503
|
+
function asciiToArray(string) {
|
|
504
|
+
return string.split("");
|
|
505
|
+
}
|
|
506
|
+
function asciiWords(string) {
|
|
507
|
+
return string.match(reAsciiWord) || [];
|
|
508
|
+
}
|
|
509
|
+
function basePropertyOf(object) {
|
|
510
|
+
return function(key) {
|
|
511
|
+
return object == null ? void 0 : object[key];
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
var deburrLetter = basePropertyOf(deburredLetters);
|
|
515
|
+
function hasUnicode(string) {
|
|
516
|
+
return reHasUnicode.test(string);
|
|
517
|
+
}
|
|
518
|
+
function hasUnicodeWord(string) {
|
|
519
|
+
return reHasUnicodeWord.test(string);
|
|
520
|
+
}
|
|
521
|
+
function stringToArray(string) {
|
|
522
|
+
return hasUnicode(string) ? unicodeToArray(string) : asciiToArray(string);
|
|
523
|
+
}
|
|
524
|
+
function unicodeToArray(string) {
|
|
525
|
+
return string.match(reUnicode) || [];
|
|
526
|
+
}
|
|
527
|
+
function unicodeWords(string) {
|
|
528
|
+
return string.match(reUnicodeWord) || [];
|
|
529
|
+
}
|
|
530
|
+
var objectProto = Object.prototype;
|
|
531
|
+
var objectToString = objectProto.toString;
|
|
532
|
+
var Symbol2 = root.Symbol;
|
|
533
|
+
var symbolProto = Symbol2 ? Symbol2.prototype : void 0;
|
|
534
|
+
var symbolToString = symbolProto ? symbolProto.toString : void 0;
|
|
535
|
+
function baseSlice(array, start, end) {
|
|
536
|
+
var index = -1, length = array.length;
|
|
537
|
+
if (start < 0) {
|
|
538
|
+
start = -start > length ? 0 : length + start;
|
|
539
|
+
}
|
|
540
|
+
end = end > length ? length : end;
|
|
541
|
+
if (end < 0) {
|
|
542
|
+
end += length;
|
|
543
|
+
}
|
|
544
|
+
length = start > end ? 0 : end - start >>> 0;
|
|
545
|
+
start >>>= 0;
|
|
546
|
+
var result = Array(length);
|
|
547
|
+
while (++index < length) {
|
|
548
|
+
result[index] = array[index + start];
|
|
549
|
+
}
|
|
550
|
+
return result;
|
|
551
|
+
}
|
|
552
|
+
function baseToString(value) {
|
|
553
|
+
if (typeof value == "string") {
|
|
554
|
+
return value;
|
|
555
|
+
}
|
|
556
|
+
if (isSymbol(value)) {
|
|
557
|
+
return symbolToString ? symbolToString.call(value) : "";
|
|
558
|
+
}
|
|
559
|
+
var result = value + "";
|
|
560
|
+
return result == "0" && 1 / value == -Infinity ? "-0" : result;
|
|
561
|
+
}
|
|
562
|
+
function castSlice(array, start, end) {
|
|
563
|
+
var length = array.length;
|
|
564
|
+
end = end === void 0 ? length : end;
|
|
565
|
+
return !start && end >= length ? array : baseSlice(array, start, end);
|
|
566
|
+
}
|
|
567
|
+
function createCaseFirst(methodName) {
|
|
568
|
+
return function(string) {
|
|
569
|
+
string = toString(string);
|
|
570
|
+
var strSymbols = hasUnicode(string) ? stringToArray(string) : void 0;
|
|
571
|
+
var chr = strSymbols ? strSymbols[0] : string.charAt(0);
|
|
572
|
+
var trailing = strSymbols ? castSlice(strSymbols, 1).join("") : string.slice(1);
|
|
573
|
+
return chr[methodName]() + trailing;
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
function createCompounder(callback) {
|
|
577
|
+
return function(string) {
|
|
578
|
+
return arrayReduce(words(deburr(string).replace(reApos, "")), callback, "");
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
function isObjectLike(value) {
|
|
582
|
+
return !!value && typeof value == "object";
|
|
583
|
+
}
|
|
584
|
+
function isSymbol(value) {
|
|
585
|
+
return typeof value == "symbol" || isObjectLike(value) && objectToString.call(value) == symbolTag;
|
|
586
|
+
}
|
|
587
|
+
function toString(value) {
|
|
588
|
+
return value == null ? "" : baseToString(value);
|
|
589
|
+
}
|
|
590
|
+
var camelCase = createCompounder(function(result, word, index) {
|
|
591
|
+
word = word.toLowerCase();
|
|
592
|
+
return result + (index ? capitalize(word) : word);
|
|
593
|
+
});
|
|
594
|
+
function capitalize(string) {
|
|
595
|
+
return upperFirst(toString(string).toLowerCase());
|
|
596
|
+
}
|
|
597
|
+
function deburr(string) {
|
|
598
|
+
string = toString(string);
|
|
599
|
+
return string && string.replace(reLatin, deburrLetter).replace(reComboMark, "");
|
|
600
|
+
}
|
|
601
|
+
var upperFirst = createCaseFirst("toUpperCase");
|
|
602
|
+
function words(string, pattern, guard) {
|
|
603
|
+
string = toString(string);
|
|
604
|
+
pattern = pattern;
|
|
605
|
+
if (pattern === void 0) {
|
|
606
|
+
return hasUnicodeWord(string) ? unicodeWords(string) : asciiWords(string);
|
|
607
|
+
}
|
|
608
|
+
return string.match(pattern) || [];
|
|
609
|
+
}
|
|
610
|
+
module.exports = camelCase;
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
|
|
212
614
|
// src/protocols/volo.ts
|
|
213
615
|
var volo_exports = {};
|
|
214
616
|
__export(volo_exports, {
|
|
@@ -776,214 +1178,3818 @@ async function fetchSuiPrice(client) {
|
|
|
776
1178
|
return _cachedSuiPrice;
|
|
777
1179
|
}
|
|
778
1180
|
try {
|
|
779
|
-
const pool = await client.getObject({
|
|
780
|
-
id: CETUS_USDC_SUI_POOL,
|
|
1181
|
+
const pool = await client.getObject({
|
|
1182
|
+
id: CETUS_USDC_SUI_POOL,
|
|
1183
|
+
options: { showContent: true }
|
|
1184
|
+
});
|
|
1185
|
+
if (pool.data?.content?.dataType === "moveObject") {
|
|
1186
|
+
const fields = pool.data.content.fields;
|
|
1187
|
+
const currentSqrtPrice = BigInt(String(fields.current_sqrt_price ?? "0"));
|
|
1188
|
+
if (currentSqrtPrice > 0n) {
|
|
1189
|
+
const Q64 = 2n ** 64n;
|
|
1190
|
+
const sqrtPriceFloat = Number(currentSqrtPrice) / Number(Q64);
|
|
1191
|
+
const rawPrice = sqrtPriceFloat * sqrtPriceFloat;
|
|
1192
|
+
const price = 1e3 / rawPrice;
|
|
1193
|
+
if (price > 0.01 && price < 1e3) {
|
|
1194
|
+
_cachedSuiPrice = price;
|
|
1195
|
+
_priceLastFetched = now;
|
|
1196
|
+
}
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
} catch {
|
|
1200
|
+
}
|
|
1201
|
+
return _cachedSuiPrice || SUI_PRICE_FALLBACK;
|
|
1202
|
+
}
|
|
1203
|
+
async function queryBalance(client, address) {
|
|
1204
|
+
const stableBalancePromises = STABLE_ASSETS.map(
|
|
1205
|
+
(asset) => client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS[asset].type }).then((b2) => ({ asset, amount: Number(b2.totalBalance) / 10 ** SUPPORTED_ASSETS[asset].decimals })).catch(() => ({ asset, amount: 0 }))
|
|
1206
|
+
);
|
|
1207
|
+
const [suiBalance, suiPriceUsd, ...stableResults] = await Promise.all([
|
|
1208
|
+
client.getBalance({ owner: address, coinType: SUPPORTED_ASSETS.SUI.type }),
|
|
1209
|
+
fetchSuiPrice(client),
|
|
1210
|
+
...stableBalancePromises
|
|
1211
|
+
]);
|
|
1212
|
+
const stables = {};
|
|
1213
|
+
let totalStables = 0;
|
|
1214
|
+
for (const { asset, amount } of stableResults) {
|
|
1215
|
+
stables[asset] = amount;
|
|
1216
|
+
totalStables += amount;
|
|
1217
|
+
}
|
|
1218
|
+
const suiAmount = Number(suiBalance.totalBalance) / Number(MIST_PER_SUI);
|
|
1219
|
+
const savings = 0;
|
|
1220
|
+
const usdEquiv = suiAmount * suiPriceUsd;
|
|
1221
|
+
const total = totalStables + savings + usdEquiv;
|
|
1222
|
+
return {
|
|
1223
|
+
available: totalStables,
|
|
1224
|
+
savings,
|
|
1225
|
+
debt: 0,
|
|
1226
|
+
pendingRewards: 0,
|
|
1227
|
+
gasReserve: {
|
|
1228
|
+
sui: suiAmount,
|
|
1229
|
+
usdEquiv
|
|
1230
|
+
},
|
|
1231
|
+
total,
|
|
1232
|
+
stables
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
// src/wallet/history.ts
|
|
1237
|
+
init_token_registry();
|
|
1238
|
+
var KNOWN_TARGETS = [
|
|
1239
|
+
[/::suilend|::obligation/, "lending"],
|
|
1240
|
+
[/::navi|::incentive_v\d+|::oracle_pro/, "lending"],
|
|
1241
|
+
[/::cetus|::pool/, "swap"],
|
|
1242
|
+
[/::deepbook/, "swap"],
|
|
1243
|
+
[/::transfer::public_transfer/, "send"]
|
|
1244
|
+
];
|
|
1245
|
+
async function queryHistory(client, address, limit = 20) {
|
|
1246
|
+
const txns = await client.queryTransactionBlocks({
|
|
1247
|
+
filter: { FromAddress: address },
|
|
1248
|
+
options: { showEffects: true, showInput: true, showBalanceChanges: true },
|
|
1249
|
+
limit,
|
|
1250
|
+
order: "descending"
|
|
1251
|
+
});
|
|
1252
|
+
return txns.data.map((tx) => parseTxRecord(tx, address));
|
|
1253
|
+
}
|
|
1254
|
+
async function queryTransaction(client, digest, senderAddress) {
|
|
1255
|
+
try {
|
|
1256
|
+
const tx = await client.getTransactionBlock({
|
|
1257
|
+
digest,
|
|
1258
|
+
options: { showEffects: true, showInput: true, showBalanceChanges: true }
|
|
1259
|
+
});
|
|
1260
|
+
return parseTxRecord(tx, senderAddress);
|
|
1261
|
+
} catch {
|
|
1262
|
+
return null;
|
|
1263
|
+
}
|
|
1264
|
+
}
|
|
1265
|
+
function parseTxRecord(tx, address) {
|
|
1266
|
+
const gasUsed = tx.effects?.gasUsed;
|
|
1267
|
+
const gasCost = gasUsed ? (Number(gasUsed.computationCost) + Number(gasUsed.storageCost) - Number(gasUsed.storageRebate)) / 1e9 : void 0;
|
|
1268
|
+
const { moveCallTargets, commandTypes } = extractCommands(tx.transaction);
|
|
1269
|
+
const { amount, asset, recipient } = extractTransferDetails(tx.balanceChanges, address);
|
|
1270
|
+
const action = classifyAction(moveCallTargets, commandTypes);
|
|
1271
|
+
return {
|
|
1272
|
+
digest: tx.digest,
|
|
1273
|
+
action,
|
|
1274
|
+
amount,
|
|
1275
|
+
asset,
|
|
1276
|
+
recipient,
|
|
1277
|
+
timestamp: Number(tx.timestampMs ?? 0),
|
|
1278
|
+
gasCost
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
function resolveOwner(owner) {
|
|
1282
|
+
if (typeof owner === "object" && owner.AddressOwner) return owner.AddressOwner;
|
|
1283
|
+
if (typeof owner === "string") return owner;
|
|
1284
|
+
return null;
|
|
1285
|
+
}
|
|
1286
|
+
function extractTransferDetails(changes, sender) {
|
|
1287
|
+
if (!changes || changes.length === 0) return {};
|
|
1288
|
+
const outflows = changes.filter((c) => resolveOwner(c.owner) === sender && BigInt(c.amount) < 0n);
|
|
1289
|
+
const inflows = changes.filter((c) => resolveOwner(c.owner) !== sender && BigInt(c.amount) > 0n);
|
|
1290
|
+
const primaryOutflow = outflows.filter((c) => c.coinType !== exports.SUI_TYPE).sort((a, b2) => Number(BigInt(a.amount) - BigInt(b2.amount)))[0] ?? outflows[0];
|
|
1291
|
+
if (!primaryOutflow) return {};
|
|
1292
|
+
const coinType = primaryOutflow.coinType;
|
|
1293
|
+
const decimals = getDecimalsForCoinType(coinType);
|
|
1294
|
+
const amount = Math.abs(Number(BigInt(primaryOutflow.amount))) / 10 ** decimals;
|
|
1295
|
+
const asset = resolveSymbol(coinType);
|
|
1296
|
+
const recipientChange = inflows.find((c) => c.coinType === coinType);
|
|
1297
|
+
const recipient = recipientChange ? resolveOwner(recipientChange.owner) ?? void 0 : void 0;
|
|
1298
|
+
return { amount, asset, recipient };
|
|
1299
|
+
}
|
|
1300
|
+
function extractCommands(txBlock) {
|
|
1301
|
+
const result = { moveCallTargets: [], commandTypes: [] };
|
|
1302
|
+
try {
|
|
1303
|
+
if (!txBlock || typeof txBlock !== "object") return result;
|
|
1304
|
+
const data = "data" in txBlock ? txBlock.data : void 0;
|
|
1305
|
+
if (!data || typeof data !== "object") return result;
|
|
1306
|
+
const inner = "transaction" in data ? data.transaction : void 0;
|
|
1307
|
+
if (!inner || typeof inner !== "object") return result;
|
|
1308
|
+
const commands = "commands" in inner ? inner.commands : "transactions" in inner ? inner.transactions : void 0;
|
|
1309
|
+
if (!Array.isArray(commands)) return result;
|
|
1310
|
+
for (const cmd of commands) {
|
|
1311
|
+
if (cmd.MoveCall) {
|
|
1312
|
+
const mc = cmd.MoveCall;
|
|
1313
|
+
result.moveCallTargets.push(`${mc.package}::${mc.module}::${mc.function}`);
|
|
1314
|
+
result.commandTypes.push("MoveCall");
|
|
1315
|
+
} else if (cmd.TransferObjects) {
|
|
1316
|
+
result.commandTypes.push("TransferObjects");
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
} catch {
|
|
1320
|
+
}
|
|
1321
|
+
return result;
|
|
1322
|
+
}
|
|
1323
|
+
function classifyAction(targets, commandTypes) {
|
|
1324
|
+
for (const target of targets) {
|
|
1325
|
+
for (const [pattern, label] of KNOWN_TARGETS) {
|
|
1326
|
+
if (pattern.test(target)) return label;
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
const hasTransfer = commandTypes.includes("TransferObjects");
|
|
1330
|
+
const hasMoveCall = commandTypes.includes("MoveCall");
|
|
1331
|
+
if (hasTransfer && !hasMoveCall) return "send";
|
|
1332
|
+
if (hasMoveCall) return "transaction";
|
|
1333
|
+
return "transaction";
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// src/t2000.ts
|
|
1337
|
+
init_token_registry();
|
|
1338
|
+
|
|
1339
|
+
// src/protocols/protocolFee.ts
|
|
1340
|
+
var FEE_RATES = {
|
|
1341
|
+
save: SAVE_FEE_BPS,
|
|
1342
|
+
borrow: BORROW_FEE_BPS
|
|
1343
|
+
};
|
|
1344
|
+
var OP_CODES = {
|
|
1345
|
+
save: 0,
|
|
1346
|
+
borrow: 2
|
|
1347
|
+
};
|
|
1348
|
+
function calculateFee(operation, amount) {
|
|
1349
|
+
const bps = FEE_RATES[operation];
|
|
1350
|
+
const feeAmount = amount * Number(bps) / Number(BPS_DENOMINATOR);
|
|
1351
|
+
const rawAmount = usdcToRaw(feeAmount);
|
|
1352
|
+
return {
|
|
1353
|
+
amount: feeAmount,
|
|
1354
|
+
asset: "USDC",
|
|
1355
|
+
rate: Number(bps) / Number(BPS_DENOMINATOR),
|
|
1356
|
+
rawAmount
|
|
1357
|
+
};
|
|
1358
|
+
}
|
|
1359
|
+
function addCollectFeeToTx(tx, paymentCoin, operation) {
|
|
1360
|
+
const bps = FEE_RATES[operation];
|
|
1361
|
+
if (bps <= 0n) return;
|
|
1362
|
+
tx.moveCall({
|
|
1363
|
+
target: `${T2000_PACKAGE_ID}::treasury::collect_fee`,
|
|
1364
|
+
typeArguments: [SUPPORTED_ASSETS.USDC.type],
|
|
1365
|
+
arguments: [
|
|
1366
|
+
tx.object(T2000_TREASURY_ID),
|
|
1367
|
+
tx.object(T2000_CONFIG_ID),
|
|
1368
|
+
paymentCoin,
|
|
1369
|
+
tx.pure.u8(OP_CODES[operation])
|
|
1370
|
+
]
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
async function reportFee(agentAddress, operation, feeAmount, feeRate, txDigest) {
|
|
1374
|
+
try {
|
|
1375
|
+
await fetch(`${API_BASE_URL}/api/fees`, {
|
|
1376
|
+
method: "POST",
|
|
1377
|
+
headers: { "Content-Type": "application/json" },
|
|
1378
|
+
body: JSON.stringify({
|
|
1379
|
+
agentAddress,
|
|
1380
|
+
operation,
|
|
1381
|
+
feeAmount: feeAmount.toString(),
|
|
1382
|
+
feeRate: feeRate.toString(),
|
|
1383
|
+
txDigest
|
|
1384
|
+
})
|
|
1385
|
+
});
|
|
1386
|
+
} catch {
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
|
|
1390
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/uleb.mjs
|
|
1391
|
+
function ulebEncode(num) {
|
|
1392
|
+
let bigNum = BigInt(num);
|
|
1393
|
+
const arr = [];
|
|
1394
|
+
let len = 0;
|
|
1395
|
+
if (bigNum === 0n) return [0];
|
|
1396
|
+
while (bigNum > 0) {
|
|
1397
|
+
arr[len] = Number(bigNum & 127n);
|
|
1398
|
+
bigNum >>= 7n;
|
|
1399
|
+
if (bigNum > 0n) arr[len] |= 128;
|
|
1400
|
+
len += 1;
|
|
1401
|
+
}
|
|
1402
|
+
return arr;
|
|
1403
|
+
}
|
|
1404
|
+
function ulebDecode(arr) {
|
|
1405
|
+
let total = 0n;
|
|
1406
|
+
let shift = 0n;
|
|
1407
|
+
let len = 0;
|
|
1408
|
+
while (true) {
|
|
1409
|
+
if (len >= arr.length) throw new Error("ULEB decode error: buffer overflow");
|
|
1410
|
+
const byte = arr[len];
|
|
1411
|
+
len += 1;
|
|
1412
|
+
total += BigInt(byte & 127) << shift;
|
|
1413
|
+
if ((byte & 128) === 0) break;
|
|
1414
|
+
shift += 7n;
|
|
1415
|
+
}
|
|
1416
|
+
if (total > BigInt(Number.MAX_SAFE_INTEGER)) throw new Error("ULEB decode error: value exceeds MAX_SAFE_INTEGER");
|
|
1417
|
+
return {
|
|
1418
|
+
value: Number(total),
|
|
1419
|
+
length: len
|
|
1420
|
+
};
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/reader.mjs
|
|
1424
|
+
var BcsReader = class {
|
|
1425
|
+
/**
|
|
1426
|
+
* @param {Uint8Array} data Data to use as a buffer.
|
|
1427
|
+
*/
|
|
1428
|
+
constructor(data) {
|
|
1429
|
+
this.bytePosition = 0;
|
|
1430
|
+
this.dataView = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
1431
|
+
}
|
|
1432
|
+
/**
|
|
1433
|
+
* Shift current cursor position by `bytes`.
|
|
1434
|
+
*
|
|
1435
|
+
* @param {Number} bytes Number of bytes to
|
|
1436
|
+
* @returns {this} Self for possible chaining.
|
|
1437
|
+
*/
|
|
1438
|
+
shift(bytes) {
|
|
1439
|
+
this.bytePosition += bytes;
|
|
1440
|
+
return this;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Read U8 value from the buffer and shift cursor by 1.
|
|
1444
|
+
* @returns
|
|
1445
|
+
*/
|
|
1446
|
+
read8() {
|
|
1447
|
+
const value = this.dataView.getUint8(this.bytePosition);
|
|
1448
|
+
this.shift(1);
|
|
1449
|
+
return value;
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
* Read U16 value from the buffer and shift cursor by 2.
|
|
1453
|
+
* @returns
|
|
1454
|
+
*/
|
|
1455
|
+
read16() {
|
|
1456
|
+
const value = this.dataView.getUint16(this.bytePosition, true);
|
|
1457
|
+
this.shift(2);
|
|
1458
|
+
return value;
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* Read U32 value from the buffer and shift cursor by 4.
|
|
1462
|
+
* @returns
|
|
1463
|
+
*/
|
|
1464
|
+
read32() {
|
|
1465
|
+
const value = this.dataView.getUint32(this.bytePosition, true);
|
|
1466
|
+
this.shift(4);
|
|
1467
|
+
return value;
|
|
1468
|
+
}
|
|
1469
|
+
/**
|
|
1470
|
+
* Read U64 value from the buffer and shift cursor by 8.
|
|
1471
|
+
* @returns
|
|
1472
|
+
*/
|
|
1473
|
+
read64() {
|
|
1474
|
+
const value1 = this.read32();
|
|
1475
|
+
const result = this.read32().toString(16) + value1.toString(16).padStart(8, "0");
|
|
1476
|
+
return BigInt("0x" + result).toString(10);
|
|
1477
|
+
}
|
|
1478
|
+
/**
|
|
1479
|
+
* Read U128 value from the buffer and shift cursor by 16.
|
|
1480
|
+
*/
|
|
1481
|
+
read128() {
|
|
1482
|
+
const value1 = BigInt(this.read64());
|
|
1483
|
+
const result = BigInt(this.read64()).toString(16) + value1.toString(16).padStart(16, "0");
|
|
1484
|
+
return BigInt("0x" + result).toString(10);
|
|
1485
|
+
}
|
|
1486
|
+
/**
|
|
1487
|
+
* Read U128 value from the buffer and shift cursor by 32.
|
|
1488
|
+
* @returns
|
|
1489
|
+
*/
|
|
1490
|
+
read256() {
|
|
1491
|
+
const value1 = BigInt(this.read128());
|
|
1492
|
+
const result = BigInt(this.read128()).toString(16) + value1.toString(16).padStart(32, "0");
|
|
1493
|
+
return BigInt("0x" + result).toString(10);
|
|
1494
|
+
}
|
|
1495
|
+
/**
|
|
1496
|
+
* Read `num` number of bytes from the buffer and shift cursor by `num`.
|
|
1497
|
+
* @param num Number of bytes to read.
|
|
1498
|
+
*/
|
|
1499
|
+
readBytes(num) {
|
|
1500
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
1501
|
+
const value = new Uint8Array(this.dataView.buffer, start, num);
|
|
1502
|
+
this.shift(num);
|
|
1503
|
+
return value;
|
|
1504
|
+
}
|
|
1505
|
+
/**
|
|
1506
|
+
* Read ULEB value - an integer of varying size. Used for enum indexes and
|
|
1507
|
+
* vector lengths.
|
|
1508
|
+
* @returns {Number} The ULEB value.
|
|
1509
|
+
*/
|
|
1510
|
+
readULEB() {
|
|
1511
|
+
const start = this.bytePosition + this.dataView.byteOffset;
|
|
1512
|
+
const { value, length } = ulebDecode(new Uint8Array(this.dataView.buffer, start));
|
|
1513
|
+
this.shift(length);
|
|
1514
|
+
return value;
|
|
1515
|
+
}
|
|
1516
|
+
/**
|
|
1517
|
+
* Read a BCS vector: read a length and then apply function `cb` X times
|
|
1518
|
+
* where X is the length of the vector, defined as ULEB in BCS bytes.
|
|
1519
|
+
* @param cb Callback to process elements of vector.
|
|
1520
|
+
* @returns {Array<Any>} Array of the resulting values, returned by callback.
|
|
1521
|
+
*/
|
|
1522
|
+
readVec(cb) {
|
|
1523
|
+
const length = this.readULEB();
|
|
1524
|
+
const result = [];
|
|
1525
|
+
for (let i = 0; i < length; i++) result.push(cb(this, i, length));
|
|
1526
|
+
return result;
|
|
1527
|
+
}
|
|
1528
|
+
};
|
|
1529
|
+
|
|
1530
|
+
// ../../node_modules/.pnpm/@scure+base@2.0.0/node_modules/@scure/base/index.js
|
|
1531
|
+
function isBytes(a) {
|
|
1532
|
+
return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
|
|
1533
|
+
}
|
|
1534
|
+
function isArrayOf(isString, arr) {
|
|
1535
|
+
if (!Array.isArray(arr))
|
|
1536
|
+
return false;
|
|
1537
|
+
if (arr.length === 0)
|
|
1538
|
+
return true;
|
|
1539
|
+
if (isString) {
|
|
1540
|
+
return arr.every((item) => typeof item === "string");
|
|
1541
|
+
} else {
|
|
1542
|
+
return arr.every((item) => Number.isSafeInteger(item));
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
function astr(label, input) {
|
|
1546
|
+
if (typeof input !== "string")
|
|
1547
|
+
throw new Error(`${label}: string expected`);
|
|
1548
|
+
return true;
|
|
1549
|
+
}
|
|
1550
|
+
function anumber(n) {
|
|
1551
|
+
if (!Number.isSafeInteger(n))
|
|
1552
|
+
throw new Error(`invalid integer: ${n}`);
|
|
1553
|
+
}
|
|
1554
|
+
function aArr(input) {
|
|
1555
|
+
if (!Array.isArray(input))
|
|
1556
|
+
throw new Error("array expected");
|
|
1557
|
+
}
|
|
1558
|
+
function astrArr(label, input) {
|
|
1559
|
+
if (!isArrayOf(true, input))
|
|
1560
|
+
throw new Error(`${label}: array of strings expected`);
|
|
1561
|
+
}
|
|
1562
|
+
function anumArr(label, input) {
|
|
1563
|
+
if (!isArrayOf(false, input))
|
|
1564
|
+
throw new Error(`${label}: array of numbers expected`);
|
|
1565
|
+
}
|
|
1566
|
+
// @__NO_SIDE_EFFECTS__
|
|
1567
|
+
function chain(...args) {
|
|
1568
|
+
const id = (a) => a;
|
|
1569
|
+
const wrap = (a, b2) => (c) => a(b2(c));
|
|
1570
|
+
const encode = args.map((x2) => x2.encode).reduceRight(wrap, id);
|
|
1571
|
+
const decode = args.map((x2) => x2.decode).reduce(wrap, id);
|
|
1572
|
+
return { encode, decode };
|
|
1573
|
+
}
|
|
1574
|
+
// @__NO_SIDE_EFFECTS__
|
|
1575
|
+
function alphabet(letters) {
|
|
1576
|
+
const lettersA = letters.split("") ;
|
|
1577
|
+
const len = lettersA.length;
|
|
1578
|
+
astrArr("alphabet", lettersA);
|
|
1579
|
+
const indexes = new Map(lettersA.map((l, i) => [l, i]));
|
|
1580
|
+
return {
|
|
1581
|
+
encode: (digits) => {
|
|
1582
|
+
aArr(digits);
|
|
1583
|
+
return digits.map((i) => {
|
|
1584
|
+
if (!Number.isSafeInteger(i) || i < 0 || i >= len)
|
|
1585
|
+
throw new Error(`alphabet.encode: digit index outside alphabet "${i}". Allowed: ${letters}`);
|
|
1586
|
+
return lettersA[i];
|
|
1587
|
+
});
|
|
1588
|
+
},
|
|
1589
|
+
decode: (input) => {
|
|
1590
|
+
aArr(input);
|
|
1591
|
+
return input.map((letter) => {
|
|
1592
|
+
astr("alphabet.decode", letter);
|
|
1593
|
+
const i = indexes.get(letter);
|
|
1594
|
+
if (i === void 0)
|
|
1595
|
+
throw new Error(`Unknown letter: "${letter}". Allowed: ${letters}`);
|
|
1596
|
+
return i;
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
};
|
|
1600
|
+
}
|
|
1601
|
+
// @__NO_SIDE_EFFECTS__
|
|
1602
|
+
function join(separator = "") {
|
|
1603
|
+
astr("join", separator);
|
|
1604
|
+
return {
|
|
1605
|
+
encode: (from) => {
|
|
1606
|
+
astrArr("join.decode", from);
|
|
1607
|
+
return from.join(separator);
|
|
1608
|
+
},
|
|
1609
|
+
decode: (to) => {
|
|
1610
|
+
astr("join.decode", to);
|
|
1611
|
+
return to.split(separator);
|
|
1612
|
+
}
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
function convertRadix(data, from, to) {
|
|
1616
|
+
if (from < 2)
|
|
1617
|
+
throw new Error(`convertRadix: invalid from=${from}, base cannot be less than 2`);
|
|
1618
|
+
if (to < 2)
|
|
1619
|
+
throw new Error(`convertRadix: invalid to=${to}, base cannot be less than 2`);
|
|
1620
|
+
aArr(data);
|
|
1621
|
+
if (!data.length)
|
|
1622
|
+
return [];
|
|
1623
|
+
let pos = 0;
|
|
1624
|
+
const res = [];
|
|
1625
|
+
const digits = Array.from(data, (d) => {
|
|
1626
|
+
anumber(d);
|
|
1627
|
+
if (d < 0 || d >= from)
|
|
1628
|
+
throw new Error(`invalid integer: ${d}`);
|
|
1629
|
+
return d;
|
|
1630
|
+
});
|
|
1631
|
+
const dlen = digits.length;
|
|
1632
|
+
while (true) {
|
|
1633
|
+
let carry = 0;
|
|
1634
|
+
let done = true;
|
|
1635
|
+
for (let i = pos; i < dlen; i++) {
|
|
1636
|
+
const digit = digits[i];
|
|
1637
|
+
const fromCarry = from * carry;
|
|
1638
|
+
const digitBase = fromCarry + digit;
|
|
1639
|
+
if (!Number.isSafeInteger(digitBase) || fromCarry / from !== carry || digitBase - digit !== fromCarry) {
|
|
1640
|
+
throw new Error("convertRadix: carry overflow");
|
|
1641
|
+
}
|
|
1642
|
+
const div = digitBase / to;
|
|
1643
|
+
carry = digitBase % to;
|
|
1644
|
+
const rounded = Math.floor(div);
|
|
1645
|
+
digits[i] = rounded;
|
|
1646
|
+
if (!Number.isSafeInteger(rounded) || rounded * to + carry !== digitBase)
|
|
1647
|
+
throw new Error("convertRadix: carry overflow");
|
|
1648
|
+
if (!done)
|
|
1649
|
+
continue;
|
|
1650
|
+
else if (!rounded)
|
|
1651
|
+
pos = i;
|
|
1652
|
+
else
|
|
1653
|
+
done = false;
|
|
1654
|
+
}
|
|
1655
|
+
res.push(carry);
|
|
1656
|
+
if (done)
|
|
1657
|
+
break;
|
|
1658
|
+
}
|
|
1659
|
+
for (let i = 0; i < data.length - 1 && data[i] === 0; i++)
|
|
1660
|
+
res.push(0);
|
|
1661
|
+
return res.reverse();
|
|
1662
|
+
}
|
|
1663
|
+
// @__NO_SIDE_EFFECTS__
|
|
1664
|
+
function radix(num) {
|
|
1665
|
+
anumber(num);
|
|
1666
|
+
const _256 = 2 ** 8;
|
|
1667
|
+
return {
|
|
1668
|
+
encode: (bytes) => {
|
|
1669
|
+
if (!isBytes(bytes))
|
|
1670
|
+
throw new Error("radix.encode input should be Uint8Array");
|
|
1671
|
+
return convertRadix(Array.from(bytes), _256, num);
|
|
1672
|
+
},
|
|
1673
|
+
decode: (digits) => {
|
|
1674
|
+
anumArr("radix.decode", digits);
|
|
1675
|
+
return Uint8Array.from(convertRadix(digits, num, _256));
|
|
1676
|
+
}
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
var genBase58 = /* @__NO_SIDE_EFFECTS__ */ (abc) => /* @__PURE__ */ chain(/* @__PURE__ */ radix(58), /* @__PURE__ */ alphabet(abc), /* @__PURE__ */ join(""));
|
|
1680
|
+
var base58 = /* @__PURE__ */ genBase58("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz");
|
|
1681
|
+
|
|
1682
|
+
// ../../node_modules/.pnpm/@mysten+utils@0.3.1/node_modules/@mysten/utils/dist/b58.mjs
|
|
1683
|
+
var toBase58 = (buffer) => base58.encode(buffer);
|
|
1684
|
+
var fromBase58 = (str) => base58.decode(str);
|
|
1685
|
+
|
|
1686
|
+
// ../../node_modules/.pnpm/@mysten+utils@0.3.1/node_modules/@mysten/utils/dist/b64.mjs
|
|
1687
|
+
function fromBase64(base64String) {
|
|
1688
|
+
return Uint8Array.from(atob(base64String), (char) => char.charCodeAt(0));
|
|
1689
|
+
}
|
|
1690
|
+
var CHUNK_SIZE = 8192;
|
|
1691
|
+
function toBase64(bytes) {
|
|
1692
|
+
if (bytes.length < CHUNK_SIZE) return btoa(String.fromCharCode(...bytes));
|
|
1693
|
+
let output = "";
|
|
1694
|
+
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
|
|
1695
|
+
const chunk = bytes.slice(i, i + CHUNK_SIZE);
|
|
1696
|
+
output += String.fromCharCode(...chunk);
|
|
1697
|
+
}
|
|
1698
|
+
return btoa(output);
|
|
1699
|
+
}
|
|
1700
|
+
|
|
1701
|
+
// ../../node_modules/.pnpm/@mysten+utils@0.3.1/node_modules/@mysten/utils/dist/hex.mjs
|
|
1702
|
+
function fromHex(hexStr) {
|
|
1703
|
+
const normalized = hexStr.startsWith("0x") ? hexStr.slice(2) : hexStr;
|
|
1704
|
+
const padded = normalized.length % 2 === 0 ? normalized : `0${normalized}`;
|
|
1705
|
+
const intArr = padded.match(/[0-9a-fA-F]{2}/g)?.map((byte) => parseInt(byte, 16)) ?? [];
|
|
1706
|
+
if (intArr.length !== padded.length / 2) throw new Error(`Invalid hex string ${hexStr}`);
|
|
1707
|
+
return Uint8Array.from(intArr);
|
|
1708
|
+
}
|
|
1709
|
+
function toHex(bytes) {
|
|
1710
|
+
return bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/utils.mjs
|
|
1714
|
+
function encodeStr(data, encoding) {
|
|
1715
|
+
switch (encoding) {
|
|
1716
|
+
case "base58":
|
|
1717
|
+
return toBase58(data);
|
|
1718
|
+
case "base64":
|
|
1719
|
+
return toBase64(data);
|
|
1720
|
+
case "hex":
|
|
1721
|
+
return toHex(data);
|
|
1722
|
+
default:
|
|
1723
|
+
throw new Error("Unsupported encoding, supported values are: base64, hex");
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/writer.mjs
|
|
1728
|
+
var BcsWriter = class {
|
|
1729
|
+
constructor({ initialSize = 1024, maxSize = Infinity, allocateSize = 1024 } = {}) {
|
|
1730
|
+
this.bytePosition = 0;
|
|
1731
|
+
this.size = initialSize;
|
|
1732
|
+
this.maxSize = maxSize;
|
|
1733
|
+
this.allocateSize = allocateSize;
|
|
1734
|
+
this.dataView = new DataView(new ArrayBuffer(initialSize));
|
|
1735
|
+
}
|
|
1736
|
+
ensureSizeOrGrow(bytes) {
|
|
1737
|
+
const requiredSize = this.bytePosition + bytes;
|
|
1738
|
+
if (requiredSize > this.size) {
|
|
1739
|
+
const nextSize = Math.min(this.maxSize, Math.max(this.size + requiredSize, this.size + this.allocateSize));
|
|
1740
|
+
if (requiredSize > nextSize) throw new Error(`Attempting to serialize to BCS, but buffer does not have enough size. Allocated size: ${this.size}, Max size: ${this.maxSize}, Required size: ${requiredSize}`);
|
|
1741
|
+
this.size = nextSize;
|
|
1742
|
+
const nextBuffer = new ArrayBuffer(this.size);
|
|
1743
|
+
new Uint8Array(nextBuffer).set(new Uint8Array(this.dataView.buffer));
|
|
1744
|
+
this.dataView = new DataView(nextBuffer);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Shift current cursor position by `bytes`.
|
|
1749
|
+
*
|
|
1750
|
+
* @param {Number} bytes Number of bytes to
|
|
1751
|
+
* @returns {this} Self for possible chaining.
|
|
1752
|
+
*/
|
|
1753
|
+
shift(bytes) {
|
|
1754
|
+
this.bytePosition += bytes;
|
|
1755
|
+
return this;
|
|
1756
|
+
}
|
|
1757
|
+
/**
|
|
1758
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
1759
|
+
* @param {Number} value Value to write.
|
|
1760
|
+
* @returns {this}
|
|
1761
|
+
*/
|
|
1762
|
+
write8(value) {
|
|
1763
|
+
this.ensureSizeOrGrow(1);
|
|
1764
|
+
this.dataView.setUint8(this.bytePosition, Number(value));
|
|
1765
|
+
return this.shift(1);
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
* Write a U8 value into a buffer and shift cursor position by 1.
|
|
1769
|
+
* @param {Number} value Value to write.
|
|
1770
|
+
* @returns {this}
|
|
1771
|
+
*/
|
|
1772
|
+
writeBytes(bytes) {
|
|
1773
|
+
this.ensureSizeOrGrow(bytes.length);
|
|
1774
|
+
for (let i = 0; i < bytes.length; i++) this.dataView.setUint8(this.bytePosition + i, bytes[i]);
|
|
1775
|
+
return this.shift(bytes.length);
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* Write a U16 value into a buffer and shift cursor position by 2.
|
|
1779
|
+
* @param {Number} value Value to write.
|
|
1780
|
+
* @returns {this}
|
|
1781
|
+
*/
|
|
1782
|
+
write16(value) {
|
|
1783
|
+
this.ensureSizeOrGrow(2);
|
|
1784
|
+
this.dataView.setUint16(this.bytePosition, Number(value), true);
|
|
1785
|
+
return this.shift(2);
|
|
1786
|
+
}
|
|
1787
|
+
/**
|
|
1788
|
+
* Write a U32 value into a buffer and shift cursor position by 4.
|
|
1789
|
+
* @param {Number} value Value to write.
|
|
1790
|
+
* @returns {this}
|
|
1791
|
+
*/
|
|
1792
|
+
write32(value) {
|
|
1793
|
+
this.ensureSizeOrGrow(4);
|
|
1794
|
+
this.dataView.setUint32(this.bytePosition, Number(value), true);
|
|
1795
|
+
return this.shift(4);
|
|
1796
|
+
}
|
|
1797
|
+
/**
|
|
1798
|
+
* Write a U64 value into a buffer and shift cursor position by 8.
|
|
1799
|
+
* @param {bigint} value Value to write.
|
|
1800
|
+
* @returns {this}
|
|
1801
|
+
*/
|
|
1802
|
+
write64(value) {
|
|
1803
|
+
toLittleEndian(BigInt(value), 8).forEach((el) => this.write8(el));
|
|
1804
|
+
return this;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Write a U128 value into a buffer and shift cursor position by 16.
|
|
1808
|
+
*
|
|
1809
|
+
* @param {bigint} value Value to write.
|
|
1810
|
+
* @returns {this}
|
|
1811
|
+
*/
|
|
1812
|
+
write128(value) {
|
|
1813
|
+
toLittleEndian(BigInt(value), 16).forEach((el) => this.write8(el));
|
|
1814
|
+
return this;
|
|
1815
|
+
}
|
|
1816
|
+
/**
|
|
1817
|
+
* Write a U256 value into a buffer and shift cursor position by 16.
|
|
1818
|
+
*
|
|
1819
|
+
* @param {bigint} value Value to write.
|
|
1820
|
+
* @returns {this}
|
|
1821
|
+
*/
|
|
1822
|
+
write256(value) {
|
|
1823
|
+
toLittleEndian(BigInt(value), 32).forEach((el) => this.write8(el));
|
|
1824
|
+
return this;
|
|
1825
|
+
}
|
|
1826
|
+
/**
|
|
1827
|
+
* Write a ULEB value into a buffer and shift cursor position by number of bytes
|
|
1828
|
+
* written.
|
|
1829
|
+
* @param {Number} value Value to write.
|
|
1830
|
+
* @returns {this}
|
|
1831
|
+
*/
|
|
1832
|
+
writeULEB(value) {
|
|
1833
|
+
ulebEncode(value).forEach((el) => this.write8(el));
|
|
1834
|
+
return this;
|
|
1835
|
+
}
|
|
1836
|
+
/**
|
|
1837
|
+
* Write a vector into a buffer by first writing the vector length and then calling
|
|
1838
|
+
* a callback on each passed value.
|
|
1839
|
+
*
|
|
1840
|
+
* @param {Array<Any>} vector Array of elements to write.
|
|
1841
|
+
* @param {WriteVecCb} cb Callback to call on each element of the vector.
|
|
1842
|
+
* @returns {this}
|
|
1843
|
+
*/
|
|
1844
|
+
writeVec(vector2, cb) {
|
|
1845
|
+
this.writeULEB(vector2.length);
|
|
1846
|
+
Array.from(vector2).forEach((el, i) => cb(this, el, i, vector2.length));
|
|
1847
|
+
return this;
|
|
1848
|
+
}
|
|
1849
|
+
/**
|
|
1850
|
+
* Adds support for iterations over the object.
|
|
1851
|
+
* @returns {Uint8Array}
|
|
1852
|
+
*/
|
|
1853
|
+
*[Symbol.iterator]() {
|
|
1854
|
+
for (let i = 0; i < this.bytePosition; i++) yield this.dataView.getUint8(i);
|
|
1855
|
+
return this.toBytes();
|
|
1856
|
+
}
|
|
1857
|
+
/**
|
|
1858
|
+
* Get underlying buffer taking only value bytes (in case initial buffer size was bigger).
|
|
1859
|
+
* @returns {Uint8Array} Resulting bcs.
|
|
1860
|
+
*/
|
|
1861
|
+
toBytes() {
|
|
1862
|
+
return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* Represent data as 'hex' or 'base64'
|
|
1866
|
+
* @param encoding Encoding to use: 'base64' or 'hex'
|
|
1867
|
+
*/
|
|
1868
|
+
toString(encoding) {
|
|
1869
|
+
return encodeStr(this.toBytes(), encoding);
|
|
1870
|
+
}
|
|
1871
|
+
};
|
|
1872
|
+
function toLittleEndian(bigint, size) {
|
|
1873
|
+
const result = new Uint8Array(size);
|
|
1874
|
+
let i = 0;
|
|
1875
|
+
while (bigint > 0) {
|
|
1876
|
+
result[i] = Number(bigint % BigInt(256));
|
|
1877
|
+
bigint = bigint / BigInt(256);
|
|
1878
|
+
i += 1;
|
|
1879
|
+
}
|
|
1880
|
+
return result;
|
|
1881
|
+
}
|
|
1882
|
+
|
|
1883
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/bcs-type.mjs
|
|
1884
|
+
var BcsType = class BcsType2 {
|
|
1885
|
+
#write;
|
|
1886
|
+
#serialize;
|
|
1887
|
+
constructor(options) {
|
|
1888
|
+
this.name = options.name;
|
|
1889
|
+
this.read = options.read;
|
|
1890
|
+
this.serializedSize = options.serializedSize ?? (() => null);
|
|
1891
|
+
this.#write = options.write;
|
|
1892
|
+
this.#serialize = options.serialize ?? ((value, options$1) => {
|
|
1893
|
+
const writer = new BcsWriter({
|
|
1894
|
+
initialSize: this.serializedSize(value) ?? void 0,
|
|
1895
|
+
...options$1
|
|
1896
|
+
});
|
|
1897
|
+
this.#write(value, writer);
|
|
1898
|
+
return writer.toBytes();
|
|
1899
|
+
});
|
|
1900
|
+
this.validate = options.validate ?? (() => {
|
|
1901
|
+
});
|
|
1902
|
+
}
|
|
1903
|
+
write(value, writer) {
|
|
1904
|
+
this.validate(value);
|
|
1905
|
+
this.#write(value, writer);
|
|
1906
|
+
}
|
|
1907
|
+
serialize(value, options) {
|
|
1908
|
+
this.validate(value);
|
|
1909
|
+
return new SerializedBcs(this, this.#serialize(value, options));
|
|
1910
|
+
}
|
|
1911
|
+
parse(bytes) {
|
|
1912
|
+
const reader = new BcsReader(bytes);
|
|
1913
|
+
return this.read(reader);
|
|
1914
|
+
}
|
|
1915
|
+
fromHex(hex) {
|
|
1916
|
+
return this.parse(fromHex(hex));
|
|
1917
|
+
}
|
|
1918
|
+
fromBase58(b64) {
|
|
1919
|
+
return this.parse(fromBase58(b64));
|
|
1920
|
+
}
|
|
1921
|
+
fromBase64(b64) {
|
|
1922
|
+
return this.parse(fromBase64(b64));
|
|
1923
|
+
}
|
|
1924
|
+
transform({ name, input, output, validate }) {
|
|
1925
|
+
return new BcsType2({
|
|
1926
|
+
name: name ?? this.name,
|
|
1927
|
+
read: (reader) => output ? output(this.read(reader)) : this.read(reader),
|
|
1928
|
+
write: (value, writer) => this.#write(input ? input(value) : value, writer),
|
|
1929
|
+
serializedSize: (value) => this.serializedSize(input ? input(value) : value),
|
|
1930
|
+
serialize: (value, options) => this.#serialize(input ? input(value) : value, options),
|
|
1931
|
+
validate: (value) => {
|
|
1932
|
+
validate?.(value);
|
|
1933
|
+
this.validate(input ? input(value) : value);
|
|
1934
|
+
}
|
|
1935
|
+
});
|
|
1936
|
+
}
|
|
1937
|
+
};
|
|
1938
|
+
var SERIALIZED_BCS_BRAND = /* @__PURE__ */ Symbol.for("@mysten/serialized-bcs");
|
|
1939
|
+
var SerializedBcs = class {
|
|
1940
|
+
#schema;
|
|
1941
|
+
#bytes;
|
|
1942
|
+
get [SERIALIZED_BCS_BRAND]() {
|
|
1943
|
+
return true;
|
|
1944
|
+
}
|
|
1945
|
+
constructor(schema, bytes) {
|
|
1946
|
+
this.#schema = schema;
|
|
1947
|
+
this.#bytes = bytes;
|
|
1948
|
+
}
|
|
1949
|
+
toBytes() {
|
|
1950
|
+
return this.#bytes;
|
|
1951
|
+
}
|
|
1952
|
+
toHex() {
|
|
1953
|
+
return toHex(this.#bytes);
|
|
1954
|
+
}
|
|
1955
|
+
toBase64() {
|
|
1956
|
+
return toBase64(this.#bytes);
|
|
1957
|
+
}
|
|
1958
|
+
toBase58() {
|
|
1959
|
+
return toBase58(this.#bytes);
|
|
1960
|
+
}
|
|
1961
|
+
parse() {
|
|
1962
|
+
return this.#schema.parse(this.#bytes);
|
|
1963
|
+
}
|
|
1964
|
+
};
|
|
1965
|
+
function fixedSizeBcsType({ size, ...options }) {
|
|
1966
|
+
return new BcsType({
|
|
1967
|
+
...options,
|
|
1968
|
+
serializedSize: () => size
|
|
1969
|
+
});
|
|
1970
|
+
}
|
|
1971
|
+
function uIntBcsType({ readMethod, writeMethod, ...options }) {
|
|
1972
|
+
return fixedSizeBcsType({
|
|
1973
|
+
...options,
|
|
1974
|
+
read: (reader) => reader[readMethod](),
|
|
1975
|
+
write: (value, writer) => writer[writeMethod](value),
|
|
1976
|
+
validate: (value) => {
|
|
1977
|
+
if (value < 0 || value > options.maxValue) throw new TypeError(`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`);
|
|
1978
|
+
options.validate?.(value);
|
|
1979
|
+
}
|
|
1980
|
+
});
|
|
1981
|
+
}
|
|
1982
|
+
function bigUIntBcsType({ readMethod, writeMethod, ...options }) {
|
|
1983
|
+
return fixedSizeBcsType({
|
|
1984
|
+
...options,
|
|
1985
|
+
read: (reader) => reader[readMethod](),
|
|
1986
|
+
write: (value, writer) => writer[writeMethod](BigInt(value)),
|
|
1987
|
+
validate: (val) => {
|
|
1988
|
+
const value = BigInt(val);
|
|
1989
|
+
if (value < 0 || value > options.maxValue) throw new TypeError(`Invalid ${options.name} value: ${value}. Expected value in range 0-${options.maxValue}`);
|
|
1990
|
+
options.validate?.(value);
|
|
1991
|
+
}
|
|
1992
|
+
});
|
|
1993
|
+
}
|
|
1994
|
+
function dynamicSizeBcsType({ serialize, ...options }) {
|
|
1995
|
+
const type = new BcsType({
|
|
1996
|
+
...options,
|
|
1997
|
+
serialize,
|
|
1998
|
+
write: (value, writer) => {
|
|
1999
|
+
for (const byte of type.serialize(value).toBytes()) writer.write8(byte);
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
2002
|
+
return type;
|
|
2003
|
+
}
|
|
2004
|
+
function stringLikeBcsType({ toBytes, fromBytes, ...options }) {
|
|
2005
|
+
return new BcsType({
|
|
2006
|
+
...options,
|
|
2007
|
+
read: (reader) => {
|
|
2008
|
+
const length = reader.readULEB();
|
|
2009
|
+
return fromBytes(reader.readBytes(length));
|
|
2010
|
+
},
|
|
2011
|
+
write: (hex, writer) => {
|
|
2012
|
+
const bytes = toBytes(hex);
|
|
2013
|
+
writer.writeULEB(bytes.length);
|
|
2014
|
+
for (let i = 0; i < bytes.length; i++) writer.write8(bytes[i]);
|
|
2015
|
+
},
|
|
2016
|
+
serialize: (value) => {
|
|
2017
|
+
const bytes = toBytes(value);
|
|
2018
|
+
const size = ulebEncode(bytes.length);
|
|
2019
|
+
const result = new Uint8Array(size.length + bytes.length);
|
|
2020
|
+
result.set(size, 0);
|
|
2021
|
+
result.set(bytes, size.length);
|
|
2022
|
+
return result;
|
|
2023
|
+
},
|
|
2024
|
+
validate: (value) => {
|
|
2025
|
+
if (typeof value !== "string") throw new TypeError(`Invalid ${options.name} value: ${value}. Expected string`);
|
|
2026
|
+
options.validate?.(value);
|
|
2027
|
+
}
|
|
2028
|
+
});
|
|
2029
|
+
}
|
|
2030
|
+
function lazyBcsType(cb) {
|
|
2031
|
+
let lazyType = null;
|
|
2032
|
+
function getType() {
|
|
2033
|
+
if (!lazyType) lazyType = cb();
|
|
2034
|
+
return lazyType;
|
|
2035
|
+
}
|
|
2036
|
+
return new BcsType({
|
|
2037
|
+
name: "lazy",
|
|
2038
|
+
read: (data) => getType().read(data),
|
|
2039
|
+
serializedSize: (value) => getType().serializedSize(value),
|
|
2040
|
+
write: (value, writer) => getType().write(value, writer),
|
|
2041
|
+
serialize: (value, options) => getType().serialize(value, options).toBytes()
|
|
2042
|
+
});
|
|
2043
|
+
}
|
|
2044
|
+
var BcsStruct = class extends BcsType {
|
|
2045
|
+
constructor({ name, fields, ...options }) {
|
|
2046
|
+
const canonicalOrder = Object.entries(fields);
|
|
2047
|
+
super({
|
|
2048
|
+
name,
|
|
2049
|
+
serializedSize: (values) => {
|
|
2050
|
+
let total = 0;
|
|
2051
|
+
for (const [field, type] of canonicalOrder) {
|
|
2052
|
+
const size = type.serializedSize(values[field]);
|
|
2053
|
+
if (size == null) return null;
|
|
2054
|
+
total += size;
|
|
2055
|
+
}
|
|
2056
|
+
return total;
|
|
2057
|
+
},
|
|
2058
|
+
read: (reader) => {
|
|
2059
|
+
const result = {};
|
|
2060
|
+
for (const [field, type] of canonicalOrder) result[field] = type.read(reader);
|
|
2061
|
+
return result;
|
|
2062
|
+
},
|
|
2063
|
+
write: (value, writer) => {
|
|
2064
|
+
for (const [field, type] of canonicalOrder) type.write(value[field], writer);
|
|
2065
|
+
},
|
|
2066
|
+
...options,
|
|
2067
|
+
validate: (value) => {
|
|
2068
|
+
options?.validate?.(value);
|
|
2069
|
+
if (typeof value !== "object" || value == null) throw new TypeError(`Expected object, found ${typeof value}`);
|
|
2070
|
+
}
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
};
|
|
2074
|
+
var BcsEnum = class extends BcsType {
|
|
2075
|
+
constructor({ fields, ...options }) {
|
|
2076
|
+
const canonicalOrder = Object.entries(fields);
|
|
2077
|
+
super({
|
|
2078
|
+
read: (reader) => {
|
|
2079
|
+
const index = reader.readULEB();
|
|
2080
|
+
const enumEntry = canonicalOrder[index];
|
|
2081
|
+
if (!enumEntry) throw new TypeError(`Unknown value ${index} for enum ${options.name}`);
|
|
2082
|
+
const [kind, type] = enumEntry;
|
|
2083
|
+
return {
|
|
2084
|
+
[kind]: type?.read(reader) ?? true,
|
|
2085
|
+
$kind: kind
|
|
2086
|
+
};
|
|
2087
|
+
},
|
|
2088
|
+
write: (value, writer) => {
|
|
2089
|
+
const [name, val] = Object.entries(value).filter(([name$1]) => Object.hasOwn(fields, name$1))[0];
|
|
2090
|
+
for (let i = 0; i < canonicalOrder.length; i++) {
|
|
2091
|
+
const [optionName, optionType] = canonicalOrder[i];
|
|
2092
|
+
if (optionName === name) {
|
|
2093
|
+
writer.writeULEB(i);
|
|
2094
|
+
optionType?.write(val, writer);
|
|
2095
|
+
return;
|
|
2096
|
+
}
|
|
2097
|
+
}
|
|
2098
|
+
},
|
|
2099
|
+
...options,
|
|
2100
|
+
validate: (value) => {
|
|
2101
|
+
options?.validate?.(value);
|
|
2102
|
+
if (typeof value !== "object" || value == null) throw new TypeError(`Expected object, found ${typeof value}`);
|
|
2103
|
+
const keys = Object.keys(value).filter((k2) => value[k2] !== void 0 && Object.hasOwn(fields, k2));
|
|
2104
|
+
if (keys.length !== 1) throw new TypeError(`Expected object with one key, but found ${keys.length} for type ${options.name}}`);
|
|
2105
|
+
const [variant] = keys;
|
|
2106
|
+
if (!Object.hasOwn(fields, variant)) throw new TypeError(`Invalid enum variant ${variant}`);
|
|
2107
|
+
}
|
|
2108
|
+
});
|
|
2109
|
+
}
|
|
2110
|
+
};
|
|
2111
|
+
var BcsTuple = class extends BcsType {
|
|
2112
|
+
constructor({ fields, name, ...options }) {
|
|
2113
|
+
super({
|
|
2114
|
+
name: name ?? `(${fields.map((t) => t.name).join(", ")})`,
|
|
2115
|
+
serializedSize: (values) => {
|
|
2116
|
+
let total = 0;
|
|
2117
|
+
for (let i = 0; i < fields.length; i++) {
|
|
2118
|
+
const size = fields[i].serializedSize(values[i]);
|
|
2119
|
+
if (size == null) return null;
|
|
2120
|
+
total += size;
|
|
2121
|
+
}
|
|
2122
|
+
return total;
|
|
2123
|
+
},
|
|
2124
|
+
read: (reader) => {
|
|
2125
|
+
const result = [];
|
|
2126
|
+
for (const field of fields) result.push(field.read(reader));
|
|
2127
|
+
return result;
|
|
2128
|
+
},
|
|
2129
|
+
write: (value, writer) => {
|
|
2130
|
+
for (let i = 0; i < fields.length; i++) fields[i].write(value[i], writer);
|
|
2131
|
+
},
|
|
2132
|
+
...options,
|
|
2133
|
+
validate: (value) => {
|
|
2134
|
+
options?.validate?.(value);
|
|
2135
|
+
if (!Array.isArray(value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
2136
|
+
if (value.length !== fields.length) throw new TypeError(`Expected array of length ${fields.length}, found ${value.length}`);
|
|
2137
|
+
}
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
};
|
|
2141
|
+
|
|
2142
|
+
// ../../node_modules/.pnpm/@mysten+bcs@2.0.2/node_modules/@mysten/bcs/dist/bcs.mjs
|
|
2143
|
+
function fixedArray(size, type, options) {
|
|
2144
|
+
return new BcsType({
|
|
2145
|
+
read: (reader) => {
|
|
2146
|
+
const result = new Array(size);
|
|
2147
|
+
for (let i = 0; i < size; i++) result[i] = type.read(reader);
|
|
2148
|
+
return result;
|
|
2149
|
+
},
|
|
2150
|
+
write: (value, writer) => {
|
|
2151
|
+
for (const item of value) type.write(item, writer);
|
|
2152
|
+
},
|
|
2153
|
+
...options,
|
|
2154
|
+
name: options?.name ?? `${type.name}[${size}]`,
|
|
2155
|
+
validate: (value) => {
|
|
2156
|
+
options?.validate?.(value);
|
|
2157
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
2158
|
+
if (value.length !== size) throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
2159
|
+
}
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
function option(type) {
|
|
2163
|
+
return bcs.enum(`Option<${type.name}>`, {
|
|
2164
|
+
None: null,
|
|
2165
|
+
Some: type
|
|
2166
|
+
}).transform({
|
|
2167
|
+
input: (value) => {
|
|
2168
|
+
if (value == null) return { None: true };
|
|
2169
|
+
return { Some: value };
|
|
2170
|
+
},
|
|
2171
|
+
output: (value) => {
|
|
2172
|
+
if (value.$kind === "Some") return value.Some;
|
|
2173
|
+
return null;
|
|
2174
|
+
}
|
|
2175
|
+
});
|
|
2176
|
+
}
|
|
2177
|
+
function vector(type, options) {
|
|
2178
|
+
return new BcsType({
|
|
2179
|
+
read: (reader) => {
|
|
2180
|
+
const length = reader.readULEB();
|
|
2181
|
+
const result = new Array(length);
|
|
2182
|
+
for (let i = 0; i < length; i++) result[i] = type.read(reader);
|
|
2183
|
+
return result;
|
|
2184
|
+
},
|
|
2185
|
+
write: (value, writer) => {
|
|
2186
|
+
writer.writeULEB(value.length);
|
|
2187
|
+
for (const item of value) type.write(item, writer);
|
|
2188
|
+
},
|
|
2189
|
+
...options,
|
|
2190
|
+
name: options?.name ?? `vector<${type.name}>`,
|
|
2191
|
+
validate: (value) => {
|
|
2192
|
+
options?.validate?.(value);
|
|
2193
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
2194
|
+
}
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
function compareBcsBytes(a, b2) {
|
|
2198
|
+
for (let i = 0; i < Math.min(a.length, b2.length); i++) if (a[i] !== b2[i]) return a[i] - b2[i];
|
|
2199
|
+
return a.length - b2.length;
|
|
2200
|
+
}
|
|
2201
|
+
function map(keyType, valueType) {
|
|
2202
|
+
return new BcsType({
|
|
2203
|
+
name: `Map<${keyType.name}, ${valueType.name}>`,
|
|
2204
|
+
read: (reader) => {
|
|
2205
|
+
const length = reader.readULEB();
|
|
2206
|
+
const result = /* @__PURE__ */ new Map();
|
|
2207
|
+
for (let i = 0; i < length; i++) result.set(keyType.read(reader), valueType.read(reader));
|
|
2208
|
+
return result;
|
|
2209
|
+
},
|
|
2210
|
+
write: (value, writer) => {
|
|
2211
|
+
const entries = [...value.entries()].map(([key, val]) => [keyType.serialize(key).toBytes(), val]);
|
|
2212
|
+
entries.sort(([a], [b2]) => compareBcsBytes(a, b2));
|
|
2213
|
+
writer.writeULEB(entries.length);
|
|
2214
|
+
for (const [keyBytes, val] of entries) {
|
|
2215
|
+
writer.writeBytes(keyBytes);
|
|
2216
|
+
valueType.write(val, writer);
|
|
2217
|
+
}
|
|
2218
|
+
}
|
|
2219
|
+
});
|
|
2220
|
+
}
|
|
2221
|
+
var bcs = {
|
|
2222
|
+
u8(options) {
|
|
2223
|
+
return uIntBcsType({
|
|
2224
|
+
readMethod: "read8",
|
|
2225
|
+
writeMethod: "write8",
|
|
2226
|
+
size: 1,
|
|
2227
|
+
maxValue: 2 ** 8 - 1,
|
|
2228
|
+
...options,
|
|
2229
|
+
name: options?.name ?? "u8"
|
|
2230
|
+
});
|
|
2231
|
+
},
|
|
2232
|
+
u16(options) {
|
|
2233
|
+
return uIntBcsType({
|
|
2234
|
+
readMethod: "read16",
|
|
2235
|
+
writeMethod: "write16",
|
|
2236
|
+
size: 2,
|
|
2237
|
+
maxValue: 2 ** 16 - 1,
|
|
2238
|
+
...options,
|
|
2239
|
+
name: options?.name ?? "u16"
|
|
2240
|
+
});
|
|
2241
|
+
},
|
|
2242
|
+
u32(options) {
|
|
2243
|
+
return uIntBcsType({
|
|
2244
|
+
readMethod: "read32",
|
|
2245
|
+
writeMethod: "write32",
|
|
2246
|
+
size: 4,
|
|
2247
|
+
maxValue: 2 ** 32 - 1,
|
|
2248
|
+
...options,
|
|
2249
|
+
name: options?.name ?? "u32"
|
|
2250
|
+
});
|
|
2251
|
+
},
|
|
2252
|
+
u64(options) {
|
|
2253
|
+
return bigUIntBcsType({
|
|
2254
|
+
readMethod: "read64",
|
|
2255
|
+
writeMethod: "write64",
|
|
2256
|
+
size: 8,
|
|
2257
|
+
maxValue: 2n ** 64n - 1n,
|
|
2258
|
+
...options,
|
|
2259
|
+
name: options?.name ?? "u64"
|
|
2260
|
+
});
|
|
2261
|
+
},
|
|
2262
|
+
u128(options) {
|
|
2263
|
+
return bigUIntBcsType({
|
|
2264
|
+
readMethod: "read128",
|
|
2265
|
+
writeMethod: "write128",
|
|
2266
|
+
size: 16,
|
|
2267
|
+
maxValue: 2n ** 128n - 1n,
|
|
2268
|
+
...options,
|
|
2269
|
+
name: options?.name ?? "u128"
|
|
2270
|
+
});
|
|
2271
|
+
},
|
|
2272
|
+
u256(options) {
|
|
2273
|
+
return bigUIntBcsType({
|
|
2274
|
+
readMethod: "read256",
|
|
2275
|
+
writeMethod: "write256",
|
|
2276
|
+
size: 32,
|
|
2277
|
+
maxValue: 2n ** 256n - 1n,
|
|
2278
|
+
...options,
|
|
2279
|
+
name: options?.name ?? "u256"
|
|
2280
|
+
});
|
|
2281
|
+
},
|
|
2282
|
+
bool(options) {
|
|
2283
|
+
return fixedSizeBcsType({
|
|
2284
|
+
size: 1,
|
|
2285
|
+
read: (reader) => reader.read8() === 1,
|
|
2286
|
+
write: (value, writer) => writer.write8(value ? 1 : 0),
|
|
2287
|
+
...options,
|
|
2288
|
+
name: options?.name ?? "bool",
|
|
2289
|
+
validate: (value) => {
|
|
2290
|
+
options?.validate?.(value);
|
|
2291
|
+
if (typeof value !== "boolean") throw new TypeError(`Expected boolean, found ${typeof value}`);
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2294
|
+
},
|
|
2295
|
+
uleb128(options) {
|
|
2296
|
+
return dynamicSizeBcsType({
|
|
2297
|
+
read: (reader) => reader.readULEB(),
|
|
2298
|
+
serialize: (value) => {
|
|
2299
|
+
return Uint8Array.from(ulebEncode(value));
|
|
2300
|
+
},
|
|
2301
|
+
...options,
|
|
2302
|
+
name: options?.name ?? "uleb128"
|
|
2303
|
+
});
|
|
2304
|
+
},
|
|
2305
|
+
bytes(size, options) {
|
|
2306
|
+
return fixedSizeBcsType({
|
|
2307
|
+
size,
|
|
2308
|
+
read: (reader) => reader.readBytes(size),
|
|
2309
|
+
write: (value, writer) => {
|
|
2310
|
+
writer.writeBytes(new Uint8Array(value));
|
|
2311
|
+
},
|
|
2312
|
+
...options,
|
|
2313
|
+
name: options?.name ?? `bytes[${size}]`,
|
|
2314
|
+
validate: (value) => {
|
|
2315
|
+
options?.validate?.(value);
|
|
2316
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
2317
|
+
if (value.length !== size) throw new TypeError(`Expected array of length ${size}, found ${value.length}`);
|
|
2318
|
+
}
|
|
2319
|
+
});
|
|
2320
|
+
},
|
|
2321
|
+
byteVector(options) {
|
|
2322
|
+
return new BcsType({
|
|
2323
|
+
read: (reader) => {
|
|
2324
|
+
const length = reader.readULEB();
|
|
2325
|
+
return reader.readBytes(length);
|
|
2326
|
+
},
|
|
2327
|
+
write: (value, writer) => {
|
|
2328
|
+
const array = new Uint8Array(value);
|
|
2329
|
+
writer.writeULEB(array.length);
|
|
2330
|
+
writer.writeBytes(array);
|
|
2331
|
+
},
|
|
2332
|
+
...options,
|
|
2333
|
+
name: options?.name ?? "vector<u8>",
|
|
2334
|
+
serializedSize: (value) => {
|
|
2335
|
+
const length = "length" in value ? value.length : null;
|
|
2336
|
+
return length == null ? null : ulebEncode(length).length + length;
|
|
2337
|
+
},
|
|
2338
|
+
validate: (value) => {
|
|
2339
|
+
options?.validate?.(value);
|
|
2340
|
+
if (!value || typeof value !== "object" || !("length" in value)) throw new TypeError(`Expected array, found ${typeof value}`);
|
|
2341
|
+
}
|
|
2342
|
+
});
|
|
2343
|
+
},
|
|
2344
|
+
string(options) {
|
|
2345
|
+
return stringLikeBcsType({
|
|
2346
|
+
toBytes: (value) => new TextEncoder().encode(value),
|
|
2347
|
+
fromBytes: (bytes) => new TextDecoder().decode(bytes),
|
|
2348
|
+
...options,
|
|
2349
|
+
name: options?.name ?? "string"
|
|
2350
|
+
});
|
|
2351
|
+
},
|
|
2352
|
+
fixedArray,
|
|
2353
|
+
option,
|
|
2354
|
+
vector,
|
|
2355
|
+
tuple(fields, options) {
|
|
2356
|
+
return new BcsTuple({
|
|
2357
|
+
fields,
|
|
2358
|
+
...options
|
|
2359
|
+
});
|
|
2360
|
+
},
|
|
2361
|
+
struct(name, fields, options) {
|
|
2362
|
+
return new BcsStruct({
|
|
2363
|
+
name,
|
|
2364
|
+
fields,
|
|
2365
|
+
...options
|
|
2366
|
+
});
|
|
2367
|
+
},
|
|
2368
|
+
enum(name, fields, options) {
|
|
2369
|
+
return new BcsEnum({
|
|
2370
|
+
name,
|
|
2371
|
+
fields,
|
|
2372
|
+
...options
|
|
2373
|
+
});
|
|
2374
|
+
},
|
|
2375
|
+
map,
|
|
2376
|
+
lazy(cb) {
|
|
2377
|
+
return lazyBcsType(cb);
|
|
2378
|
+
}
|
|
2379
|
+
};
|
|
2380
|
+
|
|
2381
|
+
// ../../node_modules/.pnpm/@naviprotocol+lending@1.4.0_patch_hash=f5361914099eac610a549dc571b1248d29f6a1f4195ed017_05ba4b180e35cb4ccd9db84691f20fcb/node_modules/@naviprotocol/lending/dist/index.esm.js
|
|
2382
|
+
__toESM(require_lodash());
|
|
2383
|
+
|
|
2384
|
+
// ../../node_modules/.pnpm/bignumber.js@9.1.2/node_modules/bignumber.js/bignumber.mjs
|
|
2385
|
+
var isNumeric = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i;
|
|
2386
|
+
var mathceil = Math.ceil;
|
|
2387
|
+
var mathfloor = Math.floor;
|
|
2388
|
+
var bignumberError = "[BigNumber Error] ";
|
|
2389
|
+
var tooManyDigits = bignumberError + "Number primitive has more than 15 significant digits: ";
|
|
2390
|
+
var BASE = 1e14;
|
|
2391
|
+
var LOG_BASE = 14;
|
|
2392
|
+
var MAX_SAFE_INTEGER = 9007199254740991;
|
|
2393
|
+
var POWS_TEN = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13];
|
|
2394
|
+
var SQRT_BASE = 1e7;
|
|
2395
|
+
var MAX = 1e9;
|
|
2396
|
+
function clone(configObject) {
|
|
2397
|
+
var div, convertBase, parseNumeric, P2 = BigNumber2.prototype = { constructor: BigNumber2, toString: null, valueOf: null }, ONE = new BigNumber2(1), DECIMAL_PLACES = 20, ROUNDING_MODE = 4, TO_EXP_NEG = -7, TO_EXP_POS = 21, MIN_EXP = -1e7, MAX_EXP = 1e7, CRYPTO = false, MODULO_MODE = 1, POW_PRECISION = 0, FORMAT = {
|
|
2398
|
+
prefix: "",
|
|
2399
|
+
groupSize: 3,
|
|
2400
|
+
secondaryGroupSize: 0,
|
|
2401
|
+
groupSeparator: ",",
|
|
2402
|
+
decimalSeparator: ".",
|
|
2403
|
+
fractionGroupSize: 0,
|
|
2404
|
+
fractionGroupSeparator: "\xA0",
|
|
2405
|
+
// non-breaking space
|
|
2406
|
+
suffix: ""
|
|
2407
|
+
}, ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyz", alphabetHasNormalDecimalDigits = true;
|
|
2408
|
+
function BigNumber2(v, b2) {
|
|
2409
|
+
var alphabet2, c, caseChanged, e, i, isNum, len, str, x2 = this;
|
|
2410
|
+
if (!(x2 instanceof BigNumber2)) return new BigNumber2(v, b2);
|
|
2411
|
+
if (b2 == null) {
|
|
2412
|
+
if (v && v._isBigNumber === true) {
|
|
2413
|
+
x2.s = v.s;
|
|
2414
|
+
if (!v.c || v.e > MAX_EXP) {
|
|
2415
|
+
x2.c = x2.e = null;
|
|
2416
|
+
} else if (v.e < MIN_EXP) {
|
|
2417
|
+
x2.c = [x2.e = 0];
|
|
2418
|
+
} else {
|
|
2419
|
+
x2.e = v.e;
|
|
2420
|
+
x2.c = v.c.slice();
|
|
2421
|
+
}
|
|
2422
|
+
return;
|
|
2423
|
+
}
|
|
2424
|
+
if ((isNum = typeof v == "number") && v * 0 == 0) {
|
|
2425
|
+
x2.s = 1 / v < 0 ? (v = -v, -1) : 1;
|
|
2426
|
+
if (v === ~~v) {
|
|
2427
|
+
for (e = 0, i = v; i >= 10; i /= 10, e++) ;
|
|
2428
|
+
if (e > MAX_EXP) {
|
|
2429
|
+
x2.c = x2.e = null;
|
|
2430
|
+
} else {
|
|
2431
|
+
x2.e = e;
|
|
2432
|
+
x2.c = [v];
|
|
2433
|
+
}
|
|
2434
|
+
return;
|
|
2435
|
+
}
|
|
2436
|
+
str = String(v);
|
|
2437
|
+
} else {
|
|
2438
|
+
if (!isNumeric.test(str = String(v))) return parseNumeric(x2, str, isNum);
|
|
2439
|
+
x2.s = str.charCodeAt(0) == 45 ? (str = str.slice(1), -1) : 1;
|
|
2440
|
+
}
|
|
2441
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
2442
|
+
if ((i = str.search(/e/i)) > 0) {
|
|
2443
|
+
if (e < 0) e = i;
|
|
2444
|
+
e += +str.slice(i + 1);
|
|
2445
|
+
str = str.substring(0, i);
|
|
2446
|
+
} else if (e < 0) {
|
|
2447
|
+
e = str.length;
|
|
2448
|
+
}
|
|
2449
|
+
} else {
|
|
2450
|
+
intCheck(b2, 2, ALPHABET.length, "Base");
|
|
2451
|
+
if (b2 == 10 && alphabetHasNormalDecimalDigits) {
|
|
2452
|
+
x2 = new BigNumber2(v);
|
|
2453
|
+
return round(x2, DECIMAL_PLACES + x2.e + 1, ROUNDING_MODE);
|
|
2454
|
+
}
|
|
2455
|
+
str = String(v);
|
|
2456
|
+
if (isNum = typeof v == "number") {
|
|
2457
|
+
if (v * 0 != 0) return parseNumeric(x2, str, isNum, b2);
|
|
2458
|
+
x2.s = 1 / v < 0 ? (str = str.slice(1), -1) : 1;
|
|
2459
|
+
if (BigNumber2.DEBUG && str.replace(/^0\.0*|\./, "").length > 15) {
|
|
2460
|
+
throw Error(tooManyDigits + v);
|
|
2461
|
+
}
|
|
2462
|
+
} else {
|
|
2463
|
+
x2.s = str.charCodeAt(0) === 45 ? (str = str.slice(1), -1) : 1;
|
|
2464
|
+
}
|
|
2465
|
+
alphabet2 = ALPHABET.slice(0, b2);
|
|
2466
|
+
e = i = 0;
|
|
2467
|
+
for (len = str.length; i < len; i++) {
|
|
2468
|
+
if (alphabet2.indexOf(c = str.charAt(i)) < 0) {
|
|
2469
|
+
if (c == ".") {
|
|
2470
|
+
if (i > e) {
|
|
2471
|
+
e = len;
|
|
2472
|
+
continue;
|
|
2473
|
+
}
|
|
2474
|
+
} else if (!caseChanged) {
|
|
2475
|
+
if (str == str.toUpperCase() && (str = str.toLowerCase()) || str == str.toLowerCase() && (str = str.toUpperCase())) {
|
|
2476
|
+
caseChanged = true;
|
|
2477
|
+
i = -1;
|
|
2478
|
+
e = 0;
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
return parseNumeric(x2, String(v), isNum, b2);
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
isNum = false;
|
|
2486
|
+
str = convertBase(str, b2, 10, x2.s);
|
|
2487
|
+
if ((e = str.indexOf(".")) > -1) str = str.replace(".", "");
|
|
2488
|
+
else e = str.length;
|
|
2489
|
+
}
|
|
2490
|
+
for (i = 0; str.charCodeAt(i) === 48; i++) ;
|
|
2491
|
+
for (len = str.length; str.charCodeAt(--len) === 48; ) ;
|
|
2492
|
+
if (str = str.slice(i, ++len)) {
|
|
2493
|
+
len -= i;
|
|
2494
|
+
if (isNum && BigNumber2.DEBUG && len > 15 && (v > MAX_SAFE_INTEGER || v !== mathfloor(v))) {
|
|
2495
|
+
throw Error(tooManyDigits + x2.s * v);
|
|
2496
|
+
}
|
|
2497
|
+
if ((e = e - i - 1) > MAX_EXP) {
|
|
2498
|
+
x2.c = x2.e = null;
|
|
2499
|
+
} else if (e < MIN_EXP) {
|
|
2500
|
+
x2.c = [x2.e = 0];
|
|
2501
|
+
} else {
|
|
2502
|
+
x2.e = e;
|
|
2503
|
+
x2.c = [];
|
|
2504
|
+
i = (e + 1) % LOG_BASE;
|
|
2505
|
+
if (e < 0) i += LOG_BASE;
|
|
2506
|
+
if (i < len) {
|
|
2507
|
+
if (i) x2.c.push(+str.slice(0, i));
|
|
2508
|
+
for (len -= LOG_BASE; i < len; ) {
|
|
2509
|
+
x2.c.push(+str.slice(i, i += LOG_BASE));
|
|
2510
|
+
}
|
|
2511
|
+
i = LOG_BASE - (str = str.slice(i)).length;
|
|
2512
|
+
} else {
|
|
2513
|
+
i -= len;
|
|
2514
|
+
}
|
|
2515
|
+
for (; i--; str += "0") ;
|
|
2516
|
+
x2.c.push(+str);
|
|
2517
|
+
}
|
|
2518
|
+
} else {
|
|
2519
|
+
x2.c = [x2.e = 0];
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
BigNumber2.clone = clone;
|
|
2523
|
+
BigNumber2.ROUND_UP = 0;
|
|
2524
|
+
BigNumber2.ROUND_DOWN = 1;
|
|
2525
|
+
BigNumber2.ROUND_CEIL = 2;
|
|
2526
|
+
BigNumber2.ROUND_FLOOR = 3;
|
|
2527
|
+
BigNumber2.ROUND_HALF_UP = 4;
|
|
2528
|
+
BigNumber2.ROUND_HALF_DOWN = 5;
|
|
2529
|
+
BigNumber2.ROUND_HALF_EVEN = 6;
|
|
2530
|
+
BigNumber2.ROUND_HALF_CEIL = 7;
|
|
2531
|
+
BigNumber2.ROUND_HALF_FLOOR = 8;
|
|
2532
|
+
BigNumber2.EUCLID = 9;
|
|
2533
|
+
BigNumber2.config = BigNumber2.set = function(obj) {
|
|
2534
|
+
var p, v;
|
|
2535
|
+
if (obj != null) {
|
|
2536
|
+
if (typeof obj == "object") {
|
|
2537
|
+
if (obj.hasOwnProperty(p = "DECIMAL_PLACES")) {
|
|
2538
|
+
v = obj[p];
|
|
2539
|
+
intCheck(v, 0, MAX, p);
|
|
2540
|
+
DECIMAL_PLACES = v;
|
|
2541
|
+
}
|
|
2542
|
+
if (obj.hasOwnProperty(p = "ROUNDING_MODE")) {
|
|
2543
|
+
v = obj[p];
|
|
2544
|
+
intCheck(v, 0, 8, p);
|
|
2545
|
+
ROUNDING_MODE = v;
|
|
2546
|
+
}
|
|
2547
|
+
if (obj.hasOwnProperty(p = "EXPONENTIAL_AT")) {
|
|
2548
|
+
v = obj[p];
|
|
2549
|
+
if (v && v.pop) {
|
|
2550
|
+
intCheck(v[0], -MAX, 0, p);
|
|
2551
|
+
intCheck(v[1], 0, MAX, p);
|
|
2552
|
+
TO_EXP_NEG = v[0];
|
|
2553
|
+
TO_EXP_POS = v[1];
|
|
2554
|
+
} else {
|
|
2555
|
+
intCheck(v, -MAX, MAX, p);
|
|
2556
|
+
TO_EXP_NEG = -(TO_EXP_POS = v < 0 ? -v : v);
|
|
2557
|
+
}
|
|
2558
|
+
}
|
|
2559
|
+
if (obj.hasOwnProperty(p = "RANGE")) {
|
|
2560
|
+
v = obj[p];
|
|
2561
|
+
if (v && v.pop) {
|
|
2562
|
+
intCheck(v[0], -MAX, -1, p);
|
|
2563
|
+
intCheck(v[1], 1, MAX, p);
|
|
2564
|
+
MIN_EXP = v[0];
|
|
2565
|
+
MAX_EXP = v[1];
|
|
2566
|
+
} else {
|
|
2567
|
+
intCheck(v, -MAX, MAX, p);
|
|
2568
|
+
if (v) {
|
|
2569
|
+
MIN_EXP = -(MAX_EXP = v < 0 ? -v : v);
|
|
2570
|
+
} else {
|
|
2571
|
+
throw Error(bignumberError + p + " cannot be zero: " + v);
|
|
2572
|
+
}
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
if (obj.hasOwnProperty(p = "CRYPTO")) {
|
|
2576
|
+
v = obj[p];
|
|
2577
|
+
if (v === !!v) {
|
|
2578
|
+
if (v) {
|
|
2579
|
+
if (typeof crypto != "undefined" && crypto && (crypto.getRandomValues || crypto.randomBytes)) {
|
|
2580
|
+
CRYPTO = v;
|
|
2581
|
+
} else {
|
|
2582
|
+
CRYPTO = !v;
|
|
2583
|
+
throw Error(bignumberError + "crypto unavailable");
|
|
2584
|
+
}
|
|
2585
|
+
} else {
|
|
2586
|
+
CRYPTO = v;
|
|
2587
|
+
}
|
|
2588
|
+
} else {
|
|
2589
|
+
throw Error(bignumberError + p + " not true or false: " + v);
|
|
2590
|
+
}
|
|
2591
|
+
}
|
|
2592
|
+
if (obj.hasOwnProperty(p = "MODULO_MODE")) {
|
|
2593
|
+
v = obj[p];
|
|
2594
|
+
intCheck(v, 0, 9, p);
|
|
2595
|
+
MODULO_MODE = v;
|
|
2596
|
+
}
|
|
2597
|
+
if (obj.hasOwnProperty(p = "POW_PRECISION")) {
|
|
2598
|
+
v = obj[p];
|
|
2599
|
+
intCheck(v, 0, MAX, p);
|
|
2600
|
+
POW_PRECISION = v;
|
|
2601
|
+
}
|
|
2602
|
+
if (obj.hasOwnProperty(p = "FORMAT")) {
|
|
2603
|
+
v = obj[p];
|
|
2604
|
+
if (typeof v == "object") FORMAT = v;
|
|
2605
|
+
else throw Error(bignumberError + p + " not an object: " + v);
|
|
2606
|
+
}
|
|
2607
|
+
if (obj.hasOwnProperty(p = "ALPHABET")) {
|
|
2608
|
+
v = obj[p];
|
|
2609
|
+
if (typeof v == "string" && !/^.?$|[+\-.\s]|(.).*\1/.test(v)) {
|
|
2610
|
+
alphabetHasNormalDecimalDigits = v.slice(0, 10) == "0123456789";
|
|
2611
|
+
ALPHABET = v;
|
|
2612
|
+
} else {
|
|
2613
|
+
throw Error(bignumberError + p + " invalid: " + v);
|
|
2614
|
+
}
|
|
2615
|
+
}
|
|
2616
|
+
} else {
|
|
2617
|
+
throw Error(bignumberError + "Object expected: " + obj);
|
|
2618
|
+
}
|
|
2619
|
+
}
|
|
2620
|
+
return {
|
|
2621
|
+
DECIMAL_PLACES,
|
|
2622
|
+
ROUNDING_MODE,
|
|
2623
|
+
EXPONENTIAL_AT: [TO_EXP_NEG, TO_EXP_POS],
|
|
2624
|
+
RANGE: [MIN_EXP, MAX_EXP],
|
|
2625
|
+
CRYPTO,
|
|
2626
|
+
MODULO_MODE,
|
|
2627
|
+
POW_PRECISION,
|
|
2628
|
+
FORMAT,
|
|
2629
|
+
ALPHABET
|
|
2630
|
+
};
|
|
2631
|
+
};
|
|
2632
|
+
BigNumber2.isBigNumber = function(v) {
|
|
2633
|
+
if (!v || v._isBigNumber !== true) return false;
|
|
2634
|
+
if (!BigNumber2.DEBUG) return true;
|
|
2635
|
+
var i, n, c = v.c, e = v.e, s = v.s;
|
|
2636
|
+
out: if ({}.toString.call(c) == "[object Array]") {
|
|
2637
|
+
if ((s === 1 || s === -1) && e >= -MAX && e <= MAX && e === mathfloor(e)) {
|
|
2638
|
+
if (c[0] === 0) {
|
|
2639
|
+
if (e === 0 && c.length === 1) return true;
|
|
2640
|
+
break out;
|
|
2641
|
+
}
|
|
2642
|
+
i = (e + 1) % LOG_BASE;
|
|
2643
|
+
if (i < 1) i += LOG_BASE;
|
|
2644
|
+
if (String(c[0]).length == i) {
|
|
2645
|
+
for (i = 0; i < c.length; i++) {
|
|
2646
|
+
n = c[i];
|
|
2647
|
+
if (n < 0 || n >= BASE || n !== mathfloor(n)) break out;
|
|
2648
|
+
}
|
|
2649
|
+
if (n !== 0) return true;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
} else if (c === null && e === null && (s === null || s === 1 || s === -1)) {
|
|
2653
|
+
return true;
|
|
2654
|
+
}
|
|
2655
|
+
throw Error(bignumberError + "Invalid BigNumber: " + v);
|
|
2656
|
+
};
|
|
2657
|
+
BigNumber2.maximum = BigNumber2.max = function() {
|
|
2658
|
+
return maxOrMin(arguments, -1);
|
|
2659
|
+
};
|
|
2660
|
+
BigNumber2.minimum = BigNumber2.min = function() {
|
|
2661
|
+
return maxOrMin(arguments, 1);
|
|
2662
|
+
};
|
|
2663
|
+
BigNumber2.random = (function() {
|
|
2664
|
+
var pow2_53 = 9007199254740992;
|
|
2665
|
+
var random53bitInt = Math.random() * pow2_53 & 2097151 ? function() {
|
|
2666
|
+
return mathfloor(Math.random() * pow2_53);
|
|
2667
|
+
} : function() {
|
|
2668
|
+
return (Math.random() * 1073741824 | 0) * 8388608 + (Math.random() * 8388608 | 0);
|
|
2669
|
+
};
|
|
2670
|
+
return function(dp) {
|
|
2671
|
+
var a, b2, e, k2, v, i = 0, c = [], rand = new BigNumber2(ONE);
|
|
2672
|
+
if (dp == null) dp = DECIMAL_PLACES;
|
|
2673
|
+
else intCheck(dp, 0, MAX);
|
|
2674
|
+
k2 = mathceil(dp / LOG_BASE);
|
|
2675
|
+
if (CRYPTO) {
|
|
2676
|
+
if (crypto.getRandomValues) {
|
|
2677
|
+
a = crypto.getRandomValues(new Uint32Array(k2 *= 2));
|
|
2678
|
+
for (; i < k2; ) {
|
|
2679
|
+
v = a[i] * 131072 + (a[i + 1] >>> 11);
|
|
2680
|
+
if (v >= 9e15) {
|
|
2681
|
+
b2 = crypto.getRandomValues(new Uint32Array(2));
|
|
2682
|
+
a[i] = b2[0];
|
|
2683
|
+
a[i + 1] = b2[1];
|
|
2684
|
+
} else {
|
|
2685
|
+
c.push(v % 1e14);
|
|
2686
|
+
i += 2;
|
|
2687
|
+
}
|
|
2688
|
+
}
|
|
2689
|
+
i = k2 / 2;
|
|
2690
|
+
} else if (crypto.randomBytes) {
|
|
2691
|
+
a = crypto.randomBytes(k2 *= 7);
|
|
2692
|
+
for (; i < k2; ) {
|
|
2693
|
+
v = (a[i] & 31) * 281474976710656 + a[i + 1] * 1099511627776 + a[i + 2] * 4294967296 + a[i + 3] * 16777216 + (a[i + 4] << 16) + (a[i + 5] << 8) + a[i + 6];
|
|
2694
|
+
if (v >= 9e15) {
|
|
2695
|
+
crypto.randomBytes(7).copy(a, i);
|
|
2696
|
+
} else {
|
|
2697
|
+
c.push(v % 1e14);
|
|
2698
|
+
i += 7;
|
|
2699
|
+
}
|
|
2700
|
+
}
|
|
2701
|
+
i = k2 / 7;
|
|
2702
|
+
} else {
|
|
2703
|
+
CRYPTO = false;
|
|
2704
|
+
throw Error(bignumberError + "crypto unavailable");
|
|
2705
|
+
}
|
|
2706
|
+
}
|
|
2707
|
+
if (!CRYPTO) {
|
|
2708
|
+
for (; i < k2; ) {
|
|
2709
|
+
v = random53bitInt();
|
|
2710
|
+
if (v < 9e15) c[i++] = v % 1e14;
|
|
2711
|
+
}
|
|
2712
|
+
}
|
|
2713
|
+
k2 = c[--i];
|
|
2714
|
+
dp %= LOG_BASE;
|
|
2715
|
+
if (k2 && dp) {
|
|
2716
|
+
v = POWS_TEN[LOG_BASE - dp];
|
|
2717
|
+
c[i] = mathfloor(k2 / v) * v;
|
|
2718
|
+
}
|
|
2719
|
+
for (; c[i] === 0; c.pop(), i--) ;
|
|
2720
|
+
if (i < 0) {
|
|
2721
|
+
c = [e = 0];
|
|
2722
|
+
} else {
|
|
2723
|
+
for (e = -1; c[0] === 0; c.splice(0, 1), e -= LOG_BASE) ;
|
|
2724
|
+
for (i = 1, v = c[0]; v >= 10; v /= 10, i++) ;
|
|
2725
|
+
if (i < LOG_BASE) e -= LOG_BASE - i;
|
|
2726
|
+
}
|
|
2727
|
+
rand.e = e;
|
|
2728
|
+
rand.c = c;
|
|
2729
|
+
return rand;
|
|
2730
|
+
};
|
|
2731
|
+
})();
|
|
2732
|
+
BigNumber2.sum = function() {
|
|
2733
|
+
var i = 1, args = arguments, sum = new BigNumber2(args[0]);
|
|
2734
|
+
for (; i < args.length; ) sum = sum.plus(args[i++]);
|
|
2735
|
+
return sum;
|
|
2736
|
+
};
|
|
2737
|
+
convertBase = /* @__PURE__ */ (function() {
|
|
2738
|
+
var decimal = "0123456789";
|
|
2739
|
+
function toBaseOut(str, baseIn, baseOut, alphabet2) {
|
|
2740
|
+
var j, arr = [0], arrL, i = 0, len = str.length;
|
|
2741
|
+
for (; i < len; ) {
|
|
2742
|
+
for (arrL = arr.length; arrL--; arr[arrL] *= baseIn) ;
|
|
2743
|
+
arr[0] += alphabet2.indexOf(str.charAt(i++));
|
|
2744
|
+
for (j = 0; j < arr.length; j++) {
|
|
2745
|
+
if (arr[j] > baseOut - 1) {
|
|
2746
|
+
if (arr[j + 1] == null) arr[j + 1] = 0;
|
|
2747
|
+
arr[j + 1] += arr[j] / baseOut | 0;
|
|
2748
|
+
arr[j] %= baseOut;
|
|
2749
|
+
}
|
|
2750
|
+
}
|
|
2751
|
+
}
|
|
2752
|
+
return arr.reverse();
|
|
2753
|
+
}
|
|
2754
|
+
return function(str, baseIn, baseOut, sign, callerIsToString) {
|
|
2755
|
+
var alphabet2, d, e, k2, r, x2, xc, y, i = str.indexOf("."), dp = DECIMAL_PLACES, rm = ROUNDING_MODE;
|
|
2756
|
+
if (i >= 0) {
|
|
2757
|
+
k2 = POW_PRECISION;
|
|
2758
|
+
POW_PRECISION = 0;
|
|
2759
|
+
str = str.replace(".", "");
|
|
2760
|
+
y = new BigNumber2(baseIn);
|
|
2761
|
+
x2 = y.pow(str.length - i);
|
|
2762
|
+
POW_PRECISION = k2;
|
|
2763
|
+
y.c = toBaseOut(
|
|
2764
|
+
toFixedPoint(coeffToString(x2.c), x2.e, "0"),
|
|
2765
|
+
10,
|
|
2766
|
+
baseOut,
|
|
2767
|
+
decimal
|
|
2768
|
+
);
|
|
2769
|
+
y.e = y.c.length;
|
|
2770
|
+
}
|
|
2771
|
+
xc = toBaseOut(str, baseIn, baseOut, callerIsToString ? (alphabet2 = ALPHABET, decimal) : (alphabet2 = decimal, ALPHABET));
|
|
2772
|
+
e = k2 = xc.length;
|
|
2773
|
+
for (; xc[--k2] == 0; xc.pop()) ;
|
|
2774
|
+
if (!xc[0]) return alphabet2.charAt(0);
|
|
2775
|
+
if (i < 0) {
|
|
2776
|
+
--e;
|
|
2777
|
+
} else {
|
|
2778
|
+
x2.c = xc;
|
|
2779
|
+
x2.e = e;
|
|
2780
|
+
x2.s = sign;
|
|
2781
|
+
x2 = div(x2, y, dp, rm, baseOut);
|
|
2782
|
+
xc = x2.c;
|
|
2783
|
+
r = x2.r;
|
|
2784
|
+
e = x2.e;
|
|
2785
|
+
}
|
|
2786
|
+
d = e + dp + 1;
|
|
2787
|
+
i = xc[d];
|
|
2788
|
+
k2 = baseOut / 2;
|
|
2789
|
+
r = r || d < 0 || xc[d + 1] != null;
|
|
2790
|
+
r = rm < 4 ? (i != null || r) && (rm == 0 || rm == (x2.s < 0 ? 3 : 2)) : i > k2 || i == k2 && (rm == 4 || r || rm == 6 && xc[d - 1] & 1 || rm == (x2.s < 0 ? 8 : 7));
|
|
2791
|
+
if (d < 1 || !xc[0]) {
|
|
2792
|
+
str = r ? toFixedPoint(alphabet2.charAt(1), -dp, alphabet2.charAt(0)) : alphabet2.charAt(0);
|
|
2793
|
+
} else {
|
|
2794
|
+
xc.length = d;
|
|
2795
|
+
if (r) {
|
|
2796
|
+
for (--baseOut; ++xc[--d] > baseOut; ) {
|
|
2797
|
+
xc[d] = 0;
|
|
2798
|
+
if (!d) {
|
|
2799
|
+
++e;
|
|
2800
|
+
xc = [1].concat(xc);
|
|
2801
|
+
}
|
|
2802
|
+
}
|
|
2803
|
+
}
|
|
2804
|
+
for (k2 = xc.length; !xc[--k2]; ) ;
|
|
2805
|
+
for (i = 0, str = ""; i <= k2; str += alphabet2.charAt(xc[i++])) ;
|
|
2806
|
+
str = toFixedPoint(str, e, alphabet2.charAt(0));
|
|
2807
|
+
}
|
|
2808
|
+
return str;
|
|
2809
|
+
};
|
|
2810
|
+
})();
|
|
2811
|
+
div = /* @__PURE__ */ (function() {
|
|
2812
|
+
function multiply(x2, k2, base) {
|
|
2813
|
+
var m, temp, xlo, xhi, carry = 0, i = x2.length, klo = k2 % SQRT_BASE, khi = k2 / SQRT_BASE | 0;
|
|
2814
|
+
for (x2 = x2.slice(); i--; ) {
|
|
2815
|
+
xlo = x2[i] % SQRT_BASE;
|
|
2816
|
+
xhi = x2[i] / SQRT_BASE | 0;
|
|
2817
|
+
m = khi * xlo + xhi * klo;
|
|
2818
|
+
temp = klo * xlo + m % SQRT_BASE * SQRT_BASE + carry;
|
|
2819
|
+
carry = (temp / base | 0) + (m / SQRT_BASE | 0) + khi * xhi;
|
|
2820
|
+
x2[i] = temp % base;
|
|
2821
|
+
}
|
|
2822
|
+
if (carry) x2 = [carry].concat(x2);
|
|
2823
|
+
return x2;
|
|
2824
|
+
}
|
|
2825
|
+
function compare2(a, b2, aL, bL) {
|
|
2826
|
+
var i, cmp;
|
|
2827
|
+
if (aL != bL) {
|
|
2828
|
+
cmp = aL > bL ? 1 : -1;
|
|
2829
|
+
} else {
|
|
2830
|
+
for (i = cmp = 0; i < aL; i++) {
|
|
2831
|
+
if (a[i] != b2[i]) {
|
|
2832
|
+
cmp = a[i] > b2[i] ? 1 : -1;
|
|
2833
|
+
break;
|
|
2834
|
+
}
|
|
2835
|
+
}
|
|
2836
|
+
}
|
|
2837
|
+
return cmp;
|
|
2838
|
+
}
|
|
2839
|
+
function subtract(a, b2, aL, base) {
|
|
2840
|
+
var i = 0;
|
|
2841
|
+
for (; aL--; ) {
|
|
2842
|
+
a[aL] -= i;
|
|
2843
|
+
i = a[aL] < b2[aL] ? 1 : 0;
|
|
2844
|
+
a[aL] = i * base + a[aL] - b2[aL];
|
|
2845
|
+
}
|
|
2846
|
+
for (; !a[0] && a.length > 1; a.splice(0, 1)) ;
|
|
2847
|
+
}
|
|
2848
|
+
return function(x2, y, dp, rm, base) {
|
|
2849
|
+
var cmp, e, i, more, n, prod, prodL, q2, qc, rem, remL, rem0, xi, xL, yc0, yL, yz, s = x2.s == y.s ? 1 : -1, xc = x2.c, yc = y.c;
|
|
2850
|
+
if (!xc || !xc[0] || !yc || !yc[0]) {
|
|
2851
|
+
return new BigNumber2(
|
|
2852
|
+
// Return NaN if either NaN, or both Infinity or 0.
|
|
2853
|
+
!x2.s || !y.s || (xc ? yc && xc[0] == yc[0] : !yc) ? NaN : (
|
|
2854
|
+
// Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0.
|
|
2855
|
+
xc && xc[0] == 0 || !yc ? s * 0 : s / 0
|
|
2856
|
+
)
|
|
2857
|
+
);
|
|
2858
|
+
}
|
|
2859
|
+
q2 = new BigNumber2(s);
|
|
2860
|
+
qc = q2.c = [];
|
|
2861
|
+
e = x2.e - y.e;
|
|
2862
|
+
s = dp + e + 1;
|
|
2863
|
+
if (!base) {
|
|
2864
|
+
base = BASE;
|
|
2865
|
+
e = bitFloor(x2.e / LOG_BASE) - bitFloor(y.e / LOG_BASE);
|
|
2866
|
+
s = s / LOG_BASE | 0;
|
|
2867
|
+
}
|
|
2868
|
+
for (i = 0; yc[i] == (xc[i] || 0); i++) ;
|
|
2869
|
+
if (yc[i] > (xc[i] || 0)) e--;
|
|
2870
|
+
if (s < 0) {
|
|
2871
|
+
qc.push(1);
|
|
2872
|
+
more = true;
|
|
2873
|
+
} else {
|
|
2874
|
+
xL = xc.length;
|
|
2875
|
+
yL = yc.length;
|
|
2876
|
+
i = 0;
|
|
2877
|
+
s += 2;
|
|
2878
|
+
n = mathfloor(base / (yc[0] + 1));
|
|
2879
|
+
if (n > 1) {
|
|
2880
|
+
yc = multiply(yc, n, base);
|
|
2881
|
+
xc = multiply(xc, n, base);
|
|
2882
|
+
yL = yc.length;
|
|
2883
|
+
xL = xc.length;
|
|
2884
|
+
}
|
|
2885
|
+
xi = yL;
|
|
2886
|
+
rem = xc.slice(0, yL);
|
|
2887
|
+
remL = rem.length;
|
|
2888
|
+
for (; remL < yL; rem[remL++] = 0) ;
|
|
2889
|
+
yz = yc.slice();
|
|
2890
|
+
yz = [0].concat(yz);
|
|
2891
|
+
yc0 = yc[0];
|
|
2892
|
+
if (yc[1] >= base / 2) yc0++;
|
|
2893
|
+
do {
|
|
2894
|
+
n = 0;
|
|
2895
|
+
cmp = compare2(yc, rem, yL, remL);
|
|
2896
|
+
if (cmp < 0) {
|
|
2897
|
+
rem0 = rem[0];
|
|
2898
|
+
if (yL != remL) rem0 = rem0 * base + (rem[1] || 0);
|
|
2899
|
+
n = mathfloor(rem0 / yc0);
|
|
2900
|
+
if (n > 1) {
|
|
2901
|
+
if (n >= base) n = base - 1;
|
|
2902
|
+
prod = multiply(yc, n, base);
|
|
2903
|
+
prodL = prod.length;
|
|
2904
|
+
remL = rem.length;
|
|
2905
|
+
while (compare2(prod, rem, prodL, remL) == 1) {
|
|
2906
|
+
n--;
|
|
2907
|
+
subtract(prod, yL < prodL ? yz : yc, prodL, base);
|
|
2908
|
+
prodL = prod.length;
|
|
2909
|
+
cmp = 1;
|
|
2910
|
+
}
|
|
2911
|
+
} else {
|
|
2912
|
+
if (n == 0) {
|
|
2913
|
+
cmp = n = 1;
|
|
2914
|
+
}
|
|
2915
|
+
prod = yc.slice();
|
|
2916
|
+
prodL = prod.length;
|
|
2917
|
+
}
|
|
2918
|
+
if (prodL < remL) prod = [0].concat(prod);
|
|
2919
|
+
subtract(rem, prod, remL, base);
|
|
2920
|
+
remL = rem.length;
|
|
2921
|
+
if (cmp == -1) {
|
|
2922
|
+
while (compare2(yc, rem, yL, remL) < 1) {
|
|
2923
|
+
n++;
|
|
2924
|
+
subtract(rem, yL < remL ? yz : yc, remL, base);
|
|
2925
|
+
remL = rem.length;
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
} else if (cmp === 0) {
|
|
2929
|
+
n++;
|
|
2930
|
+
rem = [0];
|
|
2931
|
+
}
|
|
2932
|
+
qc[i++] = n;
|
|
2933
|
+
if (rem[0]) {
|
|
2934
|
+
rem[remL++] = xc[xi] || 0;
|
|
2935
|
+
} else {
|
|
2936
|
+
rem = [xc[xi]];
|
|
2937
|
+
remL = 1;
|
|
2938
|
+
}
|
|
2939
|
+
} while ((xi++ < xL || rem[0] != null) && s--);
|
|
2940
|
+
more = rem[0] != null;
|
|
2941
|
+
if (!qc[0]) qc.splice(0, 1);
|
|
2942
|
+
}
|
|
2943
|
+
if (base == BASE) {
|
|
2944
|
+
for (i = 1, s = qc[0]; s >= 10; s /= 10, i++) ;
|
|
2945
|
+
round(q2, dp + (q2.e = i + e * LOG_BASE - 1) + 1, rm, more);
|
|
2946
|
+
} else {
|
|
2947
|
+
q2.e = e;
|
|
2948
|
+
q2.r = +more;
|
|
2949
|
+
}
|
|
2950
|
+
return q2;
|
|
2951
|
+
};
|
|
2952
|
+
})();
|
|
2953
|
+
function format(n, i, rm, id) {
|
|
2954
|
+
var c0, e, ne2, len, str;
|
|
2955
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
2956
|
+
else intCheck(rm, 0, 8);
|
|
2957
|
+
if (!n.c) return n.toString();
|
|
2958
|
+
c0 = n.c[0];
|
|
2959
|
+
ne2 = n.e;
|
|
2960
|
+
if (i == null) {
|
|
2961
|
+
str = coeffToString(n.c);
|
|
2962
|
+
str = id == 1 || id == 2 && (ne2 <= TO_EXP_NEG || ne2 >= TO_EXP_POS) ? toExponential(str, ne2) : toFixedPoint(str, ne2, "0");
|
|
2963
|
+
} else {
|
|
2964
|
+
n = round(new BigNumber2(n), i, rm);
|
|
2965
|
+
e = n.e;
|
|
2966
|
+
str = coeffToString(n.c);
|
|
2967
|
+
len = str.length;
|
|
2968
|
+
if (id == 1 || id == 2 && (i <= e || e <= TO_EXP_NEG)) {
|
|
2969
|
+
for (; len < i; str += "0", len++) ;
|
|
2970
|
+
str = toExponential(str, e);
|
|
2971
|
+
} else {
|
|
2972
|
+
i -= ne2;
|
|
2973
|
+
str = toFixedPoint(str, e, "0");
|
|
2974
|
+
if (e + 1 > len) {
|
|
2975
|
+
if (--i > 0) for (str += "."; i--; str += "0") ;
|
|
2976
|
+
} else {
|
|
2977
|
+
i += e - len;
|
|
2978
|
+
if (i > 0) {
|
|
2979
|
+
if (e + 1 == len) str += ".";
|
|
2980
|
+
for (; i--; str += "0") ;
|
|
2981
|
+
}
|
|
2982
|
+
}
|
|
2983
|
+
}
|
|
2984
|
+
}
|
|
2985
|
+
return n.s < 0 && c0 ? "-" + str : str;
|
|
2986
|
+
}
|
|
2987
|
+
function maxOrMin(args, n) {
|
|
2988
|
+
var k2, y, i = 1, x2 = new BigNumber2(args[0]);
|
|
2989
|
+
for (; i < args.length; i++) {
|
|
2990
|
+
y = new BigNumber2(args[i]);
|
|
2991
|
+
if (!y.s || (k2 = compare(x2, y)) === n || k2 === 0 && x2.s === n) {
|
|
2992
|
+
x2 = y;
|
|
2993
|
+
}
|
|
2994
|
+
}
|
|
2995
|
+
return x2;
|
|
2996
|
+
}
|
|
2997
|
+
function normalise(n, c, e) {
|
|
2998
|
+
var i = 1, j = c.length;
|
|
2999
|
+
for (; !c[--j]; c.pop()) ;
|
|
3000
|
+
for (j = c[0]; j >= 10; j /= 10, i++) ;
|
|
3001
|
+
if ((e = i + e * LOG_BASE - 1) > MAX_EXP) {
|
|
3002
|
+
n.c = n.e = null;
|
|
3003
|
+
} else if (e < MIN_EXP) {
|
|
3004
|
+
n.c = [n.e = 0];
|
|
3005
|
+
} else {
|
|
3006
|
+
n.e = e;
|
|
3007
|
+
n.c = c;
|
|
3008
|
+
}
|
|
3009
|
+
return n;
|
|
3010
|
+
}
|
|
3011
|
+
parseNumeric = /* @__PURE__ */ (function() {
|
|
3012
|
+
var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i, dotAfter = /^([^.]+)\.$/, dotBefore = /^\.([^.]+)$/, isInfinityOrNaN = /^-?(Infinity|NaN)$/, whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
|
|
3013
|
+
return function(x2, str, isNum, b2) {
|
|
3014
|
+
var base, s = isNum ? str : str.replace(whitespaceOrPlus, "");
|
|
3015
|
+
if (isInfinityOrNaN.test(s)) {
|
|
3016
|
+
x2.s = isNaN(s) ? null : s < 0 ? -1 : 1;
|
|
3017
|
+
} else {
|
|
3018
|
+
if (!isNum) {
|
|
3019
|
+
s = s.replace(basePrefix, function(m, p1, p2) {
|
|
3020
|
+
base = (p2 = p2.toLowerCase()) == "x" ? 16 : p2 == "b" ? 2 : 8;
|
|
3021
|
+
return !b2 || b2 == base ? p1 : m;
|
|
3022
|
+
});
|
|
3023
|
+
if (b2) {
|
|
3024
|
+
base = b2;
|
|
3025
|
+
s = s.replace(dotAfter, "$1").replace(dotBefore, "0.$1");
|
|
3026
|
+
}
|
|
3027
|
+
if (str != s) return new BigNumber2(s, base);
|
|
3028
|
+
}
|
|
3029
|
+
if (BigNumber2.DEBUG) {
|
|
3030
|
+
throw Error(bignumberError + "Not a" + (b2 ? " base " + b2 : "") + " number: " + str);
|
|
3031
|
+
}
|
|
3032
|
+
x2.s = null;
|
|
3033
|
+
}
|
|
3034
|
+
x2.c = x2.e = null;
|
|
3035
|
+
};
|
|
3036
|
+
})();
|
|
3037
|
+
function round(x2, sd, rm, r) {
|
|
3038
|
+
var d, i, j, k2, n, ni, rd, xc = x2.c, pows10 = POWS_TEN;
|
|
3039
|
+
if (xc) {
|
|
3040
|
+
out: {
|
|
3041
|
+
for (d = 1, k2 = xc[0]; k2 >= 10; k2 /= 10, d++) ;
|
|
3042
|
+
i = sd - d;
|
|
3043
|
+
if (i < 0) {
|
|
3044
|
+
i += LOG_BASE;
|
|
3045
|
+
j = sd;
|
|
3046
|
+
n = xc[ni = 0];
|
|
3047
|
+
rd = mathfloor(n / pows10[d - j - 1] % 10);
|
|
3048
|
+
} else {
|
|
3049
|
+
ni = mathceil((i + 1) / LOG_BASE);
|
|
3050
|
+
if (ni >= xc.length) {
|
|
3051
|
+
if (r) {
|
|
3052
|
+
for (; xc.length <= ni; xc.push(0)) ;
|
|
3053
|
+
n = rd = 0;
|
|
3054
|
+
d = 1;
|
|
3055
|
+
i %= LOG_BASE;
|
|
3056
|
+
j = i - LOG_BASE + 1;
|
|
3057
|
+
} else {
|
|
3058
|
+
break out;
|
|
3059
|
+
}
|
|
3060
|
+
} else {
|
|
3061
|
+
n = k2 = xc[ni];
|
|
3062
|
+
for (d = 1; k2 >= 10; k2 /= 10, d++) ;
|
|
3063
|
+
i %= LOG_BASE;
|
|
3064
|
+
j = i - LOG_BASE + d;
|
|
3065
|
+
rd = j < 0 ? 0 : mathfloor(n / pows10[d - j - 1] % 10);
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
r = r || sd < 0 || // Are there any non-zero digits after the rounding digit?
|
|
3069
|
+
// The expression n % pows10[d - j - 1] returns all digits of n to the right
|
|
3070
|
+
// of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714.
|
|
3071
|
+
xc[ni + 1] != null || (j < 0 ? n : n % pows10[d - j - 1]);
|
|
3072
|
+
r = rm < 4 ? (rd || r) && (rm == 0 || rm == (x2.s < 0 ? 3 : 2)) : rd > 5 || rd == 5 && (rm == 4 || r || rm == 6 && // Check whether the digit to the left of the rounding digit is odd.
|
|
3073
|
+
(i > 0 ? j > 0 ? n / pows10[d - j] : 0 : xc[ni - 1]) % 10 & 1 || rm == (x2.s < 0 ? 8 : 7));
|
|
3074
|
+
if (sd < 1 || !xc[0]) {
|
|
3075
|
+
xc.length = 0;
|
|
3076
|
+
if (r) {
|
|
3077
|
+
sd -= x2.e + 1;
|
|
3078
|
+
xc[0] = pows10[(LOG_BASE - sd % LOG_BASE) % LOG_BASE];
|
|
3079
|
+
x2.e = -sd || 0;
|
|
3080
|
+
} else {
|
|
3081
|
+
xc[0] = x2.e = 0;
|
|
3082
|
+
}
|
|
3083
|
+
return x2;
|
|
3084
|
+
}
|
|
3085
|
+
if (i == 0) {
|
|
3086
|
+
xc.length = ni;
|
|
3087
|
+
k2 = 1;
|
|
3088
|
+
ni--;
|
|
3089
|
+
} else {
|
|
3090
|
+
xc.length = ni + 1;
|
|
3091
|
+
k2 = pows10[LOG_BASE - i];
|
|
3092
|
+
xc[ni] = j > 0 ? mathfloor(n / pows10[d - j] % pows10[j]) * k2 : 0;
|
|
3093
|
+
}
|
|
3094
|
+
if (r) {
|
|
3095
|
+
for (; ; ) {
|
|
3096
|
+
if (ni == 0) {
|
|
3097
|
+
for (i = 1, j = xc[0]; j >= 10; j /= 10, i++) ;
|
|
3098
|
+
j = xc[0] += k2;
|
|
3099
|
+
for (k2 = 1; j >= 10; j /= 10, k2++) ;
|
|
3100
|
+
if (i != k2) {
|
|
3101
|
+
x2.e++;
|
|
3102
|
+
if (xc[0] == BASE) xc[0] = 1;
|
|
3103
|
+
}
|
|
3104
|
+
break;
|
|
3105
|
+
} else {
|
|
3106
|
+
xc[ni] += k2;
|
|
3107
|
+
if (xc[ni] != BASE) break;
|
|
3108
|
+
xc[ni--] = 0;
|
|
3109
|
+
k2 = 1;
|
|
3110
|
+
}
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
for (i = xc.length; xc[--i] === 0; xc.pop()) ;
|
|
3114
|
+
}
|
|
3115
|
+
if (x2.e > MAX_EXP) {
|
|
3116
|
+
x2.c = x2.e = null;
|
|
3117
|
+
} else if (x2.e < MIN_EXP) {
|
|
3118
|
+
x2.c = [x2.e = 0];
|
|
3119
|
+
}
|
|
3120
|
+
}
|
|
3121
|
+
return x2;
|
|
3122
|
+
}
|
|
3123
|
+
function valueOf(n) {
|
|
3124
|
+
var str, e = n.e;
|
|
3125
|
+
if (e === null) return n.toString();
|
|
3126
|
+
str = coeffToString(n.c);
|
|
3127
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(str, e) : toFixedPoint(str, e, "0");
|
|
3128
|
+
return n.s < 0 ? "-" + str : str;
|
|
3129
|
+
}
|
|
3130
|
+
P2.absoluteValue = P2.abs = function() {
|
|
3131
|
+
var x2 = new BigNumber2(this);
|
|
3132
|
+
if (x2.s < 0) x2.s = 1;
|
|
3133
|
+
return x2;
|
|
3134
|
+
};
|
|
3135
|
+
P2.comparedTo = function(y, b2) {
|
|
3136
|
+
return compare(this, new BigNumber2(y, b2));
|
|
3137
|
+
};
|
|
3138
|
+
P2.decimalPlaces = P2.dp = function(dp, rm) {
|
|
3139
|
+
var c, n, v, x2 = this;
|
|
3140
|
+
if (dp != null) {
|
|
3141
|
+
intCheck(dp, 0, MAX);
|
|
3142
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
3143
|
+
else intCheck(rm, 0, 8);
|
|
3144
|
+
return round(new BigNumber2(x2), dp + x2.e + 1, rm);
|
|
3145
|
+
}
|
|
3146
|
+
if (!(c = x2.c)) return null;
|
|
3147
|
+
n = ((v = c.length - 1) - bitFloor(this.e / LOG_BASE)) * LOG_BASE;
|
|
3148
|
+
if (v = c[v]) for (; v % 10 == 0; v /= 10, n--) ;
|
|
3149
|
+
if (n < 0) n = 0;
|
|
3150
|
+
return n;
|
|
3151
|
+
};
|
|
3152
|
+
P2.dividedBy = P2.div = function(y, b2) {
|
|
3153
|
+
return div(this, new BigNumber2(y, b2), DECIMAL_PLACES, ROUNDING_MODE);
|
|
3154
|
+
};
|
|
3155
|
+
P2.dividedToIntegerBy = P2.idiv = function(y, b2) {
|
|
3156
|
+
return div(this, new BigNumber2(y, b2), 0, 1);
|
|
3157
|
+
};
|
|
3158
|
+
P2.exponentiatedBy = P2.pow = function(n, m) {
|
|
3159
|
+
var half, isModExp, i, k2, more, nIsBig, nIsNeg, nIsOdd, y, x2 = this;
|
|
3160
|
+
n = new BigNumber2(n);
|
|
3161
|
+
if (n.c && !n.isInteger()) {
|
|
3162
|
+
throw Error(bignumberError + "Exponent not an integer: " + valueOf(n));
|
|
3163
|
+
}
|
|
3164
|
+
if (m != null) m = new BigNumber2(m);
|
|
3165
|
+
nIsBig = n.e > 14;
|
|
3166
|
+
if (!x2.c || !x2.c[0] || x2.c[0] == 1 && !x2.e && x2.c.length == 1 || !n.c || !n.c[0]) {
|
|
3167
|
+
y = new BigNumber2(Math.pow(+valueOf(x2), nIsBig ? n.s * (2 - isOdd(n)) : +valueOf(n)));
|
|
3168
|
+
return m ? y.mod(m) : y;
|
|
3169
|
+
}
|
|
3170
|
+
nIsNeg = n.s < 0;
|
|
3171
|
+
if (m) {
|
|
3172
|
+
if (m.c ? !m.c[0] : !m.s) return new BigNumber2(NaN);
|
|
3173
|
+
isModExp = !nIsNeg && x2.isInteger() && m.isInteger();
|
|
3174
|
+
if (isModExp) x2 = x2.mod(m);
|
|
3175
|
+
} else if (n.e > 9 && (x2.e > 0 || x2.e < -1 || (x2.e == 0 ? x2.c[0] > 1 || nIsBig && x2.c[1] >= 24e7 : x2.c[0] < 8e13 || nIsBig && x2.c[0] <= 9999975e7))) {
|
|
3176
|
+
k2 = x2.s < 0 && isOdd(n) ? -0 : 0;
|
|
3177
|
+
if (x2.e > -1) k2 = 1 / k2;
|
|
3178
|
+
return new BigNumber2(nIsNeg ? 1 / k2 : k2);
|
|
3179
|
+
} else if (POW_PRECISION) {
|
|
3180
|
+
k2 = mathceil(POW_PRECISION / LOG_BASE + 2);
|
|
3181
|
+
}
|
|
3182
|
+
if (nIsBig) {
|
|
3183
|
+
half = new BigNumber2(0.5);
|
|
3184
|
+
if (nIsNeg) n.s = 1;
|
|
3185
|
+
nIsOdd = isOdd(n);
|
|
3186
|
+
} else {
|
|
3187
|
+
i = Math.abs(+valueOf(n));
|
|
3188
|
+
nIsOdd = i % 2;
|
|
3189
|
+
}
|
|
3190
|
+
y = new BigNumber2(ONE);
|
|
3191
|
+
for (; ; ) {
|
|
3192
|
+
if (nIsOdd) {
|
|
3193
|
+
y = y.times(x2);
|
|
3194
|
+
if (!y.c) break;
|
|
3195
|
+
if (k2) {
|
|
3196
|
+
if (y.c.length > k2) y.c.length = k2;
|
|
3197
|
+
} else if (isModExp) {
|
|
3198
|
+
y = y.mod(m);
|
|
3199
|
+
}
|
|
3200
|
+
}
|
|
3201
|
+
if (i) {
|
|
3202
|
+
i = mathfloor(i / 2);
|
|
3203
|
+
if (i === 0) break;
|
|
3204
|
+
nIsOdd = i % 2;
|
|
3205
|
+
} else {
|
|
3206
|
+
n = n.times(half);
|
|
3207
|
+
round(n, n.e + 1, 1);
|
|
3208
|
+
if (n.e > 14) {
|
|
3209
|
+
nIsOdd = isOdd(n);
|
|
3210
|
+
} else {
|
|
3211
|
+
i = +valueOf(n);
|
|
3212
|
+
if (i === 0) break;
|
|
3213
|
+
nIsOdd = i % 2;
|
|
3214
|
+
}
|
|
3215
|
+
}
|
|
3216
|
+
x2 = x2.times(x2);
|
|
3217
|
+
if (k2) {
|
|
3218
|
+
if (x2.c && x2.c.length > k2) x2.c.length = k2;
|
|
3219
|
+
} else if (isModExp) {
|
|
3220
|
+
x2 = x2.mod(m);
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
if (isModExp) return y;
|
|
3224
|
+
if (nIsNeg) y = ONE.div(y);
|
|
3225
|
+
return m ? y.mod(m) : k2 ? round(y, POW_PRECISION, ROUNDING_MODE, more) : y;
|
|
3226
|
+
};
|
|
3227
|
+
P2.integerValue = function(rm) {
|
|
3228
|
+
var n = new BigNumber2(this);
|
|
3229
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
3230
|
+
else intCheck(rm, 0, 8);
|
|
3231
|
+
return round(n, n.e + 1, rm);
|
|
3232
|
+
};
|
|
3233
|
+
P2.isEqualTo = P2.eq = function(y, b2) {
|
|
3234
|
+
return compare(this, new BigNumber2(y, b2)) === 0;
|
|
3235
|
+
};
|
|
3236
|
+
P2.isFinite = function() {
|
|
3237
|
+
return !!this.c;
|
|
3238
|
+
};
|
|
3239
|
+
P2.isGreaterThan = P2.gt = function(y, b2) {
|
|
3240
|
+
return compare(this, new BigNumber2(y, b2)) > 0;
|
|
3241
|
+
};
|
|
3242
|
+
P2.isGreaterThanOrEqualTo = P2.gte = function(y, b2) {
|
|
3243
|
+
return (b2 = compare(this, new BigNumber2(y, b2))) === 1 || b2 === 0;
|
|
3244
|
+
};
|
|
3245
|
+
P2.isInteger = function() {
|
|
3246
|
+
return !!this.c && bitFloor(this.e / LOG_BASE) > this.c.length - 2;
|
|
3247
|
+
};
|
|
3248
|
+
P2.isLessThan = P2.lt = function(y, b2) {
|
|
3249
|
+
return compare(this, new BigNumber2(y, b2)) < 0;
|
|
3250
|
+
};
|
|
3251
|
+
P2.isLessThanOrEqualTo = P2.lte = function(y, b2) {
|
|
3252
|
+
return (b2 = compare(this, new BigNumber2(y, b2))) === -1 || b2 === 0;
|
|
3253
|
+
};
|
|
3254
|
+
P2.isNaN = function() {
|
|
3255
|
+
return !this.s;
|
|
3256
|
+
};
|
|
3257
|
+
P2.isNegative = function() {
|
|
3258
|
+
return this.s < 0;
|
|
3259
|
+
};
|
|
3260
|
+
P2.isPositive = function() {
|
|
3261
|
+
return this.s > 0;
|
|
3262
|
+
};
|
|
3263
|
+
P2.isZero = function() {
|
|
3264
|
+
return !!this.c && this.c[0] == 0;
|
|
3265
|
+
};
|
|
3266
|
+
P2.minus = function(y, b2) {
|
|
3267
|
+
var i, j, t, xLTy, x2 = this, a = x2.s;
|
|
3268
|
+
y = new BigNumber2(y, b2);
|
|
3269
|
+
b2 = y.s;
|
|
3270
|
+
if (!a || !b2) return new BigNumber2(NaN);
|
|
3271
|
+
if (a != b2) {
|
|
3272
|
+
y.s = -b2;
|
|
3273
|
+
return x2.plus(y);
|
|
3274
|
+
}
|
|
3275
|
+
var xe2 = x2.e / LOG_BASE, ye2 = y.e / LOG_BASE, xc = x2.c, yc = y.c;
|
|
3276
|
+
if (!xe2 || !ye2) {
|
|
3277
|
+
if (!xc || !yc) return xc ? (y.s = -b2, y) : new BigNumber2(yc ? x2 : NaN);
|
|
3278
|
+
if (!xc[0] || !yc[0]) {
|
|
3279
|
+
return yc[0] ? (y.s = -b2, y) : new BigNumber2(xc[0] ? x2 : (
|
|
3280
|
+
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity
|
|
3281
|
+
ROUNDING_MODE == 3 ? -0 : 0
|
|
3282
|
+
));
|
|
3283
|
+
}
|
|
3284
|
+
}
|
|
3285
|
+
xe2 = bitFloor(xe2);
|
|
3286
|
+
ye2 = bitFloor(ye2);
|
|
3287
|
+
xc = xc.slice();
|
|
3288
|
+
if (a = xe2 - ye2) {
|
|
3289
|
+
if (xLTy = a < 0) {
|
|
3290
|
+
a = -a;
|
|
3291
|
+
t = xc;
|
|
3292
|
+
} else {
|
|
3293
|
+
ye2 = xe2;
|
|
3294
|
+
t = yc;
|
|
3295
|
+
}
|
|
3296
|
+
t.reverse();
|
|
3297
|
+
for (b2 = a; b2--; t.push(0)) ;
|
|
3298
|
+
t.reverse();
|
|
3299
|
+
} else {
|
|
3300
|
+
j = (xLTy = (a = xc.length) < (b2 = yc.length)) ? a : b2;
|
|
3301
|
+
for (a = b2 = 0; b2 < j; b2++) {
|
|
3302
|
+
if (xc[b2] != yc[b2]) {
|
|
3303
|
+
xLTy = xc[b2] < yc[b2];
|
|
3304
|
+
break;
|
|
3305
|
+
}
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
if (xLTy) {
|
|
3309
|
+
t = xc;
|
|
3310
|
+
xc = yc;
|
|
3311
|
+
yc = t;
|
|
3312
|
+
y.s = -y.s;
|
|
3313
|
+
}
|
|
3314
|
+
b2 = (j = yc.length) - (i = xc.length);
|
|
3315
|
+
if (b2 > 0) for (; b2--; xc[i++] = 0) ;
|
|
3316
|
+
b2 = BASE - 1;
|
|
3317
|
+
for (; j > a; ) {
|
|
3318
|
+
if (xc[--j] < yc[j]) {
|
|
3319
|
+
for (i = j; i && !xc[--i]; xc[i] = b2) ;
|
|
3320
|
+
--xc[i];
|
|
3321
|
+
xc[j] += BASE;
|
|
3322
|
+
}
|
|
3323
|
+
xc[j] -= yc[j];
|
|
3324
|
+
}
|
|
3325
|
+
for (; xc[0] == 0; xc.splice(0, 1), --ye2) ;
|
|
3326
|
+
if (!xc[0]) {
|
|
3327
|
+
y.s = ROUNDING_MODE == 3 ? -1 : 1;
|
|
3328
|
+
y.c = [y.e = 0];
|
|
3329
|
+
return y;
|
|
3330
|
+
}
|
|
3331
|
+
return normalise(y, xc, ye2);
|
|
3332
|
+
};
|
|
3333
|
+
P2.modulo = P2.mod = function(y, b2) {
|
|
3334
|
+
var q2, s, x2 = this;
|
|
3335
|
+
y = new BigNumber2(y, b2);
|
|
3336
|
+
if (!x2.c || !y.s || y.c && !y.c[0]) {
|
|
3337
|
+
return new BigNumber2(NaN);
|
|
3338
|
+
} else if (!y.c || x2.c && !x2.c[0]) {
|
|
3339
|
+
return new BigNumber2(x2);
|
|
3340
|
+
}
|
|
3341
|
+
if (MODULO_MODE == 9) {
|
|
3342
|
+
s = y.s;
|
|
3343
|
+
y.s = 1;
|
|
3344
|
+
q2 = div(x2, y, 0, 3);
|
|
3345
|
+
y.s = s;
|
|
3346
|
+
q2.s *= s;
|
|
3347
|
+
} else {
|
|
3348
|
+
q2 = div(x2, y, 0, MODULO_MODE);
|
|
3349
|
+
}
|
|
3350
|
+
y = x2.minus(q2.times(y));
|
|
3351
|
+
if (!y.c[0] && MODULO_MODE == 1) y.s = x2.s;
|
|
3352
|
+
return y;
|
|
3353
|
+
};
|
|
3354
|
+
P2.multipliedBy = P2.times = function(y, b2) {
|
|
3355
|
+
var c, e, i, j, k2, m, xcL, xlo, xhi, ycL, ylo, yhi, zc, base, sqrtBase, x2 = this, xc = x2.c, yc = (y = new BigNumber2(y, b2)).c;
|
|
3356
|
+
if (!xc || !yc || !xc[0] || !yc[0]) {
|
|
3357
|
+
if (!x2.s || !y.s || xc && !xc[0] && !yc || yc && !yc[0] && !xc) {
|
|
3358
|
+
y.c = y.e = y.s = null;
|
|
3359
|
+
} else {
|
|
3360
|
+
y.s *= x2.s;
|
|
3361
|
+
if (!xc || !yc) {
|
|
3362
|
+
y.c = y.e = null;
|
|
3363
|
+
} else {
|
|
3364
|
+
y.c = [0];
|
|
3365
|
+
y.e = 0;
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
return y;
|
|
3369
|
+
}
|
|
3370
|
+
e = bitFloor(x2.e / LOG_BASE) + bitFloor(y.e / LOG_BASE);
|
|
3371
|
+
y.s *= x2.s;
|
|
3372
|
+
xcL = xc.length;
|
|
3373
|
+
ycL = yc.length;
|
|
3374
|
+
if (xcL < ycL) {
|
|
3375
|
+
zc = xc;
|
|
3376
|
+
xc = yc;
|
|
3377
|
+
yc = zc;
|
|
3378
|
+
i = xcL;
|
|
3379
|
+
xcL = ycL;
|
|
3380
|
+
ycL = i;
|
|
3381
|
+
}
|
|
3382
|
+
for (i = xcL + ycL, zc = []; i--; zc.push(0)) ;
|
|
3383
|
+
base = BASE;
|
|
3384
|
+
sqrtBase = SQRT_BASE;
|
|
3385
|
+
for (i = ycL; --i >= 0; ) {
|
|
3386
|
+
c = 0;
|
|
3387
|
+
ylo = yc[i] % sqrtBase;
|
|
3388
|
+
yhi = yc[i] / sqrtBase | 0;
|
|
3389
|
+
for (k2 = xcL, j = i + k2; j > i; ) {
|
|
3390
|
+
xlo = xc[--k2] % sqrtBase;
|
|
3391
|
+
xhi = xc[k2] / sqrtBase | 0;
|
|
3392
|
+
m = yhi * xlo + xhi * ylo;
|
|
3393
|
+
xlo = ylo * xlo + m % sqrtBase * sqrtBase + zc[j] + c;
|
|
3394
|
+
c = (xlo / base | 0) + (m / sqrtBase | 0) + yhi * xhi;
|
|
3395
|
+
zc[j--] = xlo % base;
|
|
3396
|
+
}
|
|
3397
|
+
zc[j] = c;
|
|
3398
|
+
}
|
|
3399
|
+
if (c) {
|
|
3400
|
+
++e;
|
|
3401
|
+
} else {
|
|
3402
|
+
zc.splice(0, 1);
|
|
3403
|
+
}
|
|
3404
|
+
return normalise(y, zc, e);
|
|
3405
|
+
};
|
|
3406
|
+
P2.negated = function() {
|
|
3407
|
+
var x2 = new BigNumber2(this);
|
|
3408
|
+
x2.s = -x2.s || null;
|
|
3409
|
+
return x2;
|
|
3410
|
+
};
|
|
3411
|
+
P2.plus = function(y, b2) {
|
|
3412
|
+
var t, x2 = this, a = x2.s;
|
|
3413
|
+
y = new BigNumber2(y, b2);
|
|
3414
|
+
b2 = y.s;
|
|
3415
|
+
if (!a || !b2) return new BigNumber2(NaN);
|
|
3416
|
+
if (a != b2) {
|
|
3417
|
+
y.s = -b2;
|
|
3418
|
+
return x2.minus(y);
|
|
3419
|
+
}
|
|
3420
|
+
var xe2 = x2.e / LOG_BASE, ye2 = y.e / LOG_BASE, xc = x2.c, yc = y.c;
|
|
3421
|
+
if (!xe2 || !ye2) {
|
|
3422
|
+
if (!xc || !yc) return new BigNumber2(a / 0);
|
|
3423
|
+
if (!xc[0] || !yc[0]) return yc[0] ? y : new BigNumber2(xc[0] ? x2 : a * 0);
|
|
3424
|
+
}
|
|
3425
|
+
xe2 = bitFloor(xe2);
|
|
3426
|
+
ye2 = bitFloor(ye2);
|
|
3427
|
+
xc = xc.slice();
|
|
3428
|
+
if (a = xe2 - ye2) {
|
|
3429
|
+
if (a > 0) {
|
|
3430
|
+
ye2 = xe2;
|
|
3431
|
+
t = yc;
|
|
3432
|
+
} else {
|
|
3433
|
+
a = -a;
|
|
3434
|
+
t = xc;
|
|
3435
|
+
}
|
|
3436
|
+
t.reverse();
|
|
3437
|
+
for (; a--; t.push(0)) ;
|
|
3438
|
+
t.reverse();
|
|
3439
|
+
}
|
|
3440
|
+
a = xc.length;
|
|
3441
|
+
b2 = yc.length;
|
|
3442
|
+
if (a - b2 < 0) {
|
|
3443
|
+
t = yc;
|
|
3444
|
+
yc = xc;
|
|
3445
|
+
xc = t;
|
|
3446
|
+
b2 = a;
|
|
3447
|
+
}
|
|
3448
|
+
for (a = 0; b2; ) {
|
|
3449
|
+
a = (xc[--b2] = xc[b2] + yc[b2] + a) / BASE | 0;
|
|
3450
|
+
xc[b2] = BASE === xc[b2] ? 0 : xc[b2] % BASE;
|
|
3451
|
+
}
|
|
3452
|
+
if (a) {
|
|
3453
|
+
xc = [a].concat(xc);
|
|
3454
|
+
++ye2;
|
|
3455
|
+
}
|
|
3456
|
+
return normalise(y, xc, ye2);
|
|
3457
|
+
};
|
|
3458
|
+
P2.precision = P2.sd = function(sd, rm) {
|
|
3459
|
+
var c, n, v, x2 = this;
|
|
3460
|
+
if (sd != null && sd !== !!sd) {
|
|
3461
|
+
intCheck(sd, 1, MAX);
|
|
3462
|
+
if (rm == null) rm = ROUNDING_MODE;
|
|
3463
|
+
else intCheck(rm, 0, 8);
|
|
3464
|
+
return round(new BigNumber2(x2), sd, rm);
|
|
3465
|
+
}
|
|
3466
|
+
if (!(c = x2.c)) return null;
|
|
3467
|
+
v = c.length - 1;
|
|
3468
|
+
n = v * LOG_BASE + 1;
|
|
3469
|
+
if (v = c[v]) {
|
|
3470
|
+
for (; v % 10 == 0; v /= 10, n--) ;
|
|
3471
|
+
for (v = c[0]; v >= 10; v /= 10, n++) ;
|
|
3472
|
+
}
|
|
3473
|
+
if (sd && x2.e + 1 > n) n = x2.e + 1;
|
|
3474
|
+
return n;
|
|
3475
|
+
};
|
|
3476
|
+
P2.shiftedBy = function(k2) {
|
|
3477
|
+
intCheck(k2, -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
|
|
3478
|
+
return this.times("1e" + k2);
|
|
3479
|
+
};
|
|
3480
|
+
P2.squareRoot = P2.sqrt = function() {
|
|
3481
|
+
var m, n, r, rep, t, x2 = this, c = x2.c, s = x2.s, e = x2.e, dp = DECIMAL_PLACES + 4, half = new BigNumber2("0.5");
|
|
3482
|
+
if (s !== 1 || !c || !c[0]) {
|
|
3483
|
+
return new BigNumber2(!s || s < 0 && (!c || c[0]) ? NaN : c ? x2 : 1 / 0);
|
|
3484
|
+
}
|
|
3485
|
+
s = Math.sqrt(+valueOf(x2));
|
|
3486
|
+
if (s == 0 || s == 1 / 0) {
|
|
3487
|
+
n = coeffToString(c);
|
|
3488
|
+
if ((n.length + e) % 2 == 0) n += "0";
|
|
3489
|
+
s = Math.sqrt(+n);
|
|
3490
|
+
e = bitFloor((e + 1) / 2) - (e < 0 || e % 2);
|
|
3491
|
+
if (s == 1 / 0) {
|
|
3492
|
+
n = "5e" + e;
|
|
3493
|
+
} else {
|
|
3494
|
+
n = s.toExponential();
|
|
3495
|
+
n = n.slice(0, n.indexOf("e") + 1) + e;
|
|
3496
|
+
}
|
|
3497
|
+
r = new BigNumber2(n);
|
|
3498
|
+
} else {
|
|
3499
|
+
r = new BigNumber2(s + "");
|
|
3500
|
+
}
|
|
3501
|
+
if (r.c[0]) {
|
|
3502
|
+
e = r.e;
|
|
3503
|
+
s = e + dp;
|
|
3504
|
+
if (s < 3) s = 0;
|
|
3505
|
+
for (; ; ) {
|
|
3506
|
+
t = r;
|
|
3507
|
+
r = half.times(t.plus(div(x2, t, dp, 1)));
|
|
3508
|
+
if (coeffToString(t.c).slice(0, s) === (n = coeffToString(r.c)).slice(0, s)) {
|
|
3509
|
+
if (r.e < e) --s;
|
|
3510
|
+
n = n.slice(s - 3, s + 1);
|
|
3511
|
+
if (n == "9999" || !rep && n == "4999") {
|
|
3512
|
+
if (!rep) {
|
|
3513
|
+
round(t, t.e + DECIMAL_PLACES + 2, 0);
|
|
3514
|
+
if (t.times(t).eq(x2)) {
|
|
3515
|
+
r = t;
|
|
3516
|
+
break;
|
|
3517
|
+
}
|
|
3518
|
+
}
|
|
3519
|
+
dp += 4;
|
|
3520
|
+
s += 4;
|
|
3521
|
+
rep = 1;
|
|
3522
|
+
} else {
|
|
3523
|
+
if (!+n || !+n.slice(1) && n.charAt(0) == "5") {
|
|
3524
|
+
round(r, r.e + DECIMAL_PLACES + 2, 1);
|
|
3525
|
+
m = !r.times(r).eq(x2);
|
|
3526
|
+
}
|
|
3527
|
+
break;
|
|
3528
|
+
}
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
}
|
|
3532
|
+
return round(r, r.e + DECIMAL_PLACES + 1, ROUNDING_MODE, m);
|
|
3533
|
+
};
|
|
3534
|
+
P2.toExponential = function(dp, rm) {
|
|
3535
|
+
if (dp != null) {
|
|
3536
|
+
intCheck(dp, 0, MAX);
|
|
3537
|
+
dp++;
|
|
3538
|
+
}
|
|
3539
|
+
return format(this, dp, rm, 1);
|
|
3540
|
+
};
|
|
3541
|
+
P2.toFixed = function(dp, rm) {
|
|
3542
|
+
if (dp != null) {
|
|
3543
|
+
intCheck(dp, 0, MAX);
|
|
3544
|
+
dp = dp + this.e + 1;
|
|
3545
|
+
}
|
|
3546
|
+
return format(this, dp, rm);
|
|
3547
|
+
};
|
|
3548
|
+
P2.toFormat = function(dp, rm, format2) {
|
|
3549
|
+
var str, x2 = this;
|
|
3550
|
+
if (format2 == null) {
|
|
3551
|
+
if (dp != null && rm && typeof rm == "object") {
|
|
3552
|
+
format2 = rm;
|
|
3553
|
+
rm = null;
|
|
3554
|
+
} else if (dp && typeof dp == "object") {
|
|
3555
|
+
format2 = dp;
|
|
3556
|
+
dp = rm = null;
|
|
3557
|
+
} else {
|
|
3558
|
+
format2 = FORMAT;
|
|
3559
|
+
}
|
|
3560
|
+
} else if (typeof format2 != "object") {
|
|
3561
|
+
throw Error(bignumberError + "Argument not an object: " + format2);
|
|
3562
|
+
}
|
|
3563
|
+
str = x2.toFixed(dp, rm);
|
|
3564
|
+
if (x2.c) {
|
|
3565
|
+
var i, arr = str.split("."), g1 = +format2.groupSize, g2 = +format2.secondaryGroupSize, groupSeparator = format2.groupSeparator || "", intPart = arr[0], fractionPart = arr[1], isNeg = x2.s < 0, intDigits = isNeg ? intPart.slice(1) : intPart, len = intDigits.length;
|
|
3566
|
+
if (g2) {
|
|
3567
|
+
i = g1;
|
|
3568
|
+
g1 = g2;
|
|
3569
|
+
g2 = i;
|
|
3570
|
+
len -= i;
|
|
3571
|
+
}
|
|
3572
|
+
if (g1 > 0 && len > 0) {
|
|
3573
|
+
i = len % g1 || g1;
|
|
3574
|
+
intPart = intDigits.substr(0, i);
|
|
3575
|
+
for (; i < len; i += g1) intPart += groupSeparator + intDigits.substr(i, g1);
|
|
3576
|
+
if (g2 > 0) intPart += groupSeparator + intDigits.slice(i);
|
|
3577
|
+
if (isNeg) intPart = "-" + intPart;
|
|
3578
|
+
}
|
|
3579
|
+
str = fractionPart ? intPart + (format2.decimalSeparator || "") + ((g2 = +format2.fractionGroupSize) ? fractionPart.replace(
|
|
3580
|
+
new RegExp("\\d{" + g2 + "}\\B", "g"),
|
|
3581
|
+
"$&" + (format2.fractionGroupSeparator || "")
|
|
3582
|
+
) : fractionPart) : intPart;
|
|
3583
|
+
}
|
|
3584
|
+
return (format2.prefix || "") + str + (format2.suffix || "");
|
|
3585
|
+
};
|
|
3586
|
+
P2.toFraction = function(md) {
|
|
3587
|
+
var d, d0, d1, d2, e, exp, n, n0, n1, q2, r, s, x2 = this, xc = x2.c;
|
|
3588
|
+
if (md != null) {
|
|
3589
|
+
n = new BigNumber2(md);
|
|
3590
|
+
if (!n.isInteger() && (n.c || n.s !== 1) || n.lt(ONE)) {
|
|
3591
|
+
throw Error(bignumberError + "Argument " + (n.isInteger() ? "out of range: " : "not an integer: ") + valueOf(n));
|
|
3592
|
+
}
|
|
3593
|
+
}
|
|
3594
|
+
if (!xc) return new BigNumber2(x2);
|
|
3595
|
+
d = new BigNumber2(ONE);
|
|
3596
|
+
n1 = d0 = new BigNumber2(ONE);
|
|
3597
|
+
d1 = n0 = new BigNumber2(ONE);
|
|
3598
|
+
s = coeffToString(xc);
|
|
3599
|
+
e = d.e = s.length - x2.e - 1;
|
|
3600
|
+
d.c[0] = POWS_TEN[(exp = e % LOG_BASE) < 0 ? LOG_BASE + exp : exp];
|
|
3601
|
+
md = !md || n.comparedTo(d) > 0 ? e > 0 ? d : n1 : n;
|
|
3602
|
+
exp = MAX_EXP;
|
|
3603
|
+
MAX_EXP = 1 / 0;
|
|
3604
|
+
n = new BigNumber2(s);
|
|
3605
|
+
n0.c[0] = 0;
|
|
3606
|
+
for (; ; ) {
|
|
3607
|
+
q2 = div(n, d, 0, 1);
|
|
3608
|
+
d2 = d0.plus(q2.times(d1));
|
|
3609
|
+
if (d2.comparedTo(md) == 1) break;
|
|
3610
|
+
d0 = d1;
|
|
3611
|
+
d1 = d2;
|
|
3612
|
+
n1 = n0.plus(q2.times(d2 = n1));
|
|
3613
|
+
n0 = d2;
|
|
3614
|
+
d = n.minus(q2.times(d2 = d));
|
|
3615
|
+
n = d2;
|
|
3616
|
+
}
|
|
3617
|
+
d2 = div(md.minus(d0), d1, 0, 1);
|
|
3618
|
+
n0 = n0.plus(d2.times(n1));
|
|
3619
|
+
d0 = d0.plus(d2.times(d1));
|
|
3620
|
+
n0.s = n1.s = x2.s;
|
|
3621
|
+
e = e * 2;
|
|
3622
|
+
r = div(n1, d1, e, ROUNDING_MODE).minus(x2).abs().comparedTo(
|
|
3623
|
+
div(n0, d0, e, ROUNDING_MODE).minus(x2).abs()
|
|
3624
|
+
) < 1 ? [n1, d1] : [n0, d0];
|
|
3625
|
+
MAX_EXP = exp;
|
|
3626
|
+
return r;
|
|
3627
|
+
};
|
|
3628
|
+
P2.toNumber = function() {
|
|
3629
|
+
return +valueOf(this);
|
|
3630
|
+
};
|
|
3631
|
+
P2.toPrecision = function(sd, rm) {
|
|
3632
|
+
if (sd != null) intCheck(sd, 1, MAX);
|
|
3633
|
+
return format(this, sd, rm, 2);
|
|
3634
|
+
};
|
|
3635
|
+
P2.toString = function(b2) {
|
|
3636
|
+
var str, n = this, s = n.s, e = n.e;
|
|
3637
|
+
if (e === null) {
|
|
3638
|
+
if (s) {
|
|
3639
|
+
str = "Infinity";
|
|
3640
|
+
if (s < 0) str = "-" + str;
|
|
3641
|
+
} else {
|
|
3642
|
+
str = "NaN";
|
|
3643
|
+
}
|
|
3644
|
+
} else {
|
|
3645
|
+
if (b2 == null) {
|
|
3646
|
+
str = e <= TO_EXP_NEG || e >= TO_EXP_POS ? toExponential(coeffToString(n.c), e) : toFixedPoint(coeffToString(n.c), e, "0");
|
|
3647
|
+
} else if (b2 === 10 && alphabetHasNormalDecimalDigits) {
|
|
3648
|
+
n = round(new BigNumber2(n), DECIMAL_PLACES + e + 1, ROUNDING_MODE);
|
|
3649
|
+
str = toFixedPoint(coeffToString(n.c), n.e, "0");
|
|
3650
|
+
} else {
|
|
3651
|
+
intCheck(b2, 2, ALPHABET.length, "Base");
|
|
3652
|
+
str = convertBase(toFixedPoint(coeffToString(n.c), e, "0"), 10, b2, s, true);
|
|
3653
|
+
}
|
|
3654
|
+
if (s < 0 && n.c[0]) str = "-" + str;
|
|
3655
|
+
}
|
|
3656
|
+
return str;
|
|
3657
|
+
};
|
|
3658
|
+
P2.valueOf = P2.toJSON = function() {
|
|
3659
|
+
return valueOf(this);
|
|
3660
|
+
};
|
|
3661
|
+
P2._isBigNumber = true;
|
|
3662
|
+
P2[Symbol.toStringTag] = "BigNumber";
|
|
3663
|
+
P2[/* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom")] = P2.valueOf;
|
|
3664
|
+
if (configObject != null) BigNumber2.set(configObject);
|
|
3665
|
+
return BigNumber2;
|
|
3666
|
+
}
|
|
3667
|
+
function bitFloor(n) {
|
|
3668
|
+
var i = n | 0;
|
|
3669
|
+
return n > 0 || n === i ? i : i - 1;
|
|
3670
|
+
}
|
|
3671
|
+
function coeffToString(a) {
|
|
3672
|
+
var s, z2, i = 1, j = a.length, r = a[0] + "";
|
|
3673
|
+
for (; i < j; ) {
|
|
3674
|
+
s = a[i++] + "";
|
|
3675
|
+
z2 = LOG_BASE - s.length;
|
|
3676
|
+
for (; z2--; s = "0" + s) ;
|
|
3677
|
+
r += s;
|
|
3678
|
+
}
|
|
3679
|
+
for (j = r.length; r.charCodeAt(--j) === 48; ) ;
|
|
3680
|
+
return r.slice(0, j + 1 || 1);
|
|
3681
|
+
}
|
|
3682
|
+
function compare(x2, y) {
|
|
3683
|
+
var a, b2, xc = x2.c, yc = y.c, i = x2.s, j = y.s, k2 = x2.e, l = y.e;
|
|
3684
|
+
if (!i || !j) return null;
|
|
3685
|
+
a = xc && !xc[0];
|
|
3686
|
+
b2 = yc && !yc[0];
|
|
3687
|
+
if (a || b2) return a ? b2 ? 0 : -j : i;
|
|
3688
|
+
if (i != j) return i;
|
|
3689
|
+
a = i < 0;
|
|
3690
|
+
b2 = k2 == l;
|
|
3691
|
+
if (!xc || !yc) return b2 ? 0 : !xc ^ a ? 1 : -1;
|
|
3692
|
+
if (!b2) return k2 > l ^ a ? 1 : -1;
|
|
3693
|
+
j = (k2 = xc.length) < (l = yc.length) ? k2 : l;
|
|
3694
|
+
for (i = 0; i < j; i++) if (xc[i] != yc[i]) return xc[i] > yc[i] ^ a ? 1 : -1;
|
|
3695
|
+
return k2 == l ? 0 : k2 > l ^ a ? 1 : -1;
|
|
3696
|
+
}
|
|
3697
|
+
function intCheck(n, min, max, name) {
|
|
3698
|
+
if (n < min || n > max || n !== mathfloor(n)) {
|
|
3699
|
+
throw Error(bignumberError + (name || "Argument") + (typeof n == "number" ? n < min || n > max ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(n));
|
|
3700
|
+
}
|
|
3701
|
+
}
|
|
3702
|
+
function isOdd(n) {
|
|
3703
|
+
var k2 = n.c.length - 1;
|
|
3704
|
+
return bitFloor(n.e / LOG_BASE) == k2 && n.c[k2] % 2 != 0;
|
|
3705
|
+
}
|
|
3706
|
+
function toExponential(str, e) {
|
|
3707
|
+
return (str.length > 1 ? str.charAt(0) + "." + str.slice(1) : str) + (e < 0 ? "e" : "e+") + e;
|
|
3708
|
+
}
|
|
3709
|
+
function toFixedPoint(str, e, z2) {
|
|
3710
|
+
var len, zs;
|
|
3711
|
+
if (e < 0) {
|
|
3712
|
+
for (zs = z2 + "."; ++e; zs += z2) ;
|
|
3713
|
+
str = zs + str;
|
|
3714
|
+
} else {
|
|
3715
|
+
len = str.length;
|
|
3716
|
+
if (++e > len) {
|
|
3717
|
+
for (zs = z2, e -= len; --e; zs += z2) ;
|
|
3718
|
+
str += zs;
|
|
3719
|
+
} else if (e < len) {
|
|
3720
|
+
str = str.slice(0, e) + "." + str.slice(e);
|
|
3721
|
+
}
|
|
3722
|
+
}
|
|
3723
|
+
return str;
|
|
3724
|
+
}
|
|
3725
|
+
var BigNumber = clone();
|
|
3726
|
+
var bignumber_default = BigNumber;
|
|
3727
|
+
var z = bcs.bytes(32).transform({
|
|
3728
|
+
// To change the input type, you need to provide a type definition for the input
|
|
3729
|
+
input: (e) => fromHex(e),
|
|
3730
|
+
output: (e) => toHex(e)
|
|
3731
|
+
});
|
|
3732
|
+
bcs.struct("IncentiveAPYInfo", {
|
|
3733
|
+
/** Asset identifier */
|
|
3734
|
+
asset_id: bcs.u8(),
|
|
3735
|
+
/** Annual Percentage Yield as a 256-bit integer */
|
|
3736
|
+
apy: bcs.u256(),
|
|
3737
|
+
/** List of supported coin types for this incentive */
|
|
3738
|
+
coin_types: bcs.vector(bcs.string())
|
|
3739
|
+
});
|
|
3740
|
+
var me = bcs.struct("IncentivePoolInfo", {
|
|
3741
|
+
/** Unique pool identifier */
|
|
3742
|
+
pool_id: z,
|
|
3743
|
+
/** Address holding the incentive funds */
|
|
3744
|
+
funds: z,
|
|
3745
|
+
/** Current phase of the incentive program */
|
|
3746
|
+
phase: bcs.u64(),
|
|
3747
|
+
/** Timestamp when the incentive started */
|
|
3748
|
+
start_at: bcs.u64(),
|
|
3749
|
+
/** Timestamp when the incentive ends */
|
|
3750
|
+
end_at: bcs.u64(),
|
|
3751
|
+
/** Timestamp when the incentive was closed */
|
|
3752
|
+
closed_at: bcs.u64(),
|
|
3753
|
+
/** Total supply of incentive tokens */
|
|
3754
|
+
total_supply: bcs.u64(),
|
|
3755
|
+
/** Asset identifier for the incentive */
|
|
3756
|
+
asset_id: bcs.u8(),
|
|
3757
|
+
/** Option type for the incentive */
|
|
3758
|
+
option: bcs.u8(),
|
|
3759
|
+
/** Factor used in incentive calculations */
|
|
3760
|
+
factor: bcs.u256(),
|
|
3761
|
+
/** Amount of incentives already distributed */
|
|
3762
|
+
distributed: bcs.u64(),
|
|
3763
|
+
/** Amount of incentives currently available */
|
|
3764
|
+
available: bcs.u256(),
|
|
3765
|
+
/** Total amount of incentives */
|
|
3766
|
+
total: bcs.u256()
|
|
3767
|
+
});
|
|
3768
|
+
bcs.struct("IncentivePoolInfoByPhase", {
|
|
3769
|
+
/** Phase number */
|
|
3770
|
+
phase: bcs.u64(),
|
|
3771
|
+
/** List of incentive pools in this phase */
|
|
3772
|
+
pools: bcs.vector(me)
|
|
3773
|
+
});
|
|
3774
|
+
bcs.struct("OracleInfo", {
|
|
3775
|
+
/** Oracle identifier */
|
|
3776
|
+
oracle_id: bcs.u8(),
|
|
3777
|
+
/** Current price as a 256-bit integer */
|
|
3778
|
+
price: bcs.u256(),
|
|
3779
|
+
/** Number of decimal places for the price */
|
|
3780
|
+
decimals: bcs.u8(),
|
|
3781
|
+
/** Whether the oracle data is valid */
|
|
3782
|
+
valid: bcs.bool()
|
|
3783
|
+
});
|
|
3784
|
+
bcs.struct("FlashLoanAssetConfig", {
|
|
3785
|
+
/** Unique identifier for the flash loan asset */
|
|
3786
|
+
id: bcs.string(),
|
|
3787
|
+
/** Asset identifier */
|
|
3788
|
+
asset_id: bcs.u8(),
|
|
3789
|
+
/** Coin type for the asset */
|
|
3790
|
+
coin_type: bcs.string(),
|
|
3791
|
+
/** Pool identifier for the flash loan */
|
|
3792
|
+
pool_id: bcs.string(),
|
|
3793
|
+
/** Rate paid to suppliers for flash loans */
|
|
3794
|
+
rate_to_supplier: bcs.u64(),
|
|
3795
|
+
/** Rate paid to treasury for flash loans */
|
|
3796
|
+
rate_to_treasury: bcs.u64(),
|
|
3797
|
+
/** Maximum flash loan amount */
|
|
3798
|
+
max: bcs.u64(),
|
|
3799
|
+
/** Minimum flash loan amount */
|
|
3800
|
+
min: bcs.u64()
|
|
3801
|
+
});
|
|
3802
|
+
bcs.struct("ReserveDataInfo", {
|
|
3803
|
+
/** Reserve identifier */
|
|
3804
|
+
id: bcs.u8(),
|
|
3805
|
+
/** Oracle identifier for price feeds */
|
|
3806
|
+
oracle_id: bcs.u8(),
|
|
3807
|
+
/** Coin type for the reserve */
|
|
3808
|
+
coin_type: bcs.string(),
|
|
3809
|
+
/** Maximum supply capacity */
|
|
3810
|
+
supply_cap: bcs.u256(),
|
|
3811
|
+
/** Maximum borrow capacity */
|
|
3812
|
+
borrow_cap: bcs.u256(),
|
|
3813
|
+
/** Current supply interest rate */
|
|
3814
|
+
supply_rate: bcs.u256(),
|
|
3815
|
+
/** Current borrow interest rate */
|
|
3816
|
+
borrow_rate: bcs.u256(),
|
|
3817
|
+
/** Current supply index for interest calculation */
|
|
3818
|
+
supply_index: bcs.u256(),
|
|
3819
|
+
/** Current borrow index for interest calculation */
|
|
3820
|
+
borrow_index: bcs.u256(),
|
|
3821
|
+
/** Total amount supplied to the reserve */
|
|
3822
|
+
total_supply: bcs.u256(),
|
|
3823
|
+
/** Total amount borrowed from the reserve */
|
|
3824
|
+
total_borrow: bcs.u256(),
|
|
3825
|
+
/** Timestamp of last update */
|
|
3826
|
+
last_update_at: bcs.u64(),
|
|
3827
|
+
/** Loan-to-Value ratio for collateral */
|
|
3828
|
+
ltv: bcs.u256(),
|
|
3829
|
+
/** Treasury factor for fee calculations */
|
|
3830
|
+
treasury_factor: bcs.u256(),
|
|
3831
|
+
/** Current treasury balance */
|
|
3832
|
+
treasury_balance: bcs.u256(),
|
|
3833
|
+
/** Base interest rate */
|
|
3834
|
+
base_rate: bcs.u256(),
|
|
3835
|
+
/** Interest rate multiplier */
|
|
3836
|
+
multiplier: bcs.u256(),
|
|
3837
|
+
/** Jump rate multiplier for high utilization */
|
|
3838
|
+
jump_rate_multiplier: bcs.u256(),
|
|
3839
|
+
/** Reserve factor for protocol fees */
|
|
3840
|
+
reserve_factor: bcs.u256(),
|
|
3841
|
+
/** Optimal utilization rate */
|
|
3842
|
+
optimal_utilization: bcs.u256(),
|
|
3843
|
+
/** Liquidation ratio threshold */
|
|
3844
|
+
liquidation_ratio: bcs.u256(),
|
|
3845
|
+
/** Liquidation bonus for liquidators */
|
|
3846
|
+
liquidation_bonus: bcs.u256(),
|
|
3847
|
+
/** Liquidation threshold */
|
|
3848
|
+
liquidation_threshold: bcs.u256()
|
|
3849
|
+
});
|
|
3850
|
+
var fe = bcs.struct("UserStateInfo", {
|
|
3851
|
+
/** Asset identifier */
|
|
3852
|
+
asset_id: bcs.u8(),
|
|
3853
|
+
/** User's current borrow balance */
|
|
3854
|
+
borrow_balance: bcs.u256(),
|
|
3855
|
+
/** User's current supply balance */
|
|
3856
|
+
supply_balance: bcs.u256()
|
|
3857
|
+
});
|
|
3858
|
+
var ye = "1.4.0";
|
|
3859
|
+
var V = {
|
|
3860
|
+
version: ye
|
|
3861
|
+
};
|
|
3862
|
+
var ge = V.version;
|
|
3863
|
+
var he = () => {
|
|
3864
|
+
if (typeof process < "u" && process.versions && process.versions.node)
|
|
3865
|
+
try {
|
|
3866
|
+
const e = __require("os"), n = process.version, o = e.type(), r = e.arch();
|
|
3867
|
+
return `Node.js ${n.startsWith("v") ? n.substring(1) : n}; ${o}/${r}`;
|
|
3868
|
+
} catch {
|
|
3869
|
+
return `Node.js ${process.version}; OS/Unknown (Error)`;
|
|
3870
|
+
}
|
|
3871
|
+
return "Node/Unknown";
|
|
3872
|
+
};
|
|
3873
|
+
var ve = () => {
|
|
3874
|
+
let e = "";
|
|
3875
|
+
return typeof process < "u" && process.versions && process.versions.node && (e = `lending/${ge} (${he()})`), e;
|
|
3876
|
+
};
|
|
3877
|
+
var K = ve();
|
|
3878
|
+
var P = new jsonRpc.SuiJsonRpcClient({
|
|
3879
|
+
url: jsonRpc.getJsonRpcFullnodeUrl("mainnet")
|
|
3880
|
+
});
|
|
3881
|
+
function Q(e) {
|
|
3882
|
+
const n = [];
|
|
3883
|
+
return e.forEach((o, r) => {
|
|
3884
|
+
const t = r === e.length - 1;
|
|
3885
|
+
if (typeof o == "object" && o !== null && t) {
|
|
3886
|
+
const { client: a, disableCache: s, cacheTime: i, ...c } = o;
|
|
3887
|
+
n.push(c);
|
|
3888
|
+
} else
|
|
3889
|
+
n.push(o);
|
|
3890
|
+
}), JSON.stringify(n);
|
|
3891
|
+
}
|
|
3892
|
+
function B(e) {
|
|
3893
|
+
const n = {};
|
|
3894
|
+
return (...o) => {
|
|
3895
|
+
const r = Q(o);
|
|
3896
|
+
return n[r] || (n[r] = e(...o).finally(() => {
|
|
3897
|
+
delete n[r];
|
|
3898
|
+
})), n[r];
|
|
3899
|
+
};
|
|
3900
|
+
}
|
|
3901
|
+
function $(e) {
|
|
3902
|
+
let n = {};
|
|
3903
|
+
return (...o) => {
|
|
3904
|
+
const r = o[o.length - 1], t = Q(o), a = n[t];
|
|
3905
|
+
return !r?.disableCache && typeof a?.data < "u" && (typeof r?.cacheTime > "u" || r.cacheTime > Date.now() - a.cacheAt) ? a.data : e(...o).then((s) => (n[t] = {
|
|
3906
|
+
data: s,
|
|
3907
|
+
cacheAt: Date.now()
|
|
3908
|
+
}, s));
|
|
3909
|
+
};
|
|
3910
|
+
}
|
|
3911
|
+
function g(e, n) {
|
|
3912
|
+
return typeof e == "object" ? e : n(e);
|
|
3913
|
+
}
|
|
3914
|
+
function we(e, n) {
|
|
3915
|
+
return typeof n == "string" ? e.object(n) : typeof n == "object" && n.$kind ? n : e.object(n.contract.pool);
|
|
3916
|
+
}
|
|
3917
|
+
function L(e, n, o) {
|
|
3918
|
+
if (e.results && e.results.length > 0) {
|
|
3919
|
+
if (e.results[0].returnValues && e.results[0].returnValues.length > 0)
|
|
3920
|
+
return e.results[0].returnValues.map((r, t) => (n[t] || n[0]).parse(Uint8Array.from(r[0])));
|
|
3921
|
+
} else if (e.error)
|
|
3922
|
+
return console.log(`Get an error, msg: ${e.error}`), [];
|
|
3923
|
+
return [];
|
|
3924
|
+
}
|
|
3925
|
+
function b(e) {
|
|
3926
|
+
return utils.normalizeStructTag(e);
|
|
3927
|
+
}
|
|
3928
|
+
function X(e) {
|
|
3929
|
+
const n = (e || 0) / Math.pow(10, 27);
|
|
3930
|
+
return n > Math.pow(10, 5) ? 1 / 0 : n;
|
|
3931
|
+
}
|
|
3932
|
+
new pythSuiJs.SuiPriceServiceConnection("https://hermes.pyth.network", {
|
|
3933
|
+
timeout: 2e4
|
|
3934
|
+
});
|
|
3935
|
+
var be = 27;
|
|
3936
|
+
var Y = (e, n) => {
|
|
3937
|
+
if (!Number(e) || !Number(n)) return new bignumber_default(0);
|
|
3938
|
+
const o = new bignumber_default(1).shiftedBy(1 * be), r = o.multipliedBy(new bignumber_default(0.5));
|
|
3939
|
+
return new bignumber_default(e).multipliedBy(new bignumber_default(n)).plus(r).dividedBy(o).integerValue(bignumber_default.ROUND_DOWN);
|
|
3940
|
+
};
|
|
3941
|
+
var E = K ? {
|
|
3942
|
+
"User-Agent": K
|
|
3943
|
+
} : {};
|
|
3944
|
+
function H(e, n = "uniqueId") {
|
|
3945
|
+
return e.reduce(
|
|
3946
|
+
(o, r) => (o[r[n]] = r, o),
|
|
3947
|
+
{}
|
|
3948
|
+
);
|
|
3949
|
+
}
|
|
3950
|
+
function q(e, n) {
|
|
3951
|
+
const o = e.emodes.find((t) => t.emodeId === n.emodeId);
|
|
3952
|
+
if (!o)
|
|
3953
|
+
throw new Error("EMode not found in pool");
|
|
3954
|
+
const r = o.assets.find((t) => t.assetId === e.id);
|
|
3955
|
+
return {
|
|
3956
|
+
...e,
|
|
3957
|
+
emode: {
|
|
3958
|
+
...r,
|
|
3959
|
+
emodeId: o.emodeId
|
|
3960
|
+
},
|
|
3961
|
+
isEMode: true
|
|
3962
|
+
};
|
|
3963
|
+
}
|
|
3964
|
+
function ke(e) {
|
|
3965
|
+
const [n, o] = e.split("-");
|
|
3966
|
+
return !n || !o ? null : {
|
|
3967
|
+
marketKey: n,
|
|
3968
|
+
poolId: parseInt(o)
|
|
3969
|
+
};
|
|
3970
|
+
}
|
|
3971
|
+
function T() {
|
|
3972
|
+
return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
3973
|
+
}
|
|
3974
|
+
var R = $(
|
|
3975
|
+
B(
|
|
3976
|
+
async (e) => {
|
|
3977
|
+
const n = (e?.markets || [M.main]).map((t) => _(t)), o = `https://open-api.naviprotocol.io/api/navi/pools?env=${e?.env || "prod"}&sdk=${V.version}&market=${n.map(
|
|
3978
|
+
(t) => t.key
|
|
3979
|
+
)}`, r = await fetch(o, { headers: E }).then((t) => t.json());
|
|
3980
|
+
return r.data.forEach((t) => {
|
|
3981
|
+
const s = r.meta.emodes.filter((h) => {
|
|
3982
|
+
const j = _(h.marketId);
|
|
3983
|
+
return t.market === j.key && h.isActive;
|
|
3984
|
+
}).filter((h) => !!h.assets.find((j) => j.assetId === t.id));
|
|
3985
|
+
t.emodes = s;
|
|
3986
|
+
const i = bignumber_default(t.totalSupplyAmount).div(Math.pow(10, 9)).decimalPlaces(t.token.decimals, bignumber_default.ROUND_DOWN).toString(), c = bignumber_default(t.borrowedAmount).shiftedBy(-9).decimalPlaces(t.token.decimals, bignumber_default.ROUND_DOWN).toString(), u = bignumber_default(i).multipliedBy(t.oracle.price).toString(), d = bignumber_default(c).multipliedBy(t.oracle.price).toString(), f = bignumber_default(t.supplyCapCeiling).shiftedBy(-27).decimalPlaces(t.token.decimals, bignumber_default.ROUND_DOWN).toString(), v = bignumber_default.max(
|
|
3987
|
+
bignumber_default(t.borrowedAmount),
|
|
3988
|
+
bignumber_default(t.validBorrowAmount)
|
|
3989
|
+
).shiftedBy(-9).decimalPlaces(t.token.decimals, bignumber_default.ROUND_DOWN).toString(), y = bignumber_default(f).multipliedBy(t.oracle.price).toString(), l = bignumber_default(v).multipliedBy(t.oracle.price).toString();
|
|
3990
|
+
t.poolSupplyAmount = i, t.poolBorrowAmount = c, t.poolSupplyValue = u, t.poolBorrowValue = d, t.poolSupplyCapAmount = f, t.poolBorrowCapAmount = v, t.poolSupplyCapValue = y, t.poolBorrowCapValue = l;
|
|
3991
|
+
}), r.data;
|
|
3992
|
+
}
|
|
3993
|
+
)
|
|
3994
|
+
);
|
|
3995
|
+
async function A(e, n) {
|
|
3996
|
+
let o = n?.market;
|
|
3997
|
+
if (typeof e == "string") {
|
|
3998
|
+
const a = ke(e);
|
|
3999
|
+
a && (o = a.marketKey, e = a.poolId);
|
|
4000
|
+
}
|
|
4001
|
+
const r = await R({
|
|
4002
|
+
...n,
|
|
4003
|
+
markets: [o || N],
|
|
4004
|
+
cacheTime: w
|
|
4005
|
+
});
|
|
4006
|
+
if (typeof e == "object")
|
|
4007
|
+
return e;
|
|
4008
|
+
const t = r.find((a) => typeof e == "string" ? b(a.suiCoinType) === b(e) : typeof e == "number" ? a.id === e : false);
|
|
4009
|
+
if (!t)
|
|
4010
|
+
throw new Error("Pool not found");
|
|
4011
|
+
return t.isDeprecated && console.log(`The lending pool for coinType ${t.suiCoinType} is going to be deprecated.`), t;
|
|
4012
|
+
}
|
|
4013
|
+
async function Ce(e, n, o, r) {
|
|
4014
|
+
const t = await k({
|
|
4015
|
+
...r,
|
|
4016
|
+
cacheTime: w
|
|
4017
|
+
}), a = await A(n, r);
|
|
4018
|
+
if (a?.deprecatedAt && Date.now() > a.deprecatedAt)
|
|
4019
|
+
throw new Error(`The lending pool for coinType ${a.suiCoinType} has been deprecated.`);
|
|
4020
|
+
const s = typeof o == "object" && o.$kind === "GasCoin";
|
|
4021
|
+
if (b(a.suiCoinType) === b("0x2::sui::SUI") && s) {
|
|
4022
|
+
if (!r?.amount)
|
|
4023
|
+
throw new Error("Amount is required for sui coin");
|
|
4024
|
+
o = e.splitCoins(o, [r.amount]);
|
|
4025
|
+
}
|
|
4026
|
+
let i;
|
|
4027
|
+
return typeof r?.amount < "u" ? i = g(r.amount, e.pure.u64) : i = e.moveCall({
|
|
4028
|
+
target: "0x2::coin::value",
|
|
4029
|
+
arguments: [g(o, e.object)],
|
|
4030
|
+
typeArguments: [a.suiCoinType]
|
|
4031
|
+
}), r?.accountCap ? e.moveCall({
|
|
4032
|
+
target: `${t.package}::incentive_v3::deposit_with_account_cap`,
|
|
4033
|
+
arguments: [
|
|
4034
|
+
e.object("0x06"),
|
|
4035
|
+
e.object(t.storage),
|
|
4036
|
+
e.object(a.contract.pool),
|
|
4037
|
+
e.pure.u8(a.id),
|
|
4038
|
+
g(o, e.object),
|
|
4039
|
+
e.object(t.incentiveV2),
|
|
4040
|
+
e.object(t.incentiveV3),
|
|
4041
|
+
g(r.accountCap, e.object)
|
|
4042
|
+
],
|
|
4043
|
+
typeArguments: [a.suiCoinType]
|
|
4044
|
+
}) : e.moveCall({
|
|
4045
|
+
target: `${t.package}::incentive_v3::entry_deposit`,
|
|
4046
|
+
arguments: [
|
|
4047
|
+
e.object("0x06"),
|
|
4048
|
+
e.object(t.storage),
|
|
4049
|
+
e.object(a.contract.pool),
|
|
4050
|
+
e.pure.u8(a.id),
|
|
4051
|
+
g(o, e.object),
|
|
4052
|
+
i,
|
|
4053
|
+
e.object(t.incentiveV2),
|
|
4054
|
+
e.object(t.incentiveV3)
|
|
4055
|
+
],
|
|
4056
|
+
typeArguments: [a.suiCoinType]
|
|
4057
|
+
}), t.version === 2 && a.id === 0 && (!r?.env || r?.env === "prod") && e.moveCall({
|
|
4058
|
+
target: `${t.package}::pool::refresh_stake`,
|
|
4059
|
+
arguments: [e.object(a.contract.pool), e.object("0x05")]
|
|
4060
|
+
}), e;
|
|
4061
|
+
}
|
|
4062
|
+
async function Qe(e, n, o, r) {
|
|
4063
|
+
const t = await k({
|
|
4064
|
+
...r,
|
|
4065
|
+
cacheTime: w
|
|
4066
|
+
}), a = await A(n, r), s = g(o, e.pure.u64);
|
|
4067
|
+
let i;
|
|
4068
|
+
if (t.version === 1)
|
|
4069
|
+
if (r?.accountCap) {
|
|
4070
|
+
const [u] = e.moveCall({
|
|
4071
|
+
target: `${t.package}::incentive_v3::withdraw_with_account_cap`,
|
|
4072
|
+
arguments: [
|
|
4073
|
+
e.object("0x06"),
|
|
4074
|
+
e.object(t.priceOracle),
|
|
4075
|
+
e.object(t.storage),
|
|
4076
|
+
e.object(a.contract.pool),
|
|
4077
|
+
e.pure.u8(a.id),
|
|
4078
|
+
s,
|
|
4079
|
+
e.object(t.incentiveV2),
|
|
4080
|
+
e.object(t.incentiveV3),
|
|
4081
|
+
g(r.accountCap, e.object)
|
|
4082
|
+
],
|
|
4083
|
+
typeArguments: [a.suiCoinType]
|
|
4084
|
+
});
|
|
4085
|
+
i = u;
|
|
4086
|
+
} else {
|
|
4087
|
+
const [u] = e.moveCall({
|
|
4088
|
+
target: `${t.package}::incentive_v3::withdraw`,
|
|
4089
|
+
arguments: [
|
|
4090
|
+
e.object("0x06"),
|
|
4091
|
+
e.object(t.priceOracle),
|
|
4092
|
+
e.object(t.storage),
|
|
4093
|
+
e.object(a.contract.pool),
|
|
4094
|
+
e.pure.u8(a.id),
|
|
4095
|
+
s,
|
|
4096
|
+
e.object(t.incentiveV2),
|
|
4097
|
+
e.object(t.incentiveV3)
|
|
4098
|
+
],
|
|
4099
|
+
typeArguments: [a.suiCoinType]
|
|
4100
|
+
});
|
|
4101
|
+
i = u;
|
|
4102
|
+
}
|
|
4103
|
+
else if (r?.accountCap) {
|
|
4104
|
+
const [u] = e.moveCall({
|
|
4105
|
+
target: `${t.package}::incentive_v3::withdraw_with_account_cap_v2`,
|
|
4106
|
+
arguments: [
|
|
4107
|
+
e.object("0x06"),
|
|
4108
|
+
e.object(t.priceOracle),
|
|
4109
|
+
e.object(t.storage),
|
|
4110
|
+
e.object(a.contract.pool),
|
|
4111
|
+
e.pure.u8(a.id),
|
|
4112
|
+
s,
|
|
4113
|
+
e.object(t.incentiveV2),
|
|
4114
|
+
e.object(t.incentiveV3),
|
|
4115
|
+
g(r.accountCap, e.object),
|
|
4116
|
+
e.object("0x05")
|
|
4117
|
+
],
|
|
4118
|
+
typeArguments: [a.suiCoinType]
|
|
4119
|
+
});
|
|
4120
|
+
i = u;
|
|
4121
|
+
} else {
|
|
4122
|
+
const [u] = e.moveCall({
|
|
4123
|
+
target: `${t.package}::incentive_v3::withdraw_v2`,
|
|
4124
|
+
arguments: [
|
|
4125
|
+
e.object("0x06"),
|
|
4126
|
+
e.object(t.priceOracle),
|
|
4127
|
+
e.object(t.storage),
|
|
4128
|
+
e.object(a.contract.pool),
|
|
4129
|
+
e.pure.u8(a.id),
|
|
4130
|
+
s,
|
|
4131
|
+
e.object(t.incentiveV2),
|
|
4132
|
+
e.object(t.incentiveV3),
|
|
4133
|
+
e.object("0x05")
|
|
4134
|
+
],
|
|
4135
|
+
typeArguments: [a.suiCoinType]
|
|
4136
|
+
});
|
|
4137
|
+
i = u;
|
|
4138
|
+
}
|
|
4139
|
+
return e.moveCall({
|
|
4140
|
+
target: "0x2::coin::from_balance",
|
|
4141
|
+
arguments: [i],
|
|
4142
|
+
typeArguments: [a.suiCoinType]
|
|
4143
|
+
});
|
|
4144
|
+
}
|
|
4145
|
+
async function Xe(e, n, o, r) {
|
|
4146
|
+
const t = await k({
|
|
4147
|
+
...r,
|
|
4148
|
+
cacheTime: w
|
|
4149
|
+
}), a = await A(n, r);
|
|
4150
|
+
if (a?.deprecatedAt && Date.now() > a.deprecatedAt)
|
|
4151
|
+
throw new Error(`The lending pool for coinType ${a.suiCoinType} has been deprecated.`);
|
|
4152
|
+
const s = g(o, e.pure.u64);
|
|
4153
|
+
let i;
|
|
4154
|
+
if (t.version === 1)
|
|
4155
|
+
if (r?.accountCap) {
|
|
4156
|
+
const [u] = e.moveCall({
|
|
4157
|
+
target: `${t.package}::incentive_v3::borrow_with_account_cap`,
|
|
4158
|
+
arguments: [
|
|
4159
|
+
e.object("0x06"),
|
|
4160
|
+
e.object(t.priceOracle),
|
|
4161
|
+
e.object(t.storage),
|
|
4162
|
+
e.object(a.contract.pool),
|
|
4163
|
+
e.pure.u8(a.id),
|
|
4164
|
+
s,
|
|
4165
|
+
e.object(t.incentiveV2),
|
|
4166
|
+
e.object(t.incentiveV3),
|
|
4167
|
+
g(r.accountCap, e.object)
|
|
4168
|
+
],
|
|
4169
|
+
typeArguments: [a.suiCoinType]
|
|
4170
|
+
});
|
|
4171
|
+
i = u;
|
|
4172
|
+
} else {
|
|
4173
|
+
const [u] = e.moveCall({
|
|
4174
|
+
target: `${t.package}::incentive_v3::borrow`,
|
|
4175
|
+
arguments: [
|
|
4176
|
+
e.object("0x06"),
|
|
4177
|
+
e.object(t.priceOracle),
|
|
4178
|
+
e.object(t.storage),
|
|
4179
|
+
e.object(a.contract.pool),
|
|
4180
|
+
e.pure.u8(a.id),
|
|
4181
|
+
s,
|
|
4182
|
+
e.object(t.incentiveV2),
|
|
4183
|
+
e.object(t.incentiveV3)
|
|
4184
|
+
],
|
|
4185
|
+
typeArguments: [a.suiCoinType]
|
|
4186
|
+
});
|
|
4187
|
+
i = u;
|
|
4188
|
+
}
|
|
4189
|
+
else if (r?.accountCap) {
|
|
4190
|
+
const [u] = e.moveCall({
|
|
4191
|
+
target: `${t.package}::incentive_v3::borrow_with_account_cap_v2`,
|
|
4192
|
+
arguments: [
|
|
4193
|
+
e.object("0x06"),
|
|
4194
|
+
e.object(t.priceOracle),
|
|
4195
|
+
e.object(t.storage),
|
|
4196
|
+
e.object(a.contract.pool),
|
|
4197
|
+
e.pure.u8(a.id),
|
|
4198
|
+
s,
|
|
4199
|
+
e.object(t.incentiveV2),
|
|
4200
|
+
e.object(t.incentiveV3),
|
|
4201
|
+
g(r.accountCap, e.object),
|
|
4202
|
+
e.object("0x05")
|
|
4203
|
+
],
|
|
4204
|
+
typeArguments: [a.suiCoinType]
|
|
4205
|
+
});
|
|
4206
|
+
i = u;
|
|
4207
|
+
} else {
|
|
4208
|
+
const [u] = e.moveCall({
|
|
4209
|
+
target: `${t.package}::incentive_v3::borrow_v2`,
|
|
4210
|
+
arguments: [
|
|
4211
|
+
e.object("0x06"),
|
|
4212
|
+
e.object(t.priceOracle),
|
|
4213
|
+
e.object(t.storage),
|
|
4214
|
+
e.object(a.contract.pool),
|
|
4215
|
+
e.pure.u8(a.id),
|
|
4216
|
+
s,
|
|
4217
|
+
e.object(t.incentiveV2),
|
|
4218
|
+
e.object(t.incentiveV3),
|
|
4219
|
+
e.object("0x05")
|
|
4220
|
+
],
|
|
4221
|
+
typeArguments: [a.suiCoinType]
|
|
4222
|
+
});
|
|
4223
|
+
i = u;
|
|
4224
|
+
}
|
|
4225
|
+
return e.moveCall({
|
|
4226
|
+
target: "0x2::coin::from_balance",
|
|
4227
|
+
arguments: [e.object(i)],
|
|
4228
|
+
typeArguments: [a.suiCoinType]
|
|
4229
|
+
});
|
|
4230
|
+
}
|
|
4231
|
+
async function xe(e, n, o, r) {
|
|
4232
|
+
const t = await k({
|
|
4233
|
+
...r,
|
|
4234
|
+
cacheTime: w
|
|
4235
|
+
}), a = await A(n, r), s = typeof o == "object" && o.$kind === "GasCoin";
|
|
4236
|
+
if (b(a.suiCoinType) === b("0x2::sui::SUI") && s) {
|
|
4237
|
+
if (!r?.amount)
|
|
4238
|
+
throw new Error("Amount is required for sui coin");
|
|
4239
|
+
o = e.splitCoins(o, [r.amount]);
|
|
4240
|
+
}
|
|
4241
|
+
let i;
|
|
4242
|
+
if (typeof r?.amount < "u" ? i = g(r.amount, e.pure.u64) : i = e.moveCall({
|
|
4243
|
+
target: "0x2::coin::value",
|
|
4244
|
+
arguments: [g(o, e.object)],
|
|
4245
|
+
typeArguments: [a.suiCoinType]
|
|
4246
|
+
}), r?.accountCap) {
|
|
4247
|
+
const [c] = e.moveCall({
|
|
4248
|
+
target: `${t.package}::incentive_v3::repay_with_account_cap`,
|
|
4249
|
+
arguments: [
|
|
4250
|
+
e.object("0x06"),
|
|
4251
|
+
e.object(t.priceOracle),
|
|
4252
|
+
e.object(t.storage),
|
|
4253
|
+
e.object(a.contract.pool),
|
|
4254
|
+
e.pure.u8(a.id),
|
|
4255
|
+
g(o, e.object),
|
|
4256
|
+
e.object(t.incentiveV2),
|
|
4257
|
+
e.object(t.incentiveV3),
|
|
4258
|
+
g(r.accountCap, e.object)
|
|
4259
|
+
],
|
|
4260
|
+
typeArguments: [a.suiCoinType]
|
|
4261
|
+
});
|
|
4262
|
+
return e.moveCall({
|
|
4263
|
+
target: "0x2::coin::from_balance",
|
|
4264
|
+
arguments: [c],
|
|
4265
|
+
typeArguments: [a.suiCoinType]
|
|
4266
|
+
});
|
|
4267
|
+
} else
|
|
4268
|
+
return e.moveCall({
|
|
4269
|
+
target: `${t.package}::incentive_v3::entry_repay`,
|
|
4270
|
+
arguments: [
|
|
4271
|
+
e.object("0x06"),
|
|
4272
|
+
e.object(t.priceOracle),
|
|
4273
|
+
e.object(t.storage),
|
|
4274
|
+
e.object(a.contract.pool),
|
|
4275
|
+
e.pure.u8(a.id),
|
|
4276
|
+
g(o, e.object),
|
|
4277
|
+
i,
|
|
4278
|
+
e.object(t.incentiveV2),
|
|
4279
|
+
e.object(t.incentiveV3)
|
|
4280
|
+
],
|
|
4281
|
+
typeArguments: [a.suiCoinType]
|
|
4282
|
+
}), e;
|
|
4283
|
+
}
|
|
4284
|
+
var N = "main";
|
|
4285
|
+
var M = {
|
|
4286
|
+
main: {
|
|
4287
|
+
id: 0,
|
|
4288
|
+
key: "main",
|
|
4289
|
+
name: "Main Market"
|
|
4290
|
+
}
|
|
4291
|
+
};
|
|
4292
|
+
var _ = (e) => {
|
|
4293
|
+
const o = Object.values(M).find((r) => typeof e == "number" ? r.id === e : typeof e == "string" ? r.key === e : r.id === e.id);
|
|
4294
|
+
if (!o)
|
|
4295
|
+
throw new Error("Market not found");
|
|
4296
|
+
return o;
|
|
4297
|
+
};
|
|
4298
|
+
var k = $(
|
|
4299
|
+
B(
|
|
4300
|
+
async (e) => {
|
|
4301
|
+
const n = _(e?.market || N), o = `https://open-api.naviprotocol.io/api/navi/config?env=${e?.env || "prod"}&sdk=${V.version}&market=${n.key}`;
|
|
4302
|
+
return (await fetch(o, { headers: E }).then((t) => t.json())).data;
|
|
4303
|
+
}
|
|
4304
|
+
)
|
|
4305
|
+
);
|
|
4306
|
+
var w = 1e3 * 60 * 5;
|
|
4307
|
+
var x = $(
|
|
4308
|
+
B(
|
|
4309
|
+
async (e, n) => {
|
|
4310
|
+
const o = await k({
|
|
4311
|
+
cacheTime: w,
|
|
4312
|
+
...n
|
|
4313
|
+
}), r = new transactions.Transaction(), t = n?.client ?? P;
|
|
4314
|
+
r.moveCall({
|
|
4315
|
+
target: `${o.emode.contract.registryPackage}::registry::find_user_emode_account_caps`,
|
|
4316
|
+
arguments: [r.object(o.emode.contract.registryObject), r.pure.address(e)]
|
|
4317
|
+
});
|
|
4318
|
+
const s = (await t.devInspectTransactionBlock({
|
|
4319
|
+
transactionBlock: r,
|
|
4320
|
+
sender: e
|
|
4321
|
+
})).results[0].returnValues, i = bcs$1.bcs.vector(bcs$1.bcs.u64()).parse(Uint8Array.from(s[0][0])), c = bcs$1.bcs.vector(bcs$1.bcs.u64()).parse(Uint8Array.from(s[1][0])), u = bcs$1.bcs.vector(bcs$1.bcs.Address).parse(Uint8Array.from(s[2][0]));
|
|
4322
|
+
return i.map((d, f) => ({
|
|
4323
|
+
marketId: Number(d),
|
|
4324
|
+
emodeId: Number(c[f]),
|
|
4325
|
+
accountCap: u[f].toString()
|
|
4326
|
+
}));
|
|
4327
|
+
}
|
|
4328
|
+
)
|
|
4329
|
+
);
|
|
4330
|
+
async function ee(e, n, o, r, t, a, s) {
|
|
4331
|
+
const i = await k({
|
|
4332
|
+
...s,
|
|
4333
|
+
cacheTime: w
|
|
4334
|
+
}), c = await A(o, s);
|
|
4335
|
+
return e.moveCall({
|
|
4336
|
+
target: `${i.uiGetter}::calculator_unchecked::dynamic_health_factor`,
|
|
4337
|
+
arguments: [
|
|
4338
|
+
e.object("0x06"),
|
|
4339
|
+
e.object(i.storage),
|
|
4340
|
+
e.object(i.oracle.priceOracle),
|
|
4341
|
+
we(e, c),
|
|
4342
|
+
g(n, e.pure.address),
|
|
4343
|
+
g(c.id, e.pure.u8),
|
|
4344
|
+
g(r, e.pure.u64),
|
|
4345
|
+
g(t, e.pure.u64),
|
|
4346
|
+
g(a, e.pure.bool)
|
|
4347
|
+
],
|
|
4348
|
+
typeArguments: [c.suiCoinType]
|
|
4349
|
+
});
|
|
4350
|
+
}
|
|
4351
|
+
async function Ae(e, n, o) {
|
|
4352
|
+
return ee(e, n, 0, 0, 0, false, o);
|
|
4353
|
+
}
|
|
4354
|
+
async function te(e, n, o) {
|
|
4355
|
+
const r = new transactions.Transaction(), t = o?.client ?? P, a = await R({
|
|
4356
|
+
...o,
|
|
4357
|
+
markets: Object.values(M)
|
|
4358
|
+
}), s = H(a);
|
|
4359
|
+
for (let d of n) {
|
|
4360
|
+
const f = await k({
|
|
4361
|
+
...o,
|
|
4362
|
+
cacheTime: w,
|
|
4363
|
+
market: d.market
|
|
4364
|
+
});
|
|
4365
|
+
r.moveCall({
|
|
4366
|
+
target: `${f.uiGetter}::getter_unchecked::get_user_state`,
|
|
4367
|
+
arguments: [r.object(f.storage), r.pure.address(d.address)]
|
|
4368
|
+
});
|
|
4369
|
+
}
|
|
4370
|
+
const c = ((await t.devInspectTransactionBlock({
|
|
4371
|
+
transactionBlock: r,
|
|
4372
|
+
sender: e
|
|
4373
|
+
})).results || []).map((d) => d.returnValues?.map((f) => bcs$1.bcs.vector(fe).parse(Uint8Array.from(f[0])))[0] || []), u = [];
|
|
4374
|
+
return c.forEach((d, f) => {
|
|
4375
|
+
const v = n[f], y = _(v.market);
|
|
4376
|
+
d.forEach((l) => {
|
|
4377
|
+
if (l.supply_balance === "0" && l.borrow_balance === "0" && (v.emodeId === void 0 || !o?.includeZeroBalanceEmodePositions))
|
|
4378
|
+
return;
|
|
4379
|
+
const h = s[`${y.key}-${l.asset_id}`];
|
|
4380
|
+
if (!h)
|
|
4381
|
+
return;
|
|
4382
|
+
const j = Y(
|
|
4383
|
+
l.supply_balance,
|
|
4384
|
+
h.currentSupplyIndex
|
|
4385
|
+
).toString(), I = Y(
|
|
4386
|
+
l.borrow_balance,
|
|
4387
|
+
h.currentBorrowIndex
|
|
4388
|
+
).toString();
|
|
4389
|
+
u.push({
|
|
4390
|
+
supplyBalance: j,
|
|
4391
|
+
borrowBalance: I,
|
|
4392
|
+
assetId: l.asset_id,
|
|
4393
|
+
market: y.key,
|
|
4394
|
+
pool: h,
|
|
4395
|
+
emodeId: v.emodeId
|
|
4396
|
+
});
|
|
4397
|
+
});
|
|
4398
|
+
}), u;
|
|
4399
|
+
}
|
|
4400
|
+
async function ct(e, n) {
|
|
4401
|
+
const o = n?.client ?? P, r = new transactions.Transaction();
|
|
4402
|
+
await Ae(r, e, n);
|
|
4403
|
+
const t = await o.devInspectTransactionBlock({
|
|
4404
|
+
transactionBlock: r,
|
|
4405
|
+
sender: e
|
|
4406
|
+
}), a = L(t, [bcs$1.bcs.u256()]);
|
|
4407
|
+
return X(Number(a[0]) || 0);
|
|
4408
|
+
}
|
|
4409
|
+
var $e = $(
|
|
4410
|
+
async (e, n) => {
|
|
4411
|
+
const o = [], r = (n?.markets || Object.keys(M)).map((i) => _(i));
|
|
4412
|
+
let t = [];
|
|
4413
|
+
try {
|
|
4414
|
+
t = await x(e, n);
|
|
4415
|
+
} catch (i) {
|
|
4416
|
+
console.error(i);
|
|
4417
|
+
}
|
|
4418
|
+
const a = r.map((i) => ({
|
|
4419
|
+
address: e,
|
|
4420
|
+
market: i.key
|
|
4421
|
+
})).concat(
|
|
4422
|
+
t.filter((i) => !!r.find((c) => c.id === i.marketId)).map((i) => ({
|
|
4423
|
+
address: i.accountCap,
|
|
4424
|
+
market: _(i.marketId).key,
|
|
4425
|
+
emodeId: i.emodeId
|
|
4426
|
+
}))
|
|
4427
|
+
);
|
|
4428
|
+
return (await te(e, a, n)).forEach((i) => {
|
|
4429
|
+
const c = typeof i.emodeId == "number" ? t.find((u) => {
|
|
4430
|
+
const d = _(i.market);
|
|
4431
|
+
return u.emodeId === i.emodeId && u.marketId === d.id;
|
|
4432
|
+
}) : void 0;
|
|
4433
|
+
if (c) {
|
|
4434
|
+
if (!i.pool.emodes.find((d) => d.emodeId === c.emodeId))
|
|
4435
|
+
return;
|
|
4436
|
+
if (bignumber_default(i.supplyBalance).gte(0)) {
|
|
4437
|
+
const d = bignumber_default(i.supplyBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, bignumber_default.ROUND_DOWN), f = q(i.pool, c);
|
|
4438
|
+
if (d.gt(0) || f.emode.isCollateral)
|
|
4439
|
+
try {
|
|
4440
|
+
o.push({
|
|
4441
|
+
id: `${i.pool.uniqueId}_${c.emodeId}_navi-lending-emode-supply-${T()}`,
|
|
4442
|
+
wallet: e,
|
|
4443
|
+
protocol: "navi",
|
|
4444
|
+
market: i.market,
|
|
4445
|
+
type: "navi-lending-emode-supply",
|
|
4446
|
+
"navi-lending-emode-supply": {
|
|
4447
|
+
amount: d.toString(),
|
|
4448
|
+
pool: q(i.pool, c),
|
|
4449
|
+
token: i.pool.token,
|
|
4450
|
+
valueUSD: d.multipliedBy(i.pool.oracle.price).toString(),
|
|
4451
|
+
emodeCap: c
|
|
4452
|
+
}
|
|
4453
|
+
});
|
|
4454
|
+
} catch (v) {
|
|
4455
|
+
console.error(v);
|
|
4456
|
+
}
|
|
4457
|
+
}
|
|
4458
|
+
if (bignumber_default(i.borrowBalance).gte(0)) {
|
|
4459
|
+
const d = bignumber_default(i.borrowBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, bignumber_default.ROUND_DOWN), f = q(i.pool, c);
|
|
4460
|
+
if (d.gt(0) || f.emode.isDebt)
|
|
4461
|
+
try {
|
|
4462
|
+
o.push({
|
|
4463
|
+
id: `${i.pool.uniqueId}_${c.emodeId}_navi-lending-emode-borrow-${T()}`,
|
|
4464
|
+
wallet: e,
|
|
4465
|
+
protocol: "navi",
|
|
4466
|
+
market: i.market,
|
|
4467
|
+
type: "navi-lending-emode-borrow",
|
|
4468
|
+
"navi-lending-emode-borrow": {
|
|
4469
|
+
amount: d.toString(),
|
|
4470
|
+
pool: q(i.pool, c),
|
|
4471
|
+
token: i.pool.token,
|
|
4472
|
+
valueUSD: d.multipliedBy(i.pool.oracle.price).toString(),
|
|
4473
|
+
emodeCap: c
|
|
4474
|
+
}
|
|
4475
|
+
});
|
|
4476
|
+
} catch (v) {
|
|
4477
|
+
console.error(v);
|
|
4478
|
+
}
|
|
4479
|
+
}
|
|
4480
|
+
} else {
|
|
4481
|
+
if (bignumber_default(i.supplyBalance).gt(0)) {
|
|
4482
|
+
const u = bignumber_default(i.supplyBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, bignumber_default.ROUND_DOWN);
|
|
4483
|
+
o.push({
|
|
4484
|
+
id: `${i.pool.uniqueId}_navi-lending-supply-${T()}`,
|
|
4485
|
+
wallet: e,
|
|
4486
|
+
protocol: "navi",
|
|
4487
|
+
type: "navi-lending-supply",
|
|
4488
|
+
market: i.market,
|
|
4489
|
+
"navi-lending-supply": {
|
|
4490
|
+
amount: u.toString(),
|
|
4491
|
+
pool: i.pool,
|
|
4492
|
+
token: i.pool.token,
|
|
4493
|
+
valueUSD: u.multipliedBy(i.pool.oracle.price).toString()
|
|
4494
|
+
}
|
|
4495
|
+
});
|
|
4496
|
+
}
|
|
4497
|
+
if (bignumber_default(i.borrowBalance).gt(0)) {
|
|
4498
|
+
const u = bignumber_default(i.borrowBalance).shiftedBy(-9).decimalPlaces(i.pool.token.decimals, bignumber_default.ROUND_DOWN);
|
|
4499
|
+
o.push({
|
|
4500
|
+
id: `${i.pool.uniqueId}_navi-lending-borrow-${T()}`,
|
|
4501
|
+
wallet: e,
|
|
4502
|
+
protocol: "navi",
|
|
4503
|
+
market: i.market,
|
|
4504
|
+
type: "navi-lending-borrow",
|
|
4505
|
+
"navi-lending-borrow": {
|
|
4506
|
+
amount: u.toString(),
|
|
4507
|
+
pool: i.pool,
|
|
4508
|
+
token: i.pool.token,
|
|
4509
|
+
valueUSD: u.multipliedBy(i.pool.oracle.price).toString()
|
|
4510
|
+
}
|
|
4511
|
+
});
|
|
4512
|
+
}
|
|
4513
|
+
}
|
|
4514
|
+
}), o;
|
|
4515
|
+
}
|
|
4516
|
+
);
|
|
4517
|
+
var oe = new pythSuiJs.SuiPriceServiceConnection("https://hermes.pyth.network", {
|
|
4518
|
+
timeout: 1e4
|
|
4519
|
+
});
|
|
4520
|
+
async function Pe(e, n) {
|
|
4521
|
+
try {
|
|
4522
|
+
const o = [], r = n?.client ?? P, t = e.map((s) => s.priceInfoObject), a = await r.multiGetObjects({
|
|
4523
|
+
ids: Array.from(new Set(t)),
|
|
781
4524
|
options: { showContent: true }
|
|
782
4525
|
});
|
|
783
|
-
|
|
784
|
-
const
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
_priceLastFetched = now;
|
|
794
|
-
}
|
|
4526
|
+
for (const s of a) {
|
|
4527
|
+
const i = s.data;
|
|
4528
|
+
if (!i || !i.content || i.content.dataType !== "moveObject") {
|
|
4529
|
+
console.warn(`fetched object ${i?.objectId} datatype should be moveObject`);
|
|
4530
|
+
continue;
|
|
4531
|
+
}
|
|
4532
|
+
const c = e.find((l) => l.priceInfoObject == i.objectId);
|
|
4533
|
+
if (!c) {
|
|
4534
|
+
console.warn(`unable to find pyth info from array, priceInfoObject: ${i.objectId}`);
|
|
4535
|
+
continue;
|
|
795
4536
|
}
|
|
4537
|
+
const u = i.content.fields.price_info.fields.price_feed.fields.price.fields, { magnitude: d, negative: f } = u.price.fields, v = u.conf, y = u.timestamp;
|
|
4538
|
+
o.push({
|
|
4539
|
+
priceFeedId: c.priceFeedId,
|
|
4540
|
+
priceInfoObject: c.priceInfoObject,
|
|
4541
|
+
price: f ? "-" + d : d,
|
|
4542
|
+
conf: v,
|
|
4543
|
+
publishTime: Number(y),
|
|
4544
|
+
expiration: c.expiration
|
|
4545
|
+
});
|
|
796
4546
|
}
|
|
797
|
-
|
|
4547
|
+
return o;
|
|
4548
|
+
} catch (o) {
|
|
4549
|
+
console.error(o, `Polling Sui on-chain price for ${e} failed.`);
|
|
4550
|
+
return;
|
|
798
4551
|
}
|
|
799
|
-
return _cachedSuiPrice || SUI_PRICE_FALLBACK;
|
|
800
4552
|
}
|
|
801
|
-
async function
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
4553
|
+
async function Se(e, n) {
|
|
4554
|
+
try {
|
|
4555
|
+
const o = [], r = await Pe(e, n);
|
|
4556
|
+
if (!r) return o;
|
|
4557
|
+
const t = Math.floor((/* @__PURE__ */ new Date()).valueOf() / 1e3);
|
|
4558
|
+
for (const a of r) {
|
|
4559
|
+
if (a.publishTime > t) {
|
|
4560
|
+
console.warn(
|
|
4561
|
+
`pyth price feed is invalid, id: ${a.priceFeedId}, publish time: ${a.publishTime}, current timestamp: ${t}`
|
|
4562
|
+
);
|
|
4563
|
+
continue;
|
|
4564
|
+
}
|
|
4565
|
+
const s = a.expiration || 60;
|
|
4566
|
+
t - a.publishTime > s && (console.info(
|
|
4567
|
+
`stale price feed, id: ${a.priceFeedId}, publish time: ${a.publishTime}, current timestamp: ${t}`
|
|
4568
|
+
), o.push(a.priceFeedId));
|
|
4569
|
+
}
|
|
4570
|
+
return o;
|
|
4571
|
+
} catch (o) {
|
|
4572
|
+
throw new Error(`failed to get pyth stale price feed id, msg: ${o.message}`);
|
|
815
4573
|
}
|
|
816
|
-
const suiAmount = Number(suiBalance.totalBalance) / Number(MIST_PER_SUI);
|
|
817
|
-
const savings = 0;
|
|
818
|
-
const usdEquiv = suiAmount * suiPriceUsd;
|
|
819
|
-
const total = totalStables + savings + usdEquiv;
|
|
820
|
-
return {
|
|
821
|
-
available: totalStables,
|
|
822
|
-
savings,
|
|
823
|
-
debt: 0,
|
|
824
|
-
pendingRewards: 0,
|
|
825
|
-
gasReserve: {
|
|
826
|
-
sui: suiAmount,
|
|
827
|
-
usdEquiv
|
|
828
|
-
},
|
|
829
|
-
total,
|
|
830
|
-
stables
|
|
831
|
-
};
|
|
832
4574
|
}
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
[/::suilend|::obligation/, "lending"],
|
|
838
|
-
[/::navi|::incentive_v\d+|::oracle_pro/, "lending"],
|
|
839
|
-
[/::cetus|::pool/, "swap"],
|
|
840
|
-
[/::deepbook/, "swap"],
|
|
841
|
-
[/::transfer::public_transfer/, "send"]
|
|
842
|
-
];
|
|
843
|
-
async function queryHistory(client, address, limit = 20) {
|
|
844
|
-
const txns = await client.queryTransactionBlocks({
|
|
845
|
-
filter: { FromAddress: address },
|
|
846
|
-
options: { showEffects: true, showInput: true, showBalanceChanges: true },
|
|
847
|
-
limit,
|
|
848
|
-
order: "descending"
|
|
4575
|
+
async function Ve(e, n, o) {
|
|
4576
|
+
const r = o?.client ?? P, t = await k({
|
|
4577
|
+
...o,
|
|
4578
|
+
cacheTime: w
|
|
849
4579
|
});
|
|
850
|
-
return txns.data.map((tx) => parseTxRecord(tx, address));
|
|
851
|
-
}
|
|
852
|
-
async function queryTransaction(client, digest, senderAddress) {
|
|
853
4580
|
try {
|
|
854
|
-
const
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
4581
|
+
const a = await oe.getPriceFeedsUpdateData(n);
|
|
4582
|
+
return await new pythSuiJs.SuiPythClient(
|
|
4583
|
+
r,
|
|
4584
|
+
t.oracle.pythStateId,
|
|
4585
|
+
t.oracle.wormholeStateId
|
|
4586
|
+
).updatePriceFeeds(e, a, n);
|
|
4587
|
+
} catch (a) {
|
|
4588
|
+
throw new Error(`failed to update pyth price feeds, msg: ${a.message}`);
|
|
861
4589
|
}
|
|
862
4590
|
}
|
|
863
|
-
function
|
|
864
|
-
const
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
4591
|
+
async function Ee(e, n, o) {
|
|
4592
|
+
const r = await k({
|
|
4593
|
+
...o,
|
|
4594
|
+
cacheTime: w
|
|
4595
|
+
});
|
|
4596
|
+
if (o?.updatePythPriceFeeds) {
|
|
4597
|
+
const t = n.filter((a) => !!a.pythPriceFeedId && !!a.pythPriceInfoObject).map((a) => ({
|
|
4598
|
+
priceFeedId: a.pythPriceFeedId,
|
|
4599
|
+
priceInfoObject: a.pythPriceInfoObject,
|
|
4600
|
+
expiration: 30
|
|
4601
|
+
}));
|
|
4602
|
+
try {
|
|
4603
|
+
const a = await Se(t, o);
|
|
4604
|
+
a.length > 0 && await Ve(e, a, o);
|
|
4605
|
+
} catch {
|
|
4606
|
+
console.error("Failed to update Pyth price feeds");
|
|
4607
|
+
}
|
|
4608
|
+
}
|
|
4609
|
+
for (const t of n)
|
|
4610
|
+
o?.env === "dev" ? e.moveCall({
|
|
4611
|
+
target: `${r.oracle.packageId}::oracle_pro::update_single_price`,
|
|
4612
|
+
arguments: [
|
|
4613
|
+
e.object("0x6"),
|
|
4614
|
+
// Clock object
|
|
4615
|
+
e.object(r.oracle.oracleConfig),
|
|
4616
|
+
// Oracle configuration
|
|
4617
|
+
e.object(r.oracle.priceOracle),
|
|
4618
|
+
// Price oracle contract
|
|
4619
|
+
e.object(r.oracle.supraOracleHolder),
|
|
4620
|
+
// Supra oracle holder
|
|
4621
|
+
e.object(t.pythPriceInfoObject),
|
|
4622
|
+
// Pyth price info object
|
|
4623
|
+
e.pure.address(t.feedId)
|
|
4624
|
+
// Price feed ID
|
|
4625
|
+
]
|
|
4626
|
+
}) : e.moveCall({
|
|
4627
|
+
target: `${r.oracle.packageId}::oracle_pro::update_single_price_v2`,
|
|
4628
|
+
arguments: [
|
|
4629
|
+
e.object("0x6"),
|
|
4630
|
+
// Clock object
|
|
4631
|
+
e.object(r.oracle.oracleConfig),
|
|
4632
|
+
// Oracle configuration
|
|
4633
|
+
e.object(r.oracle.priceOracle),
|
|
4634
|
+
// Price oracle contract
|
|
4635
|
+
e.object(r.oracle.supraOracleHolder),
|
|
4636
|
+
// Supra oracle holder
|
|
4637
|
+
e.object(t.pythPriceInfoObject),
|
|
4638
|
+
// Pyth price info object
|
|
4639
|
+
e.object(r.oracle.switchboardAggregator),
|
|
4640
|
+
e.pure.address(t.feedId)
|
|
4641
|
+
// Price feed ID
|
|
4642
|
+
]
|
|
4643
|
+
});
|
|
4644
|
+
return e;
|
|
878
4645
|
}
|
|
879
|
-
function
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
4646
|
+
async function ne(e) {
|
|
4647
|
+
return (await k({
|
|
4648
|
+
...e,
|
|
4649
|
+
cacheTime: w
|
|
4650
|
+
})).oracle.feeds;
|
|
883
4651
|
}
|
|
884
|
-
function
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
const decimals = getDecimalsForCoinType(coinType);
|
|
892
|
-
const amount = Math.abs(Number(BigInt(primaryOutflow.amount))) / 10 ** decimals;
|
|
893
|
-
const asset = resolveSymbol(coinType);
|
|
894
|
-
const recipientChange = inflows.find((c) => c.coinType === coinType);
|
|
895
|
-
const recipient = recipientChange ? resolveOwner(recipientChange.owner) ?? void 0 : void 0;
|
|
896
|
-
return { amount, asset, recipient };
|
|
4652
|
+
function Me(e, n) {
|
|
4653
|
+
return e.filter((o) => !!(n?.lendingState && n.lendingState.find((t) => t.assetId === o.assetId) || n?.lendingPositions && n.lendingPositions.find((t) => [
|
|
4654
|
+
"navi-lending-supply",
|
|
4655
|
+
"navi-lending-borrow",
|
|
4656
|
+
"navi-lending-emode-supply",
|
|
4657
|
+
"navi-lending-emode-borrow"
|
|
4658
|
+
].includes(t.type) ? t[t.type]?.pool?.id === o.assetId : false) || n?.pools && n.pools.find((t) => t.id === o.assetId)));
|
|
897
4659
|
}
|
|
898
|
-
function
|
|
899
|
-
const result = { moveCallTargets: [], commandTypes: [] };
|
|
4660
|
+
async function mt(e, n, o, r) {
|
|
900
4661
|
try {
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
4662
|
+
const t = await ne({
|
|
4663
|
+
...r
|
|
4664
|
+
}), a = [];
|
|
4665
|
+
o.forEach((u) => {
|
|
4666
|
+
a.includes(u.market) || a.push(u.market);
|
|
4667
|
+
});
|
|
4668
|
+
const s = await $e(n, {
|
|
4669
|
+
...r,
|
|
4670
|
+
markets: a
|
|
4671
|
+
}), i = Me(t, {
|
|
4672
|
+
lendingPositions: s,
|
|
4673
|
+
pools: o
|
|
4674
|
+
});
|
|
4675
|
+
return await Ee(e, i, {
|
|
4676
|
+
updatePythPriceFeeds: true,
|
|
4677
|
+
...r
|
|
4678
|
+
});
|
|
4679
|
+
} catch (t) {
|
|
4680
|
+
if (r?.throws)
|
|
4681
|
+
throw t;
|
|
4682
|
+
return console.error(t), e;
|
|
918
4683
|
}
|
|
919
|
-
return result;
|
|
920
4684
|
}
|
|
921
|
-
function
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
4685
|
+
async function De(e, n, o) {
|
|
4686
|
+
const r = o?.client ?? P, t = new transactions.Transaction(), a = await R({
|
|
4687
|
+
...o,
|
|
4688
|
+
markets: Object.values(M),
|
|
4689
|
+
cacheTime: w
|
|
4690
|
+
}), s = await ne(o);
|
|
4691
|
+
for (let d of n) {
|
|
4692
|
+
const f = await k({
|
|
4693
|
+
...o,
|
|
4694
|
+
cacheTime: w,
|
|
4695
|
+
market: d.market
|
|
4696
|
+
});
|
|
4697
|
+
t.moveCall({
|
|
4698
|
+
target: `${f.uiGetter}::incentive_v3_getter::get_user_atomic_claimable_rewards`,
|
|
4699
|
+
arguments: [
|
|
4700
|
+
t.object("0x06"),
|
|
4701
|
+
// Clock object
|
|
4702
|
+
t.object(f.storage),
|
|
4703
|
+
// Protocol storage
|
|
4704
|
+
t.object(f.incentiveV3),
|
|
4705
|
+
// Incentive V3 contract
|
|
4706
|
+
t.pure.address(d.address)
|
|
4707
|
+
// User address
|
|
4708
|
+
]
|
|
4709
|
+
});
|
|
926
4710
|
}
|
|
927
|
-
const
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
amount: feeAmount,
|
|
952
|
-
asset: "USDC",
|
|
953
|
-
rate: Number(bps) / Number(BPS_DENOMINATOR),
|
|
954
|
-
rawAmount
|
|
955
|
-
};
|
|
956
|
-
}
|
|
957
|
-
function addCollectFeeToTx(tx, paymentCoin, operation) {
|
|
958
|
-
const bps = FEE_RATES[operation];
|
|
959
|
-
if (bps <= 0n) return;
|
|
960
|
-
tx.moveCall({
|
|
961
|
-
target: `${T2000_PACKAGE_ID}::treasury::collect_fee`,
|
|
962
|
-
typeArguments: [SUPPORTED_ASSETS.USDC.type],
|
|
963
|
-
arguments: [
|
|
964
|
-
tx.object(T2000_TREASURY_ID),
|
|
965
|
-
tx.object(T2000_CONFIG_ID),
|
|
966
|
-
paymentCoin,
|
|
967
|
-
tx.pure.u8(OP_CODES[operation])
|
|
968
|
-
]
|
|
4711
|
+
const i = await r.devInspectTransactionBlock({
|
|
4712
|
+
transactionBlock: t,
|
|
4713
|
+
sender: e
|
|
4714
|
+
}), c = [];
|
|
4715
|
+
i?.results?.forEach((d) => {
|
|
4716
|
+
c.push(
|
|
4717
|
+
L(
|
|
4718
|
+
{
|
|
4719
|
+
results: [d]
|
|
4720
|
+
},
|
|
4721
|
+
[
|
|
4722
|
+
bcs$1.bcs.vector(bcs$1.bcs.string()),
|
|
4723
|
+
// Asset coin types
|
|
4724
|
+
bcs$1.bcs.vector(bcs$1.bcs.string()),
|
|
4725
|
+
// Reward coin types
|
|
4726
|
+
bcs$1.bcs.vector(bcs$1.bcs.u8()),
|
|
4727
|
+
// Reward options
|
|
4728
|
+
bcs$1.bcs.vector(bcs$1.bcs.Address),
|
|
4729
|
+
// Rule IDs
|
|
4730
|
+
bcs$1.bcs.vector(bcs$1.bcs.u256())
|
|
4731
|
+
// Claimable amounts
|
|
4732
|
+
]
|
|
4733
|
+
)
|
|
4734
|
+
);
|
|
969
4735
|
});
|
|
4736
|
+
const u = [];
|
|
4737
|
+
return c.forEach((d, f) => {
|
|
4738
|
+
const v = n[f];
|
|
4739
|
+
if (d.length === 5 && Array.isArray(d[0])) {
|
|
4740
|
+
const y = d[0].length;
|
|
4741
|
+
for (let l = 0; l < y; l++) {
|
|
4742
|
+
const h = s.find(
|
|
4743
|
+
(I) => b(I.coinType) === b(d[1][l])
|
|
4744
|
+
), j = a.find(
|
|
4745
|
+
(I) => b(I.coinType) === b(d[0][l]) && I.market === v.market
|
|
4746
|
+
);
|
|
4747
|
+
!h || !j || u.push({
|
|
4748
|
+
assetId: j.id,
|
|
4749
|
+
assetCoinType: b(d[0][l]),
|
|
4750
|
+
rewardCoinType: b(d[1][l]),
|
|
4751
|
+
option: Number(d[2][l]),
|
|
4752
|
+
userClaimableReward: Number(d[4][l]) / Math.pow(10, h.priceDecimal),
|
|
4753
|
+
ruleIds: Array.isArray(d[3][l]) ? d[3][l] : [d[3][l]],
|
|
4754
|
+
market: v.market,
|
|
4755
|
+
owner: v.owner,
|
|
4756
|
+
address: v.address,
|
|
4757
|
+
emodeId: v.emodeId
|
|
4758
|
+
});
|
|
4759
|
+
}
|
|
4760
|
+
}
|
|
4761
|
+
}), u;
|
|
970
4762
|
}
|
|
971
|
-
async function
|
|
4763
|
+
async function vt(e, n) {
|
|
4764
|
+
const o = (n?.markets || [M.main]).map((a) => _(a));
|
|
4765
|
+
let r = [];
|
|
972
4766
|
try {
|
|
973
|
-
await
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
4767
|
+
r = await x(e, n);
|
|
4768
|
+
} catch (a) {
|
|
4769
|
+
console.error(a);
|
|
4770
|
+
}
|
|
4771
|
+
const t = o.map((a) => ({
|
|
4772
|
+
address: e,
|
|
4773
|
+
owner: e,
|
|
4774
|
+
market: a.key
|
|
4775
|
+
})).concat(
|
|
4776
|
+
r.filter((a) => !!o.find((s) => s.id === a.marketId)).map((a) => {
|
|
4777
|
+
const s = _(a.marketId);
|
|
4778
|
+
return {
|
|
4779
|
+
address: a.accountCap,
|
|
4780
|
+
owner: e,
|
|
4781
|
+
market: s.key,
|
|
4782
|
+
emodeId: a.emodeId
|
|
4783
|
+
};
|
|
4784
|
+
})
|
|
4785
|
+
);
|
|
4786
|
+
return await De(e, t, n);
|
|
4787
|
+
}
|
|
4788
|
+
function wt(e) {
|
|
4789
|
+
const n = /* @__PURE__ */ new Map();
|
|
4790
|
+
e.forEach((r) => {
|
|
4791
|
+
const t = r.assetId, a = r.option, s = `${t}-${a}-${r.rewardCoinType}-${r.market}`;
|
|
4792
|
+
n.has(s) ? n.get(s).total += r.userClaimableReward : n.set(s, {
|
|
4793
|
+
assetId: t,
|
|
4794
|
+
rewardType: a,
|
|
4795
|
+
coinType: r.rewardCoinType,
|
|
4796
|
+
total: Number(r.userClaimableReward),
|
|
4797
|
+
market: r.market
|
|
983
4798
|
});
|
|
984
|
-
}
|
|
4799
|
+
});
|
|
4800
|
+
const o = /* @__PURE__ */ new Map();
|
|
4801
|
+
for (const { assetId: r, rewardType: t, coinType: a, total: s, market: i } of n.values()) {
|
|
4802
|
+
const c = `${r}-${t}-${i}`;
|
|
4803
|
+
o.has(c) || o.set(c, { assetId: r, rewardType: t, market: i, rewards: /* @__PURE__ */ new Map() });
|
|
4804
|
+
const u = o.get(c);
|
|
4805
|
+
u.rewards.set(a, (u.rewards.get(a) || 0) + s);
|
|
4806
|
+
}
|
|
4807
|
+
return Array.from(o.values()).map((r) => ({
|
|
4808
|
+
assetId: r.assetId,
|
|
4809
|
+
rewardType: r.rewardType,
|
|
4810
|
+
market: r.market,
|
|
4811
|
+
rewards: Array.from(r.rewards.entries()).map(([t, a]) => ({
|
|
4812
|
+
coinType: t,
|
|
4813
|
+
available: a.toFixed(6)
|
|
4814
|
+
}))
|
|
4815
|
+
}));
|
|
4816
|
+
}
|
|
4817
|
+
async function Ct(e, n, o) {
|
|
4818
|
+
const r = await R({
|
|
4819
|
+
...o,
|
|
4820
|
+
markets: Object.values(M),
|
|
4821
|
+
cacheTime: w
|
|
4822
|
+
}), t = /* @__PURE__ */ new Map();
|
|
4823
|
+
for (const s of n) {
|
|
4824
|
+
const { rewardCoinType: i, ruleIds: c, market: u, owner: d, address: f, emodeId: v } = s, y = `${i}___${f}`;
|
|
4825
|
+
for (const l of c) {
|
|
4826
|
+
t.has(y) || t.set(y, {
|
|
4827
|
+
assetIds: [],
|
|
4828
|
+
ruleIds: [],
|
|
4829
|
+
amount: 0,
|
|
4830
|
+
market: u,
|
|
4831
|
+
owner: d,
|
|
4832
|
+
address: f,
|
|
4833
|
+
isEMode: typeof v < "u"
|
|
4834
|
+
});
|
|
4835
|
+
const h = t.get(y);
|
|
4836
|
+
h.assetIds.push(s.assetCoinType.replace("0x", "")), h.ruleIds.push(l), h.amount += s.userClaimableReward;
|
|
4837
|
+
}
|
|
985
4838
|
}
|
|
4839
|
+
const a = [];
|
|
4840
|
+
for (const [
|
|
4841
|
+
s,
|
|
4842
|
+
{ assetIds: i, ruleIds: c, amount: u, market: d, owner: f, address: v, isEMode: y }
|
|
4843
|
+
] of t) {
|
|
4844
|
+
const l = await k({
|
|
4845
|
+
...o,
|
|
4846
|
+
cacheTime: w,
|
|
4847
|
+
market: d
|
|
4848
|
+
}), h = s.split("___")[0], j = r.find(
|
|
4849
|
+
(S) => b(S.suiCoinType) === b(h) && S.market === d
|
|
4850
|
+
);
|
|
4851
|
+
if (!j || !j.contract.rewardFundId)
|
|
4852
|
+
throw new Error(`No matching rewardFund found for reward coin: ${h} ${d}`);
|
|
4853
|
+
const I = j.contract.rewardFundId;
|
|
4854
|
+
if (o?.accountCap && !o.customCoinReceive)
|
|
4855
|
+
throw new Error("customCoinReceive is required when accountCap is provided");
|
|
4856
|
+
if (o?.customCoinReceive) {
|
|
4857
|
+
let S;
|
|
4858
|
+
o.accountCap ? S = e.moveCall({
|
|
4859
|
+
target: `${l.package}::incentive_v3::claim_reward_with_account_cap`,
|
|
4860
|
+
arguments: [
|
|
4861
|
+
e.object("0x06"),
|
|
4862
|
+
// Clock object
|
|
4863
|
+
e.object(l.incentiveV3),
|
|
4864
|
+
// Incentive V3 contract
|
|
4865
|
+
e.object(l.storage),
|
|
4866
|
+
// Protocol storage
|
|
4867
|
+
e.object(I),
|
|
4868
|
+
// Reward fund
|
|
4869
|
+
e.pure.vector("string", i),
|
|
4870
|
+
// Asset IDs
|
|
4871
|
+
e.pure.vector("address", c),
|
|
4872
|
+
// Rule IDs
|
|
4873
|
+
g(o.accountCap, e.object)
|
|
4874
|
+
// Account capability
|
|
4875
|
+
],
|
|
4876
|
+
typeArguments: [h]
|
|
4877
|
+
}) : y ? S = e.moveCall({
|
|
4878
|
+
target: `${l.package}::incentive_v3::claim_reward_with_account_cap`,
|
|
4879
|
+
arguments: [
|
|
4880
|
+
e.object("0x06"),
|
|
4881
|
+
// Clock object
|
|
4882
|
+
e.object(l.incentiveV3),
|
|
4883
|
+
// Incentive V3 contract
|
|
4884
|
+
e.object(l.storage),
|
|
4885
|
+
// Protocol storage
|
|
4886
|
+
e.object(I),
|
|
4887
|
+
// Reward fund
|
|
4888
|
+
e.pure.vector("string", i),
|
|
4889
|
+
// Asset IDs
|
|
4890
|
+
e.pure.vector("address", c),
|
|
4891
|
+
// Rule IDs
|
|
4892
|
+
g(v, e.object)
|
|
4893
|
+
// Account capability
|
|
4894
|
+
],
|
|
4895
|
+
typeArguments: [h]
|
|
4896
|
+
}) : S = e.moveCall({
|
|
4897
|
+
target: `${l.package}::incentive_v3::claim_reward`,
|
|
4898
|
+
arguments: [
|
|
4899
|
+
e.object("0x06"),
|
|
4900
|
+
// Clock object
|
|
4901
|
+
e.object(l.incentiveV3),
|
|
4902
|
+
// Incentive V3 contract
|
|
4903
|
+
e.object(l.storage),
|
|
4904
|
+
// Protocol storage
|
|
4905
|
+
e.object(I),
|
|
4906
|
+
// Reward fund
|
|
4907
|
+
e.pure.vector("string", i),
|
|
4908
|
+
// Asset IDs
|
|
4909
|
+
e.pure.vector("address", c)
|
|
4910
|
+
// Rule IDs
|
|
4911
|
+
],
|
|
4912
|
+
typeArguments: [h]
|
|
4913
|
+
});
|
|
4914
|
+
const [D] = e.moveCall({
|
|
4915
|
+
target: "0x2::coin::from_balance",
|
|
4916
|
+
arguments: [S],
|
|
4917
|
+
typeArguments: [h]
|
|
4918
|
+
});
|
|
4919
|
+
if (o?.customCoinReceive.type === "transfer") {
|
|
4920
|
+
if (!o.customCoinReceive.transfer)
|
|
4921
|
+
throw new Error("customCoinReceive.transfer is required");
|
|
4922
|
+
e.transferObjects(
|
|
4923
|
+
[D],
|
|
4924
|
+
g(o.customCoinReceive.transfer, e.pure.address)
|
|
4925
|
+
);
|
|
4926
|
+
}
|
|
4927
|
+
if (o?.customCoinReceive.type === "depositNAVI") {
|
|
4928
|
+
const re = bignumber_default(j.totalSupplyAmount).shiftedBy(-9), ae = bignumber_default(j.supplyCapCeiling).shiftedBy(-27);
|
|
4929
|
+
re.plus(u).isGreaterThan(ae) && o?.customCoinReceive.depositNAVI?.fallbackReceiveAddress ? e.transferObjects(
|
|
4930
|
+
[D],
|
|
4931
|
+
e.pure.address(o.customCoinReceive.depositNAVI.fallbackReceiveAddress)
|
|
4932
|
+
) : await Ce(e, j, D, o);
|
|
4933
|
+
} else
|
|
4934
|
+
a.push({
|
|
4935
|
+
coin: D,
|
|
4936
|
+
identifier: j,
|
|
4937
|
+
owner: f,
|
|
4938
|
+
isEMode: y
|
|
4939
|
+
});
|
|
4940
|
+
} else if (o?.accountCap || y) {
|
|
4941
|
+
const S = e.moveCall({
|
|
4942
|
+
target: `${l.package}::incentive_v3::claim_reward_with_account_cap`,
|
|
4943
|
+
arguments: [
|
|
4944
|
+
e.object("0x06"),
|
|
4945
|
+
// Clock object
|
|
4946
|
+
e.object(l.incentiveV3),
|
|
4947
|
+
// Incentive V3 contract
|
|
4948
|
+
e.object(l.storage),
|
|
4949
|
+
// Protocol storage
|
|
4950
|
+
e.object(I),
|
|
4951
|
+
// Reward fund
|
|
4952
|
+
e.pure.vector("string", i),
|
|
4953
|
+
// Asset IDs
|
|
4954
|
+
e.pure.vector("address", c),
|
|
4955
|
+
// Rule IDs
|
|
4956
|
+
g(o?.accountCap || v, e.object)
|
|
4957
|
+
// Account capability
|
|
4958
|
+
],
|
|
4959
|
+
typeArguments: [h]
|
|
4960
|
+
}), [D] = e.moveCall({
|
|
4961
|
+
target: "0x2::coin::from_balance",
|
|
4962
|
+
arguments: [S],
|
|
4963
|
+
typeArguments: [h]
|
|
4964
|
+
});
|
|
4965
|
+
e.transferObjects(
|
|
4966
|
+
[D],
|
|
4967
|
+
g(o?.accountCap || f, e.pure.address)
|
|
4968
|
+
);
|
|
4969
|
+
} else
|
|
4970
|
+
e.moveCall({
|
|
4971
|
+
target: `${l.package}::incentive_v3::claim_reward_entry`,
|
|
4972
|
+
arguments: [
|
|
4973
|
+
e.object("0x06"),
|
|
4974
|
+
// Clock object
|
|
4975
|
+
e.object(l.incentiveV3),
|
|
4976
|
+
// Incentive V3 contract
|
|
4977
|
+
e.object(l.storage),
|
|
4978
|
+
// Protocol storage
|
|
4979
|
+
e.object(I),
|
|
4980
|
+
// Reward fund
|
|
4981
|
+
e.pure.vector("string", i),
|
|
4982
|
+
// Asset IDs
|
|
4983
|
+
e.pure.vector("address", c)
|
|
4984
|
+
// Rule IDs
|
|
4985
|
+
],
|
|
4986
|
+
typeArguments: [h]
|
|
4987
|
+
});
|
|
4988
|
+
}
|
|
4989
|
+
return a;
|
|
986
4990
|
}
|
|
4991
|
+
|
|
4992
|
+
// src/protocols/navi.ts
|
|
987
4993
|
init_errors();
|
|
988
4994
|
var MIN_HEALTH_FACTOR = 1.5;
|
|
989
4995
|
function sdkOptions(client) {
|
|
@@ -1002,7 +5008,7 @@ async function refreshOracle(tx, client, address, options) {
|
|
|
1002
5008
|
origWarn.apply(console, args);
|
|
1003
5009
|
};
|
|
1004
5010
|
try {
|
|
1005
|
-
const pools = await
|
|
5011
|
+
const pools = await R(sdkOptions(client));
|
|
1006
5012
|
const oracleOpts = {
|
|
1007
5013
|
...sdkOptions(client),
|
|
1008
5014
|
throws: false,
|
|
@@ -1010,7 +5016,7 @@ async function refreshOracle(tx, client, address, options) {
|
|
|
1010
5016
|
// with sponsored transactions where tx.gas belongs to the sponsor.
|
|
1011
5017
|
updatePythPriceFeeds: !options?.skipPythUpdate
|
|
1012
5018
|
};
|
|
1013
|
-
await
|
|
5019
|
+
await mt(tx, address, pools, oracleOpts);
|
|
1014
5020
|
} catch {
|
|
1015
5021
|
} finally {
|
|
1016
5022
|
console.info = origInfo;
|
|
@@ -1072,7 +5078,7 @@ function mergeCoins(tx, coins) {
|
|
|
1072
5078
|
}
|
|
1073
5079
|
async function getPositions(client, address) {
|
|
1074
5080
|
try {
|
|
1075
|
-
const naviPositions = await
|
|
5081
|
+
const naviPositions = await $e(address, {
|
|
1076
5082
|
...sdkOptions(client),
|
|
1077
5083
|
markets: ["main"]
|
|
1078
5084
|
});
|
|
@@ -1106,7 +5112,7 @@ async function getPositions(client, address) {
|
|
|
1106
5112
|
}
|
|
1107
5113
|
async function getRates(client) {
|
|
1108
5114
|
try {
|
|
1109
|
-
const pools = await
|
|
5115
|
+
const pools = await R(sdkOptions(client));
|
|
1110
5116
|
const result = {};
|
|
1111
5117
|
for (const asset of ALL_NAVI_ASSETS) {
|
|
1112
5118
|
const targetType = SUPPORTED_ASSETS[asset].type;
|
|
@@ -1139,7 +5145,7 @@ async function getHealthFactor(client, address) {
|
|
|
1139
5145
|
}
|
|
1140
5146
|
let healthFactor;
|
|
1141
5147
|
try {
|
|
1142
|
-
const hf = await
|
|
5148
|
+
const hf = await ct(address, sdkOptions(client));
|
|
1143
5149
|
healthFactor = hf > 1e5 ? Infinity : hf;
|
|
1144
5150
|
} catch {
|
|
1145
5151
|
healthFactor = borrowed > 0 ? supplied * 0.75 / borrowed : Infinity;
|
|
@@ -1171,7 +5177,7 @@ async function buildSaveTx(client, address, amount, options = {}) {
|
|
|
1171
5177
|
}
|
|
1172
5178
|
const rawAmount = Math.min(Number(stableToRaw(amount, assetInfo.decimals)), Number(totalBalance));
|
|
1173
5179
|
try {
|
|
1174
|
-
await
|
|
5180
|
+
await Ce(tx, assetInfo.type, coinObj, {
|
|
1175
5181
|
...sdkOptions(client),
|
|
1176
5182
|
amount: rawAmount
|
|
1177
5183
|
});
|
|
@@ -1200,7 +5206,7 @@ async function buildWithdrawTx(client, address, amount, options = {}) {
|
|
|
1200
5206
|
tx.setSender(address);
|
|
1201
5207
|
await refreshOracle(tx, client, address, { skipPythUpdate: options.sponsored });
|
|
1202
5208
|
try {
|
|
1203
|
-
const coin = await
|
|
5209
|
+
const coin = await Qe(tx, assetInfo.type, rawAmount, sdkOptions(client));
|
|
1204
5210
|
tx.transferObjects([coin], address);
|
|
1205
5211
|
} catch (err) {
|
|
1206
5212
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -1230,7 +5236,7 @@ async function addWithdrawToTx(tx, client, address, amount, options = {}) {
|
|
|
1230
5236
|
}
|
|
1231
5237
|
await refreshOracle(tx, client, address, { skipPythUpdate: sponsored });
|
|
1232
5238
|
try {
|
|
1233
|
-
const coin = await
|
|
5239
|
+
const coin = await Qe(tx, assetInfo.type, rawAmount, sdkOptions(client));
|
|
1234
5240
|
return { coin, effectiveAmount };
|
|
1235
5241
|
} catch (err) {
|
|
1236
5242
|
const msg = err instanceof Error ? err.message : String(err);
|
|
@@ -1244,7 +5250,7 @@ async function addSaveToTx(tx, _client, _address, coin, options = {}) {
|
|
|
1244
5250
|
addCollectFeeToTx(tx, coin, "save");
|
|
1245
5251
|
}
|
|
1246
5252
|
try {
|
|
1247
|
-
await
|
|
5253
|
+
await Ce(tx, assetInfo.type, coin, { env: "prod" });
|
|
1248
5254
|
} catch (err) {
|
|
1249
5255
|
const msg = err instanceof Error ? err.message : String(err);
|
|
1250
5256
|
throw new exports.T2000Error("PROTOCOL_UNAVAILABLE", `NAVI deposit failed: ${msg}`);
|
|
@@ -1256,7 +5262,7 @@ async function addRepayToTx(tx, client, address, coin, options = {}) {
|
|
|
1256
5262
|
const assetInfo = resolveAssetInfo(asset);
|
|
1257
5263
|
await refreshOracle(tx, client, address, { skipPythUpdate: sponsored });
|
|
1258
5264
|
try {
|
|
1259
|
-
await
|
|
5265
|
+
await xe(tx, assetInfo.type, coin, { env: "prod" });
|
|
1260
5266
|
} catch (err) {
|
|
1261
5267
|
const msg = err instanceof Error ? err.message : String(err);
|
|
1262
5268
|
throw new exports.T2000Error("PROTOCOL_UNAVAILABLE", `NAVI repay failed: ${msg}`);
|
|
@@ -1273,7 +5279,7 @@ async function buildBorrowTx(client, address, amount, options = {}) {
|
|
|
1273
5279
|
tx.setSender(address);
|
|
1274
5280
|
await refreshOracle(tx, client, address, { skipPythUpdate: options.sponsored });
|
|
1275
5281
|
try {
|
|
1276
|
-
const borrowedCoin = await
|
|
5282
|
+
const borrowedCoin = await Xe(tx, assetInfo.type, rawAmount, sdkOptions(client));
|
|
1277
5283
|
if (options.collectFee) {
|
|
1278
5284
|
addCollectFeeToTx(tx, borrowedCoin, "borrow");
|
|
1279
5285
|
}
|
|
@@ -1307,7 +5313,7 @@ async function buildRepayTx(client, address, amount, options = {}) {
|
|
|
1307
5313
|
skipOracle: options.skipOracle
|
|
1308
5314
|
});
|
|
1309
5315
|
try {
|
|
1310
|
-
await
|
|
5316
|
+
await xe(tx, assetInfo.type, repayCoin, {
|
|
1311
5317
|
...sdkOptions(client),
|
|
1312
5318
|
amount: rawAmount
|
|
1313
5319
|
});
|
|
@@ -1338,12 +5344,12 @@ async function maxBorrowAmount(client, address) {
|
|
|
1338
5344
|
}
|
|
1339
5345
|
async function getPendingRewards(client, address) {
|
|
1340
5346
|
try {
|
|
1341
|
-
const rewards = await
|
|
5347
|
+
const rewards = await vt(address, {
|
|
1342
5348
|
...sdkOptions(client),
|
|
1343
5349
|
markets: ["main"]
|
|
1344
5350
|
});
|
|
1345
5351
|
if (!rewards || rewards.length === 0) return [];
|
|
1346
|
-
const summary =
|
|
5352
|
+
const summary = wt(rewards);
|
|
1347
5353
|
const result = [];
|
|
1348
5354
|
for (const s of summary) {
|
|
1349
5355
|
for (const rw of s.rewards) {
|
|
@@ -1367,7 +5373,7 @@ async function getPendingRewards(client, address) {
|
|
|
1367
5373
|
}
|
|
1368
5374
|
async function addClaimRewardsToTx(tx, client, address) {
|
|
1369
5375
|
try {
|
|
1370
|
-
const rewards = await
|
|
5376
|
+
const rewards = await vt(address, {
|
|
1371
5377
|
...sdkOptions(client),
|
|
1372
5378
|
markets: ["main"]
|
|
1373
5379
|
});
|
|
@@ -1376,7 +5382,7 @@ async function addClaimRewardsToTx(tx, client, address) {
|
|
|
1376
5382
|
(r) => Number(r.userClaimableReward) > 0
|
|
1377
5383
|
);
|
|
1378
5384
|
if (claimable.length === 0) return [];
|
|
1379
|
-
const claimed = await
|
|
5385
|
+
const claimed = await Ct(tx, claimable, {
|
|
1380
5386
|
env: "prod",
|
|
1381
5387
|
customCoinReceive: { type: "transfer", transfer: address }
|
|
1382
5388
|
});
|
|
@@ -1441,7 +5447,7 @@ var ProtocolRegistry = class {
|
|
|
1441
5447
|
if (candidates.length === 0) {
|
|
1442
5448
|
throw new exports.T2000Error("ASSET_NOT_SUPPORTED", `No lending adapter supports saving ${asset}`);
|
|
1443
5449
|
}
|
|
1444
|
-
candidates.sort((a,
|
|
5450
|
+
candidates.sort((a, b2) => b2.rate.saveApy - a.rate.saveApy);
|
|
1445
5451
|
return candidates[0];
|
|
1446
5452
|
}
|
|
1447
5453
|
async bestBorrowRate(asset, opts) {
|
|
@@ -1459,7 +5465,7 @@ var ProtocolRegistry = class {
|
|
|
1459
5465
|
if (candidates.length === 0) {
|
|
1460
5466
|
throw new exports.T2000Error("ASSET_NOT_SUPPORTED", `No lending adapter supports borrowing ${asset}`);
|
|
1461
5467
|
}
|
|
1462
|
-
candidates.sort((a,
|
|
5468
|
+
candidates.sort((a, b2) => a.rate.borrowApy - b2.rate.borrowApy);
|
|
1463
5469
|
return candidates[0];
|
|
1464
5470
|
}
|
|
1465
5471
|
async bestSaveRateAcrossAssets() {
|
|
@@ -1478,7 +5484,7 @@ var ProtocolRegistry = class {
|
|
|
1478
5484
|
if (candidates.length === 0) {
|
|
1479
5485
|
throw new exports.T2000Error("ASSET_NOT_SUPPORTED", "No lending adapter found for any stablecoin");
|
|
1480
5486
|
}
|
|
1481
|
-
candidates.sort((a,
|
|
5487
|
+
candidates.sort((a, b2) => b2.rate.saveApy - a.rate.saveApy);
|
|
1482
5488
|
return candidates[0];
|
|
1483
5489
|
}
|
|
1484
5490
|
async allRatesAcrossAssets() {
|
|
@@ -1690,12 +5696,12 @@ async function executeAutoTopUp(_client, _signer) {
|
|
|
1690
5696
|
init_errors();
|
|
1691
5697
|
|
|
1692
5698
|
// src/utils/base64.ts
|
|
1693
|
-
function
|
|
5699
|
+
function toBase642(bytes) {
|
|
1694
5700
|
let binary = "";
|
|
1695
5701
|
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
1696
5702
|
return btoa(binary);
|
|
1697
5703
|
}
|
|
1698
|
-
function
|
|
5704
|
+
function fromBase642(b64) {
|
|
1699
5705
|
const binary = atob(b64);
|
|
1700
5706
|
const bytes = new Uint8Array(binary.length);
|
|
1701
5707
|
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
@@ -1709,7 +5715,7 @@ async function requestGasSponsorship(txJson, sender, type, txBcsBytes) {
|
|
|
1709
5715
|
payload.txBcsBytes = txBcsBytes;
|
|
1710
5716
|
} else {
|
|
1711
5717
|
payload.txJson = txJson;
|
|
1712
|
-
payload.txBytes =
|
|
5718
|
+
payload.txBytes = toBase642(new TextEncoder().encode(txJson));
|
|
1713
5719
|
}
|
|
1714
5720
|
const res = await fetch(`${API_BASE_URL}/api/gas`, {
|
|
1715
5721
|
method: "POST",
|
|
@@ -1791,7 +5797,7 @@ async function trySelfFunded(client, signer, tx) {
|
|
|
1791
5797
|
const builtBytes = await tx.build({ client });
|
|
1792
5798
|
const { signature } = await signer.signTransaction(builtBytes);
|
|
1793
5799
|
const result = await client.executeTransactionBlock({
|
|
1794
|
-
transactionBlock:
|
|
5800
|
+
transactionBlock: toBase642(builtBytes),
|
|
1795
5801
|
signature: [signature],
|
|
1796
5802
|
options: { showEffects: true, showBalanceChanges: true }
|
|
1797
5803
|
});
|
|
@@ -1817,7 +5823,7 @@ async function tryAutoTopUpThenSelfFund(client, signer, buildTx) {
|
|
|
1817
5823
|
const builtBytes = await tx.build({ client });
|
|
1818
5824
|
const { signature } = await signer.signTransaction(builtBytes);
|
|
1819
5825
|
const result = await client.executeTransactionBlock({
|
|
1820
|
-
transactionBlock:
|
|
5826
|
+
transactionBlock: toBase642(builtBytes),
|
|
1821
5827
|
signature: [signature],
|
|
1822
5828
|
options: { showEffects: true, showBalanceChanges: true }
|
|
1823
5829
|
});
|
|
@@ -1842,10 +5848,10 @@ async function trySponsored(client, signer, tx) {
|
|
|
1842
5848
|
txJson = tx.serialize();
|
|
1843
5849
|
} catch {
|
|
1844
5850
|
const bcsBytes = await tx.build({ client });
|
|
1845
|
-
txBcsBase64 =
|
|
5851
|
+
txBcsBase64 = toBase642(bcsBytes);
|
|
1846
5852
|
}
|
|
1847
5853
|
const sponsoredResult = await requestGasSponsorship(txJson ?? "", address, void 0, txBcsBase64);
|
|
1848
|
-
const sponsoredTxBytes =
|
|
5854
|
+
const sponsoredTxBytes = fromBase642(sponsoredResult.txBytes);
|
|
1849
5855
|
const { signature: agentSig } = await signer.signTransaction(sponsoredTxBytes);
|
|
1850
5856
|
const result = await client.executeTransactionBlock({
|
|
1851
5857
|
transactionBlock: sponsoredResult.txBytes,
|
|
@@ -2628,13 +6634,28 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
2628
6634
|
const currency = params?.currency ?? "USDC";
|
|
2629
6635
|
const memo = params?.memo ?? null;
|
|
2630
6636
|
const label = params?.label ?? null;
|
|
2631
|
-
const
|
|
2632
|
-
|
|
2633
|
-
if (amount != null)
|
|
2634
|
-
|
|
2635
|
-
|
|
2636
|
-
|
|
2637
|
-
|
|
6637
|
+
const nonce = crypto.randomUUID();
|
|
6638
|
+
let qrUri;
|
|
6639
|
+
if (amount != null && amount > 0) {
|
|
6640
|
+
const decimals = currency === "SUI" ? 9 : 6;
|
|
6641
|
+
const coinType = currency === "SUI" ? "0x2::sui::SUI" : SUPPORTED_ASSETS.USDC.type;
|
|
6642
|
+
const rawAmount = BigInt(Math.floor(amount * 10 ** decimals));
|
|
6643
|
+
qrUri = paymentKit.createPaymentTransactionUri({
|
|
6644
|
+
receiverAddress: this._address,
|
|
6645
|
+
amount: rawAmount,
|
|
6646
|
+
coinType,
|
|
6647
|
+
nonce,
|
|
6648
|
+
...label ? { label } : {},
|
|
6649
|
+
...memo ? { message: memo } : {}
|
|
6650
|
+
});
|
|
6651
|
+
} else {
|
|
6652
|
+
const qrParts = [`sui:${this._address}`];
|
|
6653
|
+
const queryParams = [];
|
|
6654
|
+
if (currency !== "SUI") queryParams.push(`currency=${currency}`);
|
|
6655
|
+
if (memo) queryParams.push(`memo=${encodeURIComponent(memo)}`);
|
|
6656
|
+
if (label) queryParams.push(`label=${encodeURIComponent(label)}`);
|
|
6657
|
+
qrUri = queryParams.length > 0 ? `${qrParts[0]}?${queryParams.join("&")}` : qrParts[0];
|
|
6658
|
+
}
|
|
2638
6659
|
const amountStr = amount != null ? `$${amount.toFixed(2)} ` : "";
|
|
2639
6660
|
const displayParts = [`Send ${amountStr}${currency} to ${truncateAddress(this._address)}`];
|
|
2640
6661
|
if (memo) displayParts.push(`Memo: ${memo}`);
|
|
@@ -2645,6 +6666,7 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
2645
6666
|
currency,
|
|
2646
6667
|
memo,
|
|
2647
6668
|
label,
|
|
6669
|
+
nonce,
|
|
2648
6670
|
qrUri,
|
|
2649
6671
|
displayText: displayParts.join("\n")
|
|
2650
6672
|
};
|
|
@@ -2749,11 +6771,11 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
2749
6771
|
if (supplies.length === 0) {
|
|
2750
6772
|
throw new exports.T2000Error("NO_COLLATERAL", params.asset ? `No ${params.asset} savings to withdraw` : "No savings to withdraw");
|
|
2751
6773
|
}
|
|
2752
|
-
supplies.sort((a,
|
|
6774
|
+
supplies.sort((a, b2) => {
|
|
2753
6775
|
const aIsUsdc = a.asset === "USDC" ? 0 : 1;
|
|
2754
|
-
const bIsUsdc =
|
|
6776
|
+
const bIsUsdc = b2.asset === "USDC" ? 0 : 1;
|
|
2755
6777
|
if (aIsUsdc !== bIsUsdc) return aIsUsdc - bIsUsdc;
|
|
2756
|
-
return a.apy -
|
|
6778
|
+
return a.apy - b2.apy;
|
|
2757
6779
|
});
|
|
2758
6780
|
const target = supplies[0];
|
|
2759
6781
|
const adapter = this.registry.getLending(target.protocolId);
|
|
@@ -2996,8 +7018,8 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
2996
7018
|
const borrows = [];
|
|
2997
7019
|
for (const pos of allPositions) {
|
|
2998
7020
|
if (params.protocol && pos.protocolId !== params.protocol) continue;
|
|
2999
|
-
for (const
|
|
3000
|
-
if (
|
|
7021
|
+
for (const b2 of pos.positions.borrows) {
|
|
7022
|
+
if (b2.amount > 1e-3) borrows.push({ protocolId: pos.protocolId, asset: b2.asset, amount: b2.amount, apy: b2.apy });
|
|
3001
7023
|
}
|
|
3002
7024
|
}
|
|
3003
7025
|
if (borrows.length === 0) {
|
|
@@ -3006,7 +7028,7 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
3006
7028
|
if (params.amount === "all") {
|
|
3007
7029
|
return this._repayAllBorrows(borrows);
|
|
3008
7030
|
}
|
|
3009
|
-
borrows.sort((a,
|
|
7031
|
+
borrows.sort((a, b2) => b2.apy - a.apy);
|
|
3010
7032
|
const target = borrows[0];
|
|
3011
7033
|
const adapter = this.registry.getLending(target.protocolId);
|
|
3012
7034
|
if (!adapter) throw new exports.T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${target.protocolId} not found`);
|
|
@@ -3038,7 +7060,7 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
3038
7060
|
};
|
|
3039
7061
|
}
|
|
3040
7062
|
async _repayAllBorrows(borrows) {
|
|
3041
|
-
borrows.sort((a,
|
|
7063
|
+
borrows.sort((a, b2) => b2.apy - a.apy);
|
|
3042
7064
|
const entries = [];
|
|
3043
7065
|
for (const borrow of borrows) {
|
|
3044
7066
|
const adapter = this.registry.getLending(borrow.protocolId);
|
|
@@ -3281,13 +7303,13 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
3281
7303
|
amountUsd: s.amountUsd,
|
|
3282
7304
|
apy: s.apy
|
|
3283
7305
|
})),
|
|
3284
|
-
...p.positions.borrows.filter((
|
|
7306
|
+
...p.positions.borrows.filter((b2) => b2.amount > 5e-3).map((b2) => ({
|
|
3285
7307
|
protocol: p.protocolId,
|
|
3286
|
-
asset:
|
|
7308
|
+
asset: b2.asset,
|
|
3287
7309
|
type: "borrow",
|
|
3288
|
-
amount:
|
|
3289
|
-
amountUsd:
|
|
3290
|
-
apy:
|
|
7310
|
+
amount: b2.amount,
|
|
7311
|
+
amountUsd: b2.amountUsd,
|
|
7312
|
+
apy: b2.apy
|
|
3291
7313
|
}))
|
|
3292
7314
|
]
|
|
3293
7315
|
);
|
|
@@ -3741,10 +7763,15 @@ async function verifyScopedIntent(intent, adminPublicKeyBytes) {
|
|
|
3741
7763
|
}
|
|
3742
7764
|
function canonicalIntentBytes(payload) {
|
|
3743
7765
|
const sorted = Object.fromEntries(
|
|
3744
|
-
Object.entries(payload).sort(([a], [
|
|
7766
|
+
Object.entries(payload).sort(([a], [b2]) => a.localeCompare(b2))
|
|
3745
7767
|
);
|
|
3746
7768
|
return new TextEncoder().encode(JSON.stringify(sorted));
|
|
3747
7769
|
}
|
|
7770
|
+
/*! Bundled license information:
|
|
7771
|
+
|
|
7772
|
+
@scure/base/index.js:
|
|
7773
|
+
(*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
7774
|
+
*/
|
|
3748
7775
|
|
|
3749
7776
|
exports.ALLOWANCE_FEATURES = ALLOWANCE_FEATURES;
|
|
3750
7777
|
exports.ALL_NAVI_ASSETS = ALL_NAVI_ASSETS;
|