@tcswap/helpers 4.5.15
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/api/index.cjs +4 -0
- package/dist/api/index.cjs.map +16 -0
- package/dist/api/index.js +4 -0
- package/dist/api/index.js.map +16 -0
- package/dist/chunk-pfmeq01a.js +5 -0
- package/dist/chunk-pfmeq01a.js.map +9 -0
- package/dist/chunk-vb4wtm2w.js +4 -0
- package/dist/chunk-vb4wtm2w.js.map +9 -0
- package/dist/contracts.cjs +4 -0
- package/dist/contracts.cjs.map +10 -0
- package/dist/contracts.js +4 -0
- package/dist/contracts.js.map +10 -0
- package/dist/index.cjs +7 -0
- package/dist/index.cjs.map +30 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +30 -0
- package/dist/tokens.cjs +4 -0
- package/dist/tokens.cjs.map +10 -0
- package/dist/tokens.js +4 -0
- package/dist/tokens.js.map +10 -0
- package/dist/types/api/index.d.ts +502 -0
- package/dist/types/api/index.d.ts.map +1 -0
- package/dist/types/api/memoless/endpoints.d.ts +56 -0
- package/dist/types/api/memoless/endpoints.d.ts.map +1 -0
- package/dist/types/api/memoless/types.d.ts +85 -0
- package/dist/types/api/memoless/types.d.ts.map +1 -0
- package/dist/types/api/midgard/endpoints.d.ts +80 -0
- package/dist/types/api/midgard/endpoints.d.ts.map +1 -0
- package/dist/types/api/midgard/types.d.ts +543 -0
- package/dist/types/api/midgard/types.d.ts.map +1 -0
- package/dist/types/api/thornode/endpoints.d.ts +34 -0
- package/dist/types/api/thornode/endpoints.d.ts.map +1 -0
- package/dist/types/api/thornode/types.d.ts +264 -0
- package/dist/types/api/thornode/types.d.ts.map +1 -0
- package/dist/types/api/uswap/endpoints.d.ts +372 -0
- package/dist/types/api/uswap/endpoints.d.ts.map +1 -0
- package/dist/types/api/uswap/types.d.ts +1487 -0
- package/dist/types/api/uswap/types.d.ts.map +1 -0
- package/dist/types/contracts.d.ts +2 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/index.d.ts +32 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/modules/assetValue.d.ts +82 -0
- package/dist/types/modules/assetValue.d.ts.map +1 -0
- package/dist/types/modules/bigIntArithmetics.d.ts +60 -0
- package/dist/types/modules/bigIntArithmetics.d.ts.map +1 -0
- package/dist/types/modules/feeMultiplier.d.ts +48 -0
- package/dist/types/modules/feeMultiplier.d.ts.map +1 -0
- package/dist/types/modules/requestClient.d.ts +33 -0
- package/dist/types/modules/requestClient.d.ts.map +1 -0
- package/dist/types/modules/uSwapConfig.d.ts +249 -0
- package/dist/types/modules/uSwapConfig.d.ts.map +1 -0
- package/dist/types/modules/uSwapError.d.ts +879 -0
- package/dist/types/modules/uSwapError.d.ts.map +1 -0
- package/dist/types/modules/uSwapNumber.d.ts +10 -0
- package/dist/types/modules/uSwapNumber.d.ts.map +1 -0
- package/dist/types/tokens.d.ts +2 -0
- package/dist/types/tokens.d.ts.map +1 -0
- package/dist/types/types/commonTypes.d.ts +16 -0
- package/dist/types/types/commonTypes.d.ts.map +1 -0
- package/dist/types/types/derivationPath.d.ts +4 -0
- package/dist/types/types/derivationPath.d.ts.map +1 -0
- package/dist/types/types/errors/apiV1.d.ts +2 -0
- package/dist/types/types/errors/apiV1.d.ts.map +1 -0
- package/dist/types/types/index.d.ts +6 -0
- package/dist/types/types/index.d.ts.map +1 -0
- package/dist/types/types/quotes.d.ts +180 -0
- package/dist/types/types/quotes.d.ts.map +1 -0
- package/dist/types/types/sdk.d.ts +35 -0
- package/dist/types/types/sdk.d.ts.map +1 -0
- package/dist/types/types/wallet.d.ts +130 -0
- package/dist/types/types/wallet.d.ts.map +1 -0
- package/dist/types/utils/asset.d.ts +37 -0
- package/dist/types/utils/asset.d.ts.map +1 -0
- package/dist/types/utils/chains.d.ts +13 -0
- package/dist/types/utils/chains.d.ts.map +1 -0
- package/dist/types/utils/derivationPath.d.ts +21 -0
- package/dist/types/utils/derivationPath.d.ts.map +1 -0
- package/dist/types/utils/explorerUrls.d.ts +10 -0
- package/dist/types/utils/explorerUrls.d.ts.map +1 -0
- package/dist/types/utils/liquidity.d.ts +62 -0
- package/dist/types/utils/liquidity.d.ts.map +1 -0
- package/dist/types/utils/memo.d.ts +65 -0
- package/dist/types/utils/memo.d.ts.map +1 -0
- package/dist/types/utils/others.d.ts +15 -0
- package/dist/types/utils/others.d.ts.map +1 -0
- package/dist/types/utils/validators.d.ts +6 -0
- package/dist/types/utils/validators.d.ts.map +1 -0
- package/dist/types/utils/wallets.d.ts +36 -0
- package/dist/types/utils/wallets.d.ts.map +1 -0
- package/package.json +67 -0
- package/src/api/index.ts +15 -0
- package/src/api/memoless/endpoints.ts +62 -0
- package/src/api/memoless/types.ts +83 -0
- package/src/api/midgard/endpoints.ts +352 -0
- package/src/api/midgard/types.ts +515 -0
- package/src/api/thornode/endpoints.ts +109 -0
- package/src/api/thornode/types.ts +247 -0
- package/src/api/uswap/endpoints.ts +252 -0
- package/src/api/uswap/types.ts +626 -0
- package/src/contracts.ts +1 -0
- package/src/index.ts +32 -0
- package/src/modules/__tests__/assetValue.test.ts +2452 -0
- package/src/modules/__tests__/bigIntArithmetics.test.ts +410 -0
- package/src/modules/__tests__/feeMultiplier.test.ts +131 -0
- package/src/modules/__tests__/uSwapConfig.test.ts +429 -0
- package/src/modules/__tests__/uSwapNumber.test.ts +439 -0
- package/src/modules/assetValue.ts +536 -0
- package/src/modules/bigIntArithmetics.ts +366 -0
- package/src/modules/feeMultiplier.ts +84 -0
- package/src/modules/requestClient.ts +116 -0
- package/src/modules/uSwapConfig.ts +189 -0
- package/src/modules/uSwapError.ts +474 -0
- package/src/modules/uSwapNumber.ts +17 -0
- package/src/tokens.ts +1 -0
- package/src/types/commonTypes.ts +10 -0
- package/src/types/derivationPath.ts +11 -0
- package/src/types/errors/apiV1.ts +0 -0
- package/src/types/index.ts +5 -0
- package/src/types/quotes.ts +182 -0
- package/src/types/sdk.ts +38 -0
- package/src/types/wallet.ts +124 -0
- package/src/utils/__tests__/asset.test.ts +186 -0
- package/src/utils/__tests__/derivationPath.test.ts +142 -0
- package/src/utils/__tests__/explorerUrls.test.ts +59 -0
- package/src/utils/__tests__/liquidity.test.ts +302 -0
- package/src/utils/__tests__/memo.test.ts +99 -0
- package/src/utils/__tests__/others.test.ts +169 -0
- package/src/utils/__tests__/validators.test.ts +84 -0
- package/src/utils/__tests__/wallets.test.ts +625 -0
- package/src/utils/asset.ts +399 -0
- package/src/utils/chains.ts +104 -0
- package/src/utils/derivationPath.ts +101 -0
- package/src/utils/explorerUrls.ts +32 -0
- package/src/utils/liquidity.ts +154 -0
- package/src/utils/memo.ts +102 -0
- package/src/utils/others.ts +64 -0
- package/src/utils/validators.ts +36 -0
- package/src/utils/wallets.ts +238 -0
|
@@ -0,0 +1,410 @@
|
|
|
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 with toFixed", () => {
|
|
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 with abbreviation", () => {
|
|
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("currency symbols €, £, ¥", () => {
|
|
264
|
+
const num = new BigIntArithmetics(1234.56);
|
|
265
|
+
expect(num.toCurrency("€")).toBe("€1,234.56");
|
|
266
|
+
expect(num.toCurrency("£")).toBe("£1,234.56");
|
|
267
|
+
expect(num.toCurrency("¥")).toBe("¥1,234.56");
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
test("currency position start and end", () => {
|
|
271
|
+
const num = new BigIntArithmetics(1234.56);
|
|
272
|
+
expect(num.toCurrency("€", { currencyPosition: "start" })).toBe("€1,234.56");
|
|
273
|
+
expect(num.toCurrency("€", { currencyPosition: "end" })).toBe("1,234.56€");
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
test("european format with custom separators", () => {
|
|
277
|
+
const num = new BigIntArithmetics(1234567.89);
|
|
278
|
+
expect(num.toCurrency("€", { currencyPosition: "end", decimalSeparator: ",", thousandSeparator: "." })).toBe(
|
|
279
|
+
"1.234.567,89€",
|
|
280
|
+
);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("small values < 1 rounds to decimal with trailing zeros removed without floating-point artifacts", () => {
|
|
284
|
+
expect(new BigIntArithmetics(0.015072).toCurrency("")).toBe("0.02");
|
|
285
|
+
expect(new BigIntArithmetics(0.333145).toCurrency("")).toBe("0.33");
|
|
286
|
+
expect(new BigIntArithmetics(0.000005).toCurrency("")).toBe("0");
|
|
287
|
+
expect(new BigIntArithmetics(0.00000548).toCurrency("", { decimal: 6 })).toBe("0.000005");
|
|
288
|
+
expect(new BigIntArithmetics(0.00003801).toCurrency("", { decimal: 6 })).toBe("0.000038");
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
test("small values strip trailing zeros", () => {
|
|
292
|
+
expect(new BigIntArithmetics(0.12).toCurrency("")).toBe("0.12");
|
|
293
|
+
expect(new BigIntArithmetics(0.1).toCurrency("")).toBe("0.1");
|
|
294
|
+
expect(new BigIntArithmetics(0.10000001).toCurrency("")).toBe("0.1");
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test("negative small values", () => {
|
|
298
|
+
expect(new BigIntArithmetics(-0.015072).toCurrency("")).toBe("-0.02");
|
|
299
|
+
expect(new BigIntArithmetics(-0.00000001).toCurrency("", { decimal: 8 })).toBe("-0.00000001");
|
|
300
|
+
expect(new BigIntArithmetics(-0.00003801).toCurrency("", { decimal: 6 })).toBe("-0.000038");
|
|
301
|
+
expect(new BigIntArithmetics(-0.00000548).toCurrency("")).toBe("0");
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("values >= 1 round to decimal param", () => {
|
|
305
|
+
expect(new BigIntArithmetics(5.12).toCurrency("", { decimal: 2 })).toBe("5.12");
|
|
306
|
+
expect(new BigIntArithmetics(80.865327).toCurrency("", { decimal: 2 })).toBe("80.87");
|
|
307
|
+
expect(new BigIntArithmetics(33.432207).toCurrency("", { decimal: 2 })).toBe("33.43");
|
|
308
|
+
expect(new BigIntArithmetics(999.999).toCurrency("$")).toBe("$1,000");
|
|
309
|
+
expect(new BigIntArithmetics(0.0000000000000000000000000000001).toCurrency("", { decimal: 8 })).toBe("0");
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
test("zero value", () => {
|
|
313
|
+
expect(new BigIntArithmetics(0).toCurrency("$")).toBe("$0");
|
|
314
|
+
expect(new BigIntArithmetics(0).toCurrency("")).toBe("0");
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("custom decimal separator for small values", () => {
|
|
318
|
+
expect(
|
|
319
|
+
new BigIntArithmetics(0.015072).toCurrency("€", { currencyPosition: "end", decimal: 6, decimalSeparator: "," }),
|
|
320
|
+
).toBe("0,015072€");
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
describe("comparison methods with edge cases", () => {
|
|
325
|
+
test("handles comparison with different decimal precisions", () => {
|
|
326
|
+
const num1 = new BigIntArithmetics({ decimal: 8, value: 1.00000001 });
|
|
327
|
+
const num2 = new BigIntArithmetics({ decimal: 2, value: 1.0 });
|
|
328
|
+
|
|
329
|
+
expect(num1.gt(num2)).toBe(true);
|
|
330
|
+
expect(num2.lt(num1)).toBe(true);
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
test("handles zero comparisons", () => {
|
|
334
|
+
const num = new BigIntArithmetics(0);
|
|
335
|
+
|
|
336
|
+
expect(num.gt(0)).toBe(false);
|
|
337
|
+
expect(num.lt(0)).toBe(false);
|
|
338
|
+
expect(num.eqValue(0)).toBe(true);
|
|
339
|
+
expect(num.gte(0)).toBe(true);
|
|
340
|
+
expect(num.lte(0)).toBe(true);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
test("handles negative number comparisons", () => {
|
|
344
|
+
const num1 = new BigIntArithmetics(-10);
|
|
345
|
+
const num2 = new BigIntArithmetics(-5);
|
|
346
|
+
|
|
347
|
+
expect(num1.lt(num2)).toBe(true);
|
|
348
|
+
expect(num2.gt(num1)).toBe(true);
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
describe("set method", () => {
|
|
353
|
+
test("creates new instance with updated value", () => {
|
|
354
|
+
const num1 = new BigIntArithmetics({ decimal: 8, value: 100 });
|
|
355
|
+
const num2 = num1.set(200);
|
|
356
|
+
|
|
357
|
+
expect(num1.getValue("number")).toBe(100);
|
|
358
|
+
expect(num2.getValue("number")).toBe(200);
|
|
359
|
+
expect(num2.decimal).toBe(8);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
test("preserves decimal in set", () => {
|
|
363
|
+
const num1 = new BigIntArithmetics({ decimal: 6, value: 50 });
|
|
364
|
+
const num2 = num1.set(75);
|
|
365
|
+
|
|
366
|
+
expect(num2.decimal).toBe(6);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
describe("toSignificant edge cases", () => {
|
|
371
|
+
test("handles very small numbers with leading zeros", () => {
|
|
372
|
+
const num = new BigIntArithmetics(0.000000123456);
|
|
373
|
+
expect(num.toSignificant(3)).toBe("0.000000123");
|
|
374
|
+
expect(num.toSignificant(6)).toBe("0.000000123456");
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
test("handles numbers with all significant digits being zero", () => {
|
|
378
|
+
const num = new BigIntArithmetics(0.0);
|
|
379
|
+
expect(num.toSignificant(4)).toBe("0");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
test("handles integers with toSignificant", () => {
|
|
383
|
+
const num = new BigIntArithmetics(123000);
|
|
384
|
+
expect(num.toSignificant(2)).toBe("120000");
|
|
385
|
+
expect(num.toSignificant(3)).toBe("123000");
|
|
386
|
+
});
|
|
387
|
+
});
|
|
388
|
+
|
|
389
|
+
describe("getBigIntValue", () => {
|
|
390
|
+
test("handles BigIntArithmetics instance without decimal", () => {
|
|
391
|
+
const num1 = new BigIntArithmetics(100);
|
|
392
|
+
const num2 = new BigIntArithmetics(50);
|
|
393
|
+
|
|
394
|
+
const bigIntValue = num1.getBigIntValue(num2);
|
|
395
|
+
expect(typeof bigIntValue).toBe("bigint");
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
test("handles string '0' correctly", () => {
|
|
399
|
+
const num = new BigIntArithmetics(100);
|
|
400
|
+
const result = num.getBigIntValue("0");
|
|
401
|
+
expect(result).toBe(0n);
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
test("handles undefined string correctly", () => {
|
|
405
|
+
const num = new BigIntArithmetics(100);
|
|
406
|
+
const result = num.getBigIntValue("undefined");
|
|
407
|
+
expect(result).toBe(0n);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
|
6
|
+
|
|
7
|
+
import { FeeOption } from "../../types";
|
|
8
|
+
import {
|
|
9
|
+
applyFeeMultiplier,
|
|
10
|
+
applyFeeMultiplierToBigInt,
|
|
11
|
+
DEFAULT_FEE_MULTIPLIERS,
|
|
12
|
+
getFeeMultiplier,
|
|
13
|
+
getFeeMultiplierAsBigInt,
|
|
14
|
+
} from "../feeMultiplier";
|
|
15
|
+
import { USwapConfig } from "../uSwapConfig";
|
|
16
|
+
|
|
17
|
+
describe("feeMultiplier", () => {
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
USwapConfig.reinitialize();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(() => {
|
|
23
|
+
USwapConfig.reinitialize();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("getFeeMultiplier", () => {
|
|
27
|
+
test("returns 1.0 for Average, 1.5 for Fast, 2.0 for Fastest", () => {
|
|
28
|
+
expect(getFeeMultiplier(FeeOption.Average)).toBe(1.0);
|
|
29
|
+
expect(getFeeMultiplier(FeeOption.Fast)).toBe(1.5);
|
|
30
|
+
expect(getFeeMultiplier(FeeOption.Fastest)).toBe(2.0);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
test("defaults to Average when no option provided", () => {
|
|
34
|
+
expect(getFeeMultiplier()).toBe(1.0);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("uses custom multipliers from USwapConfig when set", () => {
|
|
38
|
+
USwapConfig.set({
|
|
39
|
+
feeMultipliers: { [FeeOption.Average]: 1.2, [FeeOption.Fast]: 1.8, [FeeOption.Fastest]: 2.5 },
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(getFeeMultiplier(FeeOption.Average)).toBe(1.2);
|
|
43
|
+
expect(getFeeMultiplier(FeeOption.Fast)).toBe(1.8);
|
|
44
|
+
expect(getFeeMultiplier(FeeOption.Fastest)).toBe(2.5);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe("getFeeMultiplierAsBigInt", () => {
|
|
49
|
+
test("converts multipliers to numerator/denominator for precise bigint math", () => {
|
|
50
|
+
expect(getFeeMultiplierAsBigInt(FeeOption.Average)).toEqual({ denominator: 10n, numerator: 10n });
|
|
51
|
+
expect(getFeeMultiplierAsBigInt(FeeOption.Fast)).toEqual({ denominator: 10n, numerator: 15n });
|
|
52
|
+
expect(getFeeMultiplierAsBigInt(FeeOption.Fastest)).toEqual({ denominator: 10n, numerator: 20n });
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("defaults to Average for bigint multiplier", () => {
|
|
56
|
+
expect(getFeeMultiplierAsBigInt()).toEqual({ denominator: 10n, numerator: 10n });
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("applyFeeMultiplierToBigInt", () => {
|
|
61
|
+
test("applies multiplier to bigint values", () => {
|
|
62
|
+
expect(applyFeeMultiplierToBigInt(100n, FeeOption.Average)).toBe(100n);
|
|
63
|
+
expect(applyFeeMultiplierToBigInt(100n, FeeOption.Fast)).toBe(150n);
|
|
64
|
+
expect(applyFeeMultiplierToBigInt(100n, FeeOption.Fastest)).toBe(200n);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("handles large values without overflow", () => {
|
|
68
|
+
expect(applyFeeMultiplierToBigInt(1000000000000000000n, FeeOption.Fastest)).toBe(2000000000000000000n);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("defaults to Average for applyFeeMultiplierToBigInt", () => {
|
|
72
|
+
expect(applyFeeMultiplierToBigInt(100n)).toBe(100n);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe("applyFeeMultiplier", () => {
|
|
77
|
+
test("applies multiplier to number values", () => {
|
|
78
|
+
expect(applyFeeMultiplier(100, FeeOption.Average)).toBe(100);
|
|
79
|
+
expect(applyFeeMultiplier(100, FeeOption.Fast)).toBe(150);
|
|
80
|
+
expect(applyFeeMultiplier(100, FeeOption.Fastest)).toBe(200);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test("floors result when floor=true", () => {
|
|
84
|
+
expect(applyFeeMultiplier(33, FeeOption.Fast, true)).toBe(49);
|
|
85
|
+
expect(applyFeeMultiplier(10.5, FeeOption.Fastest, true)).toBe(21);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("preserves decimals when floor=false", () => {
|
|
89
|
+
expect(applyFeeMultiplier(33, FeeOption.Fast, false)).toBe(49.5);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("defaults to Average for applyFeeMultiplier", () => {
|
|
93
|
+
expect(applyFeeMultiplier(100)).toBe(100);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("DEFAULT_FEE_MULTIPLIERS", () => {
|
|
98
|
+
test("exports correct default values", () => {
|
|
99
|
+
expect(DEFAULT_FEE_MULTIPLIERS[FeeOption.Average]).toBe(1.0);
|
|
100
|
+
expect(DEFAULT_FEE_MULTIPLIERS[FeeOption.Fast]).toBe(1.5);
|
|
101
|
+
expect(DEFAULT_FEE_MULTIPLIERS[FeeOption.Fastest]).toBe(2.0);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("edge cases", () => {
|
|
106
|
+
test("applies multiplier to zero value", () => {
|
|
107
|
+
expect(applyFeeMultiplierToBigInt(0n, FeeOption.Fastest)).toBe(0n);
|
|
108
|
+
expect(applyFeeMultiplier(0, FeeOption.Fastest)).toBe(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("handles negative numbers", () => {
|
|
112
|
+
expect(applyFeeMultiplier(-100, FeeOption.Fast)).toBe(-150);
|
|
113
|
+
expect(applyFeeMultiplier(-100, FeeOption.Fastest)).toBe(-200);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("handles negative bigint values", () => {
|
|
117
|
+
expect(applyFeeMultiplierToBigInt(-100n, FeeOption.Fast)).toBe(-150n);
|
|
118
|
+
expect(applyFeeMultiplierToBigInt(-100n, FeeOption.Fastest)).toBe(-200n);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test("handles decimal values with multiplier", () => {
|
|
122
|
+
expect(applyFeeMultiplier(0.5, FeeOption.Fastest)).toBe(1);
|
|
123
|
+
expect(applyFeeMultiplier(0.333, FeeOption.Fast)).toBeCloseTo(0.4995, 4);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("handles very small bigint values", () => {
|
|
127
|
+
expect(applyFeeMultiplierToBigInt(1n, FeeOption.Fast)).toBe(1n); // 1 * 15 / 10 = 1 (integer division)
|
|
128
|
+
expect(applyFeeMultiplierToBigInt(5n, FeeOption.Fast)).toBe(7n); // 5 * 15 / 10 = 7
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
});
|