@swapkit/helpers 1.0.0-rc.1 → 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.cjs +1 -1
- package/dist/index.d.ts +51 -41
- package/dist/index.es.js +309 -268
- package/package.json +5 -5
- package/src/helpers/asset.ts +6 -0
- 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:
|
|
@@ -92,6 +100,8 @@ const vt = (n) => {
|
|
|
92
100
|
return t === "ETH";
|
|
93
101
|
case i.Maya:
|
|
94
102
|
return t === "CACAO";
|
|
103
|
+
case i.Kujira:
|
|
104
|
+
return t === "KUJI";
|
|
95
105
|
case i.Cosmos:
|
|
96
106
|
return t === "ATOM";
|
|
97
107
|
case i.Polygon:
|
|
@@ -108,15 +118,16 @@ const vt = (n) => {
|
|
|
108
118
|
case "ETH.vTHOR":
|
|
109
119
|
return { identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", decimal: 18 };
|
|
110
120
|
case i.Cosmos:
|
|
111
|
-
return { identifier: "GAIA.ATOM", decimal:
|
|
121
|
+
return { identifier: "GAIA.ATOM", decimal: m[n] };
|
|
112
122
|
case i.THORChain:
|
|
113
|
-
return { identifier: "THOR.RUNE", decimal:
|
|
123
|
+
return { identifier: "THOR.RUNE", decimal: m[n] };
|
|
114
124
|
case i.BinanceSmartChain:
|
|
115
|
-
return { identifier: "BSC.BNB", decimal:
|
|
125
|
+
return { identifier: "BSC.BNB", decimal: m[n] };
|
|
116
126
|
case i.Maya:
|
|
117
|
-
return { identifier: "MAYA.CACAO", decimal:
|
|
127
|
+
return { identifier: "MAYA.CACAO", decimal: m.MAYA };
|
|
118
128
|
case "MAYA.MAYA":
|
|
119
129
|
return { identifier: "MAYA.MAYA", decimal: 4 };
|
|
130
|
+
case i.Kujira:
|
|
120
131
|
case i.Arbitrum:
|
|
121
132
|
case i.Optimism:
|
|
122
133
|
case i.BitcoinCash:
|
|
@@ -127,9 +138,9 @@ const vt = (n) => {
|
|
|
127
138
|
case i.Polygon:
|
|
128
139
|
case i.Bitcoin:
|
|
129
140
|
case i.Ethereum:
|
|
130
|
-
return { identifier: `${n}.${n}`, decimal:
|
|
141
|
+
return { identifier: `${n}.${n}`, decimal: m[n] };
|
|
131
142
|
}
|
|
132
|
-
},
|
|
143
|
+
}, mt = ({ chain: n, symbol: t }) => {
|
|
133
144
|
if (t.includes("/"))
|
|
134
145
|
return "Synth";
|
|
135
146
|
switch (n) {
|
|
@@ -142,6 +153,8 @@ const vt = (n) => {
|
|
|
142
153
|
return "Native";
|
|
143
154
|
case i.Cosmos:
|
|
144
155
|
return t === "ATOM" ? "Native" : "GAIA";
|
|
156
|
+
case i.Kujira:
|
|
157
|
+
return t === "KUJI" ? "Native" : "KUJI";
|
|
145
158
|
case i.Binance:
|
|
146
159
|
return t === i.Binance ? "Native" : "BEP2";
|
|
147
160
|
case i.BinanceSmartChain:
|
|
@@ -157,53 +170,53 @@ const vt = (n) => {
|
|
|
157
170
|
case i.Optimism:
|
|
158
171
|
return [i.Ethereum, i.Optimism].includes(t) ? "Native" : "OPTIMISM";
|
|
159
172
|
}
|
|
160
|
-
},
|
|
173
|
+
}, yt = (n) => {
|
|
161
174
|
var c;
|
|
162
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];
|
|
163
176
|
return { chain: t, symbol: s, ticker: a, synth: r };
|
|
164
|
-
},
|
|
177
|
+
}, _t = ({
|
|
165
178
|
liquidityUnits: n,
|
|
166
179
|
poolUnits: t,
|
|
167
180
|
runeDepth: e
|
|
168
181
|
}) => {
|
|
169
|
-
const r =
|
|
170
|
-
return c.mul(o.sub(
|
|
171
|
-
},
|
|
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 = ({
|
|
172
185
|
liquidityUnits: n,
|
|
173
186
|
poolUnits: t,
|
|
174
187
|
assetDepth: e
|
|
175
188
|
}) => {
|
|
176
|
-
const r =
|
|
177
|
-
return
|
|
178
|
-
},
|
|
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 = ({
|
|
179
192
|
percent: n,
|
|
180
193
|
runeDepth: t,
|
|
181
194
|
liquidityUnits: e,
|
|
182
195
|
poolUnits: r
|
|
183
|
-
}) =>
|
|
196
|
+
}) => _t({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Et = ({
|
|
184
197
|
percent: n,
|
|
185
198
|
assetDepth: t,
|
|
186
199
|
liquidityUnits: e,
|
|
187
200
|
poolUnits: r
|
|
188
|
-
}) =>
|
|
201
|
+
}) => gt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), h = (n) => new wt({ value: n, decimal: m.THOR }), pt = ({
|
|
189
202
|
liquidityUnits: n,
|
|
190
203
|
poolUnits: t,
|
|
191
204
|
runeDepth: e,
|
|
192
205
|
assetDepth: r
|
|
193
206
|
}) => ({
|
|
194
|
-
assetAmount:
|
|
195
|
-
runeAmount:
|
|
196
|
-
}),
|
|
207
|
+
assetAmount: h(r).mul(n).div(t),
|
|
208
|
+
runeAmount: h(e).mul(n).div(t)
|
|
209
|
+
}), Ot = ({
|
|
197
210
|
liquidityUnits: n,
|
|
198
211
|
poolUnits: t,
|
|
199
212
|
runeDepth: e,
|
|
200
213
|
assetDepth: r,
|
|
201
214
|
percent: s
|
|
202
215
|
}) => Object.fromEntries(
|
|
203
|
-
Object.entries(
|
|
216
|
+
Object.entries(pt({ liquidityUnits: n, poolUnits: t, runeDepth: e, assetDepth: r })).map(
|
|
204
217
|
([a, c]) => [a, c.mul(s)]
|
|
205
218
|
)
|
|
206
|
-
),
|
|
219
|
+
), Mt = ({
|
|
207
220
|
runeDepth: n,
|
|
208
221
|
poolUnits: t,
|
|
209
222
|
assetDepth: e,
|
|
@@ -211,59 +224,70 @@ const vt = (n) => {
|
|
|
211
224
|
runeAmount: s,
|
|
212
225
|
assetAmount: a
|
|
213
226
|
}) => {
|
|
214
|
-
const c =
|
|
215
|
-
if (
|
|
216
|
-
return
|
|
217
|
-
const
|
|
218
|
-
return
|
|
219
|
-
},
|
|
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);
|
|
228
|
+
if (U.baseValueNumber === 0)
|
|
229
|
+
return k.div(u).baseValueNumber;
|
|
230
|
+
const et = u.add(k);
|
|
231
|
+
return k.div(et).baseValueNumber;
|
|
232
|
+
}, Nt = ({
|
|
220
233
|
runeAmount: n,
|
|
221
234
|
assetAmount: t,
|
|
222
235
|
runeDepth: e,
|
|
223
236
|
assetDepth: r
|
|
224
237
|
}) => {
|
|
225
|
-
const s =
|
|
226
|
-
return Math.abs(
|
|
227
|
-
},
|
|
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 = ({
|
|
228
241
|
symbol: n,
|
|
229
242
|
ticker: t,
|
|
230
243
|
chain: e
|
|
231
|
-
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n,
|
|
244
|
+
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Rt = (n, t) => {
|
|
232
245
|
switch (n) {
|
|
233
|
-
case
|
|
234
|
-
case
|
|
246
|
+
case p.LEAVE:
|
|
247
|
+
case p.BOND: {
|
|
235
248
|
const { address: e } = t;
|
|
236
249
|
return `${n}:${e}`;
|
|
237
250
|
}
|
|
238
|
-
case
|
|
251
|
+
case p.UNBOND: {
|
|
239
252
|
const { address: e, unbondAmount: r } = t;
|
|
240
253
|
return `${n}:${e}:${r * 10 ** 8}`;
|
|
241
254
|
}
|
|
242
|
-
case
|
|
255
|
+
case p.THORNAME_REGISTER: {
|
|
243
256
|
const { name: e, chain: r, address: s, owner: a } = t;
|
|
244
257
|
return `${n}:${e}:${r}:${s}${a ? `:${a}` : ""}`;
|
|
245
258
|
}
|
|
246
|
-
case
|
|
259
|
+
case p.DEPOSIT: {
|
|
247
260
|
const { chain: e, symbol: r, address: s, singleSide: a } = t;
|
|
248
261
|
return a ? `${n}:${e}/${r}::t:0` : `${n}:${e}.${r}:${s || ""}:t:0`;
|
|
249
262
|
}
|
|
250
|
-
case
|
|
251
|
-
const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t,
|
|
252
|
-
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}`;
|
|
253
266
|
}
|
|
254
|
-
case
|
|
255
|
-
case
|
|
267
|
+
case p.OPEN_LOAN:
|
|
268
|
+
case p.CLOSE_LOAN: {
|
|
256
269
|
const { asset: e, address: r } = t;
|
|
257
270
|
return `${n}:${e}:${r}`;
|
|
258
271
|
}
|
|
259
272
|
default:
|
|
260
273
|
return "";
|
|
261
274
|
}
|
|
262
|
-
},
|
|
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({
|
|
263
287
|
value: n,
|
|
264
|
-
bigIntDecimal: t =
|
|
265
|
-
decimal: e =
|
|
266
|
-
})
|
|
288
|
+
bigIntDecimal: t = C,
|
|
289
|
+
decimal: e = C
|
|
290
|
+
}) {
|
|
267
291
|
const r = n < 0n;
|
|
268
292
|
let s = n.toString().substring(r ? 1 : 0);
|
|
269
293
|
const a = e - (s.length - 1);
|
|
@@ -274,39 +298,28 @@ const vt = (n) => {
|
|
|
274
298
|
/\.?0*$/,
|
|
275
299
|
""
|
|
276
300
|
);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return !0;
|
|
281
|
-
const [r] = t.split("/");
|
|
282
|
-
if (W.includes(r))
|
|
283
|
-
return !0;
|
|
284
|
-
throw new Error(
|
|
285
|
-
`Invalid identifier: ${n}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`
|
|
286
|
-
);
|
|
287
|
-
}, S = (n) => 10n ** BigInt(n), C = (n) => Math.log10(parseFloat(n.toString()));
|
|
288
|
-
var w, y, R, J, k, Q, O, L, b, T, V, j, I, N;
|
|
289
|
-
const M = class M {
|
|
301
|
+
}
|
|
302
|
+
var w, V, M, J, N, z, E, L, b, B, O, x;
|
|
303
|
+
const y = class y {
|
|
290
304
|
constructor(t) {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
), d(this, R, J).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);
|
|
305
318
|
}
|
|
306
319
|
static fromBigInt(t, e) {
|
|
307
|
-
return new
|
|
320
|
+
return new y({
|
|
308
321
|
decimal: e,
|
|
309
|
-
value:
|
|
322
|
+
value: G({ value: t, bigIntDecimal: e, decimal: e })
|
|
310
323
|
});
|
|
311
324
|
}
|
|
312
325
|
static shiftDecimals({
|
|
@@ -314,40 +327,34 @@ const M = class M {
|
|
|
314
327
|
from: e,
|
|
315
328
|
to: r
|
|
316
329
|
}) {
|
|
317
|
-
return
|
|
318
|
-
new
|
|
330
|
+
return y.fromBigInt(
|
|
331
|
+
new y(t).bigIntValue * I(r) / I(e),
|
|
319
332
|
r
|
|
320
333
|
);
|
|
321
334
|
}
|
|
322
|
-
get unsafeNumber() {
|
|
323
|
-
return parseFloat((this.bigIntValue / this.decimalMultiplier).toString());
|
|
324
|
-
}
|
|
325
335
|
get baseValue() {
|
|
326
|
-
return
|
|
336
|
+
return this.getBaseValue("string");
|
|
327
337
|
}
|
|
328
338
|
get baseValueNumber() {
|
|
329
|
-
return
|
|
339
|
+
return this.getBaseValue("number");
|
|
330
340
|
}
|
|
331
341
|
get baseValueBigInt() {
|
|
332
|
-
return
|
|
342
|
+
return this.getBaseValue("bigint");
|
|
333
343
|
}
|
|
334
|
-
|
|
335
|
-
return this.
|
|
336
|
-
this.bigIntValue,
|
|
337
|
-
this.decimal || C(this.decimalMultiplier)
|
|
338
|
-
);
|
|
344
|
+
set(t) {
|
|
345
|
+
return new this.constructor({ decimal: this.decimal, value: t, identifier: this.toString() });
|
|
339
346
|
}
|
|
340
347
|
add(...t) {
|
|
341
|
-
return
|
|
348
|
+
return _(this, w, V).call(this, "add", ...t);
|
|
342
349
|
}
|
|
343
350
|
sub(...t) {
|
|
344
|
-
return
|
|
351
|
+
return _(this, w, V).call(this, "sub", ...t);
|
|
345
352
|
}
|
|
346
353
|
mul(...t) {
|
|
347
|
-
return
|
|
354
|
+
return _(this, w, V).call(this, "mul", ...t);
|
|
348
355
|
}
|
|
349
356
|
div(...t) {
|
|
350
|
-
return
|
|
357
|
+
return _(this, w, V).call(this, "div", ...t);
|
|
351
358
|
}
|
|
352
359
|
gt(t) {
|
|
353
360
|
return this.bigIntValue > this.getBigIntValue(t);
|
|
@@ -364,136 +371,182 @@ const M = class M {
|
|
|
364
371
|
eqValue(t) {
|
|
365
372
|
return this.bigIntValue === this.getBigIntValue(t);
|
|
366
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
|
+
}
|
|
367
399
|
getBigIntValue(t, e) {
|
|
368
|
-
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);
|
|
369
401
|
}
|
|
370
402
|
formatBigIntToSafeValue(t, e) {
|
|
371
|
-
const r = e || this.decimal ||
|
|
403
|
+
const r = e || this.decimal || C, s = Math.max(
|
|
372
404
|
r,
|
|
373
|
-
|
|
374
|
-
), a = t < 0n;
|
|
375
|
-
let
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
)
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
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
|
+
)}`;
|
|
391
429
|
}
|
|
392
430
|
};
|
|
393
|
-
w = new WeakSet(),
|
|
394
|
-
const r =
|
|
395
|
-
(o,
|
|
396
|
-
const
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
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
|
+
}
|
|
400
450
|
},
|
|
401
451
|
//normalize is to precision multiplier base
|
|
402
452
|
this.bigIntValue * s / this.decimalMultiplier
|
|
403
|
-
), c =
|
|
453
|
+
), c = G({
|
|
404
454
|
bigIntDecimal: r,
|
|
405
|
-
decimal: Math.max(r,
|
|
455
|
+
decimal: Math.max(r, v(this.decimalMultiplier)),
|
|
406
456
|
value: a
|
|
407
457
|
});
|
|
408
458
|
return new this.constructor({ decimal: this.decimal, value: c, identifier: this.toString() });
|
|
409
|
-
},
|
|
410
|
-
const
|
|
411
|
-
this.bigIntValue =
|
|
412
|
-
},
|
|
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) {
|
|
413
463
|
const e = t.map(
|
|
414
|
-
(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))
|
|
415
465
|
).filter(Boolean);
|
|
416
|
-
return Math.max(...e,
|
|
417
|
-
},
|
|
418
|
-
const r = e ?
|
|
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(".");
|
|
419
469
|
return BigInt(`${a}${c.padEnd(s, "0")}`);
|
|
420
|
-
}, b = new WeakSet(),
|
|
470
|
+
}, b = new WeakSet(), B = function(t) {
|
|
421
471
|
const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", {
|
|
422
472
|
useGrouping: !1,
|
|
423
473
|
maximumFractionDigits: 20
|
|
424
|
-
}) : t}`.replaceAll(",", ".").split(".");
|
|
474
|
+
}) : P(t)}`.replaceAll(",", ".").split(".");
|
|
425
475
|
return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0];
|
|
426
|
-
},
|
|
476
|
+
}, O = new WeakSet(), x = function(t) {
|
|
427
477
|
var r;
|
|
428
478
|
const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0;
|
|
429
|
-
return Math.max(e,
|
|
430
|
-
}, I = new WeakSet(), N = function(t) {
|
|
431
|
-
const e = this.decimalMultiplier / S(this.decimal || 0), r = this.bigIntValue / e;
|
|
432
|
-
switch (t) {
|
|
433
|
-
case "number":
|
|
434
|
-
return Number(r);
|
|
435
|
-
case "string":
|
|
436
|
-
return r.toString();
|
|
437
|
-
default:
|
|
438
|
-
return r;
|
|
439
|
-
}
|
|
479
|
+
return Math.max(e, C);
|
|
440
480
|
};
|
|
441
|
-
let
|
|
442
|
-
|
|
443
|
-
|
|
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)
|
|
444
488
|
throw new Error("Static assets not loaded, call await AssetValue.loadStaticAssets() first");
|
|
445
|
-
return
|
|
446
|
-
},
|
|
447
|
-
|
|
448
|
-
const e = await
|
|
449
|
-
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 });
|
|
450
494
|
};
|
|
451
|
-
class
|
|
495
|
+
class f extends T {
|
|
452
496
|
constructor(e) {
|
|
453
497
|
super(
|
|
454
|
-
e.value instanceof
|
|
498
|
+
e.value instanceof T ? e.value : { decimal: e.decimal, value: e.value }
|
|
455
499
|
);
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s =
|
|
464
|
-
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;
|
|
465
518
|
}
|
|
466
519
|
static async fromString(e, r = 0) {
|
|
467
|
-
return
|
|
520
|
+
return W(e, r);
|
|
468
521
|
}
|
|
469
522
|
static fromStringSync(e, r = 0) {
|
|
470
|
-
const { decimal: s, identifier: a } =
|
|
523
|
+
const { decimal: s, identifier: a } = D(
|
|
471
524
|
e
|
|
472
525
|
);
|
|
473
|
-
return a ? new
|
|
526
|
+
return a ? new f({ decimal: s, identifier: a, value: r }) : void 0;
|
|
474
527
|
}
|
|
475
528
|
static async fromIdentifier(e, r = 0) {
|
|
476
|
-
return
|
|
529
|
+
return W(e, r);
|
|
477
530
|
}
|
|
478
531
|
static fromIdentifierSync(e, r = 0) {
|
|
479
|
-
const { decimal: s, identifier: a } =
|
|
480
|
-
return new
|
|
532
|
+
const { decimal: s, identifier: a } = D(e);
|
|
533
|
+
return new f({ decimal: s, identifier: a, value: r });
|
|
481
534
|
}
|
|
482
535
|
static fromChainOrSignature(e, r = 0) {
|
|
483
536
|
const { decimal: s, identifier: a } = ht(e);
|
|
484
|
-
return new
|
|
537
|
+
return new f({ value: r, decimal: s, identifier: a });
|
|
485
538
|
}
|
|
486
539
|
static async fromTCQuote(e, r = 0) {
|
|
487
|
-
const s = await
|
|
488
|
-
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 });
|
|
489
542
|
}
|
|
490
543
|
static fromTCQuoteStatic(e, r = 0) {
|
|
491
|
-
const s =
|
|
544
|
+
const s = D(e), a = this.shiftDecimals({
|
|
492
545
|
value: r,
|
|
493
|
-
from:
|
|
546
|
+
from: m.THOR,
|
|
494
547
|
to: s.decimal
|
|
495
548
|
});
|
|
496
|
-
return new
|
|
549
|
+
return new f({ ...s, value: a });
|
|
497
550
|
}
|
|
498
551
|
static async loadStaticAssets() {
|
|
499
552
|
return new Promise(
|
|
@@ -506,11 +559,11 @@ class A extends E {
|
|
|
506
559
|
NativeList: a,
|
|
507
560
|
...c
|
|
508
561
|
} = await import("@swapkit/tokens");
|
|
509
|
-
|
|
510
|
-
(
|
|
511
|
-
const
|
|
512
|
-
|
|
513
|
-
}),
|
|
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),
|
|
514
567
|
/* @__PURE__ */ new Map()
|
|
515
568
|
), e({ ok: !0 });
|
|
516
569
|
} catch (s) {
|
|
@@ -523,18 +576,9 @@ class A extends E {
|
|
|
523
576
|
}
|
|
524
577
|
);
|
|
525
578
|
}
|
|
526
|
-
get assetValue() {
|
|
527
|
-
return `${this.value} ${this.ticker}`;
|
|
528
|
-
}
|
|
529
|
-
toString() {
|
|
530
|
-
return `${this.chain}.${this.symbol}`;
|
|
531
|
-
}
|
|
532
|
-
eq({ chain: e, symbol: r }) {
|
|
533
|
-
return this.chain === e && this.symbol === r;
|
|
534
|
-
}
|
|
535
579
|
}
|
|
536
|
-
const
|
|
537
|
-
const t =
|
|
580
|
+
const kt = (n) => {
|
|
581
|
+
const t = f.fromChainOrSignature(n);
|
|
538
582
|
switch (n) {
|
|
539
583
|
case i.Bitcoin:
|
|
540
584
|
case i.Litecoin:
|
|
@@ -551,17 +595,17 @@ const Dt = (n) => {
|
|
|
551
595
|
default:
|
|
552
596
|
return t.add(1);
|
|
553
597
|
}
|
|
554
|
-
},
|
|
598
|
+
}, q = (n) => {
|
|
555
599
|
const t = n.slice(0, 14).includes("/"), e = n.includes(".") ? n : `${i.THORChain}.${n}`, [r, s] = e.split("."), [a, c] = s.split("-");
|
|
556
600
|
return {
|
|
557
601
|
address: c == null ? void 0 : c.toLowerCase(),
|
|
558
602
|
chain: r,
|
|
559
|
-
isGasAsset:
|
|
603
|
+
isGasAsset: dt({ chain: r, symbol: s }),
|
|
560
604
|
isSynthetic: t,
|
|
561
605
|
symbol: c ? `${a}-${(c == null ? void 0 : c.toLowerCase()) ?? ""}` : s,
|
|
562
606
|
ticker: t ? s : a
|
|
563
607
|
};
|
|
564
|
-
},
|
|
608
|
+
}, $t = {
|
|
565
609
|
/**
|
|
566
610
|
* Core
|
|
567
611
|
*/
|
|
@@ -618,44 +662,41 @@ const Dt = (n) => {
|
|
|
618
662
|
*/
|
|
619
663
|
helpers_number_different_decimals: 99101
|
|
620
664
|
};
|
|
621
|
-
class
|
|
665
|
+
class X extends Error {
|
|
622
666
|
constructor(t, e) {
|
|
623
|
-
console.error(e), super(t, { cause: { code:
|
|
667
|
+
console.error(e), super(t, { cause: { code: $t[t], message: t } }), Object.setPrototypeOf(this, X.prototype);
|
|
624
668
|
}
|
|
625
669
|
}
|
|
626
|
-
class
|
|
670
|
+
class wt extends T {
|
|
627
671
|
eq(t) {
|
|
628
672
|
return this.eqValue(t);
|
|
629
673
|
}
|
|
630
|
-
toString() {
|
|
631
|
-
return this.value;
|
|
632
|
-
}
|
|
633
674
|
}
|
|
634
675
|
export {
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
676
|
+
f as AssetValue,
|
|
677
|
+
T as BigIntArithmetics,
|
|
678
|
+
X as SwapKitError,
|
|
679
|
+
wt as SwapKitNumber,
|
|
680
|
+
yt as assetFromString,
|
|
681
|
+
Vt as derivationPathToString,
|
|
682
|
+
G as formatBigIntToSafeValue,
|
|
683
|
+
Ct as gasFeeMultiplier,
|
|
684
|
+
mt as getAssetType,
|
|
685
|
+
gt as getAsymmetricAssetShare,
|
|
686
|
+
Et as getAsymmetricAssetWithdrawAmount,
|
|
687
|
+
_t as getAsymmetricRuneShare,
|
|
688
|
+
Tt as getAsymmetricRuneWithdrawAmount,
|
|
648
689
|
ht as getCommonAssetInfo,
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
690
|
+
K as getDecimal,
|
|
691
|
+
Mt as getEstimatedPoolShare,
|
|
692
|
+
Nt as getLiquiditySlippage,
|
|
693
|
+
Rt as getMemoFor,
|
|
694
|
+
kt as getMinAmountByChain,
|
|
695
|
+
Bt as getRequest,
|
|
696
|
+
pt as getSymmetricPoolShare,
|
|
697
|
+
Ot as getSymmetricWithdraw,
|
|
698
|
+
St as getTHORNameCost,
|
|
699
|
+
dt as isGasAsset,
|
|
700
|
+
at as postRequest,
|
|
701
|
+
vt as validateTHORName
|
|
661
702
|
};
|