@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.14 to 1.0.0-rc.15
@@ -13,3 +13,3 @@ var bt = Object.defineProperty; | ||
var f = (s, t, e) => (wt(s, t, "access private method"), e); | ||
import { FeeOption as D, BaseDecimal as m, Chain as o, ChainToRPC as At, MemoType as g } from "@swapkit/types"; | ||
import { FeeOption as L, BaseDecimal as m, Chain as o, ChainToRPC as At, MemoType as b } from "@swapkit/types"; | ||
const Tt = "0x313ce567", nt = async ({ chain: s, to: t }) => { | ||
@@ -50,5 +50,5 @@ try { | ||
}, ee = { | ||
[D.Average]: 1.2, | ||
[D.Fast]: 1.5, | ||
[D.Fastest]: 2 | ||
[L.Average]: 1.2, | ||
[L.Fast]: 1.5, | ||
[L.Fastest]: 2 | ||
}, $t = ({ chain: s, symbol: t }) => { | ||
@@ -160,4 +160,4 @@ switch (s) { | ||
}) => { | ||
const r = d(s), n = d(t), i = d(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); | ||
return h.div(_); | ||
const r = d(s), n = d(t), i = d(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)), g = n.mul(n).mul(n); | ||
return h.div(g); | ||
}, ne = ({ | ||
@@ -199,7 +199,7 @@ percent: s, | ||
}) => { | ||
const a = d(s), c = d(e), u = d(t), l = d(n), h = d(i), _ = l.mul(c), T = h.mul(a), H = l.mul(h), ft = a.mul(c), pt = u.mul(_.add(T.add(H.mul(2)))), _t = _.add(T.add(ft.mul(2))), X = pt.div(_t), j = d(r).add(X); | ||
const a = d(s), c = d(e), u = d(t), l = d(n), h = d(i), g = l.mul(c), T = h.mul(a), j = l.mul(h), ft = a.mul(c), pt = u.mul(g.add(T.add(j.mul(2)))), _t = g.add(T.add(ft.mul(2))), X = pt.div(_t), D = d(r).add(X); | ||
if (X.baseValueNumber === 0) | ||
return j.div(u).baseValueNumber; | ||
const gt = u.add(j); | ||
return j.div(gt).baseValueNumber; | ||
return D.div(u).baseValueNumber; | ||
const gt = u.add(D); | ||
return D.div(gt).baseValueNumber; | ||
}, ce = ({ | ||
@@ -219,25 +219,25 @@ runeAmount: s, | ||
switch (s) { | ||
case g.LEAVE: | ||
case g.BOND: { | ||
case b.LEAVE: | ||
case b.BOND: { | ||
const { address: e } = t; | ||
return `${s}:${e}`; | ||
} | ||
case g.UNBOND: { | ||
case b.UNBOND: { | ||
const { address: e, unbondAmount: r } = t; | ||
return `${s}:${e}:${r * 10 ** 8}`; | ||
} | ||
case g.THORNAME_REGISTER: { | ||
case b.THORNAME_REGISTER: { | ||
const { name: e, chain: r, address: n, owner: i } = t; | ||
return `${s}:${e}:${r}:${n}${i ? `:${i}` : ""}`; | ||
} | ||
case g.DEPOSIT: { | ||
case b.DEPOSIT: { | ||
const { chain: e, symbol: r, address: n, singleSide: i } = t; | ||
return i ? `${s}:${e}/${r}::t:0` : `${s}:${e}.${r}:${n || ""}:t:0`; | ||
} | ||
case g.WITHDRAW: { | ||
case b.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}`; | ||
} | ||
case g.OPEN_LOAN: | ||
case g.CLOSE_LOAN: { | ||
case b.OPEN_LOAN: | ||
case b.CLOSE_LOAN: { | ||
const { asset: e, address: r } = t; | ||
@@ -328,3 +328,3 @@ return `${s}:${e}:${r}`; | ||
blob: "*/*" | ||
}, L = 2147483647, ut = Symbol("stop"), Ht = { | ||
}, U = 2147483647, ut = Symbol("stop"), Ht = { | ||
json: !0, | ||
@@ -407,7 +407,7 @@ parseJson: !0, | ||
}; | ||
class k { | ||
class q { | ||
static create(t, e) { | ||
const r = new k(t, e), n = async () => { | ||
if (typeof r._options.timeout == "number" && r._options.timeout > L) | ||
throw new RangeError(`The \`timeout\` option cannot be greater than ${L}`); | ||
const r = new q(t, e), n = async () => { | ||
if (typeof r._options.timeout == "number" && r._options.timeout > U) | ||
throw new RangeError(`The \`timeout\` option cannot be greater than ${U}`); | ||
await Promise.resolve(); | ||
@@ -540,3 +540,3 @@ let c = await r._fetch(); | ||
} catch (e) { | ||
const r = Math.min(this._calculateRetryDelay(e), L); | ||
const r = Math.min(this._calculateRetryDelay(e), U); | ||
if (r !== 0 && this._retryCount > 0) { | ||
@@ -605,8 +605,8 @@ await Gt(r, { signal: this._options.signal }); | ||
/*! MIT License © Sindre Sorhus */ | ||
const W = (s) => { | ||
const t = (e, r) => k.create(e, V(s, r)); | ||
const G = (s) => { | ||
const t = (e, r) => q.create(e, V(s, r)); | ||
for (const e of ct) | ||
t[e] = (r, n) => k.create(r, V(s, n, { method: e })); | ||
return t.create = (e) => W(V(e)), t.extend = (e) => W(V(s, e)), t.stop = ut, t; | ||
}, zt = W(), Jt = zt, et = Jt.create({ | ||
t[e] = (r, n) => q.create(r, V(s, n, { method: e })); | ||
return t.create = (e) => G(V(e)), t.extend = (e) => G(V(s, e)), t.stop = ut, t; | ||
}, zt = G(), Jt = zt, et = Jt.create({ | ||
headers: { | ||
@@ -631,3 +631,3 @@ referrer: "https://sk.thorswap.net", | ||
}, C = 8, v = (s) => 10n ** BigInt(s), R = (s) => Math.log10(parseFloat(s.toString())); | ||
function q({ | ||
function x({ | ||
value: s, | ||
@@ -648,8 +648,8 @@ bigIntDecimal: t = C, | ||
} | ||
var w, S, x, ht, P, dt, E, G, A, $, O, Y; | ||
const N = class N { | ||
var w, S, P, ht, N, dt, E, Y, A, $, O, z; | ||
const H = class H { | ||
constructor(t) { | ||
y(this, w); | ||
y(this, x); | ||
y(this, P); | ||
y(this, N); | ||
y(this, E); | ||
@@ -661,11 +661,11 @@ y(this, A); | ||
p(this, "decimal"); | ||
const e = U(t); | ||
const e = F(t); | ||
this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = v( | ||
Math.max(f(this, O, Y).call(this, f(this, A, $).call(this, e)), this.decimal || 0) | ||
), f(this, x, ht).call(this, e); | ||
Math.max(f(this, O, z).call(this, f(this, A, $).call(this, e)), this.decimal || 0) | ||
), f(this, P, ht).call(this, e); | ||
} | ||
static fromBigInt(t, e) { | ||
return new N({ | ||
return new H({ | ||
decimal: e, | ||
value: q({ value: t, bigIntDecimal: e, decimal: e }) | ||
value: x({ value: t, bigIntDecimal: e, decimal: e }) | ||
}); | ||
@@ -679,3 +679,3 @@ } | ||
return this.fromBigInt( | ||
new N(t).bigIntValue * v(r) / v(e), | ||
new H(t).bigIntValue * v(r) / v(e), | ||
r | ||
@@ -760,4 +760,4 @@ ); | ||
return t.bigIntValue; | ||
const r = U(t), n = f(this, A, $).call(this, r); | ||
return n === "0" || n === "undefined" ? 0n : f(this, E, G).call(this, n, e); | ||
const r = F(t), n = f(this, A, $).call(this, r); | ||
return n === "0" || n === "undefined" ? 0n : f(this, E, Y).call(this, n, e); | ||
} | ||
@@ -811,3 +811,3 @@ formatBigIntToSafeValue(t, e) { | ||
w = new WeakSet(), S = function(t, ...e) { | ||
const r = f(this, P, dt).call(this, this, ...e), n = v(r), i = e.reduce( | ||
const r = f(this, N, dt).call(this, this, ...e), n = v(r), i = e.reduce( | ||
(c, u) => { | ||
@@ -833,3 +833,3 @@ const l = this.getBigIntValue(u, r); | ||
this.bigIntValue * n / this.decimalMultiplier | ||
), a = q({ | ||
), a = x({ | ||
bigIntDecimal: r, | ||
@@ -840,11 +840,11 @@ decimal: Math.max(r, R(this.decimalMultiplier)), | ||
return new this.constructor({ decimal: this.decimal, value: a, identifier: this.toString() }); | ||
}, x = new WeakSet(), ht = function(t) { | ||
}, P = new WeakSet(), ht = function(t) { | ||
const e = f(this, A, $).call(this, t) || "0"; | ||
this.bigIntValue = f(this, E, G).call(this, e); | ||
}, P = new WeakSet(), dt = function(...t) { | ||
this.bigIntValue = f(this, E, Y).call(this, e); | ||
}, N = new WeakSet(), dt = function(...t) { | ||
const e = t.map( | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : f(this, O, Y).call(this, f(this, A, $).call(this, r)) | ||
(r) => typeof r == "object" ? r.decimal || R(r.decimalMultiplier) : f(this, O, z).call(this, f(this, A, $).call(this, r)) | ||
).filter(Boolean); | ||
return Math.max(...e, C); | ||
}, E = new WeakSet(), G = function(t, e) { | ||
}, E = new WeakSet(), Y = function(t, e) { | ||
const r = e ? v(e) : this.decimalMultiplier, n = R(r), [i = "", a = ""] = t.split("."); | ||
@@ -856,5 +856,5 @@ return BigInt(`${i}${a.padEnd(n, "0")}`); | ||
maximumFractionDigits: 20 | ||
}) : U(t)}`.replaceAll(",", ".").split("."); | ||
}) : F(t)}`.replaceAll(",", ".").split("."); | ||
return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0]; | ||
}, O = new WeakSet(), Y = function(t) { | ||
}, O = new WeakSet(), z = function(t) { | ||
var r; | ||
@@ -864,18 +864,18 @@ const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0; | ||
}; | ||
let I = N; | ||
function U(s) { | ||
let I = H; | ||
function F(s) { | ||
return typeof s == "object" ? "getValue" in s ? s.getValue("string") : s.value : s; | ||
} | ||
const M = (s, t) => typeof s == "bigint" ? q({ value: s, bigIntDecimal: t, decimal: t }) : s; | ||
let z; | ||
const F = (s) => { | ||
if (!z) | ||
const M = (s, t) => typeof s == "bigint" ? x({ value: s, bigIntDecimal: t, decimal: t }) : s; | ||
let J; | ||
const W = (s) => { | ||
if (!J) | ||
throw new Error("Static assets not loaded, call await AssetValue.loadStaticAssets() first"); | ||
return z.get(s.toUpperCase()) || { decimal: m.THOR, identifier: "" }; | ||
return J.get(s.toUpperCase()) || { decimal: m.THOR, identifier: "" }; | ||
}, st = async (s, t = 0) => { | ||
Qt(s); | ||
const e = await it(J(s)), r = M(t, e); | ||
return new b({ decimal: e, value: r, identifier: s }); | ||
const e = await it(k(s)), r = M(t, e); | ||
return new _({ decimal: e, value: r, identifier: s }); | ||
}; | ||
class b extends I { | ||
class _ extends I { | ||
constructor(e) { | ||
@@ -892,3 +892,3 @@ super( | ||
p(this, "type"); | ||
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, n = J(r); | ||
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, n = k(r); | ||
this.type = It(n), this.chain = n.chain, this.ticker = n.ticker, this.symbol = n.symbol, this.address = n.address, this.isSynthetic = n.isSynthetic, this.isGasAsset = n.isGasAsset; | ||
@@ -900,3 +900,3 @@ } | ||
toString(e = !1) { | ||
const r = this.isSynthetic ? this.ticker : `${this.chain}.${this.ticker}`; | ||
const r = this.isSynthetic ? this.symbol.split("-")[0] : `${this.chain}.${this.ticker}`; | ||
return e ? r : ( | ||
@@ -914,6 +914,6 @@ // THOR.ETH/ETH | ETH.USDT-0x1234567890 | ||
static fromStringSync(e, r = 0) { | ||
const { decimal: n, identifier: i } = F( | ||
const { isSynthetic: n } = k(e), { decimal: i, identifier: a } = W( | ||
e | ||
), a = M(r, n); | ||
return i ? new b({ decimal: n, identifier: i, value: a }) : void 0; | ||
), c = M(r, i); | ||
return a ? new _({ decimal: i, identifier: a, value: c }) : n ? new _({ decimal: 8, identifier: e, value: c }) : void 0; | ||
} | ||
@@ -924,15 +924,15 @@ static async fromIdentifier(e, r = 0) { | ||
static fromIdentifierSync(e, r = 0) { | ||
const { decimal: n, identifier: i } = F(e), a = M(r, n); | ||
return new b({ decimal: n, identifier: i, value: a }); | ||
const { decimal: n, identifier: i } = W(e), a = M(r, n); | ||
return new _({ decimal: n, identifier: i, value: a }); | ||
} | ||
static fromChainOrSignature(e, r = 0) { | ||
const { decimal: n, identifier: i } = Ct(e), a = M(r, n); | ||
return new b({ value: a, decimal: n, identifier: i }); | ||
return new _({ value: a, decimal: n, identifier: i }); | ||
} | ||
static async fromTCQuote(e, r = 0) { | ||
const n = await it(J(e)), i = this.shiftDecimals({ value: r, from: m.THOR, to: n }); | ||
return new b({ value: i, identifier: e, decimal: n }); | ||
const n = await it(k(e)), i = this.shiftDecimals({ value: r, from: m.THOR, to: n }); | ||
return new _({ value: i, identifier: e, decimal: n }); | ||
} | ||
static fromTCQuoteStatic(e, r = 0) { | ||
const n = F(e), i = this.shiftDecimals({ | ||
const n = W(e), i = this.shiftDecimals({ | ||
value: r, | ||
@@ -942,3 +942,3 @@ from: m.THOR, | ||
}); | ||
return new b({ ...n, value: i }); | ||
return new _({ ...n, value: i }); | ||
} | ||
@@ -956,6 +956,6 @@ static async loadStaticAssets() { | ||
} = await import("@swapkit/tokens"); | ||
z = [i, ...Object.values(a)].reduce( | ||
(u, { tokens: l }) => (l.forEach(({ identifier: h, chain: _, ...T }) => { | ||
const H = "decimals" in T ? T.decimals : m[_]; | ||
u.set(h, { identifier: h, decimal: H }); | ||
J = [i, ...Object.values(a)].reduce( | ||
(u, { tokens: l }) => (l.forEach(({ identifier: h, chain: g, ...T }) => { | ||
const j = "decimals" in T ? T.decimals : m[g]; | ||
u.set(h, { identifier: h, decimal: j }); | ||
}), u), | ||
@@ -976,3 +976,3 @@ /* @__PURE__ */ new Map() | ||
const me = (s) => { | ||
const t = b.fromChainOrSignature(s); | ||
const t = _.fromChainOrSignature(s); | ||
switch (s) { | ||
@@ -994,11 +994,11 @@ case o.Bitcoin: | ||
} | ||
}, J = (s) => { | ||
const t = s.slice(0, 14).includes("/"), e = s.includes(".") ? s : `${o.THORChain}.${s}`, [r, n] = e.split("."), [i, a] = n.split("-"); | ||
}, k = (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("-"); | ||
return { | ||
address: a == null ? void 0 : a.toLowerCase(), | ||
chain: r, | ||
isGasAsset: $t({ chain: r, symbol: n }), | ||
address: u == null ? void 0 : u.toLowerCase(), | ||
chain: i, | ||
isGasAsset: $t({ chain: i, symbol: a }), | ||
isSynthetic: t, | ||
symbol: a ? `${i}-${(a == null ? void 0 : a.toLowerCase()) ?? ""}` : n, | ||
ticker: t ? n : i | ||
symbol: (t ? `${e}/` : "") + (u ? `${c}-${(u == null ? void 0 : u.toLowerCase()) ?? ""}` : a), | ||
ticker: c | ||
}; | ||
@@ -1074,3 +1074,3 @@ }, Xt = { | ||
decimal: e, | ||
value: q({ value: t, bigIntDecimal: e, decimal: e }) | ||
value: x({ value: t, bigIntDecimal: e, decimal: e }) | ||
}); | ||
@@ -1080,3 +1080,3 @@ } | ||
export { | ||
b as AssetValue, | ||
_ as AssetValue, | ||
I as BigIntArithmetics, | ||
@@ -1089,3 +1089,3 @@ Kt as RequestClient, | ||
se as filterAssets, | ||
q as formatBigIntToSafeValue, | ||
x as formatBigIntToSafeValue, | ||
ee as gasFeeMultiplier, | ||
@@ -1092,0 +1092,0 @@ It as getAssetType, |
@@ -45,3 +45,3 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "1.0.0-rc.14", | ||
"version": "1.0.0-rc.15", | ||
"scripts": { | ||
@@ -48,0 +48,0 @@ "build": "vite build", |
@@ -31,3 +31,3 @@ import { BaseDecimal, Chain } from '@swapkit/types'; | ||
expect(ethSynth.assetValue).toBe('1234567890 ETH/ETH'); | ||
expect(ethSynth.assetValue).toBe('1234567890 ETH'); | ||
expect(ethSynth.toString()).toBe('THOR.ETH/ETH'); | ||
@@ -34,0 +34,0 @@ expect(ethSynth.toString(true)).toBe('ETH/ETH'); |
@@ -104,3 +104,5 @@ import type { | ||
// THOR.RUNE | ETH/ETH | ||
const shortFormat = this.isSynthetic ? this.ticker : `${this.chain}.${this.ticker}`; | ||
const shortFormat = this.isSynthetic | ||
? this.symbol.split('-')[0] | ||
: `${this.chain}.${this.ticker}`; | ||
@@ -122,2 +124,3 @@ return short | ||
static fromStringSync(assetString: string, value: NumberPrimitives = 0) { | ||
const { isSynthetic } = getAssetInfo(assetString); | ||
const { decimal, identifier: tokenIdentifier } = getStaticToken( | ||
@@ -131,2 +134,4 @@ assetString as unknown as TokenNames, | ||
? new AssetValue({ decimal, identifier: tokenIdentifier, value: parsedValue }) | ||
: isSynthetic | ||
? new AssetValue({ decimal: 8, identifier: assetString, value: parsedValue }) | ||
: undefined; | ||
@@ -242,8 +247,8 @@ } | ||
const isSynthetic = identifier.slice(0, 14).includes('/'); | ||
const adjustedIdentifier = identifier.includes('.') | ||
? identifier | ||
: `${Chain.THORChain}.${identifier}`; | ||
const [synthChain, synthSymbol] = identifier.split('.').pop()!.split('/'); | ||
const adjustedIdentifier = | ||
identifier.includes('.') && !isSynthetic ? identifier : `${Chain.THORChain}.${synthSymbol}`; | ||
const [chain, symbol] = adjustedIdentifier.split('.') as [Chain, string]; | ||
const [ticker, address] = symbol.split('-') as [string, string?]; | ||
const [ticker, address] = (isSynthetic ? synthSymbol : symbol).split('-') as [string, string?]; | ||
@@ -255,5 +260,7 @@ return { | ||
isSynthetic, | ||
symbol: address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol, | ||
ticker: isSynthetic ? symbol : ticker, | ||
symbol: | ||
(isSynthetic ? `${synthChain}/` : '') + | ||
(address ? `${ticker}-${address?.toLowerCase() ?? ''}` : symbol), | ||
ticker: ticker, | ||
}; | ||
}; |
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
193258
3558