From 4e5bfe63054f22bf28482dad9d5c38e5320005cb Mon Sep 17 00:00:00 2001 From: shadyfox Date: Sun, 31 May 2026 03:12:25 +0200 Subject: [PATCH] fix(wallet): clear React Query cache on Stellar wallet disconnect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #208 useStellarWallet.disconnect() previously only cleared the Zustand wallet store; user-specific cached queries (balances, borrow positions, backstop state, etc.) stayed in memory and briefly flashed across features when a second wallet connected in the same session. Now removes from the cache: - queries under any known user-scoped key prefix (balances, backstopDeposit, borrowLimit, health-factor, kyc-status, portfolio-value, tokenBalance, userCollateral, userDebt, vaultBalance, ...etc) - any query whose key array contains the disconnecting address (catches trailing-address keys like ['orchestrator', 'position', poolId, addr]) Protocol-level queries (pool configs, oracle prices, vault APY) are intentionally left cached — they hold no user data. --- apps/web-app/src/hooks/useStellarWallet.ts | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/apps/web-app/src/hooks/useStellarWallet.ts b/apps/web-app/src/hooks/useStellarWallet.ts index c4d71ebb..0a13b7ba 100644 --- a/apps/web-app/src/hooks/useStellarWallet.ts +++ b/apps/web-app/src/hooks/useStellarWallet.ts @@ -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(); @@ -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(); };