diff --git a/src/App.tsx b/src/App.tsx index 46eb56ac2..a28b23132 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -38,7 +38,7 @@ import { useSubscribeToToppedUpEvent } from "./hooks/useSubscribeToToppedUpEvent import { pages } from "./pages" import { useCheckBonusEligibility } from "./hooks/useCheckBonusEligibility" import { useFetchStakingRewards } from "./hooks/useFetchStakingRewards" -import { isSameETHAddress } from "./web3/utils" +import { isSameChainId, isSameETHAddress } from "./web3/utils" import { ThresholdProvider } from "./contexts/ThresholdContext" import { LedgerLiveAppProvider } from "./contexts/LedgerLiveAppContext" import { @@ -133,17 +133,17 @@ const useSubscribeToVendingMachineContractEvents = () => { const AppBody = () => { const dispatch = useDispatch() - const { connector, account, deactivate } = useWeb3React() + const { connector, account, chainId, deactivate } = useWeb3React() useEffect(() => { const updateHandler = (update: ConnectorUpdate) => { // if chain is changed then just deactivate the current provider and reset // store - if (update.chainId) { + if (update.chainId && !isSameChainId(update.chainId, chainId as number)) { dispatch(resetStoreAction()) deactivate() } else if ( - !update.account || + update.account && !isSameETHAddress(update.account, account as string) ) { // dispatch(resetStoreAction()) diff --git a/src/web3/utils/address.ts b/src/web3/utils/address.ts index 6ec86890c..922ff4d1e 100644 --- a/src/web3/utils/address.ts +++ b/src/web3/utils/address.ts @@ -4,6 +4,7 @@ import { } from "@ethersproject/address" import { AddressZero } from "@ethersproject/constants" export { unprefixedAndUncheckedAddress } from "../../threshold-ts/utils" +import { ethers } from "ethers" export const getAddress = (address: string) => ethersGetAddress(address) diff --git a/src/web3/utils/index.ts b/src/web3/utils/index.ts index e18ec8561..f4ddd771c 100644 --- a/src/web3/utils/index.ts +++ b/src/web3/utils/index.ts @@ -2,3 +2,4 @@ export * from "./multicall" export * from "./events" export * from "./address" export * from "./files" +export * from "./network" diff --git a/src/web3/utils/network.ts b/src/web3/utils/network.ts new file mode 100644 index 000000000..bccf6f080 --- /dev/null +++ b/src/web3/utils/network.ts @@ -0,0 +1,9 @@ +import { ethers } from "ethers" + +export const toHex = (value: string | number): string => + ethers.utils.hexlify(value) + +export const isSameChainId = ( + chainId1: string | number, + chainId2: string | number +): boolean => toHex(chainId1) === toHex(chainId2)