diff --git a/frontend/src/components/ForcedWithdrawalDialog.tsx b/frontend/src/components/ForcedWithdrawalDialog.tsx index 481d254..cadacae 100644 --- a/frontend/src/components/ForcedWithdrawalDialog.tsx +++ b/frontend/src/components/ForcedWithdrawalDialog.tsx @@ -15,6 +15,14 @@ interface ForcedWithdrawalDialogProps { chainId: number; } +interface WalletError extends Error { + code: number; +} + +interface EthereumProvider { + request: (args: { method: string; params: unknown[] }) => Promise; +} + export function ForcedWithdrawalDialog({ isOpen, onClose, @@ -46,31 +54,34 @@ export function ForcedWithdrawalDialog({ message: `Please confirm the network switch in your wallet...`, }); - // Request network switch through the wallet - // @ts-ignore - ethereum is injected by the wallet - await window.ethereum.request({ + const ethereum = window.ethereum as EthereumProvider | undefined; + if (!ethereum) { + throw new Error('No wallet detected'); + } + + await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: `0x${targetChainId.toString(16)}` }], }); // Wait a bit for the network switch to complete await new Promise((resolve) => setTimeout(resolve, 1000)); - } catch (error: any) { + } catch (switchError) { // This error code indicates that the chain has not been added to MetaMask - if (error.code === 4902) { + if ((switchError as WalletError).code === 4902) { showNotification({ type: 'error', title: 'Network Not Found', message: 'Please add this network to your wallet first.', }); } else { - console.error('Error switching network:', error); + console.error('Error switching network:', switchError); showNotification({ type: 'error', title: 'Network Switch Failed', message: - error instanceof Error - ? error.message + switchError instanceof Error + ? switchError.message : 'Failed to switch network. Please switch manually.', }); } diff --git a/frontend/src/components/Transfer.tsx b/frontend/src/components/Transfer.tsx index 3a27d99..fb79858 100644 --- a/frontend/src/components/Transfer.tsx +++ b/frontend/src/components/Transfer.tsx @@ -9,6 +9,14 @@ interface TransferProps { onDisableForceWithdraw: () => void; } +interface WalletError extends Error { + code: number; +} + +interface EthereumProvider { + request: (args: { method: string; params: unknown[] }) => Promise; +} + export function Transfer({ chainId: targetChainId, withdrawalStatus, @@ -34,17 +42,21 @@ export function Transfer({ }); // Request network switch through the wallet - // @ts-ignore - ethereum is injected by the wallet - await window.ethereum.request({ + const ethereum = window.ethereum as EthereumProvider | undefined; + if (!ethereum) { + throw new Error('No wallet detected'); + } + + await ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: `0x${targetChainIdNumber.toString(16)}` }], }); // Wait a bit for the network switch to complete await new Promise((resolve) => setTimeout(resolve, 1000)); - } catch (switchError: any) { + } catch (switchError) { // This error code indicates that the chain has not been added to MetaMask - if (switchError.code === 4902) { + if ((switchError as WalletError).code === 4902) { showNotification({ type: 'error', title: 'Network Not Found', diff --git a/frontend/src/hooks/useSessionPoller.ts b/frontend/src/hooks/useSessionPoller.ts index 7d81329..e6a334d 100644 --- a/frontend/src/hooks/useSessionPoller.ts +++ b/frontend/src/hooks/useSessionPoller.ts @@ -59,18 +59,22 @@ export function useSessionPoller( // Verify session belongs to current address if (data.session?.address.toLowerCase() !== address.toLowerCase()) { - throw new Error('Session address mismatch'); + localStorage.removeItem(`session-${address}`); + onSessionUpdate(null); + return; } // Check if session has expired const expiryTime = new Date(data.session.expiresAt).getTime(); if (expiryTime < Date.now()) { - throw new Error('Session expired'); + localStorage.removeItem(`session-${address}`); + onSessionUpdate(null); + return; } // Session is valid, set it onSessionUpdate(sessionId); - } catch (error) { + } catch { // On any error, clear the session localStorage.removeItem(`session-${address}`); onSessionUpdate(null); @@ -107,15 +111,19 @@ export function useSessionPoller( // Verify session belongs to current address if (data.session?.address.toLowerCase() !== address.toLowerCase()) { - throw new Error('Session address mismatch'); + localStorage.removeItem(`session-${address}`); + onSessionUpdate(null); + return; } // Check if session has expired const expiryTime = new Date(data.session.expiresAt).getTime(); if (expiryTime < Date.now()) { - throw new Error('Session expired'); + localStorage.removeItem(`session-${address}`); + onSessionUpdate(null); + return; } - } catch (error) { + } catch { // On any error, clear the session localStorage.removeItem(`session-${address}`); onSessionUpdate(null);