@swapkit/helpers 1.0.0-rc.2 → 1.0.0-rc.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -1,49 +1,57 @@
1
- var rt = Object.defineProperty;
2
- var st = (n, t, e) => t in n ? rt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
- var _ = (n, t, e) => (st(n, typeof t != "symbol" ? t + "" : t, e), e), it = (n, t, e) => {
1
+ var nt = Object.defineProperty;
2
+ var rt = (n, t, e) => t in n ? nt(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var g = (n, t, e) => (rt(n, typeof t != "symbol" ? t + "" : t, e), e), st = (n, t, e) => {
4
4
  if (!t.has(n))
5
5
  throw TypeError("Cannot " + e);
6
6
  };
7
- var p = (n, t, e) => {
7
+ var $ = (n, t, e) => {
8
8
  if (t.has(n))
9
9
  throw TypeError("Cannot add the same private member more than once");
10
10
  t instanceof WeakSet ? t.add(n) : t.set(n, e);
11
11
  };
12
- var d = (n, t, e) => (it(n, t, "access private method"), e);
13
- import { FeeOption as x, BaseDecimal as h, Chain as i, ChainToRPC as at, MemoType as f } from "@swapkit/types";
14
- const vt = (n) => {
12
+ var _ = (n, t, e) => (st(n, t, "access private method"), e);
13
+ import { FeeOption as H, BaseDecimal as m, Chain as i, ChainToRPC as it, MemoType as p } from "@swapkit/types";
14
+ const St = (n) => {
15
15
  if (n < 0)
16
16
  throw new Error("Invalid number of year");
17
17
  return 10 + n;
18
- }, Ct = (n) => {
18
+ }, vt = (n) => {
19
19
  if (n.length > 30)
20
20
  return !1;
21
21
  const t = /^[a-zA-Z0-9+_-]+$/g;
22
22
  return !!n.match(t);
23
- }, Tt = ([n, t, e, r, s]) => `${n}'/${t}'/${e}'/${r}${typeof s != "number" ? "" : `/${s}`}`, Bt = async (n, t) => {
24
- const e = Object.entries(t || {}).reduce(
25
- (s, [a, c]) => (c && (s[a] = c), s),
26
- {}
27
- );
28
- return (await fetch(
29
- `${n}${t ? `?${new URLSearchParams(e).toString()}` : ""}`,
30
- { method: "GET", mode: "cors", credentials: "omit", referrer: "https://sk.thorswap.net" }
31
- )).json();
32
- }, ct = async (n, t, e, r = !1) => {
33
- const s = await fetch(`${n}`, {
34
- body: t,
35
- headers: e,
36
- method: "POST",
37
- referrer: "https://sk.thorswap.net"
38
- });
39
- return r ? s.text() : s.json();
40
- }, ot = "0x313ce567", K = async ({ chain: n, to: t }) => {
23
+ }, Vt = ([n, t, e, r, s]) => `${n}'/${t}'/${e}'/${r}${typeof s != "number" ? "" : `/${s}`}`, Bt = async (n, t) => {
24
+ try {
25
+ const e = Object.entries(t || {}).reduce(
26
+ (s, [a, c]) => (c && (s[a] = c), s),
27
+ {}
28
+ );
29
+ return (await fetch(
30
+ `${n}${t ? `?${new URLSearchParams(e).toString()}` : ""}`,
31
+ { method: "GET", mode: "cors", credentials: "omit", referrer: "https://sk.thorswap.net" }
32
+ )).json();
33
+ } catch (e) {
34
+ return console.error(e), {};
35
+ }
36
+ }, at = async (n, t, e, r = !1) => {
37
+ try {
38
+ const s = await fetch(`${n}`, {
39
+ body: t,
40
+ headers: e,
41
+ method: "POST",
42
+ referrer: "https://sk.thorswap.net"
43
+ });
44
+ return r ? s.text() : s.json();
45
+ } catch {
46
+ return {};
47
+ }
48
+ }, ct = "0x313ce567", Y = async ({ chain: n, to: t }) => {
41
49
  try {
42
- const e = await ct(
43
- at[n],
50
+ const e = await at(
51
+ it[n],
44
52
  JSON.stringify({
45
53
  method: "eth_call",
46
- params: [{ to: t.toLowerCase(), data: ot }, "latest"],
54
+ params: [{ to: t.toLowerCase(), data: ct }, "latest"],
47
55
  id: 44,
48
56
  jsonrpc: "2.0"
49
57
  }),
@@ -52,32 +60,32 @@ const vt = (n) => {
52
60
  ), { result: r } = JSON.parse(e);
53
61
  return parseInt(BigInt(r).toString());
54
62
  } catch (e) {
55
- return console.error(e), h[n];
63
+ return console.error(e), m[n];
56
64
  }
57
- }, ut = async (n) => {
65
+ }, ot = async (n) => {
58
66
  if (n === i.Ethereum)
59
- return h.ETH;
67
+ return m.ETH;
60
68
  const [, t] = n.split("-");
61
- return t != null && t.startsWith("0x") ? K({ chain: i.Ethereum, to: t }) : h.ETH;
62
- }, lt = async (n) => {
69
+ return t != null && t.startsWith("0x") ? Y({ chain: i.Ethereum, to: t }) : m.ETH;
70
+ }, ut = async (n) => {
63
71
  const [, t] = n.split("-");
64
- return t != null && t.startsWith("0x") ? K({ chain: i.Avalanche, to: t.toLowerCase() }) : h.AVAX;
65
- }, dt = async (n) => n === i.BinanceSmartChain ? h.BSC : h.BSC, J = async ({ chain: n, symbol: t }) => {
72
+ return t != null && t.startsWith("0x") ? Y({ chain: i.Avalanche, to: t.toLowerCase() }) : m.AVAX;
73
+ }, lt = async (n) => n === i.BinanceSmartChain ? m.BSC : m.BSC, K = async ({ chain: n, symbol: t }) => {
66
74
  switch (n) {
67
75
  case i.Ethereum:
68
- return ut(t);
76
+ return ot(t);
69
77
  case i.Avalanche:
70
- return lt(t);
78
+ return ut(t);
71
79
  case i.BinanceSmartChain:
72
- return dt(t);
80
+ return lt(t);
73
81
  default:
74
- return h[n];
82
+ return m[n];
75
83
  }
76
- }, yt = {
77
- [x.Average]: 1.2,
78
- [x.Fast]: 1.5,
79
- [x.Fastest]: 2
80
- }, mt = ({ chain: n, symbol: t }) => {
84
+ }, Ct = {
85
+ [H.Average]: 1.2,
86
+ [H.Fast]: 1.5,
87
+ [H.Fastest]: 2
88
+ }, dt = ({ chain: n, symbol: t }) => {
81
89
  switch (n) {
82
90
  case i.Bitcoin:
83
91
  case i.BitcoinCash:
@@ -110,13 +118,13 @@ const vt = (n) => {
110
118
  case "ETH.vTHOR":
111
119
  return { identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", decimal: 18 };
112
120
  case i.Cosmos:
113
- return { identifier: "GAIA.ATOM", decimal: h[n] };
121
+ return { identifier: "GAIA.ATOM", decimal: m[n] };
114
122
  case i.THORChain:
115
- return { identifier: "THOR.RUNE", decimal: h[n] };
123
+ return { identifier: "THOR.RUNE", decimal: m[n] };
116
124
  case i.BinanceSmartChain:
117
- return { identifier: "BSC.BNB", decimal: h[n] };
125
+ return { identifier: "BSC.BNB", decimal: m[n] };
118
126
  case i.Maya:
119
- return { identifier: "MAYA.CACAO", decimal: h.MAYA };
127
+ return { identifier: "MAYA.CACAO", decimal: m.MAYA };
120
128
  case "MAYA.MAYA":
121
129
  return { identifier: "MAYA.MAYA", decimal: 4 };
122
130
  case i.Kujira:
@@ -130,9 +138,9 @@ const vt = (n) => {
130
138
  case i.Polygon:
131
139
  case i.Bitcoin:
132
140
  case i.Ethereum:
133
- return { identifier: `${n}.${n}`, decimal: h[n] };
141
+ return { identifier: `${n}.${n}`, decimal: m[n] };
134
142
  }
135
- }, _t = ({ chain: n, symbol: t }) => {
143
+ }, mt = ({ chain: n, symbol: t }) => {
136
144
  if (t.includes("/"))
137
145
  return "Synth";
138
146
  switch (n) {
@@ -162,53 +170,53 @@ const vt = (n) => {
162
170
  case i.Optimism:
163
171
  return [i.Ethereum, i.Optimism].includes(t) ? "Native" : "OPTIMISM";
164
172
  }
165
- }, Mt = (n) => {
173
+ }, yt = (n) => {
166
174
  var c;
167
175
  const [t, ...e] = n.split("."), r = n.includes("/"), s = e.join("."), a = (c = s == null ? void 0 : s.split("-")) == null ? void 0 : c[0];
168
176
  return { chain: t, symbol: s, ticker: a, synth: r };
169
- }, gt = ({
177
+ }, _t = ({
170
178
  liquidityUnits: n,
171
179
  poolUnits: t,
172
180
  runeDepth: e
173
181
  }) => {
174
- const r = m(n), s = m(t), a = m(e), c = r.mul(a), o = s.mul(s).mul(2), l = s.mul(r).mul(2), u = r.mul(r), g = s.mul(s).mul(s);
175
- return c.mul(o.sub(l).add(u)).div(g);
176
- }, pt = ({
182
+ const r = h(n), s = h(t), a = h(e), c = r.mul(a), o = s.mul(s).mul(2), u = s.mul(r).mul(2), l = r.mul(r), d = s.mul(s).mul(s);
183
+ return c.mul(o.sub(u).add(l)).div(d);
184
+ }, gt = ({
177
185
  liquidityUnits: n,
178
186
  poolUnits: t,
179
187
  assetDepth: e
180
188
  }) => {
181
- const r = m(n), s = m(t), a = m(e), c = r.mul(a), o = s.mul(s).mul(2), l = s.mul(r).mul(2), u = r.mul(r), g = c.mul(o.sub(l).add(u)), $ = s.mul(s).mul(s);
182
- return g.div($);
183
- }, Et = ({
189
+ const r = h(n), s = h(t), a = h(e), c = r.mul(a), o = s.mul(s).mul(2), u = s.mul(r).mul(2), l = r.mul(r), d = c.mul(o.sub(u).add(l)), A = s.mul(s).mul(s);
190
+ return d.div(A);
191
+ }, Tt = ({
184
192
  percent: n,
185
193
  runeDepth: t,
186
194
  liquidityUnits: e,
187
195
  poolUnits: r
188
- }) => gt({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Ot = ({
196
+ }) => _t({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Et = ({
189
197
  percent: n,
190
198
  assetDepth: t,
191
199
  liquidityUnits: e,
192
200
  poolUnits: r
193
- }) => pt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), m = (n) => new bt({ value: n, decimal: h.THOR }), ft = ({
201
+ }) => gt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), h = (n) => new wt({ value: n, decimal: m.THOR }), pt = ({
194
202
  liquidityUnits: n,
195
203
  poolUnits: t,
196
204
  runeDepth: e,
197
205
  assetDepth: r
198
206
  }) => ({
199
- assetAmount: m(r).mul(n).div(t),
200
- runeAmount: m(e).mul(n).div(t)
201
- }), Vt = ({
207
+ assetAmount: h(r).mul(n).div(t),
208
+ runeAmount: h(e).mul(n).div(t)
209
+ }), Ot = ({
202
210
  liquidityUnits: n,
203
211
  poolUnits: t,
204
212
  runeDepth: e,
205
213
  assetDepth: r,
206
214
  percent: s
207
215
  }) => Object.fromEntries(
208
- Object.entries(ft({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map(
216
+ Object.entries(pt({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map(
209
217
  ([a, c]) => [a, c.mul(s)]
210
218
  )
211
- ), Nt = ({
219
+ ), Mt = ({
212
220
  runeDepth: n,
213
221
  poolUnits: t,
214
222
  assetDepth: e,
@@ -216,59 +224,70 @@ const vt = (n) => {
216
224
  runeAmount: s,
217
225
  assetAmount: a
218
226
  }) => {
219
- const c = m(n), o = m(e), l = m(t), u = m(s), g = m(a), $ = u.mul(o), v = g.mul(c), D = u.mul(g), Z = c.mul(o), tt = l.mul($.add(v.add(D.mul(2)))), et = $.add(v.add(Z.mul(2))), U = tt.div(et), H = m(r).add(U);
227
+ 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);
220
228
  if (U.baseValueNumber === 0)
221
- return H.div(l).baseValueNumber;
222
- const nt = l.add(H);
223
- return H.div(nt).baseValueNumber;
224
- }, Rt = ({
229
+ return k.div(u).baseValueNumber;
230
+ const et = u.add(k);
231
+ return k.div(et).baseValueNumber;
232
+ }, Nt = ({
225
233
  runeAmount: n,
226
234
  assetAmount: t,
227
235
  runeDepth: e,
228
236
  assetDepth: r
229
237
  }) => {
230
- const s = m(e), a = m(r), c = m(t), o = m(n), l = c.mul(s).sub(a.mul(o)), u = a.mul(o).add(s.mul(a));
231
- return Math.abs(l.div(u).baseValueNumber);
232
- }, At = ({
238
+ const s = h(e), a = h(r), c = h(t), o = h(n), u = c.mul(s).sub(a.mul(o)), l = a.mul(o).add(s.mul(a));
239
+ return Math.abs(u.div(l).baseValueNumber);
240
+ }, ft = ({
233
241
  symbol: n,
234
242
  ticker: t,
235
243
  chain: e
236
- }) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, kt = (n, t) => {
244
+ }) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Rt = (n, t) => {
237
245
  switch (n) {
238
- case f.LEAVE:
239
- case f.BOND: {
246
+ case p.LEAVE:
247
+ case p.BOND: {
240
248
  const { address: e } = t;
241
249
  return `${n}:${e}`;
242
250
  }
243
- case f.UNBOND: {
251
+ case p.UNBOND: {
244
252
  const { address: e, unbondAmount: r } = t;
245
253
  return `${n}:${e}:${r * 10 ** 8}`;
246
254
  }
247
- case f.THORNAME_REGISTER: {
255
+ case p.THORNAME_REGISTER: {
248
256
  const { name: e, chain: r, address: s, owner: a } = t;
249
257
  return `${n}:${e}:${r}:${s}${a ? `:${a}` : ""}`;
250
258
  }
251
- case f.DEPOSIT: {
259
+ case p.DEPOSIT: {
252
260
  const { chain: e, symbol: r, address: s, singleSide: a } = t;
253
261
  return a ? `${n}:${e}/${r}::t:0` : `${n}:${e}.${r}:${s || ""}:t:0`;
254
262
  }
255
- case f.WITHDRAW: {
256
- const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t, l = !o && c ? `:${c}` : "", u = At({ chain: e, symbol: s, ticker: r });
257
- return `${n}:${e}${o ? "/" : "."}${u}:${a}${l}`;
263
+ case p.WITHDRAW: {
264
+ 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 });
265
+ return `${n}:${e}${o ? "/" : "."}${l}:${a}${u}`;
258
266
  }
259
- case f.OPEN_LOAN:
260
- case f.CLOSE_LOAN: {
267
+ case p.OPEN_LOAN:
268
+ case p.CLOSE_LOAN: {
261
269
  const { asset: e, address: r } = t;
262
270
  return `${n}:${e}:${r}`;
263
271
  }
264
272
  default:
265
273
  return "";
266
274
  }
267
- }, y = 8, G = ({
275
+ }, F = Object.values(i), At = (n = "") => {
276
+ const t = n.toUpperCase(), [e] = t.split(".");
277
+ if (F.includes(e))
278
+ return !0;
279
+ const [r] = t.split("/");
280
+ if (F.includes(r))
281
+ return !0;
282
+ throw new Error(
283
+ `Invalid identifier: ${n}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`
284
+ );
285
+ }, C = 8, I = (n) => 10n ** BigInt(n), v = (n) => Math.log10(parseFloat(n.toString()));
286
+ function G({
268
287
  value: n,
269
- bigIntDecimal: t = y,
270
- decimal: e = y
271
- }) => {
288
+ bigIntDecimal: t = C,
289
+ decimal: e = C
290
+ }) {
272
291
  const r = n < 0n;
273
292
  let s = n.toString().substring(r ? 1 : 0);
274
293
  const a = e - (s.length - 1);
@@ -279,37 +298,26 @@ const vt = (n) => {
279
298
  /\.?0*$/,
280
299
  ""
281
300
  );
282
- }, W = Object.values(i), $t = (n = "") => {
283
- const t = n.toUpperCase(), [e] = t.split(".");
284
- if (W.includes(e))
285
- return !0;
286
- const [r] = t.split("/");
287
- if (W.includes(r))
288
- return !0;
289
- throw new Error(
290
- `Invalid identifier: ${n}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`
291
- );
292
- }, I = (n) => 10n ** BigInt(n), C = (n) => Math.log10(parseFloat(n.toString()));
293
- var w, T, R, z, k, X, O, L, b, B, V, j, S, N;
294
- const M = class M {
301
+ }
302
+ var w, V, M, J, N, z, E, L, b, B, O, x;
303
+ const y = class y {
295
304
  constructor(t) {
296
- p(this, w);
297
- p(this, R);
298
- p(this, k);
299
- p(this, O);
300
- p(this, b);
301
- p(this, V);
302
- p(this, S);
303
- _(this, "decimalMultiplier", 10n ** 8n);
304
- _(this, "bigIntValue", 0n);
305
- _(this, "decimal");
306
- const e = typeof t == "object", r = e ? t.value : t;
307
- this.decimal = e ? t.decimal : void 0, this.decimalMultiplier = I(
308
- Math.max(d(this, V, j).call(this, d(this, b, B).call(this, r)), this.decimal || 0)
309
- ), d(this, R, z).call(this, r);
305
+ $(this, w);
306
+ $(this, M);
307
+ $(this, N);
308
+ $(this, E);
309
+ $(this, b);
310
+ $(this, O);
311
+ g(this, "decimalMultiplier", 10n ** 8n);
312
+ g(this, "bigIntValue", 0n);
313
+ g(this, "decimal");
314
+ const e = P(t);
315
+ this.decimal = typeof t == "object" ? t.decimal : void 0, this.decimalMultiplier = I(
316
+ Math.max(_(this, O, x).call(this, _(this, b, B).call(this, e)), this.decimal || 0)
317
+ ), _(this, M, J).call(this, e);
310
318
  }
311
319
  static fromBigInt(t, e) {
312
- return new M({
320
+ return new y({
313
321
  decimal: e,
314
322
  value: G({ value: t, bigIntDecimal: e, decimal: e })
315
323
  });
@@ -319,40 +327,34 @@ const M = class M {
319
327
  from: e,
320
328
  to: r
321
329
  }) {
322
- return M.fromBigInt(
323
- new M(t).bigIntValue * I(r) / I(e),
330
+ return y.fromBigInt(
331
+ new y(t).bigIntValue * I(r) / I(e),
324
332
  r
325
333
  );
326
334
  }
327
- get unsafeNumber() {
328
- return parseFloat((this.bigIntValue / this.decimalMultiplier).toString());
329
- }
330
335
  get baseValue() {
331
- return d(this, S, N).call(this, "string");
336
+ return this.getBaseValue("string");
332
337
  }
333
338
  get baseValueNumber() {
334
- return d(this, S, N).call(this, "number");
339
+ return this.getBaseValue("number");
335
340
  }
336
341
  get baseValueBigInt() {
337
- return d(this, S, N).call(this, "number");
342
+ return this.getBaseValue("bigint");
338
343
  }
339
- get value() {
340
- return this.formatBigIntToSafeValue(
341
- this.bigIntValue,
342
- this.decimal || C(this.decimalMultiplier)
343
- );
344
+ set(t) {
345
+ return new this.constructor({ decimal: this.decimal, value: t, identifier: this.toString() });
344
346
  }
345
347
  add(...t) {
346
- return d(this, w, T).call(this, "add", ...t);
348
+ return _(this, w, V).call(this, "add", ...t);
347
349
  }
348
350
  sub(...t) {
349
- return d(this, w, T).call(this, "sub", ...t);
351
+ return _(this, w, V).call(this, "sub", ...t);
350
352
  }
351
353
  mul(...t) {
352
- return d(this, w, T).call(this, "mul", ...t);
354
+ return _(this, w, V).call(this, "mul", ...t);
353
355
  }
354
356
  div(...t) {
355
- return d(this, w, T).call(this, "div", ...t);
357
+ return _(this, w, V).call(this, "div", ...t);
356
358
  }
357
359
  gt(t) {
358
360
  return this.bigIntValue > this.getBigIntValue(t);
@@ -369,136 +371,182 @@ const M = class M {
369
371
  eqValue(t) {
370
372
  return this.bigIntValue === this.getBigIntValue(t);
371
373
  }
374
+ getValue(t) {
375
+ const e = this.formatBigIntToSafeValue(
376
+ this.bigIntValue,
377
+ this.decimal || v(this.decimalMultiplier)
378
+ );
379
+ switch (t) {
380
+ case "number":
381
+ return Number(e);
382
+ case "string":
383
+ return e;
384
+ default:
385
+ return this.bigIntValue;
386
+ }
387
+ }
388
+ getBaseValue(t) {
389
+ const e = this.decimalMultiplier / I(this.decimal || 0), r = this.bigIntValue / e;
390
+ switch (t) {
391
+ case "number":
392
+ return Number(r);
393
+ case "string":
394
+ return r.toString();
395
+ default:
396
+ return this.bigIntValue;
397
+ }
398
+ }
372
399
  getBigIntValue(t, e) {
373
- return !e && typeof t == "object" ? t.bigIntValue : (t = typeof t == "object" ? t.value : t, d(this, O, L).call(this, d(this, b, B).call(this, t), e));
400
+ return !e && typeof t == "object" ? t.bigIntValue : _(this, E, L).call(this, _(this, b, B).call(this, P(t)), e);
374
401
  }
375
402
  formatBigIntToSafeValue(t, e) {
376
- const r = e || this.decimal || y, s = Math.max(
403
+ const r = e || this.decimal || C, s = Math.max(
377
404
  r,
378
- C(this.decimalMultiplier)
379
- ), a = t < 0n;
380
- let c = t.toString().substring(a ? 1 : 0);
381
- const o = s - (c.length - 1);
382
- o > 0 && (c = "0".repeat(o) + c);
383
- const l = c.length - s;
384
- let u = c.slice(-s);
385
- return parseInt(u[r]) >= 5 ? u = `${u.substring(0, r - 1)}${(parseInt(u[r - 1]) + 1).toString()}` : u = u.substring(0, r), `${a ? "-" : ""}${c.slice(0, l)}.${u}`.replace(
386
- /\.?0*$/,
387
- ""
388
- );
389
- }
390
- toSignificant(t) {
391
- const e = this.value.split("."), r = e[0], s = e[1];
392
- return s ? `${r}.${s.slice(0, t || this.decimal)}`.replace(
393
- /\.?0*$/,
394
- ""
395
- ) : r;
405
+ v(this.decimalMultiplier)
406
+ ), a = t < 0n, c = t.toString().substring(a ? 1 : 0), o = s - (c.length - 1), u = o > 0 ? "0".repeat(o) + c : c, l = u.length - s;
407
+ let d = u.slice(-s);
408
+ return parseInt(d[r]) >= 5 ? d = `${d.substring(0, r - 1)}${(parseInt(d[r - 1]) + 1).toString()}` : d = d.substring(0, r), `${a ? "-" : ""}${u.slice(
409
+ 0,
410
+ l
411
+ )}.${d}`.replace(/\.?0*$/, "");
412
+ }
413
+ toSignificant(t = 6) {
414
+ const [e, r] = this.getValue("string").split("."), s = e || "", a = r || "", c = parseInt(s) ? s.length + a.length : a.length;
415
+ if (c <= t)
416
+ return this.getValue("string");
417
+ if (s.length >= t)
418
+ return s.slice(0, t).padEnd(s.length, "0");
419
+ if (parseInt(s))
420
+ return `${s}.${a.slice(0, t - s.length)}`.padEnd(
421
+ c - t,
422
+ "0"
423
+ );
424
+ const o = parseInt(a), u = `${o}`.slice(0, t);
425
+ return `0.${u.padStart(
426
+ a.length - `${o}`.length + u.length,
427
+ "0"
428
+ )}`;
396
429
  }
397
430
  };
398
- w = new WeakSet(), T = function(t, ...e) {
399
- const r = d(this, k, X).call(this, this, ...e), s = I(r), a = e.reduce(
400
- (o, l) => {
401
- const u = this.getBigIntValue(l, r);
402
- if (t === "div" && u === 0n)
403
- throw new RangeError("Division by zero");
404
- return t === "add" ? o + u : t === "sub" ? o - u : t === "mul" ? o * u / s : o * s / u;
431
+ w = new WeakSet(), V = function(t, ...e) {
432
+ const r = _(this, N, z).call(this, this, ...e), s = I(r), a = e.reduce(
433
+ (o, u) => {
434
+ const l = this.getBigIntValue(u, r);
435
+ switch (t) {
436
+ case "add":
437
+ return o + l;
438
+ case "sub":
439
+ return o - l;
440
+ case "mul":
441
+ return o * l / s;
442
+ case "div": {
443
+ if (l === 0n)
444
+ throw new RangeError("Division by zero");
445
+ return o * s / l;
446
+ }
447
+ default:
448
+ return o;
449
+ }
405
450
  },
406
451
  //normalize is to precision multiplier base
407
452
  this.bigIntValue * s / this.decimalMultiplier
408
453
  ), c = G({
409
454
  bigIntDecimal: r,
410
- decimal: Math.max(r, C(this.decimalMultiplier)),
455
+ decimal: Math.max(r, v(this.decimalMultiplier)),
411
456
  value: a
412
457
  });
413
458
  return new this.constructor({ decimal: this.decimal, value: c, identifier: this.toString() });
414
- }, R = new WeakSet(), z = function(t, e) {
415
- const r = d(this, b, B).call(this, t) || "0";
416
- this.bigIntValue = e || d(this, O, L).call(this, r);
417
- }, k = new WeakSet(), X = function(...t) {
459
+ }, M = new WeakSet(), J = function(t) {
460
+ const e = _(this, b, B).call(this, t) || "0";
461
+ this.bigIntValue = _(this, E, L).call(this, e);
462
+ }, N = new WeakSet(), z = function(...t) {
418
463
  const e = t.map(
419
- (r) => typeof r == "object" ? r.decimal || C(r.decimalMultiplier) : d(this, V, j).call(this, d(this, b, B).call(this, r))
464
+ (r) => typeof r == "object" ? r.decimal || v(r.decimalMultiplier) : _(this, O, x).call(this, _(this, b, B).call(this, r))
420
465
  ).filter(Boolean);
421
- return Math.max(...e, y);
422
- }, O = new WeakSet(), L = function(t, e) {
423
- const r = e ? I(e) : this.decimalMultiplier, s = C(r), [a, c = ""] = t.split(".");
466
+ return Math.max(...e, C);
467
+ }, E = new WeakSet(), L = function(t, e) {
468
+ const r = e ? I(e) : this.decimalMultiplier, s = v(r), [a, c = ""] = t.split(".");
424
469
  return BigInt(`${a}${c.padEnd(s, "0")}`);
425
470
  }, b = new WeakSet(), B = function(t) {
426
471
  const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", {
427
472
  useGrouping: !1,
428
473
  maximumFractionDigits: 20
429
- }) : t}`.replaceAll(",", ".").split(".");
474
+ }) : P(t)}`.replaceAll(",", ".").split(".");
430
475
  return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0];
431
- }, V = new WeakSet(), j = function(t) {
476
+ }, O = new WeakSet(), x = function(t) {
432
477
  var r;
433
478
  const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0;
434
- return Math.max(e, y);
435
- }, S = new WeakSet(), N = function(t) {
436
- const e = this.decimalMultiplier / I(this.decimal || 0), r = this.bigIntValue / e;
437
- switch (t) {
438
- case "number":
439
- return Number(r);
440
- case "string":
441
- return r.toString();
442
- default:
443
- return r;
444
- }
479
+ return Math.max(e, C);
445
480
  };
446
- let E = M, q;
447
- const P = (n) => {
448
- if (!q)
481
+ let T = y;
482
+ function P(n) {
483
+ return typeof n == "object" ? "getValue" in n ? n.getValue("string") : n.value : n;
484
+ }
485
+ let j;
486
+ const D = (n) => {
487
+ if (!j)
449
488
  throw new Error("Static assets not loaded, call await AssetValue.loadStaticAssets() first");
450
- return q.get(n.toUpperCase()) || { decimal: h.THOR, identifier: "" };
451
- }, Y = async (n, t = 0) => {
452
- $t(n);
453
- const e = await J(F(n));
454
- return new A({ decimal: e, value: t, identifier: n });
489
+ return j.get(n.toUpperCase()) || { decimal: m.THOR, identifier: "" };
490
+ }, W = async (n, t = 0) => {
491
+ At(n);
492
+ const e = await K(q(n));
493
+ return new f({ decimal: e, value: t, identifier: n });
455
494
  };
456
- class A extends E {
495
+ class f extends T {
457
496
  constructor(e) {
458
497
  super(
459
- e.value instanceof E ? e.value : { decimal: e.decimal, value: e.value }
498
+ e.value instanceof T ? e.value : { decimal: e.decimal, value: e.value }
460
499
  );
461
- _(this, "address");
462
- _(this, "chain");
463
- _(this, "isSynthetic", !1);
464
- _(this, "isGasAsset", !1);
465
- _(this, "symbol");
466
- _(this, "ticker");
467
- _(this, "type");
468
- const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s = F(r);
469
- this.type = _t(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;
500
+ g(this, "address");
501
+ g(this, "chain");
502
+ g(this, "isSynthetic", !1);
503
+ g(this, "isGasAsset", !1);
504
+ g(this, "symbol");
505
+ g(this, "ticker");
506
+ g(this, "type");
507
+ const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s = q(r);
508
+ 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;
509
+ }
510
+ get assetValue() {
511
+ return `${this.getValue("string")} ${this.ticker}`;
512
+ }
513
+ toString() {
514
+ return `${this.chain}.${this.symbol}`;
515
+ }
516
+ eq({ chain: e, symbol: r }) {
517
+ return this.chain === e && this.symbol === r;
470
518
  }
471
519
  static async fromString(e, r = 0) {
472
- return Y(e, r);
520
+ return W(e, r);
473
521
  }
474
522
  static fromStringSync(e, r = 0) {
475
- const { decimal: s, identifier: a } = P(
523
+ const { decimal: s, identifier: a } = D(
476
524
  e
477
525
  );
478
- return a ? new A({ decimal: s, identifier: a, value: r }) : void 0;
526
+ return a ? new f({ decimal: s, identifier: a, value: r }) : void 0;
479
527
  }
480
528
  static async fromIdentifier(e, r = 0) {
481
- return Y(e, r);
529
+ return W(e, r);
482
530
  }
483
531
  static fromIdentifierSync(e, r = 0) {
484
- const { decimal: s, identifier: a } = P(e);
485
- return new A({ decimal: s, identifier: a, value: r });
532
+ const { decimal: s, identifier: a } = D(e);
533
+ return new f({ decimal: s, identifier: a, value: r });
486
534
  }
487
535
  static fromChainOrSignature(e, r = 0) {
488
536
  const { decimal: s, identifier: a } = ht(e);
489
- return new A({ value: r, decimal: s, identifier: a });
537
+ return new f({ value: r, decimal: s, identifier: a });
490
538
  }
491
539
  static async fromTCQuote(e, r = 0) {
492
- const s = await J(F(e)), a = this.shiftDecimals({ value: r, from: h.THOR, to: s });
493
- return new A({ value: a, identifier: e, decimal: s });
540
+ const s = await K(q(e)), a = this.shiftDecimals({ value: r, from: m.THOR, to: s });
541
+ return new f({ value: a, identifier: e, decimal: s });
494
542
  }
495
543
  static fromTCQuoteStatic(e, r = 0) {
496
- const s = P(e), a = this.shiftDecimals({
544
+ const s = D(e), a = this.shiftDecimals({
497
545
  value: r,
498
- from: h.THOR,
546
+ from: m.THOR,
499
547
  to: s.decimal
500
548
  });
501
- return new A({ ...s, value: a });
549
+ return new f({ ...s, value: a });
502
550
  }
503
551
  static async loadStaticAssets() {
504
552
  return new Promise(
@@ -511,11 +559,11 @@ class A extends E {
511
559
  NativeList: a,
512
560
  ...c
513
561
  } = await import("@swapkit/tokens");
514
- q = [a, ...Object.values(c)].reduce(
515
- (l, { tokens: u }) => (u.forEach(({ identifier: g, chain: $, ...v }) => {
516
- const D = "decimals" in v ? v.decimals : h[$];
517
- l.set(g, { identifier: g, decimal: D });
518
- }), l),
562
+ j = [a, ...Object.values(c)].reduce(
563
+ (u, { tokens: l }) => (l.forEach(({ identifier: d, chain: A, ...S }) => {
564
+ const R = "decimals" in S ? S.decimals : m[A];
565
+ u.set(d, { identifier: d, decimal: R });
566
+ }), u),
519
567
  /* @__PURE__ */ new Map()
520
568
  ), e({ ok: !0 });
521
569
  } catch (s) {
@@ -528,18 +576,9 @@ class A extends E {
528
576
  }
529
577
  );
530
578
  }
531
- get assetValue() {
532
- return `${this.value} ${this.ticker}`;
533
- }
534
- toString() {
535
- return `${this.chain}.${this.symbol}`;
536
- }
537
- eq({ chain: e, symbol: r }) {
538
- return this.chain === e && this.symbol === r;
539
- }
540
579
  }
541
- const Dt = (n) => {
542
- const t = A.fromChainOrSignature(n);
580
+ const kt = (n) => {
581
+ const t = f.fromChainOrSignature(n);
543
582
  switch (n) {
544
583
  case i.Bitcoin:
545
584
  case i.Litecoin:
@@ -556,17 +595,17 @@ const Dt = (n) => {
556
595
  default:
557
596
  return t.add(1);
558
597
  }
559
- }, F = (n) => {
598
+ }, q = (n) => {
560
599
  const t = n.slice(0, 14).includes("/"), e = n.includes(".") ? n : `${i.THORChain}.${n}`, [r, s] = e.split("."), [a, c] = s.split("-");
561
600
  return {
562
601
  address: c == null ? void 0 : c.toLowerCase(),
563
602
  chain: r,
564
- isGasAsset: mt({ chain: r, symbol: s }),
603
+ isGasAsset: dt({ chain: r, symbol: s }),
565
604
  isSynthetic: t,
566
605
  symbol: c ? `${a}-${(c == null ? void 0 : c.toLowerCase()) ?? ""}` : s,
567
606
  ticker: t ? s : a
568
607
  };
569
- }, wt = {
608
+ }, $t = {
570
609
  /**
571
610
  * Core
572
611
  */
@@ -623,44 +662,41 @@ const Dt = (n) => {
623
662
  */
624
663
  helpers_number_different_decimals: 99101
625
664
  };
626
- class Q extends Error {
665
+ class X extends Error {
627
666
  constructor(t, e) {
628
- console.error(e), super(t, { cause: { code: wt[t], message: t } }), Object.setPrototypeOf(this, Q.prototype);
667
+ console.error(e), super(t, { cause: { code: $t[t], message: t } }), Object.setPrototypeOf(this, X.prototype);
629
668
  }
630
669
  }
631
- class bt extends E {
670
+ class wt extends T {
632
671
  eq(t) {
633
672
  return this.eqValue(t);
634
673
  }
635
- toString() {
636
- return this.value;
637
- }
638
674
  }
639
675
  export {
640
- A as AssetValue,
641
- y as DEFAULT_DECIMAL,
642
- Q as SwapKitError,
643
- bt as SwapKitNumber,
644
- Mt as assetFromString,
645
- Tt as derivationPathToString,
676
+ f as AssetValue,
677
+ T as BigIntArithmetics,
678
+ X as SwapKitError,
679
+ wt as SwapKitNumber,
680
+ yt as assetFromString,
681
+ Vt as derivationPathToString,
646
682
  G as formatBigIntToSafeValue,
647
- yt as gasFeeMultiplier,
648
- _t as getAssetType,
649
- pt as getAsymmetricAssetShare,
650
- Ot as getAsymmetricAssetWithdrawAmount,
651
- gt as getAsymmetricRuneShare,
652
- Et as getAsymmetricRuneWithdrawAmount,
683
+ Ct as gasFeeMultiplier,
684
+ mt as getAssetType,
685
+ gt as getAsymmetricAssetShare,
686
+ Et as getAsymmetricAssetWithdrawAmount,
687
+ _t as getAsymmetricRuneShare,
688
+ Tt as getAsymmetricRuneWithdrawAmount,
653
689
  ht as getCommonAssetInfo,
654
- J as getDecimal,
655
- Nt as getEstimatedPoolShare,
656
- Rt as getLiquiditySlippage,
657
- kt as getMemoFor,
658
- Dt as getMinAmountByChain,
690
+ K as getDecimal,
691
+ Mt as getEstimatedPoolShare,
692
+ Nt as getLiquiditySlippage,
693
+ Rt as getMemoFor,
694
+ kt as getMinAmountByChain,
659
695
  Bt as getRequest,
660
- ft as getSymmetricPoolShare,
661
- Vt as getSymmetricWithdraw,
662
- vt as getTHORNameCost,
663
- mt as isGasAsset,
664
- ct as postRequest,
665
- Ct as validateTHORName
696
+ pt as getSymmetricPoolShare,
697
+ Ot as getSymmetricWithdraw,
698
+ St as getTHORNameCost,
699
+ dt as isGasAsset,
700
+ at as postRequest,
701
+ vt as validateTHORName
666
702
  };