@swapkit/helpers 1.0.0-rc.2 → 1.0.0-rc.4
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.cjs +1 -1
- package/dist/index.d.ts +50 -40
- package/dist/index.es.js +298 -262
- package/package.json +5 -5
- package/src/helpers/others.ts +34 -25
- package/src/index.ts +4 -4
- package/src/modules/__tests__/swapKitNumber.test.ts +143 -67
- package/src/modules/assetValue.ts +40 -40
- package/src/modules/bigIntArithmetics.ts +176 -98
- package/src/modules/swapKitNumber.ts +0 -4
- package/src/helpers/number.ts +0 -40
package/dist/index.es.js
CHANGED
|
@@ -1,49 +1,57 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
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
|
|
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
|
|
13
|
-
import { FeeOption as
|
|
14
|
-
const
|
|
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
|
-
},
|
|
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
|
-
},
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
|
43
|
-
|
|
50
|
+
const e = await at(
|
|
51
|
+
it[n],
|
|
44
52
|
JSON.stringify({
|
|
45
53
|
method: "eth_call",
|
|
46
|
-
params: [{ to: t.toLowerCase(), data:
|
|
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),
|
|
63
|
+
return console.error(e), m[n];
|
|
56
64
|
}
|
|
57
|
-
},
|
|
65
|
+
}, ot = async (n) => {
|
|
58
66
|
if (n === i.Ethereum)
|
|
59
|
-
return
|
|
67
|
+
return m.ETH;
|
|
60
68
|
const [, t] = n.split("-");
|
|
61
|
-
return t != null && t.startsWith("0x") ?
|
|
62
|
-
},
|
|
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") ?
|
|
65
|
-
},
|
|
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
|
|
76
|
+
return ot(t);
|
|
69
77
|
case i.Avalanche:
|
|
70
|
-
return
|
|
78
|
+
return ut(t);
|
|
71
79
|
case i.BinanceSmartChain:
|
|
72
|
-
return
|
|
80
|
+
return lt(t);
|
|
73
81
|
default:
|
|
74
|
-
return
|
|
82
|
+
return m[n];
|
|
75
83
|
}
|
|
76
|
-
},
|
|
77
|
-
[
|
|
78
|
-
[
|
|
79
|
-
[
|
|
80
|
-
},
|
|
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:
|
|
121
|
+
return { identifier: "GAIA.ATOM", decimal: m[n] };
|
|
114
122
|
case i.THORChain:
|
|
115
|
-
return { identifier: "THOR.RUNE", decimal:
|
|
123
|
+
return { identifier: "THOR.RUNE", decimal: m[n] };
|
|
116
124
|
case i.BinanceSmartChain:
|
|
117
|
-
return { identifier: "BSC.BNB", decimal:
|
|
125
|
+
return { identifier: "BSC.BNB", decimal: m[n] };
|
|
118
126
|
case i.Maya:
|
|
119
|
-
return { identifier: "MAYA.CACAO", decimal:
|
|
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:
|
|
141
|
+
return { identifier: `${n}.${n}`, decimal: m[n] };
|
|
134
142
|
}
|
|
135
|
-
},
|
|
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
|
-
},
|
|
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
|
-
},
|
|
177
|
+
}, _t = ({
|
|
170
178
|
liquidityUnits: n,
|
|
171
179
|
poolUnits: t,
|
|
172
180
|
runeDepth: e
|
|
173
181
|
}) => {
|
|
174
|
-
const r =
|
|
175
|
-
return c.mul(o.sub(
|
|
176
|
-
},
|
|
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 =
|
|
182
|
-
return
|
|
183
|
-
},
|
|
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
|
-
}) =>
|
|
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
|
-
}) =>
|
|
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:
|
|
200
|
-
runeAmount:
|
|
201
|
-
}),
|
|
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(
|
|
216
|
+
Object.entries(pt({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map(
|
|
209
217
|
([a, c]) => [a, c.mul(s)]
|
|
210
218
|
)
|
|
211
|
-
),
|
|
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 =
|
|
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
|
|
222
|
-
const
|
|
223
|
-
return
|
|
224
|
-
},
|
|
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 =
|
|
231
|
-
return Math.abs(
|
|
232
|
-
},
|
|
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,
|
|
244
|
+
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Rt = (n, t) => {
|
|
237
245
|
switch (n) {
|
|
238
|
-
case
|
|
239
|
-
case
|
|
246
|
+
case p.LEAVE:
|
|
247
|
+
case p.BOND: {
|
|
240
248
|
const { address: e } = t;
|
|
241
249
|
return `${n}:${e}`;
|
|
242
250
|
}
|
|
243
|
-
case
|
|
251
|
+
case p.UNBOND: {
|
|
244
252
|
const { address: e, unbondAmount: r } = t;
|
|
245
253
|
return `${n}:${e}:${r * 10 ** 8}`;
|
|
246
254
|
}
|
|
247
|
-
case
|
|
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
|
|
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
|
|
256
|
-
const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t,
|
|
257
|
-
return `${n}:${e}${o ? "/" : "."}${
|
|
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
|
|
260
|
-
case
|
|
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
|
-
},
|
|
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 =
|
|
270
|
-
decimal: e =
|
|
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
|
-
}
|
|
283
|
-
|
|
284
|
-
|
|
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
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
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
|
|
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
|
|
323
|
-
new
|
|
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
|
|
336
|
+
return this.getBaseValue("string");
|
|
332
337
|
}
|
|
333
338
|
get baseValueNumber() {
|
|
334
|
-
return
|
|
339
|
+
return this.getBaseValue("number");
|
|
335
340
|
}
|
|
336
341
|
get baseValueBigInt() {
|
|
337
|
-
return
|
|
342
|
+
return this.getBaseValue("bigint");
|
|
338
343
|
}
|
|
339
|
-
|
|
340
|
-
return this.
|
|
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
|
|
348
|
+
return _(this, w, V).call(this, "add", ...t);
|
|
347
349
|
}
|
|
348
350
|
sub(...t) {
|
|
349
|
-
return
|
|
351
|
+
return _(this, w, V).call(this, "sub", ...t);
|
|
350
352
|
}
|
|
351
353
|
mul(...t) {
|
|
352
|
-
return
|
|
354
|
+
return _(this, w, V).call(this, "mul", ...t);
|
|
353
355
|
}
|
|
354
356
|
div(...t) {
|
|
355
|
-
return
|
|
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 : (
|
|
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 ||
|
|
403
|
+
const r = e || this.decimal || C, s = Math.max(
|
|
377
404
|
r,
|
|
378
|
-
|
|
379
|
-
), a = t < 0n;
|
|
380
|
-
let
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
)
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
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(),
|
|
399
|
-
const r =
|
|
400
|
-
(o,
|
|
401
|
-
const
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
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,
|
|
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
|
-
},
|
|
415
|
-
const
|
|
416
|
-
this.bigIntValue =
|
|
417
|
-
},
|
|
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 ||
|
|
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,
|
|
422
|
-
},
|
|
423
|
-
const r = e ? I(e) : this.decimalMultiplier, s =
|
|
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
|
-
},
|
|
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,
|
|
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
|
|
447
|
-
|
|
448
|
-
|
|
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
|
|
451
|
-
},
|
|
452
|
-
|
|
453
|
-
const e = await
|
|
454
|
-
return new
|
|
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
|
|
495
|
+
class f extends T {
|
|
457
496
|
constructor(e) {
|
|
458
497
|
super(
|
|
459
|
-
e.value instanceof
|
|
498
|
+
e.value instanceof T ? e.value : { decimal: e.decimal, value: e.value }
|
|
460
499
|
);
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s =
|
|
469
|
-
this.type =
|
|
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
|
|
520
|
+
return W(e, r);
|
|
473
521
|
}
|
|
474
522
|
static fromStringSync(e, r = 0) {
|
|
475
|
-
const { decimal: s, identifier: a } =
|
|
523
|
+
const { decimal: s, identifier: a } = D(
|
|
476
524
|
e
|
|
477
525
|
);
|
|
478
|
-
return a ? new
|
|
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
|
|
529
|
+
return W(e, r);
|
|
482
530
|
}
|
|
483
531
|
static fromIdentifierSync(e, r = 0) {
|
|
484
|
-
const { decimal: s, identifier: a } =
|
|
485
|
-
return new
|
|
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
|
|
537
|
+
return new f({ value: r, decimal: s, identifier: a });
|
|
490
538
|
}
|
|
491
539
|
static async fromTCQuote(e, r = 0) {
|
|
492
|
-
const s = await
|
|
493
|
-
return new
|
|
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 =
|
|
544
|
+
const s = D(e), a = this.shiftDecimals({
|
|
497
545
|
value: r,
|
|
498
|
-
from:
|
|
546
|
+
from: m.THOR,
|
|
499
547
|
to: s.decimal
|
|
500
548
|
});
|
|
501
|
-
return new
|
|
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
|
-
|
|
515
|
-
(
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
}),
|
|
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
|
|
542
|
-
const t =
|
|
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
|
-
},
|
|
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:
|
|
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
|
-
},
|
|
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
|
|
665
|
+
class X extends Error {
|
|
627
666
|
constructor(t, e) {
|
|
628
|
-
console.error(e), super(t, { cause: { code:
|
|
667
|
+
console.error(e), super(t, { cause: { code: $t[t], message: t } }), Object.setPrototypeOf(this, X.prototype);
|
|
629
668
|
}
|
|
630
669
|
}
|
|
631
|
-
class
|
|
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
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
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
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
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
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
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
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
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
|
};
|