Skip to content

Commit

Permalink
small network switching bugfix 1
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 10, 2024
1 parent f370185 commit aac4b00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 68 deletions.
5 changes: 1 addition & 4 deletions frontend/src/components/BalanceDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,7 @@ export function BalanceDisplay({
sessionToken={sessionToken}
resetPeriod={resourceLock.resourceLock.resetPeriod}
onForceWithdraw={() => {
handleInitiateWithdrawal(
balance.chainId,
balance.lockId
);
handleInitiateWithdrawal(balance.lockId);
}}
onDisableForceWithdraw={() => {
handleDisableWithdrawal(
Expand Down
69 changes: 5 additions & 64 deletions frontend/src/hooks/useBalanceDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,70 +149,11 @@ export function useBalanceDisplay() {
[currentChainId, disableForcedWithdrawal, showNotification]
);

const handleInitiateWithdrawal = useCallback(
async (chainId: string, lockId: string) => {
const targetChainId = parseInt(chainId);
if (targetChainId !== currentChainId) {
const tempTxId = `network-switch-${Date.now()}`;
try {
showNotification({
type: 'info',
title: 'Switching Network',
message: `Please confirm the network switch in your wallet...`,
txHash: tempTxId,
autoHide: false,
});

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)}` }],
});

await new Promise((resolve) => setTimeout(resolve, 1000));

showNotification({
type: 'success',
title: 'Network Switched',
message: `Successfully switched to ${chainNames[chainId] || `Chain ${chainId}`}`,
txHash: tempTxId,
autoHide: true,
});
} catch (switchError) {
if ((switchError as WalletError).code === 4902) {
showNotification({
type: 'error',
title: 'Network Not Found',
message: 'Please add this network to your wallet first.',
txHash: tempTxId,
autoHide: true,
});
} else {
console.error('Error switching network:', switchError);
showNotification({
type: 'error',
title: 'Network Switch Failed',
message:
switchError instanceof Error
? switchError.message
: 'Failed to switch network. Please switch manually.',
txHash: tempTxId,
autoHide: true,
});
}
return;
}
}

setSelectedLockId(lockId);
setIsWithdrawalDialogOpen(true);
},
[currentChainId, showNotification]
);
const handleInitiateWithdrawal = useCallback((lockId: string) => {
// Remove manual network switching since wagmi will handle it
setSelectedLockId(lockId);
setIsWithdrawalDialogOpen(true);
}, []);

const handleExecuteWithdrawal = useCallback(
async (
Expand Down

0 comments on commit aac4b00

Please sign in to comment.