@swapkit/helpers 4.4.4 → 4.5.2

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.
Files changed (69) hide show
  1. package/dist/api/index.cjs +2 -2
  2. package/dist/api/index.cjs.map +4 -4
  3. package/dist/api/index.js +2 -2
  4. package/dist/api/index.js.map +4 -4
  5. package/dist/index.cjs +3 -3
  6. package/dist/index.cjs.map +10 -10
  7. package/dist/index.js +3 -3
  8. package/dist/index.js.map +10 -10
  9. package/dist/types/api/index.d.ts +247 -0
  10. package/dist/types/api/index.d.ts.map +1 -1
  11. package/dist/types/api/swapkitApi/endpoints.d.ts +247 -0
  12. package/dist/types/api/swapkitApi/endpoints.d.ts.map +1 -1
  13. package/dist/types/api/swapkitApi/types.d.ts +3 -1
  14. package/dist/types/api/swapkitApi/types.d.ts.map +1 -1
  15. package/dist/types/modules/assetValue.d.ts +6 -0
  16. package/dist/types/modules/assetValue.d.ts.map +1 -1
  17. package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -1
  18. package/dist/types/modules/swapKitConfig.d.ts +63 -36
  19. package/dist/types/modules/swapKitConfig.d.ts.map +1 -1
  20. package/dist/types/modules/swapKitError.d.ts +20 -24
  21. package/dist/types/modules/swapKitError.d.ts.map +1 -1
  22. package/dist/types/types/wallet.d.ts +5 -1
  23. package/dist/types/types/wallet.d.ts.map +1 -1
  24. package/dist/types/utils/asset.d.ts +1 -1
  25. package/dist/types/utils/asset.d.ts.map +1 -1
  26. package/package.json +24 -12
  27. package/src/api/index.ts +9 -0
  28. package/src/api/midgard/endpoints.ts +348 -0
  29. package/src/api/midgard/types.ts +515 -0
  30. package/src/api/swapkitApi/endpoints.ts +242 -0
  31. package/src/api/swapkitApi/types.ts +624 -0
  32. package/src/api/thornode/endpoints.ts +105 -0
  33. package/src/api/thornode/types.ts +247 -0
  34. package/src/contracts.ts +1 -0
  35. package/src/index.ts +28 -0
  36. package/src/modules/__tests__/assetValue.test.ts +1637 -0
  37. package/src/modules/__tests__/bigIntArithmetics.test.ts +383 -0
  38. package/src/modules/__tests__/swapKitConfig.test.ts +425 -0
  39. package/src/modules/__tests__/swapKitNumber.test.ts +535 -0
  40. package/src/modules/assetValue.ts +532 -0
  41. package/src/modules/bigIntArithmetics.ts +363 -0
  42. package/src/modules/feeMultiplier.ts +80 -0
  43. package/src/modules/requestClient.ts +110 -0
  44. package/src/modules/swapKitConfig.ts +174 -0
  45. package/src/modules/swapKitError.ts +470 -0
  46. package/src/modules/swapKitNumber.ts +13 -0
  47. package/src/tokens.ts +1 -0
  48. package/src/types/commonTypes.ts +10 -0
  49. package/src/types/derivationPath.ts +11 -0
  50. package/src/types/errors/apiV1.ts +0 -0
  51. package/src/types/index.ts +5 -0
  52. package/src/types/quotes.ts +174 -0
  53. package/src/types/sdk.ts +38 -0
  54. package/src/types/wallet.ts +124 -0
  55. package/src/utils/__tests__/asset.test.ts +185 -0
  56. package/src/utils/__tests__/derivationPath.test.ts +16 -0
  57. package/src/utils/__tests__/explorerUrls.test.ts +59 -0
  58. package/src/utils/__tests__/memo.test.ts +99 -0
  59. package/src/utils/__tests__/others.test.ts +83 -0
  60. package/src/utils/__tests__/validators.test.ts +24 -0
  61. package/src/utils/asset.ts +395 -0
  62. package/src/utils/chains.ts +100 -0
  63. package/src/utils/derivationPath.ts +101 -0
  64. package/src/utils/explorerUrls.ts +32 -0
  65. package/src/utils/liquidity.ts +150 -0
  66. package/src/utils/memo.ts +102 -0
  67. package/src/utils/others.ts +62 -0
  68. package/src/utils/validators.ts +32 -0
  69. package/src/utils/wallets.ts +237 -0
@@ -0,0 +1,383 @@
1
+ import { describe, expect, test } from "bun:test";
2
+
3
+ import { BigIntArithmetics, formatBigIntToSafeValue } from "../bigIntArithmetics";
4
+
5
+ describe("BigIntArithmetics", () => {
6
+ describe("formatBigIntToSafeValue", () => {
7
+ test("parse bigint with decimals to string", () => {
8
+ const safeValue1 = formatBigIntToSafeValue({ bigIntDecimal: 6, decimal: 6, value: BigInt(0) });
9
+ expect(safeValue1).toBe("0");
10
+
11
+ const safeValue2 = formatBigIntToSafeValue({ bigIntDecimal: 0, decimal: 0, value: BigInt(15) });
12
+ expect(safeValue2).toBe("15");
13
+
14
+ const safeValue3 = formatBigIntToSafeValue({ bigIntDecimal: 4, decimal: 4, value: BigInt(123456789) });
15
+ expect(safeValue3).toBe("12345.6789");
16
+ });
17
+
18
+ test("handles negative values correctly", () => {
19
+ const safeValue = formatBigIntToSafeValue({ bigIntDecimal: 8, decimal: 8, value: -100000000n });
20
+ expect(safeValue).toBe("-1");
21
+
22
+ const safeValue2 = formatBigIntToSafeValue({ bigIntDecimal: 6, decimal: 6, value: -123456789n });
23
+ expect(safeValue2).toBe("-123.456789");
24
+ });
25
+
26
+ test("handles rounding correctly when bigIntDecimal < decimal", () => {
27
+ // Value: 1.99, rounding to 1 decimal
28
+ const safeValue = formatBigIntToSafeValue({ bigIntDecimal: 1, decimal: 2, value: 199n });
29
+ expect(safeValue).toBe("1.1");
30
+
31
+ // Value: 1.94, rounding to 1 decimal
32
+ const safeValue2 = formatBigIntToSafeValue({ bigIntDecimal: 1, decimal: 2, value: 194n });
33
+ expect(safeValue2).toBe("1.9");
34
+ });
35
+
36
+ test("handles very large bigint values", () => {
37
+ const largeValue = 123456789012345678901234567890n;
38
+ const safeValue = formatBigIntToSafeValue({ bigIntDecimal: 8, decimal: 8, value: largeValue });
39
+ expect(safeValue).toBe("1234567890123456789012.3456789");
40
+ });
41
+ });
42
+
43
+ describe("toFixed", () => {
44
+ test("returns fixed decimal places for positive number", () => {
45
+ const num = new BigIntArithmetics(123.456789);
46
+ expect(num.toFixed(2)).toBe("123.46");
47
+ expect(num.toFixed(4)).toBe("123.4568");
48
+ expect(num.toFixed(6)).toBe("123.456789");
49
+ });
50
+
51
+ test("returns fixed decimal places for decimal less than 1", () => {
52
+ const num = new BigIntArithmetics(0.123456);
53
+ expect(num.toFixed(2)).toBe("0.12");
54
+ expect(num.toFixed(4)).toBe("0.1235");
55
+ expect(num.toFixed(6)).toBe("0.123456");
56
+ });
57
+
58
+ test("pads with zeros when needed", () => {
59
+ const num = new BigIntArithmetics(1.2);
60
+ expect(num.toFixed(4)).toBe("1.2000");
61
+
62
+ const num2 = new BigIntArithmetics(10);
63
+ expect(num2.toFixed(2)).toBe("10.00");
64
+ });
65
+
66
+ test("handles zero with fixed decimals", () => {
67
+ const num = new BigIntArithmetics(0);
68
+ expect(num.toFixed(0)).toBe("0");
69
+ expect(num.toFixed(2)).toBe("0.00");
70
+ });
71
+
72
+ test("handles very small decimals", () => {
73
+ const num = new BigIntArithmetics(0.00000123);
74
+ expect(num.toFixed(8)).toBe("0.00000123");
75
+ expect(num.toFixed(6)).toBe("0.000001");
76
+ });
77
+
78
+ test("handles negative numbers", () => {
79
+ const num = new BigIntArithmetics(-123.456);
80
+ expect(num.toFixed(2)).toBe("-123.46");
81
+ expect(num.toFixed(4)).toBe("-123.4560");
82
+ });
83
+
84
+ test("handles integer with toFixed", () => {
85
+ const num = new BigIntArithmetics(1234);
86
+ expect(num.toFixed(0)).toBe("1234.0");
87
+ expect(num.toFixed(2)).toBe("1234.00");
88
+ });
89
+
90
+ test("handles very large numbers without precision loss", () => {
91
+ const large = new BigIntArithmetics("12345678901234567890.123456789");
92
+ expect(large.toFixed(2)).toBe("12345678901234567890.12");
93
+ expect(large.toFixed(6)).toBe("12345678901234567890.123457");
94
+
95
+ const roundUp = new BigIntArithmetics("999999999999999999.999");
96
+ expect(roundUp.toFixed(2)).toBe("1000000000000000000.00");
97
+ expect(roundUp.toFixed(0)).toBe("1000000000000000000.0");
98
+ });
99
+ });
100
+
101
+ describe("divideBigIntWithRounding edge cases", () => {
102
+ test("handles positive divided by positive with rounding", () => {
103
+ const num1 = new BigIntArithmetics(10);
104
+ const num2 = new BigIntArithmetics(3);
105
+ const result = num1.div(num2);
106
+
107
+ // 10 / 3 = 3.333...
108
+ expect(result.getValue("number")).toBeCloseTo(3.333333, 5);
109
+ });
110
+
111
+ test("handles negative divided by positive", () => {
112
+ const num1 = new BigIntArithmetics(-10);
113
+ const num2 = new BigIntArithmetics(3);
114
+ const result = num1.div(num2);
115
+
116
+ expect(result.getValue("number")).toBeCloseTo(-3.333333, 5);
117
+ });
118
+
119
+ test("handles positive divided by negative", () => {
120
+ const num1 = new BigIntArithmetics(10);
121
+ const num2 = new BigIntArithmetics(-3);
122
+ const result = num1.div(num2);
123
+
124
+ expect(result.getValue("number")).toBeCloseTo(-3.333333, 5);
125
+ });
126
+
127
+ test("handles negative divided by negative", () => {
128
+ const num1 = new BigIntArithmetics(-10);
129
+ const num2 = new BigIntArithmetics(-3);
130
+ const result = num1.div(num2);
131
+
132
+ expect(result.getValue("number")).toBeCloseTo(3.333333, 5);
133
+ });
134
+
135
+ test("Division of minimum base value maintains precision", () => {
136
+ const minValue = new BigIntArithmetics({ decimal: 8, value: 0.00000001 });
137
+ expect(minValue.getBaseValue("bigint")).toBe(1n);
138
+
139
+ const divided = minValue.div(2);
140
+ expect(divided.getBaseValue("bigint")).toBe(1n);
141
+
142
+ const multiplied = divided.mul(2);
143
+ expect(multiplied.getBaseValue("bigint")).toBe(1n);
144
+ });
145
+
146
+ test("Division with sub-unit precision is maintained", () => {
147
+ const value = new BigIntArithmetics({ decimal: 8, value: 0.00000001 });
148
+
149
+ expect(value.div(2).getBaseValue("bigint")).toBe(1n);
150
+ expect(value.div(3).getBaseValue("bigint")).toBe(0n);
151
+ expect(value.div(10).getBaseValue("bigint")).toBe(0n);
152
+ expect(value.div(100).getBaseValue("bigint")).toBe(0n);
153
+ });
154
+
155
+ test("Percentage calculations on small values preserve precision", () => {
156
+ const fee = new BigIntArithmetics({ decimal: 8, value: 0.00000001 });
157
+
158
+ const tenPercent = fee.mul(0.1);
159
+ expect(tenPercent.getValue("string")).toBe("0.000000001");
160
+
161
+ const dividedBy10 = fee.div(10);
162
+ expect(dividedBy10.getValue("string")).toBe("0.000000001");
163
+
164
+ expect(tenPercent.getValue("string")).toBe(dividedBy10.getValue("string"));
165
+ });
166
+ });
167
+
168
+ describe("constructor edge cases", () => {
169
+ test("handles string with commas", () => {
170
+ const num = new BigIntArithmetics("1,234.56");
171
+ expect(num.getValue("number")).toBe(1234.56);
172
+ });
173
+
174
+ test("handles very large number strings", () => {
175
+ const num = new BigIntArithmetics("123456789012345678901234567890");
176
+ expect(num.getValue("string")).toContain("123456789012345678901234567890");
177
+ });
178
+
179
+ test("handles decimal parameter correctly", () => {
180
+ const num = new BigIntArithmetics({ decimal: 2, value: 1.234 });
181
+ expect(num.decimal).toBe(2);
182
+ expect(num.getValue("string")).toBe("1.23");
183
+ });
184
+
185
+ test("handles BigIntArithmetics instance as parameter", () => {
186
+ const num1 = new BigIntArithmetics(100);
187
+ const num2 = new BigIntArithmetics(num1);
188
+ expect(num2.getValue("number")).toBe(100);
189
+ });
190
+ });
191
+
192
+ describe("getValue with different types", () => {
193
+ test("returns correct type for each option", () => {
194
+ const num = new BigIntArithmetics(123.456);
195
+
196
+ expect(typeof num.getValue("string")).toBe("string");
197
+ expect(typeof num.getValue("number")).toBe("number");
198
+ expect(typeof num.getValue("bigint")).toBe("bigint");
199
+ });
200
+
201
+ test("handles adjusted decimal parameter", () => {
202
+ const num = new BigIntArithmetics({ decimal: 18, value: "1.123456789012345678" });
203
+
204
+ expect(num.getValue("string", 8)).toBe("1.12345679");
205
+ expect(num.getValue("string", 12)).toBe("1.123456789012");
206
+ });
207
+ });
208
+
209
+ describe("getBaseValue with different types", () => {
210
+ test("returns base value correctly for bigint", () => {
211
+ const num = new BigIntArithmetics({ decimal: 8, value: 1 });
212
+ expect(num.getBaseValue("bigint")).toBe(100000000n);
213
+ });
214
+
215
+ test("returns base value correctly for number", () => {
216
+ const num = new BigIntArithmetics({ decimal: 8, value: 1.5 });
217
+ expect(num.getBaseValue("number")).toBe(150000000);
218
+ });
219
+
220
+ test("returns base value correctly for string", () => {
221
+ const num = new BigIntArithmetics({ decimal: 6, value: 100.5 });
222
+ expect(num.getBaseValue("string")).toBe("100500000");
223
+ });
224
+
225
+ test("handles decimal adjustment in getBaseValue", () => {
226
+ const num = new BigIntArithmetics({ decimal: 18, value: 1 });
227
+ expect(num.getBaseValue("bigint", 8)).toBe(100000000n);
228
+ expect(num.getBaseValue("bigint", 10)).toBe(10000000000n);
229
+ });
230
+ });
231
+
232
+ describe("toAbbreviation edge cases", () => {
233
+ test("handles negative numbers", () => {
234
+ const num = new BigIntArithmetics(-1234567);
235
+ expect(num.toAbbreviation()).toBe("-1.23M");
236
+ });
237
+
238
+ test("handles zero", () => {
239
+ const num = new BigIntArithmetics(0);
240
+ expect(num.toAbbreviation()).toBe("0");
241
+ });
242
+
243
+ test("handles numbers smaller than 1000", () => {
244
+ const num = new BigIntArithmetics(999);
245
+ expect(num.toAbbreviation()).toBe("999");
246
+ });
247
+
248
+ test("handles very large numbers beyond defined abbreviations", () => {
249
+ const num = new BigIntArithmetics("1234567890123456789012345");
250
+ const result = num.toAbbreviation();
251
+ expect(result).toEqual(num.getValue("string"));
252
+ });
253
+
254
+ test("respects custom digit parameter", () => {
255
+ const num = new BigIntArithmetics(1234567);
256
+ expect(num.toAbbreviation(0)).toBe("1M");
257
+ expect(num.toAbbreviation(1)).toBe("1.2M");
258
+ expect(num.toAbbreviation(3)).toBe("1.235M");
259
+ });
260
+ });
261
+
262
+ describe("toCurrency edge cases", () => {
263
+ test("handles different currency symbols", () => {
264
+ const num = new BigIntArithmetics(1234.56);
265
+ expect(num.toCurrency("€")).toContain("€");
266
+ expect(num.toCurrency("£")).toContain("£");
267
+ expect(num.toCurrency("¥")).toContain("¥");
268
+ });
269
+
270
+ test("handles currency position", () => {
271
+ const num = new BigIntArithmetics(1234.56);
272
+ expect(num.toCurrency("€", { currencyPosition: "start" })).toMatch(/^€/);
273
+ expect(num.toCurrency("€", { currencyPosition: "end" })).toMatch(/€$/);
274
+ });
275
+
276
+ test("handles custom separators", () => {
277
+ const num = new BigIntArithmetics(1234567.89);
278
+ const result = num.toCurrency("$", { decimalSeparator: ",", thousandSeparator: " " });
279
+ expect(result).toContain(" ");
280
+ expect(result).toContain(",");
281
+ });
282
+
283
+ test("handles zero with currency", () => {
284
+ const num = new BigIntArithmetics(0);
285
+ expect(num.toCurrency()).toContain("$");
286
+ expect(num.toCurrency()).toContain("0");
287
+ });
288
+
289
+ test("handles custom decimal places", () => {
290
+ const num = new BigIntArithmetics(1234.56789);
291
+ const result = num.toCurrency("$", { decimal: 4 });
292
+ expect(result).toContain("$");
293
+ expect(result).toContain("1");
294
+ });
295
+ });
296
+
297
+ describe("comparison methods with edge cases", () => {
298
+ test("handles comparison with different decimal precisions", () => {
299
+ const num1 = new BigIntArithmetics({ decimal: 8, value: 1.00000001 });
300
+ const num2 = new BigIntArithmetics({ decimal: 2, value: 1.0 });
301
+
302
+ expect(num1.gt(num2)).toBe(true);
303
+ expect(num2.lt(num1)).toBe(true);
304
+ });
305
+
306
+ test("handles zero comparisons", () => {
307
+ const num = new BigIntArithmetics(0);
308
+
309
+ expect(num.gt(0)).toBe(false);
310
+ expect(num.lt(0)).toBe(false);
311
+ expect(num.eqValue(0)).toBe(true);
312
+ expect(num.gte(0)).toBe(true);
313
+ expect(num.lte(0)).toBe(true);
314
+ });
315
+
316
+ test("handles negative number comparisons", () => {
317
+ const num1 = new BigIntArithmetics(-10);
318
+ const num2 = new BigIntArithmetics(-5);
319
+
320
+ expect(num1.lt(num2)).toBe(true);
321
+ expect(num2.gt(num1)).toBe(true);
322
+ });
323
+ });
324
+
325
+ describe("set method", () => {
326
+ test("creates new instance with updated value", () => {
327
+ const num1 = new BigIntArithmetics({ decimal: 8, value: 100 });
328
+ const num2 = num1.set(200);
329
+
330
+ expect(num1.getValue("number")).toBe(100);
331
+ expect(num2.getValue("number")).toBe(200);
332
+ expect(num2.decimal).toBe(8);
333
+ });
334
+
335
+ test("preserves decimal in set", () => {
336
+ const num1 = new BigIntArithmetics({ decimal: 6, value: 50 });
337
+ const num2 = num1.set(75);
338
+
339
+ expect(num2.decimal).toBe(6);
340
+ });
341
+ });
342
+
343
+ describe("toSignificant edge cases", () => {
344
+ test("handles very small numbers with leading zeros", () => {
345
+ const num = new BigIntArithmetics(0.000000123456);
346
+ expect(num.toSignificant(3)).toBe("0.000000123");
347
+ expect(num.toSignificant(6)).toBe("0.000000123456");
348
+ });
349
+
350
+ test("handles numbers with all significant digits being zero", () => {
351
+ const num = new BigIntArithmetics(0.0);
352
+ expect(num.toSignificant(4)).toBe("0");
353
+ });
354
+
355
+ test("handles integers with toSignificant", () => {
356
+ const num = new BigIntArithmetics(123000);
357
+ expect(num.toSignificant(2)).toBe("120000");
358
+ expect(num.toSignificant(3)).toBe("123000");
359
+ });
360
+ });
361
+
362
+ describe("getBigIntValue", () => {
363
+ test("handles BigIntArithmetics instance without decimal", () => {
364
+ const num1 = new BigIntArithmetics(100);
365
+ const num2 = new BigIntArithmetics(50);
366
+
367
+ const bigIntValue = num1.getBigIntValue(num2);
368
+ expect(typeof bigIntValue).toBe("bigint");
369
+ });
370
+
371
+ test("handles string '0' correctly", () => {
372
+ const num = new BigIntArithmetics(100);
373
+ const result = num.getBigIntValue("0");
374
+ expect(result).toBe(0n);
375
+ });
376
+
377
+ test("handles undefined string correctly", () => {
378
+ const num = new BigIntArithmetics(100);
379
+ const result = num.getBigIntValue("undefined");
380
+ expect(result).toBe(0n);
381
+ });
382
+ });
383
+ });