@mezo-org/passport 0.5.2-dev.7 → 0.5.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/src/hooks/useTokensBalances.d.ts.map +1 -1
- package/dist/src/hooks/useTokensBalances.js +9 -8
- package/dist/src/hooks/useTokensBalances.js.map +1 -1
- package/dist/src/utils/number2.d.ts +106 -0
- package/dist/src/utils/number2.d.ts.map +1 -0
- package/dist/src/utils/number2.js +289 -0
- package/dist/src/utils/number2.js.map +1 -0
- package/dist/src/utils/number2.test.d.ts +2 -0
- package/dist/src/utils/number2.test.d.ts.map +1 -0
- package/dist/src/utils/number2.test.js +223 -0
- package/dist/src/utils/number2.test.js.map +1 -0
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useTokensBalances.ts +9 -8
- package/src/utils/number2.test.ts +309 -0
- package/src/utils/number2.ts +419 -0
- package/src/utils/numbers.test.ts +1 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { bigIntToFloat, bigIntToHumanReadableFormat, floatToBigInt, floatToHumanReadableFormat, multiplyBigIntWithDecimal, normalizeDecimalNumber, trimBeforeDecimals, trimDecimals, } from "./number2";
|
|
2
|
+
describe("floatToBigInt", () => {
|
|
3
|
+
it("should convert integer string to bigint with default decimals", () => {
|
|
4
|
+
expect(floatToBigInt({ amount: "1" })).toBe(1000000000000000000n);
|
|
5
|
+
});
|
|
6
|
+
it("should convert float string to bigint with default decimals", () => {
|
|
7
|
+
expect(floatToBigInt({ amount: "1.23" })).toBe(1230000000000000000n);
|
|
8
|
+
});
|
|
9
|
+
it("should convert float string to bigint with custom decimals", () => {
|
|
10
|
+
expect(floatToBigInt({ amount: "1.23", decimals: 2 })).toBe(123n);
|
|
11
|
+
expect(floatToBigInt({ amount: "1.2345", decimals: 2 })).toBe(123n);
|
|
12
|
+
expect(floatToBigInt({ amount: "1.2345", decimals: 6 })).toBe(1234500n);
|
|
13
|
+
});
|
|
14
|
+
it("should handle numbers as input", () => {
|
|
15
|
+
expect(floatToBigInt({ amount: 2.5 })).toBe(2500000000000000000n);
|
|
16
|
+
});
|
|
17
|
+
it("should handle zero", () => {
|
|
18
|
+
expect(floatToBigInt({ amount: "0" })).toBe(0n);
|
|
19
|
+
expect(floatToBigInt({ amount: 0 })).toBe(0n);
|
|
20
|
+
});
|
|
21
|
+
it("should handle negative numbers", () => {
|
|
22
|
+
expect(floatToBigInt({ amount: "-1.5" })).toBe(-1500000000000000000n);
|
|
23
|
+
});
|
|
24
|
+
it("should handle numbers with thousands separator", () => {
|
|
25
|
+
expect(floatToBigInt({ amount: "1,234.56" })).toBe(1234560000000000000000n);
|
|
26
|
+
});
|
|
27
|
+
it("should return 0n for invalid input", () => {
|
|
28
|
+
expect(floatToBigInt({ amount: "abc" })).toBe(0n);
|
|
29
|
+
expect(floatToBigInt({ amount: "1.2.3" })).toBe(0n);
|
|
30
|
+
expect(floatToBigInt({ amount: "" })).toBe(0n);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe("bigIntToFloat", () => {
|
|
34
|
+
it("should convert bigint to float string with default decimals", () => {
|
|
35
|
+
expect(bigIntToFloat({ amount: 1000000000000000000n })).toBe("1");
|
|
36
|
+
expect(bigIntToFloat({ amount: 1230000000000000000n })).toBe("1.23");
|
|
37
|
+
});
|
|
38
|
+
it("should convert bigint to float string with custom decimals", () => {
|
|
39
|
+
expect(bigIntToFloat({ amount: 123n, decimals: 2 })).toBe("1.23");
|
|
40
|
+
expect(bigIntToFloat({ amount: 1234500n, decimals: 6 })).toBe("1.2345");
|
|
41
|
+
});
|
|
42
|
+
it("should handle desiredDecimals less than decimals", () => {
|
|
43
|
+
expect(bigIntToFloat({
|
|
44
|
+
amount: 1234567890000000000n,
|
|
45
|
+
decimals: 18,
|
|
46
|
+
desiredDecimals: 2,
|
|
47
|
+
})).toBe("1.23");
|
|
48
|
+
expect(bigIntToFloat({
|
|
49
|
+
amount: 1234567890000000000n,
|
|
50
|
+
decimals: 18,
|
|
51
|
+
desiredDecimals: 6,
|
|
52
|
+
})).toBe("1.234568");
|
|
53
|
+
});
|
|
54
|
+
it("should handle zero", () => {
|
|
55
|
+
expect(bigIntToFloat({ amount: 0n })).toBe("0");
|
|
56
|
+
});
|
|
57
|
+
it("should handle negative numbers", () => {
|
|
58
|
+
expect(bigIntToFloat({ amount: -1500000000000000000n })).toBe("-1.5");
|
|
59
|
+
});
|
|
60
|
+
it("should handle rounding up", () => {
|
|
61
|
+
expect(bigIntToFloat({
|
|
62
|
+
amount: 1249999999999999999n,
|
|
63
|
+
decimals: 18,
|
|
64
|
+
desiredDecimals: 2,
|
|
65
|
+
})).toBe("1.25");
|
|
66
|
+
expect(bigIntToFloat({
|
|
67
|
+
amount: 1234567890000000000n,
|
|
68
|
+
decimals: 18,
|
|
69
|
+
desiredDecimals: 4,
|
|
70
|
+
})).toBe("1.2346");
|
|
71
|
+
});
|
|
72
|
+
it("should handle large numbers", () => {
|
|
73
|
+
expect(bigIntToFloat({
|
|
74
|
+
amount: 123456789012345678901234567890n,
|
|
75
|
+
decimals: 18,
|
|
76
|
+
desiredDecimals: 2,
|
|
77
|
+
})).toBe("123456789012.35");
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe("floatToHumanReadableFormat", () => {
|
|
81
|
+
it("should format number with thousands separator and default decimals", () => {
|
|
82
|
+
expect(floatToHumanReadableFormat({ amount: 1234567.891 })).toBe("1,234,567.89");
|
|
83
|
+
expect(floatToHumanReadableFormat({ amount: "1234567.891" })).toBe("1,234,567.89");
|
|
84
|
+
});
|
|
85
|
+
it("should format with custom decimals", () => {
|
|
86
|
+
expect(floatToHumanReadableFormat({ amount: 1234567.891, desiredDecimals: 3 })).toBe("1,234,567.891");
|
|
87
|
+
expect(floatToHumanReadableFormat({ amount: 1234567.8, desiredDecimals: 0 })).toBe("1,234,568");
|
|
88
|
+
});
|
|
89
|
+
it("should format with minDecimals", () => {
|
|
90
|
+
expect(floatToHumanReadableFormat({
|
|
91
|
+
amount: 1234.5,
|
|
92
|
+
desiredDecimals: 2,
|
|
93
|
+
minDecimals: 1,
|
|
94
|
+
})).toBe("1,234.5");
|
|
95
|
+
expect(floatToHumanReadableFormat({
|
|
96
|
+
amount: 1234.5,
|
|
97
|
+
desiredDecimals: 2,
|
|
98
|
+
minDecimals: 2,
|
|
99
|
+
})).toBe("1,234.50");
|
|
100
|
+
expect(floatToHumanReadableFormat({
|
|
101
|
+
amount: 1234.5,
|
|
102
|
+
desiredDecimals: 2,
|
|
103
|
+
minDecimals: 4,
|
|
104
|
+
})).toBe("1,234.5000");
|
|
105
|
+
});
|
|
106
|
+
it("should return '0' for invalid input", () => {
|
|
107
|
+
expect(floatToHumanReadableFormat({ amount: "abc" })).toBe("0");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
describe("bigIntToHumanReadableFormat", () => {
|
|
111
|
+
it("should format bigint to human readable string", () => {
|
|
112
|
+
expect(bigIntToHumanReadableFormat({
|
|
113
|
+
amount: 1234567890000000000n,
|
|
114
|
+
decimals: 18,
|
|
115
|
+
desiredDecimals: 2,
|
|
116
|
+
})).toBe("1.23");
|
|
117
|
+
expect(bigIntToHumanReadableFormat({
|
|
118
|
+
amount: 1234567890000000000n,
|
|
119
|
+
decimals: 18,
|
|
120
|
+
desiredDecimals: 6,
|
|
121
|
+
})).toBe("1.234568");
|
|
122
|
+
});
|
|
123
|
+
it("should handle negative values", () => {
|
|
124
|
+
expect(bigIntToHumanReadableFormat({
|
|
125
|
+
amount: -1234567890000000000n,
|
|
126
|
+
decimals: 18,
|
|
127
|
+
desiredDecimals: 2,
|
|
128
|
+
})).toBe("-1.23");
|
|
129
|
+
});
|
|
130
|
+
it("should show <0.01 for very small nonzero values", () => {
|
|
131
|
+
expect(bigIntToHumanReadableFormat({
|
|
132
|
+
amount: 1n,
|
|
133
|
+
decimals: 18,
|
|
134
|
+
desiredDecimals: 2,
|
|
135
|
+
})).toBe("<0.01");
|
|
136
|
+
expect(bigIntToHumanReadableFormat({
|
|
137
|
+
amount: -1n,
|
|
138
|
+
decimals: 18,
|
|
139
|
+
desiredDecimals: 2,
|
|
140
|
+
})).toBe("<0.01");
|
|
141
|
+
});
|
|
142
|
+
it("should format with minDecimals", () => {
|
|
143
|
+
expect(bigIntToHumanReadableFormat({
|
|
144
|
+
amount: 1234000000000000000n,
|
|
145
|
+
decimals: 18,
|
|
146
|
+
desiredDecimals: 2,
|
|
147
|
+
minDecimals: 3,
|
|
148
|
+
})).toBe("1.234");
|
|
149
|
+
expect(bigIntToHumanReadableFormat({
|
|
150
|
+
amount: 1234000000000000000n,
|
|
151
|
+
decimals: 18,
|
|
152
|
+
desiredDecimals: 3,
|
|
153
|
+
minDecimals: 2,
|
|
154
|
+
})).toBe("1.234");
|
|
155
|
+
expect(bigIntToHumanReadableFormat({
|
|
156
|
+
amount: 1234000000000000000n,
|
|
157
|
+
decimals: 18,
|
|
158
|
+
desiredDecimals: 2,
|
|
159
|
+
minDecimals: 4,
|
|
160
|
+
})).toBe("1.2340");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
describe("multiplyBigIntWithDecimal", () => {
|
|
164
|
+
it("should multiply bigint by integer", () => {
|
|
165
|
+
expect(multiplyBigIntWithDecimal(10n, 2)).toBe(20n);
|
|
166
|
+
});
|
|
167
|
+
it("should multiply bigint by decimal", () => {
|
|
168
|
+
expect(multiplyBigIntWithDecimal(100n, 0.5)).toBe(50n);
|
|
169
|
+
expect(multiplyBigIntWithDecimal(100n, 1.25)).toBe(125n);
|
|
170
|
+
});
|
|
171
|
+
it("should handle scientific notation", () => {
|
|
172
|
+
expect(multiplyBigIntWithDecimal(100n, 1e-2)).toBe(1n);
|
|
173
|
+
});
|
|
174
|
+
it("should handle zero", () => {
|
|
175
|
+
expect(multiplyBigIntWithDecimal(0n, 1.5)).toBe(0n);
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
describe("trimDecimals", () => {
|
|
179
|
+
it("should trim decimals to specified precision", () => {
|
|
180
|
+
expect(trimDecimals("1.23456", 2)).toBe("1.23");
|
|
181
|
+
expect(trimDecimals("1.2", 4)).toBe("1.2");
|
|
182
|
+
expect(trimDecimals("1", 2)).toBe("1");
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
describe("trimBeforeDecimals", () => {
|
|
186
|
+
it("should trim integer part to specified length", () => {
|
|
187
|
+
expect(trimBeforeDecimals("12345.67", 3)).toBe("123.67");
|
|
188
|
+
expect(trimBeforeDecimals("12.34", 5)).toBe("12.34");
|
|
189
|
+
expect(trimBeforeDecimals("123456", 2)).toBe("12.");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
describe("normalizeDecimalNumber", () => {
|
|
193
|
+
it("should remove invalid characters", () => {
|
|
194
|
+
expect(normalizeDecimalNumber("12a3.4b5")).toBe("123.45");
|
|
195
|
+
});
|
|
196
|
+
it("should prevent multiple dots", () => {
|
|
197
|
+
expect(normalizeDecimalNumber("1.2.3")).toBe("12.3");
|
|
198
|
+
expect(normalizeDecimalNumber("1..1.2")).toBe("11.2");
|
|
199
|
+
});
|
|
200
|
+
it("should allow only digits and one dot", () => {
|
|
201
|
+
expect(normalizeDecimalNumber("abc")).toBe("");
|
|
202
|
+
expect(normalizeDecimalNumber("123")).toBe("123");
|
|
203
|
+
});
|
|
204
|
+
it("should handle leading and trailing spaces", () => {
|
|
205
|
+
expect(normalizeDecimalNumber(" 1.23 ")).toBe("1.23");
|
|
206
|
+
expect(normalizeDecimalNumber(" 1.2.3 ")).toBe("12.3");
|
|
207
|
+
});
|
|
208
|
+
it("should handle empty input", () => {
|
|
209
|
+
expect(normalizeDecimalNumber("")).toBe("");
|
|
210
|
+
expect(normalizeDecimalNumber(" ")).toBe("");
|
|
211
|
+
});
|
|
212
|
+
it("should handle numbers with thousands separator", () => {
|
|
213
|
+
expect(normalizeDecimalNumber("1,234.56")).toBe("1234.56");
|
|
214
|
+
expect(normalizeDecimalNumber("1,234,567.89")).toBe("1234567.89");
|
|
215
|
+
});
|
|
216
|
+
it("should handle scientific notation", () => {
|
|
217
|
+
// This function does not support scientific notation as our input is expected
|
|
218
|
+
// to accept only decimal numbers
|
|
219
|
+
expect(normalizeDecimalNumber("1e3")).toBe("13");
|
|
220
|
+
expect(normalizeDecimalNumber("1.23e2")).toBe("1.232");
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
//# sourceMappingURL=number2.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number2.test.js","sourceRoot":"","sources":["../../../src/utils/number2.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,2BAA2B,EAC3B,aAAa,EACb,0BAA0B,EAC1B,yBAAyB,EACzB,sBAAsB,EACtB,kBAAkB,EAClB,YAAY,GACb,MAAM,WAAW,CAAA;AAElB,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;IAC7B,EAAE,CAAC,+DAA+D,EAAE,GAAG,EAAE;QACvE,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,oBAA0B,CAAC,CAAA;IACzE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,6DAA6D,EAAE,GAAG,EAAE;QACrE,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,oBAA0B,CAAC,CAAA;IAC5E,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,4DAA4D,EAAE,GAAG,EAAE;QACpE,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACjE,MAAM,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI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KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAChD,MAAM,CAAC,sBAAsB,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;IACxD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"AACA,OAAO,EACL,2BAA2B,EAC3B,SAAS,EACT,kBAAkB,GACnB,MAAM,WAAW,CAAA;AAElB,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACnE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,0BAA0B,EAAE,GAAG,EAAE;QAClC,MAAM,CAAC,2BAA2B,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC/D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACjC,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACnE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACjC,MAAM,CAAC,2BAA2B,CAAC,cAAc,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACvE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,0BAA0B,EAAE,GAAG,EAAE;QAClC,M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,MAAM,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,8BAA8B,EAAE,GAAG,EAAE;QACtC,MAAM,MAAM,GAAG,kBAAkB,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC,CAAA;QAC3C,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;IACzB,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACtD,MAAM,KAAK,GAAG,+BAA+B,CAAA;QAC7C,MAAM,MAAM,GAAG,kBAAkB,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,CAAA;QAChD,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useConfig } from "wagmi"
|
|
2
2
|
import { Abi, formatUnits } from "viem"
|
|
3
3
|
import {
|
|
4
4
|
useQuery,
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
MezoChainToken,
|
|
12
12
|
testnetTokenContracts,
|
|
13
13
|
} from "../lib/contracts"
|
|
14
|
+
import useWalletAccount from "./useWalletAccount"
|
|
14
15
|
import {
|
|
15
16
|
getAsset,
|
|
16
17
|
isBitcoinLikeCryptoAsset,
|
|
@@ -107,7 +108,7 @@ export function useTokensBalances<T extends MezoChainToken[]>(
|
|
|
107
108
|
) {
|
|
108
109
|
const { tokens = BALANCE_TOKENS, ...restQueryOptions } = options
|
|
109
110
|
|
|
110
|
-
const
|
|
111
|
+
const walletAccount = useWalletAccount()
|
|
111
112
|
|
|
112
113
|
const config = useConfig()
|
|
113
114
|
|
|
@@ -120,18 +121,18 @@ export function useTokensBalances<T extends MezoChainToken[]>(
|
|
|
120
121
|
return useQuery({
|
|
121
122
|
queryKey: [
|
|
122
123
|
TOKEN_BALANCES_QUERY_KEY,
|
|
123
|
-
accountAddress,
|
|
124
|
+
walletAccount?.accountAddress,
|
|
124
125
|
options.tokens,
|
|
125
126
|
chainId,
|
|
126
127
|
],
|
|
127
|
-
enabled: !!accountAddress && !!conversionRatesData,
|
|
128
|
+
enabled: !!walletAccount?.accountAddress && !!conversionRatesData,
|
|
128
129
|
queryFn: async () => {
|
|
129
130
|
const isMainnet = chainId === CHAIN_ID.mainnet
|
|
130
131
|
const contractsMap = isMainnet
|
|
131
132
|
? mainnetTokenContracts
|
|
132
133
|
: testnetTokenContracts
|
|
133
134
|
|
|
134
|
-
if (!accountAddress) {
|
|
135
|
+
if (!walletAccount?.accountAddress) {
|
|
135
136
|
throw new Error("Account address is not available.")
|
|
136
137
|
}
|
|
137
138
|
|
|
@@ -141,13 +142,13 @@ export function useTokensBalances<T extends MezoChainToken[]>(
|
|
|
141
142
|
address,
|
|
142
143
|
abi: BALANCE_OF_ABI,
|
|
143
144
|
functionName: "balanceOf" as const,
|
|
144
|
-
args: [accountAddress],
|
|
145
|
+
args: [walletAccount?.accountAddress],
|
|
145
146
|
chainId,
|
|
146
147
|
}
|
|
147
148
|
})
|
|
148
149
|
|
|
149
150
|
return Promise.all([
|
|
150
|
-
getBalance(config, { address: accountAddress, chainId }),
|
|
151
|
+
getBalance(config, { address: walletAccount?.accountAddress, chainId }),
|
|
151
152
|
readContracts(config, {
|
|
152
153
|
contracts: tokenContracts,
|
|
153
154
|
}),
|
|
@@ -176,7 +177,7 @@ export function useTokensBalances<T extends MezoChainToken[]>(
|
|
|
176
177
|
|
|
177
178
|
if (item.status === "failure") {
|
|
178
179
|
throw new Error(
|
|
179
|
-
`Failed to fetch balance of ${token} for ${accountAddress}.`,
|
|
180
|
+
`Failed to fetch balance of ${token} for ${walletAccount?.accountAddress}.`,
|
|
180
181
|
)
|
|
181
182
|
}
|
|
182
183
|
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import {
|
|
2
|
+
bigIntToFloat,
|
|
3
|
+
bigIntToHumanReadableFormat,
|
|
4
|
+
floatToBigInt,
|
|
5
|
+
floatToHumanReadableFormat,
|
|
6
|
+
multiplyBigIntWithDecimal,
|
|
7
|
+
normalizeDecimalNumber,
|
|
8
|
+
trimBeforeDecimals,
|
|
9
|
+
trimDecimals,
|
|
10
|
+
} from "./number2"
|
|
11
|
+
|
|
12
|
+
describe("floatToBigInt", () => {
|
|
13
|
+
it("should convert integer string to bigint with default decimals", () => {
|
|
14
|
+
expect(floatToBigInt({ amount: "1" })).toBe(1_000_000_000_000_000_000n)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it("should convert float string to bigint with default decimals", () => {
|
|
18
|
+
expect(floatToBigInt({ amount: "1.23" })).toBe(1_230_000_000_000_000_000n)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("should convert float string to bigint with custom decimals", () => {
|
|
22
|
+
expect(floatToBigInt({ amount: "1.23", decimals: 2 })).toBe(123n)
|
|
23
|
+
expect(floatToBigInt({ amount: "1.2345", decimals: 2 })).toBe(123n)
|
|
24
|
+
expect(floatToBigInt({ amount: "1.2345", decimals: 6 })).toBe(1234500n)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it("should handle numbers as input", () => {
|
|
28
|
+
expect(floatToBigInt({ amount: 2.5 })).toBe(2_500_000_000_000_000_000n)
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it("should handle zero", () => {
|
|
32
|
+
expect(floatToBigInt({ amount: "0" })).toBe(0n)
|
|
33
|
+
expect(floatToBigInt({ amount: 0 })).toBe(0n)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("should handle negative numbers", () => {
|
|
37
|
+
expect(floatToBigInt({ amount: "-1.5" })).toBe(-1_500_000_000_000_000_000n)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("should handle numbers with thousands separator", () => {
|
|
41
|
+
expect(floatToBigInt({ amount: "1,234.56" })).toBe(
|
|
42
|
+
1_234_560_000_000_000_000_000n,
|
|
43
|
+
)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it("should return 0n for invalid input", () => {
|
|
47
|
+
expect(floatToBigInt({ amount: "abc" })).toBe(0n)
|
|
48
|
+
expect(floatToBigInt({ amount: "1.2.3" })).toBe(0n)
|
|
49
|
+
expect(floatToBigInt({ amount: "" })).toBe(0n)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe("bigIntToFloat", () => {
|
|
54
|
+
it("should convert bigint to float string with default decimals", () => {
|
|
55
|
+
expect(bigIntToFloat({ amount: 1_000_000_000_000_000_000n })).toBe("1")
|
|
56
|
+
expect(bigIntToFloat({ amount: 1_230_000_000_000_000_000n })).toBe("1.23")
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("should convert bigint to float string with custom decimals", () => {
|
|
60
|
+
expect(bigIntToFloat({ amount: 123n, decimals: 2 })).toBe("1.23")
|
|
61
|
+
expect(bigIntToFloat({ amount: 1234500n, decimals: 6 })).toBe("1.2345")
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("should handle desiredDecimals less than decimals", () => {
|
|
65
|
+
expect(
|
|
66
|
+
bigIntToFloat({
|
|
67
|
+
amount: 1_234_567_890_000_000_000n,
|
|
68
|
+
decimals: 18,
|
|
69
|
+
desiredDecimals: 2,
|
|
70
|
+
}),
|
|
71
|
+
).toBe("1.23")
|
|
72
|
+
expect(
|
|
73
|
+
bigIntToFloat({
|
|
74
|
+
amount: 1_234_567_890_000_000_000n,
|
|
75
|
+
decimals: 18,
|
|
76
|
+
desiredDecimals: 6,
|
|
77
|
+
}),
|
|
78
|
+
).toBe("1.234568")
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it("should handle zero", () => {
|
|
82
|
+
expect(bigIntToFloat({ amount: 0n })).toBe("0")
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("should handle negative numbers", () => {
|
|
86
|
+
expect(bigIntToFloat({ amount: -1_500_000_000_000_000_000n })).toBe("-1.5")
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
it("should handle rounding up", () => {
|
|
90
|
+
expect(
|
|
91
|
+
bigIntToFloat({
|
|
92
|
+
amount: 1_249_999_999_999_999_999n,
|
|
93
|
+
decimals: 18,
|
|
94
|
+
desiredDecimals: 2,
|
|
95
|
+
}),
|
|
96
|
+
).toBe("1.25")
|
|
97
|
+
expect(
|
|
98
|
+
bigIntToFloat({
|
|
99
|
+
amount: 1_234_567_890_000_000_000n,
|
|
100
|
+
decimals: 18,
|
|
101
|
+
desiredDecimals: 4,
|
|
102
|
+
}),
|
|
103
|
+
).toBe("1.2346")
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it("should handle large numbers", () => {
|
|
107
|
+
expect(
|
|
108
|
+
bigIntToFloat({
|
|
109
|
+
amount: 123_456_789_012_345_678_901_234_567_890n,
|
|
110
|
+
decimals: 18,
|
|
111
|
+
desiredDecimals: 2,
|
|
112
|
+
}),
|
|
113
|
+
).toBe("123456789012.35")
|
|
114
|
+
})
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
describe("floatToHumanReadableFormat", () => {
|
|
118
|
+
it("should format number with thousands separator and default decimals", () => {
|
|
119
|
+
expect(floatToHumanReadableFormat({ amount: 1234567.891 })).toBe(
|
|
120
|
+
"1,234,567.89",
|
|
121
|
+
)
|
|
122
|
+
expect(floatToHumanReadableFormat({ amount: "1234567.891" })).toBe(
|
|
123
|
+
"1,234,567.89",
|
|
124
|
+
)
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it("should format with custom decimals", () => {
|
|
128
|
+
expect(
|
|
129
|
+
floatToHumanReadableFormat({ amount: 1234567.891, desiredDecimals: 3 }),
|
|
130
|
+
).toBe("1,234,567.891")
|
|
131
|
+
expect(
|
|
132
|
+
floatToHumanReadableFormat({ amount: 1234567.8, desiredDecimals: 0 }),
|
|
133
|
+
).toBe("1,234,568")
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it("should format with minDecimals", () => {
|
|
137
|
+
expect(
|
|
138
|
+
floatToHumanReadableFormat({
|
|
139
|
+
amount: 1234.5,
|
|
140
|
+
desiredDecimals: 2,
|
|
141
|
+
minDecimals: 1,
|
|
142
|
+
}),
|
|
143
|
+
).toBe("1,234.5")
|
|
144
|
+
expect(
|
|
145
|
+
floatToHumanReadableFormat({
|
|
146
|
+
amount: 1234.5,
|
|
147
|
+
desiredDecimals: 2,
|
|
148
|
+
minDecimals: 2,
|
|
149
|
+
}),
|
|
150
|
+
).toBe("1,234.50")
|
|
151
|
+
expect(
|
|
152
|
+
floatToHumanReadableFormat({
|
|
153
|
+
amount: 1234.5,
|
|
154
|
+
desiredDecimals: 2,
|
|
155
|
+
minDecimals: 4,
|
|
156
|
+
}),
|
|
157
|
+
).toBe("1,234.5000")
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
it("should return '0' for invalid input", () => {
|
|
161
|
+
expect(floatToHumanReadableFormat({ amount: "abc" })).toBe("0")
|
|
162
|
+
})
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
describe("bigIntToHumanReadableFormat", () => {
|
|
166
|
+
it("should format bigint to human readable string", () => {
|
|
167
|
+
expect(
|
|
168
|
+
bigIntToHumanReadableFormat({
|
|
169
|
+
amount: 1_234_567_890_000_000_000n,
|
|
170
|
+
decimals: 18,
|
|
171
|
+
desiredDecimals: 2,
|
|
172
|
+
}),
|
|
173
|
+
).toBe("1.23")
|
|
174
|
+
expect(
|
|
175
|
+
bigIntToHumanReadableFormat({
|
|
176
|
+
amount: 1_234_567_890_000_000_000n,
|
|
177
|
+
decimals: 18,
|
|
178
|
+
desiredDecimals: 6,
|
|
179
|
+
}),
|
|
180
|
+
).toBe("1.234568")
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it("should handle negative values", () => {
|
|
184
|
+
expect(
|
|
185
|
+
bigIntToHumanReadableFormat({
|
|
186
|
+
amount: -1_234_567_890_000_000_000n,
|
|
187
|
+
decimals: 18,
|
|
188
|
+
desiredDecimals: 2,
|
|
189
|
+
}),
|
|
190
|
+
).toBe("-1.23")
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
it("should show <0.01 for very small nonzero values", () => {
|
|
194
|
+
expect(
|
|
195
|
+
bigIntToHumanReadableFormat({
|
|
196
|
+
amount: 1n,
|
|
197
|
+
decimals: 18,
|
|
198
|
+
desiredDecimals: 2,
|
|
199
|
+
}),
|
|
200
|
+
).toBe("<0.01")
|
|
201
|
+
expect(
|
|
202
|
+
bigIntToHumanReadableFormat({
|
|
203
|
+
amount: -1n,
|
|
204
|
+
decimals: 18,
|
|
205
|
+
desiredDecimals: 2,
|
|
206
|
+
}),
|
|
207
|
+
).toBe("<0.01")
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
it("should format with minDecimals", () => {
|
|
211
|
+
expect(
|
|
212
|
+
bigIntToHumanReadableFormat({
|
|
213
|
+
amount: 1_234_000_000_000_000_000n,
|
|
214
|
+
decimals: 18,
|
|
215
|
+
desiredDecimals: 2,
|
|
216
|
+
minDecimals: 3,
|
|
217
|
+
}),
|
|
218
|
+
).toBe("1.234")
|
|
219
|
+
expect(
|
|
220
|
+
bigIntToHumanReadableFormat({
|
|
221
|
+
amount: 1_234_000_000_000_000_000n,
|
|
222
|
+
decimals: 18,
|
|
223
|
+
desiredDecimals: 3,
|
|
224
|
+
minDecimals: 2,
|
|
225
|
+
}),
|
|
226
|
+
).toBe("1.234")
|
|
227
|
+
expect(
|
|
228
|
+
bigIntToHumanReadableFormat({
|
|
229
|
+
amount: 1_234_000_000_000_000_000n,
|
|
230
|
+
decimals: 18,
|
|
231
|
+
desiredDecimals: 2,
|
|
232
|
+
minDecimals: 4,
|
|
233
|
+
}),
|
|
234
|
+
).toBe("1.2340")
|
|
235
|
+
})
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
describe("multiplyBigIntWithDecimal", () => {
|
|
239
|
+
it("should multiply bigint by integer", () => {
|
|
240
|
+
expect(multiplyBigIntWithDecimal(10n, 2)).toBe(20n)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
it("should multiply bigint by decimal", () => {
|
|
244
|
+
expect(multiplyBigIntWithDecimal(100n, 0.5)).toBe(50n)
|
|
245
|
+
expect(multiplyBigIntWithDecimal(100n, 1.25)).toBe(125n)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it("should handle scientific notation", () => {
|
|
249
|
+
expect(multiplyBigIntWithDecimal(100n, 1e-2)).toBe(1n)
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
it("should handle zero", () => {
|
|
253
|
+
expect(multiplyBigIntWithDecimal(0n, 1.5)).toBe(0n)
|
|
254
|
+
})
|
|
255
|
+
})
|
|
256
|
+
|
|
257
|
+
describe("trimDecimals", () => {
|
|
258
|
+
it("should trim decimals to specified precision", () => {
|
|
259
|
+
expect(trimDecimals("1.23456", 2)).toBe("1.23")
|
|
260
|
+
expect(trimDecimals("1.2", 4)).toBe("1.2")
|
|
261
|
+
expect(trimDecimals("1", 2)).toBe("1")
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
describe("trimBeforeDecimals", () => {
|
|
266
|
+
it("should trim integer part to specified length", () => {
|
|
267
|
+
expect(trimBeforeDecimals("12345.67", 3)).toBe("123.67")
|
|
268
|
+
expect(trimBeforeDecimals("12.34", 5)).toBe("12.34")
|
|
269
|
+
expect(trimBeforeDecimals("123456", 2)).toBe("12.")
|
|
270
|
+
})
|
|
271
|
+
})
|
|
272
|
+
|
|
273
|
+
describe("normalizeDecimalNumber", () => {
|
|
274
|
+
it("should remove invalid characters", () => {
|
|
275
|
+
expect(normalizeDecimalNumber("12a3.4b5")).toBe("123.45")
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
it("should prevent multiple dots", () => {
|
|
279
|
+
expect(normalizeDecimalNumber("1.2.3")).toBe("12.3")
|
|
280
|
+
expect(normalizeDecimalNumber("1..1.2")).toBe("11.2")
|
|
281
|
+
})
|
|
282
|
+
|
|
283
|
+
it("should allow only digits and one dot", () => {
|
|
284
|
+
expect(normalizeDecimalNumber("abc")).toBe("")
|
|
285
|
+
expect(normalizeDecimalNumber("123")).toBe("123")
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
it("should handle leading and trailing spaces", () => {
|
|
289
|
+
expect(normalizeDecimalNumber(" 1.23 ")).toBe("1.23")
|
|
290
|
+
expect(normalizeDecimalNumber(" 1.2.3 ")).toBe("12.3")
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it("should handle empty input", () => {
|
|
294
|
+
expect(normalizeDecimalNumber("")).toBe("")
|
|
295
|
+
expect(normalizeDecimalNumber(" ")).toBe("")
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
it("should handle numbers with thousands separator", () => {
|
|
299
|
+
expect(normalizeDecimalNumber("1,234.56")).toBe("1234.56")
|
|
300
|
+
expect(normalizeDecimalNumber("1,234,567.89")).toBe("1234567.89")
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
it("should handle scientific notation", () => {
|
|
304
|
+
// This function does not support scientific notation as our input is expected
|
|
305
|
+
// to accept only decimal numbers
|
|
306
|
+
expect(normalizeDecimalNumber("1e3")).toBe("13")
|
|
307
|
+
expect(normalizeDecimalNumber("1.23e2")).toBe("1.232")
|
|
308
|
+
})
|
|
309
|
+
})
|