Skip to content

Commit

Permalink
Force wallet network check before evm txs
Browse files Browse the repository at this point in the history
  • Loading branch information
jmrossy committed Mar 15, 2024
1 parent 70edb76 commit 918b44d
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/features/wallet/hooks/evm.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useConnectModal } from '@rainbow-me/rainbowkit';
import { sendTransaction, switchNetwork, waitForTransaction } from '@wagmi/core';
import { getNetwork, sendTransaction, switchNetwork, waitForTransaction } from '@wagmi/core';
import { useCallback, useMemo } from 'react';
import { useAccount, useDisconnect, useNetwork } from 'wagmi';

import { ProviderType, TypedTransactionReceipt, WarpTypedTransaction } from '@hyperlane-xyz/sdk';
import { ProtocolType, sleep } from '@hyperlane-xyz/utils';
import { ProtocolType, assert, sleep } from '@hyperlane-xyz/utils';

import { logger } from '../../../utils/logger';
import { getChainMetadata, tryGetChainMetadata } from '../../chains/utils';
Expand Down Expand Up @@ -72,9 +72,20 @@ export function useEvmTransactionFns(): ChainTransactionFns {
activeChainName?: ChainName;
}) => {
if (tx.type !== ProviderType.EthersV5) throw new Error(`Unsupported tx type: ${tx.type}`);

// If the active chain is different from tx origin chain, try to switch network first
if (activeChainName && activeChainName !== chainName) await onSwitchNetwork(chainName);
logger.debug(`Sending tx on chain ${chainName}`);

// Since the network switching is not foolproof, we also force a network check here
const chainId = getChainMetadata(chainName).chainId as number;
logger.debug('Checking wallet current chain');
const latestNetwork = await getNetwork();
assert(
latestNetwork.chain?.id === chainId,
`Wallet not on chain ${chainName} (ChainMismatchError)`,
);

logger.debug(`Sending tx on chain ${chainName}`);
const wagmiTx = ethers5TxToWagmiTx(tx.transaction);
const { hash } = await sendTransaction({
chainId,
Expand Down

0 comments on commit 918b44d

Please sign in to comment.