@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.5 to 1.0.0-rc.6
@@ -33,3 +33,3 @@ import { Chain } from '@swapkit/types'; | ||
get assetValue(): string; | ||
toString(): string; | ||
toString(short?: boolean): string; | ||
eq({ chain, symbol }: { | ||
@@ -77,4 +77,13 @@ chain: Chain; | ||
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; | ||
@@ -160,2 +169,4 @@ set(value: SKBigIntParams): any; | ||
export declare const filterAssets: (assets: AssetValue[]) => AssetValue[]; | ||
export declare function formatBigIntToSafeValue({ value, bigIntDecimal, decimal, }: { | ||
@@ -162,0 +173,0 @@ value: bigint; |
var nt = Object.defineProperty; | ||
var rt = (n, t, e) => t in n ? nt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e; | ||
var g = (n, t, e) => (rt(n, typeof t != "symbol" ? t + "" : t, e), e), st = (n, t, e) => { | ||
var _ = (n, t, e) => (rt(n, typeof t != "symbol" ? t + "" : t, e), e), st = (n, t, e) => { | ||
if (!t.has(n)) | ||
@@ -12,9 +12,9 @@ throw TypeError("Cannot " + e); | ||
}; | ||
var _ = (n, t, e) => (st(n, t, "access private method"), e); | ||
var g = (n, t, e) => (st(n, t, "access private method"), e); | ||
import { FeeOption as H, BaseDecimal as m, Chain as i, ChainToRPC as it, MemoType as p } from "@swapkit/types"; | ||
const St = (n) => { | ||
const vt = (n) => { | ||
if (n < 0) | ||
throw new Error("Invalid number of year"); | ||
return 10 + n; | ||
}, vt = (n) => { | ||
}, Vt = (n) => { | ||
if (n.length > 30) | ||
@@ -24,3 +24,3 @@ return !1; | ||
return !!n.match(t); | ||
}, Vt = ([n, t, e, r, s]) => `${n}'/${t}'/${e}'/${r}${typeof s != "number" ? "" : `/${s}`}`, Bt = async (n, t) => { | ||
}, Ct = ([n, t, e, r, s]) => `${n}'/${t}'/${e}'/${r}${typeof s != "number" ? "" : `/${s}`}`, Tt = async (n, t) => { | ||
try { | ||
@@ -86,3 +86,3 @@ const e = Object.entries(t || {}).reduce( | ||
} | ||
}, Ct = { | ||
}, Bt = { | ||
[H.Average]: 1.2, | ||
@@ -180,3 +180,6 @@ [H.Fast]: 1.5, | ||
return { chain: t, symbol: s, ticker: a, synth: r }; | ||
}, _t = ({ | ||
}, gt = new RegExp( | ||
/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/, | ||
"gmi" | ||
), Et = (n) => n.filter((t) => !gt.test(t.toString())), _t = ({ | ||
liquidityUnits: n, | ||
@@ -188,3 +191,3 @@ poolUnits: t, | ||
return c.mul(o.sub(u).add(l)).div(d); | ||
}, gt = ({ | ||
}, pt = ({ | ||
liquidityUnits: n, | ||
@@ -196,3 +199,3 @@ poolUnits: t, | ||
return d.div(A); | ||
}, Tt = ({ | ||
}, Ot = ({ | ||
percent: n, | ||
@@ -202,3 +205,3 @@ runeDepth: t, | ||
poolUnits: r | ||
}) => _t({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Et = ({ | ||
}) => _t({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Mt = ({ | ||
percent: n, | ||
@@ -208,3 +211,3 @@ assetDepth: t, | ||
poolUnits: r | ||
}) => gt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), h = (n) => new wt({ value: n, decimal: m.THOR }), pt = ({ | ||
}) => pt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), h = (n) => new bt({ value: n, decimal: m.THOR }), ft = ({ | ||
liquidityUnits: n, | ||
@@ -217,3 +220,3 @@ poolUnits: t, | ||
runeAmount: h(e).mul(n).div(t) | ||
}), Ot = ({ | ||
}), Nt = ({ | ||
liquidityUnits: n, | ||
@@ -225,6 +228,6 @@ poolUnits: t, | ||
}) => Object.fromEntries( | ||
Object.entries(pt({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map( | ||
Object.entries(ft({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map( | ||
([a, c]) => [a, c.mul(s)] | ||
) | ||
), Mt = ({ | ||
), Rt = ({ | ||
runeDepth: n, | ||
@@ -237,8 +240,8 @@ poolUnits: t, | ||
}) => { | ||
const c = h(n), o = h(e), u = h(t), l = h(s), d = h(a), A = l.mul(o), S = d.mul(c), R = l.mul(d), Q = c.mul(o), Z = u.mul(A.add(S.add(R.mul(2)))), tt = A.add(S.add(Q.mul(2))), U = Z.div(tt), k = h(r).add(U); | ||
if (U.baseValueNumber === 0) | ||
const c = h(n), o = h(e), u = h(t), l = h(s), d = h(a), A = l.mul(o), S = d.mul(c), R = l.mul(d), Q = c.mul(o), Z = u.mul(A.add(S.add(R.mul(2)))), tt = A.add(S.add(Q.mul(2))), F = Z.div(tt), k = h(r).add(F); | ||
if (F.baseValueNumber === 0) | ||
return k.div(u).baseValueNumber; | ||
const et = u.add(k); | ||
return k.div(et).baseValueNumber; | ||
}, Nt = ({ | ||
}, kt = ({ | ||
runeAmount: n, | ||
@@ -251,7 +254,7 @@ assetAmount: t, | ||
return Math.abs(u.div(l).baseValueNumber); | ||
}, ft = ({ | ||
}, At = ({ | ||
symbol: n, | ||
ticker: t, | ||
chain: e | ||
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Rt = (n, t) => { | ||
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Ht = (n, t) => { | ||
switch (n) { | ||
@@ -276,3 +279,3 @@ case p.LEAVE: | ||
case p.WITHDRAW: { | ||
const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t, u = !o && c ? `:${c}` : "", l = ft({ chain: e, symbol: s, ticker: r }); | ||
const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t, u = !o && c ? `:${c}` : "", l = At({ chain: e, symbol: s, ticker: r }); | ||
return `${n}:${e}${o ? "/" : "."}${l}:${a}${u}`; | ||
@@ -288,8 +291,8 @@ } | ||
} | ||
}, F = Object.values(i), At = (n = "") => { | ||
}, U = Object.values(i), $t = (n = "") => { | ||
const t = n.toUpperCase(), [e] = t.split("."); | ||
if (F.includes(e)) | ||
if (U.includes(e)) | ||
return !0; | ||
const [r] = t.split("/"); | ||
if (F.includes(r)) | ||
if (U.includes(r)) | ||
return !0; | ||
@@ -299,7 +302,7 @@ throw new Error( | ||
); | ||
}, C = 8, I = (n) => 10n ** BigInt(n), v = (n) => Math.log10(parseFloat(n.toString())); | ||
}, T = 8, I = (n) => 10n ** BigInt(n), v = (n) => Math.log10(parseFloat(n.toString())); | ||
function G({ | ||
value: n, | ||
bigIntDecimal: t = C, | ||
decimal: e = C | ||
bigIntDecimal: t = T, | ||
decimal: e = T | ||
}) { | ||
@@ -317,4 +320,4 @@ const r = n < 0n; | ||
} | ||
var w, V, M, J, N, z, E, L, b, B, O, x; | ||
const y = class y { | ||
var w, V, M, J, N, z, E, D, b, C, O, L; | ||
const B = class B { | ||
constructor(t) { | ||
@@ -327,12 +330,12 @@ $(this, w); | ||
$(this, O); | ||
g(this, "decimalMultiplier", 10n ** 8n); | ||
g(this, "bigIntValue", 0n); | ||
g(this, "decimal"); | ||
_(this, "decimalMultiplier", 10n ** 8n); | ||
_(this, "bigIntValue", 0n); | ||
_(this, "decimal"); | ||
const e = P(t); | ||
this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = I( | ||
Math.max(_(this, O, x).call(this, _(this, b, B).call(this, e)), this.decimal || 0) | ||
), _(this, M, J).call(this, e); | ||
Math.max(g(this, O, L).call(this, g(this, b, C).call(this, e)), this.decimal || 0) | ||
), g(this, M, J).call(this, e); | ||
} | ||
static fromBigInt(t, e) { | ||
return new y({ | ||
return new B({ | ||
decimal: e, | ||
@@ -347,13 +350,22 @@ value: G({ value: t, bigIntDecimal: e, decimal: e }) | ||
}) { | ||
return y.fromBigInt( | ||
new y(t).bigIntValue * I(r) / I(e), | ||
return B.fromBigInt( | ||
new B(t).bigIntValue * I(r) / I(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() { | ||
@@ -366,12 +378,12 @@ return this.getBaseValue("bigint"); | ||
add(...t) { | ||
return _(this, w, V).call(this, "add", ...t); | ||
return g(this, w, V).call(this, "add", ...t); | ||
} | ||
sub(...t) { | ||
return _(this, w, V).call(this, "sub", ...t); | ||
return g(this, w, V).call(this, "sub", ...t); | ||
} | ||
mul(...t) { | ||
return _(this, w, V).call(this, "mul", ...t); | ||
return g(this, w, V).call(this, "mul", ...t); | ||
} | ||
div(...t) { | ||
return _(this, w, V).call(this, "div", ...t); | ||
return g(this, w, V).call(this, "div", ...t); | ||
} | ||
@@ -419,6 +431,6 @@ gt(t) { | ||
getBigIntValue(t, e) { | ||
return !e && typeof t == "object" ? t.bigIntValue : _(this, E, L).call(this, _(this, b, B).call(this, P(t)), e); | ||
return !e && typeof t == "object" ? t.bigIntValue : g(this, E, D).call(this, g(this, b, C).call(this, P(t)), e); | ||
} | ||
formatBigIntToSafeValue(t, e) { | ||
const r = e || this.decimal || C, s = Math.max( | ||
const r = e || this.decimal || T, s = Math.max( | ||
r, | ||
@@ -452,3 +464,3 @@ v(this.decimalMultiplier) | ||
w = new WeakSet(), V = function(t, ...e) { | ||
const r = _(this, N, z).call(this, this, ...e), s = I(r), a = e.reduce( | ||
const r = g(this, N, z).call(this, this, ...e), s = I(r), a = e.reduce( | ||
(o, u) => { | ||
@@ -481,13 +493,13 @@ const l = this.getBigIntValue(u, r); | ||
}, M = new WeakSet(), J = function(t) { | ||
const e = _(this, b, B).call(this, t) || "0"; | ||
this.bigIntValue = _(this, E, L).call(this, e); | ||
const e = g(this, b, C).call(this, t) || "0"; | ||
this.bigIntValue = g(this, E, D).call(this, e); | ||
}, N = new WeakSet(), z = function(...t) { | ||
const e = t.map( | ||
(r) => typeof r == "object" ? r.decimal || v(r.decimalMultiplier) : _(this, O, x).call(this, _(this, b, B).call(this, r)) | ||
(r) => typeof r == "object" ? r.decimal || v(r.decimalMultiplier) : g(this, O, L).call(this, g(this, b, C).call(this, r)) | ||
).filter(Boolean); | ||
return Math.max(...e, C); | ||
}, E = new WeakSet(), L = function(t, e) { | ||
return Math.max(...e, T); | ||
}, E = new WeakSet(), D = function(t, e) { | ||
const r = e ? I(e) : this.decimalMultiplier, s = v(r), [a, c = ""] = t.split("."); | ||
return BigInt(`${a}${c.padEnd(s, "0")}`); | ||
}, b = new WeakSet(), B = function(t) { | ||
}, b = new WeakSet(), C = function(t) { | ||
const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", { | ||
@@ -498,8 +510,8 @@ useGrouping: !1, | ||
return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0]; | ||
}, O = new WeakSet(), x = function(t) { | ||
}, O = new WeakSet(), L = function(t) { | ||
var r; | ||
const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0; | ||
return Math.max(e, C); | ||
return Math.max(e, T); | ||
}; | ||
let T = y; | ||
let y = B; | ||
function P(n) { | ||
@@ -509,3 +521,3 @@ return typeof n == "object" ? "getValue" in n ? n.getValue("string") : n.value : n; | ||
let j; | ||
const D = (n) => { | ||
const x = (n) => { | ||
if (!j) | ||
@@ -515,18 +527,18 @@ throw new Error("Static assets not loaded, call await AssetValue.loadStaticAssets() first"); | ||
}, W = async (n, t = 0) => { | ||
At(n); | ||
$t(n); | ||
const e = await K(q(n)); | ||
return new f({ decimal: e, value: t, identifier: n }); | ||
}; | ||
class f extends T { | ||
class f extends y { | ||
constructor(e) { | ||
super( | ||
e.value instanceof T ? e.value : { decimal: e.decimal, value: e.value } | ||
e.value instanceof y ? e.value : { decimal: e.decimal, value: e.value } | ||
); | ||
g(this, "address"); | ||
g(this, "chain"); | ||
g(this, "isSynthetic", !1); | ||
g(this, "isGasAsset", !1); | ||
g(this, "symbol"); | ||
g(this, "ticker"); | ||
g(this, "type"); | ||
_(this, "address"); | ||
_(this, "chain"); | ||
_(this, "isSynthetic", !1); | ||
_(this, "isGasAsset", !1); | ||
_(this, "symbol"); | ||
_(this, "ticker"); | ||
_(this, "type"); | ||
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s = q(r); | ||
@@ -538,4 +550,8 @@ this.type = mt(s), this.chain = s.chain, this.ticker = s.ticker, this.symbol = s.symbol, this.address = s.address, this.isSynthetic = s.isSynthetic, this.isGasAsset = s.isGasAsset; | ||
} | ||
toString() { | ||
return `${this.chain}.${this.symbol}`; | ||
toString(e = !1) { | ||
const r = this.isSynthetic ? this.ticker : `${this.chain}.${this.ticker}`; | ||
return e ? r : ( | ||
// THOR.ETH/ETH | ETH.USDT-0x1234567890 | ||
`${this.chain}.${this.symbol}` | ||
); | ||
} | ||
@@ -549,3 +565,3 @@ eq({ chain: e, symbol: r }) { | ||
static fromStringSync(e, r = 0) { | ||
const { decimal: s, identifier: a } = D( | ||
const { decimal: s, identifier: a } = x( | ||
e | ||
@@ -559,3 +575,3 @@ ); | ||
static fromIdentifierSync(e, r = 0) { | ||
const { decimal: s, identifier: a } = D(e); | ||
const { decimal: s, identifier: a } = x(e); | ||
return new f({ decimal: s, identifier: a, value: r }); | ||
@@ -572,3 +588,3 @@ } | ||
static fromTCQuoteStatic(e, r = 0) { | ||
const s = D(e), a = this.shiftDecimals({ | ||
const s = x(e), a = this.shiftDecimals({ | ||
value: r, | ||
@@ -609,3 +625,3 @@ from: m.THOR, | ||
} | ||
const kt = (n) => { | ||
const Pt = (n) => { | ||
const t = f.fromChainOrSignature(n); | ||
@@ -638,3 +654,3 @@ switch (n) { | ||
}; | ||
}, $t = { | ||
}, wt = { | ||
/** | ||
@@ -698,6 +714,6 @@ * Core | ||
constructor(t, e) { | ||
console.error(e), super(t, { cause: { code: $t[t], message: t } }), Object.setPrototypeOf(this, X.prototype); | ||
console.error(e), super(t, { cause: { code: wt[t], message: t } }), Object.setPrototypeOf(this, X.prototype); | ||
} | ||
} | ||
class wt extends T { | ||
class bt extends y { | ||
eq(t) { | ||
@@ -709,27 +725,28 @@ return this.eqValue(t); | ||
f as AssetValue, | ||
T as BigIntArithmetics, | ||
y as BigIntArithmetics, | ||
X as SwapKitError, | ||
wt as SwapKitNumber, | ||
bt as SwapKitNumber, | ||
yt as assetFromString, | ||
Vt as derivationPathToString, | ||
Ct as derivationPathToString, | ||
Et as filterAssets, | ||
G as formatBigIntToSafeValue, | ||
Ct as gasFeeMultiplier, | ||
Bt as gasFeeMultiplier, | ||
mt as getAssetType, | ||
gt as getAsymmetricAssetShare, | ||
Et as getAsymmetricAssetWithdrawAmount, | ||
pt as getAsymmetricAssetShare, | ||
Mt as getAsymmetricAssetWithdrawAmount, | ||
_t as getAsymmetricRuneShare, | ||
Tt as getAsymmetricRuneWithdrawAmount, | ||
Ot as getAsymmetricRuneWithdrawAmount, | ||
ht as getCommonAssetInfo, | ||
K as getDecimal, | ||
Mt as getEstimatedPoolShare, | ||
Nt as getLiquiditySlippage, | ||
Rt as getMemoFor, | ||
kt as getMinAmountByChain, | ||
Bt as getRequest, | ||
pt as getSymmetricPoolShare, | ||
Ot as getSymmetricWithdraw, | ||
St as getTHORNameCost, | ||
Rt as getEstimatedPoolShare, | ||
kt as getLiquiditySlippage, | ||
Ht as getMemoFor, | ||
Pt as getMinAmountByChain, | ||
Tt as getRequest, | ||
ft as getSymmetricPoolShare, | ||
Nt as getSymmetricWithdraw, | ||
vt as getTHORNameCost, | ||
dt as isGasAsset, | ||
at as postRequest, | ||
vt as validateTHORName | ||
Vt as validateTHORName | ||
}; |
@@ -9,4 +9,4 @@ { | ||
"@internal/config": "0.0.0-internal.0", | ||
"@swapkit/tokens": "1.0.0-rc.4", | ||
"@swapkit/types": "1.0.0-rc.4" | ||
"@swapkit/tokens": "1.0.0-rc.5", | ||
"@swapkit/types": "1.0.0-rc.5" | ||
}, | ||
@@ -17,4 +17,4 @@ "eslintConfig": { | ||
"peerDependencies": { | ||
"@swapkit/tokens": "1.0.0-rc.4", | ||
"@swapkit/types": "1.0.0-rc.4" | ||
"@swapkit/tokens": "1.0.0-rc.5", | ||
"@swapkit/types": "1.0.0-rc.5" | ||
}, | ||
@@ -44,3 +44,3 @@ "exports": { | ||
"types": "./dist/index.d.ts", | ||
"version": "1.0.0-rc.5", | ||
"version": "1.0.0-rc.6", | ||
"scripts": { | ||
@@ -47,0 +47,0 @@ "build": "vite build", |
import type { EVMChain } from '@swapkit/types'; | ||
import { BaseDecimal, Chain, ChainToRPC, FeeOption } from '@swapkit/types'; | ||
import type { AssetValue } from '../index.ts'; | ||
import { postRequest } from './others.ts'; | ||
@@ -184,1 +186,8 @@ | ||
}; | ||
const potentialScamRegex = new RegExp( | ||
/(.)\1{6}|\.ORG|\.NET|\.FINANCE|\.COM|WWW|HTTP|\\\\|\/\/|[\s$%:[\]]/, | ||
'gmi', | ||
); | ||
export const filterAssets = (assets: AssetValue[]) => | ||
assets.filter((asset) => !potentialScamRegex.test(asset.toString())); |
@@ -16,2 +16,6 @@ import { BaseDecimal, Chain } from '@swapkit/types'; | ||
expect(fakeAvaxUSDCAsset.assetValue).toBe('1234567890 USDC'); | ||
expect(fakeAvaxUSDCAsset.toString()).toBe( | ||
'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e', | ||
); | ||
expect(fakeAvaxUSDCAsset.toString(true)).toBe('AVAX.USDC'); | ||
@@ -30,2 +34,4 @@ const thor = AssetValue.fromChainOrSignature('ETH.THOR'); | ||
expect(ethSynth.toString()).toBe('THOR.ETH/ETH'); | ||
expect(ethSynth.toString(true)).toBe('ETH/ETH'); | ||
expect(ethSynth.mul(21.37).getValue('string')).toBe('26382715809.3'); | ||
@@ -40,2 +46,3 @@ const atomDerived = new AssetValue({ | ||
expect(atomDerived.toString()).toBe('THOR.ATOM'); | ||
expect(atomDerived.toString(true)).toBe('THOR.ATOM'); | ||
}); | ||
@@ -42,0 +49,0 @@ }); |
@@ -94,4 +94,10 @@ import type { | ||
toString() { | ||
return `${this.chain}.${this.symbol}`; | ||
toString(short = false) { | ||
// THOR.RUNE | ETH/ETH | ||
const shortFormat = this.isSynthetic ? this.ticker : `${this.chain}.${this.ticker}`; | ||
return short | ||
? shortFormat | ||
: // THOR.ETH/ETH | ETH.USDT-0x1234567890 | ||
`${this.chain}.${this.symbol}`; | ||
} | ||
@@ -98,0 +104,0 @@ |
@@ -93,8 +93,17 @@ import type { SwapKitNumber } from './swapKitNumber.ts'; | ||
/** | ||
* @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() { | ||
@@ -101,0 +110,0 @@ return this.getBaseValue('bigint') as bigint; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
155226
3058