@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.
- package/README.md +18 -22
- package/dist/src/api/auth.d.ts +7 -3
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +3 -1
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +4 -1
- package/dist/src/api/client.d.ts.map +1 -1
- package/dist/src/api/client.js +9 -2
- package/dist/src/api/client.js.map +1 -1
- package/dist/src/api/portal.d.ts +3 -9
- package/dist/src/api/portal.d.ts.map +1 -1
- package/dist/src/api/portal.js +8 -5
- package/dist/src/api/portal.js.map +1 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +2 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -1
- package/dist/src/components/Dropdown/ConnectedTrigger.js +29 -20
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -1
- package/dist/src/components/Dropdown/Content.d.ts +4 -5
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Content.js +19 -61
- package/dist/src/components/Dropdown/Content.js.map +1 -1
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -1
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +2 -2
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.d.ts +9 -6
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.js +10 -10
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
- package/dist/src/components/Dropdown/ListingItem.d.ts +14 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.js +42 -0
- package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts +8 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js +32 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts +4 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.js +64 -0
- package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +4 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js +49 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +6 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js +35 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts +6 -0
- package/dist/src/components/Dropdown/Root/AccountBtcListing.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBtcListing.js +27 -0
- package/dist/src/components/Dropdown/Root/AccountBtcListing.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountError.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/AccountError.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountError.js +17 -0
- package/dist/src/components/Dropdown/Root/AccountError.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts +4 -0
- package/dist/src/components/Dropdown/Root/AccountMusdListing.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountMusdListing.js +21 -0
- package/dist/src/components/Dropdown/Root/AccountMusdListing.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +43 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.js +45 -0
- package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +4 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js +66 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +6 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js +88 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/SlotNumber.d.ts +19 -0
- package/dist/src/components/Dropdown/SlotNumber.d.ts.map +1 -0
- package/dist/src/components/Dropdown/SlotNumber.js +67 -0
- package/dist/src/components/Dropdown/SlotNumber.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/config.d.ts +19 -5
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +58 -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/index.d.ts +4 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +4 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts +18 -0
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsConversionRates.js +67 -0
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +13 -13
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js +9 -8
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
- package/dist/src/hooks/useBorrowData.d.ts +108 -0
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
- package/dist/src/hooks/useBorrowData.js +201 -0
- package/dist/src/hooks/useBorrowData.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +22 -1
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +4 -6
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +19 -13
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +20 -1
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- 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/useSignInWithWallet.d.ts +13 -13
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +13 -13
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useTokensBalances.d.ts +75 -0
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
- package/dist/src/hooks/useTokensBalances.js +181 -0
- package/dist/src/hooks/useTokensBalances.js.map +1 -0
- package/dist/src/hooks/useValidateMezoId.d.ts +8 -0
- package/dist/src/hooks/useValidateMezoId.d.ts.map +1 -0
- package/dist/src/hooks/useValidateMezoId.js +24 -0
- package/dist/src/hooks/useValidateMezoId.js.map +1 -0
- package/dist/src/hooks/useWalletAccount.d.ts +2 -1
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
- package/dist/src/hooks/useWalletAccount.js +20 -15
- package/dist/src/hooks/useWalletAccount.js.map +1 -1
- 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 +66 -0
- package/dist/src/lib/contracts/index.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 +7 -1
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +4 -1
- 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 +1 -1
- package/dist/src/utils/address.d.ts.map +1 -1
- package/dist/src/utils/address.js +2 -0
- package/dist/src/utils/address.js.map +1 -1
- package/dist/src/utils/address.test.js +8 -5
- package/dist/src/utils/address.test.js.map +1 -1
- package/dist/src/utils/assets.d.ts +145 -0
- package/dist/src/utils/assets.d.ts.map +1 -0
- package/dist/src/utils/assets.js +100 -0
- package/dist/src/utils/assets.js.map +1 -0
- package/dist/src/utils/assets.test.d.ts +2 -0
- package/dist/src/utils/assets.test.d.ts.map +1 -0
- package/dist/src/utils/assets.test.js +46 -0
- package/dist/src/utils/assets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +13 -2
- package/dist/src/utils/currency.d.ts.map +1 -1
- package/dist/src/utils/currency.js +22 -8
- package/dist/src/utils/currency.js.map +1 -1
- package/dist/src/utils/currency.test.js +44 -1
- package/dist/src/utils/currency.test.js.map +1 -1
- package/dist/src/utils/mezoId.d.ts +7 -0
- package/dist/src/utils/mezoId.d.ts.map +1 -0
- package/dist/src/utils/mezoId.js +41 -0
- package/dist/src/utils/mezoId.js.map +1 -0
- package/dist/src/utils/number2.d.ts +106 -0
- package/dist/src/utils/number2.d.ts.map +1 -0
- package/dist/src/utils/number2.js +289 -0
- package/dist/src/utils/number2.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +15 -33
- package/dist/src/utils/numbers.d.ts.map +1 -1
- package/dist/src/utils/numbers.js +26 -70
- package/dist/src/utils/numbers.js.map +1 -1
- package/dist/src/utils/numbers.test.js +46 -42
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/dist/src/utils/siww.d.ts +1 -0
- package/dist/src/utils/siww.d.ts.map +1 -1
- package/dist/src/utils/siww.js +50 -12
- package/dist/src/utils/siww.js.map +1 -1
- package/dist/src/utils/wagmi.d.ts +3 -0
- package/dist/src/utils/wagmi.d.ts.map +1 -0
- package/dist/src/utils/wagmi.js +7 -0
- package/dist/src/utils/wagmi.js.map +1 -0
- package/dist/src/wallet/index.d.ts +1 -1
- package/dist/src/wallet/index.d.ts.map +1 -1
- package/dist/src/wallet/index.js +1 -1
- package/dist/src/wallet/index.js.map +1 -1
- package/package.json +11 -8
- package/src/api/auth.ts +8 -4
- package/src/api/client.ts +11 -2
- package/src/api/portal.ts +11 -14
- package/src/components/Dropdown/ConnectedTrigger.tsx +52 -32
- package/src/components/Dropdown/Content.tsx +26 -121
- package/src/components/Dropdown/DisconnectedTrigger.tsx +2 -1
- package/src/components/Dropdown/Dropdown.tsx +29 -17
- package/src/components/Dropdown/ListingItem.tsx +176 -0
- package/src/components/Dropdown/NestedViewLayout.tsx +87 -0
- package/src/components/Dropdown/README.md +10 -18
- package/src/components/Dropdown/Receive/Receive.tsx +144 -0
- package/src/components/Dropdown/Root/AccountAddressActions.tsx +99 -0
- package/src/components/Dropdown/Root/AccountBalance.tsx +75 -0
- package/src/components/Dropdown/Root/AccountBtcListing.tsx +52 -0
- package/src/components/Dropdown/Root/AccountError.tsx +34 -0
- package/src/components/Dropdown/Root/AccountMusdListing.tsx +45 -0
- package/src/components/Dropdown/Root/AccountOtherAssets.tsx +85 -0
- package/src/components/Dropdown/Root/Root.tsx +77 -0
- package/src/components/Dropdown/Root/WalletAddress.tsx +123 -0
- package/src/components/Dropdown/Root/WelcomeBlock.tsx +173 -0
- package/src/components/Dropdown/SlotNumber.tsx +131 -0
- package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
- package/src/config.ts +88 -20
- package/src/constants.ts +12 -4
- package/src/hooks/index.ts +7 -0
- package/src/hooks/useAssetsConversionRates.ts +79 -0
- package/src/hooks/useAuthenticateWithWallet.ts +30 -14
- package/src/hooks/useBorrowData.ts +246 -0
- package/src/hooks/useCreateAccount.ts +29 -2
- package/src/hooks/useGetCurrentAccount.ts +5 -7
- package/src/hooks/useLinkAccount.ts +37 -4
- package/src/hooks/useRefreshPassport.ts +56 -0
- package/src/hooks/useSignInWithWallet.ts +2 -2
- package/src/hooks/useSignUpWithWallet.ts +2 -2
- package/src/hooks/useTokensBalances.ts +265 -0
- package/src/hooks/useValidateMezoId.ts +31 -0
- package/src/hooks/useWalletAccount.ts +32 -20
- package/src/hooks/useWatchTransferEvents.ts +74 -0
- package/src/index.ts +12 -1
- package/src/lib/contracts/index.ts +99 -0
- package/src/lib/contracts/priceOracle.ts +53 -0
- package/src/provider.ts +11 -3
- package/src/stores/dropdownStore.ts +20 -0
- package/src/utils/address.test.ts +10 -6
- package/src/utils/address.ts +5 -3
- package/src/utils/assets.test.ts +57 -0
- package/src/utils/assets.ts +103 -0
- package/src/utils/currency.test.ts +77 -1
- package/src/utils/currency.ts +35 -9
- package/src/utils/mezoId.ts +51 -0
- package/src/utils/number2.ts +419 -0
- package/src/utils/numbers.test.ts +49 -42
- package/src/utils/numbers.ts +33 -95
- package/src/utils/siww.ts +71 -16
- package/src/utils/wagmi.ts +12 -0
- package/src/wallet/index.ts +3 -2
- package/dist/src/components/Dropdown/AccountAddress.d.ts +0 -8
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountAddress.js +0 -58
- package/dist/src/components/Dropdown/AccountAddress.js.map +0 -1
- package/dist/src/components/Dropdown/AccountAssets.d.ts +0 -14
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountAssets.js +0 -44
- package/dist/src/components/Dropdown/AccountAssets.js.map +0 -1
- package/dist/src/components/Dropdown/AccountBalance.d.ts +0 -7
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +0 -1
- package/dist/src/components/Dropdown/AccountBalance.js +0 -18
- package/dist/src/components/Dropdown/AccountBalance.js.map +0 -1
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts +0 -8
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +0 -1
- package/dist/src/components/Dropdown/WelcomeBlock.js +0 -44
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +0 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +0 -8
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +0 -1
- package/dist/src/hooks/useAssetsUSDConversion.js +0 -21
- package/dist/src/hooks/useAssetsUSDConversion.js.map +0 -1
- package/dist/src/hooks/useDropdownData.d.ts +0 -44
- package/dist/src/hooks/useDropdownData.d.ts.map +0 -1
- package/dist/src/hooks/useDropdownData.js +0 -73
- package/dist/src/hooks/useDropdownData.js.map +0 -1
- package/dist/src/utils/cryptoAssets.d.ts +0 -28
- package/dist/src/utils/cryptoAssets.d.ts.map +0 -1
- package/dist/src/utils/cryptoAssets.js +0 -73
- package/dist/src/utils/cryptoAssets.js.map +0 -1
- package/dist/src/utils/cryptoAssets.test.d.ts +0 -2
- package/dist/src/utils/cryptoAssets.test.d.ts.map +0 -1
- package/dist/src/utils/cryptoAssets.test.js +0 -49
- package/dist/src/utils/cryptoAssets.test.js.map +0 -1
- package/src/components/Dropdown/AccountAddress.tsx +0 -111
- package/src/components/Dropdown/AccountAssets.tsx +0 -110
- package/src/components/Dropdown/AccountBalance.tsx +0 -38
- package/src/components/Dropdown/WelcomeBlock.tsx +0 -92
- package/src/hooks/useAssetsUSDConversion.ts +0 -31
- package/src/hooks/useDropdownData.ts +0 -130
- package/src/utils/cryptoAssets.test.ts +0 -59
- 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":"","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AAC,CAAC,IAAI,oBAAoB,EAAE;QAC5B,CAAC,CAAC,oBAAoB,CAAA;IAExB,OAAO,0BAA0B,CAAC;QAChC,MAAM,EAAE,YAAY;QACpB,eAAe,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
|
|
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
|
|
8
|
+
export declare function formatNumberToCompactString(value: number | bigint, decimals?: CompactStringDecimals): string;
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
12
|
-
* @
|
|
13
|
-
* @
|
|
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
|
|
16
|
+
export declare const normalizePrecision: (value: bigint, currentPrecision: number, targetPrecision?: number) => bigint;
|
|
16
17
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
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
|
|
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
|
|
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
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
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
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
69
|
-
|
|
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
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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":"
|
|
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 {
|
|
2
|
-
describe("
|
|
1
|
+
import { formatNumberToCompactString, bigIntMax, normalizePrecision, } from "./numbers";
|
|
2
|
+
describe("formatNumberToCompactString", () => {
|
|
3
3
|
it("formats number less than 1000 with decimals", () => {
|
|
4
|
-
expect(
|
|
4
|
+
expect(formatNumberToCompactString(123.456789, 2)).toBe("123.46");
|
|
5
5
|
});
|
|
6
6
|
it("formats thousands with K", () => {
|
|
7
|
-
expect(
|
|
7
|
+
expect(formatNumberToCompactString(12_345, 2)).toBe("12.35K");
|
|
8
8
|
});
|
|
9
9
|
it("formats millions with M", () => {
|
|
10
|
-
expect(
|
|
10
|
+
expect(formatNumberToCompactString(12_345_678, 2)).toBe("12.35M");
|
|
11
11
|
});
|
|
12
12
|
it("formats billions with B", () => {
|
|
13
|
-
expect(
|
|
13
|
+
expect(formatNumberToCompactString(12_345_678_901, 2)).toBe("12.35B");
|
|
14
14
|
});
|
|
15
15
|
it("formats trillions with T", () => {
|
|
16
|
-
expect(
|
|
16
|
+
expect(formatNumberToCompactString(12_345_678_901_234, 2)).toBe("12.35T");
|
|
17
17
|
});
|
|
18
18
|
it("formats bigint as readable number", () => {
|
|
19
|
-
expect(
|
|
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(
|
|
23
|
+
expect(formatNumberToCompactString(hugeNumber, 2)).toBe("1,000,000T");
|
|
24
24
|
});
|
|
25
|
-
|
|
26
|
-
|
|
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("
|
|
38
|
-
it("
|
|
39
|
-
|
|
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("
|
|
43
|
-
|
|
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("
|
|
47
|
-
|
|
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
|
-
|
|
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("
|
|
57
|
-
expect(
|
|
42
|
+
it("throws an error if called with no arguments", () => {
|
|
43
|
+
expect(() => bigIntMax()).toThrow();
|
|
58
44
|
});
|
|
59
|
-
it("
|
|
60
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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,
|
|
1
|
+
{"version":3,"file":"numbers.test.js","sourceRoot":"","sources":["../../../src/utils/numbers.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,2BAA2B,EAC3B,SAAS,EACT,kBAAkB,GACnB,MAAM,WAAW,CAAA;AAElB,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;IACnE,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,0BAA0B,EAAE,GAAG,EAAE;QAClC,MAAM,CAAC,2BAA2B,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA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