@swapkit/helpers
Advanced tools
Comparing version 1.0.0-rc.70 to 1.0.0-rc.71
@@ -60,2 +60,4 @@ import { Chain } from '@swapkit/types'; | ||
static fromStringSync(assetString: string, value?: NumberPrimitives): AssetValue; | ||
static fromStringWithBase(assetString: string, value?: NumberPrimitives, baseDecimal?: number): Promise<AssetValue>; | ||
static fromStringWithBaseSync(assetString: string, value?: NumberPrimitives, baseDecimal?: number): AssetValue; | ||
static fromIdentifierSync(assetString: TokenNames, value?: NumberPrimitives): AssetValue; | ||
@@ -62,0 +64,0 @@ static fromChainOrSignature(assetString: CommonAssetString, value?: NumberPrimitives): AssetValue; |
@@ -13,7 +13,7 @@ var ye = Object.defineProperty; | ||
var T = (e, t, n) => (Ve(e, t, "access private method"), n); | ||
import { FeeOption as Y, BaseDecimal as d, Chain as l, EVMChainList as Be, ChainToRPC as Ue, MemoType as E, ChainToChainId as Le } from "@swapkit/types"; | ||
import { FeeOption as Y, BaseDecimal as d, Chain as l, EVMChainList as Be, ChainToRPC as Ue, MemoType as I, ChainToChainId as Le } from "@swapkit/types"; | ||
var u = /* @__PURE__ */ ((e) => (e.INVALID_INPUT_PARAMETERS = "1000", e.UNKNOWN_PROVIDERS = "1001", e.CANNOT_FIND_INBOUND_ADDRESS = "1002", e.NO_INBOUND_ADDRESSES = "1003", e.CHAIN_HALTED_OR_UNSUPPORTED = "1004", e.MISSING_INPUT_PARAMETER = "1005", e.INVALID_TYPE_GENERIC = "1100", e.INVALID_NUMBER_STRING = "1101", e.INVALID_NUMBER = "1102", e.INVALID_BOOLEAN = "1103", e.INVALID_OBJECT = "1104", e.INVALID_ARRAY = "1105", e.SELL_AMOUNT_MUST_BE_POSITIVE_INTEGER = "2000", e.SELL_BUY_ASSETS_ARE_THE_SAME = "2001", e.MISSING_SOURCE_ADDRESS_FOR_SYNTH = "2002", e.AFF_ADDRESS_AND_BPS_OR_NEITHER = "2003", e.AFF_ADDRESS_TOO_LONG = "2004", e.AFF_BPS_INTEGER_MAX_500 = "2005", e.SOURCE_ADDRESS_INVALID_FOR_SELL_CHAIN = "2006", e.DESTINATION_ADDRESS_INVALID_FOR_BUY_CHAIN = "2007", e.PREFERRED_PROVIDER_NOT_SUPPORTED = "2008", e.DESTINATION_ADDRESS_SMART_CONTRACT = "2009", e.BUY_AMOUNT_MUST_BE_POSITIVE_INTEGER = "2010", e.SOURCE_ADDRESS_SMART_CONTRACT = "2011", e.SWAP_AMOUNT_TOO_LOW = "2012", e.INVALID_PROVIDER = "2100", e.MISSING_CROSS_CHAIN_PROVIDER = "2101", e.MISSING_AVAX_PROVIDER = "2102", e.MISSING_BSC_PROVIDER = "2103", e.MISSING_ETH_PROVIDER = "2104", e.INVALID_PROVIDER_FOR_SWAP_OUT = "2105", e.MISSING_ARB_PROVIDER = "2106", e.INVALID_CHAIN = "2200", e.INVALID_ASSET = "2201", e.INVALID_ASSET_IDENTIFIER = "2202", e.UNSUPPORTED_CHAIN = "2204", e.UNSUPPORTED_ASSET = "2203", e.UNSUPPORTED_ASSET_FOR_SWAPOUT = "2205", e.INVALID_SOURCE_ADDRESS = "2300", e.INVALID_DESTINATION_ADDRESS = "2301", e.THORNODE_QUOTE_GENERIC_ERROR = "3000", e.NOT_ENOUGH_SYNTH_BALANCE = "3001", e.SYNTH_MINTING_CAP_REACHED = "3002", e.INVALID_QUOTE_MODE = "4000", e.NO_QUOTES = "4001", e.SERVICE_UNAVAILABLE_GENERIC = "5000", e.MISSING_GAS_DATA_GENERIC = "5100", e.MISSING_TOKEN_INFO_GENERIC = "5200", e.CANT_FIND_TOKEN_LIST = "5201", e.NO_PRICE = "5202", e.PRICE_IS_STALE = "5203", e.ADDRESS_NOT_WHITELISTED = "6000", e.ADDRESS_ALREADY_CLAIMED = "6001", e.TEMPORARY_ERROR = "9999", e))(u || {}); | ||
u.INVALID_INPUT_PARAMETERS + "", u.UNKNOWN_PROVIDERS + "", u.CANNOT_FIND_INBOUND_ADDRESS + "", u.NO_INBOUND_ADDRESSES + "", u.CHAIN_HALTED_OR_UNSUPPORTED + "", u.MISSING_INPUT_PARAMETER + "", u.INVALID_TYPE_GENERIC + "", u.INVALID_NUMBER_STRING + "", u.INVALID_NUMBER + "", u.INVALID_BOOLEAN + "", u.INVALID_OBJECT + "", u.INVALID_ARRAY + "", u.SELL_AMOUNT_MUST_BE_POSITIVE_INTEGER + "", u.SELL_BUY_ASSETS_ARE_THE_SAME + "", u.MISSING_SOURCE_ADDRESS_FOR_SYNTH + "", u.AFF_ADDRESS_AND_BPS_OR_NEITHER + "", u.AFF_ADDRESS_TOO_LONG + "", u.AFF_BPS_INTEGER_MAX_500 + "", u.SOURCE_ADDRESS_INVALID_FOR_SELL_CHAIN + "", u.DESTINATION_ADDRESS_INVALID_FOR_BUY_CHAIN + "", u.PREFERRED_PROVIDER_NOT_SUPPORTED + "", u.SOURCE_ADDRESS_SMART_CONTRACT + "", u.SWAP_AMOUNT_TOO_LOW + "", u.DESTINATION_ADDRESS_SMART_CONTRACT + "", u.BUY_AMOUNT_MUST_BE_POSITIVE_INTEGER + "", u.INVALID_PROVIDER + "", u.MISSING_CROSS_CHAIN_PROVIDER + "", u.MISSING_AVAX_PROVIDER + "", u.MISSING_BSC_PROVIDER + "", u.MISSING_ETH_PROVIDER + "", u.MISSING_ARB_PROVIDER + "", u.INVALID_PROVIDER_FOR_SWAP_OUT + "", u.INVALID_CHAIN + "", u.INVALID_ASSET + "", u.UNSUPPORTED_CHAIN + "", u.UNSUPPORTED_ASSET + "", u.UNSUPPORTED_ASSET_FOR_SWAPOUT + "", u.THORNODE_QUOTE_GENERIC_ERROR + "", u.INVALID_SOURCE_ADDRESS + "", u.INVALID_DESTINATION_ADDRESS + "", u.NOT_ENOUGH_SYNTH_BALANCE + "", u.SYNTH_MINTING_CAP_REACHED + "", u.INVALID_QUOTE_MODE + "", u.NO_QUOTES + "", u.SERVICE_UNAVAILABLE_GENERIC + "", u.MISSING_GAS_DATA_GENERIC + "", u.MISSING_TOKEN_INFO_GENERIC + "", u.CANT_FIND_TOKEN_LIST + "", u.NO_PRICE + "", u.PRICE_IS_STALE + "", u.ADDRESS_NOT_WHITELISTED + "", u.ADDRESS_ALREADY_CLAIMED + ""; | ||
var j = /* @__PURE__ */ ((e) => (e.Arbitrum = "ARB", e.Avalanche = "AVAX", e.Binance = "BNB", e.BinanceSmartChain = "BSC", e.Bitcoin = "BTC", e.BitcoinCash = "BCH", e.Cosmos = "GAIA", e.Dash = "DASH", e.Dogecoin = "DOGE", e.Ethereum = "ETH", e.Kujira = "KUJI", e.Litecoin = "LTC", e.Maya = "MAYA", e.Optimism = "OP", e.Polkadot = "DOT", e.Chainflip = "FLIP", e.Polygon = "MATIC", e.THORChain = "THOR", e))(j || {}), te = /* @__PURE__ */ ((e) => (e.Arbitrum = "42161", e.ArbitrumHex = "0xa4b1", e.Avalanche = "43114", e.AvalancheHex = "0xa86a", e.Binance = "Binance-Chain-Tigris", e.BinanceSmartChain = "56", e.BinanceSmartChainHex = "0x38", e.Bitcoin = "bitcoin", e.BitcoinCash = "bitcoincash", e.Chainflip = "chainflip", e.Cosmos = "cosmoshub-4", e.Dash = "dash", e.Dogecoin = "dogecoin", e.Kujira = "kaiyo-1", e.Ethereum = "1", e.EthereumHex = "0x1", e.Litecoin = "litecoin", e.Maya = "mayachain-mainnet-v1", e.MayaStagenet = "mayachain-stagenet-v1", e.Optimism = "10", e.OptimismHex = "0xa", e.Polkadot = "polkadot", e.Polygon = "137", e.PolygonHex = "0x89", e.THORChain = "thorchain-mainnet-v1", e.THORChainStagenet = "thorchain-stagenet-v2", e))(te || {}), de = /* @__PURE__ */ ((e) => (e.Arbitrum = "https://arb1.arbitrum.io/rpc", e.Avalanche = "https://node-router.thorswap.net/avalanche-c", e.Binance = "", e.BinanceSmartChain = "https://bsc-dataseed.binance.org", e.Bitcoin = "https://node-router.thorswap.net/bitcoin", e.BitcoinCash = "https://node-router.thorswap.net/bitcoin-cash", e.Chainflip = "wss://mainnet-archive.chainflip.io", e.Cosmos = "https://node-router.thorswap.net/cosmos/rpc", e.Dash = "https://node-router.thorswap.net/dash", e.Dogecoin = "https://node-router.thorswap.net/dogecoin", e.Ethereum = "https://node-router.thorswap.net/ethereum", e.Kujira = "https://rpc-kujira.synergynodes.com/", e.Litecoin = "https://node-router.thorswap.net/litecoin", e.Maya = "https://tendermint.mayachain.info", e.MayaStagenet = "https://stagenet.tendermint.mayachain.info", e.Optimism = "https://mainnet.optimism.io", e.Polkadot = "wss://rpc.polkadot.io", e.Polygon = "https://polygon-rpc.com", e.THORChain = "https://rpc.thorswap.net", e.THORChainStagenet = "https://stagenet-rpc.ninerealms.com", e))(de || {}), R = /* @__PURE__ */ ((e) => (e.Cosmos = "https://node-router.thorswap.net/cosmos/rest", e.Kujira = "https://lcd-kujira.synergynodes.com/", e.MayanodeMainnet = "https://mayanode.mayachain.info", e.MayanodeStagenet = "https://stagenet.mayanode.mayachain.info", e.ThornodeMainnet = "https://thornode.thorswap.net", e.ThornodeStagenet = "https://stagenet-thornode.ninerealms.com", e.ThorswapApi = "https://api.thorswap.net", e.ThorswapStatic = "https://static.thorswap.net", e))(R || {}); | ||
const q = Object.values(j), $e = Object.keys(j), ne = q.reduce( | ||
var j = /* @__PURE__ */ ((e) => (e.Arbitrum = "ARB", e.Avalanche = "AVAX", e.Binance = "BNB", e.BinanceSmartChain = "BSC", e.Bitcoin = "BTC", e.BitcoinCash = "BCH", e.Cosmos = "GAIA", e.Dash = "DASH", e.Dogecoin = "DOGE", e.Ethereum = "ETH", e.Kujira = "KUJI", e.Litecoin = "LTC", e.Maya = "MAYA", e.Optimism = "OP", e.Polkadot = "DOT", e.Chainflip = "FLIP", e.Polygon = "MATIC", e.THORChain = "THOR", e))(j || {}), ne = /* @__PURE__ */ ((e) => (e.Arbitrum = "42161", e.ArbitrumHex = "0xa4b1", e.Avalanche = "43114", e.AvalancheHex = "0xa86a", e.Binance = "Binance-Chain-Tigris", e.BinanceSmartChain = "56", e.BinanceSmartChainHex = "0x38", e.Bitcoin = "bitcoin", e.BitcoinCash = "bitcoincash", e.Chainflip = "chainflip", e.Cosmos = "cosmoshub-4", e.Dash = "dash", e.Dogecoin = "dogecoin", e.Kujira = "kaiyo-1", e.Ethereum = "1", e.EthereumHex = "0x1", e.Litecoin = "litecoin", e.Maya = "mayachain-mainnet-v1", e.MayaStagenet = "mayachain-stagenet-v1", e.Optimism = "10", e.OptimismHex = "0xa", e.Polkadot = "polkadot", e.Polygon = "137", e.PolygonHex = "0x89", e.THORChain = "thorchain-mainnet-v1", e.THORChainStagenet = "thorchain-stagenet-v2", e))(ne || {}), de = /* @__PURE__ */ ((e) => (e.Arbitrum = "https://arb1.arbitrum.io/rpc", e.Avalanche = "https://node-router.thorswap.net/avalanche-c", e.Binance = "", e.BinanceSmartChain = "https://bsc-dataseed.binance.org", e.Bitcoin = "https://node-router.thorswap.net/bitcoin", e.BitcoinCash = "https://node-router.thorswap.net/bitcoin-cash", e.Chainflip = "wss://mainnet-archive.chainflip.io", e.Cosmos = "https://node-router.thorswap.net/cosmos/rpc", e.Dash = "https://node-router.thorswap.net/dash", e.Dogecoin = "https://node-router.thorswap.net/dogecoin", e.Ethereum = "https://node-router.thorswap.net/ethereum", e.Kujira = "https://rpc-kujira.synergynodes.com/", e.Litecoin = "https://node-router.thorswap.net/litecoin", e.Maya = "https://tendermint.mayachain.info", e.MayaStagenet = "https://stagenet.tendermint.mayachain.info", e.Optimism = "https://mainnet.optimism.io", e.Polkadot = "wss://rpc.polkadot.io", e.Polygon = "https://polygon-rpc.com", e.THORChain = "https://rpc.thorswap.net", e.THORChainStagenet = "https://stagenet-rpc.ninerealms.com", e))(de || {}), f = /* @__PURE__ */ ((e) => (e.Cosmos = "https://node-router.thorswap.net/cosmos/rest", e.Kujira = "https://lcd-kujira.synergynodes.com/", e.MayanodeMainnet = "https://mayanode.mayachain.info", e.MayanodeStagenet = "https://stagenet.mayanode.mayachain.info", e.ThornodeMainnet = "https://thornode.thorswap.net", e.ThornodeStagenet = "https://stagenet-thornode.ninerealms.com", e.ThorswapApi = "https://api.thorswap.net", e.ThorswapStatic = "https://static.thorswap.net", e))(f || {}); | ||
const q = Object.values(j), $e = Object.keys(j), re = q.reduce( | ||
(e, t) => { | ||
@@ -26,7 +26,7 @@ const n = $e.find((r) => j[r] === t); | ||
q.reduce( | ||
(e, t) => (e[t] = te[ne[t]], e), | ||
(e, t) => (e[t] = ne[re[t]], e), | ||
{} | ||
); | ||
q.reduce( | ||
(e, t) => (e[t] = de[ne[t]], e), | ||
(e, t) => (e[t] = de[re[t]], e), | ||
{} | ||
@@ -36,8 +36,8 @@ ); | ||
(e, t) => { | ||
const n = `${ne[t]}Hex`; | ||
return e[t] = te[n], e; | ||
const n = `${re[t]}Hex`; | ||
return e[t] = ne[n], e; | ||
}, | ||
{} | ||
); | ||
class oe extends Error { | ||
class ce extends Error { | ||
constructor(t, n, r) { | ||
@@ -73,7 +73,7 @@ const s = t.status || t.status === 0 ? t.status : "", i = t.statusText || "", a = `${s} ${i}`.trim(), c = a ? `status code ${a}` : "an unknown error"; | ||
} | ||
const M = (e) => e !== null && typeof e == "object", $ = (...e) => { | ||
const x = (e) => e !== null && typeof e == "object", M = (...e) => { | ||
for (const t of e) | ||
if ((!M(t) || Array.isArray(t)) && t !== void 0) | ||
if ((!x(t) || Array.isArray(t)) && t !== void 0) | ||
throw new TypeError("The `options` argument must be an object"); | ||
return re({}, ...e); | ||
return se({}, ...e); | ||
}, Ae = (e = {}, t = {}) => { | ||
@@ -84,3 +84,3 @@ const n = new globalThis.Headers(e), r = t instanceof globalThis.Headers, s = new globalThis.Headers(t); | ||
return n; | ||
}, re = (...e) => { | ||
}, se = (...e) => { | ||
let t = {}, n = {}; | ||
@@ -90,6 +90,6 @@ for (const r of e) | ||
Array.isArray(t) || (t = []), t = [...t, ...r]; | ||
else if (M(r)) { | ||
else if (x(r)) { | ||
for (let [s, i] of Object.entries(r)) | ||
M(i) && s in t && (i = re(t[s], i)), t = { ...t, [s]: i }; | ||
M(r.headers) && (n = Ae(n, r.headers), t.headers = n); | ||
x(i) && s in t && (i = se(t[s], i)), t = { ...t, [s]: i }; | ||
x(r.headers) && (n = Ae(n, r.headers), t.headers = n); | ||
} | ||
@@ -108,3 +108,3 @@ return t; | ||
}).headers.has("Content-Type")), e && !t; | ||
})(), Me = typeof globalThis.AbortController == "function", He = typeof globalThis.ReadableStream == "function", Fe = typeof globalThis.FormData == "function", pe = ["get", "post", "put", "patch", "head", "delete"], xe = { | ||
})(), Me = typeof globalThis.AbortController == "function", He = typeof globalThis.ReadableStream == "function", xe = typeof globalThis.FormData == "function", pe = ["get", "post", "put", "patch", "head", "delete"], Fe = { | ||
json: "application/json", | ||
@@ -142,3 +142,3 @@ text: "text/*", | ||
duplex: !0 | ||
}, ke = (e) => pe.includes(e) ? e.toUpperCase() : e, je = ["get", "put", "head", "delete", "options", "trace"], qe = [408, 413, 429, 500, 502, 503, 504], me = [413, 429, 503], ce = { | ||
}, ke = (e) => pe.includes(e) ? e.toUpperCase() : e, je = ["get", "put", "head", "delete", "options", "trace"], qe = [408, 413, 429, 500, 502, 503, 504], me = [413, 429, 503], le = { | ||
limit: 2, | ||
@@ -154,3 +154,3 @@ methods: je, | ||
return { | ||
...ce, | ||
...le, | ||
limit: e | ||
@@ -163,3 +163,3 @@ }; | ||
return { | ||
...ce, | ||
...le, | ||
...e, | ||
@@ -196,5 +196,5 @@ afterStatusCodes: me | ||
}; | ||
class x { | ||
class G { | ||
static create(t, n) { | ||
const r = new x(t, n), s = async () => { | ||
const r = new G(t, n), s = async () => { | ||
if (typeof r._options.timeout == "number" && r._options.timeout > K) | ||
@@ -209,3 +209,3 @@ throw new RangeError(`The \`timeout\` option cannot be greater than ${K}`); | ||
if (r._decorateResponse(a), !a.ok && r._options.throwHttpErrors) { | ||
let c = new oe(a, r.request, r._options); | ||
let c = new ce(a, r.request, r._options); | ||
for (const o of r._options.hooks.beforeError) | ||
@@ -224,3 +224,3 @@ c = await o(c); | ||
}, i = r._options.retry.methods.includes(r.request.method.toLowerCase()) ? r._retry(s) : s(); | ||
for (const [a, c] of Object.entries(xe)) | ||
for (const [a, c] of Object.entries(Fe)) | ||
i[a] = async () => { | ||
@@ -271,3 +271,3 @@ r.request.headers.set("accept", r.request.headers.get("accept") || c); | ||
headers: Ae(this._input.headers, n.headers), | ||
hooks: re({ | ||
hooks: se({ | ||
beforeRequest: [], | ||
@@ -303,3 +303,3 @@ beforeRetry: [], | ||
const r = "?" + (typeof this._options.searchParams == "string" ? this._options.searchParams.replace(/^\?/, "") : new URLSearchParams(this._options.searchParams).toString()), s = this.request.url.replace(/(?:\?.*?)?(?=#|$)/, r); | ||
(Fe && this._options.body instanceof globalThis.FormData || this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers["content-type"]) && this.request.headers.delete("content-type"), this.request = new globalThis.Request(new globalThis.Request(s, { ...this.request }), this._options); | ||
(xe && this._options.body instanceof globalThis.FormData || this._options.body instanceof URLSearchParams) && !(this._options.headers && this._options.headers["content-type"]) && this.request.headers.delete("content-type"), this.request = new globalThis.Request(new globalThis.Request(s, { ...this.request }), this._options); | ||
} | ||
@@ -310,3 +310,3 @@ this._options.json !== void 0 && (this._options.body = JSON.stringify(this._options.json), this.request.headers.set("content-type", this._options.headers.get("content-type") ?? "application/json"), this.request = new globalThis.Request(this.request, { body: this._options.body })); | ||
if (this._retryCount++, this._retryCount <= this._options.retry.limit && !(t instanceof Te)) { | ||
if (t instanceof oe) { | ||
if (t instanceof ce) { | ||
if (!this._options.retry.statusCodes.includes(t.response.status)) | ||
@@ -398,19 +398,19 @@ return 0; | ||
/*! MIT License © Sindre Sorhus */ | ||
const Q = (e) => { | ||
const t = (n, r) => x.create(n, $(e, r)); | ||
const z = (e) => { | ||
const t = (n, r) => G.create(n, M(e, r)); | ||
for (const n of pe) | ||
t[n] = (r, s) => x.create(r, $(e, s, { method: n })); | ||
return t.create = (n) => Q($(n)), t.extend = (n) => Q($(e, n)), t.stop = Se, t; | ||
}, Ee = Q(); | ||
let H; | ||
t[n] = (r, s) => G.create(r, M(e, s, { method: n })); | ||
return t.create = (n) => z(M(n)), t.extend = (n) => z(M(e, n)), t.stop = Se, t; | ||
}, Ee = z(); | ||
let F; | ||
const m = { | ||
CachedPrices: `${R.ThorswapApi}/tokenlist/cached-price`, | ||
GasRates: `${R.ThorswapApi}/resource-worker/gasPrice/getAll`, | ||
Quote: `${R.ThorswapApi}/aggregator/tokens/quote`, | ||
Txn: `${R.ThorswapApi}/apiusage/v2/txn`, | ||
TokenlistProviders: `${R.ThorswapApi}/tokenlist/providers`, | ||
TokenList: `${R.ThorswapStatic}/token-list`, | ||
Thorname: `${R.ThorswapApi}/thorname` | ||
}, Re = typeof window < "u" ? {} : { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" }, le = () => H || (H = Ee.create({ headers: Re }), H), mt = ({ apiKey: e, ...t }) => { | ||
H = Ee.create({ | ||
CachedPrices: `${f.ThorswapApi}/tokenlist/cached-price`, | ||
GasRates: `${f.ThorswapApi}/resource-worker/gasPrice/getAll`, | ||
Quote: `${f.ThorswapApi}/aggregator/tokens/quote`, | ||
Txn: `${f.ThorswapApi}/apiusage/v2/txn`, | ||
TokenlistProviders: `${f.ThorswapApi}/tokenlist/providers`, | ||
TokenList: `${f.ThorswapStatic}/token-list`, | ||
Thorname: `${f.ThorswapApi}/thorname` | ||
}, Re = typeof window < "u" ? {} : { referrer: "https://sk.thorswap.net", referer: "https://sk.thorswap.net" }, ue = () => F || (F = Ee.create({ headers: Re }), F), mt = ({ apiKey: e, ...t }) => { | ||
F = Ee.create({ | ||
...t, | ||
@@ -420,4 +420,4 @@ headers: { ...Re, ...t.headers, "x-api-key": e } | ||
}, S = { | ||
get: (e, t) => le().get(e, t).json(), | ||
post: (e, t) => le().post(e, t).json() | ||
get: (e, t) => ue().get(e, t).json(), | ||
post: (e, t) => ue().post(e, t).json() | ||
}, Qe = ({ tokens: e, ...t }) => { | ||
@@ -579,4 +579,4 @@ const n = new URLSearchParams(); | ||
}) { | ||
const r = A(e), s = A(t), i = A(n), a = r.mul(i), c = s.mul(s).mul(2), o = s.mul(r).mul(2), _ = r.mul(r), h = a.mul(c.sub(o).add(_)), P = s.mul(s).mul(s); | ||
return h.div(P); | ||
const r = A(e), s = A(t), i = A(n), a = r.mul(i), c = s.mul(s).mul(2), o = s.mul(r).mul(2), _ = r.mul(r), h = a.mul(c.sub(o).add(_)), y = s.mul(s).mul(s); | ||
return h.div(y); | ||
} | ||
@@ -600,3 +600,3 @@ function Nt({ | ||
function A(e) { | ||
return I.fromBigInt(BigInt(e), d.THOR); | ||
return E.fromBigInt(BigInt(e), d.THOR); | ||
} | ||
@@ -635,4 +635,4 @@ function ht({ | ||
}) { | ||
const a = new I({ value: e, decimal: 8 }), c = new I({ value: n, decimal: 8 }), o = new I({ value: t, decimal: 8 }), _ = new I({ value: s, decimal: 8 }), h = new I({ value: i, decimal: 8 }), P = _.mul(c), ie = h.mul(a), ge = _.mul(h), Oe = a.mul(c), De = o.mul(P.add(ie.add(ge.mul(2)))), Pe = P.add(ie.add(Oe.mul(2))), ae = De.div(Pe), X = A(r).add(ae); | ||
if (ae.getBaseValue("number") === 0) | ||
const a = new E({ value: e, decimal: 8 }), c = new E({ value: n, decimal: 8 }), o = new E({ value: t, decimal: 8 }), _ = new E({ value: s, decimal: 8 }), h = new E({ value: i, decimal: 8 }), y = _.mul(c), ae = h.mul(a), ge = _.mul(h), Oe = a.mul(c), De = o.mul(y.add(ae.add(ge.mul(2)))), Pe = y.add(ae.add(Oe.mul(2))), oe = De.div(Pe), X = A(r).add(oe); | ||
if (oe.getBaseValue("number") === 0) | ||
return X.div(o).getBaseValue("number"); | ||
@@ -655,16 +655,16 @@ const be = o.add(X); | ||
switch (e) { | ||
case E.LEAVE: | ||
case E.BOND: { | ||
case I.LEAVE: | ||
case I.BOND: { | ||
const { address: n } = t; | ||
return `${e}:${n}`; | ||
} | ||
case E.UNBOND: { | ||
case I.UNBOND: { | ||
const { address: n, unbondAmount: r } = t; | ||
return `${e}:${n}:${r}`; | ||
} | ||
case E.THORNAME_REGISTER: { | ||
case I.THORNAME_REGISTER: { | ||
const { name: n, chain: r, address: s, owner: i } = t; | ||
return `${e}:${n}:${r}:${s}${i ? `:${i}` : ""}`; | ||
} | ||
case E.DEPOSIT: { | ||
case I.DEPOSIT: { | ||
const { chain: n, symbol: r, address: s, singleSide: i } = t; | ||
@@ -684,8 +684,8 @@ return i ? `${e}:${n}/${r}` : `${e}:${((c, o) => { | ||
} | ||
case E.WITHDRAW: { | ||
case I.WITHDRAW: { | ||
const { chain: n, ticker: r, symbol: s, basisPoints: i, targetAssetString: a, singleSide: c } = t, o = n === "ETH" && r !== "ETH" ? `${r}-${s.slice(-3)}` : s, _ = !c && a ? `:${a}` : ""; | ||
return `${e}:${n}${c ? "/" : "."}${o}:${i}${_}`; | ||
} | ||
case E.OPEN_LOAN: | ||
case E.CLOSE_LOAN: { | ||
case I.OPEN_LOAN: | ||
case I.CLOSE_LOAN: { | ||
const { asset: n, address: r } = t; | ||
@@ -712,9 +712,9 @@ return `${e}:${n}:${r}`; | ||
} | ||
const ue = [...Object.values(l), "TERRA"]; | ||
const _e = [...Object.values(l), "TERRA"]; | ||
function dt(e = "") { | ||
const t = e.toUpperCase(), [n] = t.split("."); | ||
if (ue.includes(n)) | ||
if (_e.includes(n)) | ||
return !0; | ||
const [r] = t.split("/"); | ||
if (ue.includes(r)) | ||
if (_e.includes(r)) | ||
return !0; | ||
@@ -725,7 +725,7 @@ throw new Error( | ||
} | ||
const B = 8, N = (e) => 10n ** BigInt(e), b = (e) => Math.log10(parseFloat(e.toString())); | ||
function G({ | ||
const U = 8, C = (e) => 10n ** BigInt(e), w = (e) => Math.log10(parseFloat(e.toString())); | ||
function W({ | ||
value: e, | ||
bigIntDecimal: t = B, | ||
decimal: n = B | ||
bigIntDecimal: t = U, | ||
decimal: n = U | ||
}) { | ||
@@ -745,20 +745,20 @@ if (n === 0) | ||
} | ||
var C, y, f, D, k, Ne, U, z, L, Z; | ||
const se = class se { | ||
var g, V, N, D, k, Ne, $, Z, v, ee; | ||
const ie = class ie { | ||
constructor(t) { | ||
O(this, C); | ||
O(this, f); | ||
O(this, g); | ||
O(this, N); | ||
O(this, k); | ||
O(this, U); | ||
O(this, L); | ||
O(this, $); | ||
O(this, v); | ||
p(this, "decimalMultiplier", 10n ** 8n); | ||
p(this, "bigIntValue", 0n); | ||
p(this, "decimal"); | ||
const n = ee(t), r = typeof t == "object"; | ||
this.decimal = r ? t.decimal : void 0, this.decimalMultiplier = r && "decimalMultiplier" in t ? t.decimalMultiplier : N(Math.max(_e(v(n)), this.decimal || 0)), T(this, k, Ne).call(this, n); | ||
const n = te(t), r = typeof t == "object"; | ||
this.decimal = r ? t.decimal : void 0, this.decimalMultiplier = r && "decimalMultiplier" in t ? t.decimalMultiplier : C(Math.max(he(H(n)), this.decimal || 0)), T(this, k, Ne).call(this, n); | ||
} | ||
static fromBigInt(t, n) { | ||
return new se({ | ||
return new ie({ | ||
decimal: n, | ||
value: G({ value: t, bigIntDecimal: n, decimal: n }) | ||
value: W({ value: t, bigIntDecimal: n, decimal: n }) | ||
}); | ||
@@ -772,3 +772,3 @@ } | ||
return this.fromBigInt( | ||
t.getBaseValue("bigint") * N(r) / N(n), | ||
t.getBaseValue("bigint") * C(r) / C(n), | ||
r | ||
@@ -781,27 +781,27 @@ ); | ||
add(...t) { | ||
return T(this, C, y).call(this, "add", ...t); | ||
return T(this, g, V).call(this, "add", ...t); | ||
} | ||
sub(...t) { | ||
return T(this, C, y).call(this, "sub", ...t); | ||
return T(this, g, V).call(this, "sub", ...t); | ||
} | ||
mul(...t) { | ||
return T(this, C, y).call(this, "mul", ...t); | ||
return T(this, g, V).call(this, "mul", ...t); | ||
} | ||
div(...t) { | ||
return T(this, C, y).call(this, "div", ...t); | ||
return T(this, g, V).call(this, "div", ...t); | ||
} | ||
gt(t) { | ||
return T(this, f, D).call(this, "gt", t); | ||
return T(this, N, D).call(this, "gt", t); | ||
} | ||
gte(t) { | ||
return T(this, f, D).call(this, "gte", t); | ||
return T(this, N, D).call(this, "gte", t); | ||
} | ||
lt(t) { | ||
return T(this, f, D).call(this, "lt", t); | ||
return T(this, N, D).call(this, "lt", t); | ||
} | ||
lte(t) { | ||
return T(this, f, D).call(this, "lte", t); | ||
return T(this, N, D).call(this, "lte", t); | ||
} | ||
eqValue(t) { | ||
return T(this, f, D).call(this, "eqValue", t); | ||
return T(this, N, D).call(this, "eqValue", t); | ||
} | ||
@@ -812,3 +812,3 @@ // @ts-expect-error False positive | ||
this.bigIntValue, | ||
this.decimal || b(this.decimalMultiplier) | ||
this.decimal || w(this.decimalMultiplier) | ||
); | ||
@@ -826,3 +826,3 @@ switch (t) { | ||
getBaseValue(t) { | ||
const n = this.decimalMultiplier / N(this.decimal || d.THOR), r = this.bigIntValue / n; | ||
const n = this.decimalMultiplier / C(this.decimal || d.THOR), r = this.bigIntValue / n; | ||
switch (t) { | ||
@@ -840,4 +840,4 @@ case "number": | ||
return t.bigIntValue; | ||
const r = ee(t), s = v(r); | ||
return s === "0" || s === "undefined" ? 0n : T(this, L, Z).call(this, s, n); | ||
const r = te(t), s = H(r); | ||
return s === "0" || s === "undefined" ? 0n : T(this, v, ee).call(this, s, n); | ||
} | ||
@@ -888,5 +888,5 @@ toSignificant(t = 6) { | ||
formatBigIntToSafeValue(t, n) { | ||
const r = n || this.decimal || B, s = Math.max( | ||
const r = n || this.decimal || U, s = Math.max( | ||
r, | ||
b(this.decimalMultiplier) | ||
w(this.decimalMultiplier) | ||
), i = t < 0n, a = t.toString().substring(i ? 1 : 0), c = s - (a.length - 1), o = c > 0 ? "0".repeat(c) + a : a, _ = o.length - s; | ||
@@ -900,4 +900,4 @@ let h = o.slice(-s); | ||
}; | ||
C = new WeakSet(), y = function(t, ...n) { | ||
const r = T(this, U, z).call(this, this, ...n), s = Math.max(r, b(this.decimalMultiplier)), i = N(s), a = n.reduce( | ||
g = new WeakSet(), V = function(t, ...n) { | ||
const r = T(this, $, Z).call(this, this, ...n), s = Math.max(r, w(this.decimalMultiplier)), i = C(s), a = n.reduce( | ||
(o, _) => { | ||
@@ -923,3 +923,3 @@ const h = this.getBigIntValue(_, s); | ||
this.bigIntValue * i / this.decimalMultiplier | ||
), c = G({ | ||
), c = W({ | ||
bigIntDecimal: s, | ||
@@ -930,3 +930,3 @@ decimal: s, | ||
return new this.constructor({ | ||
decimalMultiplier: N(s), | ||
decimalMultiplier: C(s), | ||
decimal: this.decimal, | ||
@@ -936,4 +936,4 @@ value: c, | ||
}); | ||
}, f = new WeakSet(), D = function(t, ...n) { | ||
const r = T(this, U, z).call(this, this, ...n), s = this.getBigIntValue(n[0], r), i = this.getBigIntValue(this, r); | ||
}, N = new WeakSet(), D = function(t, ...n) { | ||
const r = T(this, $, Z).call(this, this, ...n), s = this.getBigIntValue(n[0], r), i = this.getBigIntValue(this, r); | ||
switch (t) { | ||
@@ -952,12 +952,12 @@ case "gt": | ||
}, k = new WeakSet(), Ne = function(t) { | ||
const n = v(t) || "0"; | ||
this.bigIntValue = T(this, L, Z).call(this, n); | ||
}, U = new WeakSet(), z = function(...t) { | ||
const n = t.map((r) => typeof r == "object" ? r.decimal || b(r.decimalMultiplier) : _e(v(r))).filter(Boolean); | ||
return Math.max(...n, B); | ||
}, L = new WeakSet(), Z = function(t, n) { | ||
const r = n ? N(n) : this.decimalMultiplier, s = b(r), [i = "", a = ""] = t.split("."); | ||
const n = H(t) || "0"; | ||
this.bigIntValue = T(this, v, ee).call(this, n); | ||
}, $ = new WeakSet(), Z = function(...t) { | ||
const n = t.map((r) => typeof r == "object" ? r.decimal || w(r.decimalMultiplier) : he(H(r))).filter(Boolean); | ||
return Math.max(...n, U); | ||
}, v = new WeakSet(), ee = function(t, n) { | ||
const r = n ? C(n) : this.decimalMultiplier, s = w(r), [i = "", a = ""] = t.split("."); | ||
return BigInt(`${i}${a.padEnd(s, "0")}`); | ||
}; | ||
let W = se; | ||
let L = ie; | ||
const Tt = Intl.NumberFormat("fullwide", { | ||
@@ -967,16 +967,27 @@ useGrouping: !1, | ||
}); | ||
function v(e) { | ||
const n = `${typeof e == "number" ? Tt.format(e) : ee(e)}`.replaceAll(",", ".").split("."); | ||
function H(e) { | ||
const n = `${typeof e == "number" ? Tt.format(e) : te(e)}`.replaceAll(",", ".").split("."); | ||
return n.length > 1 ? `${n.slice(0, -1).join("")}.${n.at(-1)}` : n[0]; | ||
} | ||
function _e(e) { | ||
function he(e) { | ||
var n; | ||
const t = ((n = e.split(".")[1]) == null ? void 0 : n.length) || 0; | ||
return Math.max(t, B); | ||
return Math.max(t, U); | ||
} | ||
function ee(e) { | ||
function te(e) { | ||
return typeof e == "object" ? "getValue" in e ? e.getValue("string") : e.value : e; | ||
} | ||
const w = /* @__PURE__ */ new Map(); | ||
class g extends W { | ||
class E extends L { | ||
eq(t) { | ||
return this.eqValue(t); | ||
} | ||
static fromBigInt(t, n) { | ||
return new E({ | ||
decimal: n, | ||
value: W({ value: t, bigIntDecimal: n, decimal: n }) | ||
}); | ||
} | ||
} | ||
const P = /* @__PURE__ */ new Map(); | ||
class R extends L { | ||
constructor({ | ||
@@ -999,3 +1010,3 @@ value: n, | ||
p(this, "type"); | ||
const o = F(c || `${i}.${a}`); | ||
const o = B(c || `${i}.${a}`); | ||
this.type = ot(o), this.tax = s, this.chain = o.chain, this.ticker = o.ticker, this.symbol = o.symbol, this.address = o.address, this.isSynthetic = o.isSynthetic, this.isGasAsset = o.isGasAsset; | ||
@@ -1032,5 +1043,5 @@ } | ||
static fromStringSync(n, r = 0) { | ||
const { chain: s, isSynthetic: i } = F(n), a = w.get(n.toUpperCase()); | ||
const { chain: s, isSynthetic: i } = B(n), a = P.get(n.toUpperCase()); | ||
if (i) | ||
return he(n, r); | ||
return Q(n, r); | ||
const { tax: c, decimal: o, identifier: _ } = a || { | ||
@@ -1040,5 +1051,5 @@ decimal: d[s], | ||
}; | ||
return new g({ | ||
return new R({ | ||
tax: c, | ||
value: V(r, o), | ||
value: b(r, o), | ||
identifier: i ? n : _, | ||
@@ -1048,6 +1059,29 @@ decimal: i ? 8 : o | ||
} | ||
static async fromStringWithBase(n, r = 0, s = d.THOR) { | ||
const i = L.shiftDecimals({ | ||
value: E.fromBigInt(BigInt(r)), | ||
from: 0, | ||
to: s | ||
}).getBaseValue("string"); | ||
return (await R.fromString(n, r)).set(i); | ||
} | ||
static fromStringWithBaseSync(n, r = 0, s = d.THOR) { | ||
const { chain: i, isSynthetic: a } = B(n), c = P.get(n.toUpperCase()); | ||
if (a) | ||
return Q(n, r); | ||
const { tax: o, decimal: _, identifier: h } = c || { | ||
decimal: d[i], | ||
identifier: n | ||
}; | ||
return new R({ | ||
tax: o, | ||
value: b(BigInt(r), s), | ||
identifier: h, | ||
decimal: _ | ||
}); | ||
} | ||
static fromIdentifierSync(n, r = 0) { | ||
const { chain: s, isSynthetic: i } = F(n), a = w.get(n); | ||
const { chain: s, isSynthetic: i } = B(n), a = P.get(n); | ||
if (i) | ||
return he(n, r); | ||
return Q(n, r); | ||
const { tax: c, decimal: o, identifier: _ } = a || { | ||
@@ -1057,7 +1091,7 @@ decimal: d[s], | ||
}; | ||
return new g({ tax: c, decimal: o, identifier: _, value: V(r, o) }); | ||
return new R({ tax: c, decimal: o, identifier: _, value: b(r, o) }); | ||
} | ||
static fromChainOrSignature(n, r = 0) { | ||
const { decimal: s, identifier: i } = at(n); | ||
return new g({ value: V(r, s), decimal: s, identifier: i }); | ||
return new R({ value: b(r, s), decimal: s, identifier: i }); | ||
} | ||
@@ -1072,3 +1106,3 @@ static loadStaticAssets() { | ||
(a = i == null ? void 0 : i.tokens) == null || a.forEach(({ identifier: c, chain: o, ..._ }) => { | ||
w.set(c.toUpperCase(), { | ||
P.set(c.toUpperCase(), { | ||
identifier: c, | ||
@@ -1091,3 +1125,3 @@ decimal: "decimals" in _ ? _.decimals : d[o] | ||
function Vt(e) { | ||
const t = g.fromChainOrSignature(e); | ||
const t = R.fromChainOrSignature(e); | ||
switch (e) { | ||
@@ -1114,19 +1148,19 @@ case l.Bitcoin: | ||
dt(e); | ||
const n = w.get(e.toUpperCase()), r = (n == null ? void 0 : n.decimal) || await it(F(e)); | ||
return n || w.set(e.toUpperCase(), { identifier: e, decimal: r }), new g({ decimal: r, value: V(t, r), identifier: e }); | ||
const n = P.get(e.toUpperCase()), r = (n == null ? void 0 : n.decimal) || await it(B(e)); | ||
return n || P.set(e.toUpperCase(), { identifier: e, decimal: r }), new R({ decimal: r, value: b(t, r), identifier: e }); | ||
} | ||
function he(e, t = 0) { | ||
function Q(e, t = 0) { | ||
const [n, r] = e.split(".")[0].toUpperCase() === l.THORChain ? e.split(".").slice(1).join().split("/") : e.split("/"); | ||
if (!n || !r) | ||
throw new Error("Invalid asset identifier"); | ||
return new g({ | ||
return new R({ | ||
decimal: 8, | ||
value: V(t, 8), | ||
value: b(t, 8), | ||
identifier: `${l.THORChain}.${n}/${r}` | ||
}); | ||
} | ||
function V(e, t) { | ||
return typeof e == "bigint" ? G({ value: e, bigIntDecimal: t, decimal: t }) : e; | ||
function b(e, t) { | ||
return typeof e == "bigint" ? W({ value: e, bigIntDecimal: t, decimal: t }) : e; | ||
} | ||
function F(e) { | ||
function B(e) { | ||
const t = e.slice(0, 14).includes("/"), [n, r] = e.split(".")[0].toUpperCase() === l.THORChain ? e.split(".").slice(1).join().split("/") : e.split("/"); | ||
@@ -1214,17 +1248,6 @@ if (t && (!n || !r)) | ||
} | ||
class I extends W { | ||
eq(t) { | ||
return this.eqValue(t); | ||
} | ||
static fromBigInt(t, n) { | ||
return new I({ | ||
decimal: n, | ||
value: G({ value: t, bigIntDecimal: n, decimal: n }) | ||
}); | ||
} | ||
} | ||
export { | ||
m as ApiEndpoints, | ||
g as AssetValue, | ||
W as BigIntArithmetics, | ||
R as AssetValue, | ||
L as BigIntArithmetics, | ||
S as RequestClient, | ||
@@ -1234,3 +1257,3 @@ et as StreamingSwapProgressStatus, | ||
Ce as SwapKitError, | ||
I as SwapKitNumber, | ||
E as SwapKitNumber, | ||
ze as TransactionType, | ||
@@ -1242,3 +1265,3 @@ Ze as TxStatus, | ||
ft as filterAssets, | ||
G as formatBigIntToSafeValue, | ||
W as formatBigIntToSafeValue, | ||
Rt as gasFeeMultiplier, | ||
@@ -1245,0 +1268,0 @@ ot as getAssetType, |
@@ -45,3 +45,3 @@ { | ||
"types": "./dist/index.d.ts", | ||
"version": "1.0.0-rc.70", | ||
"version": "1.0.0-rc.71", | ||
"scripts": { | ||
@@ -48,0 +48,0 @@ "build": "vite build", |
@@ -179,2 +179,23 @@ import { BaseDecimal, Chain } from '@swapkit/types'; | ||
describe('fromStringWithBase', () => { | ||
test('creates AssetValue from string with base', async () => { | ||
const fakeAvaxAssetString = 'AVAX.ASDF-1234'; | ||
const fakeAvaxAsset = await AssetValue.fromStringWithBase(fakeAvaxAssetString, 1, 8); | ||
expect(fakeAvaxAsset).toEqual( | ||
expect.objectContaining({ | ||
address: '1234', | ||
chain: Chain.Avalanche, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: 'ASDF-1234', | ||
ticker: 'ASDF', | ||
}), | ||
); | ||
expect(fakeAvaxAsset.getValue('string')).toBe('100000000'); | ||
expect(fakeAvaxAsset.getBaseValue('string')).toBe('100000000000000000000000000'); | ||
}); | ||
}); | ||
describe('fromUrl', () => { | ||
@@ -279,2 +300,68 @@ test('creates AssetValue from url like format', async () => { | ||
describe('fromStringWithBaseSync', () => { | ||
test('creates AssetValue from string with base decimals via `@swapkit/tokens` lists', async () => { | ||
await AssetValue.loadStaticAssets(); | ||
const btc = AssetValue.fromStringWithBaseSync('BTC.BTC', 5200000000000, 8); | ||
expect(btc).toBeDefined(); | ||
expect(btc).toEqual( | ||
expect.objectContaining({ | ||
chain: Chain.Bitcoin, | ||
decimal: 8, | ||
isGasAsset: true, | ||
isSynthetic: false, | ||
symbol: 'BTC', | ||
ticker: 'BTC', | ||
}), | ||
); | ||
expect(btc.getValue('string')).toBe('52000'); | ||
expect(btc.getBaseValue('string')).toBe('5200000000000'); | ||
}); | ||
test('returns safe decimals if string is not in `@swapkit/tokens` lists', async () => { | ||
await AssetValue.loadStaticAssets(); | ||
const fakeAvaxUSDCAssetString = 'AVAX.USDC-1234'; | ||
const fakeAvaxUSDCAsset = AssetValue.fromStringWithBaseSync(fakeAvaxUSDCAssetString, 1, 8); | ||
expect(fakeAvaxUSDCAsset).toBeDefined(); | ||
expect(fakeAvaxUSDCAsset).toEqual( | ||
expect.objectContaining({ | ||
address: '1234', | ||
chain: Chain.Avalanche, | ||
decimal: 18, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: 'USDC-1234', | ||
ticker: 'USDC', | ||
}), | ||
); | ||
expect(fakeAvaxUSDCAsset.getValue('string')).toBe('0.00000001'); | ||
expect(fakeAvaxUSDCAsset.getBaseValue('string')).toBe('10000000000'); | ||
}); | ||
test('returns proper avax string with address from `@swapkit/tokens` lists', async () => { | ||
await AssetValue.loadStaticAssets(); | ||
const avaxUSDC = 'AVAX.USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e'; | ||
const AvaxUSDC = AssetValue.fromStringWithBaseSync(avaxUSDC, 100000000, 8); | ||
expect(AvaxUSDC).toBeDefined(); | ||
expect(AvaxUSDC).toEqual( | ||
expect.objectContaining({ | ||
address: '0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e', | ||
chain: Chain.Avalanche, | ||
decimal: 6, | ||
isGasAsset: false, | ||
isSynthetic: false, | ||
symbol: 'USDC-0xb97ef9ef8734c71904d8002f8b6bc66dd9c48a6e', | ||
ticker: 'USDC', | ||
}), | ||
); | ||
expect(AvaxUSDC.getValue('string')).toBe('1'); | ||
expect(AvaxUSDC.getBaseValue('string')).toBe('1000000'); | ||
}); | ||
}); | ||
describe('fromChainOrSignature', () => { | ||
@@ -281,0 +368,0 @@ test('creates AssetValue from common asset string or chain', () => { |
@@ -10,3 +10,3 @@ import { BaseDecimal, Chain, ChainToChainId } from '@swapkit/types'; | ||
import { BigIntArithmetics, formatBigIntToSafeValue } from './bigIntArithmetics.ts'; | ||
import type { SwapKitValueType } from './swapKitNumber.ts'; | ||
import { SwapKitNumber, type SwapKitValueType } from './swapKitNumber.ts'; | ||
@@ -121,2 +121,40 @@ const staticTokensMap = new Map< | ||
static async fromStringWithBase( | ||
assetString: string, | ||
value: NumberPrimitives = 0, | ||
baseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
const shiftedAmount = BigIntArithmetics.shiftDecimals({ | ||
value: SwapKitNumber.fromBigInt(BigInt(value)), | ||
from: 0, | ||
to: baseDecimal, | ||
}).getBaseValue('string'); | ||
const assetValue = await AssetValue.fromString(assetString, value); | ||
return assetValue.set(shiftedAmount); | ||
} | ||
static fromStringWithBaseSync( | ||
assetString: string, | ||
value: NumberPrimitives = 0, | ||
baseDecimal: number = BaseDecimal.THOR, | ||
) { | ||
const { chain, isSynthetic } = getAssetInfo(assetString); | ||
const tokenInfo = staticTokensMap.get(assetString.toUpperCase() as TokenNames); | ||
if (isSynthetic) return createSyntheticAssetValue(assetString, value); | ||
const { tax, decimal, identifier } = tokenInfo || { | ||
decimal: BaseDecimal[chain], | ||
identifier: assetString, | ||
}; | ||
return new AssetValue({ | ||
tax, | ||
value: safeValue(BigInt(value), baseDecimal), | ||
identifier, | ||
decimal, | ||
}); | ||
} | ||
static fromIdentifierSync(assetString: TokenNames, value: NumberPrimitives = 0) { | ||
@@ -123,0 +161,0 @@ const { chain, isSynthetic } = getAssetInfo(assetString); |
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
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
470282
4227