@mezo-org/passport 0.4.0-dev.10 → 0.4.0-dev.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/components/Dropdown/AccountAssets.d.ts +1 -1
- package/dist/src/components/Dropdown/WelcomeBlock.js +2 -2
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -0
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -0
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +1 -1
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -1
- package/dist/src/hooks/useDropdownData.js +4 -4
- package/dist/src/hooks/useDropdownData.js.map +1 -1
- package/dist/src/hooks/useUpdatePassport.d.ts +17 -0
- package/dist/src/hooks/useUpdatePassport.d.ts.map +1 -0
- package/dist/src/hooks/useUpdatePassport.js +29 -0
- package/dist/src/hooks/useUpdatePassport.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +21 -2
- package/dist/src/utils/numbers.d.ts.map +1 -1
- package/dist/src/utils/numbers.js +49 -3
- package/dist/src/utils/numbers.js.map +1 -1
- package/dist/src/utils/numbers.test.js +11 -11
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/package.json +2 -2
- package/src/components/Dropdown/AccountAssets.tsx +1 -1
- package/src/components/Dropdown/WelcomeBlock.tsx +2 -2
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useDropdownData.ts +7 -4
- package/src/hooks/useUpdatePassport.ts +40 -0
- package/src/utils/numbers.test.ts +11 -11
- package/src/utils/numbers.ts +68 -3
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Block, HeadingSmall, LabelSmall, Mats, useStyletron, } from "@mezo-org/mezo-clay";
|
|
2
2
|
import React from "react";
|
|
3
3
|
import DefaultAvatar from "../../assets/DefaultAvatar";
|
|
4
|
-
import {
|
|
4
|
+
import { formatNumberToCompactString } from "../../utils/numbers";
|
|
5
5
|
const DOT_DELIMITER_REGEX = /(?=\.)/;
|
|
6
6
|
export default function WelcomeBlock(props) {
|
|
7
7
|
const { mezoId, matsBalance } = props;
|
|
@@ -39,6 +39,6 @@ export default function WelcomeBlock(props) {
|
|
|
39
39
|
},
|
|
40
40
|
} },
|
|
41
41
|
React.createElement(Mats, { color: theme.colors.contentPrimary, size: theme.sizing.scale600 }),
|
|
42
|
-
|
|
42
|
+
formatNumberToCompactString(matsBalance, 2)))));
|
|
43
43
|
}
|
|
44
44
|
//# sourceMappingURL=WelcomeBlock.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WelcomeBlock.js","sourceRoot":"","sources":["../../../../src/components/Dropdown/WelcomeBlock.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,YAAY,EACZ,UAAU,EACV,IAAI,EACJ,YAAY,GACb,MAAM,qBAAqB,CAAA;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"WelcomeBlock.js","sourceRoot":"","sources":["../../../../src/components/Dropdown/WelcomeBlock.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,EACL,YAAY,EACZ,UAAU,EACV,IAAI,EACJ,YAAY,GACb,MAAM,qBAAqB,CAAA;AAC5B,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAA;AAEjE,MAAM,mBAAmB,GAAG,QAAQ,CAAA;AAOpC,MAAM,CAAC,OAAO,UAAU,YAAY,CAAC,KAAwB;IAC3D,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,KAAK,CAAA;IAErC,MAAM,CAAC,IAAI,EAAE,UAAU,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC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AAE;4BACL,GAAG,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ;yBAC3B;qBACF;iBACF;gBAED,oBAAC,IAAI,IACH,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,cAAc,EAClC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,QAAQ,GAC3B;gBACD,2BAA2B,CAAC,WAAW,EAAE,CAAC,CAAC,CACjC,CACP,CACF,CACT,CAAA;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,kDAAkD,EAClD,4BAA4B,EAC5B,oDAAoD,GACrD,MAAM,qBAAqB,CAAA;AAC5B,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA;AACtC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,kDAAkD,EAClD,4BAA4B,EAC5B,oDAAoD,GACrD,MAAM,qBAAqB,CAAA;AAC5B,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA;AACtC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA"}
|
package/dist/src/hooks/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,kDAAkD,EAClD,4BAA4B,EAC5B,oDAAoD,GACrD,MAAM,qBAAqB,CAAA;AAC5B,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA;AACtC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/hooks/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EAClB,kDAAkD,EAClD,4BAA4B,EAC5B,oDAAoD,GACrD,MAAM,qBAAqB,CAAA;AAC5B,cAAc,0BAA0B,CAAA;AACxC,cAAc,yBAAyB,CAAA;AACvC,cAAc,wBAAwB,CAAA;AACtC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,kBAAkB,CAAA;AAChC,cAAc,wBAAwB,CAAA;AACtC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,mBAAmB,CAAA;AACjC,cAAc,qBAAqB,CAAA"}
|
|
@@ -8,7 +8,7 @@ import { CryptoAssetKey } from "../utils/cryptoAssets";
|
|
|
8
8
|
*/
|
|
9
9
|
declare function formatCryptoAsset(type: CryptoAssetKey, balance: bigint, btcUsdConversionRate: number): {
|
|
10
10
|
type: CryptoAssetKey;
|
|
11
|
-
balance:
|
|
11
|
+
balance: string;
|
|
12
12
|
balanceInUsd: number;
|
|
13
13
|
decimals: number;
|
|
14
14
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDropdownData.d.ts","sourceRoot":"","sources":["../../../src/hooks/useDropdownData.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,cAAc,EAIf,MAAM,uBAAuB,CAAA;AAK9B;;;;;;GAMG;AACH,iBAAS,iBAAiB,CACxB,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,MAAM,EACf,oBAAoB,EAAE,MAAM;;;;;
|
|
1
|
+
{"version":3,"file":"useDropdownData.d.ts","sourceRoot":"","sources":["../../../src/hooks/useDropdownData.ts"],"names":[],"mappings":"AAEA,OAAO,EACL,cAAc,EAIf,MAAM,uBAAuB,CAAA;AAK9B;;;;;;GAMG;AACH,iBAAS,iBAAiB,CACxB,IAAI,EAAE,cAAc,EACpB,OAAO,EAAE,MAAM,EACf,oBAAoB,EAAE,MAAM;;;;;EAsB7B;AAED,MAAM,MAAM,mBAAmB,CAAC,CAAC,GAAG,cAAc,IAAI;IACpD,IAAI,EAAE,CAAC,CAAA;IACP,OAAO,EAAE,MAAM,CAAA;CAChB,CAAA;AAED,KAAK,2BAA2B,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAA;AAEvE,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,MAAM,CAAA;IACf,UAAU,EAAE,SAAS,GAAG,KAAK,CAAA;IAC7B,iBAAiB,EAAE,MAAM,CAAA;IACzB,qBAAqB,EAAE,2BAA2B,EAAE,CAAA;IACpD,OAAO,EAAE;QACP,IAAI,EAAE,MAAM,CAAA;QACZ,eAAe,EAAE,2BAA2B,EAAE,CAAA;KAC/C,CAAA;CACF,CAAA;AAED,KAAK,sBAAsB,GAAG;IAE5B,eAAe,CAAC,EAAE,mBAAmB,EAAE,CAAA;IACvC,aAAa,CAAC,EAAE,mBAAmB,EAAE,CAAA;CACtC,CAAA;AAED;;;;;;;GAOG;AACH,MAAM,CAAC,OAAO,UAAU,eAAe,CACrC,OAAO,CAAC,EAAE,sBAAsB,GAC/B,YAAY,GAAG,IAAI,CAoDrB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useBitcoinAccount } from "@mezo-org/orangekit";
|
|
2
2
|
import { useGetCurrentAccount } from ".";
|
|
3
3
|
import { getCryptoAsset, isBitcoinLikeCryptoAsset, isUsdLikeCryptoAsset, } from "../utils/cryptoAssets";
|
|
4
|
-
import { fromFixedPoint } from "../utils/numbers";
|
|
4
|
+
import { fromFixedPoint, fromFixedPointToString } from "../utils/numbers";
|
|
5
5
|
import { useAssetsUsdConversion } from "./useAssetsUSDConversion";
|
|
6
6
|
import useWalletAccount from "./useWalletAccount";
|
|
7
7
|
/**
|
|
@@ -13,17 +13,17 @@ import useWalletAccount from "./useWalletAccount";
|
|
|
13
13
|
*/
|
|
14
14
|
function formatCryptoAsset(type, balance, btcUsdConversionRate) {
|
|
15
15
|
const { decimals } = getCryptoAsset(type);
|
|
16
|
-
const nativeBalance = fromFixedPoint(balance, decimals
|
|
16
|
+
const nativeBalance = fromFixedPoint(balance, decimals);
|
|
17
17
|
let balanceInUsd = 0;
|
|
18
18
|
if (isBitcoinLikeCryptoAsset(type)) {
|
|
19
|
-
balanceInUsd =
|
|
19
|
+
balanceInUsd = fromFixedPoint(balance * BigInt(btcUsdConversionRate), decimals);
|
|
20
20
|
}
|
|
21
21
|
if (isUsdLikeCryptoAsset(type)) {
|
|
22
22
|
balanceInUsd = nativeBalance;
|
|
23
23
|
}
|
|
24
24
|
return {
|
|
25
25
|
type,
|
|
26
|
-
balance:
|
|
26
|
+
balance: fromFixedPointToString(balance, decimals),
|
|
27
27
|
balanceInUsd,
|
|
28
28
|
decimals,
|
|
29
29
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDropdownData.js","sourceRoot":"","sources":["../../../src/hooks/useDropdownData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,GAAG,CAAA;AACxC,OAAO,EAEL,cAAc,EACd,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"useDropdownData.js","sourceRoot":"","sources":["../../../src/hooks/useDropdownData.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,GAAG,CAAA;AACxC,OAAO,EAEL,cAAc,EACd,wBAAwB,EACxB,oBAAoB,GACrB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,cAAc,EAAE,sBAAsB,EAAE,MAAM,kBAAkB,CAAA;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAA;AACjE,OAAO,gBAAgB,MAAM,oBAAoB,CAAA;AAEjD;;;;;;GAMG;AACH,SAAS,iBAAiB,CACxB,IAAoB,EACpB,OAAe,EACf,oBAA4B;IAE5B,MAAM,EAAE,QAAQ,EAAE,GAAG,cAAc,CAAC,IAAI,CAAC,CAAA;IAEzC,MAAM,aAAa,GAAG,cAAc,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAA;IACvD,IAAI,YAAY,GAAG,CAAC,CAAA;IACpB,IAAI,wBAAwB,CAAC,IAAI,CAAC,EAAE,CAAC;QACnC,YAAY,GAAG,cAAc,CAC3B,OAAO,GAAG,MAAM,CAAC,oBAAoB,CAAC,EACtC,QAAQ,CACT,CAAA;IACH,CAAC;IACD,IAAI,oBAAoB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/B,YAAY,GAAG,aAAa,CAAA;IAC9B,CAAC;IAED,OAAO;QACL,IAAI;QACJ,OAAO,EAAE,sBAA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AG,KAAK,CAAC,YAAY,EACxC,CAAC,CACF,CAAA;IAED,MAAM,WAAW,GAAG,eAAe,CAAC,IAAI,CAAC,SAAS,CAAA;IAElD,MAAM,sBAAsB,GAAG,aAAa,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CACzD,iBAAiB,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,EAAE,oBAAoB,CAAC,CACnE,CAAA;IAED,OAAO;QACL,MAAM;QACN,OAAO;QACP,UAAU,EAAE,aAAa;QACzB,iBAAiB;QACjB,qBAAqB;QACrB,OAAO,EAAE;YACP,IAAI,EAAE,WAAW;YACjB,eAAe,EAAE,sBAAsB;SACxC;KACF,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to update the passport using query invalidation mechanism.
|
|
3
|
+
* By calling the update function, the latest data will be refetched as soon as possible.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* const { updateCurrentAccount, updateUsdConversion } = useUpdatePassport()
|
|
7
|
+
*
|
|
8
|
+
* // Call this function to update the current account
|
|
9
|
+
* const handleUpdateCurrentAccount = async () => {
|
|
10
|
+
* await updateCurrentAccount()
|
|
11
|
+
* }
|
|
12
|
+
*/
|
|
13
|
+
export declare function useUpdatePassport(): {
|
|
14
|
+
updateCurrentAccount: () => Promise<void>;
|
|
15
|
+
updateUsdConversion: () => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=useUpdatePassport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUpdatePassport.d.ts","sourceRoot":"","sources":["../../../src/hooks/useUpdatePassport.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB;;;EAuBhC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
2
|
+
import { useCallback } from "react";
|
|
3
|
+
import { QUERY_KEYS } from "./constants";
|
|
4
|
+
/**
|
|
5
|
+
* Hook to update the passport using query invalidation mechanism.
|
|
6
|
+
* By calling the update function, the latest data will be refetched as soon as possible.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const { updateCurrentAccount, updateUsdConversion } = useUpdatePassport()
|
|
10
|
+
*
|
|
11
|
+
* // Call this function to update the current account
|
|
12
|
+
* const handleUpdateCurrentAccount = async () => {
|
|
13
|
+
* await updateCurrentAccount()
|
|
14
|
+
* }
|
|
15
|
+
*/
|
|
16
|
+
export function useUpdatePassport() {
|
|
17
|
+
const queryClient = useQueryClient();
|
|
18
|
+
const updateCurrentAccount = useCallback(() => queryClient.invalidateQueries({
|
|
19
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
20
|
+
}), [queryClient]);
|
|
21
|
+
const updateUsdConversion = useCallback(() => queryClient.invalidateQueries({
|
|
22
|
+
queryKey: [QUERY_KEYS.ASSETS_USD_CONVERSION],
|
|
23
|
+
}), [queryClient]);
|
|
24
|
+
return {
|
|
25
|
+
updateCurrentAccount,
|
|
26
|
+
updateUsdConversion,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=useUpdatePassport.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useUpdatePassport.js","sourceRoot":"","sources":["../../../src/hooks/useUpdatePassport.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAA;AACtD,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AACnC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AAExC;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,iBAAiB;IAC/B,MAAM,WAAW,GAAG,cAAc,EAAE,CAAA;IAEpC,MAAM,oBAAoB,GAAG,WAAW,CACtC,GAAG,EAAE,CACH,WAAW,CAAC,iBAAiB,CAAC;QAC5B,QAAQ,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,CAAC,OAAO,CAAC;KACnD,CAAC,EACJ,CAAC,WAAW,CAAC,CACd,CAAA;IAED,MAAM,mBAAmB,GAAG,WAAW,CACrC,GAAG,EAAE,CACH,WAAW,CAAC,iBAAiB,CAAC;QAC5B,QAAQ,EAAE,CAAC,UAAU,CAAC,qBAAqB,CAAC;KAC7C,CAAC,EACJ,CAAC,WAAW,CAAC,CACd,CAAA;IAED,OAAO;QACL,oBAAoB;QACpB,mBAAmB;KACpB,CAAA;AACH,CAAC"}
|
|
@@ -1,10 +1,18 @@
|
|
|
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 declare function
|
|
7
|
+
export declare function formatNumberToCompactString(value: number | bigint, decimals?: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Formats a number or string into a locale-sensitive string with the desired number of decimals.
|
|
10
|
+
* @param value The number or string to format.
|
|
11
|
+
* @param desiredDecimals The number of decimal places to include.
|
|
12
|
+
* @returns The formatted number as a string.
|
|
13
|
+
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L5-L19
|
|
14
|
+
*/
|
|
15
|
+
export declare function formatNumberToLocaleString(value: string | number, desiredDecimals?: number): string;
|
|
8
16
|
/**
|
|
9
17
|
* Returns a number rounded up to the desired number of decimals.
|
|
10
18
|
* @param amount The number to round up.
|
|
@@ -23,4 +31,15 @@ export declare function roundUpNumber(amount: number, desiredDecimals?: number):
|
|
|
23
31
|
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
24
32
|
*/
|
|
25
33
|
export declare function fromFixedPoint(fixedPoint: bigint, fixedPointDecimals: number, desiredDecimals?: number): number;
|
|
34
|
+
/**
|
|
35
|
+
* Display a token amount correctly with desired decimals.
|
|
36
|
+
* The function returns a string with a language-sensitive representation of this number.
|
|
37
|
+
* @param amount The fixed point number to convert.
|
|
38
|
+
* @param decimals The number of decimals in the fixed point number.
|
|
39
|
+
* @param desiredDecimals The number of decimals to display.
|
|
40
|
+
* @param withRoundUp Whether to round up the number.
|
|
41
|
+
* @returns The formatted number as a string.
|
|
42
|
+
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L57-L87
|
|
43
|
+
*/
|
|
44
|
+
export declare const fromFixedPointToString: (amount: number | string | bigint, decimals?: number, desiredDecimals?: number, withRoundUp?: boolean) => string;
|
|
26
45
|
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,
|
|
1
|
+
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,QAAQ,SAAI,GACX,MAAM,CAMR;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CACxC,KAAK,EAAE,MAAM,GAAG,MAAM,EACtB,eAAe,SAAI,UAYpB;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;;;;;;;;;GASG;AACH,eAAO,MAAM,sBAAsB,GACjC,QAAQ,MAAM,GAAG,MAAM,GAAG,MAAM,EAChC,iBAAa,EACb,wBAAmB,EACnB,qBAAkB,WA0BnB,CAAA"}
|
|
@@ -1,16 +1,34 @@
|
|
|
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
|
|
7
|
+
export function formatNumberToCompactString(value, decimals = 4) {
|
|
8
8
|
return value.toLocaleString("en-US", {
|
|
9
9
|
notation: "compact",
|
|
10
10
|
compactDisplay: "short",
|
|
11
11
|
maximumFractionDigits: decimals,
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Formats a number or string into a locale-sensitive string with the desired number of decimals.
|
|
16
|
+
* @param value The number or string to format.
|
|
17
|
+
* @param desiredDecimals The number of decimal places to include.
|
|
18
|
+
* @returns The formatted number as a string.
|
|
19
|
+
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L5-L19
|
|
20
|
+
*/
|
|
21
|
+
export function formatNumberToLocaleString(value, desiredDecimals = 0) {
|
|
22
|
+
const number = typeof value === "number" ? value : parseFloat(value);
|
|
23
|
+
if (number === 0 && desiredDecimals === 0)
|
|
24
|
+
return "0";
|
|
25
|
+
if (number === 0)
|
|
26
|
+
return `0.${"0".repeat(desiredDecimals)}`;
|
|
27
|
+
return number.toLocaleString("default", {
|
|
28
|
+
minimumFractionDigits: desiredDecimals,
|
|
29
|
+
maximumFractionDigits: desiredDecimals,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
14
32
|
/**
|
|
15
33
|
* Returns a number rounded up to the desired number of decimals.
|
|
16
34
|
* @param amount The number to round up.
|
|
@@ -30,11 +48,39 @@ export function roundUpNumber(amount, desiredDecimals = 2) {
|
|
|
30
48
|
* @returns The floating point number truncated to `desiredDecimals`.
|
|
31
49
|
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
32
50
|
*/
|
|
33
|
-
export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals =
|
|
51
|
+
export function fromFixedPoint(fixedPoint, fixedPointDecimals, desiredDecimals = 4) {
|
|
34
52
|
const fixedPointDesiredDecimalsAmount = fixedPoint /
|
|
35
53
|
10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals));
|
|
36
54
|
const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
|
|
37
55
|
10 ** Math.min(desiredDecimals, fixedPointDecimals);
|
|
38
56
|
return formattedAmount;
|
|
39
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Display a token amount correctly with desired decimals.
|
|
60
|
+
* The function returns a string with a language-sensitive representation of this number.
|
|
61
|
+
* @param amount The fixed point number to convert.
|
|
62
|
+
* @param decimals The number of decimals in the fixed point number.
|
|
63
|
+
* @param desiredDecimals The number of decimals to display.
|
|
64
|
+
* @param withRoundUp Whether to round up the number.
|
|
65
|
+
* @returns The formatted number as a string.
|
|
66
|
+
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L57-L87
|
|
67
|
+
*/
|
|
68
|
+
export const fromFixedPointToString = (amount, decimals = 18, desiredDecimals = 4, withRoundUp = true) => {
|
|
69
|
+
const fixedPoint = BigInt(amount);
|
|
70
|
+
if (fixedPoint === 0n) {
|
|
71
|
+
return `0.${"0".repeat(desiredDecimals)}`;
|
|
72
|
+
}
|
|
73
|
+
const formattedAmount = fromFixedPoint(fixedPoint, decimals,
|
|
74
|
+
// To round the amount up, let's increase the precision by one.
|
|
75
|
+
// The desired decimal numbers will be set later anyway.
|
|
76
|
+
withRoundUp ? desiredDecimals + 1 : desiredDecimals);
|
|
77
|
+
const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals);
|
|
78
|
+
if (minAmountToDisplay > formattedAmount) {
|
|
79
|
+
return `<0.${"0".repeat(desiredDecimals - 1)}1`;
|
|
80
|
+
}
|
|
81
|
+
const finalFormattedAmount = withRoundUp
|
|
82
|
+
? Math.ceil(formattedAmount * 10 ** desiredDecimals) / 10 ** desiredDecimals
|
|
83
|
+
: formattedAmount;
|
|
84
|
+
return formatNumberToLocaleString(finalFormattedAmount, desiredDecimals);
|
|
85
|
+
};
|
|
40
86
|
//# sourceMappingURL=numbers.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CACzC,KAAsB,EACtB,QAAQ,GAAG,CAAC;IAEZ,OAAO,KAAK,CAAC,cAAc,CAAC,OAAO,EAAE;QACnC,QAAQ,EAAE,SAAS;QACnB,cAAc,EAAE,OAAO;QACvB,qBAAqB,EAAE,QAA2B;KACnD,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CACxC,KAAsB,EACtB,eAAe,GAAG,CAAC;IAEnB,MAAM,MAAM,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAA;IAEpE,IAAI,MAAM,KAAK,CAAC,IAAI,eAAe,KAAK,CAAC;QAAE,OAAO,GAAG,CAAA;IAErD,IAAI,MAAM,KAAK,CAAC;QAAE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G,CAAC,CAAC,CAAC,CAAC,eAAe,CACpD,CAAA;IAED,MAAM,kBAAkB,GAAG,CAAC,GAAG,EAAE,IAAI,IAAI,CAAC,GAAG,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAA;IAExE,IAAI,kBAAkB,GAAG,eAAe,EAAE,CAAC;QACzC,OAAO,MAAM,GAAG,CAAC,MAAM,CAAC,eAAe,GAAG,CAAC,CAAC,GAAG,CAAA;IACjD,CAAC;IAED,MAAM,oBAAoB,GAAG,WAAW;QACtC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,GAAG,EAAE,IAAI,eAAe,CAAC,GAAG,EAAE,IAAI,eAAe;QAC5E,CAAC,CAAC,eAAe,CAAA;IACnB,OAAO,0BAA0B,CAAC,oBAAoB,EAAE,eAAe,CAAC,CAAA;AAC1E,CAAC,CAAA"}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
describe("
|
|
1
|
+
import { formatNumberToCompactString, fromFixedPoint, roundUpNumber, } 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
25
|
it("formats numbers with trailing zeros correctly", () => {
|
|
26
|
-
expect(
|
|
26
|
+
expect(formatNumberToCompactString(1_000, 2)).toBe("1K");
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
29
|
describe("roundUpNumber", () => {
|
|
@@ -40,7 +40,7 @@ describe("roundUpNumber", () => {
|
|
|
40
40
|
describe("fromFixedPoint", () => {
|
|
41
41
|
it("converts fixed point with 18 decimals to float", () => {
|
|
42
42
|
const raw = 1234567890000000000n; // 1.23456789 ETH in 18 decimals
|
|
43
|
-
expect(fromFixedPoint(raw, 18
|
|
43
|
+
expect(fromFixedPoint(raw, 18)).toBe(1.2345);
|
|
44
44
|
});
|
|
45
45
|
it("converts with truncation to 2 decimals", () => {
|
|
46
46
|
const raw = 987654321000000000n; // ~0.987654321 ETH
|
|
@@ -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,cAAc,EACd,aAAa,GACd,MAAM,WAAW,CAAA;AAElB,QAAQ,CAAC,6BAA6B,EAAE,GAAG,EAAE;IAC3C,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACrD,MAAM,CAAC,2BAA2B,CAAC,UAAU,EAAE,CAA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,0BAAkC,CAAA,CAAC,qBAAqB;QACpE,MAAM,CAAC,cAAc,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAA;IAC7D,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mezo-org/passport",
|
|
3
|
-
"version": "0.4.0-dev.
|
|
3
|
+
"version": "0.4.0-dev.12",
|
|
4
4
|
"main": "dist/src/index.js",
|
|
5
5
|
"license": "GPL-3.0-only",
|
|
6
6
|
"scripts": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@mezo-org/mezo-clay": "0.1.0-dev.18",
|
|
41
|
-
"@mezo-org/orangekit": "1.0.0-beta.40-dev.
|
|
41
|
+
"@mezo-org/orangekit": "1.0.0-beta.40-dev.1",
|
|
42
42
|
"@mezo-org/sign-in-with-wallet": "1.0.0-beta.8"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from "@mezo-org/mezo-clay"
|
|
8
8
|
import React from "react"
|
|
9
9
|
import DefaultAvatar from "../../assets/DefaultAvatar"
|
|
10
|
-
import {
|
|
10
|
+
import { formatNumberToCompactString } from "../../utils/numbers"
|
|
11
11
|
|
|
12
12
|
const DOT_DELIMITER_REGEX = /(?=\.)/
|
|
13
13
|
|
|
@@ -84,7 +84,7 @@ export default function WelcomeBlock(props: WelcomeBlockProps) {
|
|
|
84
84
|
color={theme.colors.contentPrimary}
|
|
85
85
|
size={theme.sizing.scale600}
|
|
86
86
|
/>
|
|
87
|
-
{
|
|
87
|
+
{formatNumberToCompactString(matsBalance, 2)}
|
|
88
88
|
</LabelSmall>
|
|
89
89
|
</Block>
|
|
90
90
|
</Block>
|
package/src/hooks/index.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
isBitcoinLikeCryptoAsset,
|
|
7
7
|
isUsdLikeCryptoAsset,
|
|
8
8
|
} from "../utils/cryptoAssets"
|
|
9
|
-
import { fromFixedPoint } from "../utils/numbers"
|
|
9
|
+
import { fromFixedPoint, fromFixedPointToString } from "../utils/numbers"
|
|
10
10
|
import { useAssetsUsdConversion } from "./useAssetsUSDConversion"
|
|
11
11
|
import useWalletAccount from "./useWalletAccount"
|
|
12
12
|
|
|
@@ -24,10 +24,13 @@ function formatCryptoAsset(
|
|
|
24
24
|
) {
|
|
25
25
|
const { decimals } = getCryptoAsset(type)
|
|
26
26
|
|
|
27
|
-
const nativeBalance = fromFixedPoint(balance, decimals
|
|
27
|
+
const nativeBalance = fromFixedPoint(balance, decimals)
|
|
28
28
|
let balanceInUsd = 0
|
|
29
29
|
if (isBitcoinLikeCryptoAsset(type)) {
|
|
30
|
-
balanceInUsd =
|
|
30
|
+
balanceInUsd = fromFixedPoint(
|
|
31
|
+
balance * BigInt(btcUsdConversionRate),
|
|
32
|
+
decimals,
|
|
33
|
+
)
|
|
31
34
|
}
|
|
32
35
|
if (isUsdLikeCryptoAsset(type)) {
|
|
33
36
|
balanceInUsd = nativeBalance
|
|
@@ -35,7 +38,7 @@ function formatCryptoAsset(
|
|
|
35
38
|
|
|
36
39
|
return {
|
|
37
40
|
type,
|
|
38
|
-
balance:
|
|
41
|
+
balance: fromFixedPointToString(balance, decimals),
|
|
39
42
|
balanceInUsd,
|
|
40
43
|
decimals,
|
|
41
44
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { useQueryClient } from "@tanstack/react-query"
|
|
2
|
+
import { useCallback } from "react"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook to update the passport using query invalidation mechanism.
|
|
7
|
+
* By calling the update function, the latest data will be refetched as soon as possible.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* const { updateCurrentAccount, updateUsdConversion } = useUpdatePassport()
|
|
11
|
+
*
|
|
12
|
+
* // Call this function to update the current account
|
|
13
|
+
* const handleUpdateCurrentAccount = async () => {
|
|
14
|
+
* await updateCurrentAccount()
|
|
15
|
+
* }
|
|
16
|
+
*/
|
|
17
|
+
export function useUpdatePassport() {
|
|
18
|
+
const queryClient = useQueryClient()
|
|
19
|
+
|
|
20
|
+
const updateCurrentAccount = useCallback(
|
|
21
|
+
() =>
|
|
22
|
+
queryClient.invalidateQueries({
|
|
23
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
24
|
+
}),
|
|
25
|
+
[queryClient],
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
const updateUsdConversion = useCallback(
|
|
29
|
+
() =>
|
|
30
|
+
queryClient.invalidateQueries({
|
|
31
|
+
queryKey: [QUERY_KEYS.ASSETS_USD_CONVERSION],
|
|
32
|
+
}),
|
|
33
|
+
[queryClient],
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
updateCurrentAccount,
|
|
38
|
+
updateUsdConversion,
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -1,43 +1,43 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
formatNumberToCompactString,
|
|
3
3
|
fromFixedPoint,
|
|
4
4
|
roundUpNumber,
|
|
5
5
|
} from "./numbers"
|
|
6
6
|
|
|
7
|
-
describe("
|
|
7
|
+
describe("formatNumberToCompactString", () => {
|
|
8
8
|
it("formats number less than 1000 with decimals", () => {
|
|
9
|
-
expect(
|
|
9
|
+
expect(formatNumberToCompactString(123.456789, 2)).toBe("123.46")
|
|
10
10
|
})
|
|
11
11
|
|
|
12
12
|
it("formats thousands with K", () => {
|
|
13
|
-
expect(
|
|
13
|
+
expect(formatNumberToCompactString(12_345, 2)).toBe("12.35K")
|
|
14
14
|
})
|
|
15
15
|
|
|
16
16
|
it("formats millions with M", () => {
|
|
17
|
-
expect(
|
|
17
|
+
expect(formatNumberToCompactString(12_345_678, 2)).toBe("12.35M")
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
it("formats billions with B", () => {
|
|
21
|
-
expect(
|
|
21
|
+
expect(formatNumberToCompactString(12_345_678_901, 2)).toBe("12.35B")
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
it("formats trillions with T", () => {
|
|
25
|
-
expect(
|
|
25
|
+
expect(formatNumberToCompactString(12_345_678_901_234, 2)).toBe("12.35T")
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
it("formats bigint as readable number", () => {
|
|
29
|
-
expect(
|
|
29
|
+
expect(formatNumberToCompactString(1234567890123456789n, 2)).toBe(
|
|
30
30
|
"1,234,567.89T",
|
|
31
31
|
)
|
|
32
32
|
})
|
|
33
33
|
|
|
34
34
|
it("caps at > 1000T for huge numbers", () => {
|
|
35
35
|
const hugeNumber = 10 ** 18 // 1e18
|
|
36
|
-
expect(
|
|
36
|
+
expect(formatNumberToCompactString(hugeNumber, 2)).toBe("1,000,000T")
|
|
37
37
|
})
|
|
38
38
|
|
|
39
39
|
it("formats numbers with trailing zeros correctly", () => {
|
|
40
|
-
expect(
|
|
40
|
+
expect(formatNumberToCompactString(1_000, 2)).toBe("1K")
|
|
41
41
|
})
|
|
42
42
|
})
|
|
43
43
|
|
|
@@ -58,7 +58,7 @@ describe("roundUpNumber", () => {
|
|
|
58
58
|
describe("fromFixedPoint", () => {
|
|
59
59
|
it("converts fixed point with 18 decimals to float", () => {
|
|
60
60
|
const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
|
|
61
|
-
expect(fromFixedPoint(raw, 18
|
|
61
|
+
expect(fromFixedPoint(raw, 18)).toBe(1.2345)
|
|
62
62
|
})
|
|
63
63
|
|
|
64
64
|
it("converts with truncation to 2 decimals", () => {
|
package/src/utils/numbers.ts
CHANGED
|
@@ -2,12 +2,12 @@ type DesiredDecimals = Intl.NumberFormatOptions &
|
|
|
2
2
|
BigIntToLocaleStringOptions["minimumFractionDigits"]
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Formats a number or bigint into
|
|
5
|
+
* Formats a number or bigint into compact string with K, M, B, or T suffix.
|
|
6
6
|
* @param value The number or bigint to format.
|
|
7
7
|
* @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
|
|
8
8
|
* @returns The formatted number as a string.
|
|
9
9
|
*/
|
|
10
|
-
export function
|
|
10
|
+
export function formatNumberToCompactString(
|
|
11
11
|
value: number | bigint,
|
|
12
12
|
decimals = 4,
|
|
13
13
|
): string {
|
|
@@ -18,6 +18,29 @@ export function formatHumanReadableNumber(
|
|
|
18
18
|
})
|
|
19
19
|
}
|
|
20
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("default", {
|
|
39
|
+
minimumFractionDigits: desiredDecimals,
|
|
40
|
+
maximumFractionDigits: desiredDecimals,
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
21
44
|
/**
|
|
22
45
|
* Returns a number rounded up to the desired number of decimals.
|
|
23
46
|
* @param amount The number to round up.
|
|
@@ -41,7 +64,7 @@ export function roundUpNumber(amount: number, desiredDecimals = 2): number {
|
|
|
41
64
|
export function fromFixedPoint(
|
|
42
65
|
fixedPoint: bigint,
|
|
43
66
|
fixedPointDecimals: number,
|
|
44
|
-
desiredDecimals =
|
|
67
|
+
desiredDecimals = 4,
|
|
45
68
|
): number {
|
|
46
69
|
const fixedPointDesiredDecimalsAmount =
|
|
47
70
|
fixedPoint /
|
|
@@ -53,3 +76,45 @@ export function fromFixedPoint(
|
|
|
53
76
|
|
|
54
77
|
return formattedAmount
|
|
55
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 = true,
|
|
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
|
+
}
|