Skip to content

Commit

Permalink
fix: resolve linting issues
Browse files Browse the repository at this point in the history
- Add proper typing for ethereum provider
- Remove unused error variables in useSessionPoller
- Remove @ts-ignore comments
- Remove any types
- Handle session errors consistently
  • Loading branch information
0age committed Dec 7, 2024
1 parent 89eb030 commit 3155330
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
27 changes: 19 additions & 8 deletions frontend/src/components/ForcedWithdrawalDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ interface ForcedWithdrawalDialogProps {
chainId: number;
}

interface WalletError extends Error {
code: number;
}

interface EthereumProvider {
request: (args: { method: string; params: unknown[] }) => Promise<unknown>;
}

export function ForcedWithdrawalDialog({
isOpen,
onClose,
Expand Down Expand Up @@ -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.',
});
}
Expand Down
20 changes: 16 additions & 4 deletions frontend/src/components/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ interface TransferProps {
onDisableForceWithdraw: () => void;
}

interface WalletError extends Error {
code: number;
}

interface EthereumProvider {
request: (args: { method: string; params: unknown[] }) => Promise<unknown>;
}

export function Transfer({
chainId: targetChainId,
withdrawalStatus,
Expand All @@ -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',
Expand Down
20 changes: 14 additions & 6 deletions frontend/src/hooks/useSessionPoller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3155330

Please sign in to comment.