Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions apps/web-app/src/hooks/useStellarWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,38 @@

import { getStellarWalletKit } from "@/lib/helpers/stellar/wallet";
import { useStellarWalletStore } from "@/stores/stellarWalletStore";
import { useQueryClient } from "@tanstack/react-query";

/**
* React Query key prefixes that scope to the connected wallet's address.
* These are removed from the cache on wallet disconnect so that connecting a
* second wallet in the same session never flashes the previous wallet's data.
*
* Protocol-level keys (pool configs, oracle prices, vault APYs, etc.) are
* intentionally omitted — they contain no user data and can stay cached.
*/
const USER_SCOPED_QUERY_PREFIXES: readonly string[] = [
"backstopDeposit",
"backstopWalletBalance",
"balances",
"borrowLimit",
"cetesBalance",
"etherfuse-assets",
"health-factor",
"kyc-status",
"portfolio-value",
"repayWalletBalance",
"stellar-balances",
"tokenBalance",
"userCollateral",
"userDebt",
"vaultBalance",
];

export function useStellarWallet() {
const { address, walletName, setWallet, clearWallet } =
useStellarWalletStore();
const queryClient = useQueryClient();

const connect = async () => {
const Kit = await getStellarWalletKit();
Expand All @@ -18,6 +46,28 @@ export function useStellarWallet() {
const disconnect = async () => {
const Kit = await getStellarWalletKit();
await Kit.disconnect();

// Capture the disconnecting address before clearing the store so we can
// also remove address-tagged keys that don't use one of the known prefixes
// (e.g. ["orchestrator", "position", poolId, address]).
const previousAddress = address;

queryClient.removeQueries({
predicate: (query) => {
const first = query.queryKey[0];
if (
typeof first === "string" &&
USER_SCOPED_QUERY_PREFIXES.includes(first)
) {
return true;
}
if (previousAddress) {
return query.queryKey.some((k) => k === previousAddress);
}
return false;
},
});

clearWallet();
};

Expand Down