Skip to content

Commit

Permalink
Merge pull request #4770 from Giveth/changing-network-bug
Browse files Browse the repository at this point in the history
FIX: switching networks bug
  • Loading branch information
MohammadPCh authored Oct 28, 2024
2 parents 4c6bd33 + 8c75399 commit 02a85a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
29 changes: 14 additions & 15 deletions src/components/modals/SwitchNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const defaultNetworks = Object.keys(networksConfig).map(key => ({
networkId: Number(key),
chainType: networksConfig[Number(key)].chainType,
}));

interface ISwitchNetworkModal extends IModal {
desc?: string;
customNetworks?: INetworkIdWithChain[];
Expand All @@ -39,12 +40,12 @@ const SwitchNetwork: FC<ISwitchNetworkModal> = ({
setShowModal,
}) => {
const { isAnimating, closeModal } = useModalAnimation(setShowModal);

const { switchChain } = useSwitchChain();
const { formatMessage } = useIntl();
const {
walletChainType,
handleSingOutAndSignInWithEVM,
setPendingNetworkId,
handleSignOutAndSignInWithSolana,
chain,
} = useGeneralWallet();
Expand All @@ -59,6 +60,17 @@ const SwitchNetwork: FC<ISwitchNetworkModal> = ({
};
}) || defaultNetworks;

const handleNetworkItemClick = (networkId: number) => {
if (walletChainType === ChainType.SOLANA) {
setPendingNetworkId(networkId);
handleSingOutAndSignInWithEVM();
closeModal(); // Close the modal since we cannot control the wallet modal
} else {
switchChain?.({ chainId: networkId });
closeModal();
}
};

return (
<Modal
headerTitle={formatMessage({ id: 'label.switch_network' })}
Expand All @@ -71,20 +83,7 @@ const SwitchNetwork: FC<ISwitchNetworkModal> = ({
{desc && <P>{desc}</P>}
{networks?.map(({ networkId, chainType }) => (
<NetworkItem
onClick={() => {
if (walletChainType === ChainType.SOLANA) {
handleSingOutAndSignInWithEVM();
}
if (
walletChainType === ChainType.EVM &&
chainType === ChainType.SOLANA
) {
handleSignOutAndSignInWithSolana();
} else {
switchChain?.({ chainId: networkId });
}
closeModal();
}}
onClick={() => handleNetworkItemClick(networkId)}
$isSelected={networkId === chainId}
key={networkId}
$baseTheme={theme}
Expand Down
16 changes: 15 additions & 1 deletion src/providers/generalWalletProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Transaction,
SystemProgram,
} from '@solana/web3.js';
import { useBalance, useDisconnect, useAccount } from 'wagmi';
import { useBalance, useDisconnect, useAccount, useSwitchChain } from 'wagmi';
import { getWalletClient } from '@wagmi/core';
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
import { useWeb3Modal } from '@web3modal/wagmi/react';
Expand Down Expand Up @@ -58,6 +58,7 @@ interface IGeneralWalletContext {
handleSignOutAndShowWelcomeModal: () => Promise<void>;
isOnSolana: boolean;
isOnEVM: boolean;
setPendingNetworkId: (id: number | null) => void;
}
// Create the context
export const GeneralWalletContext = createContext<IGeneralWalletContext>({
Expand All @@ -76,6 +77,7 @@ export const GeneralWalletContext = createContext<IGeneralWalletContext>({
handleSignOutAndShowWelcomeModal: async () => {},
isOnSolana: false,
isOnEVM: false,
setPendingNetworkId: () => {},
});

const getPhantomSolanaProvider = () => {
Expand All @@ -93,6 +95,9 @@ export const GeneralWalletProvider: React.FC<{
const [walletChainType, setWalletChainType] = useState<ChainType | null>(
null,
);
const [pendingNetworkId, setPendingNetworkId] = useState<number | null>(
null,
);
const [walletAddress, setWalletAddress] = useState<string | null>(null);
const [balance, setBalance] = useState<string>();
const [isConnected, setIsConnected] = useState<boolean>(false);
Expand All @@ -106,6 +111,7 @@ export const GeneralWalletProvider: React.FC<{
const router = useRouter();
const { token } = useAppSelector(state => state.user);
const { setVisible, visible } = useWalletModal();
const { switchChain } = useSwitchChain();

const isGIVeconomyRoute = useMemo(
() => checkIsGIVeconomyRoute(router.route),
Expand Down Expand Up @@ -266,6 +272,13 @@ export const GeneralWalletProvider: React.FC<{
}
}, [walletChainType, nonFormattedEvBalance, solanaBalance]);

useEffect(() => {
if (walletChainType === ChainType.EVM && pendingNetworkId !== null) {
switchChain?.({ chainId: pendingNetworkId });
setPendingNetworkId(null);
}
}, [walletChainType, pendingNetworkId]);

const signMessage = async (
message: string,
): Promise<string | undefined> => {
Expand Down Expand Up @@ -408,6 +421,7 @@ export const GeneralWalletProvider: React.FC<{
handleSignOutAndShowWelcomeModal,
isOnSolana,
isOnEVM,
setPendingNetworkId,
};

// Render the provider component with the provided context value
Expand Down

0 comments on commit 02a85a3

Please sign in to comment.