@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.21 to 1.0.0-rc.22
@@ -75,14 +75,2 @@ import { Chain } from '@swapkit/types'; | ||
constructor(params: SKBigIntParams); | ||
/** | ||
* @deprecated Use `getBaseValue('string')` instead | ||
*/ | ||
get baseValue(): string; | ||
/** | ||
* @deprecated Use `getBaseValue('number')` instead | ||
*/ | ||
get baseValueNumber(): number; | ||
/** | ||
* @deprecated Use `getBaseValue('bigint')` instead | ||
*/ | ||
get baseValueBigInt(): bigint; | ||
set(value: SKBigIntParams): this; | ||
@@ -105,2 +93,8 @@ add(...args: InitialisationValueType[]): this; | ||
toAbbreviation(digits?: number): string; | ||
toCurrency(currency?: string, { currencyPosition, decimal, decimalSeparator, thousandSeparator, }?: { | ||
currencyPosition?: string | undefined; | ||
decimal?: number | undefined; | ||
decimalSeparator?: string | undefined; | ||
thousandSeparator?: string | undefined; | ||
}): string; | ||
} | ||
@@ -107,0 +101,0 @@ |
@@ -41,3 +41,3 @@ var gt = Object.defineProperty; | ||
return t != null && t.startsWith("0x") ? nt({ chain: o.Avalanche, to: t.toLowerCase() }) : d.AVAX; | ||
}, St = async (s) => s === o.BinanceSmartChain ? d.BSC : d.BSC, Rt = async ({ chain: s, symbol: t }) => { | ||
}, $t = async (s) => s === o.BinanceSmartChain ? d.BSC : d.BSC, Rt = async ({ chain: s, symbol: t }) => { | ||
switch (s) { | ||
@@ -49,3 +49,3 @@ case o.Ethereum: | ||
case o.BinanceSmartChain: | ||
return St(t); | ||
return $t(t); | ||
default: | ||
@@ -58,3 +58,3 @@ return d[s]; | ||
[L.Fastest]: 2 | ||
}, $t = ({ chain: s, symbol: t }) => { | ||
}, St = ({ chain: s, symbol: t }) => { | ||
switch (s) { | ||
@@ -145,4 +145,4 @@ case o.Bitcoin: | ||
}, se = (s) => { | ||
var a; | ||
const [t, ...e] = s.split("."), r = s.includes("/"), n = e.join("."), i = (a = n == null ? void 0 : n.split("-")) == null ? void 0 : a[0]; | ||
var c; | ||
const [t, ...e] = s.split("."), r = s.includes("/"), n = e.join("."), i = (c = n == null ? void 0 : n.split("-")) == null ? void 0 : c[0]; | ||
return { chain: t, symbol: n, ticker: i, synth: r }; | ||
@@ -154,3 +154,3 @@ }, Et = new RegExp( | ||
(t) => !Et.test(t.toString()) && !t.toString().includes("undefined") | ||
), Bt = ({ | ||
), Ot = ({ | ||
liquidityUnits: s, | ||
@@ -160,5 +160,5 @@ poolUnits: t, | ||
}) => { | ||
const r = p(s), n = p(t), i = p(e), a = r.mul(i), c = n.mul(n).mul(2), u = n.mul(r).mul(2), l = r.mul(r), h = n.mul(n).mul(n); | ||
return a.mul(c.sub(u).add(l)).div(h); | ||
}, Ot = ({ | ||
const r = p(s), n = p(t), i = p(e), c = r.mul(i), a = n.mul(n).mul(2), u = n.mul(r).mul(2), l = r.mul(r), h = n.mul(n).mul(n); | ||
return c.mul(a.sub(u).add(l)).div(h); | ||
}, Bt = ({ | ||
liquidityUnits: s, | ||
@@ -168,3 +168,3 @@ poolUnits: t, | ||
}) => { | ||
const r = p(s), n = p(t), i = p(e), a = r.mul(i), c = n.mul(n).mul(2), u = n.mul(r).mul(2), l = r.mul(r), h = a.mul(c.sub(u).add(l)), _ = n.mul(n).mul(n); | ||
const r = p(s), n = p(t), i = p(e), c = r.mul(i), a = n.mul(n).mul(2), u = n.mul(r).mul(2), l = r.mul(r), h = c.mul(a.sub(u).add(l)), _ = n.mul(n).mul(n); | ||
return h.div(_); | ||
@@ -176,3 +176,3 @@ }, ie = ({ | ||
poolUnits: r | ||
}) => Bt({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(s), oe = ({ | ||
}) => Ot({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(s), oe = ({ | ||
percent: s, | ||
@@ -182,3 +182,3 @@ assetDepth: t, | ||
poolUnits: r | ||
}) => Ot({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(s), p = (s) => b.fromBigInt(BigInt(s), d.THOR), Vt = ({ | ||
}) => Bt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(s), p = (s) => b.fromBigInt(BigInt(s), d.THOR), Mt = ({ | ||
liquidityUnits: s, | ||
@@ -198,4 +198,4 @@ poolUnits: t, | ||
}) => Object.fromEntries( | ||
Object.entries(Vt({ liquidityUnits: s, poolUnits: t, runeDepth: e, assetDepth: r })).map( | ||
([i, a]) => [i, a.mul(n)] | ||
Object.entries(Mt({ liquidityUnits: s, poolUnits: t, runeDepth: e, assetDepth: r })).map( | ||
([i, c]) => [i, c.mul(n)] | ||
) | ||
@@ -210,3 +210,3 @@ ), ce = ({ | ||
}) => { | ||
const a = new b({ value: s, decimal: 8 }), c = new b({ value: e, decimal: 8 }), u = new b({ value: t, decimal: 8 }), l = new b({ value: n, decimal: 8 }), h = new b({ value: i, decimal: 8 }), _ = l.mul(c), v = h.mul(a), j = l.mul(h), mt = a.mul(c), pt = u.mul(_.add(v.add(j.mul(2)))), ft = _.add(v.add(mt.mul(2))), Q = pt.div(ft), H = p(r).add(Q); | ||
const c = new b({ value: s, decimal: 8 }), a = new b({ value: e, decimal: 8 }), u = new b({ value: t, decimal: 8 }), l = new b({ value: n, decimal: 8 }), h = new b({ value: i, decimal: 8 }), _ = l.mul(a), v = h.mul(c), j = l.mul(h), mt = c.mul(a), pt = u.mul(_.add(v.add(j.mul(2)))), ft = _.add(v.add(mt.mul(2))), Q = pt.div(ft), H = p(r).add(Q); | ||
if (Q.getBaseValue("number") === 0) | ||
@@ -224,5 +224,5 @@ return H.div(u).getBaseValue("number"); | ||
return 0; | ||
const n = p(e), i = p(r), a = p(t), c = p(s), u = a.mul(n).sub(i.mul(c)), l = i.mul(c).add(n.mul(i)); | ||
const n = p(e), i = p(r), c = p(t), a = p(s), u = c.mul(n).sub(i.mul(a)), l = i.mul(a).add(n.mul(i)); | ||
return Math.abs(u.div(l).getBaseValue("number")); | ||
}, Mt = ({ | ||
}, Vt = ({ | ||
symbol: s, | ||
@@ -251,4 +251,4 @@ ticker: t, | ||
case g.WITHDRAW: { | ||
const { chain: e, ticker: r, symbol: n, basisPoints: i, targetAssetString: a, singleSide: c } = t, u = !c && a ? `:${a}` : "", l = Mt({ chain: e, symbol: n, ticker: r }); | ||
return `${s}:${e}${c ? "/" : "."}${l}:${i}${u}`; | ||
const { chain: e, ticker: r, symbol: n, basisPoints: i, targetAssetString: c, singleSide: a } = t, u = !a && c ? `:${c}` : "", l = Vt({ chain: e, symbol: n, ticker: r }); | ||
return `${s}:${e}${a ? "/" : "."}${l}:${i}${u}`; | ||
} | ||
@@ -275,4 +275,4 @@ case g.OPEN_LOAN: | ||
constructor(t, e, r) { | ||
const n = t.status || t.status === 0 ? t.status : "", i = t.statusText || "", a = `${n} ${i}`.trim(), c = a ? `status code ${a}` : "an unknown error"; | ||
super(`Request failed with ${c}`), Object.defineProperty(this, "response", { | ||
const n = t.status || t.status === 0 ? t.status : "", i = t.statusText || "", c = `${n} ${i}`.trim(), a = c ? `status code ${c}` : "an unknown error"; | ||
super(`Request failed with ${a}`), Object.defineProperty(this, "response", { | ||
enumerable: !0, | ||
@@ -305,5 +305,5 @@ configurable: !0, | ||
} | ||
const M = (s) => s !== null && typeof s == "object", V = (...s) => { | ||
const V = (s) => s !== null && typeof s == "object", M = (...s) => { | ||
for (const t of s) | ||
if ((!M(t) || Array.isArray(t)) && t !== void 0) | ||
if ((!V(t) || Array.isArray(t)) && t !== void 0) | ||
throw new TypeError("The `options` argument must be an object"); | ||
@@ -313,4 +313,4 @@ return J({}, ...s); | ||
const e = new globalThis.Headers(s), r = t instanceof globalThis.Headers, n = new globalThis.Headers(t); | ||
for (const [i, a] of n.entries()) | ||
r && a === "undefined" || a === void 0 ? e.delete(i) : e.set(i, a); | ||
for (const [i, c] of n.entries()) | ||
r && c === "undefined" || c === void 0 ? e.delete(i) : e.set(i, c); | ||
return e; | ||
@@ -322,6 +322,6 @@ }, J = (...s) => { | ||
Array.isArray(t) || (t = []), t = [...t, ...r]; | ||
else if (M(r)) { | ||
else if (V(r)) { | ||
for (let [n, i] of Object.entries(r)) | ||
M(i) && n in t && (i = J(t[n], i)), t = { ...t, [n]: i }; | ||
M(r.headers) && (e = ot(e, r.headers), t.headers = e); | ||
V(i) && n in t && (i = J(t[n], i)), t = { ...t, [n]: i }; | ||
V(r.headers) && (e = ot(e, r.headers), t.headers = e); | ||
} | ||
@@ -399,7 +399,7 @@ return t; | ||
return new Promise((n, i) => { | ||
const a = setTimeout(() => { | ||
const c = setTimeout(() => { | ||
e && e.abort(), i(new it(s)); | ||
}, r.timeout); | ||
r.fetch(s, t).then(n).catch(i).then(() => { | ||
clearTimeout(a); | ||
clearTimeout(c); | ||
}); | ||
@@ -431,9 +431,9 @@ }); | ||
await Promise.resolve(); | ||
let c = await r._fetch(); | ||
let a = await r._fetch(); | ||
for (const u of r._options.hooks.afterResponse) { | ||
const l = await u(r.request, r._options, r._decorateResponse(c.clone())); | ||
l instanceof globalThis.Response && (c = l); | ||
const l = await u(r.request, r._options, r._decorateResponse(a.clone())); | ||
l instanceof globalThis.Response && (a = l); | ||
} | ||
if (r._decorateResponse(c), !c.ok && r._options.throwHttpErrors) { | ||
let u = new X(c, r.request, r._options); | ||
if (r._decorateResponse(a), !a.ok && r._options.throwHttpErrors) { | ||
let u = new X(a, r.request, r._options); | ||
for (const l of r._options.hooks.beforeError) | ||
@@ -448,11 +448,11 @@ u = await l(u); | ||
throw new Error("Streams are not supported in your environment. `ReadableStream` is missing."); | ||
return r._stream(c.clone(), r._options.onDownloadProgress); | ||
return r._stream(a.clone(), r._options.onDownloadProgress); | ||
} | ||
return c; | ||
}, a = r._options.retry.methods.includes(r.request.method.toLowerCase()) ? r._retry(n) : n(); | ||
for (const [c, u] of Object.entries(Nt)) | ||
a[c] = async () => { | ||
return a; | ||
}, c = r._options.retry.methods.includes(r.request.method.toLowerCase()) ? r._retry(n) : n(); | ||
for (const [a, u] of Object.entries(Nt)) | ||
c[a] = async () => { | ||
r.request.headers.set("accept", r.request.headers.get("accept") || u); | ||
const h = (await a).clone(); | ||
if (c === "json") { | ||
const h = (await c).clone(); | ||
if (a === "json") { | ||
if (h.status === 204 || (await h.clone().arrayBuffer()).byteLength === 0) | ||
@@ -463,5 +463,5 @@ return ""; | ||
} | ||
return h[c](); | ||
return h[a](); | ||
}; | ||
return a; | ||
return c; | ||
} | ||
@@ -599,6 +599,6 @@ // eslint-disable-next-line complexity | ||
async start(i) { | ||
const a = t.body.getReader(); | ||
const c = t.body.getReader(); | ||
e && e({ percent: 0, transferredBytes: 0, totalBytes: r }, new Uint8Array()); | ||
async function c() { | ||
const { done: u, value: l } = await a.read(); | ||
async function a() { | ||
const { done: u, value: l } = await c.read(); | ||
if (u) { | ||
@@ -613,5 +613,5 @@ i.close(); | ||
} | ||
i.enqueue(l), await c(); | ||
i.enqueue(l), await a(); | ||
} | ||
await c(); | ||
await a(); | ||
} | ||
@@ -627,6 +627,6 @@ }), { | ||
const F = (s) => { | ||
const t = (e, r) => q.create(e, V(s, r)); | ||
const t = (e, r) => q.create(e, M(s, r)); | ||
for (const e of at) | ||
t[e] = (r, n) => q.create(r, V(s, n, { method: e })); | ||
return t.create = (e) => F(V(e)), t.extend = (e) => F(V(s, e)), t.stop = ct, t; | ||
t[e] = (r, n) => q.create(r, M(s, n, { method: e })); | ||
return t.create = (e) => F(M(e)), t.extend = (e) => F(M(s, e)), t.stop = ct, t; | ||
}, zt = F(), Jt = zt, Kt = typeof window < "u" ? {} : { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" }, tt = Jt.create({ headers: Kt }), Qt = { | ||
@@ -645,3 +645,3 @@ get: (s, t) => tt.get(s, t).json(), | ||
); | ||
}, I = 8, S = (s) => 10n ** BigInt(s), R = (s) => Math.log10(parseFloat(s.toString())); | ||
}, I = 8, $ = (s) => 10n ** BigInt(s), R = (s) => Math.log10(parseFloat(s.toString())); | ||
function x({ | ||
@@ -656,5 +656,5 @@ value: s, | ||
i > 0 && (n = "0".repeat(i) + n); | ||
const a = n.length - e; | ||
let c = n.slice(-e); | ||
return parseInt(c[t]) >= 5 ? c = `${c.substring(0, t - 1)}${(parseInt(c[t - 1]) + 1).toString()}` : c = c.substring(0, t), `${r ? "-" : ""}${n.slice(0, a)}.${c}`.replace( | ||
const c = n.length - e; | ||
let a = n.slice(-e); | ||
return parseInt(a[t]) >= 5 ? a = `${a.substring(0, t - 1)}${(parseInt(a[t - 1]) + 1).toString()}` : a = a.substring(0, t), `${r ? "-" : ""}${n.slice(0, c)}.${a}`.replace( | ||
/\.?0*$/, | ||
@@ -664,3 +664,3 @@ "" | ||
} | ||
var A, $, P, lt, N, ht, B, W, T, C, O, G; | ||
var A, S, P, lt, N, ht, O, W, T, C, B, G; | ||
const K = class K { | ||
@@ -671,5 +671,5 @@ constructor(t) { | ||
y(this, N); | ||
y(this, O); | ||
y(this, T); | ||
y(this, B); | ||
y(this, T); | ||
y(this, O); | ||
f(this, "decimalMultiplier", 10n ** 8n); | ||
@@ -679,4 +679,4 @@ f(this, "bigIntValue", 0n); | ||
const e = U(t); | ||
this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = S( | ||
Math.max(m(this, O, G).call(this, m(this, T, C).call(this, e)), this.decimal || 0) | ||
this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = $( | ||
Math.max(m(this, B, G).call(this, m(this, T, C).call(this, e)), this.decimal || 0) | ||
), m(this, P, lt).call(this, e); | ||
@@ -696,24 +696,6 @@ } | ||
return this.fromBigInt( | ||
t.getBaseValue("bigint") * S(r) / S(e), | ||
t.getBaseValue("bigint") * $(r) / $(e), | ||
r | ||
); | ||
} | ||
/** | ||
* @deprecated Use `getBaseValue('string')` instead | ||
*/ | ||
get baseValue() { | ||
return this.getBaseValue("string"); | ||
} | ||
/** | ||
* @deprecated Use `getBaseValue('number')` instead | ||
*/ | ||
get baseValueNumber() { | ||
return this.getBaseValue("number"); | ||
} | ||
/** | ||
* @deprecated Use `getBaseValue('bigint')` instead | ||
*/ | ||
get baseValueBigInt() { | ||
return this.getBaseValue("bigint"); | ||
} | ||
set(t) { | ||
@@ -723,12 +705,12 @@ return new this.constructor({ decimal: this.decimal, value: t, identifier: this.toString() }); | ||
add(...t) { | ||
return m(this, A, $).call(this, "add", ...t); | ||
return m(this, A, S).call(this, "add", ...t); | ||
} | ||
sub(...t) { | ||
return m(this, A, $).call(this, "sub", ...t); | ||
return m(this, A, S).call(this, "sub", ...t); | ||
} | ||
mul(...t) { | ||
return m(this, A, $).call(this, "mul", ...t); | ||
return m(this, A, S).call(this, "mul", ...t); | ||
} | ||
div(...t) { | ||
return m(this, A, $).call(this, "div", ...t); | ||
return m(this, A, S).call(this, "div", ...t); | ||
} | ||
@@ -765,3 +747,3 @@ gt(t) { | ||
getBaseValue(t) { | ||
const e = this.decimalMultiplier / S(this.decimal || d.THOR), r = this.bigIntValue / e; | ||
const e = this.decimalMultiplier / $(this.decimal || d.THOR), r = this.bigIntValue / e; | ||
switch (t) { | ||
@@ -780,3 +762,3 @@ case "number": | ||
const r = U(t), n = m(this, T, C).call(this, r); | ||
return n === "0" || n === "undefined" ? 0n : m(this, B, W).call(this, n, e); | ||
return n === "0" || n === "undefined" ? 0n : m(this, O, W).call(this, n, e); | ||
} | ||
@@ -787,3 +769,3 @@ formatBigIntToSafeValue(t, e) { | ||
R(this.decimalMultiplier) | ||
), i = t < 0n, a = t.toString().substring(i ? 1 : 0), c = n - (a.length - 1), u = c > 0 ? "0".repeat(c) + a : a, l = u.length - n; | ||
), i = t < 0n, c = t.toString().substring(i ? 1 : 0), a = n - (c.length - 1), u = a > 0 ? "0".repeat(a) + c : c, l = u.length - n; | ||
let h = u.slice(-n); | ||
@@ -806,5 +788,5 @@ return parseInt(h[r]) >= 5 ? h = `${h.substring(0, r - 1)}${(parseInt(h[r - 1]) + 1).toString()}` : h = h.substring(0, r), `${i ? "-" : ""}${u.slice( | ||
); | ||
const c = parseInt(i), u = `${c}`.slice(0, t); | ||
const a = parseInt(i), u = `${a}`.slice(0, t); | ||
return `0.${u.padStart( | ||
i.length - `${c}`.length + u.length, | ||
i.length - `${a}`.length + u.length, | ||
"0" | ||
@@ -817,5 +799,5 @@ )}`; | ||
return `${n}.${i.slice(0, t)}`.padEnd(t, "0"); | ||
const a = parseInt(i), c = `${a}`.slice(0, t); | ||
return `0.${c.padStart( | ||
i.length - `${a}`.length + c.length, | ||
const c = parseInt(i), a = `${c}`.slice(0, t); | ||
return `0.${a.padStart( | ||
i.length - `${c}`.length + a.length, | ||
"0" | ||
@@ -828,24 +810,33 @@ )}`; | ||
return this.getValue("string"); | ||
const a = 10 ** (n * 3); | ||
return `${(e / a).toFixed(t)}${i}`; | ||
const c = 10 ** (n * 3); | ||
return `${(e / c).toFixed(t)}${i}`; | ||
} | ||
toCurrency(t = "$", { | ||
currencyPosition: e = "start", | ||
decimal: r = 2, | ||
decimalSeparator: n = ".", | ||
thousandSeparator: i = "," | ||
} = {}) { | ||
const c = this.getValue("number"), [a, u = ""] = c.toFixed(6).split("."), l = a.replace(/\B(?=(\d{3})+(?!\d))/g, i), h = !a && !u ? "0.00" : a === "0" ? `${parseFloat(`0.${u}`)}`.replace(".", n) : `${l}${u ? `${n}${u.slice(0, r)}` : ""}`; | ||
return `${e === "start" ? t : ""}${h}${e === "end" ? t : ""}`; | ||
} | ||
}; | ||
A = new WeakSet(), $ = function(t, ...e) { | ||
const r = m(this, N, ht).call(this, this, ...e), n = S(r), i = e.reduce( | ||
(c, u) => { | ||
A = new WeakSet(), S = function(t, ...e) { | ||
const r = m(this, N, ht).call(this, this, ...e), n = $(r), i = e.reduce( | ||
(a, u) => { | ||
const l = this.getBigIntValue(u, r); | ||
switch (t) { | ||
case "add": | ||
return c + l; | ||
return a + l; | ||
case "sub": | ||
return c - l; | ||
return a - l; | ||
case "mul": | ||
return c * l / n; | ||
return a * l / n; | ||
case "div": { | ||
if (l === 0n) | ||
throw new RangeError("Division by zero"); | ||
return c * n / l; | ||
return a * n / l; | ||
} | ||
default: | ||
return c; | ||
return a; | ||
} | ||
@@ -855,3 +846,3 @@ }, | ||
this.bigIntValue * n / this.decimalMultiplier | ||
), a = x({ | ||
), c = x({ | ||
bigIntDecimal: r, | ||
@@ -861,14 +852,14 @@ decimal: Math.max(r, R(this.decimalMultiplier)), | ||
}); | ||
return new this.constructor({ decimal: this.decimal, value: a, identifier: this.toString() }); | ||
return new this.constructor({ decimal: this.decimal, value: c, identifier: this.toString() }); | ||
}, P = new WeakSet(), lt = function(t) { | ||
const e = m(this, T, C).call(this, t) || "0"; | ||
this.bigIntValue = m(this, B, W).call(this, e); | ||
this.bigIntValue = m(this, O, W).call(this, e); | ||
}, N = new WeakSet(), ht = function(...t) { | ||
const e = t.map( | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : m(this, O, G).call(this, m(this, T, C).call(this, r)) | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : m(this, B, G).call(this, m(this, T, C).call(this, r)) | ||
).filter(Boolean); | ||
return Math.max(...e, I); | ||
}, B = new WeakSet(), W = function(t, e) { | ||
const r = e ? S(e) : this.decimalMultiplier, n = R(r), [i = "", a = ""] = t.split("."); | ||
return BigInt(`${i}${a.padEnd(n, "0")}`); | ||
}, O = new WeakSet(), W = function(t, e) { | ||
const r = e ? $(e) : this.decimalMultiplier, n = R(r), [i = "", c = ""] = t.split("."); | ||
return BigInt(`${i}${c.padEnd(n, "0")}`); | ||
}, T = new WeakSet(), C = function(t) { | ||
@@ -880,3 +871,3 @@ const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", { | ||
return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0]; | ||
}, O = new WeakSet(), G = function(t) { | ||
}, B = new WeakSet(), G = function(t) { | ||
var r; | ||
@@ -933,6 +924,6 @@ const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0; | ||
static fromStringSync(e, r = 0) { | ||
const { isSynthetic: n } = z(e), { decimal: i, identifier: a } = rt( | ||
const { isSynthetic: n } = z(e), { decimal: i, identifier: c } = rt( | ||
e | ||
), c = k(r, i); | ||
return a ? new w({ decimal: i, identifier: a, value: c }) : n ? new w({ decimal: 8, identifier: e, value: c }) : void 0; | ||
), a = k(r, i); | ||
return c ? new w({ decimal: i, identifier: c, value: a }) : n ? new w({ decimal: 8, identifier: e, value: a }) : void 0; | ||
} | ||
@@ -943,8 +934,8 @@ static async fromIdentifier(e, r = 0) { | ||
static fromIdentifierSync(e, r = 0) { | ||
const { decimal: n, identifier: i } = rt(e), a = k(r, n); | ||
return new w({ decimal: n, identifier: i, value: a }); | ||
const { decimal: n, identifier: i } = rt(e), c = k(r, n); | ||
return new w({ decimal: n, identifier: i, value: c }); | ||
} | ||
static fromChainOrSignature(e, r = 0) { | ||
const { decimal: n, identifier: i } = Ct(e), a = k(r, n); | ||
return new w({ value: a, decimal: n, identifier: i }); | ||
const { decimal: n, identifier: i } = Ct(e), c = k(r, n); | ||
return new w({ value: c, decimal: n, identifier: i }); | ||
} | ||
@@ -960,5 +951,5 @@ static async loadStaticAssets() { | ||
NativeList: i, | ||
...a | ||
...c | ||
} = await import("@swapkit/tokens"); | ||
Y = [i, ...Object.values(a)].reduce( | ||
Y = [i, ...Object.values(c)].reduce( | ||
(u, { tokens: l }) => (l.forEach(({ identifier: h, chain: _, ...v }) => { | ||
@@ -1000,10 +991,10 @@ const j = "decimals" in v ? v.decimals : d[_]; | ||
}, z = (s) => { | ||
const t = s.slice(0, 14).includes("/"), [e, r] = s.split(".").pop().split("/"), n = s.includes(".") && !t ? s : `${o.THORChain}.${r}`, [i, a] = n.split("."), [c, u] = (t ? r : a).split("-"); | ||
const t = s.slice(0, 14).includes("/"), [e, r] = s.split(".").pop().split("/"), n = s.includes(".") && !t ? s : `${o.THORChain}.${r}`, [i, c] = n.split("."), [a, u] = (t ? r : c).split("-"); | ||
return { | ||
address: u == null ? void 0 : u.toLowerCase(), | ||
chain: i, | ||
isGasAsset: $t({ chain: i, symbol: a }), | ||
isGasAsset: St({ chain: i, symbol: c }), | ||
isSynthetic: t, | ||
symbol: (t ? `${e}/` : "") + (u ? `${c}-${(u == null ? void 0 : u.toLowerCase()) ?? ""}` : a), | ||
ticker: c | ||
symbol: (t ? `${e}/` : "") + (u ? `${a}-${(u == null ? void 0 : u.toLowerCase()) ?? ""}` : c), | ||
ticker: a | ||
}; | ||
@@ -1095,5 +1086,5 @@ }, Zt = { | ||
It as getAssetType, | ||
Ot as getAsymmetricAssetShare, | ||
Bt as getAsymmetricAssetShare, | ||
oe as getAsymmetricAssetWithdrawAmount, | ||
Bt as getAsymmetricRuneShare, | ||
Ot as getAsymmetricRuneShare, | ||
ie as getAsymmetricRuneWithdrawAmount, | ||
@@ -1106,7 +1097,7 @@ Ct as getCommonAssetInfo, | ||
pe as getMinAmountByChain, | ||
Vt as getSymmetricPoolShare, | ||
Mt as getSymmetricPoolShare, | ||
ae as getSymmetricWithdraw, | ||
he as getTHORNameCost, | ||
$t as isGasAsset, | ||
St as isGasAsset, | ||
de as validateTHORName | ||
}; |
@@ -45,3 +45,3 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "1.0.0-rc.21", | ||
"version": "1.0.0-rc.22", | ||
"scripts": { | ||
@@ -48,0 +48,0 @@ "build": "vite build", |
@@ -203,2 +203,35 @@ import { describe, expect, test } from 'vitest'; | ||
describe('toCurrency', () => { | ||
test('returns abbreviation with up to 3 integer digits', () => { | ||
const skNumber = new SwapKitNumber(1234.5678); | ||
expect(skNumber.toCurrency()).toBe('$1,234.56'); | ||
expect( | ||
skNumber.toCurrency('€', { | ||
decimalSeparator: ',', | ||
thousandSeparator: ' ', | ||
currencyPosition: 'end', | ||
}), | ||
).toBe('1 234,56€'); | ||
const skNumber2 = new SwapKitNumber(0.5678); | ||
expect(skNumber2.toCurrency()).toBe('$0.5678'); | ||
expect( | ||
skNumber2.toCurrency('€', { | ||
decimalSeparator: ',', | ||
currencyPosition: 'end', | ||
}), | ||
).toBe('0,5678€'); | ||
const skNumber3 = new SwapKitNumber(0.00005678); | ||
expect(skNumber3.toCurrency()).toBe('$0.000057'); | ||
expect( | ||
skNumber3.toCurrency('€', { | ||
decimalSeparator: ',', | ||
thousandSeparator: ' ', | ||
currencyPosition: 'end', | ||
}), | ||
).toBe('0,000057€'); | ||
}); | ||
}); | ||
describe('add', () => { | ||
@@ -205,0 +238,0 @@ test('adds same type numbers correctly', () => { |
@@ -95,21 +95,2 @@ import { BaseDecimal } from '@swapkit/types'; | ||
/** | ||
* @deprecated Use `getBaseValue('string')` instead | ||
*/ | ||
get baseValue() { | ||
return this.getBaseValue('string') as string; | ||
} | ||
/** | ||
* @deprecated Use `getBaseValue('number')` instead | ||
*/ | ||
get baseValueNumber() { | ||
return this.getBaseValue('number') as number; | ||
} | ||
/** | ||
* @deprecated Use `getBaseValue('bigint')` instead | ||
*/ | ||
get baseValueBigInt() { | ||
return this.getBaseValue('bigint') as bigint; | ||
} | ||
set(value: SKBigIntParams): this { | ||
@@ -288,2 +269,27 @@ // @ts-expect-error False positive | ||
toCurrency( | ||
currency = '$', | ||
{ | ||
currencyPosition = 'start', | ||
decimal = 2, | ||
decimalSeparator = '.', | ||
thousandSeparator = ',', | ||
} = {}, | ||
) { | ||
const value = this.getValue('number'); | ||
const [int, dec = ''] = value.toFixed(6).split('.'); | ||
const integer = int.replace(/\B(?=(\d{3})+(?!\d))/g, thousandSeparator); | ||
const parsedValue = | ||
!int && !dec | ||
? '0.00' | ||
: int === '0' | ||
? `${parseFloat(`0.${dec}`)}`.replace('.', decimalSeparator) | ||
: `${integer}${dec ? `${decimalSeparator}${dec.slice(0, decimal)}` : ''}`; | ||
return `${currencyPosition === 'start' ? currency : ''}${parsedValue}${ | ||
currencyPosition === 'end' ? currency : '' | ||
}`; | ||
} | ||
#arithmetics(method: 'add' | 'sub' | 'mul' | 'div', ...args: InitialisationValueType[]): this { | ||
@@ -290,0 +296,0 @@ const precisionDecimal = this.#retrievePrecisionDecimal(this, ...args); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
195755
3582