@mezo-org/passport 0.4.0-dev.5 → 0.4.0-dev.51
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/README.md +18 -22
- package/dist/src/api/auth.d.ts +36 -59
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +22 -48
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +27 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +61 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/endpoint-error.d.ts +4 -0
- package/dist/src/api/endpoint-error.d.ts.map +1 -0
- package/dist/src/api/endpoint-error.js +6 -0
- package/dist/src/api/endpoint-error.js.map +1 -0
- package/dist/src/api/fetch-error copy.d.ts +5 -0
- package/dist/src/api/fetch-error copy.d.ts.map +1 -0
- package/dist/src/api/fetch-error copy.js +8 -0
- package/dist/src/api/fetch-error copy.js.map +1 -0
- package/dist/src/api/fetch-error.d.ts +5 -0
- package/dist/src/api/fetch-error.d.ts.map +1 -0
- package/dist/src/api/fetch-error.js +8 -0
- package/dist/src/api/fetch-error.js.map +1 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/portal.d.ts +26 -0
- package/dist/src/api/portal.d.ts.map +1 -0
- package/dist/src/api/portal.js +26 -0
- package/dist/src/api/portal.js.map +1 -0
- package/dist/src/assets/DefaultAvatar.d.ts +5 -0
- package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
- package/dist/src/assets/DefaultAvatar.js +21 -0
- package/dist/src/assets/DefaultAvatar.js.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +8 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +39 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +23 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +27 -0
- package/dist/src/components/Dropdown/Content.js.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts +27 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +68 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts +13 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.js +34 -0
- package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
- package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +11 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js +9 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js +15 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +9 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +48 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts +23 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.js +57 -0
- package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js +64 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
- package/dist/src/components/Dropdown/index.d.ts +3 -0
- package/dist/src/components/Dropdown/index.d.ts.map +1 -0
- package/dist/src/components/Dropdown/index.js +2 -0
- package/dist/src/components/Dropdown/index.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/config.d.ts +17 -5
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +51 -14
- package/dist/src/config.js.map +1 -1
- package/dist/src/constants.d.ts +11 -3
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +12 -4
- package/dist/src/constants.js.map +1 -1
- package/dist/src/hooks/constants.d.ts +1 -0
- package/dist/src/hooks/constants.d.ts.map +1 -1
- package/dist/src/hooks/constants.js +1 -0
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +3 -1
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +3 -1
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts +23 -0
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsConversionRates.js +89 -0
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
- package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +12 -102
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js +28 -17
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
- package/dist/src/hooks/useBitcoinPriceConversion copy.d.ts +2 -0
- package/dist/src/hooks/useBitcoinPriceConversion copy.d.ts.map +1 -0
- package/dist/src/hooks/useBitcoinPriceConversion copy.js +43 -0
- package/dist/src/hooks/useBitcoinPriceConversion copy.js.map +1 -0
- package/dist/src/hooks/useBitcoinPriceConversion.d.ts +103 -0
- package/dist/src/hooks/useBitcoinPriceConversion.d.ts.map +1 -0
- package/dist/src/hooks/useBitcoinPriceConversion.js +50 -0
- package/dist/src/hooks/useBitcoinPriceConversion.js.map +1 -0
- package/dist/src/hooks/useBitcoinPriceFeed.d.ts +15 -0
- package/dist/src/hooks/useBitcoinPriceFeed.d.ts.map +1 -0
- package/dist/src/hooks/useBitcoinPriceFeed.js +67 -0
- package/dist/src/hooks/useBitcoinPriceFeed.js.map +1 -0
- package/dist/src/hooks/useBorrowData.d.ts +60 -0
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
- package/dist/src/hooks/useBorrowData.js +122 -0
- package/dist/src/hooks/useBorrowData.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +23 -173
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +7 -4
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +12 -101
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +10 -8
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +47 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +99 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +2 -2
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +2 -2
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +16 -2
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +25 -3
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +1 -1
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +15 -175
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +7 -4
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useRefreshPassport.d.ts +19 -0
- package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
- package/dist/src/hooks/useRefreshPassport.js +44 -0
- package/dist/src/hooks/useRefreshPassport.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -100
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +6 -3
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +12 -100
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +2 -2
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts +12 -28
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +7 -4
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +12 -100
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.js +2 -2
- package/dist/src/hooks/useSignUpWithWallet.js.map +1 -1
- package/dist/src/hooks/useTokensBalances.d.ts +74 -0
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
- package/dist/src/hooks/useTokensBalances.js +140 -0
- package/dist/src/hooks/useTokensBalances.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +30 -136
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +7 -4
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +13 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +29 -0
- package/dist/src/hooks/useWalletAccount.js.map +1 -0
- package/dist/src/hooks/useWatchTransferEvents.d.ts +5 -0
- package/dist/src/hooks/useWatchTransferEvents.d.ts.map +1 -0
- package/dist/src/hooks/useWatchTransferEvents.js +63 -0
- package/dist/src/hooks/useWatchTransferEvents.js.map +1 -0
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib/contracts/index.d.ts +13 -0
- package/dist/src/lib/contracts/index.d.ts.map +1 -0
- package/dist/src/lib/contracts/index.js +58 -0
- package/dist/src/lib/contracts/index.js.map +1 -0
- package/dist/src/lib/contracts/price-oracle.d.ts +43 -0
- package/dist/src/lib/contracts/price-oracle.d.ts.map +1 -0
- package/dist/src/lib/contracts/price-oracle.js +52 -0
- package/dist/src/lib/contracts/price-oracle.js.map +1 -0
- package/dist/src/lib/contracts/priceOracle.d.ts +43 -0
- package/dist/src/lib/contracts/priceOracle.d.ts.map +1 -0
- package/dist/src/lib/contracts/priceOracle.js +52 -0
- package/dist/src/lib/contracts/priceOracle.js.map +1 -0
- package/dist/src/provider.d.ts +10 -13
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +11 -20
- package/dist/src/provider.js.map +1 -1
- package/dist/src/stores/dropdownStore.d.ts +12 -0
- package/dist/src/stores/dropdownStore.d.ts.map +1 -0
- package/dist/src/stores/dropdownStore.js +13 -0
- package/dist/src/stores/dropdownStore.js.map +1 -0
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +37 -0
- package/dist/src/utils/address.js.map +1 -0
- package/dist/src/utils/address.test.d.ts +2 -0
- package/dist/src/utils/address.test.d.ts.map +1 -0
- package/dist/src/utils/address.test.js +40 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +44 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +132 -0
- package/dist/src/utils/cryptoAssets.js.map +1 -0
- package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
- package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.test.js +67 -0
- package/dist/src/utils/cryptoAssets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +14 -0
- package/dist/src/utils/currency.d.ts.map +1 -0
- package/dist/src/utils/currency.js +27 -0
- package/dist/src/utils/currency.js.map +1 -0
- package/dist/src/utils/currency.test.d.ts +2 -0
- package/dist/src/utils/currency.test.d.ts.map +1 -0
- package/dist/src/utils/currency.test.js +34 -0
- package/dist/src/utils/currency.test.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +58 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +132 -0
- package/dist/src/utils/numbers.js.map +1 -0
- package/dist/src/utils/numbers.test.d.ts +2 -0
- package/dist/src/utils/numbers.test.d.ts.map +1 -0
- package/dist/src/utils/numbers.test.js +170 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +14 -8
- package/src/api/auth.ts +73 -128
- package/src/api/client.ts +87 -0
- package/src/api/fetch-error.ts +8 -0
- package/src/api/index.ts +2 -0
- package/src/api/portal.ts +53 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +76 -0
- package/src/components/Dropdown/Content.tsx +98 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +148 -0
- package/src/components/Dropdown/ListingItem.tsx +80 -0
- package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
- package/src/components/Dropdown/README.md +41 -0
- package/src/components/Dropdown/Receive/Receive.tsx +119 -0
- package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
- package/src/components/Dropdown/Root/AccountAssetItem.tsx +26 -0
- package/src/components/Dropdown/Root/AccountBalance.tsx +30 -0
- package/src/components/Dropdown/Root/AccountOtherAssets.tsx +68 -0
- package/src/components/Dropdown/Root/Root.tsx +147 -0
- package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
- package/src/components/Dropdown/Root/WelcomeBlock.tsx +91 -0
- package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/config.ts +77 -20
- package/src/constants.ts +12 -4
- package/src/hooks/constants.ts +1 -0
- package/src/hooks/index.ts +6 -1
- package/src/hooks/useAssetsConversionRates.ts +96 -0
- package/src/hooks/useAuthenticateWithWallet.ts +44 -23
- package/src/hooks/useBorrowData.ts +143 -0
- package/src/hooks/useCreateAccount.ts +19 -5
- package/src/hooks/useCreateSession.ts +22 -9
- package/src/hooks/useDropdownData.ts +149 -0
- package/src/hooks/useGetAccountByAddress.ts +2 -2
- package/src/hooks/useGetAccountByMezoId.ts +2 -2
- package/src/hooks/useGetCurrentAccount.ts +46 -6
- package/src/hooks/useGetSession.ts +1 -1
- package/src/hooks/useLinkAccount.ts +18 -5
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useRefreshPassport.ts +56 -0
- package/src/hooks/useSignInWithDiscord.ts +14 -5
- package/src/hooks/useSignInWithWallet.ts +9 -2
- package/src/hooks/useSignOut.ts +18 -4
- package/src/hooks/useSignUpWithWallet.ts +9 -2
- package/src/hooks/useTokensBalances.ts +187 -0
- package/src/hooks/useUpdateMezoId.ts +19 -4
- package/src/hooks/useWalletAccount.ts +55 -0
- package/src/hooks/useWatchTransferEvents.ts +74 -0
- package/src/index.ts +12 -1
- package/src/lib/contracts/index.ts +90 -0
- package/src/lib/contracts/priceOracle.ts +53 -0
- package/src/provider.ts +30 -37
- package/src/stores/dropdownStore.ts +20 -0
- package/src/utils/address.test.ts +48 -0
- package/src/utils/address.ts +45 -0
- package/src/utils/cryptoAssets.test.ts +79 -0
- package/src/utils/cryptoAssets.ts +173 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +220 -0
- package/src/utils/numbers.ts +188 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {
|
|
2
|
+
formatNumberToCompactString,
|
|
3
|
+
fromFixedPoint,
|
|
4
|
+
roundUpNumber,
|
|
5
|
+
formatNumberToLocaleString,
|
|
6
|
+
fromFixedPointToString,
|
|
7
|
+
} from "./numbers"
|
|
8
|
+
|
|
9
|
+
describe("formatNumberToCompactString", () => {
|
|
10
|
+
it("formats number less than 1000 with decimals", () => {
|
|
11
|
+
expect(formatNumberToCompactString(123.456789, 2)).toBe("123.46")
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it("formats thousands with K", () => {
|
|
15
|
+
expect(formatNumberToCompactString(12_345, 2)).toBe("12.35K")
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("formats millions with M", () => {
|
|
19
|
+
expect(formatNumberToCompactString(12_345_678, 2)).toBe("12.35M")
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it("formats billions with B", () => {
|
|
23
|
+
expect(formatNumberToCompactString(12_345_678_901, 2)).toBe("12.35B")
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("formats trillions with T", () => {
|
|
27
|
+
expect(formatNumberToCompactString(12_345_678_901_234, 2)).toBe("12.35T")
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("formats bigint as readable number", () => {
|
|
31
|
+
expect(formatNumberToCompactString(1234567890123456789n, 2)).toBe(
|
|
32
|
+
"1,234,567.89T",
|
|
33
|
+
)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("caps at > 1000T for huge numbers", () => {
|
|
37
|
+
const hugeNumber = 10 ** 18 // 1e18
|
|
38
|
+
expect(formatNumberToCompactString(hugeNumber, 2)).toBe("1,000,000T")
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it("formats numbers with trailing zeros correctly", () => {
|
|
42
|
+
expect(formatNumberToCompactString(1_000, 2)).toBe("1K")
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe("roundUpNumber", () => {
|
|
47
|
+
it("rounds up to 2 decimals", () => {
|
|
48
|
+
expect(roundUpNumber(1.234)).toBe(1.24)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it("rounds up to 3 decimals", () => {
|
|
52
|
+
expect(roundUpNumber(9.87654, 3)).toBe(9.877)
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it("rounds up exact decimal", () => {
|
|
56
|
+
expect(roundUpNumber(2.5, 1)).toBe(2.5)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe("fromFixedPoint", () => {
|
|
61
|
+
it("converts fixed point with 18 decimals to float", () => {
|
|
62
|
+
const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
|
|
63
|
+
expect(fromFixedPoint(raw, 18)).toBe(1.2345)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it("converts with truncation to 2 decimals", () => {
|
|
67
|
+
const raw = 987654321000000000n // ~0.987654321 ETH
|
|
68
|
+
expect(fromFixedPoint(raw, 18, 2)).toBe(0.98)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it("handles large fixedPoint decimals", () => {
|
|
72
|
+
const raw = 1_000_000_000_000_000_000_000_000n // 1M with 6 decimals
|
|
73
|
+
expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000)
|
|
74
|
+
})
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
describe("formatNumberToLocaleString", () => {
|
|
78
|
+
it("formats zero with 0 decimals", () => {
|
|
79
|
+
expect(formatNumberToLocaleString(0, 0)).toBe("0")
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
it("formats zero with >0 decimals", () => {
|
|
83
|
+
expect(formatNumberToLocaleString(0, 2)).toBe("0.00")
|
|
84
|
+
expect(formatNumberToLocaleString(0, 4)).toBe("0.0000")
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
it("formats small decimal number", () => {
|
|
88
|
+
expect(formatNumberToLocaleString(0.123456, 2)).toBe("0.12")
|
|
89
|
+
expect(formatNumberToLocaleString(0.123456, 4)).toBe("0.1235")
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it("formats number just above 1", () => {
|
|
93
|
+
expect(formatNumberToLocaleString(1.000001, 3)).toBe("1.000")
|
|
94
|
+
expect(formatNumberToLocaleString(1.234567, 2)).toBe("1.23")
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it("formats whole number", () => {
|
|
98
|
+
expect(formatNumberToLocaleString(1234, 0)).toBe("1,234")
|
|
99
|
+
expect(formatNumberToLocaleString(1000, 2)).toBe("1,000.00")
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
it("formats number with trailing decimal zeroes", () => {
|
|
103
|
+
expect(formatNumberToLocaleString(10.1, 3)).toBe("10.100")
|
|
104
|
+
expect(formatNumberToLocaleString(5.0, 2)).toBe("5.00")
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
it("formats string input", () => {
|
|
108
|
+
expect(formatNumberToLocaleString("123.456", 2)).toBe("123.46")
|
|
109
|
+
expect(formatNumberToLocaleString("0.00123", 4)).toBe("0.0012")
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
it("formats negative number", () => {
|
|
113
|
+
expect(formatNumberToLocaleString(-123.456, 2)).toBe("-123.46")
|
|
114
|
+
expect(formatNumberToLocaleString("-0.00999", 3)).toBe("-0.010")
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it("formats large number with commas", () => {
|
|
118
|
+
expect(formatNumberToLocaleString(12345678.9, 2)).toBe("12,345,678.90")
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
it("handles max decimals safely", () => {
|
|
122
|
+
expect(formatNumberToLocaleString(1.123456789, 8)).toBe("1.12345679")
|
|
123
|
+
expect(formatNumberToLocaleString("0.000000000000000001", 18)).toBe(
|
|
124
|
+
"0.000000000000000001",
|
|
125
|
+
)
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
it("defaults to 0 decimals if not passed", () => {
|
|
129
|
+
expect(formatNumberToLocaleString(1234.567)).toBe("1,235") // rounds up by default
|
|
130
|
+
})
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
describe("fromFixedPointToString", () => {
|
|
134
|
+
const toFixedPoint = (num: number) => BigInt(Math.round(num * 1e18))
|
|
135
|
+
|
|
136
|
+
const cases = [
|
|
137
|
+
{ input: 2.0, expected: "2.000" },
|
|
138
|
+
{ input: 2.009, expected: "2.009" },
|
|
139
|
+
{ input: 2.01, expected: "2.010" },
|
|
140
|
+
{ input: 2.015, expected: "2.016" },
|
|
141
|
+
{ input: 2.05, expected: "2.050" },
|
|
142
|
+
{ input: 2.09, expected: "2.090" },
|
|
143
|
+
]
|
|
144
|
+
|
|
145
|
+
describe("withRoundUp: true", () => {
|
|
146
|
+
cases.forEach(({ input, expected }) => {
|
|
147
|
+
it(`formats ${input} → ${expected}`, () => {
|
|
148
|
+
const fixed = toFixedPoint(input)
|
|
149
|
+
expect(fromFixedPointToString(fixed, 18, 3, true)).toBe(expected)
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
describe("withRoundUp: false", () => {
|
|
155
|
+
const noRoundCases = [
|
|
156
|
+
{ input: 2.0099, expected: "2.009" },
|
|
157
|
+
{ input: 2.0159, expected: "2.015" },
|
|
158
|
+
{ input: 2.0101, expected: "2.010" },
|
|
159
|
+
{ input: 2.0199, expected: "2.019" },
|
|
160
|
+
]
|
|
161
|
+
|
|
162
|
+
noRoundCases.forEach(({ input, expected }) => {
|
|
163
|
+
it(`formats ${input} → ${expected} (no rounding)`, () => {
|
|
164
|
+
const fixed = toFixedPoint(input)
|
|
165
|
+
expect(fromFixedPointToString(fixed, 18, 3, false)).toBe(expected)
|
|
166
|
+
})
|
|
167
|
+
})
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
describe("zero handling", () => {
|
|
171
|
+
it("formats 0 → 0.000 (3 decimals)", () => {
|
|
172
|
+
expect(fromFixedPointToString(0n, 18, 3)).toBe("0.000")
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
it("formats 0 → 0.0000 (4 decimals)", () => {
|
|
176
|
+
expect(fromFixedPointToString(0n, 18, 4)).toBe("0.0000")
|
|
177
|
+
})
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
describe("very small numbers below display threshold", () => {
|
|
181
|
+
it("returns <0.0001 for small number (3 decimals)", () => {
|
|
182
|
+
const small = BigInt(123) // ~1.23e-16
|
|
183
|
+
expect(fromFixedPointToString(small, 18, 4)).toBe("<0.0001")
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
it("returns <0.01 for small number with only 2 decimals", () => {
|
|
187
|
+
const tiny = BigInt(9999999999999) // <0.01
|
|
188
|
+
expect(fromFixedPointToString(tiny, 18, 2)).toBe("<0.01")
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
describe("input types", () => {
|
|
193
|
+
it("accepts bigint", () => {
|
|
194
|
+
const amount = 2100000000000000000n
|
|
195
|
+
expect(fromFixedPointToString(amount, 18, 3)).toBe("2.100")
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it("accepts string", () => {
|
|
199
|
+
expect(fromFixedPointToString("2000000000000000000", 18, 3)).toBe("2.000")
|
|
200
|
+
})
|
|
201
|
+
|
|
202
|
+
it("accepts number", () => {
|
|
203
|
+
expect(fromFixedPointToString(Number("1000000000000000000"), 18, 3)).toBe(
|
|
204
|
+
"1.000",
|
|
205
|
+
)
|
|
206
|
+
})
|
|
207
|
+
})
|
|
208
|
+
|
|
209
|
+
describe("high values", () => {
|
|
210
|
+
it("formats 1_000_000.123456789 correctly", () => {
|
|
211
|
+
const big = toFixedPoint(1_000_000.123456789)
|
|
212
|
+
expect(fromFixedPointToString(big, 18, 6)).toBe("1,000,000.123456")
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it("formats 1e12 ETH", () => {
|
|
216
|
+
const big = BigInt("1000000000000000000000000000000")
|
|
217
|
+
expect(fromFixedPointToString(big, 18, 2)).toBe("1,000,000,000,000.00")
|
|
218
|
+
})
|
|
219
|
+
})
|
|
220
|
+
})
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
type DesiredDecimals = Intl.NumberFormatOptions &
|
|
2
|
+
BigIntToLocaleStringOptions["minimumFractionDigits"]
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number or bigint into compact string with K, M, B, or T suffix.
|
|
6
|
+
* @param value The number or bigint to format.
|
|
7
|
+
* @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
|
|
8
|
+
* @returns The formatted number as a string.
|
|
9
|
+
*/
|
|
10
|
+
export function formatNumberToCompactString(
|
|
11
|
+
value: number | bigint,
|
|
12
|
+
decimals = 4,
|
|
13
|
+
): string {
|
|
14
|
+
return value.toLocaleString("en-US", {
|
|
15
|
+
notation: "compact",
|
|
16
|
+
compactDisplay: "short",
|
|
17
|
+
maximumFractionDigits: decimals as DesiredDecimals,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
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
|
|
63
|
+
*/
|
|
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,
|
|
137
|
+
): 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)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const parsedAmount = amount.toString()
|
|
177
|
+
const fixedPointAmount = parseToFixedPointNumber(parsedAmount)
|
|
178
|
+
|
|
179
|
+
if (typeof fixedPointAmount === "undefined") {
|
|
180
|
+
return 0n
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return convertFixedPoint(
|
|
184
|
+
fixedPointAmount.amount,
|
|
185
|
+
fixedPointAmount.decimals,
|
|
186
|
+
decimals,
|
|
187
|
+
)
|
|
188
|
+
}
|