@mezo-org/passport 0.4.0-dev.1 → 0.4.0-dev.10
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 +57 -52
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +22 -53
- package/dist/src/api/auth.js.map +1 -1
- package/dist/src/api/client.d.ts +24 -0
- package/dist/src/api/client.d.ts.map +1 -0
- package/dist/src/api/client.js +54 -0
- package/dist/src/api/client.js.map +1 -0
- package/dist/src/api/fetch-error.d.ts +5 -0
- package/dist/src/api/fetch-error.d.ts.map +1 -0
- package/dist/src/api/fetch-error.js +8 -0
- package/dist/src/api/fetch-error.js.map +1 -0
- package/dist/src/api/index.d.ts +3 -0
- package/dist/src/api/index.d.ts.map +1 -0
- package/dist/src/api/index.js +3 -0
- package/dist/src/api/index.js.map +1 -0
- package/dist/src/api/portal.d.ts +32 -0
- package/dist/src/api/portal.d.ts.map +1 -0
- package/dist/src/api/portal.js +23 -0
- package/dist/src/api/portal.js.map +1 -0
- package/dist/src/assets/DefaultAvatar.d.ts +5 -0
- package/dist/src/assets/DefaultAvatar.d.ts.map +1 -0
- package/dist/src/assets/DefaultAvatar.js +21 -0
- package/dist/src/assets/DefaultAvatar.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/AccountAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAddress.js +66 -0
- package/dist/src/components/Dropdown/AccountAddress.js.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts +14 -0
- package/dist/src/components/Dropdown/AccountAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountAssets.js +44 -0
- package/dist/src/components/Dropdown/AccountAssets.js.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts +7 -0
- package/dist/src/components/Dropdown/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/AccountBalance.js +18 -0
- package/dist/src/components/Dropdown/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +30 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +9 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +69 -0
- package/dist/src/components/Dropdown/Content.js.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts +7 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js +13 -0
- package/dist/src/components/Dropdown/DisconnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts +20 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +64 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/index.d.ts +3 -0
- package/dist/src/components/Dropdown/index.d.ts.map +1 -0
- package/dist/src/components/Dropdown/index.js +2 -0
- package/dist/src/components/Dropdown/index.js.map +1 -0
- package/dist/src/components/index.d.ts +2 -0
- package/dist/src/components/index.d.ts.map +1 -0
- package/dist/src/components/index.js +2 -0
- package/dist/src/components/index.js.map +1 -0
- package/dist/src/hooks/constants.d.ts +3 -3
- package/dist/src/hooks/constants.js +4 -4
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -4
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -4
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsUSDConversion.d.ts +8 -0
- package/dist/src/hooks/useAssetsUSDConversion.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsUSDConversion.js +21 -0
- package/dist/src/hooks/useAssetsUSDConversion.js.map +1 -0
- package/dist/src/hooks/useAuthApiClient.d.ts +1 -1
- package/dist/src/hooks/useAuthApiClient.d.ts.map +1 -1
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts +73 -0
- package/dist/src/hooks/useAuthenticateWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js +69 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +41 -137
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +8 -10
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +12 -182
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +12 -18
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +45 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +74 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts +5 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +34 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -0
- package/dist/src/hooks/useGetAccountByAddress.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByAddress.js +3 -4
- package/dist/src/hooks/useGetAccountByAddress.js.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +3 -5
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.js +3 -4
- package/dist/src/hooks/useGetAccountByMezoId.js.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +14 -8
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +28 -6
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +3 -20
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useGetSession.js +2 -2
- package/dist/src/hooks/useGetSession.js.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +15 -121
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +8 -21
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -181
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +11 -22
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +12 -181
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +6 -43
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts +12 -28
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +11 -6
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +73 -0
- package/dist/src/hooks/useSignUpWithWallet.d.ts.map +1 -0
- package/dist/src/hooks/useSignUpWithWallet.js +11 -0
- package/dist/src/hooks/useSignUpWithWallet.js.map +1 -0
- package/dist/src/hooks/useUpdateMezoId.d.ts +30 -82
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +8 -12
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +12 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +28 -0
- package/dist/src/hooks/useWalletAccount.js.map +1 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +1 -0
- package/dist/src/index.js.map +1 -1
- package/dist/src/provider.d.ts +10 -13
- package/dist/src/provider.d.ts.map +1 -1
- package/dist/src/provider.js +11 -20
- package/dist/src/provider.js.map +1 -1
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +35 -0
- package/dist/src/utils/address.js.map +1 -0
- package/dist/src/utils/address.test.d.ts +2 -0
- package/dist/src/utils/address.test.d.ts.map +1 -0
- package/dist/src/utils/address.test.js +32 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +28 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +73 -0
- package/dist/src/utils/cryptoAssets.js.map +1 -0
- package/dist/src/utils/cryptoAssets.test.d.ts +2 -0
- package/dist/src/utils/cryptoAssets.test.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.test.js +50 -0
- package/dist/src/utils/cryptoAssets.test.js.map +1 -0
- package/dist/src/utils/currency.d.ts +14 -0
- package/dist/src/utils/currency.d.ts.map +1 -0
- package/dist/src/utils/currency.js +27 -0
- package/dist/src/utils/currency.js.map +1 -0
- package/dist/src/utils/currency.test.d.ts +2 -0
- package/dist/src/utils/currency.test.d.ts.map +1 -0
- package/dist/src/utils/currency.test.js +34 -0
- package/dist/src/utils/currency.test.js.map +1 -0
- package/dist/src/utils/numbers.d.ts +26 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +40 -0
- package/dist/src/utils/numbers.js.map +1 -0
- package/dist/src/utils/numbers.test.d.ts +2 -0
- package/dist/src/utils/numbers.test.d.ts.map +1 -0
- package/dist/src/utils/numbers.test.js +54 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +9 -3
- package/src/api/auth.ts +104 -129
- package/src/api/client.ts +78 -0
- package/src/api/fetch-error.ts +8 -0
- package/src/api/index.ts +2 -0
- package/src/api/portal.ts +56 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/AccountAddress.tsx +130 -0
- package/src/components/Dropdown/AccountAssets.tsx +110 -0
- package/src/components/Dropdown/AccountBalance.tsx +38 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +56 -0
- package/src/components/Dropdown/Content.tsx +148 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +111 -0
- package/src/components/Dropdown/README.md +51 -0
- package/src/components/Dropdown/WelcomeBlock.tsx +92 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/hooks/constants.ts +4 -4
- package/src/hooks/index.ts +1 -4
- package/src/hooks/useAssetsUSDConversion.ts +31 -0
- package/src/hooks/useAuthenticateWithWallet.ts +98 -0
- package/src/hooks/useCreateAccount.ts +20 -11
- package/src/hooks/useCreateSession.ts +24 -18
- package/src/hooks/useDropdownData.ts +131 -0
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +46 -0
- package/src/hooks/useGetAccountByAddress.ts +11 -5
- package/src/hooks/useGetAccountByMezoId.ts +11 -5
- package/src/hooks/useGetCurrentAccount.ts +53 -7
- package/src/hooks/useGetSession.ts +10 -3
- package/src/hooks/useLinkAccount.ts +19 -31
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useSignInWithDiscord.ts +19 -28
- package/src/hooks/useSignInWithWallet.ts +14 -54
- package/src/hooks/useSignOut.ts +22 -6
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useUpdateMezoId.ts +20 -12
- package/src/hooks/useWalletAccount.ts +53 -0
- package/src/index.ts +1 -0
- package/src/provider.ts +30 -37
- package/src/utils/address.test.ts +38 -0
- package/src/utils/address.ts +43 -0
- package/src/utils/cryptoAssets.test.ts +61 -0
- package/src/utils/cryptoAssets.ts +93 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +73 -0
- package/src/utils/numbers.ts +55 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BitcoinCircle,
|
|
3
|
+
EthCircle,
|
|
4
|
+
MUsdCircle,
|
|
5
|
+
TBtcCircle,
|
|
6
|
+
} from "@mezo-org/mezo-clay"
|
|
7
|
+
import { getCryptoAsset } from "./cryptoAssets"
|
|
8
|
+
|
|
9
|
+
// FIXME: Test fails if run in full suite (pnpm test) but works when run in isolation (pnpm test cryptoAssets)
|
|
10
|
+
|
|
11
|
+
describe("getCryptoAsset", () => {
|
|
12
|
+
it("returns the correct asset for 'btc'", () => {
|
|
13
|
+
const asset = getCryptoAsset("btc")
|
|
14
|
+
expect(asset.name).toBe("Bitcoin")
|
|
15
|
+
expect(asset.symbol).toBe("BTC")
|
|
16
|
+
expect(asset.decimals).toBe(8)
|
|
17
|
+
expect(asset.icon).toBe(BitcoinCircle)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it("returns the correct asset for 'eth'", () => {
|
|
21
|
+
const asset = getCryptoAsset("eth")
|
|
22
|
+
expect(asset.name).toBe("Ethereum")
|
|
23
|
+
expect(asset.symbol).toBe("ETH")
|
|
24
|
+
expect(asset.decimals).toBe(18)
|
|
25
|
+
expect(asset.icon).toBe(EthCircle)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it("returns the correct asset for 'tbtc'", () => {
|
|
29
|
+
const asset = getCryptoAsset("tbtc")
|
|
30
|
+
expect(asset.name).toBe("Threshold Bitcoin")
|
|
31
|
+
expect(asset.symbol).toBe("tBTC")
|
|
32
|
+
expect(asset.decimals).toBe(18)
|
|
33
|
+
expect(asset.icon).toBe(TBtcCircle)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
it("returns the correct asset for 'wbtc'", () => {
|
|
37
|
+
const asset = getCryptoAsset("wbtc")
|
|
38
|
+
expect(asset.name).toBe("Wrapped Bitcoin")
|
|
39
|
+
expect(asset.symbol).toBe("wBTC")
|
|
40
|
+
expect(asset.decimals).toBe(8)
|
|
41
|
+
// FIXME: Test should expect the exact icon component
|
|
42
|
+
expect(asset.icon).toEqual(expect.any(Function))
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it("returns the correct asset for 'mbtc'", () => {
|
|
46
|
+
const asset = getCryptoAsset("mbtc")
|
|
47
|
+
expect(asset.name).toBe("Matsnet Bitcoin")
|
|
48
|
+
expect(asset.symbol).toBe("BTC")
|
|
49
|
+
expect(asset.decimals).toBe(18)
|
|
50
|
+
// FIXME: Test should expect the exact icon component
|
|
51
|
+
expect(asset.icon).toEqual(expect.any(Function))
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it("returns the correct asset for 'musd'", () => {
|
|
55
|
+
const asset = getCryptoAsset("musd")
|
|
56
|
+
expect(asset.name).toBe("Mezo USD")
|
|
57
|
+
expect(asset.symbol).toBe("MUSD")
|
|
58
|
+
expect(asset.decimals).toBe(18)
|
|
59
|
+
expect(asset.icon).toBe(MUsdCircle)
|
|
60
|
+
})
|
|
61
|
+
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BitcoinCircle,
|
|
3
|
+
EthCircle,
|
|
4
|
+
IconProps,
|
|
5
|
+
MUsdCircle,
|
|
6
|
+
TBtcCircle,
|
|
7
|
+
} from "@mezo-org/mezo-clay"
|
|
8
|
+
import { createElement, FC } from "react"
|
|
9
|
+
|
|
10
|
+
export type CryptoAssetKey = "btc" | "eth" | "tbtc" | "wbtc" | "mbtc" | "musd"
|
|
11
|
+
|
|
12
|
+
export type CryptoAsset = {
|
|
13
|
+
name: string
|
|
14
|
+
symbol: string
|
|
15
|
+
icon: FC<IconProps>
|
|
16
|
+
decimals: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CRYPTO_ASSETS: Record<CryptoAssetKey, CryptoAsset> = {
|
|
20
|
+
btc: {
|
|
21
|
+
name: "Bitcoin",
|
|
22
|
+
symbol: "BTC",
|
|
23
|
+
icon: BitcoinCircle,
|
|
24
|
+
decimals: 8,
|
|
25
|
+
},
|
|
26
|
+
eth: {
|
|
27
|
+
name: "Ethereum",
|
|
28
|
+
symbol: "ETH",
|
|
29
|
+
icon: EthCircle,
|
|
30
|
+
decimals: 18,
|
|
31
|
+
},
|
|
32
|
+
tbtc: {
|
|
33
|
+
name: "Threshold Bitcoin",
|
|
34
|
+
symbol: "tBTC",
|
|
35
|
+
icon: TBtcCircle,
|
|
36
|
+
decimals: 18,
|
|
37
|
+
},
|
|
38
|
+
wbtc: {
|
|
39
|
+
name: "Wrapped Bitcoin",
|
|
40
|
+
symbol: "wBTC",
|
|
41
|
+
icon: (props) =>
|
|
42
|
+
createElement(BitcoinCircle, {
|
|
43
|
+
...props,
|
|
44
|
+
symbolColor: "#FF8A00",
|
|
45
|
+
color: "#000000",
|
|
46
|
+
}),
|
|
47
|
+
decimals: 8,
|
|
48
|
+
},
|
|
49
|
+
mbtc: {
|
|
50
|
+
name: "Matsnet Bitcoin",
|
|
51
|
+
symbol: "BTC",
|
|
52
|
+
icon: (props) =>
|
|
53
|
+
createElement(BitcoinCircle, {
|
|
54
|
+
...props,
|
|
55
|
+
symbolColor: "#000000",
|
|
56
|
+
color: "#FFC907",
|
|
57
|
+
}),
|
|
58
|
+
decimals: 18,
|
|
59
|
+
},
|
|
60
|
+
musd: {
|
|
61
|
+
name: "Mezo USD",
|
|
62
|
+
symbol: "MUSD",
|
|
63
|
+
icon: MUsdCircle,
|
|
64
|
+
decimals: 18,
|
|
65
|
+
},
|
|
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 getCryptoAsset(key: CryptoAssetKey) {
|
|
74
|
+
return CRYPTO_ASSETS[key]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Checks if given crypto asset is Bitcoin-like
|
|
79
|
+
* @param key The key of crypto asset
|
|
80
|
+
* @returns True if crypto asset is Bitcoin-like
|
|
81
|
+
*/
|
|
82
|
+
export function isBitcoinLikeCryptoAsset(key: CryptoAssetKey) {
|
|
83
|
+
return ["btc", "tbtc", "wbtc", "mbtc"].includes(key)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Checks if given crypto asset is USD-like
|
|
88
|
+
* @param key The key of crypto asset
|
|
89
|
+
* @returns True if crypto asset is USD-like
|
|
90
|
+
*/
|
|
91
|
+
export function isUsdLikeCryptoAsset(key: CryptoAssetKey) {
|
|
92
|
+
return ["musd"].includes(key)
|
|
93
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { formatCurrency, formatUsd } from "./currency"
|
|
2
|
+
|
|
3
|
+
describe("formatCurrency", () => {
|
|
4
|
+
it("formats a number as currency with default options", () => {
|
|
5
|
+
const result = formatCurrency(1234.5, { currency: "EUR" })
|
|
6
|
+
expect(result).toBe("€1,234.50")
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
it("respects minimumFractionDigits (min = 3, max = 3)", () => {
|
|
10
|
+
const result = formatCurrency(1.2, {
|
|
11
|
+
currency: "USD",
|
|
12
|
+
minimumFractionDigits: 3,
|
|
13
|
+
maximumFractionDigits: 3,
|
|
14
|
+
})
|
|
15
|
+
expect(result).toBe("$1.200")
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("respects maximumFractionDigits (min = 0, max = 1)", () => {
|
|
19
|
+
const result = formatCurrency(1.234567, {
|
|
20
|
+
currency: "USD",
|
|
21
|
+
minimumFractionDigits: 0,
|
|
22
|
+
maximumFractionDigits: 1,
|
|
23
|
+
})
|
|
24
|
+
expect(result).toBe("$1.2")
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
describe("formatUsd", () => {
|
|
29
|
+
it("formats a number as USD", () => {
|
|
30
|
+
const result = formatUsd(99.99)
|
|
31
|
+
expect(result).toBe("$99.99")
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("formats zero", () => {
|
|
35
|
+
const result = formatUsd(0)
|
|
36
|
+
expect(result).toBe("$0.00")
|
|
37
|
+
})
|
|
38
|
+
})
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const DEFAULT_FORMAT_OPTIONS: Intl.NumberFormatOptions = {
|
|
2
|
+
style: "currency",
|
|
3
|
+
minimumFractionDigits: 2,
|
|
4
|
+
maximumFractionDigits: 2,
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Formats a number as a currency
|
|
9
|
+
* @param value The value to format
|
|
10
|
+
* @param options The options to pass to the formatter
|
|
11
|
+
* @returns The formatted currency
|
|
12
|
+
*/
|
|
13
|
+
export function formatCurrency(
|
|
14
|
+
value: number,
|
|
15
|
+
options: Omit<Intl.NumberFormatOptions, "style"> = {},
|
|
16
|
+
): string {
|
|
17
|
+
const formatter = new Intl.NumberFormat("en-US", {
|
|
18
|
+
...DEFAULT_FORMAT_OPTIONS,
|
|
19
|
+
...options,
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return formatter.format(value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Formats a number as a USD currency
|
|
27
|
+
* @param value The value to format
|
|
28
|
+
* @returns The formatted currency
|
|
29
|
+
*/
|
|
30
|
+
export function formatUsd(value: number): string {
|
|
31
|
+
return formatCurrency(value, { currency: "USD" })
|
|
32
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {
|
|
2
|
+
formatHumanReadableNumber,
|
|
3
|
+
fromFixedPoint,
|
|
4
|
+
roundUpNumber,
|
|
5
|
+
} from "./numbers"
|
|
6
|
+
|
|
7
|
+
describe("formatHumanReadableNumber", () => {
|
|
8
|
+
it("formats number less than 1000 with decimals", () => {
|
|
9
|
+
expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46")
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
it("formats thousands with K", () => {
|
|
13
|
+
expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K")
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it("formats millions with M", () => {
|
|
17
|
+
expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M")
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it("formats billions with B", () => {
|
|
21
|
+
expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B")
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it("formats trillions with T", () => {
|
|
25
|
+
expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T")
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it("formats bigint as readable number", () => {
|
|
29
|
+
expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe(
|
|
30
|
+
"1,234,567.89T",
|
|
31
|
+
)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("caps at > 1000T for huge numbers", () => {
|
|
35
|
+
const hugeNumber = 10 ** 18 // 1e18
|
|
36
|
+
expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("1,000,000T")
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it("formats numbers with trailing zeros correctly", () => {
|
|
40
|
+
expect(formatHumanReadableNumber(1_000, 2)).toBe("1K")
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
describe("roundUpNumber", () => {
|
|
45
|
+
it("rounds up to 2 decimals", () => {
|
|
46
|
+
expect(roundUpNumber(1.234)).toBe(1.24)
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
it("rounds up to 3 decimals", () => {
|
|
50
|
+
expect(roundUpNumber(9.87654, 3)).toBe(9.877)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it("rounds up exact decimal", () => {
|
|
54
|
+
expect(roundUpNumber(2.5, 1)).toBe(2.5)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe("fromFixedPoint", () => {
|
|
59
|
+
it("converts fixed point with 18 decimals to float", () => {
|
|
60
|
+
const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
|
|
61
|
+
expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("converts with truncation to 2 decimals", () => {
|
|
65
|
+
const raw = 987654321000000000n // ~0.987654321 ETH
|
|
66
|
+
expect(fromFixedPoint(raw, 18, 2)).toBe(0.98)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it("handles large fixedPoint decimals", () => {
|
|
70
|
+
const raw = 1_000_000_000_000_000_000_000_000n // 1M with 6 decimals
|
|
71
|
+
expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
type DesiredDecimals = Intl.NumberFormatOptions &
|
|
2
|
+
BigIntToLocaleStringOptions["minimumFractionDigits"]
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
|
|
6
|
+
* @param value The number or bigint to format.
|
|
7
|
+
* @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
|
|
8
|
+
* @returns The formatted number as a string.
|
|
9
|
+
*/
|
|
10
|
+
export function formatHumanReadableNumber(
|
|
11
|
+
value: number | bigint,
|
|
12
|
+
decimals = 4,
|
|
13
|
+
): string {
|
|
14
|
+
return value.toLocaleString("en-US", {
|
|
15
|
+
notation: "compact",
|
|
16
|
+
compactDisplay: "short",
|
|
17
|
+
maximumFractionDigits: decimals as DesiredDecimals,
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 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/2078d339f4ddce79e69c78529c9d72910dd2640a/dapp/src/utils/numbersUtils.ts#L1-L3
|
|
27
|
+
*/
|
|
28
|
+
export function roundUpNumber(amount: number, desiredDecimals = 2): number {
|
|
29
|
+
return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Convert a fixed point bigint with precision `fixedPointDecimals` to a
|
|
34
|
+
* floating point number truncated to `desiredDecimals`.
|
|
35
|
+
* @param fixedPoint The fixed point bigint to convert.
|
|
36
|
+
* @param fixedPointDecimals The number of decimals in the fixed point bigint.
|
|
37
|
+
* @param desiredDecimals The number of decimals in the output number.
|
|
38
|
+
* @returns The floating point number truncated to `desiredDecimals`.
|
|
39
|
+
* @see https://github.com/tahowallet/extension/blob/f6b26b7cb4a0a0278b1fccc6319626638f1a6dfa/background/lib/fixed-point.ts#L216-L239
|
|
40
|
+
*/
|
|
41
|
+
export function fromFixedPoint(
|
|
42
|
+
fixedPoint: bigint,
|
|
43
|
+
fixedPointDecimals: number,
|
|
44
|
+
desiredDecimals = 2,
|
|
45
|
+
): number {
|
|
46
|
+
const fixedPointDesiredDecimalsAmount =
|
|
47
|
+
fixedPoint /
|
|
48
|
+
10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals))
|
|
49
|
+
|
|
50
|
+
const formattedAmount =
|
|
51
|
+
Number(fixedPointDesiredDecimalsAmount) /
|
|
52
|
+
10 ** Math.min(desiredDecimals, fixedPointDecimals)
|
|
53
|
+
|
|
54
|
+
return formattedAmount
|
|
55
|
+
}
|