From 3b62b640ff071aec39513ffc1ea999074daf5133 Mon Sep 17 00:00:00 2001 From: evandrosaturnino Date: Fri, 19 Jul 2024 05:01:15 -0300 Subject: [PATCH 1/3] fix update handler validation --- src/App.tsx | 8 ++++---- src/web3/utils/address.ts | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) 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..92752e243 100644 --- a/src/web3/utils/address.ts +++ b/src/web3/utils/address.ts @@ -17,6 +17,15 @@ export const isSameETHAddress = ( return getAddress(address1) === getAddress(address2) } +export const isSameChainId = ( + chainId1: number | string, + chainId2: number +): boolean => { + const chainId1Str = chainId1.toString() + const chainId2Str = chainId2.toString() + return chainId1Str === chainId2Str || chainId1Str === `0x${chainId2Str}` +} + export const isAddressZero = (address: string): boolean => isSameETHAddress(address, AddressZero) From bddade40536ba322783032d8346282d84278cfe6 Mon Sep 17 00:00:00 2001 From: evandrosaturnino Date: Mon, 29 Jul 2024 11:23:20 -0300 Subject: [PATCH 2/3] refactor chain id validation --- src/web3/utils/address.ts | 10 +--------- src/web3/utils/index.ts | 1 + src/web3/utils/network.ts | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 src/web3/utils/network.ts diff --git a/src/web3/utils/address.ts b/src/web3/utils/address.ts index 92752e243..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) @@ -17,15 +18,6 @@ export const isSameETHAddress = ( return getAddress(address1) === getAddress(address2) } -export const isSameChainId = ( - chainId1: number | string, - chainId2: number -): boolean => { - const chainId1Str = chainId1.toString() - const chainId2Str = chainId2.toString() - return chainId1Str === chainId2Str || chainId1Str === `0x${chainId2Str}` -} - export const isAddressZero = (address: string): boolean => isSameETHAddress(address, AddressZero) 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..480ebe9b4 --- /dev/null +++ b/src/web3/utils/network.ts @@ -0,0 +1,14 @@ +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 => { + const chainId1Hex = toHex(chainId1) + const chainId2Hex = toHex(chainId2) + + return chainId1Hex.toLowerCase() === chainId2Hex.toLowerCase() +} From 0a5d6dd793f43a30490304bd65c280e48f4b6bd3 Mon Sep 17 00:00:00 2001 From: evandrosaturnino Date: Mon, 29 Jul 2024 16:50:05 -0300 Subject: [PATCH 3/3] refactor chain id validation to make it leaner --- src/web3/utils/network.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/web3/utils/network.ts b/src/web3/utils/network.ts index 480ebe9b4..bccf6f080 100644 --- a/src/web3/utils/network.ts +++ b/src/web3/utils/network.ts @@ -6,9 +6,4 @@ export const toHex = (value: string | number): string => export const isSameChainId = ( chainId1: string | number, chainId2: string | number -): boolean => { - const chainId1Hex = toHex(chainId1) - const chainId2Hex = toHex(chainId2) - - return chainId1Hex.toLowerCase() === chainId2Hex.toLowerCase() -} +): boolean => toHex(chainId1) === toHex(chainId2)