Skip to content

Commit

Permalink
wait for wallet to be fetch before trying to switch chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Da-Colon committed Dec 17, 2024
1 parent 5d4c6fd commit 3a6d01e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/hooks/utils/useAutomaticSwitchChain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect } from 'react';
import { useSwitchChain } from 'wagmi';
import { useSwitchChain, useWalletClient } from 'wagmi';
import { useNetworkConfigStore } from '../../providers/NetworkConfig/useNetworkConfigStore';
import { getChainIdFromPrefix } from '../../utils/url';

Expand All @@ -9,6 +9,7 @@ export const useAutomaticSwitchChain = ({
urlAddressPrefix: string | undefined;
}) => {
const { setCurrentConfig, getConfigByChainId, addressPrefix } = useNetworkConfigStore();
const { isFetched } = useWalletClient();
const { switchChain } = useSwitchChain({
mutation: {
onError: () => {
Expand All @@ -25,9 +26,16 @@ export const useAutomaticSwitchChain = ({
return;
}
const chainId = getChainIdFromPrefix(urlAddressPrefix);
if (addressPrefix !== urlAddressPrefix && urlAddressPrefix !== undefined) {
if (addressPrefix !== urlAddressPrefix && urlAddressPrefix !== undefined && isFetched) {
switchChain({ chainId });
}
setTimeout(() => setCurrentConfig(getConfigByChainId(chainId)), 300);
}, [addressPrefix, setCurrentConfig, getConfigByChainId, urlAddressPrefix, switchChain]);
}, [
addressPrefix,
setCurrentConfig,
getConfigByChainId,
urlAddressPrefix,
switchChain,
isFetched,
]);
};

0 comments on commit 3a6d01e

Please sign in to comment.