@mezo-org/passport 0.4.0-dev.81 → 0.4.0-dev.83
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/api/auth.d.ts +1 -1
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Content.d.ts +0 -15
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Content.js +3 -3
- package/dist/src/components/Dropdown/Content.js.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.d.ts +0 -4
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Dropdown.js +10 -33
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -1
- package/dist/src/components/Dropdown/ListingItem.d.ts +8 -7
- package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -1
- package/dist/src/components/Dropdown/ListingItem.js +36 -28
- package/dist/src/components/Dropdown/ListingItem.js.map +1 -1
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts +2 -1
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -1
- package/dist/src/components/Dropdown/NestedViewLayout.js +13 -15
- package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -1
- package/dist/src/components/Dropdown/Receive/Receive.d.ts +1 -4
- package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Receive/Receive.js +36 -19
- package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +2 -5
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js +28 -12
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +2 -4
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountBalance.js +24 -8
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -1
- 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 +2 -3
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +34 -39
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -1
- package/dist/src/components/Dropdown/Root/Root.d.ts +0 -15
- package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/Root.js +22 -34
- package/dist/src/components/Dropdown/Root/Root.js.map +1 -1
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +2 -6
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/WalletAddress.js +43 -34
- package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -1
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +2 -4
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -1
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js +60 -16
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -1
- 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/config.d.ts +1 -11
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +3 -40
- package/dist/src/config.js.map +1 -1
- package/dist/src/constants.d.ts +0 -2
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +0 -2
- package/dist/src/constants.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts +8 -13
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.js +44 -67
- package/dist/src/hooks/useAssetsConversionRates.js.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -1
- package/dist/src/hooks/useBorrowData.d.ts +31 -5
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -1
- package/dist/src/hooks/useBorrowData.js +53 -11
- package/dist/src/hooks/useBorrowData.js.map +1 -1
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +3 -3
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +2 -2
- 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.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +3 -3
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/useTokensBalances.d.ts +36 -35
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -1
- package/dist/src/hooks/useTokensBalances.js +93 -52
- package/dist/src/hooks/useTokensBalances.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +8 -10
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -1
- package/dist/src/hooks/useWalletAccount.js +22 -19
- package/dist/src/hooks/useWalletAccount.js.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/lib/contracts/index.d.ts +1 -1
- package/dist/src/lib/contracts/index.d.ts.map +1 -1
- package/dist/src/lib/contracts/index.js +4 -0
- package/dist/src/lib/contracts/index.js.map +1 -1
- 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/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 +6 -3
- package/dist/src/utils/currency.d.ts.map +1 -1
- package/dist/src/utils/currency.js +13 -10
- package/dist/src/utils/currency.js.map +1 -1
- package/dist/src/utils/currency.test.js +44 -2
- package/dist/src/utils/currency.test.js.map +1 -1
- package/dist/src/utils/numbers.d.ts +13 -53
- package/dist/src/utils/numbers.d.ts.map +1 -1
- package/dist/src/utils/numbers.js +16 -118
- package/dist/src/utils/numbers.js.map +1 -1
- package/dist/src/utils/numbers.test.js +24 -142
- package/dist/src/utils/numbers.test.js.map +1 -1
- package/package.json +2 -1
- package/src/api/auth.ts +1 -1
- package/src/components/Dropdown/Content.tsx +3 -48
- package/src/components/Dropdown/Dropdown.tsx +7 -55
- package/src/components/Dropdown/ListingItem.tsx +155 -59
- package/src/components/Dropdown/NestedViewLayout.tsx +32 -20
- package/src/components/Dropdown/Receive/Receive.tsx +69 -32
- package/src/components/Dropdown/Root/AccountAddressActions.tsx +64 -35
- package/src/components/Dropdown/Root/AccountBalance.tsx +54 -16
- 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 +63 -46
- package/src/components/Dropdown/Root/Root.tsx +28 -98
- package/src/components/Dropdown/Root/WalletAddress.tsx +95 -89
- package/src/components/Dropdown/Root/WelcomeBlock.tsx +109 -29
- package/src/components/Dropdown/SlotNumber.tsx +131 -0
- package/src/config.ts +3 -59
- package/src/constants.ts +0 -6
- package/src/hooks/useAssetsConversionRates.ts +49 -67
- package/src/hooks/useAuthenticateWithWallet.ts +7 -5
- package/src/hooks/useBorrowData.ts +71 -12
- package/src/hooks/useCreateAccount.ts +5 -4
- package/src/hooks/useGetCurrentAccount.ts +5 -7
- package/src/hooks/useLinkAccount.ts +5 -4
- package/src/hooks/useTokensBalances.ts +152 -74
- package/src/hooks/useWalletAccount.ts +27 -36
- package/src/index.ts +0 -1
- package/src/lib/contracts/index.ts +8 -1
- package/src/provider.ts +11 -3
- package/src/utils/assets.test.ts +57 -0
- package/src/utils/assets.ts +103 -0
- package/src/utils/currency.test.ts +76 -2
- package/src/utils/currency.ts +20 -15
- package/src/utils/numbers.test.ts +29 -180
- package/src/utils/numbers.ts +22 -171
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +0 -11
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +0 -1
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js +0 -9
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +0 -1
- package/dist/src/hooks/useDropdownData.d.ts +0 -47
- package/dist/src/hooks/useDropdownData.d.ts.map +0 -1
- package/dist/src/hooks/useDropdownData.js +0 -97
- package/dist/src/hooks/useDropdownData.js.map +0 -1
- package/dist/src/utils/cryptoAssets.d.ts +0 -44
- package/dist/src/utils/cryptoAssets.d.ts.map +0 -1
- package/dist/src/utils/cryptoAssets.js +0 -129
- 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 -67
- package/dist/src/utils/cryptoAssets.test.js.map +0 -1
- package/src/components/Dropdown/Root/AccountAssetItem.tsx +0 -26
- package/src/hooks/useDropdownData.ts +0 -152
- package/src/utils/cryptoAssets.test.ts +0 -79
- package/src/utils/cryptoAssets.ts +0 -171
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const ASSETS = {
|
|
2
|
+
BTC: {
|
|
3
|
+
name: "Bitcoin",
|
|
4
|
+
symbol: "BTC",
|
|
5
|
+
decimals: 18,
|
|
6
|
+
},
|
|
7
|
+
ETH: {
|
|
8
|
+
name: "Ethereum",
|
|
9
|
+
symbol: "ETH",
|
|
10
|
+
decimals: 18,
|
|
11
|
+
},
|
|
12
|
+
MUSD: {
|
|
13
|
+
name: "MUSD",
|
|
14
|
+
symbol: "MUSD",
|
|
15
|
+
decimals: 18,
|
|
16
|
+
},
|
|
17
|
+
mDAI: {
|
|
18
|
+
name: "Mezo Dai Stablecoin",
|
|
19
|
+
symbol: "mDAI",
|
|
20
|
+
decimals: 18,
|
|
21
|
+
},
|
|
22
|
+
mFBTC: {
|
|
23
|
+
name: "Mezo Fire Bitcoin",
|
|
24
|
+
symbol: "mFBTC",
|
|
25
|
+
decimals: 8,
|
|
26
|
+
},
|
|
27
|
+
mcbBTC: {
|
|
28
|
+
name: "Mezo Coinbase Wrapped BTC",
|
|
29
|
+
symbol: "mcbBTC",
|
|
30
|
+
decimals: 8,
|
|
31
|
+
},
|
|
32
|
+
mSolvBTC: {
|
|
33
|
+
name: "Mezo SolvBTC",
|
|
34
|
+
symbol: "mSolvBTC",
|
|
35
|
+
decimals: 18,
|
|
36
|
+
},
|
|
37
|
+
mswBTC: {
|
|
38
|
+
name: "Mezo swBTC",
|
|
39
|
+
symbol: "mswBTC",
|
|
40
|
+
decimals: 8,
|
|
41
|
+
},
|
|
42
|
+
mT: {
|
|
43
|
+
name: "Mezo Threshold Network Token",
|
|
44
|
+
symbol: "mT",
|
|
45
|
+
decimals: 18,
|
|
46
|
+
},
|
|
47
|
+
mUSDC: {
|
|
48
|
+
name: "Mezo Circle USDC",
|
|
49
|
+
symbol: "mUSDC",
|
|
50
|
+
decimals: 6,
|
|
51
|
+
},
|
|
52
|
+
mUSDe: {
|
|
53
|
+
name: "Mezo Ethena USDe",
|
|
54
|
+
symbol: "mUSDe",
|
|
55
|
+
decimals: 18,
|
|
56
|
+
},
|
|
57
|
+
mUSDT: {
|
|
58
|
+
name: "Mezo Tether USDe",
|
|
59
|
+
symbol: "mUSDT",
|
|
60
|
+
decimals: 6,
|
|
61
|
+
},
|
|
62
|
+
mxSolvBTC: {
|
|
63
|
+
name: "Mezo xSolvBTC",
|
|
64
|
+
symbol: "mxSolvBTC",
|
|
65
|
+
decimals: 18,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Gets details of given crypto asset
|
|
70
|
+
* @param key The key of crypto asset
|
|
71
|
+
* @returns The crypto asset details
|
|
72
|
+
*/
|
|
73
|
+
export function getAsset(key) {
|
|
74
|
+
return ASSETS[key];
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Checks if given crypto asset is Bitcoin-like
|
|
78
|
+
* @param key The key of crypto asset
|
|
79
|
+
* @returns True if crypto asset is Bitcoin-like
|
|
80
|
+
*/
|
|
81
|
+
export function isBitcoinLikeCryptoAsset(key) {
|
|
82
|
+
return /(btc)/i.test(key);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks if given crypto asset is USD-like
|
|
86
|
+
* @param key The key of crypto asset
|
|
87
|
+
* @returns True if crypto asset is USD-like
|
|
88
|
+
*/
|
|
89
|
+
export function isUsdLikeCryptoAsset(key) {
|
|
90
|
+
return /(usd|dai)/i.test(key);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Checks if given crypto asset is T token
|
|
94
|
+
* @param key The key of crypto asset
|
|
95
|
+
* @returns True if crypto asset is T token
|
|
96
|
+
*/
|
|
97
|
+
export function isTTokenCryptoAsset(key) {
|
|
98
|
+
return /^(mt|t)$/i.test(key);
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=assets.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.js","sourceRoot":"","sources":["../../../src/utils/assets.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,GAAG;IACb,GAAG,EAAE;QACH,IAAI,EAAE,SAAS;QACf,MAAM,EAAE,KAAK;QACb,QAAQ,EAAE,EAAE;KACb;IACD,GAAG,EAAE;QACH,IAAI,EAAE,UAAU;QAChB,MAAM,EAAE,KAAK;QACb,QAAQ,EAAE,EAAE;KACb;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,MAAM;QACZ,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,EAAE;KACb;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,qBAAqB;QAC3B,MAAM,EAAE,MAAM;QACd,QAAQ,EAAE,EAAE;KACb;IACD,KAAK,EAAE;QACL,IAAI,EAAE,mBAAmB;QACzB,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,CAAC;KACZ;IACD,MAAM,EAAE;QACN,IAAI,EAAE,2BAA2B;QACjC,MAAM,EAAE,QAAQ;QAChB,QAAQ,EAAE,CAAC;KACZ;IACD,QAAQ,EAAE;QACR,IAAI,EAAE,cAAc;QACpB,MAAM,EAAE,UAAU;QAClB,QAAQ,EAAE,EAAE;KACb;IACD,MAAM,EAAE;QACN,IAAI,EAAE,YAAY;QAClB,MAAM,EAAE,QAAQ;QAChB,QAAQ,EAAE,CAAC;KACZ;IACD,EAAE,EAAE;QACF,IAAI,EAAE,8BAA8B;QACpC,MAAM,EAAE,IAAI;QACZ,QAAQ,EAAE,EAAE;KACb;IACD,KAAK,EAAE;QACL,IAAI,EAAE,kBAAkB;QACxB,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,CAAC;KACZ;IACD,KAAK,EAAE;QACL,IAAI,EAAE,kBAAkB;QACxB,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,EAAE;KACb;IACD,KAAK,EAAE;QACL,IAAI,EAAE,kBAAkB;QACxB,MAAM,EAAE,OAAO;QACf,QAAQ,EAAE,CAAC;KACZ;IACD,SAAS,EAAE;QACT,IAAI,EAAE,eAAe;QACrB,MAAM,EAAE,WAAW;QACnB,QAAQ,EAAE,EAAE;KACb;CACF,CAAA;AAED;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAwB;IAC/C,OAAO,MAAM,CAAC,GAAG,CAAC,CAAA;AACpB,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CAAC,GAAW;IAClD,OAAO,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC3B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,oBAAoB,CAAC,GAAW;IAC9C,OAAO,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC/B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,GAAW;IAC7C,OAAO,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;AAC9B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.test.d.ts","sourceRoot":"","sources":["../../../src/utils/assets.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { getAsset, isBitcoinLikeCryptoAsset, isUsdLikeCryptoAsset, isTTokenCryptoAsset, } from "./assets";
|
|
2
|
+
describe("getAsset", () => {
|
|
3
|
+
it("should return correct asset data", () => {
|
|
4
|
+
const asset = getAsset("BTC");
|
|
5
|
+
expect(asset).toEqual({
|
|
6
|
+
name: "Bitcoin",
|
|
7
|
+
symbol: "BTC",
|
|
8
|
+
decimals: 18,
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
describe("isBitcoinLikeCryptoAsset", () => {
|
|
13
|
+
it('should return true for keys containing "btc"', () => {
|
|
14
|
+
expect(isBitcoinLikeCryptoAsset("BTC")).toBe(true);
|
|
15
|
+
expect(isBitcoinLikeCryptoAsset("mFBTC")).toBe(true);
|
|
16
|
+
expect(isBitcoinLikeCryptoAsset("mcbBTC")).toBe(true);
|
|
17
|
+
expect(isBitcoinLikeCryptoAsset("mxSolvBTC")).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
it("should return false for non-Bitcoin assets", () => {
|
|
20
|
+
expect(isBitcoinLikeCryptoAsset("ETH")).toBe(false);
|
|
21
|
+
expect(isBitcoinLikeCryptoAsset("mUSDC")).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
describe("isUsdLikeCryptoAsset", () => {
|
|
25
|
+
it('should return true for keys containing "usd" or "dai"', () => {
|
|
26
|
+
expect(isUsdLikeCryptoAsset("mUSDC")).toBe(true);
|
|
27
|
+
expect(isUsdLikeCryptoAsset("mUSDT")).toBe(true);
|
|
28
|
+
expect(isUsdLikeCryptoAsset("mUSDe")).toBe(true);
|
|
29
|
+
expect(isUsdLikeCryptoAsset("mDAI")).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
it("should return false for other tokens", () => {
|
|
32
|
+
expect(isUsdLikeCryptoAsset("BTC")).toBe(false);
|
|
33
|
+
expect(isUsdLikeCryptoAsset("mT")).toBe(false);
|
|
34
|
+
});
|
|
35
|
+
});
|
|
36
|
+
describe("isTTokenCryptoAsset", () => {
|
|
37
|
+
it('should return true for keys like "mT" or "t"', () => {
|
|
38
|
+
expect(isTTokenCryptoAsset("mT")).toBe(true);
|
|
39
|
+
expect(isTTokenCryptoAsset("T")).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
it("should return false for other tokens", () => {
|
|
42
|
+
expect(isTTokenCryptoAsset("BTC")).toBe(false);
|
|
43
|
+
expect(isTTokenCryptoAsset("mUSDT")).toBe(false);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
//# sourceMappingURL=assets.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assets.test.js","sourceRoot":"","sources":["../../../src/utils/assets.test.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,wBAAwB,EACxB,oBAAoB,EACpB,mBAAmB,GACpB,MAAM,UAAU,CAAA;AAEjB,QAAQ,CAAC,UAAU,EAAE,GAAG,EAAE;IACxB,EAAE,CAAC,kCAAkC,EAAE,GAAG,EAAE;QAC1C,MAAM,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC7B,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC;YACpB,IAAI,EAAE,SAAS;YACf,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,EAAE;SACb,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA;AAEF,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACxC,EAAE,CAAC,8CAA8C,EAAE,GAAG,EAAE;QACtD,MAAM,CAAC,wBAAwB,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAClD,MAAM,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACpD,MAAM,CAAC,wBAAwB,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QACrD,MAAM,CAAC,wBAAwB,CAAC,WAAW,CAAC,CAAC,CAA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AC5C,MAAM,CAAC,mBAAmB,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;IAEF,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;QAC9C,MAAM,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC9C,MAAM,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IAClD,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -4,13 +4,13 @@
|
|
|
4
4
|
* @param options The options to pass to the formatter
|
|
5
5
|
* @returns The formatted currency
|
|
6
6
|
*/
|
|
7
|
-
export declare function formatCurrency(value: number, options
|
|
7
|
+
export declare function formatCurrency(value: number, options: Omit<Intl.NumberFormatOptions, "style">): string;
|
|
8
8
|
/**
|
|
9
9
|
* Formats a number as a USD currency
|
|
10
10
|
* @param value The value to format
|
|
11
11
|
* @returns The formatted currency
|
|
12
12
|
*/
|
|
13
|
-
export declare function formatUsd(value: number): string;
|
|
13
|
+
export declare function formatUsd(value: number | string): string;
|
|
14
14
|
/**
|
|
15
15
|
* Converts an asset amount to USD based on a conversion rate and decimals
|
|
16
16
|
* @param assetAmount - The amount of the asset in its smallest unit (e.g., satoshis for BTC)
|
|
@@ -18,5 +18,8 @@ export declare function formatUsd(value: number): string;
|
|
|
18
18
|
* @param decimals - The number of decimals for the asset
|
|
19
19
|
* @returns - The value in USD as a number
|
|
20
20
|
*/
|
|
21
|
-
export declare function convertToUsd(assetAmount: bigint, assetDecimals: number, conversionRate: bigint, conversionRateDecimals: number):
|
|
21
|
+
export declare function convertToUsd(assetAmount: bigint, assetDecimals: number, conversionRate: bigint, conversionRateDecimals: number): {
|
|
22
|
+
value: bigint;
|
|
23
|
+
formatted: string;
|
|
24
|
+
};
|
|
22
25
|
//# sourceMappingURL=currency.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../../src/utils/currency.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../../src/utils/currency.ts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,IAAI,CAAC,IAAI,CAAC,mBAAmB,EAAE,OAAO,CAAC,GAC/C,MAAM,CASR;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAExD;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAC1B,WAAW,EAAE,MAAM,EACnB,aAAa,EAAE,MAAM,EACrB,cAAc,EAAE,MAAM,EACtB,sBAAsB,EAAE,MAAM;;;EAc/B"}
|
|
@@ -1,18 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
style: "currency",
|
|
4
|
-
minimumFractionDigits: 2,
|
|
5
|
-
maximumFractionDigits: 2,
|
|
6
|
-
};
|
|
1
|
+
import { formatUnits } from "viem";
|
|
2
|
+
import { normalizePrecision } from "./numbers";
|
|
7
3
|
/**
|
|
8
4
|
* Formats a number as a currency
|
|
9
5
|
* @param value The value to format
|
|
10
6
|
* @param options The options to pass to the formatter
|
|
11
7
|
* @returns The formatted currency
|
|
12
8
|
*/
|
|
13
|
-
export function formatCurrency(value, options
|
|
9
|
+
export function formatCurrency(value, options) {
|
|
14
10
|
const formatter = new Intl.NumberFormat("en-US", {
|
|
15
|
-
|
|
11
|
+
style: "currency",
|
|
12
|
+
minimumFractionDigits: 2,
|
|
13
|
+
maximumFractionDigits: 2,
|
|
16
14
|
...options,
|
|
17
15
|
});
|
|
18
16
|
return formatter.format(value);
|
|
@@ -23,7 +21,7 @@ export function formatCurrency(value, options = {}) {
|
|
|
23
21
|
* @returns The formatted currency
|
|
24
22
|
*/
|
|
25
23
|
export function formatUsd(value) {
|
|
26
|
-
return formatCurrency(value, { currency: "USD" });
|
|
24
|
+
return formatCurrency(Number(value), { currency: "USD" });
|
|
27
25
|
}
|
|
28
26
|
/**
|
|
29
27
|
* Converts an asset amount to USD based on a conversion rate and decimals
|
|
@@ -33,6 +31,11 @@ export function formatUsd(value) {
|
|
|
33
31
|
* @returns - The value in USD as a number
|
|
34
32
|
*/
|
|
35
33
|
export function convertToUsd(assetAmount, assetDecimals, conversionRate, conversionRateDecimals) {
|
|
36
|
-
|
|
34
|
+
const value = normalizePrecision(assetAmount * conversionRate, assetDecimals + conversionRateDecimals, conversionRateDecimals);
|
|
35
|
+
const formatted = formatUnits(value, conversionRateDecimals);
|
|
36
|
+
return {
|
|
37
|
+
value,
|
|
38
|
+
formatted,
|
|
39
|
+
};
|
|
37
40
|
}
|
|
38
41
|
//# sourceMappingURL=currency.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"currency.js","sourceRoot":"","sources":["../../../src/utils/currency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"currency.js","sourceRoot":"","sources":["../../../src/utils/currency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,MAAM,CAAA;AAClC,OAAO,EAAE,kBAAkB,EAAE,MAAM,WAAW,CAAA;AAE9C;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAa,EACb,OAAgD;IAEhD,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE;QAC/C,KAAK,EAAE,UAAU;QACjB,qBAAqB,EAAE,CAAC;QACxB,qBAAqB,EAAE,CAAC;QACxB,GAAG,OAAO;KACX,CAAC,CAAA;IAEF,OAAO,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAChC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,SAAS,CAAC,KAAsB;IAC9C,OAAO,cAAc,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE,CAAC,CAAA;AAC3D,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAC1B,WAAmB,EACnB,aAAqB,EACrB,cAAsB,EACtB,sBAA8B;IAE9B,MAAM,KAAK,GAAG,kBAAkB,CAC9B,WAAW,GAAG,cAAc,EAC5B,aAAa,GAAG,sBAAsB,EACtC,sBAAsB,CACvB,CAAA;IAED,MAAM,SAAS,GAAG,WAAW,CAAC,KAAK,EAAE,sBAAsB,CAAC,CAAA;IAE5D,OAAO;QACL,KAAK;QACL,SAAS;KACV,CAAA;AACH,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { formatCurrency, formatUsd } from "./currency";
|
|
1
|
+
import { convertToUsd, formatCurrency, formatUsd } from "./currency";
|
|
2
2
|
describe("formatCurrency", () => {
|
|
3
3
|
it("formats a number as currency with default options", () => {
|
|
4
4
|
const result = formatCurrency(1234.5, { currency: "EUR" });
|
|
@@ -31,5 +31,47 @@ describe("formatUsd", () => {
|
|
|
31
31
|
expect(result).toBe("$0.00");
|
|
32
32
|
});
|
|
33
33
|
});
|
|
34
|
-
|
|
34
|
+
describe("convertToUsd", () => {
|
|
35
|
+
it("converts 1 BTC (8 decimals) with $30,000 rate (6 decimals)", () => {
|
|
36
|
+
const assetAmount = 100000000n; // 1 BTC
|
|
37
|
+
const assetDecimals = 8;
|
|
38
|
+
const conversionRate = 30000000000n; // $30,000 in 6 decimals
|
|
39
|
+
const conversionRateDecimals = 6;
|
|
40
|
+
const result = convertToUsd(assetAmount, assetDecimals, conversionRate, conversionRateDecimals);
|
|
41
|
+
expect(result.value).toBe(30000000000n);
|
|
42
|
+
expect(result.formatted).toBe("30000");
|
|
43
|
+
});
|
|
44
|
+
it("converts 0.5 BTC correctly", () => {
|
|
45
|
+
const assetAmount = 50000000n; // 0.5 BTC
|
|
46
|
+
const assetDecimals = 8;
|
|
47
|
+
const conversionRate = 40000000000n; // $40,000
|
|
48
|
+
const conversionRateDecimals = 6;
|
|
49
|
+
const result = convertToUsd(assetAmount, assetDecimals, conversionRate, conversionRateDecimals);
|
|
50
|
+
expect(result.value).toBe(20000000000n);
|
|
51
|
+
expect(result.formatted).toBe("20000");
|
|
52
|
+
});
|
|
53
|
+
it("converts 1 ETH (18 decimals) with $2,000 rate (6 decimals)", () => {
|
|
54
|
+
const assetAmount = 1000000000000000000n; // 1 ETH
|
|
55
|
+
const assetDecimals = 18;
|
|
56
|
+
const conversionRate = 2000000000n;
|
|
57
|
+
const conversionRateDecimals = 6;
|
|
58
|
+
const result = convertToUsd(assetAmount, assetDecimals, conversionRate, conversionRateDecimals);
|
|
59
|
+
expect(result.value).toBe(2000000000n);
|
|
60
|
+
expect(result.formatted).toBe("2000");
|
|
61
|
+
});
|
|
62
|
+
it("handles zero amount correctly", () => {
|
|
63
|
+
const result = convertToUsd(0n, 18, 999999999n, 6);
|
|
64
|
+
expect(result.value).toBe(0n);
|
|
65
|
+
expect(result.formatted).toBe("0");
|
|
66
|
+
});
|
|
67
|
+
it("handles assets with fewer decimals than conversion rate", () => {
|
|
68
|
+
const assetAmount = 100n;
|
|
69
|
+
const assetDecimals = 2;
|
|
70
|
+
const conversionRate = 1500000000n; // $1500
|
|
71
|
+
const conversionRateDecimals = 6;
|
|
72
|
+
const result = convertToUsd(assetAmount, assetDecimals, conversionRate, conversionRateDecimals);
|
|
73
|
+
expect(result.value).toBe(1500000000n);
|
|
74
|
+
expect(result.formatted).toBe("1500");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
35
77
|
//# sourceMappingURL=currency.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"currency.test.js","sourceRoot":"","sources":["../../../src/utils/currency.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"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,CAAA;IACvC,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -1,65 +1,25 @@
|
|
|
1
|
+
type CompactStringDecimals = Intl.NumberFormatOptions["minimumFractionDigits"] & BigIntToLocaleStringOptions["minimumFractionDigits"];
|
|
1
2
|
/**
|
|
2
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 formatNumberToCompactString(value: number | bigint, decimals?:
|
|
8
|
+
export declare function formatNumberToCompactString(value: number | bigint, decimals?: CompactStringDecimals): string;
|
|
8
9
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @param value The
|
|
11
|
-
* @param
|
|
12
|
-
* @
|
|
13
|
-
* @
|
|
14
|
-
*/
|
|
15
|
-
export declare function formatNumberToLocaleString(value: string | number, desiredDecimals?: number): string;
|
|
16
|
-
/**
|
|
17
|
-
* Returns a number rounded up to the desired number of decimals.
|
|
18
|
-
* @param amount The number to round up.
|
|
19
|
-
* @param desiredDecimals The number of decimals to round up to.
|
|
20
|
-
* @returns The rounded up number.
|
|
21
|
-
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
|
|
22
|
-
*/
|
|
23
|
-
export declare function roundUpNumber(amount: number, desiredDecimals?: number): number;
|
|
24
|
-
/**
|
|
25
|
-
* Convert a fixed point bigint with precision `fixedPointDecimals` to a
|
|
26
|
-
* floating point number truncated to `desiredDecimals`.
|
|
27
|
-
* @param fixedPoint The fixed point bigint to convert.
|
|
28
|
-
* @param fixedPointDecimals The number of decimals in the fixed point bigint.
|
|
29
|
-
* @param desiredDecimals The number of decimals in the output number.
|
|
30
|
-
* @returns The floating point number truncated to `desiredDecimals`.
|
|
31
|
-
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
32
|
-
*/
|
|
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;
|
|
45
|
-
/**
|
|
46
|
-
* Converts a floating-point number or string to a bigint representation
|
|
47
|
-
* scaled by the specified number of decimal places (default: 18).
|
|
48
|
-
*
|
|
49
|
-
* This function avoids floating-point precision issues by manually
|
|
50
|
-
* parsing and scaling the number.
|
|
51
|
-
*
|
|
52
|
-
* @param amount - A number or string containing a floating point number.
|
|
53
|
-
* @param decimals - Number of decimals to scale to (default: 18).
|
|
54
|
-
* @returns A bigint representation scaled to the given decimal places.
|
|
55
|
-
* @see https://github.com/thesis/mezo-portal/blob/3bd4f39cb77c39948af2886a6ce7415a0c60307a/dapp/src/shared/utils/numbers.ts#L55
|
|
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.
|
|
56
15
|
*/
|
|
57
|
-
export declare const
|
|
16
|
+
export declare const normalizePrecision: (value: bigint, currentPrecision: number, targetPrecision?: number) => bigint;
|
|
58
17
|
/**
|
|
59
|
-
* Utility function to find the maximum value from
|
|
60
|
-
* @param values Array of
|
|
61
|
-
* @returns The maximum value from the provided
|
|
62
|
-
* @dev Works exactly like `Math.max` but for
|
|
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.
|
|
63
22
|
*/
|
|
64
23
|
export declare const bigIntMax: (...values: bigint[]) => bigint;
|
|
24
|
+
export {};
|
|
65
25
|
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/utils/numbers.ts"],"names":[],"mappings":"
|
|
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"}
|
|
@@ -4,7 +4,7 @@
|
|
|
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 formatNumberToCompactString(value, decimals =
|
|
7
|
+
export function formatNumberToCompactString(value, decimals = 2) {
|
|
8
8
|
return value.toLocaleString("en-US", {
|
|
9
9
|
notation: "compact",
|
|
10
10
|
compactDisplay: "short",
|
|
@@ -12,128 +12,26 @@ export function formatNumberToCompactString(value, decimals = 4) {
|
|
|
12
12
|
});
|
|
13
13
|
}
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
16
|
-
* @param value The
|
|
17
|
-
* @param
|
|
18
|
-
* @
|
|
19
|
-
* @
|
|
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("en-US", {
|
|
28
|
-
minimumFractionDigits: desiredDecimals,
|
|
29
|
-
maximumFractionDigits: desiredDecimals,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Returns a number rounded up to the desired number of decimals.
|
|
34
|
-
* @param amount The number to round up.
|
|
35
|
-
* @param desiredDecimals The number of decimals to round up to.
|
|
36
|
-
* @returns The rounded up number.
|
|
37
|
-
* @see https://github.com/thesis/acre/blob/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
|
|
38
|
-
*/
|
|
39
|
-
export function roundUpNumber(amount, desiredDecimals = 2) {
|
|
40
|
-
return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals;
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Convert a fixed point bigint with precision `fixedPointDecimals` to a
|
|
44
|
-
* floating point number truncated to `desiredDecimals`.
|
|
45
|
-
* @param fixedPoint The fixed point bigint to convert.
|
|
46
|
-
* @param fixedPointDecimals The number of decimals in the fixed point bigint.
|
|
47
|
-
* @param desiredDecimals The number of decimals in the output number.
|
|
48
|
-
* @returns The floating point number truncated to `desiredDecimals`.
|
|
49
|
-
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
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.
|
|
50
20
|
*/
|
|
51
|
-
export
|
|
52
|
-
|
|
53
|
-
10n ** BigInt(
|
|
54
|
-
const formattedAmount = Number(fixedPointDesiredDecimalsAmount) /
|
|
55
|
-
10 ** Math.min(desiredDecimals, fixedPointDecimals);
|
|
56
|
-
return formattedAmount;
|
|
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 = false) => {
|
|
69
|
-
const fixedPoint = BigInt(amount);
|
|
70
|
-
if (fixedPoint === 0n) {
|
|
71
|
-
return `0.${"0".repeat(desiredDecimals)}`;
|
|
21
|
+
export const normalizePrecision = (value, currentPrecision, targetPrecision = currentPrecision) => {
|
|
22
|
+
if (currentPrecision > targetPrecision) {
|
|
23
|
+
return value / 10n ** BigInt(currentPrecision - targetPrecision);
|
|
72
24
|
}
|
|
73
|
-
|
|
74
|
-
|
|
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
|
-
};
|
|
86
|
-
/**
|
|
87
|
-
* Converts a floating-point number or string to a bigint representation
|
|
88
|
-
* scaled by the specified number of decimal places (default: 18).
|
|
89
|
-
*
|
|
90
|
-
* This function avoids floating-point precision issues by manually
|
|
91
|
-
* parsing and scaling the number.
|
|
92
|
-
*
|
|
93
|
-
* @param amount - A number or string containing a floating point number.
|
|
94
|
-
* @param decimals - Number of decimals to scale to (default: 18).
|
|
95
|
-
* @returns A bigint representation scaled to the given decimal places.
|
|
96
|
-
* @see https://github.com/thesis/mezo-portal/blob/3bd4f39cb77c39948af2886a6ce7415a0c60307a/dapp/src/shared/utils/numbers.ts#L55
|
|
97
|
-
*/
|
|
98
|
-
export const fromFloatToBigInt = (amount, decimals = 18) => {
|
|
99
|
-
const FLOATING_POINT_REGEX = /^-?\d{1,3}(,\d{3})*(\.\d+)?$|^-?\d+(\.\d+)?$/;
|
|
100
|
-
const parseToFixedPointNumber = (floatingPointString) => {
|
|
101
|
-
if (!floatingPointString.match(FLOATING_POINT_REGEX)) {
|
|
102
|
-
return undefined;
|
|
103
|
-
}
|
|
104
|
-
const [whole, decimalsPart, ...extra] = floatingPointString.split(".");
|
|
105
|
-
if (extra.length > 0)
|
|
106
|
-
return undefined;
|
|
107
|
-
const noThousandsSeparatorWhole = whole.replace(/,/g, "");
|
|
108
|
-
const setDecimals = decimalsPart ?? "";
|
|
109
|
-
try {
|
|
110
|
-
return {
|
|
111
|
-
amount: BigInt([noThousandsSeparatorWhole, setDecimals].join("")),
|
|
112
|
-
decimals: setDecimals.length,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
catch {
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
118
|
-
};
|
|
119
|
-
const convertFixedPoint = (fixedPoint, fixedPointDecimals, targetDecimals) => {
|
|
120
|
-
if (fixedPointDecimals >= targetDecimals) {
|
|
121
|
-
return fixedPoint / 10n ** BigInt(fixedPointDecimals - targetDecimals);
|
|
122
|
-
}
|
|
123
|
-
return fixedPoint * 10n ** BigInt(targetDecimals - fixedPointDecimals);
|
|
124
|
-
};
|
|
125
|
-
const parsedAmount = amount.toString();
|
|
126
|
-
const fixedPointAmount = parseToFixedPointNumber(parsedAmount);
|
|
127
|
-
if (typeof fixedPointAmount === "undefined") {
|
|
128
|
-
return 0n;
|
|
25
|
+
if (currentPrecision < targetPrecision) {
|
|
26
|
+
return value * 10n ** BigInt(targetPrecision - currentPrecision);
|
|
129
27
|
}
|
|
130
|
-
return
|
|
28
|
+
return value;
|
|
131
29
|
};
|
|
132
30
|
/**
|
|
133
|
-
* Utility function to find the maximum value from
|
|
134
|
-
* @param values Array of
|
|
135
|
-
* @returns The maximum value from the provided
|
|
136
|
-
* @dev Works exactly like `Math.max` but for
|
|
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.
|
|
137
35
|
*/
|
|
138
36
|
export const bigIntMax = (...values) => values.reduce((max, value) => (value > max ? value : max));
|
|
139
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"}
|