Skip to content

Commit

Permalink
pending chain id change
Browse files Browse the repository at this point in the history
  • Loading branch information
mateodaza committed Sep 24, 2024
1 parent bbf99fe commit 2f07fb1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
32 changes: 21 additions & 11 deletions src/components/modals/SwitchNetwork.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useState, useEffect } from 'react';

Check failure on line 1 in src/components/modals/SwitchNetwork.tsx

View workflow job for this annotation

GitHub Actions / build

'useState' is defined but never used

Check failure on line 1 in src/components/modals/SwitchNetwork.tsx

View workflow job for this annotation

GitHub Actions / build

'useEffect' is defined but never used
import {
B,
brandColors,
Expand Down 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,11 +40,15 @@ const SwitchNetwork: FC<ISwitchNetworkModal> = ({
setShowModal,
}) => {
const { isAnimating, closeModal } = useModalAnimation(setShowModal);

const { switchChain } = useSwitchChain();
const { formatMessage } = useIntl();
const { walletChainType, handleSingOutAndSignInWithEVM, chain } =
useGeneralWallet();
const {
walletChainType,
handleSingOutAndSignInWithEVM,
pendingNetworkId,
setPendingNetworkId,
chain,
} = useGeneralWallet();
const chainId = (chain as Chain)?.id;
const theme = useAppSelector(state => state.general.theme);

Expand All @@ -55,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 @@ -67,13 +83,7 @@ const SwitchNetwork: FC<ISwitchNetworkModal> = ({
{desc && <P>{desc}</P>}
{networks?.map(({ networkId, chainType }) => (
<NetworkItem
onClick={() => {
if (walletChainType === ChainType.SOLANA) {
handleSingOutAndSignInWithEVM();
}
switchChain?.({ chainId: networkId });
closeModal();
}}
onClick={() => handleNetworkItemClick(networkId)}
$isSelected={networkId === chainId}
key={networkId}
$baseTheme={theme}
Expand Down
19 changes: 18 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,8 @@ interface IGeneralWalletContext {
handleSignOutAndShowWelcomeModal: () => Promise<void>;
isOnSolana: boolean;
isOnEVM: boolean;
pendingNetworkId: number | null;
setPendingNetworkId: (id: number | null) => void;
}
// Create the context
export const GeneralWalletContext = createContext<IGeneralWalletContext>({
Expand All @@ -76,6 +78,8 @@ export const GeneralWalletContext = createContext<IGeneralWalletContext>({
handleSignOutAndShowWelcomeModal: async () => {},
isOnSolana: false,
isOnEVM: false,
pendingNetworkId: null,
setPendingNetworkId: () => {},
});

const getPhantomSolanaProvider = () => {
Expand All @@ -93,6 +97,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 +113,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 +274,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 +423,8 @@ export const GeneralWalletProvider: React.FC<{
handleSignOutAndShowWelcomeModal,
isOnSolana,
isOnEVM,
pendingNetworkId,
setPendingNetworkId,
};

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

0 comments on commit 2f07fb1

Please sign in to comment.