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