@mezo-org/passport 0.4.0-dev.2 → 0.4.0-dev.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -22
- package/dist/src/api/auth.d.ts +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 +33 -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/ConnectedTrigger.d.ts +8 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js +39 -0
- package/dist/src/components/Dropdown/ConnectedTrigger.js.map +1 -0
- package/dist/src/components/Dropdown/Content.d.ts +23 -0
- package/dist/src/components/Dropdown/Content.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Content.js +27 -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 +23 -0
- package/dist/src/components/Dropdown/Dropdown.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Dropdown.js +65 -0
- package/dist/src/components/Dropdown/Dropdown.js.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts +13 -0
- package/dist/src/components/Dropdown/ListingItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/ListingItem.js +34 -0
- package/dist/src/components/Dropdown/ListingItem.js.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts +7 -0
- package/dist/src/components/Dropdown/NestedViewLayout.d.ts.map +1 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js +34 -0
- package/dist/src/components/Dropdown/NestedViewLayout.js.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts +7 -0
- package/dist/src/components/Dropdown/Receive/Receive.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Receive/Receive.js +53 -0
- package/dist/src/components/Dropdown/Receive/Receive.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts +7 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js +48 -0
- package/dist/src/components/Dropdown/Root/AccountAddressActions.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts +11 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js +9 -0
- package/dist/src/components/Dropdown/Root/AccountAssetItem.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js +15 -0
- package/dist/src/components/Dropdown/Root/AccountBalance.js.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts +9 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js +47 -0
- package/dist/src/components/Dropdown/Root/AccountOtherAssets.js.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts +23 -0
- package/dist/src/components/Dropdown/Root/Root.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/Root.js +57 -0
- package/dist/src/components/Dropdown/Root/Root.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js +64 -0
- package/dist/src/components/Dropdown/Root/WalletAddress.js.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts +8 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.d.ts.map +1 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js +44 -0
- package/dist/src/components/Dropdown/Root/WelcomeBlock.js.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts +3 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.d.ts.map +1 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js +14 -0
- package/dist/src/components/Dropdown/TestnetTopBanner.js.map +1 -0
- package/dist/src/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/config.js +2 -2
- package/dist/src/config.js.map +1 -1
- package/dist/src/constants.d.ts +2 -1
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +5 -4
- package/dist/src/constants.js.map +1 -1
- 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 +4 -4
- package/dist/src/hooks/index.d.ts.map +1 -1
- package/dist/src/hooks/index.js +4 -4
- package/dist/src/hooks/index.js.map +1 -1
- package/dist/src/hooks/useAssetsConversionRates.d.ts +7 -0
- package/dist/src/hooks/useAssetsConversionRates.d.ts.map +1 -0
- package/dist/src/hooks/useAssetsConversionRates.js +24 -0
- package/dist/src/hooks/useAssetsConversionRates.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/useBorrowData.d.ts +1418 -0
- package/dist/src/hooks/useBorrowData.d.ts.map +1 -0
- package/dist/src/hooks/useBorrowData.js +65 -0
- package/dist/src/hooks/useBorrowData.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 +39 -0
- package/dist/src/hooks/useDropdownData.d.ts.map +1 -0
- package/dist/src/hooks/useDropdownData.js +63 -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/useRefreshPassport.d.ts +17 -0
- package/dist/src/hooks/useRefreshPassport.d.ts.map +1 -0
- package/dist/src/hooks/useRefreshPassport.js +29 -0
- package/dist/src/hooks/useRefreshPassport.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 -44
- 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/useTokensBalances.d.ts +74 -0
- package/dist/src/hooks/useTokensBalances.d.ts.map +1 -0
- package/dist/src/hooks/useTokensBalances.js +140 -0
- package/dist/src/hooks/useTokensBalances.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 +13 -0
- package/dist/src/hooks/useWalletAccount.d.ts.map +1 -0
- package/dist/src/hooks/useWalletAccount.js +29 -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/lib/contracts/index.d.ts +10 -0
- package/dist/src/lib/contracts/index.d.ts.map +1 -0
- package/dist/src/lib/contracts/index.js +49 -0
- package/dist/src/lib/contracts/index.js.map +1 -0
- package/dist/src/lib/contracts/musd.d.ts +107 -0
- package/dist/src/lib/contracts/musd.d.ts.map +1 -0
- package/dist/src/lib/contracts/musd.js +1070 -0
- package/dist/src/lib/contracts/musd.js.map +1 -0
- package/dist/src/lib/contracts/troveManager.d.ts +1401 -0
- package/dist/src/lib/contracts/troveManager.d.ts.map +1 -0
- package/dist/src/lib/contracts/troveManager.js +1820 -0
- package/dist/src/lib/contracts/troveManager.js.map +1 -0
- 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/stores/dropdownStore.d.ts +12 -0
- package/dist/src/stores/dropdownStore.d.ts.map +1 -0
- package/dist/src/stores/dropdownStore.js +13 -0
- package/dist/src/stores/dropdownStore.js.map +1 -0
- package/dist/src/utils/address.d.ts +15 -0
- package/dist/src/utils/address.d.ts.map +1 -0
- package/dist/src/utils/address.js +37 -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 +40 -0
- package/dist/src/utils/address.test.js.map +1 -0
- package/dist/src/utils/cryptoAssets.d.ts +44 -0
- package/dist/src/utils/cryptoAssets.d.ts.map +1 -0
- package/dist/src/utils/cryptoAssets.js +131 -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 +67 -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 +58 -0
- package/dist/src/utils/numbers.d.ts.map +1 -0
- package/dist/src/utils/numbers.js +132 -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 +170 -0
- package/dist/src/utils/numbers.test.js.map +1 -0
- package/package.json +12 -5
- 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 +57 -0
- package/src/assets/DefaultAvatar.tsx +74 -0
- package/src/components/Dropdown/ConnectedTrigger.tsx +76 -0
- package/src/components/Dropdown/Content.tsx +98 -0
- package/src/components/Dropdown/DisconnectedTrigger.tsx +36 -0
- package/src/components/Dropdown/Dropdown.tsx +139 -0
- package/src/components/Dropdown/ListingItem.tsx +80 -0
- package/src/components/Dropdown/NestedViewLayout.tsx +75 -0
- package/src/components/Dropdown/README.md +41 -0
- package/src/components/Dropdown/Receive/Receive.tsx +119 -0
- package/src/components/Dropdown/Root/AccountAddressActions.tsx +101 -0
- package/src/components/Dropdown/Root/AccountAssetItem.tsx +26 -0
- package/src/components/Dropdown/Root/AccountBalance.tsx +30 -0
- package/src/components/Dropdown/Root/AccountOtherAssets.tsx +63 -0
- package/src/components/Dropdown/Root/Root.tsx +147 -0
- package/src/components/Dropdown/Root/WalletAddress.tsx +128 -0
- package/src/components/Dropdown/Root/WelcomeBlock.tsx +91 -0
- package/src/components/Dropdown/TestnetTopBanner.tsx +32 -0
- package/src/components/Dropdown/index.ts +2 -0
- package/src/components/index.ts +1 -0
- package/src/config.ts +2 -2
- package/src/constants.ts +5 -4
- package/src/hooks/constants.ts +4 -4
- package/src/hooks/index.ts +7 -4
- package/src/hooks/useAssetsConversionRates.ts +32 -0
- package/src/hooks/useAuthenticateWithWallet.ts +102 -0
- package/src/hooks/useBorrowData.ts +79 -0
- package/src/hooks/useCreateAccount.ts +20 -11
- package/src/hooks/useCreateSession.ts +24 -18
- package/src/hooks/useDropdownData.ts +101 -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/useRefreshPassport.ts +40 -0
- package/src/hooks/useSignInWithDiscord.ts +19 -28
- package/src/hooks/useSignInWithWallet.ts +14 -55
- package/src/hooks/useSignOut.ts +22 -6
- package/src/hooks/useSignUpWithWallet.ts +21 -0
- package/src/hooks/useTokensBalances.ts +187 -0
- package/src/hooks/useUpdateMezoId.ts +20 -12
- package/src/hooks/useWalletAccount.ts +55 -0
- package/src/index.ts +1 -0
- package/src/lib/contracts/index.ts +75 -0
- package/src/lib/contracts/musd.ts +1071 -0
- package/src/lib/contracts/troveManager.ts +1819 -0
- package/src/provider.ts +30 -37
- package/src/stores/dropdownStore.ts +20 -0
- package/src/utils/address.test.ts +48 -0
- package/src/utils/address.ts +45 -0
- package/src/utils/cryptoAssets.test.ts +79 -0
- package/src/utils/cryptoAssets.ts +173 -0
- package/src/utils/currency.test.ts +38 -0
- package/src/utils/currency.ts +32 -0
- package/src/utils/numbers.test.ts +220 -0
- package/src/utils/numbers.ts +188 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { useSignMessage } from "wagmi"
|
|
2
|
+
import { MutationOptions, useMutation } from "@tanstack/react-query"
|
|
3
|
+
import { useCreateAccount } from "./useCreateAccount"
|
|
4
|
+
import { useCreateSession } from "./useCreateSession"
|
|
5
|
+
import { useEnsureNoSessionAndFetchNonce } from "./useEnsureNoSessionAndFetchNonce"
|
|
6
|
+
import { createSignInWithWalletMessage } from "../utils/siww"
|
|
7
|
+
import { useGetAccountByAddress } from "./useGetAccountByAddress"
|
|
8
|
+
import type { Session } from "../api"
|
|
9
|
+
import useWalletAccount from "./useWalletAccount"
|
|
10
|
+
|
|
11
|
+
function useAuthenticateWithWallet(
|
|
12
|
+
shouldCreateAccount = false,
|
|
13
|
+
mutationOptions: Omit<
|
|
14
|
+
MutationOptions<Session>,
|
|
15
|
+
"mutationFn" | "mutationKey"
|
|
16
|
+
> = {},
|
|
17
|
+
) {
|
|
18
|
+
const { walletAddress, chainId, connector, networkFamily } =
|
|
19
|
+
useWalletAccount()
|
|
20
|
+
const { ensureNoSessionAndFetchNonce } = useEnsureNoSessionAndFetchNonce()
|
|
21
|
+
const { createSessionAsync } = useCreateSession()
|
|
22
|
+
const { createAccountAsync } = useCreateAccount()
|
|
23
|
+
const { signMessageAsync } = useSignMessage()
|
|
24
|
+
|
|
25
|
+
const { refetch: getAccountByAddress } = useGetAccountByAddress(
|
|
26
|
+
walletAddress,
|
|
27
|
+
{
|
|
28
|
+
enabled: false,
|
|
29
|
+
retry: false,
|
|
30
|
+
},
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
const { mutate, mutateAsync, ...signInMutationRestParameters } = useMutation({
|
|
34
|
+
mutationFn: async () => {
|
|
35
|
+
if (!walletAddress) {
|
|
36
|
+
throw new Error("Sign in error: Wallet not connected!")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const nonce = await ensureNoSessionAndFetchNonce()
|
|
40
|
+
|
|
41
|
+
const messageResult = createSignInWithWalletMessage(
|
|
42
|
+
walletAddress,
|
|
43
|
+
nonce,
|
|
44
|
+
networkFamily,
|
|
45
|
+
chainId,
|
|
46
|
+
)
|
|
47
|
+
const signatureResult = await signMessageAsync({
|
|
48
|
+
message: messageResult,
|
|
49
|
+
connector,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
if (shouldCreateAccount) {
|
|
53
|
+
const getAccountByAddressResult = await getAccountByAddress()
|
|
54
|
+
|
|
55
|
+
if (getAccountByAddressResult.data) {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"Wallet authentication failed: Account creation failed. Account already exists.",
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (getAccountByAddressResult.error) {
|
|
62
|
+
throw new Error(
|
|
63
|
+
`Wallet authentication failed: Account creation failed. Could not verify if account already exists: ${getAccountByAddressResult.error}`,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Fetch was successful, but no account was found
|
|
68
|
+
try {
|
|
69
|
+
const createAccountResult = await createAccountAsync({
|
|
70
|
+
type: "wallet",
|
|
71
|
+
message: messageResult,
|
|
72
|
+
signature: signatureResult,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
if (!createAccountResult) {
|
|
76
|
+
throw new Error("Failed to create new account")
|
|
77
|
+
}
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`Wallet authentication failed: Account creation failed: ${error}`,
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return (await createSessionAsync({
|
|
86
|
+
type: "wallet",
|
|
87
|
+
message: messageResult,
|
|
88
|
+
signature: signatureResult,
|
|
89
|
+
})) as Session
|
|
90
|
+
// ^ By passing type: "wallet" we know for sure the return type is Session
|
|
91
|
+
},
|
|
92
|
+
...mutationOptions,
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
return {
|
|
96
|
+
authenticateWithWallet: mutate,
|
|
97
|
+
authenticateWithWalletAsync: mutateAsync,
|
|
98
|
+
...signInMutationRestParameters,
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export { useAuthenticateWithWallet }
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useAccount, useReadContract } from "wagmi"
|
|
2
|
+
import { useCallback } from "react"
|
|
3
|
+
import { useQueryClient } from "@tanstack/react-query"
|
|
4
|
+
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import { troveManagerContract } from "../lib/contracts"
|
|
6
|
+
import { usePassportContext } from "./usePassportContext"
|
|
7
|
+
import { CHAIN_ID } from "../constants"
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Query hook for getting borrow data. Returns collateral and trove debt for the
|
|
11
|
+
* connected account, based on it's evm address.
|
|
12
|
+
* @param queryOptions Query options passed to the underlying `useQuery` hook.
|
|
13
|
+
*/
|
|
14
|
+
export function useBorrowData(queryOptions = {}) {
|
|
15
|
+
const { environment = "mainnet" } = usePassportContext()
|
|
16
|
+
const { address } = useAccount()
|
|
17
|
+
return useReadContract({
|
|
18
|
+
abi: troveManagerContract.abi,
|
|
19
|
+
address: troveManagerContract.address[environment],
|
|
20
|
+
functionName: "getEntireDebtAndColl",
|
|
21
|
+
args: [address || "0x"],
|
|
22
|
+
query: {
|
|
23
|
+
enabled: !!address,
|
|
24
|
+
staleTime: 5 * ONE_MINUTE_MS,
|
|
25
|
+
retry: 1,
|
|
26
|
+
select: (data) => {
|
|
27
|
+
if (!data) return undefined
|
|
28
|
+
|
|
29
|
+
const [collateral, principal, interest] = data
|
|
30
|
+
return {
|
|
31
|
+
collateral,
|
|
32
|
+
troveDebt: principal + interest,
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
...queryOptions,
|
|
36
|
+
},
|
|
37
|
+
chainId: CHAIN_ID[environment],
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Hook for for invalidating current user's borrow data. Can be used to
|
|
43
|
+
* invalidate borrow data manually, which forces the data to be re-fetched.
|
|
44
|
+
* @returns Function `invalidateBorrowData` for invalidating the borrow data
|
|
45
|
+
*/
|
|
46
|
+
export function useInvalidateBorrowData() {
|
|
47
|
+
const queryClient = useQueryClient()
|
|
48
|
+
|
|
49
|
+
const { queryKey } = useBorrowData()
|
|
50
|
+
|
|
51
|
+
const invalidateBorrowDataHandler = useCallback(
|
|
52
|
+
() => queryClient.invalidateQueries({ queryKey }),
|
|
53
|
+
[queryClient, queryKey],
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
invalidateBorrowData: invalidateBorrowDataHandler,
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Hook for for resetting current user's borrow data. Can be used to reset
|
|
63
|
+
* borrow data manually, which forces the data to be re-fetched.
|
|
64
|
+
* @returns Function `resetBorrowData` for resetting the borrow data
|
|
65
|
+
*/
|
|
66
|
+
export function useResetBorrowData() {
|
|
67
|
+
const queryClient = useQueryClient()
|
|
68
|
+
|
|
69
|
+
const { queryKey } = useBorrowData()
|
|
70
|
+
|
|
71
|
+
const resetBorrowDataHandler = useCallback(
|
|
72
|
+
() => queryClient.resetQueries({ queryKey }),
|
|
73
|
+
[queryClient, queryKey],
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
resetBorrowData: resetBorrowDataHandler,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,26 +1,35 @@
|
|
|
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"
|
|
4
|
-
import { CreateAccountRequest } from "../api
|
|
9
|
+
import type { CreateAccountRequest, CreateAccountResponse } from "../api"
|
|
5
10
|
|
|
6
|
-
export function useCreateAccount(
|
|
11
|
+
export function useCreateAccount(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<CreateAccountResponse, DefaultError, CreateAccountRequest>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
7
17
|
const queryClient = useQueryClient()
|
|
8
18
|
const authApiClient = useAuthApiClient()
|
|
9
19
|
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
10
22
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
11
23
|
mutationFn: (createAccountRequest: CreateAccountRequest) =>
|
|
12
24
|
authApiClient.createAccount(createAccountRequest),
|
|
13
|
-
onSuccess: () => {
|
|
14
|
-
queryClient.resetQueries({
|
|
15
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
|
|
16
|
-
exact: false,
|
|
17
|
-
})
|
|
25
|
+
onSuccess: (data, variables, context) => {
|
|
18
26
|
queryClient.resetQueries({
|
|
19
|
-
queryKey: [QUERY_KEYS.
|
|
20
|
-
exact: false,
|
|
27
|
+
queryKey: [QUERY_KEYS.ACCOUNT],
|
|
21
28
|
})
|
|
29
|
+
|
|
30
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
22
31
|
},
|
|
23
|
-
...
|
|
32
|
+
...restMutationOptions,
|
|
24
33
|
})
|
|
25
34
|
|
|
26
35
|
return { createAccount: mutate, createAccountAsync: mutateAsync, ...rest }
|
|
@@ -1,37 +1,43 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
useQueryClient,
|
|
3
|
+
useMutation,
|
|
4
|
+
MutationOptions,
|
|
5
|
+
DefaultError,
|
|
6
|
+
} from "@tanstack/react-query"
|
|
2
7
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
-
import { CreateSessionRequest } from "../api
|
|
8
|
+
import type { CreateSessionRequest, CreateSessionResponse } from "../api"
|
|
4
9
|
import { QUERY_KEYS } from "./constants"
|
|
5
10
|
|
|
6
11
|
/**
|
|
7
12
|
* This hook is not exposed for external consumers. For creating session they
|
|
8
13
|
* should use `useSignIn` hook related to the authentication method.
|
|
9
14
|
*/
|
|
10
|
-
export function useCreateSession(
|
|
15
|
+
export function useCreateSession(
|
|
16
|
+
mutationOptions: Omit<
|
|
17
|
+
MutationOptions<CreateSessionResponse, DefaultError, CreateSessionRequest>,
|
|
18
|
+
"mutationFn" | "mutationKey"
|
|
19
|
+
> = {},
|
|
20
|
+
) {
|
|
11
21
|
const queryClient = useQueryClient()
|
|
12
22
|
const authApiClient = useAuthApiClient()
|
|
13
23
|
|
|
24
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
25
|
+
|
|
14
26
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
15
27
|
mutationFn: (createSessionRequest: CreateSessionRequest) =>
|
|
16
28
|
authApiClient.createSession(createSessionRequest),
|
|
17
|
-
onSuccess: (data) => {
|
|
18
|
-
// Since `getSession` query also has code in
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
|
|
29
|
+
onSuccess: (data, variables, context) => {
|
|
30
|
+
// Since `getSession` query also has code in its query keys we can't set
|
|
31
|
+
// query data for a specific query, because we don't know if the code was
|
|
32
|
+
// used or not. Because of that we just reset all getSession queries
|
|
33
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.SESSION] })
|
|
22
34
|
queryClient.resetQueries({
|
|
23
|
-
queryKey: [QUERY_KEYS.
|
|
24
|
-
exact: false,
|
|
35
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
25
36
|
})
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
queryClient.resetQueries({
|
|
30
|
-
queryKey: [QUERY_KEYS.CURRENT_ACCOUNT],
|
|
31
|
-
})
|
|
32
|
-
}
|
|
37
|
+
|
|
38
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
33
39
|
},
|
|
34
|
-
...
|
|
40
|
+
...restMutationOptions,
|
|
35
41
|
})
|
|
36
42
|
|
|
37
43
|
return { createSession: mutate, createSessionAsync: mutateAsync, ...rest }
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { useBalance } from "wagmi"
|
|
2
|
+
import { useGetCurrentAccount } from "."
|
|
3
|
+
import {
|
|
4
|
+
CryptoAssetKey,
|
|
5
|
+
isBitcoinLikeCryptoAsset,
|
|
6
|
+
mapCryptoAssetToDetails,
|
|
7
|
+
} from "../utils/cryptoAssets"
|
|
8
|
+
import { fromFixedPoint } from "../utils/numbers"
|
|
9
|
+
import useAssetsConversionRates from "./useAssetsConversionRates"
|
|
10
|
+
import useWalletAccount from "./useWalletAccount"
|
|
11
|
+
import { useTokensBalances } from "./useTokensBalances"
|
|
12
|
+
import { useBorrowData } from "./useBorrowData"
|
|
13
|
+
|
|
14
|
+
export type DropdownCryptoAsset<T = CryptoAssetKey> = {
|
|
15
|
+
type: T
|
|
16
|
+
balance: bigint
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Aggregates dropdown data for Dropdown component.
|
|
21
|
+
* @returns Dropdown data.
|
|
22
|
+
* @dev This hook is for internal use only.
|
|
23
|
+
*/
|
|
24
|
+
export default function useDropdownData() {
|
|
25
|
+
const { data: btcBalance } = useBalance()
|
|
26
|
+
const {
|
|
27
|
+
walletAddress,
|
|
28
|
+
accountAddress,
|
|
29
|
+
networkFamily: walletType,
|
|
30
|
+
} = useWalletAccount()
|
|
31
|
+
const { data: passportAccount } = useGetCurrentAccount()
|
|
32
|
+
const { data: assetsConversionRates } = useAssetsConversionRates()
|
|
33
|
+
const { data: tokensBalances } = useTokensBalances()
|
|
34
|
+
const { data: debt } = useBorrowData()
|
|
35
|
+
const mezoId = passportAccount?.mezoId
|
|
36
|
+
|
|
37
|
+
const detailedAssets = Object.fromEntries(
|
|
38
|
+
[
|
|
39
|
+
["BTC", btcBalance?.value ?? 0n],
|
|
40
|
+
...Object.entries(tokensBalances ?? {}),
|
|
41
|
+
].map((asset) => {
|
|
42
|
+
const [type, balance] = asset as [CryptoAssetKey, bigint]
|
|
43
|
+
|
|
44
|
+
let usdConversionRate: number | undefined
|
|
45
|
+
if (isBitcoinLikeCryptoAsset(type))
|
|
46
|
+
usdConversionRate = assetsConversionRates?.usd.btc
|
|
47
|
+
if (type === "mT") {
|
|
48
|
+
usdConversionRate = assetsConversionRates?.usd.t
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return [
|
|
52
|
+
type,
|
|
53
|
+
mapCryptoAssetToDetails(type, balance, usdConversionRate || 1),
|
|
54
|
+
]
|
|
55
|
+
}),
|
|
56
|
+
) as Record<CryptoAssetKey, ReturnType<typeof mapCryptoAssetToDetails>>
|
|
57
|
+
|
|
58
|
+
const assets = {
|
|
59
|
+
btc: detailedAssets.BTC,
|
|
60
|
+
musd: detailedAssets.MUSD,
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const otherAssetsData = Object.values(detailedAssets).filter(
|
|
64
|
+
(asset) => !["BTC", "MUSD"].includes(asset.type),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
const otherAssetsCount = otherAssetsData.length
|
|
68
|
+
|
|
69
|
+
const otherAssetsUsdTotal = otherAssetsData.reduce(
|
|
70
|
+
(sum, { usdBalance }) => sum + usdBalance,
|
|
71
|
+
0,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
const usdTotalBalance = Object.values(detailedAssets).reduce(
|
|
75
|
+
(sum, { usdBalance }) => sum + usdBalance,
|
|
76
|
+
0,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
const matsBalance = passportAccount?.mats.totalMats || 0
|
|
80
|
+
|
|
81
|
+
const usdCollateral = fromFixedPoint(
|
|
82
|
+
(debt?.collateral ?? 0n) * BigInt(assetsConversionRates?.usd.btc ?? 0),
|
|
83
|
+
18,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
const usdTroveDebt = fromFixedPoint(debt?.troveDebt ?? 0n, 18)
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
mezoId,
|
|
90
|
+
matsBalance,
|
|
91
|
+
accountAddress,
|
|
92
|
+
walletAddress,
|
|
93
|
+
walletType,
|
|
94
|
+
usdTotalBalance,
|
|
95
|
+
usdTroveDebt,
|
|
96
|
+
usdCollateral,
|
|
97
|
+
assets,
|
|
98
|
+
otherAssetsCount,
|
|
99
|
+
otherAssetsUsdTotal,
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useCallback } from "react"
|
|
2
|
+
import { useGetSession } from "./useGetSession"
|
|
3
|
+
import { useSignOut } from "./useSignOut"
|
|
4
|
+
|
|
5
|
+
function useEnsureNoSessionAndFetchNonce() {
|
|
6
|
+
const { refetch: getSession } = useGetSession(undefined, {
|
|
7
|
+
enabled: false,
|
|
8
|
+
retry: false,
|
|
9
|
+
})
|
|
10
|
+
const { signOutAsync } = useSignOut()
|
|
11
|
+
|
|
12
|
+
const getAndValidateSession = useCallback(async () => {
|
|
13
|
+
const getSessionResult = await getSession()
|
|
14
|
+
|
|
15
|
+
// TODO: We should create a separate endpoint that will always return nonce
|
|
16
|
+
if (getSessionResult.error) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`Sign in error: Failed to fetch the current session: ${getSessionResult.error}`,
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
if (!getSessionResult.data) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"Sign in error: Failed to fetch the current session; no data returned.",
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return getSessionResult.data
|
|
28
|
+
}, [getSession])
|
|
29
|
+
|
|
30
|
+
const ensureNoSessionAndFetchNonce = useCallback(async () => {
|
|
31
|
+
let getSessionResult = await getAndValidateSession()
|
|
32
|
+
|
|
33
|
+
if (!("nonce" in getSessionResult)) {
|
|
34
|
+
// If there is active session we are terminating it
|
|
35
|
+
await signOutAsync()
|
|
36
|
+
// Because we've signed out we can assume nonce will be returned here
|
|
37
|
+
getSessionResult = (await getAndValidateSession()) as { nonce: string }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return getSessionResult.nonce
|
|
41
|
+
}, [getAndValidateSession, signOutAsync])
|
|
42
|
+
|
|
43
|
+
return { ensureNoSessionAndFetchNonce }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { useEnsureNoSessionAndFetchNonce }
|
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import { useQuery, skipToken } from "@tanstack/react-query"
|
|
1
|
+
import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
2
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
3
|
import { QUERY_KEYS } from "./constants"
|
|
4
4
|
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetAccountByMezoIdOrAddressResponse } from "../api"
|
|
5
6
|
|
|
6
|
-
export function useGetAccountByAddress(
|
|
7
|
+
export function useGetAccountByAddress(
|
|
8
|
+
address?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
7
14
|
const authApiClient = useAuthApiClient()
|
|
8
15
|
|
|
9
16
|
return useQuery({
|
|
10
|
-
queryKey: [QUERY_KEYS.
|
|
17
|
+
queryKey: [QUERY_KEYS.ACCOUNT, address],
|
|
11
18
|
queryFn: address
|
|
12
19
|
? () => authApiClient.getAccountByMezoIdOrAddress(address)
|
|
13
20
|
: skipToken,
|
|
14
21
|
staleTime: ONE_MINUTE_MS,
|
|
15
22
|
retry: 1,
|
|
16
|
-
|
|
17
|
-
...useQueryOptions,
|
|
23
|
+
...queryOptions,
|
|
18
24
|
})
|
|
19
25
|
}
|
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import { useQuery, skipToken } from "@tanstack/react-query"
|
|
1
|
+
import { useQuery, skipToken, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
2
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
3
|
import { QUERY_KEYS } from "./constants"
|
|
4
4
|
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetAccountByMezoIdOrAddressResponse } from "../api"
|
|
5
6
|
|
|
6
|
-
export function useGetAccountByMezoId(
|
|
7
|
+
export function useGetAccountByMezoId(
|
|
8
|
+
mezoId?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetAccountByMezoIdOrAddressResponse | null>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
7
14
|
const authApiClient = useAuthApiClient()
|
|
8
15
|
|
|
9
16
|
return useQuery({
|
|
10
|
-
queryKey: [QUERY_KEYS.
|
|
17
|
+
queryKey: [QUERY_KEYS.ACCOUNT, mezoId],
|
|
11
18
|
queryFn: mezoId
|
|
12
19
|
? () => authApiClient.getAccountByMezoIdOrAddress(mezoId)
|
|
13
20
|
: skipToken,
|
|
14
21
|
staleTime: ONE_MINUTE_MS,
|
|
15
22
|
retry: 1,
|
|
16
|
-
|
|
17
|
-
...useQueryOptions,
|
|
23
|
+
...queryOptions,
|
|
18
24
|
})
|
|
19
25
|
}
|
|
@@ -1,16 +1,62 @@
|
|
|
1
|
-
import { useQuery } from "@tanstack/react-query"
|
|
2
|
-
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
|
-
import { QUERY_KEYS } from "./constants"
|
|
1
|
+
import { UseBaseQueryOptions, useQuery } from "@tanstack/react-query"
|
|
4
2
|
import { ONE_MINUTE_MS } from "../utils/time"
|
|
3
|
+
import { QUERY_KEYS } from "./constants"
|
|
4
|
+
import { useAuthApiClient } from "./useAuthApiClient"
|
|
5
|
+
import { usePortalApiClient } from "./usePortalApiClient"
|
|
6
|
+
import type { GetCurrentAccountResponse } from "../api"
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
type GetCurrentAccountResponseWithMats = GetCurrentAccountResponse & {
|
|
9
|
+
mats: {
|
|
10
|
+
totalMats: number
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function useGetCurrentAccount(
|
|
15
|
+
queryOptions: Omit<
|
|
16
|
+
UseBaseQueryOptions<GetCurrentAccountResponseWithMats>,
|
|
17
|
+
"queryKey" | "queryFn"
|
|
18
|
+
> = {},
|
|
19
|
+
) {
|
|
7
20
|
const authApiClient = useAuthApiClient()
|
|
21
|
+
const portalApiClient = usePortalApiClient()
|
|
8
22
|
|
|
9
23
|
return useQuery({
|
|
10
|
-
queryKey: [QUERY_KEYS.
|
|
11
|
-
queryFn: () =>
|
|
24
|
+
queryKey: [QUERY_KEYS.ACCOUNT, QUERY_KEYS.CURRENT],
|
|
25
|
+
queryFn: async () => {
|
|
26
|
+
const currentAccount = await authApiClient.getCurrentAccount()
|
|
27
|
+
const linkedWallets = currentAccount?.linkedAccounts?.filter(
|
|
28
|
+
(account) => account.type === "wallet",
|
|
29
|
+
)
|
|
30
|
+
if (!linkedWallets || linkedWallets.length === 0) {
|
|
31
|
+
return {
|
|
32
|
+
...currentAccount,
|
|
33
|
+
mats: {
|
|
34
|
+
totalMats: 0,
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const addresses = linkedWallets.map(
|
|
40
|
+
(account) => account.btcAddress || account.evmAddress,
|
|
41
|
+
)
|
|
42
|
+
const mats = await Promise.all(
|
|
43
|
+
addresses.map((address) => portalApiClient.getPortalMats(address)),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
const totalMats = mats.reduce(
|
|
47
|
+
(sumMats, currentMats) => sumMats + currentMats.totalMats,
|
|
48
|
+
0,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
...currentAccount,
|
|
53
|
+
mats: {
|
|
54
|
+
totalMats,
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
},
|
|
12
58
|
staleTime: ONE_MINUTE_MS,
|
|
13
59
|
retry: 1,
|
|
14
|
-
...
|
|
60
|
+
...queryOptions,
|
|
15
61
|
})
|
|
16
62
|
}
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
import { useQuery } from "@tanstack/react-query"
|
|
1
|
+
import { useQuery, UseBaseQueryOptions } from "@tanstack/react-query"
|
|
2
2
|
import { useAuthApiClient } from "./useAuthApiClient"
|
|
3
3
|
import { QUERY_KEYS } from "./constants"
|
|
4
4
|
import { ONE_MINUTE_MS } from "../utils/time"
|
|
5
|
+
import type { GetSessionResponse } from "../api"
|
|
5
6
|
|
|
6
|
-
export function useGetSession(
|
|
7
|
+
export function useGetSession(
|
|
8
|
+
code?: string,
|
|
9
|
+
queryOptions: Omit<
|
|
10
|
+
UseBaseQueryOptions<GetSessionResponse>,
|
|
11
|
+
"queryKey" | "queryFn"
|
|
12
|
+
> = {},
|
|
13
|
+
) {
|
|
7
14
|
const authApiClient = useAuthApiClient()
|
|
8
15
|
return useQuery({
|
|
9
16
|
queryKey: [QUERY_KEYS.SESSION, code],
|
|
10
17
|
queryFn: () => authApiClient.getSession(code),
|
|
11
18
|
staleTime: ONE_MINUTE_MS,
|
|
12
19
|
retry: 1,
|
|
13
|
-
...
|
|
20
|
+
...queryOptions,
|
|
14
21
|
})
|
|
15
22
|
}
|
|
@@ -1,44 +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
|
-
import { LinkAccountRequest } from "../api
|
|
8
|
+
import type { LinkAccountRequest, LinkAccountResponse } from "../api"
|
|
4
9
|
import { QUERY_KEYS } from "./constants"
|
|
5
10
|
|
|
6
|
-
export function useLinkAccount(
|
|
11
|
+
export function useLinkAccount(
|
|
12
|
+
mutationOptions: Omit<
|
|
13
|
+
MutationOptions<LinkAccountResponse, DefaultError, LinkAccountRequest>,
|
|
14
|
+
"mutationFn" | "mutationKey"
|
|
15
|
+
> = {},
|
|
16
|
+
) {
|
|
7
17
|
const queryClient = useQueryClient()
|
|
8
18
|
const authApiClient = useAuthApiClient()
|
|
9
19
|
|
|
20
|
+
const { onSuccess: customOnSuccess, ...restMutationOptions } = mutationOptions
|
|
21
|
+
|
|
10
22
|
const { mutate, mutateAsync, ...rest } = useMutation({
|
|
11
23
|
mutationFn: (linkAccountRequest: LinkAccountRequest) =>
|
|
12
24
|
authApiClient.linkAccount(linkAccountRequest),
|
|
13
|
-
onSuccess: (data) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
queryClient.setQueryData(
|
|
17
|
-
[QUERY_KEYS.ACCOUNT_BY_MEZO_ID, data.user_metadata.mezoId],
|
|
18
|
-
data,
|
|
19
|
-
)
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
queryClient.setQueryData(
|
|
23
|
-
[QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.btcAddress],
|
|
24
|
-
data,
|
|
25
|
-
)
|
|
26
|
-
queryClient.setQueryData(
|
|
27
|
-
[QUERY_KEYS.ACCOUNT_BY_ADDRESS, data.user_metadata.evmAddress],
|
|
28
|
-
data,
|
|
29
|
-
)
|
|
30
|
-
} else {
|
|
31
|
-
queryClient.resetQueries({
|
|
32
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_MEZO_ID],
|
|
33
|
-
exact: false,
|
|
34
|
-
})
|
|
35
|
-
queryClient.resetQueries({
|
|
36
|
-
queryKey: [QUERY_KEYS.ACCOUNT_BY_ADDRESS],
|
|
37
|
-
exact: false,
|
|
38
|
-
})
|
|
39
|
-
}
|
|
25
|
+
onSuccess: (data, variables, context) => {
|
|
26
|
+
queryClient.resetQueries({ queryKey: [QUERY_KEYS.ACCOUNT] })
|
|
27
|
+
if (customOnSuccess) customOnSuccess(data, variables, context)
|
|
40
28
|
},
|
|
41
|
-
...
|
|
29
|
+
...restMutationOptions,
|
|
42
30
|
})
|
|
43
31
|
|
|
44
32
|
return { linkAccount: mutate, linkAccountAsync: mutateAsync, ...rest }
|