@mezo-org/passport 0.4.0-dev.82 → 0.4.0-dev.84

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 (173) hide show
  1. package/dist/src/components/Dropdown/Content.d.ts +0 -15
  2. package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
  3. package/dist/src/components/Dropdown/Content.js +3 -3
  4. package/dist/src/components/Dropdown/Content.js.map +1 -1
  5. package/dist/src/components/Dropdown/Dropdown.d.ts +0 -4
  6. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
  7. package/dist/src/components/Dropdown/Dropdown.js +10 -36
  8. package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
  9. package/dist/src/components/Dropdown/ListingItem.d.ts +8 -7
  10. package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -1
  11. package/dist/src/components/Dropdown/ListingItem.js +36 -28
  12. package/dist/src/components/Dropdown/ListingItem.js.map +1 -1
  13. package/dist/src/components/Dropdown/NestedViewLayout.d.ts +2 -1
  14. package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -1
  15. package/dist/src/components/Dropdown/NestedViewLayout.js +13 -15
  16. package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -1
  17. package/dist/src/components/Dropdown/Receive/Receive.d.ts +1 -4
  18. package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -1
  19. package/dist/src/components/Dropdown/Receive/Receive.js +30 -19
  20. package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -1
  21. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +2 -5
  22. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -1
  23. package/dist/src/components/Dropdown/Root/AccountAddressActions.js +13 -12
  24. package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -1
  25. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +2 -4
  26. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -1
  27. package/dist/src/components/Dropdown/Root/AccountBalance.js +24 -8
  28. package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -1
  29. package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts +6 -0
  30. package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts.map +1 -0
  31. package/dist/src/components/Dropdown/Root/AccountBtcListing.js +27 -0
  32. package/dist/src/components/Dropdown/Root/AccountBtcListing.js.map +1 -0
  33. package/dist/src/components/Dropdown/Root/AccountError.d.ts +8 -0
  34. package/dist/src/components/Dropdown/Root/AccountError.d.ts.map +1 -0
  35. package/dist/src/components/Dropdown/Root/AccountError.js +17 -0
  36. package/dist/src/components/Dropdown/Root/AccountError.js.map +1 -0
  37. package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts +4 -0
  38. package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts.map +1 -0
  39. package/dist/src/components/Dropdown/Root/AccountMusdListing.js +21 -0
  40. package/dist/src/components/Dropdown/Root/AccountMusdListing.js.map +1 -0
  41. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +2 -3
  42. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -1
  43. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +34 -39
  44. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -1
  45. package/dist/src/components/Dropdown/Root/Root.d.ts +0 -15
  46. package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -1
  47. package/dist/src/components/Dropdown/Root/Root.js +22 -34
  48. package/dist/src/components/Dropdown/Root/Root.js.map +1 -1
  49. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +2 -6
  50. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -1
  51. package/dist/src/components/Dropdown/Root/WalletAddress.js +37 -34
  52. package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -1
  53. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +2 -4
  54. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -1
  55. package/dist/src/components/Dropdown/Root/WelcomeBlock.js +60 -16
  56. package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -1
  57. package/dist/src/components/Dropdown/SlotNumber.d.ts +19 -0
  58. package/dist/src/components/Dropdown/SlotNumber.d.ts.map +1 -0
  59. package/dist/src/components/Dropdown/SlotNumber.js +67 -0
  60. package/dist/src/components/Dropdown/SlotNumber.js.map +1 -0
  61. package/dist/src/config.d.ts +0 -1
  62. package/dist/src/config.d.ts.map +1 -1
  63. package/dist/src/config.js +1 -9
  64. package/dist/src/config.js.map +1 -1
  65. package/dist/src/hooks/useAssetsConversionRates.d.ts +8 -13
  66. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
  67. package/dist/src/hooks/useAssetsConversionRates.js +44 -67
  68. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
  69. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
  70. package/dist/src/hooks/useAuthenticateWithWallet.js +1 -1
  71. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
  72. package/dist/src/hooks/useBorrowData.d.ts +30 -4
  73. package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
  74. package/dist/src/hooks/useBorrowData.js +53 -11
  75. package/dist/src/hooks/useBorrowData.js.map +1 -1
  76. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  77. package/dist/src/hooks/useCreateAccount.js +3 -3
  78. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  79. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  80. package/dist/src/hooks/useGetCurrentAccount.js +4 -6
  81. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  82. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  83. package/dist/src/hooks/useLinkAccount.js +3 -3
  84. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  85. package/dist/src/hooks/useTokensBalances.d.ts +36 -35
  86. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -1
  87. package/dist/src/hooks/useTokensBalances.js +93 -52
  88. package/dist/src/hooks/useTokensBalances.js.map +1 -1
  89. package/dist/src/hooks/useWalletAccount.d.ts +8 -6
  90. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
  91. package/dist/src/hooks/useWalletAccount.js +9 -6
  92. package/dist/src/hooks/useWalletAccount.js.map +1 -1
  93. package/dist/src/lib/contracts/index.d.ts +1 -1
  94. package/dist/src/lib/contracts/index.d.ts.map +1 -1
  95. package/dist/src/lib/contracts/index.js +4 -0
  96. package/dist/src/lib/contracts/index.js.map +1 -1
  97. package/dist/src/provider.d.ts +7 -1
  98. package/dist/src/provider.d.ts.map +1 -1
  99. package/dist/src/provider.js +4 -1
  100. package/dist/src/provider.js.map +1 -1
  101. package/dist/src/utils/assets.d.ts +145 -0
  102. package/dist/src/utils/assets.d.ts.map +1 -0
  103. package/dist/src/utils/assets.js +100 -0
  104. package/dist/src/utils/assets.js.map +1 -0
  105. package/dist/src/utils/assets.test.d.ts +2 -0
  106. package/dist/src/utils/assets.test.d.ts.map +1 -0
  107. package/dist/src/utils/assets.test.js +46 -0
  108. package/dist/src/utils/assets.test.js.map +1 -0
  109. package/dist/src/utils/currency.d.ts +6 -3
  110. package/dist/src/utils/currency.d.ts.map +1 -1
  111. package/dist/src/utils/currency.js +13 -10
  112. package/dist/src/utils/currency.js.map +1 -1
  113. package/dist/src/utils/currency.test.js +44 -2
  114. package/dist/src/utils/currency.test.js.map +1 -1
  115. package/dist/src/utils/numbers.d.ts +13 -53
  116. package/dist/src/utils/numbers.d.ts.map +1 -1
  117. package/dist/src/utils/numbers.js +16 -118
  118. package/dist/src/utils/numbers.js.map +1 -1
  119. package/dist/src/utils/numbers.test.js +24 -142
  120. package/dist/src/utils/numbers.test.js.map +1 -1
  121. package/package.json +2 -1
  122. package/src/components/Dropdown/Content.tsx +3 -48
  123. package/src/components/Dropdown/Dropdown.tsx +7 -58
  124. package/src/components/Dropdown/ListingItem.tsx +155 -59
  125. package/src/components/Dropdown/NestedViewLayout.tsx +32 -20
  126. package/src/components/Dropdown/Receive/Receive.tsx +57 -32
  127. package/src/components/Dropdown/Root/AccountAddressActions.tsx +33 -35
  128. package/src/components/Dropdown/Root/AccountBalance.tsx +54 -16
  129. package/src/components/Dropdown/Root/AccountBtcListing.tsx +52 -0
  130. package/src/components/Dropdown/Root/AccountError.tsx +34 -0
  131. package/src/components/Dropdown/Root/AccountMusdListing.tsx +45 -0
  132. package/src/components/Dropdown/Root/AccountOtherAssets.tsx +63 -46
  133. package/src/components/Dropdown/Root/Root.tsx +28 -98
  134. package/src/components/Dropdown/Root/WalletAddress.tsx +87 -89
  135. package/src/components/Dropdown/Root/WelcomeBlock.tsx +112 -30
  136. package/src/components/Dropdown/SlotNumber.tsx +131 -0
  137. package/src/config.ts +1 -11
  138. package/src/hooks/useAssetsConversionRates.ts +49 -67
  139. package/src/hooks/useAuthenticateWithWallet.ts +7 -5
  140. package/src/hooks/useBorrowData.ts +71 -12
  141. package/src/hooks/useCreateAccount.ts +5 -4
  142. package/src/hooks/useGetCurrentAccount.ts +5 -7
  143. package/src/hooks/useLinkAccount.ts +5 -4
  144. package/src/hooks/useTokensBalances.ts +152 -74
  145. package/src/hooks/useWalletAccount.ts +19 -13
  146. package/src/lib/contracts/index.ts +8 -1
  147. package/src/provider.ts +11 -3
  148. package/src/utils/assets.test.ts +57 -0
  149. package/src/utils/assets.ts +103 -0
  150. package/src/utils/currency.test.ts +76 -2
  151. package/src/utils/currency.ts +20 -15
  152. package/src/utils/numbers.test.ts +29 -180
  153. package/src/utils/numbers.ts +22 -171
  154. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +0 -11
  155. package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +0 -1
  156. package/dist/src/components/Dropdown/Root/AccountAssetItem.js +0 -9
  157. package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +0 -1
  158. package/dist/src/hooks/useDropdownData.d.ts +0 -47
  159. package/dist/src/hooks/useDropdownData.d.ts.map +0 -1
  160. package/dist/src/hooks/useDropdownData.js +0 -97
  161. package/dist/src/hooks/useDropdownData.js.map +0 -1
  162. package/dist/src/utils/cryptoAssets.d.ts +0 -44
  163. package/dist/src/utils/cryptoAssets.d.ts.map +0 -1
  164. package/dist/src/utils/cryptoAssets.js +0 -129
  165. package/dist/src/utils/cryptoAssets.js.map +0 -1
  166. package/dist/src/utils/cryptoAssets.test.d.ts +0 -2
  167. package/dist/src/utils/cryptoAssets.test.d.ts.map +0 -1
  168. package/dist/src/utils/cryptoAssets.test.js +0 -67
  169. package/dist/src/utils/cryptoAssets.test.js.map +0 -1
  170. package/src/components/Dropdown/Root/AccountAssetItem.tsx +0 -26
  171. package/src/hooks/useDropdownData.ts +0 -152
  172. package/src/utils/cryptoAssets.test.ts +0 -79
  173. package/src/utils/cryptoAssets.ts +0 -171
@@ -1,10 +1,5 @@
1
- import { fromFixedPoint } from "./numbers"
2
-
3
- const DEFAULT_FORMAT_OPTIONS: Intl.NumberFormatOptions = {
4
- style: "currency",
5
- minimumFractionDigits: 2,
6
- maximumFractionDigits: 2,
7
- }
1
+ import { formatUnits } from "viem"
2
+ import { normalizePrecision } from "./numbers"
8
3
 
9
4
  /**
10
5
  * Formats a number as a currency
@@ -14,10 +9,12 @@ const DEFAULT_FORMAT_OPTIONS: Intl.NumberFormatOptions = {
14
9
  */
15
10
  export function formatCurrency(
16
11
  value: number,
17
- options: Omit<Intl.NumberFormatOptions, "style"> = {},
12
+ options: Omit<Intl.NumberFormatOptions, "style">,
18
13
  ): string {
19
14
  const formatter = new Intl.NumberFormat("en-US", {
20
- ...DEFAULT_FORMAT_OPTIONS,
15
+ style: "currency",
16
+ minimumFractionDigits: 2,
17
+ maximumFractionDigits: 2,
21
18
  ...options,
22
19
  })
23
20
 
@@ -29,8 +26,8 @@ export function formatCurrency(
29
26
  * @param value The value to format
30
27
  * @returns The formatted currency
31
28
  */
32
- export function formatUsd(value: number): string {
33
- return formatCurrency(value, { currency: "USD" })
29
+ export function formatUsd(value: number | string): string {
30
+ return formatCurrency(Number(value), { currency: "USD" })
34
31
  }
35
32
 
36
33
  /**
@@ -45,9 +42,17 @@ export function convertToUsd(
45
42
  assetDecimals: number,
46
43
  conversionRate: bigint,
47
44
  conversionRateDecimals: number,
48
- ): number {
49
- return fromFixedPoint(
50
- (assetAmount * conversionRate) / 10n ** BigInt(conversionRateDecimals),
51
- assetDecimals,
45
+ ) {
46
+ const value = normalizePrecision(
47
+ assetAmount * conversionRate,
48
+ assetDecimals + conversionRateDecimals,
49
+ conversionRateDecimals,
52
50
  )
51
+
52
+ const formatted = formatUnits(value, conversionRateDecimals)
53
+
54
+ return {
55
+ value,
56
+ formatted,
57
+ }
53
58
  }
@@ -1,10 +1,7 @@
1
1
  import {
2
2
  formatNumberToCompactString,
3
- fromFixedPoint,
4
- roundUpNumber,
5
- formatNumberToLocaleString,
6
- fromFixedPointToString,
7
3
  bigIntMax,
4
+ normalizePrecision,
8
5
  } from "./numbers"
9
6
 
10
7
  describe("formatNumberToCompactString", () => {
@@ -44,182 +41,6 @@ describe("formatNumberToCompactString", () => {
44
41
  })
45
42
  })
46
43
 
47
- describe("roundUpNumber", () => {
48
- it("rounds up to 2 decimals", () => {
49
- expect(roundUpNumber(1.234)).toBe(1.24)
50
- })
51
-
52
- it("rounds up to 3 decimals", () => {
53
- expect(roundUpNumber(9.87654, 3)).toBe(9.877)
54
- })
55
-
56
- it("rounds up exact decimal", () => {
57
- expect(roundUpNumber(2.5, 1)).toBe(2.5)
58
- })
59
- })
60
-
61
- describe("fromFixedPoint", () => {
62
- it("converts fixed point with 18 decimals to float", () => {
63
- const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
64
- expect(fromFixedPoint(raw, 18)).toBe(1.2345)
65
- })
66
-
67
- it("converts with truncation to 2 decimals", () => {
68
- const raw = 987654321000000000n // ~0.987654321 ETH
69
- expect(fromFixedPoint(raw, 18, 2)).toBe(0.98)
70
- })
71
-
72
- it("handles large fixedPoint decimals", () => {
73
- const raw = 1_000_000_000_000_000_000_000_000n // 1M with 6 decimals
74
- expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000)
75
- })
76
- })
77
-
78
- describe("formatNumberToLocaleString", () => {
79
- it("formats zero with 0 decimals", () => {
80
- expect(formatNumberToLocaleString(0, 0)).toBe("0")
81
- })
82
-
83
- it("formats zero with >0 decimals", () => {
84
- expect(formatNumberToLocaleString(0, 2)).toBe("0.00")
85
- expect(formatNumberToLocaleString(0, 4)).toBe("0.0000")
86
- })
87
-
88
- it("formats small decimal number", () => {
89
- expect(formatNumberToLocaleString(0.123456, 2)).toBe("0.12")
90
- expect(formatNumberToLocaleString(0.123456, 4)).toBe("0.1235")
91
- })
92
-
93
- it("formats number just above 1", () => {
94
- expect(formatNumberToLocaleString(1.000001, 3)).toBe("1.000")
95
- expect(formatNumberToLocaleString(1.234567, 2)).toBe("1.23")
96
- })
97
-
98
- it("formats whole number", () => {
99
- expect(formatNumberToLocaleString(1234, 0)).toBe("1,234")
100
- expect(formatNumberToLocaleString(1000, 2)).toBe("1,000.00")
101
- })
102
-
103
- it("formats number with trailing decimal zeroes", () => {
104
- expect(formatNumberToLocaleString(10.1, 3)).toBe("10.100")
105
- expect(formatNumberToLocaleString(5.0, 2)).toBe("5.00")
106
- })
107
-
108
- it("formats string input", () => {
109
- expect(formatNumberToLocaleString("123.456", 2)).toBe("123.46")
110
- expect(formatNumberToLocaleString("0.00123", 4)).toBe("0.0012")
111
- })
112
-
113
- it("formats negative number", () => {
114
- expect(formatNumberToLocaleString(-123.456, 2)).toBe("-123.46")
115
- expect(formatNumberToLocaleString("-0.00999", 3)).toBe("-0.010")
116
- })
117
-
118
- it("formats large number with commas", () => {
119
- expect(formatNumberToLocaleString(12345678.9, 2)).toBe("12,345,678.90")
120
- })
121
-
122
- it("handles max decimals safely", () => {
123
- expect(formatNumberToLocaleString(1.123456789, 8)).toBe("1.12345679")
124
- expect(formatNumberToLocaleString("0.000000000000000001", 18)).toBe(
125
- "0.000000000000000001",
126
- )
127
- })
128
-
129
- it("defaults to 0 decimals if not passed", () => {
130
- expect(formatNumberToLocaleString(1234.567)).toBe("1,235") // rounds up by default
131
- })
132
- })
133
-
134
- describe("fromFixedPointToString", () => {
135
- const toFixedPoint = (num: number) => BigInt(Math.round(num * 1e18))
136
-
137
- const cases = [
138
- { input: 2.0, expected: "2.000" },
139
- { input: 2.009, expected: "2.009" },
140
- { input: 2.01, expected: "2.010" },
141
- { input: 2.015, expected: "2.016" },
142
- { input: 2.05, expected: "2.050" },
143
- { input: 2.09, expected: "2.090" },
144
- ]
145
-
146
- describe("withRoundUp: true", () => {
147
- cases.forEach(({ input, expected }) => {
148
- it(`formats ${input} → ${expected}`, () => {
149
- const fixed = toFixedPoint(input)
150
- expect(fromFixedPointToString(fixed, 18, 3, true)).toBe(expected)
151
- })
152
- })
153
- })
154
-
155
- describe("withRoundUp: false", () => {
156
- const noRoundCases = [
157
- { input: 2.0099, expected: "2.009" },
158
- { input: 2.0159, expected: "2.015" },
159
- { input: 2.0101, expected: "2.010" },
160
- { input: 2.0199, expected: "2.019" },
161
- ]
162
-
163
- noRoundCases.forEach(({ input, expected }) => {
164
- it(`formats ${input} → ${expected} (no rounding)`, () => {
165
- const fixed = toFixedPoint(input)
166
- expect(fromFixedPointToString(fixed, 18, 3, false)).toBe(expected)
167
- })
168
- })
169
- })
170
-
171
- describe("zero handling", () => {
172
- it("formats 0 → 0.000 (3 decimals)", () => {
173
- expect(fromFixedPointToString(0n, 18, 3)).toBe("0.000")
174
- })
175
-
176
- it("formats 0 → 0.0000 (4 decimals)", () => {
177
- expect(fromFixedPointToString(0n, 18, 4)).toBe("0.0000")
178
- })
179
- })
180
-
181
- describe("very small numbers below display threshold", () => {
182
- it("returns <0.0001 for small number (3 decimals)", () => {
183
- const small = BigInt(123) // ~1.23e-16
184
- expect(fromFixedPointToString(small, 18, 4)).toBe("<0.0001")
185
- })
186
-
187
- it("returns <0.01 for small number with only 2 decimals", () => {
188
- const tiny = BigInt(9999999999999) // <0.01
189
- expect(fromFixedPointToString(tiny, 18, 2)).toBe("<0.01")
190
- })
191
- })
192
-
193
- describe("input types", () => {
194
- it("accepts bigint", () => {
195
- const amount = 2100000000000000000n
196
- expect(fromFixedPointToString(amount, 18, 3)).toBe("2.100")
197
- })
198
-
199
- it("accepts string", () => {
200
- expect(fromFixedPointToString("2000000000000000000", 18, 3)).toBe("2.000")
201
- })
202
-
203
- it("accepts number", () => {
204
- expect(fromFixedPointToString(Number("1000000000000000000"), 18, 3)).toBe(
205
- "1.000",
206
- )
207
- })
208
- })
209
-
210
- describe("high values", () => {
211
- it("formats 1_000_000.123456789 correctly", () => {
212
- const big = toFixedPoint(1_000_000.123456789)
213
- expect(fromFixedPointToString(big, 18, 6)).toBe("1,000,000.123456")
214
- })
215
-
216
- it("formats 1e12 ETH", () => {
217
- const big = BigInt("1000000000000000000000000000000")
218
- expect(fromFixedPointToString(big, 18, 2)).toBe("1,000,000,000,000.00")
219
- })
220
- })
221
- })
222
-
223
44
  describe("bigIntMax", () => {
224
45
  it("returns the maximum of positive bigint values", () => {
225
46
  expect(bigIntMax(1n, 2n, 3n)).toBe(3n)
@@ -245,3 +66,31 @@ describe("bigIntMax", () => {
245
66
  expect(bigIntMax(5n, 5n, 5n)).toBe(5n)
246
67
  })
247
68
  })
69
+
70
+ describe("normalizePrecision", () => {
71
+ it("reduces precision correctly (scale down)", () => {
72
+ const result = normalizePrecision(1234500n, 6, 3)
73
+ expect(result).toBe(1234n)
74
+ })
75
+
76
+ it("increases precision correctly (scale up)", () => {
77
+ const result = normalizePrecision(1234n, 3, 6)
78
+ expect(result).toBe(1234000n)
79
+ })
80
+
81
+ it("returns the same value when precision is equal", () => {
82
+ const result = normalizePrecision(987654321n, 5, 5)
83
+ expect(result).toBe(987654321n)
84
+ })
85
+
86
+ it("handles zero value correctly", () => {
87
+ const result = normalizePrecision(0n, 4, 8)
88
+ expect(result).toBe(0n)
89
+ })
90
+
91
+ it("handles large numbers without precision loss", () => {
92
+ const value = 123456789012345678901234567890n
93
+ const result = normalizePrecision(value, 18, 20)
94
+ expect(result).toBe(value * 100n)
95
+ })
96
+ })
@@ -1,6 +1,5 @@
1
- type DesiredDecimals = Intl.NumberFormatOptions &
1
+ type CompactStringDecimals = Intl.NumberFormatOptions["minimumFractionDigits"] &
2
2
  BigIntToLocaleStringOptions["minimumFractionDigits"]
3
-
4
3
  /**
5
4
  * Formats a number or bigint into compact string with K, M, B, or T suffix.
6
5
  * @param value The number or bigint to format.
@@ -9,189 +8,41 @@ type DesiredDecimals = Intl.NumberFormatOptions &
9
8
  */
10
9
  export function formatNumberToCompactString(
11
10
  value: number | bigint,
12
- decimals = 4,
11
+ decimals: CompactStringDecimals = 2,
13
12
  ): string {
14
13
  return value.toLocaleString("en-US", {
15
14
  notation: "compact",
16
15
  compactDisplay: "short",
17
- maximumFractionDigits: decimals as DesiredDecimals,
16
+ maximumFractionDigits: decimals,
18
17
  })
19
18
  }
20
19
 
21
20
  /**
22
- * Formats a number or string into a locale-sensitive string with the desired number of decimals.
23
- * @param value The number or string to format.
24
- * @param desiredDecimals The number of decimal places to include.
25
- * @returns The formatted number as a string.
26
- * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L5-L19
27
- */
28
- export function formatNumberToLocaleString(
29
- value: string | number,
30
- desiredDecimals = 0,
31
- ) {
32
- const number = typeof value === "number" ? value : parseFloat(value)
33
-
34
- if (number === 0 && desiredDecimals === 0) return "0"
35
-
36
- if (number === 0) return `0.${"0".repeat(desiredDecimals)}`
37
-
38
- return number.toLocaleString("en-US", {
39
- minimumFractionDigits: desiredDecimals,
40
- maximumFractionDigits: desiredDecimals,
41
- })
42
- }
43
-
44
- /**
45
- * Returns a number rounded up to the desired number of decimals.
46
- * @param amount The number to round up.
47
- * @param desiredDecimals The number of decimals to round up to.
48
- * @returns The rounded up number.
49
- * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
50
- */
51
- export function roundUpNumber(amount: number, desiredDecimals = 2): number {
52
- return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals
53
- }
54
-
55
- /**
56
- * Convert a fixed point bigint with precision `fixedPointDecimals` to a
57
- * floating point number truncated to `desiredDecimals`.
58
- * @param fixedPoint The fixed point bigint to convert.
59
- * @param fixedPointDecimals The number of decimals in the fixed point bigint.
60
- * @param desiredDecimals The number of decimals in the output number.
61
- * @returns The floating point number truncated to `desiredDecimals`.
62
- * @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
21
+ * Normalizes a bigint value to a target precision.
22
+ * @param value The bigint value to normalize.
23
+ * @param currentPrecision The current precision of the value.
24
+ * @param targetPrecision The target precision to normalize to.
25
+ * @returns The normalized bigint value.
63
26
  */
64
- export function fromFixedPoint(
65
- fixedPoint: bigint,
66
- fixedPointDecimals: number,
67
- desiredDecimals = 4,
68
- ): number {
69
- const fixedPointDesiredDecimalsAmount =
70
- fixedPoint /
71
- 10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals))
72
-
73
- const formattedAmount =
74
- Number(fixedPointDesiredDecimalsAmount) /
75
- 10 ** Math.min(desiredDecimals, fixedPointDecimals)
76
-
77
- return formattedAmount
78
- }
79
-
80
- /**
81
- * Display a token amount correctly with desired decimals.
82
- * The function returns a string with a language-sensitive representation of this number.
83
- * @param amount The fixed point number to convert.
84
- * @param decimals The number of decimals in the fixed point number.
85
- * @param desiredDecimals The number of decimals to display.
86
- * @param withRoundUp Whether to round up the number.
87
- * @returns The formatted number as a string.
88
- * @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L57-L87
89
- */
90
- export const fromFixedPointToString = (
91
- amount: number | string | bigint,
92
- decimals = 18,
93
- desiredDecimals = 4,
94
- withRoundUp = false,
95
- ) => {
96
- const fixedPoint = BigInt(amount)
97
-
98
- if (fixedPoint === 0n) {
99
- return `0.${"0".repeat(desiredDecimals)}`
100
- }
101
-
102
- const formattedAmount = fromFixedPoint(
103
- fixedPoint,
104
- decimals,
105
- // To round the amount up, let's increase the precision by one.
106
- // The desired decimal numbers will be set later anyway.
107
- withRoundUp ? desiredDecimals + 1 : desiredDecimals,
108
- )
109
-
110
- const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals)
111
-
112
- if (minAmountToDisplay > formattedAmount) {
113
- return `<0.${"0".repeat(desiredDecimals - 1)}1`
114
- }
115
-
116
- const finalFormattedAmount = withRoundUp
117
- ? Math.ceil(formattedAmount * 10 ** desiredDecimals) / 10 ** desiredDecimals
118
- : formattedAmount
119
- return formatNumberToLocaleString(finalFormattedAmount, desiredDecimals)
120
- }
121
-
122
- /**
123
- * Converts a floating-point number or string to a bigint representation
124
- * scaled by the specified number of decimal places (default: 18).
125
- *
126
- * This function avoids floating-point precision issues by manually
127
- * parsing and scaling the number.
128
- *
129
- * @param amount - A number or string containing a floating point number.
130
- * @param decimals - Number of decimals to scale to (default: 18).
131
- * @returns A bigint representation scaled to the given decimal places.
132
- * @see https://github.com/thesis/mezo-portal/blob/3bd4f39cb77c39948af2886a6ce7415a0c60307a/dapp/src/shared/utils/numbers.ts#L55
133
- */
134
- export const fromFloatToBigInt = (
135
- amount: string | number,
136
- decimals = 18,
27
+ export const normalizePrecision = (
28
+ value: bigint,
29
+ currentPrecision: number,
30
+ targetPrecision = currentPrecision,
137
31
  ): bigint => {
138
- const FLOATING_POINT_REGEX = /^-?\d{1,3}(,\d{3})*(\.\d+)?$|^-?\d+(\.\d+)?$/
139
-
140
- const parseToFixedPointNumber = (
141
- floatingPointString: string,
142
- ): { amount: bigint; decimals: number } | undefined => {
143
- if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
144
- return undefined
145
- }
146
-
147
- const [whole, decimalsPart, ...extra] = floatingPointString.split(".")
148
-
149
- if (extra.length > 0) return undefined
150
-
151
- const noThousandsSeparatorWhole = whole.replace(/,/g, "")
152
- const setDecimals = decimalsPart ?? ""
153
-
154
- try {
155
- return {
156
- amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
157
- decimals: setDecimals.length,
158
- }
159
- } catch {
160
- return undefined
161
- }
162
- }
163
-
164
- const convertFixedPoint = (
165
- fixedPoint: bigint,
166
- fixedPointDecimals: number,
167
- targetDecimals: number,
168
- ): bigint => {
169
- if (fixedPointDecimals >= targetDecimals) {
170
- return fixedPoint / 10n ** BigInt(fixedPointDecimals - targetDecimals)
171
- }
172
-
173
- return fixedPoint * 10n ** BigInt(targetDecimals - fixedPointDecimals)
32
+ if (currentPrecision > targetPrecision) {
33
+ return value / 10n ** BigInt(currentPrecision - targetPrecision)
174
34
  }
175
-
176
- const parsedAmount = amount.toString()
177
- const fixedPointAmount = parseToFixedPointNumber(parsedAmount)
178
-
179
- if (typeof fixedPointAmount === "undefined") {
180
- return 0n
35
+ if (currentPrecision < targetPrecision) {
36
+ return value * 10n ** BigInt(targetPrecision - currentPrecision)
181
37
  }
182
-
183
- return convertFixedPoint(
184
- fixedPointAmount.amount,
185
- fixedPointAmount.decimals,
186
- decimals,
187
- )
38
+ return value
188
39
  }
189
40
 
190
41
  /**
191
- * Utility function to find the maximum value from an array of bigint values.
192
- * @param values Array of bigint values to find the maximum from.
193
- * @returns The maximum value from the provided bigint values.
194
- * @dev Works exactly like `Math.max` but for bigint values.
42
+ * Utility function to find the maximum value from given `BigInt` values.
43
+ * @param values Array of BigInt values to find the maximum from.
44
+ * @returns The maximum value from the provided `BigInt` values.
45
+ * @dev Works exactly like `Math.max` but for `BigInt` values.
195
46
  */
196
- export const bigIntMax = (...values: bigint[]) =>
47
+ export const bigIntMax = (...values: bigint[]): bigint =>
197
48
  values.reduce((max, value) => (value > max ? value : max))
@@ -1,11 +0,0 @@
1
- import React from "react";
2
- import { CryptoAssetKey } from "../../../utils/cryptoAssets";
3
- type AccountAssetItemProps = {
4
- type: CryptoAssetKey;
5
- amount: string;
6
- subLabel: string;
7
- subValue: string;
8
- };
9
- export default function AccountAssetItem(props: AccountAssetItemProps): React.JSX.Element;
10
- export {};
11
- //# sourceMappingURL=AccountAssetItem.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AccountAssetItem.d.ts","sourceRoot":"","sources":["../../../../../src/components/Dropdown/Root/AccountAssetItem.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAE,cAAc,EAAkB,MAAM,6BAA6B,CAAA;AAG5E,KAAK,qBAAqB,GAAG;IAC3B,IAAI,EAAE,cAAc,CAAA;IACpB,MAAM,EAAE,MAAM,CAAA;IACd,QAAQ,EAAE,MAAM,CAAA;IAChB,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,qBAcpE"}
@@ -1,9 +0,0 @@
1
- import React from "react";
2
- import { getCryptoAsset } from "../../../utils/cryptoAssets";
3
- import ListingItem from "../ListingItem";
4
- export default function AccountAssetItem(props) {
5
- const { type, amount, subLabel, subValue } = props;
6
- const { icon: Icon, name: label } = getCryptoAsset(type);
7
- return (React.createElement(ListingItem, { icon: Icon, label: label, subLabel: subLabel, value: amount, subValue: subValue }));
8
- }
9
- //# sourceMappingURL=AccountAssetItem.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"AccountAssetItem.js","sourceRoot":"","sources":["../../../../../src/components/Dropdown/Root/AccountAssetItem.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,EAAkB,cAAc,EAAE,MAAM,6BAA6B,CAAA;AAC5E,OAAO,WAAW,MAAM,gBAAgB,CAAA;AASxC,MAAM,CAAC,OAAO,UAAU,gBAAgB,CAAC,KAA4B;IACnE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,KAAK,CAAA;IAElD,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IAExD,OAAO,CACL,oBAAC,WAAW,IACV,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,QAAQ,GAClB,CACH,CAAA;AACH,CAAC"}
@@ -1,47 +0,0 @@
1
- import { CryptoAssetKey } from "../utils/cryptoAssets";
2
- export type DropdownCryptoAsset<T = CryptoAssetKey> = {
3
- type: T;
4
- balance: bigint;
5
- };
6
- /**
7
- * Aggregates dropdown data for Dropdown component.
8
- * @param {accountDataRefetchInterval: number, nativeBalanceRefetchInterval: number }
9
- * Object containing refetch intervals time (in milliseconds) for account data
10
- * (like mats or mezo id) and for native balance. After that time the specific
11
- * value should be marked as stale and re-fetched. Default value for both is
12
- * 90000 (90 secs).
13
- * @returns Dropdown data.
14
- * @dev This hook is for internal use only.
15
- */
16
- export default function useDropdownData({ accountDataRefetchInterval, nativeBalanceRefetchInterval, }: {
17
- accountDataRefetchInterval?: number | undefined;
18
- nativeBalanceRefetchInterval?: number | undefined;
19
- }): {
20
- mezoId: string | undefined;
21
- matsBalance: number;
22
- accountAddress: `0x${string}` | undefined;
23
- walletAddress: string | undefined;
24
- walletType: "bitcoin" | "evm";
25
- usdTotalBalance: number;
26
- usdTroveDebt: number;
27
- usdCollateral: number;
28
- assets: {
29
- btc: {
30
- type: CryptoAssetKey;
31
- rawBalance: bigint;
32
- formattedBalance: string;
33
- usdBalance: number;
34
- formattedUsdBalance: string;
35
- };
36
- musd: {
37
- type: CryptoAssetKey;
38
- rawBalance: bigint;
39
- formattedBalance: string;
40
- usdBalance: number;
41
- formattedUsdBalance: string;
42
- };
43
- };
44
- otherAssetsCount: number;
45
- otherAssetsUsdTotal: number;
46
- };
47
- //# sourceMappingURL=useDropdownData.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useDropdownData.d.ts","sourceRoot":"","sources":["../../../src/hooks/useDropdownData.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,cAAc,EAIf,MAAM,uBAAuB,CAAA;AAW9B,MAAM,MAAM,mBAAmB,CAAC,CAAC,GAAG,cAAc,IAAI;IACpD,IAAI,EAAE,CAAC,CAAA;IACP,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AASD;;;;;;;;;GASG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CAAC,EACtC,0BAAkC,EAClC,4BAAoC,GACrC;;;CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;EA4GA"}
@@ -1,97 +0,0 @@
1
- import { useBalance } from "wagmi";
2
- import { useGetCurrentAccount } from ".";
3
- import { getCryptoAsset, isBitcoinLikeCryptoAsset, mapCryptoAssetToDetails, } from "../utils/cryptoAssets";
4
- import { fromFixedPoint } from "../utils/numbers";
5
- import { useAssetsConversionRates } from "./useAssetsConversionRates";
6
- import useWalletAccount from "./useWalletAccount";
7
- import { useTokensBalances } from "./useTokensBalances";
8
- import { useBorrowData } from "./useBorrowData";
9
- import { CHAIN_ID } from "../constants";
10
- import { usePassportContext } from "./usePassportContext";
11
- import { useWatchTransferEventsForAllTokens } from "./useWatchTransferEvents";
12
- import { convertToUsd } from "../utils/currency";
13
- const DEFAULT_ASSET_DATA = {
14
- rawBalance: 0n,
15
- formattedBalance: "0.0000",
16
- usdBalance: 0,
17
- formattedUsdBalance: "$0.00",
18
- };
19
- /**
20
- * Aggregates dropdown data for Dropdown component.
21
- * @param {accountDataRefetchInterval: number, nativeBalanceRefetchInterval: number }
22
- * Object containing refetch intervals time (in milliseconds) for account data
23
- * (like mats or mezo id) and for native balance. After that time the specific
24
- * value should be marked as stale and re-fetched. Default value for both is
25
- * 90000 (90 secs).
26
- * @returns Dropdown data.
27
- * @dev This hook is for internal use only.
28
- */
29
- export default function useDropdownData({ accountDataRefetchInterval = 90000, nativeBalanceRefetchInterval = 90000, }) {
30
- const { walletAddress, accountAddress, networkFamily: walletType, } = useWalletAccount();
31
- const { environment = "mainnet" } = usePassportContext();
32
- const { data: btcBalance } = useBalance({
33
- address: accountAddress,
34
- chainId: CHAIN_ID[environment],
35
- query: {
36
- staleTime: nativeBalanceRefetchInterval,
37
- refetchInterval: nativeBalanceRefetchInterval,
38
- },
39
- });
40
- const { data: passportAccount } = useGetCurrentAccount({
41
- staleTime: accountDataRefetchInterval,
42
- refetchInterval: accountDataRefetchInterval,
43
- enabled: !!accountAddress,
44
- });
45
- const { data: assetsConversionRates } = useAssetsConversionRates();
46
- const { data: tokensBalances } = useTokensBalances();
47
- const { data: debt } = useBorrowData();
48
- useWatchTransferEventsForAllTokens();
49
- const mezoId = passportAccount?.mezoId;
50
- const detailedAssets = [
51
- ["BTC", btcBalance?.value ?? 0n],
52
- ...Object.entries(tokensBalances ?? {}),
53
- ].map((asset) => {
54
- const [type, balance] = asset;
55
- if (isBitcoinLikeCryptoAsset(type)) {
56
- return mapCryptoAssetToDetails(type, balance, assetsConversionRates.btc.price, assetsConversionRates.btc.decimals);
57
- }
58
- if (type === "mT") {
59
- return mapCryptoAssetToDetails(type, balance, assetsConversionRates.t.price, assetsConversionRates.t.decimals);
60
- }
61
- return mapCryptoAssetToDetails(type, balance, 1n, 0);
62
- });
63
- const btcData = detailedAssets.find(({ type }) => type === "BTC");
64
- const musdData = detailedAssets.find(({ type }) => type === "MUSD");
65
- const otherAssetsData = detailedAssets.filter(({ type }) => !["BTC", "MUSD"].includes(type));
66
- const otherAssetsCount = otherAssetsData.filter((asset) => asset.rawBalance !== 0n).length;
67
- const assets = {
68
- btc: btcData || {
69
- type: "BTC",
70
- ...DEFAULT_ASSET_DATA,
71
- },
72
- musd: musdData || {
73
- type: "mUSD",
74
- ...DEFAULT_ASSET_DATA,
75
- },
76
- };
77
- const otherAssetsUsdTotal = otherAssetsData.reduce((sum, { usdBalance }) => sum + usdBalance, 0);
78
- const matsBalance = passportAccount?.mats.totalMats || 0;
79
- const usdCollateral = convertToUsd(debt?.collateral ?? 0n, getCryptoAsset("MUSD").decimals, assetsConversionRates.btc.price, assetsConversionRates.btc.decimals);
80
- const usdTroveDebt = fromFixedPoint(debt?.troveDebt ?? 0n, 18);
81
- const usdTotalBalance = detailedAssets.reduce((sum, { usdBalance }) => sum + usdBalance, 0) +
82
- usdCollateral;
83
- return {
84
- mezoId,
85
- matsBalance,
86
- accountAddress,
87
- walletAddress,
88
- walletType,
89
- usdTotalBalance,
90
- usdTroveDebt,
91
- usdCollateral,
92
- assets,
93
- otherAssetsCount,
94
- otherAssetsUsdTotal,
95
- };
96
- }
97
- //# sourceMappingURL=useDropdownData.js.map