@mezo-org/passport 0.4.0-dev.4 → 0.4.0-dev.6
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 +36 -59
- package/dist/src/api/auth.d.ts.map +1 -1
- package/dist/src/api/auth.js +21 -49
- 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 +58 -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 +8 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +65 -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 +18 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +42 -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 +1 -0
- package/dist/src/hooks/constants.d.ts.map +1 -1
- package/dist/src/hooks/constants.js +1 -0
- package/dist/src/hooks/constants.js.map +1 -1
- package/dist/src/hooks/index.d.ts +1 -1
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +1 -1
- 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 +70 -0
- package/dist/src/hooks/useAuthenticateWithWallet.js.map +1 -0
- package/dist/src/hooks/useCreateAccount.d.ts +23 -173
- package/dist/src/hooks/useCreateAccount.d.ts.map +1 -1
- package/dist/src/hooks/useCreateAccount.js +7 -4
- package/dist/src/hooks/useCreateAccount.js.map +1 -1
- package/dist/src/hooks/useCreateSession.d.ts +12 -101
- package/dist/src/hooks/useCreateSession.d.ts.map +1 -1
- package/dist/src/hooks/useCreateSession.js +10 -8
- package/dist/src/hooks/useCreateSession.js.map +1 -1
- package/dist/src/hooks/useDropdownData.d.ts +44 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +73 -0
- package/dist/src/hooks/useDropdownData.js.map +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.d.ts.map +1 -1
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js +1 -0
- package/dist/src/hooks/useEnsureNoSessionAndFetchNonce.js.map +1 -1
- package/dist/src/hooks/useGetAccountByAddress.d.ts +2 -2
- package/dist/src/hooks/useGetAccountByAddress.d.ts.map +1 -1
- package/dist/src/hooks/useGetAccountByMezoId.d.ts +2 -2
- package/dist/src/hooks/useGetAccountByMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.d.ts +16 -2
- package/dist/src/hooks/useGetCurrentAccount.d.ts.map +1 -1
- package/dist/src/hooks/useGetCurrentAccount.js +25 -3
- package/dist/src/hooks/useGetCurrentAccount.js.map +1 -1
- package/dist/src/hooks/useGetSession.d.ts +1 -1
- package/dist/src/hooks/useGetSession.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.d.ts +15 -175
- package/dist/src/hooks/useLinkAccount.d.ts.map +1 -1
- package/dist/src/hooks/useLinkAccount.js +7 -4
- package/dist/src/hooks/useLinkAccount.js.map +1 -1
- package/dist/src/hooks/usePortalApiClient.d.ts +2 -0
- package/dist/src/hooks/usePortalApiClient.d.ts.map +1 -0
- package/dist/src/hooks/usePortalApiClient.js +6 -0
- package/dist/src/hooks/usePortalApiClient.js.map +1 -0
- package/dist/src/hooks/useSignInWithDiscord.d.ts +12 -100
- package/dist/src/hooks/useSignInWithDiscord.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithDiscord.js +6 -3
- package/dist/src/hooks/useSignInWithDiscord.js.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.d.ts +12 -100
- package/dist/src/hooks/useSignInWithWallet.d.ts.map +1 -1
- package/dist/src/hooks/useSignInWithWallet.js +6 -34
- package/dist/src/hooks/useSignInWithWallet.js.map +1 -1
- package/dist/src/hooks/useSignOut.d.ts +12 -28
- package/dist/src/hooks/useSignOut.d.ts.map +1 -1
- package/dist/src/hooks/useSignOut.js +7 -4
- package/dist/src/hooks/useSignOut.js.map +1 -1
- package/dist/src/hooks/useSignUpWithWallet.d.ts +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 -136
- package/dist/src/hooks/useUpdateMezoId.d.ts.map +1 -1
- package/dist/src/hooks/useUpdateMezoId.js +7 -4
- package/dist/src/hooks/useUpdateMezoId.js.map +1 -1
- package/dist/src/hooks/useWalletAccount.d.ts +12 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +25 -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 +37 -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 +49 -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 +43 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +81 -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 +68 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +9 -3
- package/src/api/auth.ts +72 -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 +111 -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 +142 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +35 -0
- package/src/components/Dropdown/Dropdown.tsx +81 -0
- package/src/components/Dropdown/README.md +46 -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 +1 -0
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useAssetsUSDConversion.ts +31 -0
- package/src/hooks/useAuthenticateWithWallet.ts +105 -0
- package/src/hooks/useCreateAccount.ts +19 -5
- package/src/hooks/useCreateSession.ts +22 -9
- package/src/hooks/useDropdownData.ts +130 -0
- package/src/hooks/useEnsureNoSessionAndFetchNonce.ts +1 -0
- package/src/hooks/useGetAccountByAddress.ts +2 -2
- package/src/hooks/useGetAccountByMezoId.ts +2 -2
- package/src/hooks/useGetCurrentAccount.ts +46 -6
- package/src/hooks/useGetSession.ts +1 -1
- package/src/hooks/useLinkAccount.ts +18 -5
- package/src/hooks/usePortalApiClient.ts +6 -0
- package/src/hooks/useSignInWithDiscord.ts +14 -5
- package/src/hooks/useSignInWithWallet.ts +14 -42
- package/src/hooks/useSignOut.ts +18 -4
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useUpdateMezoId.ts +19 -4
- package/src/hooks/useWalletAccount.ts +41 -0
- package/src/index.ts +1 -0
- package/src/provider.ts +30 -37
- package/src/utils/address.test.ts +44 -0
- package/src/utils/address.ts +43 -0
- package/src/utils/cryptoAssets.test.ts +59 -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 +89 -0
- package/src/utils/numbers.ts +110 -0
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
useQueryClient,
|
|
3
|
+
useMutation,
|
|
4
|
+
MutationOptions,
|
|
5
|
+
DefaultError,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
2
7
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
8
|
import { QUERY_KEYS } from "./constants"
|
|
9
|
+
import type { UpdateMezoIdResponse } from "../api"
|
|
4
10
|
|
|
5
|
-
export function useUpdateMezoId(
|
|
11
|
+
export function useUpdateMezoId(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<UpdateMezoIdResponse, DefaultError, string>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
6
17
|
const queryClient = useQueryClient()
|
|
7
18
|
const authApiClient = useAuthApiClient()
|
|
8
19
|
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
9
22
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
10
23
|
mutationFn: (newMezoId: string) => authApiClient.updateMezoId(newMezoId),
|
|
11
|
-
onSuccess: () => {
|
|
24
|
+
onSuccess: (data, variables, context) => {
|
|
12
25
|
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
26
|
+
|
|
27
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
13
28
|
},
|
|
14
|
-
...
|
|
29
|
+
...restMutationOptions,
|
|
15
30
|
})
|
|
16
31
|
|
|
17
32
|
return { updateMezoId: mutate, updateMezoIdAsync: mutateAsync, ...rest }
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useBitcoinAccount } from "@mezo-org/orangekit"
|
|
2
|
+
import { useEffect, useMemo, useState } from "react"
|
|
3
|
+
import { Address } from "viem"
|
|
4
|
+
import { Connector, useAccount } from "wagmi"
|
|
5
|
+
|
|
6
|
+
type UseWalletAccountReturn = {
|
|
7
|
+
address?: string | Address
|
|
8
|
+
isConnected: boolean
|
|
9
|
+
networkFamily: "bitcoin" | "evm"
|
|
10
|
+
connector?: Connector
|
|
11
|
+
chainId?: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default function useWalletAccount(): UseWalletAccountReturn {
|
|
15
|
+
const { address: evmAddress, connector, chainId } = useAccount()
|
|
16
|
+
const { btcAddress } = useBitcoinAccount()
|
|
17
|
+
const [isConnected, setIsConnected] = useState(false)
|
|
18
|
+
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!connector) {
|
|
21
|
+
setIsConnected(false)
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const isBitcoinFlow = connector.type === "orangekit"
|
|
26
|
+
setIsConnected(isBitcoinFlow ? !!btcAddress : !!evmAddress)
|
|
27
|
+
}, [btcAddress, connector, evmAddress])
|
|
28
|
+
|
|
29
|
+
const networkFamily = useMemo(
|
|
30
|
+
() => (btcAddress ? "bitcoin" : "evm"),
|
|
31
|
+
[btcAddress],
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
address: btcAddress || evmAddress,
|
|
36
|
+
isConnected,
|
|
37
|
+
networkFamily,
|
|
38
|
+
connector,
|
|
39
|
+
chainId,
|
|
40
|
+
} as const
|
|
41
|
+
}
|
package/src/index.ts
CHANGED
package/src/provider.ts
CHANGED
|
@@ -1,56 +1,49 @@
|
|
|
1
|
-
import { createContext, createElement
|
|
2
|
-
import {
|
|
1
|
+
import { createContext, createElement } from "react"
|
|
2
|
+
import { AuthApiClient, PortalApiClient } from "./api"
|
|
3
3
|
|
|
4
4
|
export interface PassportContextValue {
|
|
5
5
|
authApiClient: AuthApiClient
|
|
6
|
+
portalApiClient: PortalApiClient
|
|
7
|
+
environment?: Environment
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export const PassportContext = createContext<PassportContextValue | undefined>(
|
|
9
11
|
undefined,
|
|
10
12
|
)
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
environment?: keyof typeof API_ENDPOINTS_BY_ENV
|
|
16
|
-
apiUrl?: string
|
|
17
|
-
} & (
|
|
18
|
-
| {
|
|
19
|
-
environment: keyof typeof API_ENDPOINTS_BY_ENV
|
|
20
|
-
}
|
|
21
|
-
| {
|
|
22
|
-
apiUrl: string
|
|
23
|
-
}
|
|
24
|
-
)
|
|
14
|
+
type Environment = "mainnet" | "testnet"
|
|
15
|
+
|
|
16
|
+
type PassportProviderProps = {
|
|
25
17
|
children: React.ReactNode
|
|
18
|
+
// eslint-disable-next-line react/require-default-props
|
|
19
|
+
environment?: Environment
|
|
20
|
+
// eslint-disable-next-line react/require-default-props
|
|
21
|
+
authApiUrl?: string
|
|
22
|
+
// eslint-disable-next-line react/require-default-props
|
|
23
|
+
portalApiUrl?: string
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
export function PassportProvider({
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (apiUrl) return apiUrl
|
|
38
|
-
|
|
39
|
-
if (!(environment! in API_ENDPOINTS_BY_ENV)) {
|
|
40
|
-
throw new Error("Wrong environment passed to PassportProvider.")
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return API_ENDPOINTS_BY_ENV[environment!]
|
|
44
|
-
}, [environment, apiUrl])
|
|
26
|
+
export function PassportProvider({
|
|
27
|
+
environment,
|
|
28
|
+
authApiUrl,
|
|
29
|
+
portalApiUrl,
|
|
30
|
+
children,
|
|
31
|
+
}: PassportProviderProps) {
|
|
32
|
+
if (environment !== "mainnet" && environment !== "testnet") {
|
|
33
|
+
throw new Error("Wrong environment passed to PassportProvider.")
|
|
34
|
+
}
|
|
45
35
|
|
|
46
36
|
return createElement(
|
|
47
37
|
PassportContext.Provider,
|
|
48
38
|
{
|
|
49
|
-
value:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
value: {
|
|
40
|
+
authApiClient: new AuthApiClient(environment ?? "mainnet", authApiUrl),
|
|
41
|
+
portalApiClient: new PortalApiClient(
|
|
42
|
+
environment ?? "mainnet",
|
|
43
|
+
portalApiUrl,
|
|
44
|
+
),
|
|
45
|
+
environment,
|
|
46
|
+
},
|
|
54
47
|
},
|
|
55
48
|
children,
|
|
56
49
|
)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { getAddressExplorerUrl, trimAddress } from "./address"
|
|
2
|
+
|
|
3
|
+
describe("trimAddress", () => {
|
|
4
|
+
it("returns full address if length is less than 11", () => {
|
|
5
|
+
expect(trimAddress("0x12345")).toBe("0x12345")
|
|
6
|
+
})
|
|
7
|
+
|
|
8
|
+
it("returns trimmed address if length is 11 or more", () => {
|
|
9
|
+
const input = "0x123456789abcdef"
|
|
10
|
+
const expected = "0x123...bcdef"
|
|
11
|
+
expect(trimAddress(input)).toBe(expected)
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it("returns lowercase result", () => {
|
|
15
|
+
const input = "0xABCDEF1234567890"
|
|
16
|
+
const expected = "0xabc...67890"
|
|
17
|
+
expect(trimAddress(input)).toBe(expected)
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe("getAddressExplorerUrl", () => {
|
|
22
|
+
const btcAddress = "bc1qxyz123"
|
|
23
|
+
const evmAddress = "0x1234567890abcdef"
|
|
24
|
+
|
|
25
|
+
it("returns correct mainnet Bitcoin explorer URL", () => {
|
|
26
|
+
const url = getAddressExplorerUrl(btcAddress, "bitcoin", false)
|
|
27
|
+
expect(url).toBe(`https://mempool.space/address/${btcAddress}`)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it("returns correct testnet Bitcoin explorer URL", () => {
|
|
31
|
+
const url = getAddressExplorerUrl(btcAddress, "bitcoin", true)
|
|
32
|
+
expect(url).toBe(`https://mempool.space/testnet/address/${btcAddress}`)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
it("returns correct mainnet EVM explorer URL", () => {
|
|
36
|
+
const url = getAddressExplorerUrl(evmAddress, "evm", false)
|
|
37
|
+
expect(url).toBe(`https://etherscan.io/address/${evmAddress}`)
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it("returns correct testnet EVM explorer URL", () => {
|
|
41
|
+
const url = getAddressExplorerUrl(evmAddress, "evm", true)
|
|
42
|
+
expect(url).toBe(`https://sepolia.etherscan.io/address/${evmAddress}`)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trims the address to the first 5 and last 5 characters.
|
|
3
|
+
* @param address The address to trim.
|
|
4
|
+
* @returns The trimmed address.
|
|
5
|
+
*/
|
|
6
|
+
export function trimAddress(address: string): string {
|
|
7
|
+
if (address.length < 11) return address
|
|
8
|
+
|
|
9
|
+
const trimmedAddress = `${address.slice(0, 5)}...${address.slice(-5)}`
|
|
10
|
+
|
|
11
|
+
return trimmedAddress
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const MAINNET_EXPLORER_URLS: Record<"bitcoin" | "evm", string> = {
|
|
15
|
+
bitcoin: "https://mempool.space",
|
|
16
|
+
evm: "https://etherscan.io",
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const TESTNET_EXPLORER_URLS: Record<"bitcoin" | "evm", string> = {
|
|
20
|
+
bitcoin: "https://mempool.space/testnet",
|
|
21
|
+
evm: "https://sepolia.etherscan.io",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Get the URL of the block explorer page for an address.
|
|
26
|
+
* @param address - The address to link to.
|
|
27
|
+
* @param chainType - The chain type of the address.
|
|
28
|
+
* @param isTestnet - Whether the address is on testnet.
|
|
29
|
+
* @returns The URL of the block explorer page for the address.
|
|
30
|
+
*/
|
|
31
|
+
export function getAddressExplorerUrl(
|
|
32
|
+
address: string,
|
|
33
|
+
chainType: "bitcoin" | "evm",
|
|
34
|
+
isTestnet: boolean,
|
|
35
|
+
) {
|
|
36
|
+
const EXPLORER_URLS = isTestnet
|
|
37
|
+
? TESTNET_EXPLORER_URLS
|
|
38
|
+
: MAINNET_EXPLORER_URLS
|
|
39
|
+
const baseUrl = EXPLORER_URLS[chainType]
|
|
40
|
+
const endpoint = `address/${address}`
|
|
41
|
+
|
|
42
|
+
return `${baseUrl}/${endpoint}`
|
|
43
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BitcoinCircle,
|
|
3
|
+
EthCircle,
|
|
4
|
+
MUsdCircle,
|
|
5
|
+
TBtcCircle,
|
|
6
|
+
} from "@mezo-org/mezo-clay"
|
|
7
|
+
import { getCryptoAsset } from "./cryptoAssets"
|
|
8
|
+
|
|
9
|
+
describe("getCryptoAsset", () => {
|
|
10
|
+
it("returns the correct asset for 'btc'", () => {
|
|
11
|
+
const asset = getCryptoAsset("btc")
|
|
12
|
+
expect(asset.name).toBe("Bitcoin")
|
|
13
|
+
expect(asset.symbol).toBe("BTC")
|
|
14
|
+
expect(asset.decimals).toBe(8)
|
|
15
|
+
expect(asset.icon).toBe(BitcoinCircle)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it("returns the correct asset for 'eth'", () => {
|
|
19
|
+
const asset = getCryptoAsset("eth")
|
|
20
|
+
expect(asset.name).toBe("Ethereum")
|
|
21
|
+
expect(asset.symbol).toBe("ETH")
|
|
22
|
+
expect(asset.decimals).toBe(18)
|
|
23
|
+
expect(asset.icon).toBe(EthCircle)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it("returns the correct asset for 'tbtc'", () => {
|
|
27
|
+
const asset = getCryptoAsset("tbtc")
|
|
28
|
+
expect(asset.name).toBe("Threshold Bitcoin")
|
|
29
|
+
expect(asset.symbol).toBe("tBTC")
|
|
30
|
+
expect(asset.decimals).toBe(18)
|
|
31
|
+
expect(asset.icon).toBe(TBtcCircle)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it("returns the correct asset for 'wbtc'", () => {
|
|
35
|
+
const asset = getCryptoAsset("wbtc")
|
|
36
|
+
expect(asset.name).toBe("Wrapped Bitcoin")
|
|
37
|
+
expect(asset.symbol).toBe("wBTC")
|
|
38
|
+
expect(asset.decimals).toBe(8)
|
|
39
|
+
// FIXME: Test should expect the exact icon component
|
|
40
|
+
expect(asset.icon).toEqual(expect.any(Function))
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it("returns the correct asset for 'mbtc'", () => {
|
|
44
|
+
const asset = getCryptoAsset("mbtc")
|
|
45
|
+
expect(asset.name).toBe("Matsnet Bitcoin")
|
|
46
|
+
expect(asset.symbol).toBe("BTC")
|
|
47
|
+
expect(asset.decimals).toBe(18)
|
|
48
|
+
// FIXME: Test should expect the exact icon component
|
|
49
|
+
expect(asset.icon).toEqual(expect.any(Function))
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it("returns the correct asset for 'musd'", () => {
|
|
53
|
+
const asset = getCryptoAsset("musd")
|
|
54
|
+
expect(asset.name).toBe("Mezo USD")
|
|
55
|
+
expect(asset.symbol).toBe("mUSD")
|
|
56
|
+
expect(asset.decimals).toBe(18)
|
|
57
|
+
expect(asset.icon).toBe(MUsdCircle)
|
|
58
|
+
})
|
|
59
|
+
})
|
|
@@ -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,89 @@
|
|
|
1
|
+
import {
|
|
2
|
+
formatHumanReadableNumber,
|
|
3
|
+
formatTokenAmount,
|
|
4
|
+
fromFixedPoint,
|
|
5
|
+
roundUpNumber,
|
|
6
|
+
} from "./numbers"
|
|
7
|
+
|
|
8
|
+
describe("formatHumanReadableNumber", () => {
|
|
9
|
+
it("formats number less than 1000 with decimals", () => {
|
|
10
|
+
expect(formatHumanReadableNumber(123.456789, 2)).toBe("123.46")
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
it("formats thousands with K", () => {
|
|
14
|
+
expect(formatHumanReadableNumber(12_345, 2)).toBe("12.35K")
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it("formats millions with M", () => {
|
|
18
|
+
expect(formatHumanReadableNumber(12_345_678, 2)).toBe("12.35M")
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it("formats billions with B", () => {
|
|
22
|
+
expect(formatHumanReadableNumber(12_345_678_901, 2)).toBe("12.35B")
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it("formats trillions with T", () => {
|
|
26
|
+
expect(formatHumanReadableNumber(12_345_678_901_234, 2)).toBe("12.35T")
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
it("formats bigint as readable number", () => {
|
|
30
|
+
expect(formatHumanReadableNumber(1234567890123456789n, 2)).toBe("> 1000T")
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it("caps at > 1000T for huge numbers", () => {
|
|
34
|
+
const hugeNumber = 10 ** 18 // 1e18
|
|
35
|
+
expect(formatHumanReadableNumber(hugeNumber, 2)).toBe("> 1000T")
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe("roundUpNumber", () => {
|
|
40
|
+
it("rounds up to 2 decimals", () => {
|
|
41
|
+
expect(roundUpNumber(1.234)).toBe(1.24)
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it("rounds up to 3 decimals", () => {
|
|
45
|
+
expect(roundUpNumber(9.87654, 3)).toBe(9.877)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it("rounds up exact decimal", () => {
|
|
49
|
+
expect(roundUpNumber(2.5, 1)).toBe(2.5)
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe("fromFixedPoint", () => {
|
|
54
|
+
it("converts fixed point with 18 decimals to float", () => {
|
|
55
|
+
const raw = 1234567890000000000n // 1.23456789 ETH in 18 decimals
|
|
56
|
+
expect(fromFixedPoint(raw, 18, 4)).toBe(1.2345)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it("converts with truncation to 2 decimals", () => {
|
|
60
|
+
const raw = 987654321000000000n // ~0.987654321 ETH
|
|
61
|
+
expect(fromFixedPoint(raw, 18, 2)).toBe(0.98)
|
|
62
|
+
})
|
|
63
|
+
|
|
64
|
+
it("handles large fixedPoint decimals", () => {
|
|
65
|
+
const raw = 1_000_000_000_000_000_000_000_000n // 1M with 6 decimals
|
|
66
|
+
expect(fromFixedPoint(raw, 6, 2)).toBe(1000000000000000000)
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
describe("formatTokenAmount", () => {
|
|
71
|
+
it("formats zero value with desired decimals", () => {
|
|
72
|
+
expect(formatTokenAmount(0, 18, 3)).toBe("0.000")
|
|
73
|
+
expect(formatTokenAmount("0", 18, 2)).toBe("0.00")
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it("returns <0.01 if amount is less than minimum display value", () => {
|
|
77
|
+
expect(formatTokenAmount("1", 18, 2)).toBe("<0.01") // 1 wei = 1e-18
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it("formats a normal value without rounding", () => {
|
|
81
|
+
const result = formatTokenAmount("1230000000000000000", 18, 2)
|
|
82
|
+
expect(result).toBe("1.23")
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
it("formats with rounding if withRoundUp = true", () => {
|
|
86
|
+
const result = formatTokenAmount("1234560000000000000", 18, 2, true)
|
|
87
|
+
expect(result).toBe("1.24")
|
|
88
|
+
})
|
|
89
|
+
})
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a number or bigint into a human-readable string with K, M, B, or T suffix.
|
|
3
|
+
* @param value The number or bigint to format.
|
|
4
|
+
* @param decimals Number of decimal places to include (ignored for bigint to avoid floating point issues).
|
|
5
|
+
* @returns The formatted number as a string.
|
|
6
|
+
*/
|
|
7
|
+
export function formatHumanReadableNumber(
|
|
8
|
+
value: number | bigint,
|
|
9
|
+
decimals: number = 4,
|
|
10
|
+
): string {
|
|
11
|
+
const suffixes = ["", "K", "M", "B", "T"]
|
|
12
|
+
const numericValue = typeof value === "bigint" ? Number(value) : value
|
|
13
|
+
|
|
14
|
+
if (numericValue < 1000) return numericValue.toFixed(decimals)
|
|
15
|
+
|
|
16
|
+
const magnitude = Math.floor(Math.log10(numericValue) / 3)
|
|
17
|
+
|
|
18
|
+
if (magnitude >= suffixes.length) {
|
|
19
|
+
const max = 10 ** ((suffixes.length - 1) * 3) * 1000 - 1
|
|
20
|
+
const capped = max / 10 ** ((suffixes.length - 1) * 3)
|
|
21
|
+
return `> ${capped.toFixed(0)}${suffixes[suffixes.length - 1]}`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const scaledValue = numericValue / 10 ** (magnitude * 3)
|
|
25
|
+
return `${scaledValue.toFixed(decimals)}${suffixes[magnitude]}`
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Returns a number rounded up to the desired number of decimals.
|
|
30
|
+
* @param amount The number to round up.
|
|
31
|
+
* @param desiredDecimals The number of decimals to round up to.
|
|
32
|
+
* @returns The rounded up number.
|
|
33
|
+
* @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L1-L3
|
|
34
|
+
*/
|
|
35
|
+
export function roundUpNumber(amount: number, desiredDecimals = 2): number {
|
|
36
|
+
return Math.ceil(amount * 10 ** desiredDecimals) / 10 ** desiredDecimals
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Convert a fixed point bigint with precision `fixedPointDecimals` to a
|
|
41
|
+
* floating point number truncated to `desiredDecimals`.
|
|
42
|
+
* @param fixedPoint The fixed point bigint to convert.
|
|
43
|
+
* @param fixedPointDecimals The number of decimals in the fixed point bigint.
|
|
44
|
+
* @param desiredDecimals The number of decimals in the output number.
|
|
45
|
+
* @returns The floating point number truncated to `desiredDecimals`.
|
|
46
|
+
* @see https://github.com/tahowallet/extension/blob/main/background/lib/fixed-point.ts#L216-L239
|
|
47
|
+
*/
|
|
48
|
+
export function fromFixedPoint(
|
|
49
|
+
fixedPoint: bigint,
|
|
50
|
+
fixedPointDecimals: number,
|
|
51
|
+
desiredDecimals = 2,
|
|
52
|
+
): number {
|
|
53
|
+
const fixedPointDesiredDecimalsAmount =
|
|
54
|
+
fixedPoint /
|
|
55
|
+
10n ** BigInt(Math.max(1, fixedPointDecimals - desiredDecimals))
|
|
56
|
+
|
|
57
|
+
const formattedAmount =
|
|
58
|
+
Number(fixedPointDesiredDecimalsAmount) /
|
|
59
|
+
10 ** Math.min(desiredDecimals, fixedPointDecimals)
|
|
60
|
+
|
|
61
|
+
return formattedAmount
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Display a cryptoAsset amount correctly with desired decimals.
|
|
66
|
+
* The function returns a string with a language-sensitive representation of this number.
|
|
67
|
+
* - If the amount entered is zero, return the result with the desired decimals.
|
|
68
|
+
* For example, 0.00 for a precision of 2.
|
|
69
|
+
* - If `formattedAmount` is less than the minimum amount to display
|
|
70
|
+
* for the specified precision return information about this.
|
|
71
|
+
* For example, <0.01 for a precision of 2.
|
|
72
|
+
* - Other amounts are formatted according to the use of the `bigIntToUserAmount` function.
|
|
73
|
+
* @param amount The amount of tokens to format.
|
|
74
|
+
* @param decimals The number of decimals the cryptoAsset has.
|
|
75
|
+
* @param desiredDecimals The number of decimals to display.
|
|
76
|
+
* @param withRoundUp If true, the amount will be rounded up.
|
|
77
|
+
* @returns The formatted cryptoAsset amount.
|
|
78
|
+
* @see https://github.com/thesis/acre/blob/main/dapp/src/utils/numbersUtils.ts#L57-L87
|
|
79
|
+
*/
|
|
80
|
+
export function formatTokenAmount(
|
|
81
|
+
amount: number | string | bigint,
|
|
82
|
+
decimals = 18,
|
|
83
|
+
desiredDecimals = 2,
|
|
84
|
+
withRoundUp = false,
|
|
85
|
+
) {
|
|
86
|
+
const fixedPoint = BigInt(amount)
|
|
87
|
+
|
|
88
|
+
if (fixedPoint === 0n) {
|
|
89
|
+
return `0.${"0".repeat(desiredDecimals)}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const formattedAmount = fromFixedPoint(
|
|
93
|
+
fixedPoint,
|
|
94
|
+
decimals,
|
|
95
|
+
// To round the amount up, let's increase the precision by one.
|
|
96
|
+
// The desired decimal numbers will be set later anyway.
|
|
97
|
+
withRoundUp ? desiredDecimals + 1 : desiredDecimals,
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
const minAmountToDisplay = 1 / 10 ** Math.min(desiredDecimals, decimals)
|
|
101
|
+
|
|
102
|
+
if (minAmountToDisplay > formattedAmount) {
|
|
103
|
+
return `<0.${"0".repeat(desiredDecimals - 1)}1`
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const finalFormattedAmount = withRoundUp
|
|
107
|
+
? roundUpNumber(formattedAmount, desiredDecimals)
|
|
108
|
+
: formattedAmount
|
|
109
|
+
return formatHumanReadableNumber(finalFormattedAmount, desiredDecimals)
|
|
110
|
+
}
|