@mezo-org/passport 0.4.0-dev.9 → 0.5.1-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (305) hide show
  1. package/README.md +18 -22
  2. package/dist/src/api/auth.d.ts +7 -3
  3. package/dist/src/api/auth.d.ts.map +1 -1
  4. package/dist/src/api/auth.js +3 -1
  5. package/dist/src/api/auth.js.map +1 -1
  6. package/dist/src/api/client.d.ts +4 -1
  7. package/dist/src/api/client.d.ts.map +1 -1
  8. package/dist/src/api/client.js +9 -2
  9. package/dist/src/api/client.js.map +1 -1
  10. package/dist/src/api/portal.d.ts +3 -9
  11. package/dist/src/api/portal.d.ts.map +1 -1
  12. package/dist/src/api/portal.js +8 -5
  13. package/dist/src/api/portal.js.map +1 -1
  14. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +2 -1
  15. package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -1
  16. package/dist/src/components/Dropdown/ConnectedTrigger.js +29 -20
  17. package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -1
  18. package/dist/src/components/Dropdown/Content.d.ts +4 -5
  19. package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
  20. package/dist/src/components/Dropdown/Content.js +19 -61
  21. package/dist/src/components/Dropdown/Content.js.map +1 -1
  22. package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -1
  23. package/dist/src/components/Dropdown/DisconnectedTrigger.js +2 -2
  24. package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -1
  25. package/dist/src/components/Dropdown/Dropdown.d.ts +9 -6
  26. package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
  27. package/dist/src/components/Dropdown/Dropdown.js +10 -10
  28. package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
  29. package/dist/src/components/Dropdown/ListingItem.d.ts +14 -0
  30. package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
  31. package/dist/src/components/Dropdown/ListingItem.js +42 -0
  32. package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
  33. package/dist/src/components/Dropdown/NestedViewLayout.d.ts +8 -0
  34. package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
  35. package/dist/src/components/Dropdown/NestedViewLayout.js +32 -0
  36. package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
  37. package/dist/src/components/Dropdown/Receive/Receive.d.ts +4 -0
  38. package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
  39. package/dist/src/components/Dropdown/Receive/Receive.js +64 -0
  40. package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
  41. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +4 -0
  42. package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
  43. package/dist/src/components/Dropdown/Root/AccountAddressActions.js +49 -0
  44. package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
  45. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +6 -0
  46. package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
  47. package/dist/src/components/Dropdown/Root/AccountBalance.js +35 -0
  48. package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
  49. package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts +6 -0
  50. package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts.map +1 -0
  51. package/dist/src/components/Dropdown/Root/AccountBtcListing.js +27 -0
  52. package/dist/src/components/Dropdown/Root/AccountBtcListing.js.map +1 -0
  53. package/dist/src/components/Dropdown/Root/AccountError.d.ts +8 -0
  54. package/dist/src/components/Dropdown/Root/AccountError.d.ts.map +1 -0
  55. package/dist/src/components/Dropdown/Root/AccountError.js +17 -0
  56. package/dist/src/components/Dropdown/Root/AccountError.js.map +1 -0
  57. package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts +4 -0
  58. package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts.map +1 -0
  59. package/dist/src/components/Dropdown/Root/AccountMusdListing.js +21 -0
  60. package/dist/src/components/Dropdown/Root/AccountMusdListing.js.map +1 -0
  61. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +8 -0
  62. package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
  63. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +43 -0
  64. package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
  65. package/dist/src/components/Dropdown/Root/Root.d.ts +8 -0
  66. package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
  67. package/dist/src/components/Dropdown/Root/Root.js +45 -0
  68. package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
  69. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +4 -0
  70. package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
  71. package/dist/src/components/Dropdown/Root/WalletAddress.js +66 -0
  72. package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
  73. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +6 -0
  74. package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
  75. package/dist/src/components/Dropdown/Root/WelcomeBlock.js +88 -0
  76. package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
  77. package/dist/src/components/Dropdown/SlotNumber.d.ts +19 -0
  78. package/dist/src/components/Dropdown/SlotNumber.d.ts.map +1 -0
  79. package/dist/src/components/Dropdown/SlotNumber.js +67 -0
  80. package/dist/src/components/Dropdown/SlotNumber.js.map +1 -0
  81. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
  82. package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
  83. package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
  84. package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
  85. package/dist/src/config.d.ts +19 -5
  86. package/dist/src/config.d.ts.map +1 -1
  87. package/dist/src/config.js +58 -14
  88. package/dist/src/config.js.map +1 -1
  89. package/dist/src/constants.d.ts +11 -3
  90. package/dist/src/constants.d.ts.map +1 -1
  91. package/dist/src/constants.js +12 -4
  92. package/dist/src/constants.js.map +1 -1
  93. package/dist/src/hooks/index.d.ts +4 -0
  94. package/dist/src/hooks/index.d.ts.map +1 -1
  95. package/dist/src/hooks/index.js +4 -0
  96. package/dist/src/hooks/index.js.map +1 -1
  97. package/dist/src/hooks/useAssetsConversionRates.d.ts +18 -0
  98. package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
  99. package/dist/src/hooks/useAssetsConversionRates.js +67 -0
  100. package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
  101. package/dist/src/hooks/useAuthenticateWithWallet.d.ts +13 -13
  102. package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
  103. package/dist/src/hooks/useAuthenticateWithWallet.js +9 -8
  104. package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
  105. package/dist/src/hooks/useBorrowData.d.ts +108 -0
  106. package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
  107. package/dist/src/hooks/useBorrowData.js +201 -0
  108. package/dist/src/hooks/useBorrowData.js.map +1 -0
  109. package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
  110. package/dist/src/hooks/useCreateAccount.js +22 -1
  111. package/dist/src/hooks/useCreateAccount.js.map +1 -1
  112. package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
  113. package/dist/src/hooks/useGetCurrentAccount.js +4 -6
  114. package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
  115. package/dist/src/hooks/useLinkAccount.d.ts +19 -13
  116. package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
  117. package/dist/src/hooks/useLinkAccount.js +20 -1
  118. package/dist/src/hooks/useLinkAccount.js.map +1 -1
  119. package/dist/src/hooks/useRefreshPassport.d.ts +19 -0
  120. package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
  121. package/dist/src/hooks/useRefreshPassport.js +44 -0
  122. package/dist/src/hooks/useRefreshPassport.js.map +1 -0
  123. package/dist/src/hooks/useSignInWithWallet.d.ts +13 -13
  124. package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
  125. package/dist/src/hooks/useSignUpWithWallet.d.ts +13 -13
  126. package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -1
  127. package/dist/src/hooks/useTokensBalances.d.ts +75 -0
  128. package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
  129. package/dist/src/hooks/useTokensBalances.js +181 -0
  130. package/dist/src/hooks/useTokensBalances.js.map +1 -0
  131. package/dist/src/hooks/useValidateMezoId.d.ts +8 -0
  132. package/dist/src/hooks/useValidateMezoId.d.ts.map +1 -0
  133. package/dist/src/hooks/useValidateMezoId.js +24 -0
  134. package/dist/src/hooks/useValidateMezoId.js.map +1 -0
  135. package/dist/src/hooks/useWalletAccount.d.ts +2 -1
  136. package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
  137. package/dist/src/hooks/useWalletAccount.js +20 -15
  138. package/dist/src/hooks/useWalletAccount.js.map +1 -1
  139. package/dist/src/hooks/useWatchTransferEvents.d.ts +5 -0
  140. package/dist/src/hooks/useWatchTransferEvents.d.ts.map +1 -0
  141. package/dist/src/hooks/useWatchTransferEvents.js +63 -0
  142. package/dist/src/hooks/useWatchTransferEvents.js.map +1 -0
  143. package/dist/src/index.d.ts +2 -1
  144. package/dist/src/index.d.ts.map +1 -1
  145. package/dist/src/index.js +2 -1
  146. package/dist/src/index.js.map +1 -1
  147. package/dist/src/lib/contracts/index.d.ts +13 -0
  148. package/dist/src/lib/contracts/index.d.ts.map +1 -0
  149. package/dist/src/lib/contracts/index.js +66 -0
  150. package/dist/src/lib/contracts/index.js.map +1 -0
  151. package/dist/src/lib/contracts/priceOracle.d.ts +43 -0
  152. package/dist/src/lib/contracts/priceOracle.d.ts.map +1 -0
  153. package/dist/src/lib/contracts/priceOracle.js +52 -0
  154. package/dist/src/lib/contracts/priceOracle.js.map +1 -0
  155. package/dist/src/provider.d.ts +7 -1
  156. package/dist/src/provider.d.ts.map +1 -1
  157. package/dist/src/provider.js +4 -1
  158. package/dist/src/provider.js.map +1 -1
  159. package/dist/src/stores/dropdownStore.d.ts +12 -0
  160. package/dist/src/stores/dropdownStore.d.ts.map +1 -0
  161. package/dist/src/stores/dropdownStore.js +13 -0
  162. package/dist/src/stores/dropdownStore.js.map +1 -0
  163. package/dist/src/utils/address.d.ts +1 -1
  164. package/dist/src/utils/address.d.ts.map +1 -1
  165. package/dist/src/utils/address.js +2 -0
  166. package/dist/src/utils/address.js.map +1 -1
  167. package/dist/src/utils/address.test.js +8 -5
  168. package/dist/src/utils/address.test.js.map +1 -1
  169. package/dist/src/utils/assets.d.ts +145 -0
  170. package/dist/src/utils/assets.d.ts.map +1 -0
  171. package/dist/src/utils/assets.js +100 -0
  172. package/dist/src/utils/assets.js.map +1 -0
  173. package/dist/src/utils/assets.test.d.ts +2 -0
  174. package/dist/src/utils/assets.test.d.ts.map +1 -0
  175. package/dist/src/utils/assets.test.js +46 -0
  176. package/dist/src/utils/assets.test.js.map +1 -0
  177. package/dist/src/utils/currency.d.ts +13 -2
  178. package/dist/src/utils/currency.d.ts.map +1 -1
  179. package/dist/src/utils/currency.js +22 -8
  180. package/dist/src/utils/currency.js.map +1 -1
  181. package/dist/src/utils/currency.test.js +44 -1
  182. package/dist/src/utils/currency.test.js.map +1 -1
  183. package/dist/src/utils/mezoId.d.ts +7 -0
  184. package/dist/src/utils/mezoId.d.ts.map +1 -0
  185. package/dist/src/utils/mezoId.js +41 -0
  186. package/dist/src/utils/mezoId.js.map +1 -0
  187. package/dist/src/utils/number2.d.ts +106 -0
  188. package/dist/src/utils/number2.d.ts.map +1 -0
  189. package/dist/src/utils/number2.js +289 -0
  190. package/dist/src/utils/number2.js.map +1 -0
  191. package/dist/src/utils/numbers.d.ts +15 -33
  192. package/dist/src/utils/numbers.d.ts.map +1 -1
  193. package/dist/src/utils/numbers.js +26 -70
  194. package/dist/src/utils/numbers.js.map +1 -1
  195. package/dist/src/utils/numbers.test.js +46 -42
  196. package/dist/src/utils/numbers.test.js.map +1 -1
  197. package/dist/src/utils/siww.d.ts +1 -0
  198. package/dist/src/utils/siww.d.ts.map +1 -1
  199. package/dist/src/utils/siww.js +50 -12
  200. package/dist/src/utils/siww.js.map +1 -1
  201. package/dist/src/utils/wagmi.d.ts +3 -0
  202. package/dist/src/utils/wagmi.d.ts.map +1 -0
  203. package/dist/src/utils/wagmi.js +7 -0
  204. package/dist/src/utils/wagmi.js.map +1 -0
  205. package/dist/src/wallet/index.d.ts +1 -1
  206. package/dist/src/wallet/index.d.ts.map +1 -1
  207. package/dist/src/wallet/index.js +1 -1
  208. package/dist/src/wallet/index.js.map +1 -1
  209. package/package.json +11 -8
  210. package/src/api/auth.ts +8 -4
  211. package/src/api/client.ts +11 -2
  212. package/src/api/portal.ts +11 -14
  213. package/src/components/Dropdown/ConnectedTrigger.tsx +52 -32
  214. package/src/components/Dropdown/Content.tsx +26 -121
  215. package/src/components/Dropdown/DisconnectedTrigger.tsx +2 -1
  216. package/src/components/Dropdown/Dropdown.tsx +29 -17
  217. package/src/components/Dropdown/ListingItem.tsx +176 -0
  218. package/src/components/Dropdown/NestedViewLayout.tsx +87 -0
  219. package/src/components/Dropdown/README.md +10 -18
  220. package/src/components/Dropdown/Receive/Receive.tsx +144 -0
  221. package/src/components/Dropdown/Root/AccountAddressActions.tsx +99 -0
  222. package/src/components/Dropdown/Root/AccountBalance.tsx +75 -0
  223. package/src/components/Dropdown/Root/AccountBtcListing.tsx +52 -0
  224. package/src/components/Dropdown/Root/AccountError.tsx +34 -0
  225. package/src/components/Dropdown/Root/AccountMusdListing.tsx +45 -0
  226. package/src/components/Dropdown/Root/AccountOtherAssets.tsx +85 -0
  227. package/src/components/Dropdown/Root/Root.tsx +77 -0
  228. package/src/components/Dropdown/Root/WalletAddress.tsx +123 -0
  229. package/src/components/Dropdown/Root/WelcomeBlock.tsx +173 -0
  230. package/src/components/Dropdown/SlotNumber.tsx +131 -0
  231. package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
  232. package/src/config.ts +88 -20
  233. package/src/constants.ts +12 -4
  234. package/src/hooks/index.ts +7 -0
  235. package/src/hooks/useAssetsConversionRates.ts +79 -0
  236. package/src/hooks/useAuthenticateWithWallet.ts +30 -14
  237. package/src/hooks/useBorrowData.ts +246 -0
  238. package/src/hooks/useCreateAccount.ts +29 -2
  239. package/src/hooks/useGetCurrentAccount.ts +5 -7
  240. package/src/hooks/useLinkAccount.ts +37 -4
  241. package/src/hooks/useRefreshPassport.ts +56 -0
  242. package/src/hooks/useSignInWithWallet.ts +2 -2
  243. package/src/hooks/useSignUpWithWallet.ts +2 -2
  244. package/src/hooks/useTokensBalances.ts +265 -0
  245. package/src/hooks/useValidateMezoId.ts +31 -0
  246. package/src/hooks/useWalletAccount.ts +32 -20
  247. package/src/hooks/useWatchTransferEvents.ts +74 -0
  248. package/src/index.ts +12 -1
  249. package/src/lib/contracts/index.ts +99 -0
  250. package/src/lib/contracts/priceOracle.ts +53 -0
  251. package/src/provider.ts +11 -3
  252. package/src/stores/dropdownStore.ts +20 -0
  253. package/src/utils/address.test.ts +10 -6
  254. package/src/utils/address.ts +5 -3
  255. package/src/utils/assets.test.ts +57 -0
  256. package/src/utils/assets.ts +103 -0
  257. package/src/utils/currency.test.ts +77 -1
  258. package/src/utils/currency.ts +35 -9
  259. package/src/utils/mezoId.ts +51 -0
  260. package/src/utils/number2.ts +419 -0
  261. package/src/utils/numbers.test.ts +49 -42
  262. package/src/utils/numbers.ts +33 -95
  263. package/src/utils/siww.ts +71 -16
  264. package/src/utils/wagmi.ts +12 -0
  265. package/src/wallet/index.ts +3 -2
  266. package/dist/src/components/Dropdown/AccountAddress.d.ts +0 -8
  267. package/dist/src/components/Dropdown/AccountAddress.d.ts.map +0 -1
  268. package/dist/src/components/Dropdown/AccountAddress.js +0 -58
  269. package/dist/src/components/Dropdown/AccountAddress.js.map +0 -1
  270. package/dist/src/components/Dropdown/AccountAssets.d.ts +0 -14
  271. package/dist/src/components/Dropdown/AccountAssets.d.ts.map +0 -1
  272. package/dist/src/components/Dropdown/AccountAssets.js +0 -44
  273. package/dist/src/components/Dropdown/AccountAssets.js.map +0 -1
  274. package/dist/src/components/Dropdown/AccountBalance.d.ts +0 -7
  275. package/dist/src/components/Dropdown/AccountBalance.d.ts.map +0 -1
  276. package/dist/src/components/Dropdown/AccountBalance.js +0 -18
  277. package/dist/src/components/Dropdown/AccountBalance.js.map +0 -1
  278. package/dist/src/components/Dropdown/WelcomeBlock.d.ts +0 -8
  279. package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +0 -1
  280. package/dist/src/components/Dropdown/WelcomeBlock.js +0 -44
  281. package/dist/src/components/Dropdown/WelcomeBlock.js.map +0 -1
  282. package/dist/src/hooks/useAssetsUSDConversion.d.ts +0 -8
  283. package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +0 -1
  284. package/dist/src/hooks/useAssetsUSDConversion.js +0 -21
  285. package/dist/src/hooks/useAssetsUSDConversion.js.map +0 -1
  286. package/dist/src/hooks/useDropdownData.d.ts +0 -44
  287. package/dist/src/hooks/useDropdownData.d.ts.map +0 -1
  288. package/dist/src/hooks/useDropdownData.js +0 -73
  289. package/dist/src/hooks/useDropdownData.js.map +0 -1
  290. package/dist/src/utils/cryptoAssets.d.ts +0 -28
  291. package/dist/src/utils/cryptoAssets.d.ts.map +0 -1
  292. package/dist/src/utils/cryptoAssets.js +0 -73
  293. package/dist/src/utils/cryptoAssets.js.map +0 -1
  294. package/dist/src/utils/cryptoAssets.test.d.ts +0 -2
  295. package/dist/src/utils/cryptoAssets.test.d.ts.map +0 -1
  296. package/dist/src/utils/cryptoAssets.test.js +0 -49
  297. package/dist/src/utils/cryptoAssets.test.js.map +0 -1
  298. package/src/components/Dropdown/AccountAddress.tsx +0 -111
  299. package/src/components/Dropdown/AccountAssets.tsx +0 -110
  300. package/src/components/Dropdown/AccountBalance.tsx +0 -38
  301. package/src/components/Dropdown/WelcomeBlock.tsx +0 -92
  302. package/src/hooks/useAssetsUSDConversion.ts +0 -31
  303. package/src/hooks/useDropdownData.ts +0 -130
  304. package/src/utils/cryptoAssets.test.ts +0 -59
  305. package/src/utils/cryptoAssets.ts +0 -93
@@ -0,0 +1,289 @@
1
+ // Matches floating point numbers with optional thousands separators
2
+ export const FLOATING_POINT_REGEX = /^[^0-9]*([0-9,]+)(?:\.([0-9]*))?$/;
3
+ const FRIENDLY_DECIMALS_AMOUNT = 6;
4
+ /**
5
+ * HELPER FUNCTIONS
6
+ * These functions are used to convert between floating-point numbers and fixed-point numbers.
7
+ * They handle parsing, conversion, and formatting of numbers with specific decimal places.
8
+ * They shouldn't be used directly in the UI or business logic.
9
+ */
10
+ /**
11
+ * Parses a floating-point string into a fixed-point number representation.
12
+ * @param floatingPointString The floating-point string to parse.
13
+ * @returns The fixed-point number representation or undefined if parsing fails.
14
+ */
15
+ const parseToFixedPointNumber = (floatingPointString) => {
16
+ if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
17
+ return undefined;
18
+ }
19
+ const [whole, decimals, ...extra] = floatingPointString.split(".");
20
+ // Only one `.` supported.
21
+ if (extra.length > 0) {
22
+ return undefined;
23
+ }
24
+ const noThousandsSeparatorWhole = whole.replace(",", "");
25
+ const setDecimals = decimals ?? "";
26
+ try {
27
+ return {
28
+ amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
29
+ decimals: setDecimals.length,
30
+ };
31
+ }
32
+ catch (error) {
33
+ return undefined;
34
+ }
35
+ };
36
+ /**
37
+ * Separates thousands in a number with commas.
38
+ * @param value The value to format.
39
+ * @param minDecimals The minimum number of decimal places.
40
+ * @param maxDecimals The maximum number of decimal places.
41
+ * @returns The formatted value with thousands separators.
42
+ */
43
+ const separateThousandsWithComma = (value, minDecimals = 0, maxDecimals = 2) => {
44
+ let adjustedValue = value;
45
+ let adjustedMaxDecimals = maxDecimals;
46
+ // If passed value is not numeric we should return 0
47
+ if (Number.isNaN(+value.toString())) {
48
+ adjustedValue = 0;
49
+ }
50
+ if (typeof adjustedValue === "string") {
51
+ adjustedValue = +adjustedValue;
52
+ }
53
+ if (minDecimals > maxDecimals) {
54
+ adjustedMaxDecimals = minDecimals;
55
+ }
56
+ // Ensure maximumFractionDigits is within allowed range [0, 20]
57
+ const safeMaxDecimals = Math.max(0, Math.min(adjustedMaxDecimals, 20));
58
+ // @ts-expect-error - `maximumFractionDigits` wants to get number less than 21,
59
+ // but as the tokens have 18 decimals have, we can safely pass a parameter
60
+ return adjustedValue.toLocaleString("en-US", {
61
+ minimumFractionDigits: minDecimals,
62
+ maximumFractionDigits: safeMaxDecimals,
63
+ roundingMode: "halfExpand",
64
+ });
65
+ };
66
+ /** PUBLIC HELPER FUNCTIONS for calculations */
67
+ /**
68
+ * Multiplies a bigint with a decimal number, taking into account the decimal places.
69
+ *
70
+ * @param bigint - The bigint value to multiply.
71
+ * @param number - The decimal number to multiply with.
72
+ * @returns The result of the multiplication as a bigint.
73
+ */
74
+ export const multiplyBigIntWithDecimal = (bigint, number) => {
75
+ const numberString = number.toString();
76
+ const scientificMatch = /^1e-(\d+)$/.exec(numberString);
77
+ // Ensure we can correctly deal with scientific number expression
78
+ if (scientificMatch) {
79
+ const exponent = parseInt(scientificMatch[1], 10);
80
+ const scale = BigInt(10 ** exponent);
81
+ return (bigint * 1n) / scale;
82
+ }
83
+ const decimalsPart = number.toString().split(".")[1];
84
+ const decimalsCount = decimalsPart ? decimalsPart.length : 0;
85
+ if (!decimalsCount)
86
+ return bigint * BigInt(number);
87
+ const scale = BigInt(10 ** decimalsCount);
88
+ const scaledNumber = BigInt(Math.round(number * Number(scale)));
89
+ return (bigint * scaledNumber) / scale;
90
+ };
91
+ /**
92
+ * Converts a fixed-point number to another fixed-point number with a different decimal precision.
93
+ * @param args The arguments for the conversion.
94
+ * - amount: required, the fixed-point number to convert.
95
+ * - decimals: required, the current decimal precision of the number.
96
+ * - targetDecimals: required, the target decimal precision for the conversion.
97
+ * @returns The converted fixed-point number.
98
+ */
99
+ export const convertDecimals = ({ amount, decimals, targetDecimals, }) => {
100
+ if (decimals >= targetDecimals) {
101
+ return amount / 10n ** BigInt(decimals - targetDecimals);
102
+ }
103
+ return amount * 10n ** BigInt(targetDecimals - decimals);
104
+ };
105
+ /** Helper functions for input components */
106
+ /**
107
+ * Trims decimal places to a specified precision - NO ROUNDING!
108
+ * @param value The value to trim
109
+ * @param precision The number of decimal places to keep
110
+ * @returns The trimmed value
111
+ */
112
+ export const trimDecimals = (value, precision) => {
113
+ const [integerPart, decimalPart = ""] = value.split(".");
114
+ return decimalPart.length > precision
115
+ ? `${integerPart}.${decimalPart.slice(0, precision)}`
116
+ : value;
117
+ };
118
+ /**
119
+ * Trims integer part to a specified length - NO ROUNDING!
120
+ * @param value The value to trim
121
+ * @param precision The number of integer places to keep
122
+ * @returns The trimmed value
123
+ * @deprecated just compare the value to the max allowed value in the input component
124
+ */
125
+ export const trimBeforeDecimals = (value, precision) => {
126
+ const [integerPart, decimalPart = ""] = value.split(".");
127
+ return integerPart.length > precision
128
+ ? `${integerPart.slice(0, precision)}.${decimalPart}`
129
+ : value;
130
+ };
131
+ /**
132
+ * Normalizes a decimal number input by removing invalid characters and preventing multiple dots.
133
+ * @param value The value to normalize
134
+ * @returns The normalized value
135
+ */
136
+ export const normalizeDecimalNumber = (value) => value.replace(/[^0-9.]/g, "").replace(/\.(?=.*\.)/g, ""); // Remove all dots except the first one
137
+ /**
138
+ * CONVERSION FUNCTIONS
139
+ * These functions convert between floating-point numbers and big integers as well as
140
+ * provide human-readable formats.
141
+ *
142
+ * They are used to ensure that numbers are correctly formatted for display and calculations
143
+ * and should be used in the UI and business logic to ensure correct number formatting.
144
+ */
145
+ /** Basic conversion bigint <-> float */
146
+ /**
147
+ * Converts a floating-point number to a big integer representation.
148
+ * @param args The arguments for the conversion.
149
+ * - amount: required, the floating-point number to convert.
150
+ * - decimals: optional, the number of decimal places to consider.
151
+ * @returns The big integer representation of the floating-point number.
152
+ */
153
+ export const floatToBigInt = ({ amount, decimals = 18, }) => {
154
+ const parsedAmount = amount.toString();
155
+ const fixedPointAmount = parseToFixedPointNumber(parsedAmount);
156
+ if (typeof fixedPointAmount === "undefined") {
157
+ return 0n;
158
+ }
159
+ const bigIntAmount = convertDecimals({
160
+ amount: fixedPointAmount.amount,
161
+ decimals: fixedPointAmount.decimals,
162
+ targetDecimals: decimals,
163
+ });
164
+ return bigIntAmount;
165
+ };
166
+ /**
167
+ * Converts a big integer representation to a floating-point number.
168
+ * @param args The arguments for the conversion.
169
+ * - amount: required, the big integer representation to convert.
170
+ * - decimals: optional, the number of decimal places to consider.
171
+ * - desiredDecimals: optional, the desired number of decimal places for the output.
172
+ * @returns The floating-point representation of the big integer.
173
+ */
174
+ export const bigIntToFloat = ({ amount, decimals = 18, desiredDecimals = decimals, }) => {
175
+ let isNegative = false;
176
+ let normalizedAmount = amount;
177
+ if (amount < 0n) {
178
+ normalizedAmount = -amount;
179
+ isNegative = true;
180
+ }
181
+ const desiredDecimalsAmount = normalizedAmount / 10n ** BigInt(Math.max(0, decimals - desiredDecimals));
182
+ // Desired decimals amount with additional decimal to check if number
183
+ // should be rounded up
184
+ const desiredDecimalsAmountWithAdditionalDecimal = normalizedAmount /
185
+ 10n ** BigInt(Math.max(0, decimals - desiredDecimals - 1));
186
+ // We are getting the first value that has been cut off
187
+ const firstDecimalAfterCutOff = desiredDecimalsAmountWithAdditionalDecimal
188
+ .toString()
189
+ .at(-1);
190
+ // Padding amount when its length is smaller than desired
191
+ const parsedDesiredAmount = desiredDecimalsAmount
192
+ .toString()
193
+ .padStart(desiredDecimals + 1, "0");
194
+ const integersPart = parsedDesiredAmount.slice(0, -desiredDecimals);
195
+ const decimalsPart = parsedDesiredAmount.slice(-desiredDecimals);
196
+ // If number is above or equal to 5 we should round up the number
197
+ const shouldIncreaseDecimalsPart = Number(firstDecimalAfterCutOff) >= 5 && desiredDecimals !== decimals;
198
+ // If above is true we add 1 to the decimals value and convert it back to string
199
+ const adjustedDecimalsPart = shouldIncreaseDecimalsPart
200
+ ? (Number(decimalsPart) + 1).toString().padStart(desiredDecimals, "0")
201
+ : decimalsPart;
202
+ // Determining whether integer part needs to be increased due to rounding up
203
+ const shouldIncreaseIntegerPart = (decimalsPart === "0" && adjustedDecimalsPart !== decimalsPart) ||
204
+ adjustedDecimalsPart.length > decimalsPart.length;
205
+ // If above is true we add 1 to the integers value and convert it back to string
206
+ // and set first value of decimals part to 0
207
+ const adjustedIntegersPart = shouldIncreaseIntegerPart
208
+ ? (Number(integersPart) + 1).toString()
209
+ : integersPart;
210
+ const decimalsPartAfterIntegersAdjustments = shouldIncreaseIntegerPart
211
+ ? `0${adjustedDecimalsPart.slice(1)}`
212
+ : adjustedDecimalsPart;
213
+ // Inserting "." to separate integers part from decimal part
214
+ const desiredAmount = [
215
+ ...adjustedIntegersPart,
216
+ ".",
217
+ ...decimalsPartAfterIntegersAdjustments,
218
+ ].join("");
219
+ // Remove trailing zeros and the trailing decimal point (if exists)
220
+ const trimmedDesiredAmount = desiredAmount.replace(/(?:\.0*|(\.\d+?)0+)$/, "$1");
221
+ return isNegative ? `-${trimmedDesiredAmount}` : trimmedDesiredAmount;
222
+ };
223
+ /**
224
+ * Helpers for setting correct arguments and getting unified human-readable
225
+ * formats across the app with _toHumanReadableFormat functions.
226
+ */
227
+ export const amountAsUSD = (args) => ({
228
+ desiredDecimals: 2,
229
+ minDecimals: 2,
230
+ ...args,
231
+ });
232
+ export const amountAsToken = (args) => ({
233
+ desiredDecimals: FRIENDLY_DECIMALS_AMOUNT,
234
+ minDecimals: 0,
235
+ ...args,
236
+ });
237
+ export const amountAsMusd = (args) => ({
238
+ desiredDecimals: 2,
239
+ minDecimals: 2,
240
+ decimals: 18, // MUSD has 18 decimals
241
+ ...args,
242
+ });
243
+ export const amountAsMats = (args) => ({
244
+ desiredDecimals: 0,
245
+ minDecimals: 0,
246
+ ...args,
247
+ });
248
+ /**
249
+ * Converts a floating-point number to a human-readable format - with thousands
250
+ * separators and specified decimal places.
251
+ * @param args The arguments for the conversion.
252
+ * - amount: required, the floating-point number to convert.
253
+ * - desiredDecimals: optional, the desired number of decimal places for the output.
254
+ * - minDecimals: optional, the minimum number of decimal places.
255
+ * @returns The human-readable format of the floating-point number.
256
+ */
257
+ export const floatToHumanReadableFormat = ({ amount, desiredDecimals = 2, // treat is as an USD amount by default
258
+ minDecimals = 0, }) => separateThousandsWithComma(amount, minDecimals, desiredDecimals);
259
+ export const bigIntToHumanReadableFormat = ({ amount, decimals = 18, desiredDecimals = FRIENDLY_DECIMALS_AMOUNT, // treat is as a token by default
260
+ minDecimals = 0, }) => {
261
+ let isNegative = false;
262
+ let normalizedAmount = amount;
263
+ let adjustedDesiredDecimals = desiredDecimals;
264
+ if (minDecimals > desiredDecimals) {
265
+ adjustedDesiredDecimals = minDecimals;
266
+ }
267
+ if (normalizedAmount < 0n) {
268
+ normalizedAmount = -normalizedAmount;
269
+ isNegative = true;
270
+ }
271
+ const trimmedDesiredAmount = bigIntToFloat({
272
+ amount: normalizedAmount,
273
+ decimals,
274
+ desiredDecimals: adjustedDesiredDecimals,
275
+ });
276
+ if (trimmedDesiredAmount === "0" && normalizedAmount !== 0n) {
277
+ const decimalsString = "1".padStart(adjustedDesiredDecimals, "0");
278
+ return `<0.${decimalsString}`;
279
+ }
280
+ const desiredValue = isNegative
281
+ ? `-${trimmedDesiredAmount}`
282
+ : trimmedDesiredAmount;
283
+ return floatToHumanReadableFormat({
284
+ amount: desiredValue,
285
+ desiredDecimals: adjustedDesiredDecimals,
286
+ minDecimals,
287
+ });
288
+ };
289
+ //# sourceMappingURL=number2.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number2.js","sourceRoot":"","sources":["../../../src/utils/number2.ts"],"names":[],"mappings":"AAAA,oEAAoE;AACpE,MAAM,CAAC,MAAM,oBAAoB,GAAG,mCAAmC,CAAA;AACvE,MAAM,wBAAwB,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Ae,EAAE,uBAAuB;QACxC,WAAW;KACZ,CAAC,CAAA;AACJ,CAAC,CAAA"}
@@ -1,43 +1,25 @@
1
+ type CompactStringDecimals = Intl.NumberFormatOptions["minimumFractionDigits"] & BigIntToLocaleStringOptions["minimumFractionDigits"];
1
2
  /**
2
- * Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
3
+ * Formats a number or bigint into compact string with K, M, B, or T suffix.
3
4
  * @param value The number or bigint to format.
4
5
  * @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
5
6
  * @returns The formatted number as a string.
6
7
  */
7
- export declare function formatHumanReadableNumber(value: number | bigint, decimals?: number): string;
8
+ export declare function formatNumberToCompactString(value: number | bigint, decimals?: CompactStringDecimals): string;
8
9
  /**
9
- * Returns a number rounded up to the desired number of decimals.
10
- * @param amount The number to round up.
11
- * @param desiredDecimals The number of decimals to round up to.
12
- * @returns The rounded up number.
13
- * @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L1-L3
10
+ * Normalizes a bigint value to a target precision.
11
+ * @param value The bigint value to normalize.
12
+ * @param currentPrecision The current precision of the value.
13
+ * @param targetPrecision The target precision to normalize to.
14
+ * @returns The normalized bigint value.
14
15
  */
15
- export declare function roundUpNumber(amount: number, desiredDecimals?: number): number;
16
+ export declare const normalizePrecision: (value: bigint, currentPrecision: number, targetPrecision?: number) => bigint;
16
17
  /**
17
- * Convert a fixed point bigint with precision `fixedPointDecimals` to a
18
- * floating point number truncated to `desiredDecimals`.
19
- * @param fixedPoint The fixed point bigint to convert.
20
- * @param fixedPointDecimals The number of decimals in the fixed point bigint.
21
- * @param desiredDecimals The number of decimals in the output number.
22
- * @returns The floating point number truncated to `desiredDecimals`.
23
- * @see https://github.com/tahowallet/extension/blob/main/background/lib/fixed-point.ts#L216-L239
18
+ * Utility function to find the maximum value from given `BigInt` values.
19
+ * @param values Array of BigInt values to find the maximum from.
20
+ * @returns The maximum value from the provided `BigInt` values.
21
+ * @dev Works exactly like `Math.max` but for `BigInt` values.
24
22
  */
25
- export declare function fromFixedPoint(fixedPoint: bigint, fixedPointDecimals: number, desiredDecimals?: number): number;
26
- /**
27
- * Display a cryptoAsset amount correctly with desired decimals.
28
- * The function returns a string with a language-sensitive representation of this number.
29
- * - If the amount entered is zero, return the result with the desired decimals.
30
- * For example, 0.00 for a precision of 2.
31
- * - If `formattedAmount` is less than the minimum amount to display
32
- * for the specified precision return information about this.
33
- * For example, <0.01 for a precision of 2.
34
- * - Other amounts are formatted according to the use of the `bigIntToUserAmount` function.
35
- * @param amount The amount of tokens to format.
36
- * @param decimals The number of decimals the cryptoAsset has.
37
- * @param desiredDecimals The number of decimals to display.
38
- * @param withRoundUp If true, the amount will be rounded up.
39
- * @returns The formatted cryptoAsset amount.
40
- * @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L57-L87
41
- */
42
- export declare function formatTokenAmount(amount: number | string | bigint, decimals?: number, desiredDecimals?: number, withRoundUp?: boolean): string;
23
+ export declare const bigIntMax: (...values: bigint[]) => bigint;
24
+ export {};
43
25
  //# sourceMappingURL=numbers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,QAAQ,GAAE,MAAU,GACnB,MAAM,CAgBR;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,EAAE,eAAe,SAAI,GAAG,MAAM,CAEzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAClB,kBAAkB,EAAE,MAAM,EAC1B,eAAe,SAAI,GAClB,MAAM,CAUR;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,EAChC,QAAQ,SAAK,EACb,eAAe,SAAI,EACnB,WAAW,UAAQ,UA0BpB"}
1
+ {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAAA,KAAK,qBAAqB,GAAG,IAAI,CAAC,mBAAmB,CAAC,uBAAuB,CAAC,GAC5E,2BAA2B,CAAC,uBAAuB,CAAC,CAAA;AACtD;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,QAAQ,GAAE,qBAAyB,GAClC,MAAM,CAMR;AAED;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,GAC7B,OAAO,MAAM,EACb,kBAAkB,MAAM,EACxB,wBAAkC,KACjC,MAQF,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,GAAG,QAAQ,MAAM,EAAE,KAAG,MACY,CAAA"}
@@ -1,81 +1,37 @@
1
1
  /**
2
- * Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
2
+ * Formats a number or bigint into compact string with K, M, B, or T suffix.
3
3
  * @param value The number or bigint to format.
4
4
  * @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
5
5
  * @returns The formatted number as a string.
6
6
  */
7
- export function formatHumanReadableNumber(value, decimals = 4) {
8
- const suffixes = ["", "K", "M", "B", "T"];
9
- const numericValue = typeof value === "bigint" ? Number(value) : value;
10
- if (numericValue < 1000)
11
- return numericValue.toFixed(decimals);
12
- const magnitude = Math.floor(Math.log10(numericValue) / 3);
13
- if (magnitude >= suffixes.length) {
14
- const max = 10 ** ((suffixes.length - 1) * 3) * 1000 - 1;
15
- const capped = max / 10 ** ((suffixes.length - 1) * 3);
16
- return `> ${capped.toFixed(0)}${suffixes[suffixes.length - 1]}`;
17
- }
18
- const scaledValue = numericValue / 10 ** (magnitude * 3);
19
- return `${scaledValue.toFixed(decimals)}${suffixes[magnitude]}`;
20
- }
21
- /**
22
- * Returns a number rounded up to the desired number of decimals.
23
- * @param amount The number to round up.
24
- * @param desiredDecimals The number of decimals to round up to.
25
- * @returns The rounded up number.
26
- * @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L1-L3
27
- */
28
- export function roundUpNumber(amount, desiredDecimals = 2) {
29
- return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals;
30
- }
31
- /**
32
- * Convert a fixed point bigint with precision `fixedPointDecimals` to a
33
- * floating point number truncated to `desiredDecimals`.
34
- * @param fixedPoint The fixed point bigint to convert.
35
- * @param fixedPointDecimals The number of decimals in the fixed point bigint.
36
- * @param desiredDecimals The number of decimals in the output number.
37
- * @returns The floating point number truncated to `desiredDecimals`.
38
- * @see https://github.com/tahowallet/extension/blob/main/background/lib/fixed-point.ts#L216-L239
39
- */
40
- export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals = 2) {
41
- const fixedPointDesiredDecimalsAmount = fixedPoint /
42
- 10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals));
43
- const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
44
- 10 ** Math.min(desiredDecimals, fixedPointDecimals);
45
- return formattedAmount;
7
+ export function formatNumberToCompactString(value, decimals = 2) {
8
+ return value.toLocaleString("en-US", {
9
+ notation: "compact",
10
+ compactDisplay: "short",
11
+ maximumFractionDigits: decimals,
12
+ });
46
13
  }
47
14
  /**
48
- * Display a cryptoAsset amount correctly with desired decimals.
49
- * The function returns a string with a language-sensitive representation of this number.
50
- * - If the amount entered is zero, return the result with the desired decimals.
51
- * For example, 0.00 for a precision of 2.
52
- * - If `formattedAmount` is less than the minimum amount to display
53
- * for the specified precision return information about this.
54
- * For example, <0.01 for a precision of 2.
55
- * - Other amounts are formatted according to the use of the `bigIntToUserAmount` function.
56
- * @param amount The amount of tokens to format.
57
- * @param decimals The number of decimals the cryptoAsset has.
58
- * @param desiredDecimals The number of decimals to display.
59
- * @param withRoundUp If true, the amount will be rounded up.
60
- * @returns The formatted cryptoAsset amount.
61
- * @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L57-L87
15
+ * Normalizes a bigint value to a target precision.
16
+ * @param value The bigint value to normalize.
17
+ * @param currentPrecision The current precision of the value.
18
+ * @param targetPrecision The target precision to normalize to.
19
+ * @returns The normalized bigint value.
62
20
  */
63
- export function formatTokenAmount(amount, decimals = 18, desiredDecimals = 2, withRoundUp = false) {
64
- const fixedPoint = BigInt(amount);
65
- if (fixedPoint === 0n) {
66
- return `0.${"0".repeat(desiredDecimals)}`;
21
+ export const normalizePrecision = (value, currentPrecision, targetPrecision = currentPrecision) => {
22
+ if (currentPrecision > targetPrecision) {
23
+ return value / 10n ** BigInt(currentPrecision - targetPrecision);
67
24
  }
68
- const formattedAmount = fromFixedPoint(fixedPoint, decimals,
69
- // To round the amount up, let's increase the precision by one.
70
- // The desired decimal numbers will be set later anyway.
71
- withRoundUp ? desiredDecimals + 1 : desiredDecimals);
72
- const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals);
73
- if (minAmountToDisplay > formattedAmount) {
74
- return `<0.${"0".repeat(desiredDecimals - 1)}1`;
25
+ if (currentPrecision < targetPrecision) {
26
+ return value * 10n ** BigInt(targetPrecision - currentPrecision);
75
27
  }
76
- const finalFormattedAmount = withRoundUp
77
- ? roundUpNumber(formattedAmount, desiredDecimals)
78
- : formattedAmount;
79
- return formatHumanReadableNumber(finalFormattedAmount, desiredDecimals);
80
- }
28
+ return value;
29
+ };
30
+ /**
31
+ * Utility function to find the maximum value from given `BigInt` values.
32
+ * @param values Array of BigInt values to find the maximum from.
33
+ * @returns The maximum value from the provided `BigInt` values.
34
+ * @dev Works exactly like `Math.max` but for `BigInt` values.
35
+ */
36
+ export const bigIntMax = (...values) => values.reduce((max, value) => (value > max ? value : max));
81
37
  //# sourceMappingURL=numbers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAAA;;;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QACtC,CAAC,CAAC,aAAa,CAAC,eAAe,EAAE,eAAe,CAAC;QACjD,CAAC,CAAC,eAAe,CAAA;IACnB,OAAO,yBAAyB,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAA;AACzE,CAAC"}
1
+ {"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CACzC,KAAsB,EACtB,WAAkC,CAAC;IAEnC,OAAO,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE;QACnC,QAAQ,EAAE,SAAS;QACnB,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,QAAQ;KAChC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAChC,KAAa,EACb,gBAAwB,EACxB,eAAe,GAAG,gBAAgB,EAC1B,EAAE;IACV,IAAI,gBAAgB,GAAG,eAAe,EAAE,CAAC;QACvC,OAAO,KAAK,GAAG,GAAG,IAAI,MAAM,CAAC,gBAAgB,GAAG,eAAe,CAAC,CAAA;IAClE,CAAC;IACD,IAAI,gBAAgB,GAAG,eAAe,EAAE,CAAC;QACvC,OAAO,KAAK,GAAG,GAAG,IAAI,MAAM,CAAC,eAAe,GAAG,gBAAgB,CAAC,CAAA;IAClE,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,GAAG,MAAgB,EAAU,EAAE,CACvD,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA"}
@@ -1,68 +1,72 @@
1
- import { formatHumanReadableNumber, formatTokenAmount, fromFixedPoint, roundUpNumber, } from "./numbers";
2
- describe("formatHumanReadableNumber", () => {
1
+ import { formatNumberToCompactString, bigIntMax, normalizePrecision, } from "./numbers";
2
+ describe("formatNumberToCompactString", () => {
3
3
  it("formats number less than 1000 with decimals", () => {
4
- expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46");
4
+ expect(formatNumberToCompactString(123.456789, 2)).toBe("123.46");
5
5
  });
6
6
  it("formats thousands with K", () => {
7
- expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K");
7
+ expect(formatNumberToCompactString(12_345, 2)).toBe("12.35K");
8
8
  });
9
9
  it("formats millions with M", () => {
10
- expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M");
10
+ expect(formatNumberToCompactString(12_345_678, 2)).toBe("12.35M");
11
11
  });
12
12
  it("formats billions with B", () => {
13
- expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B");
13
+ expect(formatNumberToCompactString(12_345_678_901, 2)).toBe("12.35B");
14
14
  });
15
15
  it("formats trillions with T", () => {
16
- expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T");
16
+ expect(formatNumberToCompactString(12_345_678_901_234, 2)).toBe("12.35T");
17
17
  });
18
18
  it("formats bigint as readable number", () => {
19
- expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe("> 1000T");
19
+ expect(formatNumberToCompactString(1234567890123456789n, 2)).toBe("1,234,567.89T");
20
20
  });
21
21
  it("caps at > 1000T for huge numbers", () => {
22
22
  const hugeNumber = 10 ** 18; // 1e18
23
- expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("> 1000T");
23
+ expect(formatNumberToCompactString(hugeNumber, 2)).toBe("1,000,000T");
24
24
  });
25
- });
26
- describe("roundUpNumber", () => {
27
- it("rounds up to 2 decimals", () => {
28
- expect(roundUpNumber(1.234)).toBe(1.24);
29
- });
30
- it("rounds up to 3 decimals", () => {
31
- expect(roundUpNumber(9.87654, 3)).toBe(9.877);
32
- });
33
- it("rounds up exact decimal", () => {
34
- expect(roundUpNumber(2.5, 1)).toBe(2.5);
25
+ it("formats numbers with trailing zeros correctly", () => {
26
+ expect(formatNumberToCompactString(1_000, 2)).toBe("1K");
35
27
  });
36
28
  });
37
- describe("fromFixedPoint", () => {
38
- it("converts fixed point with 18 decimals to float", () => {
39
- const raw = 1234567890000000000n; // 1.23456789 ETH in 18 decimals
40
- expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345);
29
+ describe("bigIntMax", () => {
30
+ it("returns the maximum of positive bigint values", () => {
31
+ expect(bigIntMax(1n, 2n, 3n)).toBe(3n);
41
32
  });
42
- it("converts with truncation to 2 decimals", () => {
43
- const raw = 987654321000000000n; // ~0.987654321 ETH
44
- expect(fromFixedPoint(raw, 18, 2)).toBe(0.98);
33
+ it("returns the maximum of negative bigint values", () => {
34
+ expect(bigIntMax(-10n, -5n, -20n)).toBe(-5n);
45
35
  });
46
- it("handles large fixedPoint decimals", () => {
47
- const raw = 1000000000000000000000000n; // 1M with 6 decimals
48
- expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000);
36
+ it("returns the only element if array has one bigint", () => {
37
+ expect(bigIntMax(42n)).toBe(42n);
49
38
  });
50
- });
51
- describe("formatTokenAmount", () => {
52
- it("formats zero value with desired decimals", () => {
53
- expect(formatTokenAmount(0, 18, 3)).toBe("0.000");
54
- expect(formatTokenAmount("0", 18, 2)).toBe("0.00");
39
+ it("works with a mix of positive and negative bigints", () => {
40
+ expect(bigIntMax(-100n, 0n, 50n, -10n)).toBe(50n);
55
41
  });
56
- it("returns <0.01 if amount is less than minimum display value", () => {
57
- expect(formatTokenAmount("1", 18, 2)).toBe("<0.01"); // 1 wei = 1e-18
42
+ it("throws an error if called with no arguments", () => {
43
+ expect(() => bigIntMax()).toThrow();
58
44
  });
59
- it("formats a normal value without rounding", () => {
60
- const result = formatTokenAmount("1230000000000000000", 18, 2);
61
- expect(result).toBe("1.23");
45
+ it("returns the correct value when all values are equal", () => {
46
+ expect(bigIntMax(5n, 5n, 5n)).toBe(5n);
62
47
  });
63
- it("formats with rounding if withRoundUp = true", () => {
64
- const result = formatTokenAmount("1234560000000000000", 18, 2, true);
65
- expect(result).toBe("1.24");
48
+ });
49
+ describe("normalizePrecision", () => {
50
+ it("reduces precision correctly (scale down)", () => {
51
+ const result = normalizePrecision(1234500n, 6, 3);
52
+ expect(result).toBe(1234n);
53
+ });
54
+ it("increases precision correctly (scale up)", () => {
55
+ const result = normalizePrecision(1234n, 3, 6);
56
+ expect(result).toBe(1234000n);
57
+ });
58
+ it("returns the same value when precision is equal", () => {
59
+ const result = normalizePrecision(987654321n, 5, 5);
60
+ expect(result).toBe(987654321n);
61
+ });
62
+ it("handles zero value correctly", () => {
63
+ const result = normalizePrecision(0n, 4, 8);
64
+ expect(result).toBe(0n);
65
+ });
66
+ it("handles large numbers without precision loss", () => {
67
+ const value = 123456789012345678901234567890n;
68
+ const result = normalizePrecision(value, 18, 20);
69
+ expect(result).toBe(value * 100n);
66
70
  });
67
71
  });
68
72
  //# sourceMappingURL=numbers.test.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,cAAc,EACd,aAAa,GACd,MAAM,WAAW,CAAA;AAElB,QAAQ,CAAC,2BAA2B,EAAE,GAAG,EAAE;IACzC,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,CAAC,yBAAyB,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACjE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,0BAA0B,EAAE,GAAG,EAAE;QAClC,MAAM,CAAC,yBAAyB,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IAC7D,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACjC,MAA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