@storm-trade/dex-math
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -31,3 +31,3 @@ import { BigNumber } from 'bignumber.js'; | ||
export declare function calculateFunding(settings: AmmSettings, amm: AmmState, fundingSettings: FundingSettings): BN; | ||
export declare function calculateFundingRate(settings: AmmSettings, amm: AmmState, fundingSettings: FundingSettings | null): BN; | ||
@@ -34,0 +34,0 @@ export declare function calculateQuoteAssetWeight(amm: AmmState, terminalPrice: BN): { |
@@ -1,4 +0,4 @@ | ||
var xe = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i, Ae = Math.ceil, J = Math.floor, b = "[BigNumber Error] ", Ee = b + "Number primitive has more than 15 significant digits: ", ee = 1e14, R = 14, Ne = 9007199254740991, Oe = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], oe = 1e7, U = 1e9; | ||
var xe = /^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i, Ae = Math.ceil, J = Math.floor, H = "[BigNumber Error] ", Ee = H + "Number primitive has more than 15 significant digits: ", ee = 1e14, S = 14, Ne = 9007199254740991, Oe = [1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13], se = 1e7, W = 1e9; | ||
function Re(a) { | ||
var g, v, N, p = d.prototype = { constructor: d, toString: null, valueOf: null }, P = new d(1), E = 20, O = 4, _ = -7, M = 21, z = -1e7, q = 1e7, k = !1, Q = 1, G = 0, X = { | ||
var g, v, O, p = d.prototype = { constructor: d, toString: null, valueOf: null }, P = new d(1), E = 20, A = 4, q = -7, M = 21, _ = -1e7, F = 1e7, T = !1, C = 1, U = 0, V = { | ||
prefix: "", | ||
@@ -13,5 +13,5 @@ groupSize: 3, | ||
suffix: "" | ||
}, $ = "0123456789abcdefghijklmnopqrstuvwxyz", se = !0; | ||
}, G = "0123456789abcdefghijklmnopqrstuvwxyz", re = !0; | ||
function d(e, t) { | ||
var n, u, r, o, f, i, s, l, c = this; | ||
var n, u, r, o, l, i, s, f, c = this; | ||
if (!(c instanceof d)) | ||
@@ -21,3 +21,3 @@ return new d(e, t); | ||
if (e && e._isBigNumber === !0) { | ||
c.s = e.s, !e.c || e.e > q ? c.c = c.e = null : e.e < z ? c.c = [c.e = 0] : (c.e = e.e, c.c = e.c.slice()); | ||
c.s = e.s, !e.c || e.e > F ? c.c = c.e = null : e.e < _ ? c.c = [c.e = 0] : (c.e = e.e, c.c = e.c.slice()); | ||
return; | ||
@@ -27,60 +27,60 @@ } | ||
if (c.s = 1 / e < 0 ? (e = -e, -1) : 1, e === ~~e) { | ||
for (o = 0, f = e; f >= 10; f /= 10, o++) | ||
for (o = 0, l = e; l >= 10; l /= 10, o++) | ||
; | ||
o > q ? c.c = c.e = null : (c.e = o, c.c = [e]); | ||
o > F ? c.c = c.e = null : (c.e = o, c.c = [e]); | ||
return; | ||
} | ||
l = String(e); | ||
f = String(e); | ||
} else { | ||
if (!xe.test(l = String(e))) | ||
return N(c, l, i); | ||
c.s = l.charCodeAt(0) == 45 ? (l = l.slice(1), -1) : 1; | ||
if (!xe.test(f = String(e))) | ||
return O(c, f, i); | ||
c.s = f.charCodeAt(0) == 45 ? (f = f.slice(1), -1) : 1; | ||
} | ||
(o = l.indexOf(".")) > -1 && (l = l.replace(".", "")), (f = l.search(/e/i)) > 0 ? (o < 0 && (o = f), o += +l.slice(f + 1), l = l.substring(0, f)) : o < 0 && (o = l.length); | ||
(o = f.indexOf(".")) > -1 && (f = f.replace(".", "")), (l = f.search(/e/i)) > 0 ? (o < 0 && (o = l), o += +f.slice(l + 1), f = f.substring(0, l)) : o < 0 && (o = f.length); | ||
} else { | ||
if (F(t, 2, $.length, "Base"), t == 10 && se) | ||
return c = new d(e), W(c, E + c.e + 1, O); | ||
if (l = String(e), i = typeof e == "number") { | ||
if (D(t, 2, G.length, "Base"), t == 10 && re) | ||
return c = new d(e), y(c, E + c.e + 1, A); | ||
if (f = String(e), i = typeof e == "number") { | ||
if (e * 0 != 0) | ||
return N(c, l, i, t); | ||
if (c.s = 1 / e < 0 ? (l = l.slice(1), -1) : 1, d.DEBUG && l.replace(/^0\.0*|\./, "").length > 15) | ||
return O(c, f, i, t); | ||
if (c.s = 1 / e < 0 ? (f = f.slice(1), -1) : 1, d.DEBUG && f.replace(/^0\.0*|\./, "").length > 15) | ||
throw Error(Ee + e); | ||
} else | ||
c.s = l.charCodeAt(0) === 45 ? (l = l.slice(1), -1) : 1; | ||
for (n = $.slice(0, t), o = f = 0, s = l.length; f < s; f++) | ||
if (n.indexOf(u = l.charAt(f)) < 0) { | ||
c.s = f.charCodeAt(0) === 45 ? (f = f.slice(1), -1) : 1; | ||
for (n = G.slice(0, t), o = l = 0, s = f.length; l < s; l++) | ||
if (n.indexOf(u = f.charAt(l)) < 0) { | ||
if (u == ".") { | ||
if (f > o) { | ||
if (l > o) { | ||
o = s; | ||
continue; | ||
} | ||
} else if (!r && (l == l.toUpperCase() && (l = l.toLowerCase()) || l == l.toLowerCase() && (l = l.toUpperCase()))) { | ||
r = !0, f = -1, o = 0; | ||
} else if (!r && (f == f.toUpperCase() && (f = f.toLowerCase()) || f == f.toLowerCase() && (f = f.toUpperCase()))) { | ||
r = !0, l = -1, o = 0; | ||
continue; | ||
} | ||
return N(c, String(e), i, t); | ||
return O(c, String(e), i, t); | ||
} | ||
i = !1, l = v(l, t, 10, c.s), (o = l.indexOf(".")) > -1 ? l = l.replace(".", "") : o = l.length; | ||
i = !1, f = v(f, t, 10, c.s), (o = f.indexOf(".")) > -1 ? f = f.replace(".", "") : o = f.length; | ||
} | ||
for (f = 0; l.charCodeAt(f) === 48; f++) | ||
for (l = 0; f.charCodeAt(l) === 48; l++) | ||
; | ||
for (s = l.length; l.charCodeAt(--s) === 48; ) | ||
for (s = f.length; f.charCodeAt(--s) === 48; ) | ||
; | ||
if (l = l.slice(f, ++s)) { | ||
if (s -= f, i && d.DEBUG && s > 15 && (e > Ne || e !== J(e))) | ||
if (f = f.slice(l, ++s)) { | ||
if (s -= l, i && d.DEBUG && s > 15 && (e > Ne || e !== J(e))) | ||
throw Error(Ee + c.s * e); | ||
if ((o = o - f - 1) > q) | ||
if ((o = o - l - 1) > F) | ||
c.c = c.e = null; | ||
else if (o < z) | ||
else if (o < _) | ||
c.c = [c.e = 0]; | ||
else { | ||
if (c.e = o, c.c = [], f = (o + 1) % R, o < 0 && (f += R), f < s) { | ||
for (f && c.c.push(+l.slice(0, f)), s -= R; f < s; ) | ||
c.c.push(+l.slice(f, f += R)); | ||
f = R - (l = l.slice(f)).length; | ||
if (c.e = o, c.c = [], l = (o + 1) % S, o < 0 && (l += S), l < s) { | ||
for (l && c.c.push(+f.slice(0, l)), s -= S; l < s; ) | ||
c.c.push(+f.slice(l, l += S)); | ||
l = S - (f = f.slice(l)).length; | ||
} else | ||
f -= s; | ||
for (; f--; l += "0") | ||
l -= s; | ||
for (; l--; f += "0") | ||
; | ||
c.c.push(+l); | ||
c.c.push(+f); | ||
} | ||
@@ -94,9 +94,9 @@ } else | ||
if (typeof e == "object") { | ||
if (e.hasOwnProperty(t = "DECIMAL_PLACES") && (n = e[t], F(n, 0, U, t), E = n), e.hasOwnProperty(t = "ROUNDING_MODE") && (n = e[t], F(n, 0, 8, t), O = n), e.hasOwnProperty(t = "EXPONENTIAL_AT") && (n = e[t], n && n.pop ? (F(n[0], -U, 0, t), F(n[1], 0, U, t), _ = n[0], M = n[1]) : (F(n, -U, U, t), _ = -(M = n < 0 ? -n : n))), e.hasOwnProperty(t = "RANGE")) | ||
if (e.hasOwnProperty(t = "DECIMAL_PLACES") && (n = e[t], D(n, 0, W, t), E = n), e.hasOwnProperty(t = "ROUNDING_MODE") && (n = e[t], D(n, 0, 8, t), A = n), e.hasOwnProperty(t = "EXPONENTIAL_AT") && (n = e[t], n && n.pop ? (D(n[0], -W, 0, t), D(n[1], 0, W, t), q = n[0], M = n[1]) : (D(n, -W, W, t), q = -(M = n < 0 ? -n : n))), e.hasOwnProperty(t = "RANGE")) | ||
if (n = e[t], n && n.pop) | ||
F(n[0], -U, -1, t), F(n[1], 1, U, t), z = n[0], q = n[1]; | ||
else if (F(n, -U, U, t), n) | ||
z = -(q = n < 0 ? -n : n); | ||
D(n[0], -W, -1, t), D(n[1], 1, W, t), _ = n[0], F = n[1]; | ||
else if (D(n, -W, W, t), n) | ||
_ = -(F = n < 0 ? -n : n); | ||
else | ||
throw Error(b + t + " cannot be zero: " + n); | ||
throw Error(H + t + " cannot be zero: " + n); | ||
if (e.hasOwnProperty(t = "CRYPTO")) | ||
@@ -106,31 +106,31 @@ if (n = e[t], n === !!n) | ||
if (typeof crypto < "u" && crypto && (crypto.getRandomValues || crypto.randomBytes)) | ||
k = n; | ||
T = n; | ||
else | ||
throw k = !n, Error(b + "crypto unavailable"); | ||
throw T = !n, Error(H + "crypto unavailable"); | ||
else | ||
k = n; | ||
T = n; | ||
else | ||
throw Error(b + t + " not true or false: " + n); | ||
if (e.hasOwnProperty(t = "MODULO_MODE") && (n = e[t], F(n, 0, 9, t), Q = n), e.hasOwnProperty(t = "POW_PRECISION") && (n = e[t], F(n, 0, U, t), G = n), e.hasOwnProperty(t = "FORMAT")) | ||
throw Error(H + t + " not true or false: " + n); | ||
if (e.hasOwnProperty(t = "MODULO_MODE") && (n = e[t], D(n, 0, 9, t), C = n), e.hasOwnProperty(t = "POW_PRECISION") && (n = e[t], D(n, 0, W, t), U = n), e.hasOwnProperty(t = "FORMAT")) | ||
if (n = e[t], typeof n == "object") | ||
X = n; | ||
V = n; | ||
else | ||
throw Error(b + t + " not an object: " + n); | ||
throw Error(H + t + " not an object: " + n); | ||
if (e.hasOwnProperty(t = "ALPHABET")) | ||
if (n = e[t], typeof n == "string" && !/^.?$|[+\-.\s]|(.).*\1/.test(n)) | ||
se = n.slice(0, 10) == "0123456789", $ = n; | ||
re = n.slice(0, 10) == "0123456789", G = n; | ||
else | ||
throw Error(b + t + " invalid: " + n); | ||
throw Error(H + t + " invalid: " + n); | ||
} else | ||
throw Error(b + "Object expected: " + e); | ||
throw Error(H + "Object expected: " + e); | ||
return { | ||
DECIMAL_PLACES: E, | ||
ROUNDING_MODE: O, | ||
EXPONENTIAL_AT: [_, M], | ||
RANGE: [z, q], | ||
CRYPTO: k, | ||
MODULO_MODE: Q, | ||
POW_PRECISION: G, | ||
FORMAT: X, | ||
ALPHABET: $ | ||
ROUNDING_MODE: A, | ||
EXPONENTIAL_AT: [q, M], | ||
RANGE: [_, F], | ||
CRYPTO: T, | ||
MODULO_MODE: C, | ||
POW_PRECISION: U, | ||
FORMAT: V, | ||
ALPHABET: G | ||
}; | ||
@@ -145,3 +145,3 @@ }, d.isBigNumber = function(e) { | ||
if ({}.toString.call(u) == "[object Array]") { | ||
if ((o === 1 || o === -1) && r >= -U && r <= U && r === J(r)) { | ||
if ((o === 1 || o === -1) && r >= -W && r <= W && r === J(r)) { | ||
if (u[0] === 0) { | ||
@@ -152,3 +152,3 @@ if (r === 0 && u.length === 1) | ||
} | ||
if (t = (r + 1) % R, t < 1 && (t += R), String(u[0]).length == t) { | ||
if (t = (r + 1) % S, t < 1 && (t += S), String(u[0]).length == t) { | ||
for (t = 0; t < u.length; t++) | ||
@@ -163,3 +163,3 @@ if (n = u[t], n < 0 || n >= ee || n !== J(n)) | ||
return !0; | ||
throw Error(b + "Invalid BigNumber: " + e); | ||
throw Error(H + "Invalid BigNumber: " + e); | ||
}, d.maximum = d.max = function() { | ||
@@ -176,29 +176,29 @@ return Y(arguments, -1); | ||
return function(n) { | ||
var u, r, o, f, i, s = 0, l = [], c = new d(P); | ||
if (n == null ? n = E : F(n, 0, U), f = Ae(n / R), k) | ||
var u, r, o, l, i, s = 0, f = [], c = new d(P); | ||
if (n == null ? n = E : D(n, 0, W), l = Ae(n / S), T) | ||
if (crypto.getRandomValues) { | ||
for (u = crypto.getRandomValues(new Uint32Array(f *= 2)); s < f; ) | ||
i = u[s] * 131072 + (u[s + 1] >>> 11), i >= 9e15 ? (r = crypto.getRandomValues(new Uint32Array(2)), u[s] = r[0], u[s + 1] = r[1]) : (l.push(i % 1e14), s += 2); | ||
s = f / 2; | ||
for (u = crypto.getRandomValues(new Uint32Array(l *= 2)); s < l; ) | ||
i = u[s] * 131072 + (u[s + 1] >>> 11), i >= 9e15 ? (r = crypto.getRandomValues(new Uint32Array(2)), u[s] = r[0], u[s + 1] = r[1]) : (f.push(i % 1e14), s += 2); | ||
s = l / 2; | ||
} else if (crypto.randomBytes) { | ||
for (u = crypto.randomBytes(f *= 7); s < f; ) | ||
i = (u[s] & 31) * 281474976710656 + u[s + 1] * 1099511627776 + u[s + 2] * 4294967296 + u[s + 3] * 16777216 + (u[s + 4] << 16) + (u[s + 5] << 8) + u[s + 6], i >= 9e15 ? crypto.randomBytes(7).copy(u, s) : (l.push(i % 1e14), s += 7); | ||
s = f / 7; | ||
for (u = crypto.randomBytes(l *= 7); s < l; ) | ||
i = (u[s] & 31) * 281474976710656 + u[s + 1] * 1099511627776 + u[s + 2] * 4294967296 + u[s + 3] * 16777216 + (u[s + 4] << 16) + (u[s + 5] << 8) + u[s + 6], i >= 9e15 ? crypto.randomBytes(7).copy(u, s) : (f.push(i % 1e14), s += 7); | ||
s = l / 7; | ||
} else | ||
throw k = !1, Error(b + "crypto unavailable"); | ||
if (!k) | ||
for (; s < f; ) | ||
i = t(), i < 9e15 && (l[s++] = i % 1e14); | ||
for (f = l[--s], n %= R, f && n && (i = Oe[R - n], l[s] = J(f / i) * i); l[s] === 0; l.pop(), s--) | ||
throw T = !1, Error(H + "crypto unavailable"); | ||
if (!T) | ||
for (; s < l; ) | ||
i = t(), i < 9e15 && (f[s++] = i % 1e14); | ||
for (l = f[--s], n %= S, l && n && (i = Oe[S - n], f[s] = J(l / i) * i); f[s] === 0; f.pop(), s--) | ||
; | ||
if (s < 0) | ||
l = [o = 0]; | ||
f = [o = 0]; | ||
else { | ||
for (o = -1; l[0] === 0; l.splice(0, 1), o -= R) | ||
for (o = -1; f[0] === 0; f.splice(0, 1), o -= S) | ||
; | ||
for (s = 1, i = l[0]; i >= 10; i /= 10, s++) | ||
for (s = 1, i = f[0]; i >= 10; i /= 10, s++) | ||
; | ||
s < R && (o -= R - s); | ||
s < S && (o -= S - s); | ||
} | ||
return c.e = o, c.c = l, c; | ||
return c.e = o, c.c = f, c; | ||
}; | ||
@@ -212,32 +212,32 @@ }(), d.sum = function() { | ||
function t(n, u, r, o) { | ||
for (var f, i = [0], s, l = 0, c = n.length; l < c; ) { | ||
for (var l, i = [0], s, f = 0, c = n.length; f < c; ) { | ||
for (s = i.length; s--; i[s] *= u) | ||
; | ||
for (i[0] += o.indexOf(n.charAt(l++)), f = 0; f < i.length; f++) | ||
i[f] > r - 1 && (i[f + 1] == null && (i[f + 1] = 0), i[f + 1] += i[f] / r | 0, i[f] %= r); | ||
for (i[0] += o.indexOf(n.charAt(f++)), l = 0; l < i.length; l++) | ||
i[l] > r - 1 && (i[l + 1] == null && (i[l + 1] = 0), i[l + 1] += i[l] / r | 0, i[l] %= r); | ||
} | ||
return i.reverse(); | ||
} | ||
return function(n, u, r, o, f) { | ||
var i, s, l, c, h, m, w, B, D = n.indexOf("."), T = E, x = O; | ||
for (D >= 0 && (c = G, G = 0, n = n.replace(".", ""), B = new d(u), m = B.pow(n.length - D), G = c, B.c = t( | ||
ie(K(m.c), m.e, "0"), | ||
return function(n, u, r, o, l) { | ||
var i, s, f, c, h, w, N, B, L = n.indexOf("."), k = E, x = A; | ||
for (L >= 0 && (c = U, U = 0, n = n.replace(".", ""), B = new d(u), w = B.pow(n.length - L), U = c, B.c = t( | ||
ie(Z(w.c), w.e, "0"), | ||
10, | ||
r, | ||
e | ||
), B.e = B.c.length), w = t(n, u, r, f ? (i = $, e) : (i = e, $)), l = c = w.length; w[--c] == 0; w.pop()) | ||
), B.e = B.c.length), N = t(n, u, r, l ? (i = G, e) : (i = e, G)), f = c = N.length; N[--c] == 0; N.pop()) | ||
; | ||
if (!w[0]) | ||
if (!N[0]) | ||
return i.charAt(0); | ||
if (D < 0 ? --l : (m.c = w, m.e = l, m.s = o, m = g(m, B, T, x, r), w = m.c, h = m.r, l = m.e), s = l + T + 1, D = w[s], c = r / 2, h = h || s < 0 || w[s + 1] != null, h = x < 4 ? (D != null || h) && (x == 0 || x == (m.s < 0 ? 3 : 2)) : D > c || D == c && (x == 4 || h || x == 6 && w[s - 1] & 1 || x == (m.s < 0 ? 8 : 7)), s < 1 || !w[0]) | ||
n = h ? ie(i.charAt(1), -T, i.charAt(0)) : i.charAt(0); | ||
if (L < 0 ? --f : (w.c = N, w.e = f, w.s = o, w = g(w, B, k, x, r), N = w.c, h = w.r, f = w.e), s = f + k + 1, L = N[s], c = r / 2, h = h || s < 0 || N[s + 1] != null, h = x < 4 ? (L != null || h) && (x == 0 || x == (w.s < 0 ? 3 : 2)) : L > c || L == c && (x == 4 || h || x == 6 && N[s - 1] & 1 || x == (w.s < 0 ? 8 : 7)), s < 1 || !N[0]) | ||
n = h ? ie(i.charAt(1), -k, i.charAt(0)) : i.charAt(0); | ||
else { | ||
if (w.length = s, h) | ||
for (--r; ++w[--s] > r; ) | ||
w[s] = 0, s || (++l, w = [1].concat(w)); | ||
for (c = w.length; !w[--c]; ) | ||
if (N.length = s, h) | ||
for (--r; ++N[--s] > r; ) | ||
N[s] = 0, s || (++f, N = [1].concat(N)); | ||
for (c = N.length; !N[--c]; ) | ||
; | ||
for (D = 0, n = ""; D <= c; n += i.charAt(w[D++])) | ||
for (L = 0, n = ""; L <= c; n += i.charAt(N[L++])) | ||
; | ||
n = ie(n, l, i.charAt(0)); | ||
n = ie(n, f, i.charAt(0)); | ||
} | ||
@@ -248,11 +248,11 @@ return n; | ||
function e(u, r, o) { | ||
var f, i, s, l, c = 0, h = u.length, m = r % oe, w = r / oe | 0; | ||
var l, i, s, f, c = 0, h = u.length, w = r % se, N = r / se | 0; | ||
for (u = u.slice(); h--; ) | ||
s = u[h] % oe, l = u[h] / oe | 0, f = w * s + l * m, i = m * s + f % oe * oe + c, c = (i / o | 0) + (f / oe | 0) + w * l, u[h] = i % o; | ||
s = u[h] % se, f = u[h] / se | 0, l = N * s + f * w, i = w * s + l % se * se + c, c = (i / o | 0) + (l / se | 0) + N * f, u[h] = i % o; | ||
return c && (u = [c].concat(u)), u; | ||
} | ||
function t(u, r, o, f) { | ||
function t(u, r, o, l) { | ||
var i, s; | ||
if (o != f) | ||
s = o > f ? 1 : -1; | ||
if (o != l) | ||
s = o > l ? 1 : -1; | ||
else | ||
@@ -266,62 +266,62 @@ for (i = s = 0; i < o; i++) | ||
} | ||
function n(u, r, o, f) { | ||
function n(u, r, o, l) { | ||
for (var i = 0; o--; ) | ||
u[o] -= i, i = u[o] < r[o] ? 1 : 0, u[o] = i * f + u[o] - r[o]; | ||
u[o] -= i, i = u[o] < r[o] ? 1 : 0, u[o] = i * l + u[o] - r[o]; | ||
for (; !u[0] && u.length > 1; u.splice(0, 1)) | ||
; | ||
} | ||
return function(u, r, o, f, i) { | ||
var s, l, c, h, m, w, B, D, T, x, I, y, he, me, we, te, le, Z = u.s == r.s ? 1 : -1, V = u.c, L = r.c; | ||
if (!V || !V[0] || !L || !L[0]) | ||
return function(u, r, o, l, i) { | ||
var s, f, c, h, w, N, B, L, k, x, I, b, he, me, we, te, ae, K = u.s == r.s ? 1 : -1, $ = u.c, z = r.c; | ||
if (!$ || !$[0] || !z || !z[0]) | ||
return new d( | ||
// Return NaN if either NaN, or both Infinity or 0. | ||
!u.s || !r.s || (V ? L && V[0] == L[0] : !L) ? NaN : ( | ||
!u.s || !r.s || ($ ? z && $[0] == z[0] : !z) ? NaN : ( | ||
// Return ±0 if x is ±0 or y is ±Infinity, or return ±Infinity as y is ±0. | ||
V && V[0] == 0 || !L ? Z * 0 : Z / 0 | ||
$ && $[0] == 0 || !z ? K * 0 : K / 0 | ||
) | ||
); | ||
for (D = new d(Z), T = D.c = [], l = u.e - r.e, Z = o + l + 1, i || (i = ee, l = j(u.e / R) - j(r.e / R), Z = Z / R | 0), c = 0; L[c] == (V[c] || 0); c++) | ||
for (L = new d(K), k = L.c = [], f = u.e - r.e, K = o + f + 1, i || (i = ee, f = j(u.e / S) - j(r.e / S), K = K / S | 0), c = 0; z[c] == ($[c] || 0); c++) | ||
; | ||
if (L[c] > (V[c] || 0) && l--, Z < 0) | ||
T.push(1), h = !0; | ||
if (z[c] > ($[c] || 0) && f--, K < 0) | ||
k.push(1), h = !0; | ||
else { | ||
for (me = V.length, te = L.length, c = 0, Z += 2, m = J(i / (L[0] + 1)), m > 1 && (L = e(L, m, i), V = e(V, m, i), te = L.length, me = V.length), he = te, x = V.slice(0, te), I = x.length; I < te; x[I++] = 0) | ||
for (me = $.length, te = z.length, c = 0, K += 2, w = J(i / (z[0] + 1)), w > 1 && (z = e(z, w, i), $ = e($, w, i), te = z.length, me = $.length), he = te, x = $.slice(0, te), I = x.length; I < te; x[I++] = 0) | ||
; | ||
le = L.slice(), le = [0].concat(le), we = L[0], L[1] >= i / 2 && we++; | ||
ae = z.slice(), ae = [0].concat(ae), we = z[0], z[1] >= i / 2 && we++; | ||
do { | ||
if (m = 0, s = t(L, x, te, I), s < 0) { | ||
if (y = x[0], te != I && (y = y * i + (x[1] || 0)), m = J(y / we), m > 1) | ||
for (m >= i && (m = i - 1), w = e(L, m, i), B = w.length, I = x.length; t(w, x, B, I) == 1; ) | ||
m--, n(w, te < B ? le : L, B, i), B = w.length, s = 1; | ||
if (w = 0, s = t(z, x, te, I), s < 0) { | ||
if (b = x[0], te != I && (b = b * i + (x[1] || 0)), w = J(b / we), w > 1) | ||
for (w >= i && (w = i - 1), N = e(z, w, i), B = N.length, I = x.length; t(N, x, B, I) == 1; ) | ||
w--, n(N, te < B ? ae : z, B, i), B = N.length, s = 1; | ||
else | ||
m == 0 && (s = m = 1), w = L.slice(), B = w.length; | ||
if (B < I && (w = [0].concat(w)), n(x, w, I, i), I = x.length, s == -1) | ||
for (; t(L, x, te, I) < 1; ) | ||
m++, n(x, te < I ? le : L, I, i), I = x.length; | ||
w == 0 && (s = w = 1), N = z.slice(), B = N.length; | ||
if (B < I && (N = [0].concat(N)), n(x, N, I, i), I = x.length, s == -1) | ||
for (; t(z, x, te, I) < 1; ) | ||
w++, n(x, te < I ? ae : z, I, i), I = x.length; | ||
} else | ||
s === 0 && (m++, x = [0]); | ||
T[c++] = m, x[0] ? x[I++] = V[he] || 0 : (x = [V[he]], I = 1); | ||
} while ((he++ < me || x[0] != null) && Z--); | ||
h = x[0] != null, T[0] || T.splice(0, 1); | ||
s === 0 && (w++, x = [0]); | ||
k[c++] = w, x[0] ? x[I++] = $[he] || 0 : (x = [$[he]], I = 1); | ||
} while ((he++ < me || x[0] != null) && K--); | ||
h = x[0] != null, k[0] || k.splice(0, 1); | ||
} | ||
if (i == ee) { | ||
for (c = 1, Z = T[0]; Z >= 10; Z /= 10, c++) | ||
for (c = 1, K = k[0]; K >= 10; K /= 10, c++) | ||
; | ||
W(D, o + (D.e = c + l * R - 1) + 1, f, h); | ||
y(L, o + (L.e = c + f * S - 1) + 1, l, h); | ||
} else | ||
D.e = l, D.r = +h; | ||
return D; | ||
L.e = f, L.r = +h; | ||
return L; | ||
}; | ||
}(); | ||
function H(e, t, n, u) { | ||
var r, o, f, i, s; | ||
if (n == null ? n = O : F(n, 0, 8), !e.c) | ||
function X(e, t, n, u) { | ||
var r, o, l, i, s; | ||
if (n == null ? n = A : D(n, 0, 8), !e.c) | ||
return e.toString(); | ||
if (r = e.c[0], f = e.e, t == null) | ||
s = K(e.c), s = u == 1 || u == 2 && (f <= _ || f >= M) ? pe(s, f) : ie(s, f, "0"); | ||
else if (e = W(new d(e), t, n), o = e.e, s = K(e.c), i = s.length, u == 1 || u == 2 && (t <= o || o <= _)) { | ||
if (r = e.c[0], l = e.e, t == null) | ||
s = Z(e.c), s = u == 1 || u == 2 && (l <= q || l >= M) ? pe(s, l) : ie(s, l, "0"); | ||
else if (e = y(new d(e), t, n), o = e.e, s = Z(e.c), i = s.length, u == 1 || u == 2 && (t <= o || o <= q)) { | ||
for (; i < t; s += "0", i++) | ||
; | ||
s = pe(s, o); | ||
} else if (t -= f, s = ie(s, o, "0"), o + 1 > i) { | ||
} else if (t -= l, s = ie(s, o, "0"), o + 1 > i) { | ||
if (--t > 0) | ||
@@ -337,3 +337,3 @@ for (s += "."; t--; s += "0") | ||
for (var n, u, r = 1, o = new d(e[0]); r < e.length; r++) | ||
u = new d(e[r]), (!u.s || (n = ce(o, u)) === t || n === 0 && o.s === t) && (o = u); | ||
u = new d(e[r]), (!u.s || (n = ue(o, u)) === t || n === 0 && o.s === t) && (o = u); | ||
return o; | ||
@@ -346,17 +346,17 @@ } | ||
; | ||
return (n = u + n * R - 1) > q ? e.c = e.e = null : n < z ? e.c = [e.e = 0] : (e.e = n, e.c = t), e; | ||
return (n = u + n * S - 1) > F ? e.c = e.e = null : n < _ ? e.c = [e.e = 0] : (e.e = n, e.c = t), e; | ||
} | ||
N = /* @__PURE__ */ function() { | ||
O = /* @__PURE__ */ function() { | ||
var e = /^(-?)0([xbo])(?=\w[\w.]*$)/i, t = /^([^.]+)\.$/, n = /^\.([^.]+)$/, u = /^-?(Infinity|NaN)$/, r = /^\s*\+(?=[\w.])|^\s+|\s+$/g; | ||
return function(o, f, i, s) { | ||
var l, c = i ? f : f.replace(r, ""); | ||
return function(o, l, i, s) { | ||
var f, c = i ? l : l.replace(r, ""); | ||
if (u.test(c)) | ||
o.s = isNaN(c) ? null : c < 0 ? -1 : 1; | ||
else { | ||
if (!i && (c = c.replace(e, function(h, m, w) { | ||
return l = (w = w.toLowerCase()) == "x" ? 16 : w == "b" ? 2 : 8, !s || s == l ? m : h; | ||
}), s && (l = s, c = c.replace(t, "$1").replace(n, "0.$1")), f != c)) | ||
return new d(c, l); | ||
if (!i && (c = c.replace(e, function(h, w, N) { | ||
return f = (N = N.toLowerCase()) == "x" ? 16 : N == "b" ? 2 : 8, !s || s == f ? w : h; | ||
}), s && (f = s, c = c.replace(t, "$1").replace(n, "0.$1")), l != c)) | ||
return new d(c, f); | ||
if (d.DEBUG) | ||
throw Error(b + "Not a" + (s ? " base " + s : "") + " number: " + f); | ||
throw Error(H + "Not a" + (s ? " base " + s : "") + " number: " + l); | ||
o.s = null; | ||
@@ -367,4 +367,4 @@ } | ||
}(); | ||
function W(e, t, n, u) { | ||
var r, o, f, i, s, l, c, h = e.c, m = Oe; | ||
function y(e, t, n, u) { | ||
var r, o, l, i, s, f, c, h = e.c, w = Oe; | ||
if (h) { | ||
@@ -375,14 +375,14 @@ e: { | ||
if (o = t - r, o < 0) | ||
o += R, f = t, s = h[l = 0], c = J(s / m[r - f - 1] % 10); | ||
else if (l = Ae((o + 1) / R), l >= h.length) | ||
o += S, l = t, s = h[f = 0], c = J(s / w[r - l - 1] % 10); | ||
else if (f = Ae((o + 1) / S), f >= h.length) | ||
if (u) { | ||
for (; h.length <= l; h.push(0)) | ||
for (; h.length <= f; h.push(0)) | ||
; | ||
s = c = 0, r = 1, o %= R, f = o - R + 1; | ||
s = c = 0, r = 1, o %= S, l = o - S + 1; | ||
} else | ||
break e; | ||
else { | ||
for (s = i = h[l], r = 1; i >= 10; i /= 10, r++) | ||
for (s = i = h[f], r = 1; i >= 10; i /= 10, r++) | ||
; | ||
o %= R, f = o - R + r, c = f < 0 ? 0 : J(s / m[r - f - 1] % 10); | ||
o %= S, l = o - S + r, c = l < 0 ? 0 : J(s / w[r - l - 1] % 10); | ||
} | ||
@@ -392,11 +392,11 @@ if (u = u || t < 0 || // Are there any non-zero digits after the rounding digit? | ||
// of the digit at j, e.g. if n is 908714 and j is 2, the expression gives 714. | ||
h[l + 1] != null || (f < 0 ? s : s % m[r - f - 1]), u = n < 4 ? (c || u) && (n == 0 || n == (e.s < 0 ? 3 : 2)) : c > 5 || c == 5 && (n == 4 || u || n == 6 && // Check whether the digit to the left of the rounding digit is odd. | ||
(o > 0 ? f > 0 ? s / m[r - f] : 0 : h[l - 1]) % 10 & 1 || n == (e.s < 0 ? 8 : 7)), t < 1 || !h[0]) | ||
return h.length = 0, u ? (t -= e.e + 1, h[0] = m[(R - t % R) % R], e.e = -t || 0) : h[0] = e.e = 0, e; | ||
if (o == 0 ? (h.length = l, i = 1, l--) : (h.length = l + 1, i = m[R - o], h[l] = f > 0 ? J(s / m[r - f] % m[f]) * i : 0), u) | ||
h[f + 1] != null || (l < 0 ? s : s % w[r - l - 1]), u = n < 4 ? (c || u) && (n == 0 || n == (e.s < 0 ? 3 : 2)) : c > 5 || c == 5 && (n == 4 || u || n == 6 && // Check whether the digit to the left of the rounding digit is odd. | ||
(o > 0 ? l > 0 ? s / w[r - l] : 0 : h[f - 1]) % 10 & 1 || n == (e.s < 0 ? 8 : 7)), t < 1 || !h[0]) | ||
return h.length = 0, u ? (t -= e.e + 1, h[0] = w[(S - t % S) % S], e.e = -t || 0) : h[0] = e.e = 0, e; | ||
if (o == 0 ? (h.length = f, i = 1, f--) : (h.length = f + 1, i = w[S - o], h[f] = l > 0 ? J(s / w[r - l] % w[l]) * i : 0), u) | ||
for (; ; ) | ||
if (l == 0) { | ||
for (o = 1, f = h[0]; f >= 10; f /= 10, o++) | ||
if (f == 0) { | ||
for (o = 1, l = h[0]; l >= 10; l /= 10, o++) | ||
; | ||
for (f = h[0] += i, i = 1; f >= 10; f /= 10, i++) | ||
for (l = h[0] += i, i = 1; l >= 10; l /= 10, i++) | ||
; | ||
@@ -406,5 +406,5 @@ o != i && (e.e++, h[0] == ee && (h[0] = 1)); | ||
} else { | ||
if (h[l] += i, h[l] != ee) | ||
if (h[f] += i, h[f] != ee) | ||
break; | ||
h[l--] = 0, i = 1; | ||
h[f--] = 0, i = 1; | ||
} | ||
@@ -414,9 +414,9 @@ for (o = h.length; h[--o] === 0; h.pop()) | ||
} | ||
e.e > q ? e.c = e.e = null : e.e < z && (e.c = [e.e = 0]); | ||
e.e > F ? e.c = e.e = null : e.e < _ && (e.c = [e.e = 0]); | ||
} | ||
return e; | ||
} | ||
function C(e) { | ||
function Q(e) { | ||
var t, n = e.e; | ||
return n === null ? e.toString() : (t = K(e.c), t = n <= _ || n >= M ? pe(t, n) : ie(t, n, "0"), e.s < 0 ? "-" + t : t); | ||
return n === null ? e.toString() : (t = Z(e.c), t = n <= q || n >= M ? pe(t, n) : ie(t, n, "0"), e.s < 0 ? "-" + t : t); | ||
} | ||
@@ -427,10 +427,10 @@ return p.absoluteValue = p.abs = function() { | ||
}, p.comparedTo = function(e, t) { | ||
return ce(this, new d(e, t)); | ||
return ue(this, new d(e, t)); | ||
}, p.decimalPlaces = p.dp = function(e, t) { | ||
var n, u, r, o = this; | ||
if (e != null) | ||
return F(e, 0, U), t == null ? t = O : F(t, 0, 8), W(new d(o), e + o.e + 1, t); | ||
return D(e, 0, W), t == null ? t = A : D(t, 0, 8), y(new d(o), e + o.e + 1, t); | ||
if (!(n = o.c)) | ||
return null; | ||
if (u = ((r = n.length - 1) - j(this.e / R)) * R, r = n[r]) | ||
if (u = ((r = n.length - 1) - j(this.e / S)) * S, r = n[r]) | ||
for (; r % 10 == 0; r /= 10, u--) | ||
@@ -440,11 +440,11 @@ ; | ||
}, p.dividedBy = p.div = function(e, t) { | ||
return g(this, new d(e, t), E, O); | ||
return g(this, new d(e, t), E, A); | ||
}, p.dividedToIntegerBy = p.idiv = function(e, t) { | ||
return g(this, new d(e, t), 0, 1); | ||
}, p.exponentiatedBy = p.pow = function(e, t) { | ||
var n, u, r, o, f, i, s, l, c, h = this; | ||
var n, u, r, o, l, i, s, f, c, h = this; | ||
if (e = new d(e), e.c && !e.isInteger()) | ||
throw Error(b + "Exponent not an integer: " + C(e)); | ||
throw Error(H + "Exponent not an integer: " + Q(e)); | ||
if (t != null && (t = new d(t)), i = e.e > 14, !h.c || !h.c[0] || h.c[0] == 1 && !h.e && h.c.length == 1 || !e.c || !e.c[0]) | ||
return c = new d(Math.pow(+C(h), i ? e.s * (2 - ge(e)) : +C(e))), t ? c.mod(t) : c; | ||
return c = new d(Math.pow(+Q(h), i ? e.s * (2 - ge(e)) : +Q(e))), t ? c.mod(t) : c; | ||
if (s = e.s < 0, t) { | ||
@@ -457,6 +457,6 @@ if (t.c ? !t.c[0] : !t.s) | ||
return o = h.s < 0 && ge(e) ? -0 : 0, h.e > -1 && (o = 1 / o), new d(s ? 1 / o : o); | ||
G && (o = Ae(G / R + 2)); | ||
U && (o = Ae(U / S + 2)); | ||
} | ||
for (i ? (n = new d(0.5), s && (e.s = 1), l = ge(e)) : (r = Math.abs(+C(e)), l = r % 2), c = new d(P); ; ) { | ||
if (l) { | ||
for (i ? (n = new d(0.5), s && (e.s = 1), f = ge(e)) : (r = Math.abs(+Q(e)), f = r % 2), c = new d(P); ; ) { | ||
if (f) { | ||
if (c = c.times(h), !c.c) | ||
@@ -469,30 +469,30 @@ break; | ||
break; | ||
l = r % 2; | ||
} else if (e = e.times(n), W(e, e.e + 1, 1), e.e > 14) | ||
l = ge(e); | ||
f = r % 2; | ||
} else if (e = e.times(n), y(e, e.e + 1, 1), e.e > 14) | ||
f = ge(e); | ||
else { | ||
if (r = +C(e), r === 0) | ||
if (r = +Q(e), r === 0) | ||
break; | ||
l = r % 2; | ||
f = r % 2; | ||
} | ||
h = h.times(h), o ? h.c && h.c.length > o && (h.c.length = o) : u && (h = h.mod(t)); | ||
} | ||
return u ? c : (s && (c = P.div(c)), t ? c.mod(t) : o ? W(c, G, O, f) : c); | ||
return u ? c : (s && (c = P.div(c)), t ? c.mod(t) : o ? y(c, U, A, l) : c); | ||
}, p.integerValue = function(e) { | ||
var t = new d(this); | ||
return e == null ? e = O : F(e, 0, 8), W(t, t.e + 1, e); | ||
return e == null ? e = A : D(e, 0, 8), y(t, t.e + 1, e); | ||
}, p.isEqualTo = p.eq = function(e, t) { | ||
return ce(this, new d(e, t)) === 0; | ||
return ue(this, new d(e, t)) === 0; | ||
}, p.isFinite = function() { | ||
return !!this.c; | ||
}, p.isGreaterThan = p.gt = function(e, t) { | ||
return ce(this, new d(e, t)) > 0; | ||
return ue(this, new d(e, t)) > 0; | ||
}, p.isGreaterThanOrEqualTo = p.gte = function(e, t) { | ||
return (t = ce(this, new d(e, t))) === 1 || t === 0; | ||
return (t = ue(this, new d(e, t))) === 1 || t === 0; | ||
}, p.isInteger = function() { | ||
return !!this.c && j(this.e / R) > this.c.length - 2; | ||
return !!this.c && j(this.e / S) > this.c.length - 2; | ||
}, p.isLessThan = p.lt = function(e, t) { | ||
return ce(this, new d(e, t)) < 0; | ||
return ue(this, new d(e, t)) < 0; | ||
}, p.isLessThanOrEqualTo = p.lte = function(e, t) { | ||
return (t = ce(this, new d(e, t))) === -1 || t === 0; | ||
return (t = ue(this, new d(e, t))) === -1 || t === 0; | ||
}, p.isNaN = function() { | ||
@@ -507,19 +507,19 @@ return !this.s; | ||
}, p.minus = function(e, t) { | ||
var n, u, r, o, f = this, i = f.s; | ||
var n, u, r, o, l = this, i = l.s; | ||
if (e = new d(e, t), t = e.s, !i || !t) | ||
return new d(NaN); | ||
if (i != t) | ||
return e.s = -t, f.plus(e); | ||
var s = f.e / R, l = e.e / R, c = f.c, h = e.c; | ||
if (!s || !l) { | ||
return e.s = -t, l.plus(e); | ||
var s = l.e / S, f = e.e / S, c = l.c, h = e.c; | ||
if (!s || !f) { | ||
if (!c || !h) | ||
return c ? (e.s = -t, e) : new d(h ? f : NaN); | ||
return c ? (e.s = -t, e) : new d(h ? l : NaN); | ||
if (!c[0] || !h[0]) | ||
return h[0] ? (e.s = -t, e) : new d(c[0] ? f : ( | ||
return h[0] ? (e.s = -t, e) : new d(c[0] ? l : ( | ||
// IEEE 754 (2008) 6.3: n - n = -0 when rounding to -Infinity | ||
O == 3 ? -0 : 0 | ||
A == 3 ? -0 : 0 | ||
)); | ||
} | ||
if (s = j(s), l = j(l), c = c.slice(), i = s - l) { | ||
for ((o = i < 0) ? (i = -i, r = c) : (l = s, r = h), r.reverse(), t = i; t--; r.push(0)) | ||
if (s = j(s), f = j(f), c = c.slice(), i = s - f) { | ||
for ((o = i < 0) ? (i = -i, r = c) : (f = s, r = h), r.reverse(), t = i; t--; r.push(0)) | ||
; | ||
@@ -544,17 +544,17 @@ r.reverse(); | ||
} | ||
for (; c[0] == 0; c.splice(0, 1), --l) | ||
for (; c[0] == 0; c.splice(0, 1), --f) | ||
; | ||
return c[0] ? ne(e, c, l) : (e.s = O == 3 ? -1 : 1, e.c = [e.e = 0], e); | ||
return c[0] ? ne(e, c, f) : (e.s = A == 3 ? -1 : 1, e.c = [e.e = 0], e); | ||
}, p.modulo = p.mod = function(e, t) { | ||
var n, u, r = this; | ||
return e = new d(e, t), !r.c || !e.s || e.c && !e.c[0] ? new d(NaN) : !e.c || r.c && !r.c[0] ? new d(r) : (Q == 9 ? (u = e.s, e.s = 1, n = g(r, e, 0, 3), e.s = u, n.s *= u) : n = g(r, e, 0, Q), e = r.minus(n.times(e)), !e.c[0] && Q == 1 && (e.s = r.s), e); | ||
return e = new d(e, t), !r.c || !e.s || e.c && !e.c[0] ? new d(NaN) : !e.c || r.c && !r.c[0] ? new d(r) : (C == 9 ? (u = e.s, e.s = 1, n = g(r, e, 0, 3), e.s = u, n.s *= u) : n = g(r, e, 0, C), e = r.minus(n.times(e)), !e.c[0] && C == 1 && (e.s = r.s), e); | ||
}, p.multipliedBy = p.times = function(e, t) { | ||
var n, u, r, o, f, i, s, l, c, h, m, w, B, D, T, x = this, I = x.c, y = (e = new d(e, t)).c; | ||
if (!I || !y || !I[0] || !y[0]) | ||
return !x.s || !e.s || I && !I[0] && !y || y && !y[0] && !I ? e.c = e.e = e.s = null : (e.s *= x.s, !I || !y ? e.c = e.e = null : (e.c = [0], e.e = 0)), e; | ||
for (u = j(x.e / R) + j(e.e / R), e.s *= x.s, s = I.length, h = y.length, s < h && (B = I, I = y, y = B, r = s, s = h, h = r), r = s + h, B = []; r--; B.push(0)) | ||
var n, u, r, o, l, i, s, f, c, h, w, N, B, L, k, x = this, I = x.c, b = (e = new d(e, t)).c; | ||
if (!I || !b || !I[0] || !b[0]) | ||
return !x.s || !e.s || I && !I[0] && !b || b && !b[0] && !I ? e.c = e.e = e.s = null : (e.s *= x.s, !I || !b ? e.c = e.e = null : (e.c = [0], e.e = 0)), e; | ||
for (u = j(x.e / S) + j(e.e / S), e.s *= x.s, s = I.length, h = b.length, s < h && (B = I, I = b, b = B, r = s, s = h, h = r), r = s + h, B = []; r--; B.push(0)) | ||
; | ||
for (D = ee, T = oe, r = h; --r >= 0; ) { | ||
for (n = 0, m = y[r] % T, w = y[r] / T | 0, f = s, o = r + f; o > r; ) | ||
l = I[--f] % T, c = I[f] / T | 0, i = w * l + c * m, l = m * l + i % T * T + B[o] + n, n = (l / D | 0) + (i / T | 0) + w * c, B[o--] = l % D; | ||
for (L = ee, k = se, r = h; --r >= 0; ) { | ||
for (n = 0, w = b[r] % k, N = b[r] / k | 0, l = s, o = r + l; o > r; ) | ||
f = I[--l] % k, c = I[l] / k | 0, i = N * f + c * w, f = w * f + i % k * k + B[o] + n, n = (f / L | 0) + (i / k | 0) + N * c, B[o--] = f % L; | ||
B[o] = n; | ||
@@ -572,4 +572,4 @@ } | ||
return e.s = -t, u.minus(e); | ||
var o = u.e / R, f = e.e / R, i = u.c, s = e.c; | ||
if (!o || !f) { | ||
var o = u.e / S, l = e.e / S, i = u.c, s = e.c; | ||
if (!o || !l) { | ||
if (!i || !s) | ||
@@ -580,4 +580,4 @@ return new d(r / 0); | ||
} | ||
if (o = j(o), f = j(f), i = i.slice(), r = o - f) { | ||
for (r > 0 ? (f = o, n = s) : (r = -r, n = i), n.reverse(); r--; n.push(0)) | ||
if (o = j(o), l = j(l), i = i.slice(), r = o - l) { | ||
for (r > 0 ? (l = o, n = s) : (r = -r, n = i), n.reverse(); r--; n.push(0)) | ||
; | ||
@@ -588,10 +588,10 @@ n.reverse(); | ||
r = (i[--t] = i[t] + s[t] + r) / ee | 0, i[t] = ee === i[t] ? 0 : i[t] % ee; | ||
return r && (i = [r].concat(i), ++f), ne(e, i, f); | ||
return r && (i = [r].concat(i), ++l), ne(e, i, l); | ||
}, p.precision = p.sd = function(e, t) { | ||
var n, u, r, o = this; | ||
if (e != null && e !== !!e) | ||
return F(e, 1, U), t == null ? t = O : F(t, 0, 8), W(new d(o), e, t); | ||
return D(e, 1, W), t == null ? t = A : D(t, 0, 8), y(new d(o), e, t); | ||
if (!(n = o.c)) | ||
return null; | ||
if (r = n.length - 1, u = r * R + 1, r = n[r]) { | ||
if (r = n.length - 1, u = r * S + 1, r = n[r]) { | ||
for (; r % 10 == 0; r /= 10, u--) | ||
@@ -604,38 +604,38 @@ ; | ||
}, p.shiftedBy = function(e) { | ||
return F(e, -Ne, Ne), this.times("1e" + e); | ||
return D(e, -Ne, Ne), this.times("1e" + e); | ||
}, p.squareRoot = p.sqrt = function() { | ||
var e, t, n, u, r, o = this, f = o.c, i = o.s, s = o.e, l = E + 4, c = new d("0.5"); | ||
if (i !== 1 || !f || !f[0]) | ||
return new d(!i || i < 0 && (!f || f[0]) ? NaN : f ? o : 1 / 0); | ||
if (i = Math.sqrt(+C(o)), i == 0 || i == 1 / 0 ? (t = K(f), (t.length + s) % 2 == 0 && (t += "0"), i = Math.sqrt(+t), s = j((s + 1) / 2) - (s < 0 || s % 2), i == 1 / 0 ? t = "5e" + s : (t = i.toExponential(), t = t.slice(0, t.indexOf("e") + 1) + s), n = new d(t)) : n = new d(i + ""), n.c[0]) { | ||
for (s = n.e, i = s + l, i < 3 && (i = 0); ; ) | ||
if (r = n, n = c.times(r.plus(g(o, r, l, 1))), K(r.c).slice(0, i) === (t = K(n.c)).slice(0, i)) | ||
var e, t, n, u, r, o = this, l = o.c, i = o.s, s = o.e, f = E + 4, c = new d("0.5"); | ||
if (i !== 1 || !l || !l[0]) | ||
return new d(!i || i < 0 && (!l || l[0]) ? NaN : l ? o : 1 / 0); | ||
if (i = Math.sqrt(+Q(o)), i == 0 || i == 1 / 0 ? (t = Z(l), (t.length + s) % 2 == 0 && (t += "0"), i = Math.sqrt(+t), s = j((s + 1) / 2) - (s < 0 || s % 2), i == 1 / 0 ? t = "5e" + s : (t = i.toExponential(), t = t.slice(0, t.indexOf("e") + 1) + s), n = new d(t)) : n = new d(i + ""), n.c[0]) { | ||
for (s = n.e, i = s + f, i < 3 && (i = 0); ; ) | ||
if (r = n, n = c.times(r.plus(g(o, r, f, 1))), Z(r.c).slice(0, i) === (t = Z(n.c)).slice(0, i)) | ||
if (n.e < s && --i, t = t.slice(i - 3, i + 1), t == "9999" || !u && t == "4999") { | ||
if (!u && (W(r, r.e + E + 2, 0), r.times(r).eq(o))) { | ||
if (!u && (y(r, r.e + E + 2, 0), r.times(r).eq(o))) { | ||
n = r; | ||
break; | ||
} | ||
l += 4, i += 4, u = 1; | ||
f += 4, i += 4, u = 1; | ||
} else { | ||
(!+t || !+t.slice(1) && t.charAt(0) == "5") && (W(n, n.e + E + 2, 1), e = !n.times(n).eq(o)); | ||
(!+t || !+t.slice(1) && t.charAt(0) == "5") && (y(n, n.e + E + 2, 1), e = !n.times(n).eq(o)); | ||
break; | ||
} | ||
} | ||
return W(n, n.e + E + 1, O, e); | ||
return y(n, n.e + E + 1, A, e); | ||
}, p.toExponential = function(e, t) { | ||
return e != null && (F(e, 0, U), e++), H(this, e, t, 1); | ||
return e != null && (D(e, 0, W), e++), X(this, e, t, 1); | ||
}, p.toFixed = function(e, t) { | ||
return e != null && (F(e, 0, U), e = e + this.e + 1), H(this, e, t); | ||
return e != null && (D(e, 0, W), e = e + this.e + 1), X(this, e, t); | ||
}, p.toFormat = function(e, t, n) { | ||
var u, r = this; | ||
if (n == null) | ||
e != null && t && typeof t == "object" ? (n = t, t = null) : e && typeof e == "object" ? (n = e, e = t = null) : n = X; | ||
e != null && t && typeof t == "object" ? (n = t, t = null) : e && typeof e == "object" ? (n = e, e = t = null) : n = V; | ||
else if (typeof n != "object") | ||
throw Error(b + "Argument not an object: " + n); | ||
throw Error(H + "Argument not an object: " + n); | ||
if (u = r.toFixed(e, t), r.c) { | ||
var o, f = u.split("."), i = +n.groupSize, s = +n.secondaryGroupSize, l = n.groupSeparator || "", c = f[0], h = f[1], m = r.s < 0, w = m ? c.slice(1) : c, B = w.length; | ||
var o, l = u.split("."), i = +n.groupSize, s = +n.secondaryGroupSize, f = n.groupSeparator || "", c = l[0], h = l[1], w = r.s < 0, N = w ? c.slice(1) : c, B = N.length; | ||
if (s && (o = i, i = s, s = o, B -= o), i > 0 && B > 0) { | ||
for (o = B % i || i, c = w.substr(0, o); o < B; o += i) | ||
c += l + w.substr(o, i); | ||
s > 0 && (c += l + w.slice(o)), m && (c = "-" + c); | ||
for (o = B % i || i, c = N.substr(0, o); o < B; o += i) | ||
c += f + N.substr(o, i); | ||
s > 0 && (c += f + N.slice(o)), w && (c = "-" + c); | ||
} | ||
@@ -649,21 +649,21 @@ u = h ? c + (n.decimalSeparator || "") + ((s = +n.fractionGroupSize) ? h.replace( | ||
}, p.toFraction = function(e) { | ||
var t, n, u, r, o, f, i, s, l, c, h, m, w = this, B = w.c; | ||
var t, n, u, r, o, l, i, s, f, c, h, w, N = this, B = N.c; | ||
if (e != null && (i = new d(e), !i.isInteger() && (i.c || i.s !== 1) || i.lt(P))) | ||
throw Error(b + "Argument " + (i.isInteger() ? "out of range: " : "not an integer: ") + C(i)); | ||
throw Error(H + "Argument " + (i.isInteger() ? "out of range: " : "not an integer: ") + Q(i)); | ||
if (!B) | ||
return new d(w); | ||
for (t = new d(P), l = n = new d(P), u = s = new d(P), m = K(B), o = t.e = m.length - w.e - 1, t.c[0] = Oe[(f = o % R) < 0 ? R + f : f], e = !e || i.comparedTo(t) > 0 ? o > 0 ? t : l : i, f = q, q = 1 / 0, i = new d(m), s.c[0] = 0; c = g(i, t, 0, 1), r = n.plus(c.times(u)), r.comparedTo(e) != 1; ) | ||
n = u, u = r, l = s.plus(c.times(r = l)), s = r, t = i.minus(c.times(r = t)), i = r; | ||
return r = g(e.minus(n), u, 0, 1), s = s.plus(r.times(l)), n = n.plus(r.times(u)), s.s = l.s = w.s, o = o * 2, h = g(l, u, o, O).minus(w).abs().comparedTo( | ||
g(s, n, o, O).minus(w).abs() | ||
) < 1 ? [l, u] : [s, n], q = f, h; | ||
return new d(N); | ||
for (t = new d(P), f = n = new d(P), u = s = new d(P), w = Z(B), o = t.e = w.length - N.e - 1, t.c[0] = Oe[(l = o % S) < 0 ? S + l : l], e = !e || i.comparedTo(t) > 0 ? o > 0 ? t : f : i, l = F, F = 1 / 0, i = new d(w), s.c[0] = 0; c = g(i, t, 0, 1), r = n.plus(c.times(u)), r.comparedTo(e) != 1; ) | ||
n = u, u = r, f = s.plus(c.times(r = f)), s = r, t = i.minus(c.times(r = t)), i = r; | ||
return r = g(e.minus(n), u, 0, 1), s = s.plus(r.times(f)), n = n.plus(r.times(u)), s.s = f.s = N.s, o = o * 2, h = g(f, u, o, A).minus(N).abs().comparedTo( | ||
g(s, n, o, A).minus(N).abs() | ||
) < 1 ? [f, u] : [s, n], F = l, h; | ||
}, p.toNumber = function() { | ||
return +C(this); | ||
return +Q(this); | ||
}, p.toPrecision = function(e, t) { | ||
return e != null && F(e, 1, U), H(this, e, t, 2); | ||
return e != null && D(e, 1, W), X(this, e, t, 2); | ||
}, p.toString = function(e) { | ||
var t, n = this, u = n.s, r = n.e; | ||
return r === null ? u ? (t = "Infinity", u < 0 && (t = "-" + t)) : t = "NaN" : (e == null ? t = r <= _ || r >= M ? pe(K(n.c), r) : ie(K(n.c), r, "0") : e === 10 && se ? (n = W(new d(n), E + r + 1, O), t = ie(K(n.c), n.e, "0")) : (F(e, 2, $.length, "Base"), t = v(ie(K(n.c), r, "0"), 10, e, u, !0)), u < 0 && n.c[0] && (t = "-" + t)), t; | ||
return r === null ? u ? (t = "Infinity", u < 0 && (t = "-" + t)) : t = "NaN" : (e == null ? t = r <= q || r >= M ? pe(Z(n.c), r) : ie(Z(n.c), r, "0") : e === 10 && re ? (n = y(new d(n), E + r + 1, A), t = ie(Z(n.c), n.e, "0")) : (D(e, 2, G.length, "Base"), t = v(ie(Z(n.c), r, "0"), 10, e, u, !0)), u < 0 && n.c[0] && (t = "-" + t)), t; | ||
}, p.valueOf = p.toJSON = function() { | ||
return C(this); | ||
return Q(this); | ||
}, p._isBigNumber = !0, p[Symbol.toStringTag] = "BigNumber", p[Symbol.for("nodejs.util.inspect.custom")] = p.valueOf, a != null && d.set(a), d; | ||
@@ -675,5 +675,5 @@ } | ||
} | ||
function K(a) { | ||
for (var g, v, N = 1, p = a.length, P = a[0] + ""; N < p; ) { | ||
for (g = a[N++] + "", v = R - g.length; v--; g = "0" + g) | ||
function Z(a) { | ||
for (var g, v, O = 1, p = a.length, P = a[0] + ""; O < p; ) { | ||
for (g = a[O++] + "", v = S - g.length; v--; g = "0" + g) | ||
; | ||
@@ -686,26 +686,26 @@ P += g; | ||
} | ||
function ce(a, g) { | ||
var v, N, p = a.c, P = g.c, E = a.s, O = g.s, _ = a.e, M = g.e; | ||
if (!E || !O) | ||
function ue(a, g) { | ||
var v, O, p = a.c, P = g.c, E = a.s, A = g.s, q = a.e, M = g.e; | ||
if (!E || !A) | ||
return null; | ||
if (v = p && !p[0], N = P && !P[0], v || N) | ||
return v ? N ? 0 : -O : E; | ||
if (E != O) | ||
if (v = p && !p[0], O = P && !P[0], v || O) | ||
return v ? O ? 0 : -A : E; | ||
if (E != A) | ||
return E; | ||
if (v = E < 0, N = _ == M, !p || !P) | ||
return N ? 0 : !p ^ v ? 1 : -1; | ||
if (!N) | ||
return _ > M ^ v ? 1 : -1; | ||
for (O = (_ = p.length) < (M = P.length) ? _ : M, E = 0; E < O; E++) | ||
if (v = E < 0, O = q == M, !p || !P) | ||
return O ? 0 : !p ^ v ? 1 : -1; | ||
if (!O) | ||
return q > M ^ v ? 1 : -1; | ||
for (A = (q = p.length) < (M = P.length) ? q : M, E = 0; E < A; E++) | ||
if (p[E] != P[E]) | ||
return p[E] > P[E] ^ v ? 1 : -1; | ||
return _ == M ? 0 : _ > M ^ v ? 1 : -1; | ||
return q == M ? 0 : q > M ^ v ? 1 : -1; | ||
} | ||
function F(a, g, v, N) { | ||
function D(a, g, v, O) { | ||
if (a < g || a > v || a !== J(a)) | ||
throw Error(b + (N || "Argument") + (typeof a == "number" ? a < g || a > v ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(a)); | ||
throw Error(H + (O || "Argument") + (typeof a == "number" ? a < g || a > v ? " out of range: " : " not an integer: " : " not a primitive number: ") + String(a)); | ||
} | ||
function ge(a) { | ||
var g = a.c.length - 1; | ||
return j(a.e / R) == g && a.c[g] % 2 != 0; | ||
return j(a.e / S) == g && a.c[g] % 2 != 0; | ||
} | ||
@@ -716,3 +716,3 @@ function pe(a, g) { | ||
function ie(a, g, v) { | ||
var N, p; | ||
var O, p; | ||
if (g < 0) { | ||
@@ -722,33 +722,37 @@ for (p = v + "."; ++g; p += v) | ||
a = p + a; | ||
} else if (N = a.length, ++g > N) { | ||
for (p = v, g -= N; --g; p += v) | ||
} else if (O = a.length, ++g > O) { | ||
for (p = v, g -= O; --g; p += v) | ||
; | ||
a += p; | ||
} else | ||
g < N && (a = a.slice(0, g) + "." + a.slice(g)); | ||
g < O && (a = a.slice(0, g) + "." + a.slice(g)); | ||
return a; | ||
} | ||
var re = Re(); | ||
const ae = new re(0), Be = 9, S = A(A(10).pow(Be)); | ||
function A(a) { | ||
return new re(a); | ||
var le = Re(); | ||
const fe = new le(0), Be = 9, R = m(m(10).pow(Be)); | ||
function m(a) { | ||
return new le(a); | ||
} | ||
function ue(a) { | ||
return new re(a).integerValue().toNumber(); | ||
function oe(a) { | ||
return new le(a).integerValue().toNumber(); | ||
} | ||
function De(a) { | ||
return ue(a) / 1e9; | ||
return oe(a) / 1e9; | ||
} | ||
var Me = /* @__PURE__ */ ((a) => (a.base = "base", a.coinm = "coinm", a.prelaunch = "prelaunch", a))(Me || {}); | ||
function fe(a, g = S) { | ||
return A(a.quoteAssetReserve).times(a.quoteAssetReserveWeight).div(A(a.baseAssetReserve).times(g)).times(g); | ||
function ce(a, g = R) { | ||
return m(a.quoteAssetReserve).times(a.quoteAssetReserveWeight).div(m(a.baseAssetReserve).times(g)).times(g); | ||
} | ||
function Le(a, g, v) { | ||
const N = new re(g.openInterestLong).minus(g.openInterestShort), p = N.abs().div(a.maxOpenNotional); | ||
let P = new re(0); | ||
if (p.isZero()) | ||
return new re(0); | ||
p.lte(v.inflectionPoint) ? P = p.times(v.lowFundingFnB).plus(v.lowFundingFnA) : P = p.times(v.highFundingFnB).plus(v.highFundingFnA); | ||
let E = P.times(fe(g)); | ||
return N.isNegative() && (E = E.negated()), E; | ||
const O = m(g.openInterestLong), p = m(g.openInterestShort); | ||
if (O.eq(p)) | ||
return fe; | ||
const P = ce(g), E = ce(g); | ||
let A = P.minus(E); | ||
if (v) { | ||
const M = m(v.lowFundingFnA).div(R), _ = m(v.lowFundingFnB).div(R), F = m(v.highFundingFnA).div(R), T = m(v.highFundingFnB).div(R), C = m(v.inflectionPoint).div(R), U = O.minus(p), V = U.abs().div(a.maxOpenNotional); | ||
let G; | ||
V.lte(C) ? G = V.times(_).plus(M) : G = V.times(T).plus(F), A = G.times(E), U.isNegative() && (A = A.negated()); | ||
} | ||
return A.times(3600 / 86400).div(E); | ||
} | ||
@@ -758,15 +762,15 @@ function Ie(a, g) { | ||
quoteAssetWeight: g.times(a.baseAssetReserve).div(a.quoteAssetReserve), | ||
baseAssetWeight: S | ||
baseAssetWeight: R | ||
}; | ||
} | ||
function _e(a, g) { | ||
const v = A(a.baseAssetReserve).div(S), N = A(a.totalPositionSize).div(S), p = A(a.quoteAssetReserve).div(S), P = g.div(S), E = p.times(v), O = v.plus(N), M = E.div(O).div(O); | ||
return P.div(M).times(S); | ||
function Fe(a, g) { | ||
const v = m(a.baseAssetReserve).div(R), O = m(a.totalPositionSize).div(R), p = m(a.quoteAssetReserve).div(R), P = g.div(R), E = p.times(v), A = v.plus(O), M = E.div(A).div(A); | ||
return P.div(M).times(R); | ||
} | ||
function qe(a, g) { | ||
if (A(a.totalPositionSize).eq(ae)) { | ||
if (m(a.totalPositionSize).eq(fe)) { | ||
const { quoteAssetWeight: v } = Ie(a, g); | ||
return { ...a, quoteAssetReserveWeight: v }; | ||
} else { | ||
const v = _e(a, g); | ||
const v = Fe(a, g); | ||
return { ...a, quoteAssetReserveWeight: v }; | ||
@@ -776,14 +780,14 @@ } | ||
function ze(a, g, v = 1) { | ||
return A(a).times(g).times(v).div(S); | ||
return m(a).times(g).times(v).div(R); | ||
} | ||
function Pe(a) { | ||
return A(a.openNotional).div(a.size).abs(); | ||
return m(a.openNotional).div(a.size).abs(); | ||
} | ||
function Te(a) { | ||
const { leverage: g, feeRate: v, amount: N, decimals: p = S } = a, P = A(N), E = A(g).times(v), O = P.div(A(p).plus(E).div(p)); | ||
return P.abs().minus(O.abs()); | ||
const { leverage: g, feeRate: v, amount: O, decimals: p = R } = a, P = m(O), E = m(g).times(v), A = P.div(m(p).plus(E).div(p)); | ||
return P.abs().minus(A.abs()); | ||
} | ||
function de(a, g) { | ||
const v = A(a.size).gt(0) ? g.latestLongPremiumFraction : g.latestShortPremiumFraction; | ||
return A(v).minus(a.fraction).times(a.size).div(S).negated(); | ||
const v = m(a.size).gt(0) ? g.latestLongPremiumFraction : g.latestShortPremiumFraction; | ||
return m(v).minus(a.fraction).times(a.size).div(R).negated(); | ||
} | ||
@@ -794,88 +798,88 @@ function Ce(a) { | ||
...a, | ||
totalPositionSize: A(a.totalPositionSize).negated() | ||
totalPositionSize: m(a.totalPositionSize).negated() | ||
}, | ||
fe(a) | ||
ce(a) | ||
); | ||
return fe(g); | ||
return ce(g); | ||
} | ||
function Fe(a, g = 1) { | ||
return A(a.openNotional).div(a.margin).div(g); | ||
function _e(a, g = 1) { | ||
return m(a.openNotional).div(a.margin).div(g); | ||
} | ||
function ve(a, g, v = {}) { | ||
const { blockTimestamp: N = /* @__PURE__ */ new Date(), decimals: p = S } = v, P = Math.floor( | ||
(N.getTime() - a.timestamp.getTime()) / 1e3 / 60 | ||
const { blockTimestamp: O = /* @__PURE__ */ new Date(), decimals: p = R } = v, P = Math.floor( | ||
(O.getTime() - a.timestamp.getTime()) / 1e3 / 60 | ||
); | ||
return A(a.margin).times(P).times(A(g.rolloverFee).div(p)).div(525600); | ||
return m(a.margin).times(P).times(m(g.rolloverFee).div(p)).div(525600); | ||
} | ||
function Ue(a, g, v, N = 1, p = {}) { | ||
const { isSettleBySpotPrice: P = !1 } = p, E = A(a.openNotional).div(S), O = A(v.maintenanceMarginRatio).div(S), _ = A(g.quoteAssetReserve).div(S), M = A(g.baseAssetReserve).div(S), z = A(g.totalPositionSize).div(S), q = A(a.margin).div(S), k = A(a.size).div(S), Q = de(a, g).div(S), G = ve(a, v, p).div(S), X = Fe(a, N).decimalPlaces(9), $ = Q, se = G, d = q, H = _, Y = M, ne = z, W = k; | ||
let C = A(N), e = O; | ||
const t = Y.div(Y.plus(W)).gt(1); | ||
if (X.eq(1)) | ||
return t ? Pe(a).div(O).times(S) : Pe(a).times(O).times(S); | ||
if (X.lt(1)) | ||
return ae; | ||
const n = H.times(Y), u = n.div(Y.plus(ne)).div(Y.plus(ne)), r = n.div(Y.plus(W)).minus(H), o = $.minus(se).plus(d); | ||
let f, i; | ||
return t ? (f = E, i = e.plus(1)) : (f = E.negated(), i = e.minus(1).negated()), P && (C = f.times(u).div(i.abs().times(r).minus(o.times(u)))), f.plus(o.times(C)).div(i).div(r).times(u).times(S).abs(); | ||
function Ue(a, g, v, O = 1, p = {}) { | ||
const { isSettleBySpotPrice: P = !1 } = p, E = m(a.openNotional).div(R), A = m(v.maintenanceMarginRatio).div(R), q = m(g.quoteAssetReserve).div(R), M = m(g.baseAssetReserve).div(R), _ = m(g.totalPositionSize).div(R), F = m(a.margin).div(R), T = m(a.size).div(R), C = de(a, g).div(R), U = ve(a, v, p).div(R), V = _e(a, O).decimalPlaces(9), G = C, re = U, d = F, X = q, Y = M, ne = _, y = T; | ||
let Q = m(O), e = A; | ||
const t = Y.div(Y.plus(y)).gt(1); | ||
if (V.eq(1)) | ||
return t ? Pe(a).div(A).times(R) : Pe(a).times(A).times(R); | ||
if (V.lt(1)) | ||
return fe; | ||
const n = X.times(Y), u = n.div(Y.plus(ne)).div(Y.plus(ne)), r = n.div(Y.plus(y)).minus(X), o = G.minus(re).plus(d); | ||
let l, i; | ||
return t ? (l = E, i = e.plus(1)) : (l = E.negated(), i = e.minus(1).negated()), P && (Q = l.times(u).div(i.abs().times(r).minus(o.times(u)))), l.plus(o.times(Q)).div(i).div(r).times(u).times(R).abs(); | ||
} | ||
function Se(a, g, v = S) { | ||
const N = A(a.baseAssetReserve).div(v), p = A(g).div(v), P = A(a.quoteAssetReserve).div(v), E = A(a.quoteAssetReserveWeight).div(v), O = P.times(N), _ = re.max(N.plus(p), 1e-8), M = O.div(_), q = M.minus(P).times(E).times(v), k = fe(a, v), Q = q.abs().div(g).times(v).abs(), G = k.minus(Q).abs(), X = 1 - Q.dividedBy(Q.plus(G)).toNumber(); | ||
function Se(a, g, v = R) { | ||
const O = m(a.baseAssetReserve).div(v), p = m(g).div(v), P = m(a.quoteAssetReserve).div(v), E = m(a.quoteAssetReserveWeight).div(v), A = P.times(O), q = le.max(O.plus(p), 1e-8), M = A.div(q), F = M.minus(P).times(E).times(v), T = ce(a, v), C = F.abs().div(g).times(v).abs(), U = T.minus(C).abs(), V = 1 - C.dividedBy(C.plus(U)).toNumber(); | ||
return { | ||
quoteAssetOutput: q, | ||
baseAssetAfter: _.times(v), | ||
quoteAssetOutput: F, | ||
baseAssetAfter: q.times(v), | ||
quoteAssetAfter: M.times(v), | ||
priceImpact: X | ||
priceImpact: V | ||
}; | ||
} | ||
function ke(a, g, v, N = 1, p = {}) { | ||
const P = de(a, g), E = ve(a, v, p), { quoteAssetOutput: O } = Se(g, a.size), z = (O.gt(ae) ? A(a.openNotional).minus(O.integerValue()) : A(a.openNotional).plus(O.integerValue()).negated()).div(N).plus(P).minus(E).plus(a.margin), q = O.abs(); | ||
return z.times(N).div(q).times(p.decimals ?? S); | ||
function ke(a, g, v, O = 1, p = {}) { | ||
const P = de(a, g), E = ve(a, v, p), { quoteAssetOutput: A } = Se(g, a.size), _ = (A.gt(fe) ? m(a.openNotional).minus(A.integerValue()) : m(a.openNotional).plus(A.integerValue()).negated()).div(O).plus(P).minus(E).plus(a.margin), F = A.abs(); | ||
return _.times(O).div(F).times(p.decimals ?? R); | ||
} | ||
function Qe(a) { | ||
return a ? Math.floor(1 / A(a.initMarginRatio).div(S).toNumber()) : 0; | ||
return a ? Math.floor(1 / m(a.initMarginRatio).div(R).toNumber()) : 0; | ||
} | ||
function We(a, g = S) { | ||
function We(a, g = R) { | ||
return a ? Math.max( | ||
1, | ||
A(1 / A(a.minInitMarginRatio).div(g).toNumber()).integerValue(re.ROUND_UP).toNumber() | ||
m(1 / m(a.minInitMarginRatio).div(g).toNumber()).integerValue(le.ROUND_UP).toNumber() | ||
) : 1; | ||
} | ||
function Ge(a, g, v, N, p = 1, P = {}) { | ||
const { isSettleBySpotPrice: E = !1 } = P, O = A(g.openNotional).div(S), _ = A(v.quoteAssetReserve).div(S), M = A(v.baseAssetReserve).div(S), z = A(v.totalPositionSize).div(S), q = A(g.margin).div(S), k = A(g.size).div(S), Q = de(g, v).div(S), G = ve(g, N, P).div(S), X = Q, $ = G, se = q, d = _, H = M, Y = z, ne = k, W = A(a); | ||
let C = A(p); | ||
const e = H.div(H.plus(ne)).gt(1), t = d.times(H), n = t.div(H.plus(Y)).div(H.plus(Y)), u = t.div(H.plus(ne)).minus(d), r = X.minus($).plus(se), f = se.times(C).times(W), i = O; | ||
function Ge(a, g, v, O, p = 1, P = {}) { | ||
const { isSettleBySpotPrice: E = !1 } = P, A = m(g.openNotional).div(R), q = m(v.quoteAssetReserve).div(R), M = m(v.baseAssetReserve).div(R), _ = m(v.totalPositionSize).div(R), F = m(g.margin).div(R), T = m(g.size).div(R), C = de(g, v).div(R), U = ve(g, O, P).div(R), V = C, G = U, re = F, d = q, X = M, Y = _, ne = T, y = m(a); | ||
let Q = m(p); | ||
const e = X.div(X.plus(ne)).gt(1), t = d.times(X), n = t.div(X.plus(Y)).div(X.plus(Y)), u = t.div(X.plus(ne)).minus(d), r = V.minus(G).plus(re), l = re.times(Q).times(y), i = A; | ||
if (e) { | ||
E && (C = i.minus(f).div(u.abs().div(n).minus(r))); | ||
const s = i.minus(f).plus(r.times(C)).div(u.abs()).times(n); | ||
return s.lte(0) ? ae : s.times(S); | ||
E && (Q = i.minus(l).div(u.abs().div(n).minus(r))); | ||
const s = i.minus(l).plus(r.times(Q)).div(u.abs()).times(n); | ||
return s.lte(0) ? fe : s.times(R); | ||
} else | ||
return E && (C = i.plus(f).div(u.abs().div(n).plus(r))), i.plus(f).minus(r.times(C)).div(u.abs()).times(n).times(S); | ||
return E && (Q = i.plus(l).div(u.abs().div(n).plus(r))), i.plus(l).minus(r.times(Q)).div(u.abs()).times(n).times(R); | ||
} | ||
function ye(a, g) { | ||
return A(g).minus(a).div(a).abs(); | ||
return m(g).minus(a).div(a).abs(); | ||
} | ||
function Ve(a) { | ||
const g = fe(a), v = g.plus(a.indexPrice).div(2); | ||
return A(a.indexPrice).minus(g).abs().div(v); | ||
const g = ce(a), v = g.plus(a.indexPrice).div(2); | ||
return m(a.indexPrice).minus(g).abs().div(v); | ||
} | ||
function be(a, g, v, N = 1, p = {}) { | ||
const P = de(a, g), E = ve(a, v, p), { quoteAssetOutput: O } = Se(g, a.size), M = (O.gt(ae) ? A(a.openNotional).minus(O.integerValue()) : A(a.openNotional).plus(O.integerValue()).negated()).div(N), z = M.plus(P).minus(E).plus(a.margin), q = O.abs(), k = z.times(N).div(q).times(p.decimals ?? S); | ||
function be(a, g, v, O = 1, p = {}) { | ||
const P = de(a, g), E = ve(a, v, p), { quoteAssetOutput: A } = Se(g, a.size), M = (A.gt(fe) ? m(a.openNotional).minus(A.integerValue()) : m(a.openNotional).plus(A.integerValue()).negated()).div(O), _ = M.plus(P).minus(E).plus(a.margin), F = A.abs(), T = _.times(O).div(F).times(p.decimals ?? R); | ||
return { | ||
remainMargin: ue(z), | ||
marginRatio: ue(k), | ||
positionNotional: ue(q), | ||
fundingPayment: ue(P), | ||
rolloverFee: ue(E), | ||
unrealizedPnl: ue(M), | ||
badDebt: ue(re.min(0, z)) | ||
remainMargin: oe(_), | ||
marginRatio: oe(T), | ||
positionNotional: oe(F), | ||
fundingPayment: oe(P), | ||
rolloverFee: oe(E), | ||
unrealizedPnl: oe(M), | ||
badDebt: oe(le.min(0, _)) | ||
}; | ||
} | ||
function $e(a, g, v = S) { | ||
const N = A(a.baseAssetReserve).div(v), p = A(g).div(a.quoteAssetReserveWeight), P = A(a.quoteAssetReserve).div(v), E = N.times(P), O = P.plus(p), M = E.div(O).minus(N), z = M.times(v), q = N.plus(M).times(v), k = O.times(v), Q = fe(a, v), G = A(g).abs().div(z.abs()).times(v), X = Q.minus(G).abs(), $ = 1 - Q.dividedBy(Q.plus(X)).toNumber(); | ||
function $e(a, g, v = R) { | ||
const O = m(a.baseAssetReserve).div(v), p = m(g).div(a.quoteAssetReserveWeight), P = m(a.quoteAssetReserve).div(v), E = O.times(P), A = P.plus(p), M = E.div(A).minus(O), _ = M.times(v), F = O.plus(M).times(v), T = A.times(v), C = ce(a, v), U = m(g).abs().div(_.abs()).times(v), V = C.minus(U).abs(), G = 1 - C.dividedBy(C.plus(V)).toNumber(); | ||
return { | ||
baseAssetOutput: z, | ||
baseAssetAfter: q, | ||
quoteAssetAfter: k, | ||
priceImpact: $ | ||
baseAssetOutput: _, | ||
baseAssetAfter: F, | ||
quoteAssetAfter: T, | ||
priceImpact: G | ||
}; | ||
@@ -885,7 +889,7 @@ } | ||
Be as DECIMALS_NUM, | ||
S as DECIMAL_UNIT, | ||
R as DECIMAL_UNIT, | ||
Me as MarketType, | ||
ae as ZERO, | ||
A as bn, | ||
Le as calculateFunding, | ||
fe as ZERO, | ||
m as bn, | ||
Le as calculateFundingRate, | ||
Ie as calculateQuoteAssetWeight, | ||
@@ -899,6 +903,6 @@ De as decimalValue, | ||
Ce as getIndexPrice, | ||
Fe as getLeverage, | ||
_e as getLeverage, | ||
Ue as getLiquidationPrice, | ||
ke as getMarginRatio, | ||
fe as getMarketPrice, | ||
ce as getMarketPrice, | ||
Qe as getMaximumLeverage, | ||
@@ -909,6 +913,6 @@ We as getMinimumLeverage, | ||
Ve as getPriceSpread, | ||
_e as getQuoteAssetWeight, | ||
Fe as getQuoteAssetWeight, | ||
be as getRemainMarginWithFundingPayment, | ||
ve as getRolloverFee, | ||
ue as integerValue, | ||
oe as integerValue, | ||
$e as swapInput, | ||
@@ -915,0 +919,0 @@ Se as swapOutput |
{ | ||
"name": "@storm-trade/dex-math", | ||
"private": false, | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
403555
1118