@swapkit/helpers 1.0.0-rc.0
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/LICENSE +201 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +326 -0
- package/dist/index.es-8503fb04.js +34669 -0
- package/dist/index.es-903b9173.cjs +1 -0
- package/dist/index.es.js +651 -0
- package/package.json +49 -0
- package/src/helpers/__tests__/asset.test.ts +157 -0
- package/src/helpers/__tests__/memo.test.ts +79 -0
- package/src/helpers/__tests__/others.test.ts +59 -0
- package/src/helpers/asset.ts +177 -0
- package/src/helpers/liquidity.ts +157 -0
- package/src/helpers/memo.ts +90 -0
- package/src/helpers/number.ts +40 -0
- package/src/helpers/others.ts +59 -0
- package/src/helpers/validators.ts +17 -0
- package/src/index.ts +15 -0
- package/src/modules/__tests__/assetValue.test.ts +285 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +331 -0
- package/src/modules/assetValue.ts +236 -0
- package/src/modules/bigIntArithmetics.ts +259 -0
- package/src/modules/swapKitError.ts +70 -0
- package/src/modules/swapKitNumber.ts +13 -0
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
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) => {
|
|
4
|
+
if (!t.has(n))
|
|
5
|
+
throw TypeError("Cannot " + e);
|
|
6
|
+
};
|
|
7
|
+
var p = (n, t, e) => {
|
|
8
|
+
if (t.has(n))
|
|
9
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
10
|
+
t instanceof WeakSet ? t.add(n) : t.set(n, e);
|
|
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) => {
|
|
15
|
+
if (n < 0)
|
|
16
|
+
throw new Error("Invalid number of year");
|
|
17
|
+
return 10 + n;
|
|
18
|
+
}, St = (n) => {
|
|
19
|
+
if (n.length > 30)
|
|
20
|
+
return !1;
|
|
21
|
+
const t = /^[a-zA-Z0-9+_-]+$/g;
|
|
22
|
+
return !!n.match(t);
|
|
23
|
+
}, Ct = ([n, t, e, r, s]) => `${n}'/${t}'/${e}'/${r}${typeof s != "number" ? "" : `/${s}`}`, Tt = 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", z = async ({ chain: n, to: t }) => {
|
|
41
|
+
try {
|
|
42
|
+
const e = await ct(
|
|
43
|
+
at[n],
|
|
44
|
+
JSON.stringify({
|
|
45
|
+
method: "eth_call",
|
|
46
|
+
params: [{ to: t.toLowerCase(), data: ot }, "latest"],
|
|
47
|
+
id: 44,
|
|
48
|
+
jsonrpc: "2.0"
|
|
49
|
+
}),
|
|
50
|
+
{ accept: "*/*", "cache-control": "no-cache", "content-type": "application/json" },
|
|
51
|
+
!0
|
|
52
|
+
), { result: r } = JSON.parse(e);
|
|
53
|
+
return parseInt(BigInt(r).toString());
|
|
54
|
+
} catch (e) {
|
|
55
|
+
return console.error(e), h[n];
|
|
56
|
+
}
|
|
57
|
+
}, ut = async (n) => {
|
|
58
|
+
if (n === i.Ethereum)
|
|
59
|
+
return h.ETH;
|
|
60
|
+
const [, t] = n.split("-");
|
|
61
|
+
return t != null && t.startsWith("0x") ? z({ chain: i.Ethereum, to: t }) : h.ETH;
|
|
62
|
+
}, lt = async (n) => {
|
|
63
|
+
const [, t] = n.split("-");
|
|
64
|
+
return t != null && t.startsWith("0x") ? z({ chain: i.Avalanche, to: t.toLowerCase() }) : h.AVAX;
|
|
65
|
+
}, dt = async (n) => n === i.BinanceSmartChain ? h.BSC : h.BSC, X = async ({ chain: n, symbol: t }) => {
|
|
66
|
+
switch (n) {
|
|
67
|
+
case i.Ethereum:
|
|
68
|
+
return ut(t);
|
|
69
|
+
case i.Avalanche:
|
|
70
|
+
return lt(t);
|
|
71
|
+
case i.BinanceSmartChain:
|
|
72
|
+
return dt(t);
|
|
73
|
+
default:
|
|
74
|
+
return h[n];
|
|
75
|
+
}
|
|
76
|
+
}, yt = {
|
|
77
|
+
[x.Average]: 1.2,
|
|
78
|
+
[x.Fast]: 1.5,
|
|
79
|
+
[x.Fastest]: 2
|
|
80
|
+
}, mt = ({ chain: n, symbol: t }) => {
|
|
81
|
+
switch (n) {
|
|
82
|
+
case i.Bitcoin:
|
|
83
|
+
case i.BitcoinCash:
|
|
84
|
+
case i.Litecoin:
|
|
85
|
+
case i.Dogecoin:
|
|
86
|
+
case i.Binance:
|
|
87
|
+
case i.Ethereum:
|
|
88
|
+
case i.Avalanche:
|
|
89
|
+
return t === n;
|
|
90
|
+
case i.Arbitrum:
|
|
91
|
+
case i.Optimism:
|
|
92
|
+
return t === "ETH";
|
|
93
|
+
case i.Maya:
|
|
94
|
+
return t === "CACAO";
|
|
95
|
+
case i.Cosmos:
|
|
96
|
+
return t === "ATOM";
|
|
97
|
+
case i.Polygon:
|
|
98
|
+
return t === "MATIC";
|
|
99
|
+
case i.BinanceSmartChain:
|
|
100
|
+
return t === "BNB";
|
|
101
|
+
case i.THORChain:
|
|
102
|
+
return t === "RUNE";
|
|
103
|
+
}
|
|
104
|
+
}, ht = (n) => {
|
|
105
|
+
switch (n) {
|
|
106
|
+
case "ETH.THOR":
|
|
107
|
+
return { identifier: "ETH.THOR-0xa5f2211b9b8170f694421f2046281775e8468044", decimal: 18 };
|
|
108
|
+
case "ETH.vTHOR":
|
|
109
|
+
return { identifier: "ETH.vTHOR-0x815c23eca83261b6ec689b60cc4a58b54bc24d8d", decimal: 18 };
|
|
110
|
+
case i.Cosmos:
|
|
111
|
+
return { identifier: "GAIA.ATOM", decimal: h[n] };
|
|
112
|
+
case i.THORChain:
|
|
113
|
+
return { identifier: "THOR.RUNE", decimal: h[n] };
|
|
114
|
+
case i.BinanceSmartChain:
|
|
115
|
+
return { identifier: "BSC.BNB", decimal: h[n] };
|
|
116
|
+
case i.Maya:
|
|
117
|
+
return { identifier: "MAYA.CACAO", decimal: h.MAYA };
|
|
118
|
+
case "MAYA.MAYA":
|
|
119
|
+
return { identifier: "MAYA.MAYA", decimal: 4 };
|
|
120
|
+
case i.Arbitrum:
|
|
121
|
+
case i.Optimism:
|
|
122
|
+
case i.BitcoinCash:
|
|
123
|
+
case i.Litecoin:
|
|
124
|
+
case i.Dogecoin:
|
|
125
|
+
case i.Binance:
|
|
126
|
+
case i.Avalanche:
|
|
127
|
+
case i.Polygon:
|
|
128
|
+
case i.Bitcoin:
|
|
129
|
+
case i.Ethereum:
|
|
130
|
+
return { identifier: `${n}.${n}`, decimal: h[n] };
|
|
131
|
+
}
|
|
132
|
+
}, _t = ({ chain: n, symbol: t }) => {
|
|
133
|
+
if (t.includes("/"))
|
|
134
|
+
return "Synth";
|
|
135
|
+
switch (n) {
|
|
136
|
+
case i.Bitcoin:
|
|
137
|
+
case i.BitcoinCash:
|
|
138
|
+
case i.Dogecoin:
|
|
139
|
+
case i.Litecoin:
|
|
140
|
+
case i.Maya:
|
|
141
|
+
case i.THORChain:
|
|
142
|
+
return "Native";
|
|
143
|
+
case i.Cosmos:
|
|
144
|
+
return t === "ATOM" ? "Native" : "GAIA";
|
|
145
|
+
case i.Binance:
|
|
146
|
+
return t === i.Binance ? "Native" : "BEP2";
|
|
147
|
+
case i.BinanceSmartChain:
|
|
148
|
+
return t === i.Binance ? "Native" : "BEP20";
|
|
149
|
+
case i.Ethereum:
|
|
150
|
+
return t === i.Ethereum ? "Native" : "ERC20";
|
|
151
|
+
case i.Avalanche:
|
|
152
|
+
return t === i.Avalanche ? "Native" : "AVAX";
|
|
153
|
+
case i.Polygon:
|
|
154
|
+
return t === i.Polygon ? "Native" : "POLYGON";
|
|
155
|
+
case i.Arbitrum:
|
|
156
|
+
return [i.Ethereum, i.Arbitrum].includes(t) ? "Native" : "ARBITRUM";
|
|
157
|
+
case i.Optimism:
|
|
158
|
+
return [i.Ethereum, i.Optimism].includes(t) ? "Native" : "OPTIMISM";
|
|
159
|
+
}
|
|
160
|
+
}, Bt = (n) => {
|
|
161
|
+
var c;
|
|
162
|
+
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
|
+
return { chain: t, symbol: s, ticker: a, synth: r };
|
|
164
|
+
}, gt = ({
|
|
165
|
+
liquidityUnits: n,
|
|
166
|
+
poolUnits: t,
|
|
167
|
+
runeDepth: e
|
|
168
|
+
}) => {
|
|
169
|
+
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);
|
|
170
|
+
return c.mul(o.sub(l).add(u)).div(g);
|
|
171
|
+
}, pt = ({
|
|
172
|
+
liquidityUnits: n,
|
|
173
|
+
poolUnits: t,
|
|
174
|
+
assetDepth: e
|
|
175
|
+
}) => {
|
|
176
|
+
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);
|
|
177
|
+
return g.div($);
|
|
178
|
+
}, Mt = ({
|
|
179
|
+
percent: n,
|
|
180
|
+
runeDepth: t,
|
|
181
|
+
liquidityUnits: e,
|
|
182
|
+
poolUnits: r
|
|
183
|
+
}) => gt({ runeDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), Et = ({
|
|
184
|
+
percent: n,
|
|
185
|
+
assetDepth: t,
|
|
186
|
+
liquidityUnits: e,
|
|
187
|
+
poolUnits: r
|
|
188
|
+
}) => pt({ assetDepth: t, liquidityUnits: e, poolUnits: r }).mul(n), m = (n) => new wt({ value: n, decimal: h.THOR }), Vt = ({
|
|
189
|
+
liquidityUnits: n,
|
|
190
|
+
poolUnits: t,
|
|
191
|
+
runeDepth: e,
|
|
192
|
+
assetDepth: r,
|
|
193
|
+
percent: s
|
|
194
|
+
}) => ({
|
|
195
|
+
assetAmount: m(r).mul(n).div(t).mul(s),
|
|
196
|
+
runeAmount: m(e).mul(n).div(t).mul(s)
|
|
197
|
+
}), Ot = ({
|
|
198
|
+
runeDepth: n,
|
|
199
|
+
poolUnits: t,
|
|
200
|
+
assetDepth: e,
|
|
201
|
+
liquidityUnits: r,
|
|
202
|
+
runeAmount: s,
|
|
203
|
+
assetAmount: a
|
|
204
|
+
}) => {
|
|
205
|
+
const c = m(n), o = m(e), l = m(t), u = m(s), g = m(a), $ = u.mul(o), S = g.mul(c), D = u.mul(g), K = c.mul(o), tt = l.mul($.add(S.add(D.mul(2)))), et = $.add(S.add(K.mul(2))), G = tt.div(et), H = m(r).add(G);
|
|
206
|
+
if (G.baseValueNumber === 0)
|
|
207
|
+
return H.div(l).baseValueNumber;
|
|
208
|
+
const nt = l.add(H);
|
|
209
|
+
return H.div(nt).baseValueNumber;
|
|
210
|
+
}, Nt = ({
|
|
211
|
+
runeAmount: n,
|
|
212
|
+
assetAmount: t,
|
|
213
|
+
runeDepth: e,
|
|
214
|
+
assetDepth: r
|
|
215
|
+
}) => {
|
|
216
|
+
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));
|
|
217
|
+
return Math.abs(l.div(u).baseValueNumber);
|
|
218
|
+
}, ft = ({
|
|
219
|
+
symbol: n,
|
|
220
|
+
ticker: t,
|
|
221
|
+
chain: e
|
|
222
|
+
}) => e === "ETH" && t !== "ETH" ? `${t}-${n.slice(-3)}` : n, Rt = (n, t) => {
|
|
223
|
+
switch (n) {
|
|
224
|
+
case f.LEAVE:
|
|
225
|
+
case f.BOND: {
|
|
226
|
+
const { address: e } = t;
|
|
227
|
+
return `${n}:${e}`;
|
|
228
|
+
}
|
|
229
|
+
case f.UNBOND: {
|
|
230
|
+
const { address: e, unbondAmount: r } = t;
|
|
231
|
+
return `${n}:${e}:${r * 10 ** 8}`;
|
|
232
|
+
}
|
|
233
|
+
case f.THORNAME_REGISTER: {
|
|
234
|
+
const { name: e, chain: r, address: s, owner: a } = t;
|
|
235
|
+
return `${n}:${e}:${r}:${s}${a ? `:${a}` : ""}`;
|
|
236
|
+
}
|
|
237
|
+
case f.DEPOSIT: {
|
|
238
|
+
const { chain: e, symbol: r, address: s, singleSide: a } = t;
|
|
239
|
+
return a ? `${n}:${e}/${r}::t:0` : `${n}:${e}.${r}:${s || ""}:t:0`;
|
|
240
|
+
}
|
|
241
|
+
case f.WITHDRAW: {
|
|
242
|
+
const { chain: e, ticker: r, symbol: s, basisPoints: a, targetAssetString: c, singleSide: o } = t, l = !o && c ? `:${c}` : "", u = ft({ chain: e, symbol: s, ticker: r });
|
|
243
|
+
return `${n}:${e}${o ? "/" : "."}${u}:${a}${l}`;
|
|
244
|
+
}
|
|
245
|
+
case f.OPEN_LOAN:
|
|
246
|
+
case f.CLOSE_LOAN: {
|
|
247
|
+
const { asset: e, address: r } = t;
|
|
248
|
+
return `${n}:${e}:${r}`;
|
|
249
|
+
}
|
|
250
|
+
default:
|
|
251
|
+
return "";
|
|
252
|
+
}
|
|
253
|
+
}, B = 8, U = ({
|
|
254
|
+
value: n,
|
|
255
|
+
bigIntDecimal: t = B,
|
|
256
|
+
decimal: e = B
|
|
257
|
+
}) => {
|
|
258
|
+
const r = n < 0n;
|
|
259
|
+
let s = n.toString().substring(r ? 1 : 0);
|
|
260
|
+
const a = e - (s.length - 1);
|
|
261
|
+
a > 0 && (s = "0".repeat(a) + s);
|
|
262
|
+
const c = s.length - e;
|
|
263
|
+
let o = s.slice(-e);
|
|
264
|
+
return parseInt(o[t]) >= 5 ? o = `${o.substring(0, t - 1)}${(parseInt(o[t - 1]) + 1).toString()}` : o = o.substring(0, t), `${r ? "-" : ""}${s.slice(0, c)}.${o}`.replace(
|
|
265
|
+
/\.?0*$/,
|
|
266
|
+
""
|
|
267
|
+
);
|
|
268
|
+
}, W = Object.values(i), At = (n = "") => {
|
|
269
|
+
const t = n.toUpperCase(), [e] = t.split(".");
|
|
270
|
+
if (W.includes(e))
|
|
271
|
+
return !0;
|
|
272
|
+
const [r] = t.split("/");
|
|
273
|
+
if (W.includes(r))
|
|
274
|
+
return !0;
|
|
275
|
+
throw new Error(
|
|
276
|
+
`Invalid identifier: ${n}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`
|
|
277
|
+
);
|
|
278
|
+
}, I = (n) => 10n ** BigInt(n), C = (n) => Math.log10(parseFloat(n.toString()));
|
|
279
|
+
var w, T, R, J, k, Q, V, P, b, y, O, q, v, N;
|
|
280
|
+
const M = class M {
|
|
281
|
+
constructor(t) {
|
|
282
|
+
p(this, w);
|
|
283
|
+
p(this, R);
|
|
284
|
+
p(this, k);
|
|
285
|
+
p(this, V);
|
|
286
|
+
p(this, b);
|
|
287
|
+
p(this, O);
|
|
288
|
+
p(this, v);
|
|
289
|
+
_(this, "decimalMultiplier", 10n ** 8n);
|
|
290
|
+
_(this, "bigIntValue", 0n);
|
|
291
|
+
_(this, "decimal");
|
|
292
|
+
const e = typeof t == "object", r = e ? t.value : t;
|
|
293
|
+
this.decimal = e ? t.decimal : void 0, this.decimalMultiplier = I(
|
|
294
|
+
Math.max(d(this, O, q).call(this, d(this, b, y).call(this, r)), this.decimal || 0)
|
|
295
|
+
), d(this, R, J).call(this, r);
|
|
296
|
+
}
|
|
297
|
+
static fromBigInt(t, e) {
|
|
298
|
+
return new M({
|
|
299
|
+
decimal: e,
|
|
300
|
+
value: U({ value: t, bigIntDecimal: e, decimal: e })
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
static shiftDecimals({
|
|
304
|
+
value: t,
|
|
305
|
+
from: e,
|
|
306
|
+
to: r
|
|
307
|
+
}) {
|
|
308
|
+
return M.fromBigInt(
|
|
309
|
+
new M(t).bigIntValue * I(r) / I(e),
|
|
310
|
+
r
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
get unsafeNumber() {
|
|
314
|
+
return parseFloat((this.bigIntValue / this.decimalMultiplier).toString());
|
|
315
|
+
}
|
|
316
|
+
get baseValue() {
|
|
317
|
+
return d(this, v, N).call(this, "string");
|
|
318
|
+
}
|
|
319
|
+
get baseValueNumber() {
|
|
320
|
+
return d(this, v, N).call(this, "number");
|
|
321
|
+
}
|
|
322
|
+
get baseValueBigInt() {
|
|
323
|
+
return d(this, v, N).call(this, "number");
|
|
324
|
+
}
|
|
325
|
+
get value() {
|
|
326
|
+
return this.formatBigIntToSafeValue(
|
|
327
|
+
this.bigIntValue,
|
|
328
|
+
this.decimal || C(this.decimalMultiplier)
|
|
329
|
+
);
|
|
330
|
+
}
|
|
331
|
+
add(...t) {
|
|
332
|
+
return d(this, w, T).call(this, "add", ...t);
|
|
333
|
+
}
|
|
334
|
+
sub(...t) {
|
|
335
|
+
return d(this, w, T).call(this, "sub", ...t);
|
|
336
|
+
}
|
|
337
|
+
mul(...t) {
|
|
338
|
+
return d(this, w, T).call(this, "mul", ...t);
|
|
339
|
+
}
|
|
340
|
+
div(...t) {
|
|
341
|
+
return d(this, w, T).call(this, "div", ...t);
|
|
342
|
+
}
|
|
343
|
+
gt(t) {
|
|
344
|
+
return this.bigIntValue > this.getBigIntValue(t);
|
|
345
|
+
}
|
|
346
|
+
gte(t) {
|
|
347
|
+
return this.bigIntValue >= this.getBigIntValue(t);
|
|
348
|
+
}
|
|
349
|
+
lt(t) {
|
|
350
|
+
return this.bigIntValue < this.getBigIntValue(t);
|
|
351
|
+
}
|
|
352
|
+
lte(t) {
|
|
353
|
+
return this.bigIntValue <= this.getBigIntValue(t);
|
|
354
|
+
}
|
|
355
|
+
eqValue(t) {
|
|
356
|
+
return this.bigIntValue === this.getBigIntValue(t);
|
|
357
|
+
}
|
|
358
|
+
getBigIntValue(t, e) {
|
|
359
|
+
return !e && typeof t == "object" ? t.bigIntValue : (t = typeof t == "object" ? t.value : t, d(this, V, P).call(this, d(this, b, y).call(this, t), e));
|
|
360
|
+
}
|
|
361
|
+
formatBigIntToSafeValue(t, e) {
|
|
362
|
+
const r = e || this.decimal || B, s = Math.max(
|
|
363
|
+
r,
|
|
364
|
+
C(this.decimalMultiplier)
|
|
365
|
+
), a = t < 0n;
|
|
366
|
+
let c = t.toString().substring(a ? 1 : 0);
|
|
367
|
+
const o = s - (c.length - 1);
|
|
368
|
+
o > 0 && (c = "0".repeat(o) + c);
|
|
369
|
+
const l = c.length - s;
|
|
370
|
+
let u = c.slice(-s);
|
|
371
|
+
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(
|
|
372
|
+
/\.?0*$/,
|
|
373
|
+
""
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
toSignificant(t) {
|
|
377
|
+
const e = this.value.split("."), r = e[0], s = e[1];
|
|
378
|
+
return s ? `${r}.${s.slice(0, t || this.decimal)}`.replace(
|
|
379
|
+
/\.?0*$/,
|
|
380
|
+
""
|
|
381
|
+
) : r;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
w = new WeakSet(), T = function(t, ...e) {
|
|
385
|
+
const r = d(this, k, Q).call(this, this, ...e), s = I(r), a = e.reduce(
|
|
386
|
+
(o, l) => {
|
|
387
|
+
const u = this.getBigIntValue(l, r);
|
|
388
|
+
if (t === "div" && u === 0n)
|
|
389
|
+
throw new RangeError("Division by zero");
|
|
390
|
+
return t === "add" ? o + u : t === "sub" ? o - u : t === "mul" ? o * u / s : o * s / u;
|
|
391
|
+
},
|
|
392
|
+
//normalize is to precision multiplier base
|
|
393
|
+
this.bigIntValue * s / this.decimalMultiplier
|
|
394
|
+
), c = U({
|
|
395
|
+
bigIntDecimal: r,
|
|
396
|
+
decimal: Math.max(r, C(this.decimalMultiplier)),
|
|
397
|
+
value: a
|
|
398
|
+
});
|
|
399
|
+
return new this.constructor({ decimal: this.decimal, value: c, identifier: this.toString() });
|
|
400
|
+
}, R = new WeakSet(), J = function(t, e) {
|
|
401
|
+
const r = d(this, b, y).call(this, t) || "0";
|
|
402
|
+
this.bigIntValue = e || d(this, V, P).call(this, r);
|
|
403
|
+
}, k = new WeakSet(), Q = function(...t) {
|
|
404
|
+
const e = t.map(
|
|
405
|
+
(r) => typeof r == "object" ? r.decimal || C(r.decimalMultiplier) : d(this, O, q).call(this, d(this, b, y).call(this, r))
|
|
406
|
+
).filter(Boolean);
|
|
407
|
+
return Math.max(...e, B);
|
|
408
|
+
}, V = new WeakSet(), P = function(t, e) {
|
|
409
|
+
const r = e ? I(e) : this.decimalMultiplier, s = C(r), [a, c = ""] = t.split(".");
|
|
410
|
+
return BigInt(`${a}${c.padEnd(s, "0")}`);
|
|
411
|
+
}, b = new WeakSet(), y = function(t) {
|
|
412
|
+
const r = `${typeof t == "number" ? Number(t).toLocaleString("fullwide", {
|
|
413
|
+
useGrouping: !1,
|
|
414
|
+
maximumFractionDigits: 20
|
|
415
|
+
}) : t}`.replaceAll(",", ".").split(".");
|
|
416
|
+
return r.length > 1 ? `${r.slice(0, -1).join("")}.${r.at(-1)}` : r[0];
|
|
417
|
+
}, O = new WeakSet(), q = function(t) {
|
|
418
|
+
var r;
|
|
419
|
+
const e = ((r = t.split(".")[1]) == null ? void 0 : r.length) || 0;
|
|
420
|
+
return Math.max(e, B);
|
|
421
|
+
}, v = new WeakSet(), N = function(t) {
|
|
422
|
+
const e = this.decimalMultiplier / I(this.decimal || 0), r = this.bigIntValue / e;
|
|
423
|
+
switch (t) {
|
|
424
|
+
case "number":
|
|
425
|
+
return Number(r);
|
|
426
|
+
case "string":
|
|
427
|
+
return r.toString();
|
|
428
|
+
default:
|
|
429
|
+
return r;
|
|
430
|
+
}
|
|
431
|
+
};
|
|
432
|
+
let E = M, j;
|
|
433
|
+
const L = (n) => {
|
|
434
|
+
if (!j)
|
|
435
|
+
throw new Error("Static assets not loaded, call await AssetValue.loadStaticAssets() first");
|
|
436
|
+
return j.get(n.toUpperCase()) || { decimal: h.THOR, identifier: "" };
|
|
437
|
+
}, Y = async (n, t = 0) => {
|
|
438
|
+
At(n);
|
|
439
|
+
const e = await X(F(n));
|
|
440
|
+
return new A({ decimal: e, value: t, identifier: n });
|
|
441
|
+
};
|
|
442
|
+
class A extends E {
|
|
443
|
+
constructor(e) {
|
|
444
|
+
super(
|
|
445
|
+
e.value instanceof E ? e.value : { decimal: e.decimal, value: e.value }
|
|
446
|
+
);
|
|
447
|
+
_(this, "address");
|
|
448
|
+
_(this, "chain");
|
|
449
|
+
_(this, "isSynthetic", !1);
|
|
450
|
+
_(this, "isGasAsset", !1);
|
|
451
|
+
_(this, "symbol");
|
|
452
|
+
_(this, "ticker");
|
|
453
|
+
_(this, "type");
|
|
454
|
+
const r = "identifier" in e ? e.identifier : `${e.chain}.${e.symbol}`, s = F(r);
|
|
455
|
+
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;
|
|
456
|
+
}
|
|
457
|
+
static async fromString(e, r = 0) {
|
|
458
|
+
return Y(e, r);
|
|
459
|
+
}
|
|
460
|
+
static fromStringSync(e, r = 0) {
|
|
461
|
+
const { decimal: s, identifier: a } = L(
|
|
462
|
+
e
|
|
463
|
+
);
|
|
464
|
+
return a ? new A({ decimal: s, identifier: a, value: r }) : void 0;
|
|
465
|
+
}
|
|
466
|
+
static async fromIdentifier(e, r = 0) {
|
|
467
|
+
return Y(e, r);
|
|
468
|
+
}
|
|
469
|
+
static fromIdentifierSync(e, r = 0) {
|
|
470
|
+
const { decimal: s, identifier: a } = L(e);
|
|
471
|
+
return new A({ decimal: s, identifier: a, value: r });
|
|
472
|
+
}
|
|
473
|
+
static fromChainOrSignature(e, r = 0) {
|
|
474
|
+
const { decimal: s, identifier: a } = ht(e);
|
|
475
|
+
return new A({ value: r, decimal: s, identifier: a });
|
|
476
|
+
}
|
|
477
|
+
static async fromTCQuote(e, r = 0) {
|
|
478
|
+
const s = await X(F(e)), a = this.shiftDecimals({ value: r, from: h.THOR, to: s });
|
|
479
|
+
return new A({ value: a, identifier: e, decimal: s });
|
|
480
|
+
}
|
|
481
|
+
static fromTCQuoteStatic(e, r = 0) {
|
|
482
|
+
const s = L(e), a = this.shiftDecimals({
|
|
483
|
+
value: r,
|
|
484
|
+
from: h.THOR,
|
|
485
|
+
to: s.decimal
|
|
486
|
+
});
|
|
487
|
+
return new A({ ...s, value: a });
|
|
488
|
+
}
|
|
489
|
+
static async loadStaticAssets() {
|
|
490
|
+
return new Promise(
|
|
491
|
+
async (e, r) => {
|
|
492
|
+
try {
|
|
493
|
+
const {
|
|
494
|
+
// Omit ThorchainList from import to avoid decimals conflict (TC uses 8 for all)
|
|
495
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
496
|
+
ThorchainList: s,
|
|
497
|
+
NativeList: a,
|
|
498
|
+
...c
|
|
499
|
+
} = await import("./index.es-8503fb04.js");
|
|
500
|
+
j = [a, ...Object.values(c)].reduce(
|
|
501
|
+
(l, { tokens: u }) => (u.forEach(({ identifier: g, chain: $, ...S }) => {
|
|
502
|
+
const D = "decimals" in S ? S.decimals : h[$];
|
|
503
|
+
l.set(g, { identifier: g, decimal: D });
|
|
504
|
+
}), l),
|
|
505
|
+
/* @__PURE__ */ new Map()
|
|
506
|
+
), e({ ok: !0 });
|
|
507
|
+
} catch (s) {
|
|
508
|
+
console.error(s), r({
|
|
509
|
+
ok: !1,
|
|
510
|
+
error: s,
|
|
511
|
+
message: "Couldn't load static assets. Ensure you have installed @swapkit/tokens package"
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
);
|
|
516
|
+
}
|
|
517
|
+
get assetValue() {
|
|
518
|
+
return `${this.value} ${this.ticker}`;
|
|
519
|
+
}
|
|
520
|
+
toString() {
|
|
521
|
+
return `${this.chain}.${this.symbol}`;
|
|
522
|
+
}
|
|
523
|
+
eq({ chain: e, symbol: r }) {
|
|
524
|
+
return this.chain === e && this.symbol === r;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const kt = (n) => {
|
|
528
|
+
const t = A.fromChainOrSignature(n);
|
|
529
|
+
switch (n) {
|
|
530
|
+
case i.Bitcoin:
|
|
531
|
+
case i.Litecoin:
|
|
532
|
+
case i.BitcoinCash:
|
|
533
|
+
return t.add(10001);
|
|
534
|
+
case i.Dogecoin:
|
|
535
|
+
return t.add(100000001);
|
|
536
|
+
case i.Avalanche:
|
|
537
|
+
case i.Ethereum:
|
|
538
|
+
return t.add(10 * 10 ** 9);
|
|
539
|
+
case i.THORChain:
|
|
540
|
+
case i.Maya:
|
|
541
|
+
return t.add(0);
|
|
542
|
+
default:
|
|
543
|
+
return t.add(1);
|
|
544
|
+
}
|
|
545
|
+
}, F = (n) => {
|
|
546
|
+
const t = n.slice(0, 14).includes("/"), e = n.includes(".") ? n : `${i.THORChain}.${n}`, [r, s] = e.split("."), [a, c] = s.split("-");
|
|
547
|
+
return {
|
|
548
|
+
address: c == null ? void 0 : c.toLowerCase(),
|
|
549
|
+
chain: r,
|
|
550
|
+
isGasAsset: mt({ chain: r, symbol: s }),
|
|
551
|
+
isSynthetic: t,
|
|
552
|
+
symbol: c ? `${a}-${(c == null ? void 0 : c.toLowerCase()) ?? ""}` : s,
|
|
553
|
+
ticker: t ? s : a
|
|
554
|
+
};
|
|
555
|
+
}, $t = {
|
|
556
|
+
/**
|
|
557
|
+
* Core
|
|
558
|
+
*/
|
|
559
|
+
core_wallet_connection_not_found: 10001,
|
|
560
|
+
core_estimated_max_spendable_chain_not_supported: 10002,
|
|
561
|
+
core_extend_error: 10003,
|
|
562
|
+
core_inbound_data_not_found: 10004,
|
|
563
|
+
core_approve_asset_address_or_from_not_found: 10005,
|
|
564
|
+
core_chain_halted: 10099,
|
|
565
|
+
/**
|
|
566
|
+
* Core - Wallet Connection
|
|
567
|
+
*/
|
|
568
|
+
core_wallet_xdefi_not_installed: 10101,
|
|
569
|
+
core_wallet_evmwallet_not_installed: 10102,
|
|
570
|
+
core_wallet_walletconnect_not_installed: 10103,
|
|
571
|
+
core_wallet_keystore_not_installed: 10104,
|
|
572
|
+
core_wallet_ledger_not_installed: 10105,
|
|
573
|
+
core_wallet_trezor_not_installed: 10106,
|
|
574
|
+
core_wallet_keplr_not_installed: 10107,
|
|
575
|
+
core_wallet_okx_not_installed: 10108,
|
|
576
|
+
/**
|
|
577
|
+
* Core - Swap
|
|
578
|
+
*/
|
|
579
|
+
core_swap_invalid_params: 10200,
|
|
580
|
+
core_swap_route_not_complete: 10201,
|
|
581
|
+
core_swap_asset_not_recognized: 10202,
|
|
582
|
+
core_swap_contract_not_found: 10203,
|
|
583
|
+
core_swap_route_transaction_not_found: 10204,
|
|
584
|
+
core_swap_contract_not_supported: 10205,
|
|
585
|
+
core_swap_transaction_error: 10206,
|
|
586
|
+
core_swap_quote_mode_not_supported: 10207,
|
|
587
|
+
/**
|
|
588
|
+
* Core - Transaction
|
|
589
|
+
*/
|
|
590
|
+
core_transaction_deposit_error: 10301,
|
|
591
|
+
core_transaction_create_liquidity_rune_error: 10302,
|
|
592
|
+
core_transaction_create_liquidity_asset_error: 10303,
|
|
593
|
+
core_transaction_create_liquidity_invalid_params: 10304,
|
|
594
|
+
core_transaction_add_liquidity_invalid_params: 10305,
|
|
595
|
+
core_transaction_add_liquidity_no_rune_address: 10306,
|
|
596
|
+
core_transaction_add_liquidity_rune_error: 10307,
|
|
597
|
+
core_transaction_add_liquidity_asset_error: 10308,
|
|
598
|
+
core_transaction_withdraw_error: 10309,
|
|
599
|
+
core_transaction_deposit_to_pool_error: 10310,
|
|
600
|
+
core_transaction_deposit_insufficient_funds_error: 10311,
|
|
601
|
+
core_transaction_deposit_gas_error: 10312,
|
|
602
|
+
core_transaction_deposit_server_error: 10313,
|
|
603
|
+
/**
|
|
604
|
+
* Wallets
|
|
605
|
+
*/
|
|
606
|
+
wallet_ledger_connection_error: 20001,
|
|
607
|
+
/**
|
|
608
|
+
* Helpers
|
|
609
|
+
*/
|
|
610
|
+
helpers_number_different_decimals: 99101
|
|
611
|
+
};
|
|
612
|
+
class Z extends Error {
|
|
613
|
+
constructor(t, e) {
|
|
614
|
+
console.error(e), super(t, { cause: { code: $t[t], message: t } }), Object.setPrototypeOf(this, Z.prototype);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
class wt extends E {
|
|
618
|
+
eq(t) {
|
|
619
|
+
return this.eqValue(t);
|
|
620
|
+
}
|
|
621
|
+
toString() {
|
|
622
|
+
return this.value;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
export {
|
|
626
|
+
A as AssetValue,
|
|
627
|
+
B as DEFAULT_DECIMAL,
|
|
628
|
+
Z as SwapKitError,
|
|
629
|
+
wt as SwapKitNumber,
|
|
630
|
+
Bt as assetFromString,
|
|
631
|
+
Ct as derivationPathToString,
|
|
632
|
+
U as formatBigIntToSafeValue,
|
|
633
|
+
yt as gasFeeMultiplier,
|
|
634
|
+
_t as getAssetType,
|
|
635
|
+
pt as getAsymmetricAssetShare,
|
|
636
|
+
Et as getAsymmetricAssetWithdrawAmount,
|
|
637
|
+
gt as getAsymmetricRuneShare,
|
|
638
|
+
Mt as getAsymmetricRuneWithdrawAmount,
|
|
639
|
+
ht as getCommonAssetInfo,
|
|
640
|
+
X as getDecimal,
|
|
641
|
+
Ot as getEstimatedPoolShare,
|
|
642
|
+
Nt as getLiquiditySlippage,
|
|
643
|
+
Rt as getMemoFor,
|
|
644
|
+
kt as getMinAmountByChain,
|
|
645
|
+
Tt as getRequest,
|
|
646
|
+
Vt as getSymmetricWithdraw,
|
|
647
|
+
vt as getTHORNameCost,
|
|
648
|
+
mt as isGasAsset,
|
|
649
|
+
ct as postRequest,
|
|
650
|
+
St as validateTHORName
|
|
651
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"author": "swapkit-oss-team",
|
|
3
|
+
"description": "SwapKit Lib swapkit-helpers",
|
|
4
|
+
"devDependencies": {
|
|
5
|
+
"@vitest/coverage-istanbul": "0.34.4",
|
|
6
|
+
"vite": "4.4.9",
|
|
7
|
+
"vitest": "0.34.4",
|
|
8
|
+
"@internal/config": "0.0.0-internal.0",
|
|
9
|
+
"@swapkit/tokens": "1.0.0-rc.0",
|
|
10
|
+
"@swapkit/types": "1.0.0-rc.0"
|
|
11
|
+
},
|
|
12
|
+
"eslintConfig": {
|
|
13
|
+
"extends": "../../../internal/eslint-config"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@swapkit/types": "1.0.0-rc.0"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/index.es.js",
|
|
21
|
+
"require": "./dist/index.cjs",
|
|
22
|
+
"types": "./dist/index.d.ts"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"src/",
|
|
27
|
+
"dist/"
|
|
28
|
+
],
|
|
29
|
+
"homepage": "https://github.com/thorswap/SwapKit",
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"main": "./dist/index.cjs",
|
|
32
|
+
"module": "./dist/index.es.js",
|
|
33
|
+
"name": "@swapkit/helpers",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"react-native": "./src/index.ts",
|
|
38
|
+
"repository": "https://github.com/thorswap/SwapKit.git",
|
|
39
|
+
"type": "module",
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"version": "1.0.0-rc.0",
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "vite build",
|
|
44
|
+
"clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
|
|
45
|
+
"lint": "eslint ./ --ext .ts,.tsx --fix; tsc --noEmit",
|
|
46
|
+
"test": "vitest --run",
|
|
47
|
+
"test:coverage": "vitest run --coverage"
|
|
48
|
+
}
|
|
49
|
+
}
|