From 765c82dab47883851afe9f0489f88ae5b044efa3 Mon Sep 17 00:00:00 2001 From: KillariDev Date: Mon, 22 Jan 2024 16:37:33 +0200 Subject: [PATCH] use wallet provider for ENS queries if its on mainnet --- src/hooks/useContract.ts | 10 +++++----- src/hooks/useFetchListCallback.ts | 11 +++++------ src/lib/hooks/useBlockNumber.tsx | 7 ++++--- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/hooks/useContract.ts b/src/hooks/useContract.ts index 85565ce87a1..d982bec4500 100644 --- a/src/hooks/useContract.ts +++ b/src/hooks/useContract.ts @@ -61,22 +61,22 @@ export function useContract( } function useMainnetContract(address: string | undefined, ABI: any): T | null { - const { chainId } = useWeb3React() + const { chainId, provider } = useWeb3React() const isMainnet = chainId === ChainId.MAINNET const contract = useContract(isMainnet ? address : undefined, ABI, false) - const providers = RPC_PROVIDERS + const mainnetProvider = + chainId === ChainId.MAINNET && provider !== undefined ? provider : RPC_PROVIDERS[ChainId.MAINNET] return useMemo(() => { if (isMainnet) return contract if (!address) return null - const provider = providers[ChainId.MAINNET] try { - return getContract(address, ABI, provider) + return getContract(address, ABI, mainnetProvider) } catch (error) { console.error('Failed to get mainnet contract', error) return null } - }, [isMainnet, contract, address, providers, ABI]) as T + }, [isMainnet, contract, address, ABI, mainnetProvider]) as T } export function useV2MigratorContract() { diff --git a/src/hooks/useFetchListCallback.ts b/src/hooks/useFetchListCallback.ts index 688c9419128..510e08cedbf 100644 --- a/src/hooks/useFetchListCallback.ts +++ b/src/hooks/useFetchListCallback.ts @@ -1,6 +1,7 @@ import { nanoid } from '@reduxjs/toolkit' import { ChainId } from '@uniswap/sdk-core' import { TokenList } from '@uniswap/token-lists' +import { useWeb3React } from '@web3-react/core' import { RPC_PROVIDERS } from 'constants/providers' import getTokenList from 'lib/hooks/useTokenList/fetchTokenList' import resolveENSContentHash from 'lib/utils/resolveENSContentHash' @@ -11,15 +12,13 @@ import { fetchTokenList } from '../state/lists/actions' export function useFetchListCallback(): (listUrl: string, skipValidation?: boolean) => Promise { const dispatch = useAppDispatch() + const { provider, chainId } = useWeb3React() + const mainnetProvider = chainId === ChainId.MAINNET && provider ? provider : RPC_PROVIDERS[ChainId.MAINNET] return useCallback( async (listUrl: string, skipValidation?: boolean) => { const requestId = nanoid() dispatch(fetchTokenList.pending({ requestId, url: listUrl })) - return getTokenList( - listUrl, - (ensName: string) => resolveENSContentHash(ensName, RPC_PROVIDERS[ChainId.MAINNET]), - skipValidation - ) + return getTokenList(listUrl, (ensName: string) => resolveENSContentHash(ensName, mainnetProvider), skipValidation) .then((tokenList) => { dispatch(fetchTokenList.fulfilled({ url: listUrl, tokenList, requestId })) return tokenList @@ -30,6 +29,6 @@ export function useFetchListCallback(): (listUrl: string, skipValidation?: boole throw error }) }, - [dispatch] + [dispatch, mainnetProvider] ) } diff --git a/src/lib/hooks/useBlockNumber.tsx b/src/lib/hooks/useBlockNumber.tsx index 36c144a2f71..d86d3bf5f2d 100644 --- a/src/lib/hooks/useBlockNumber.tsx +++ b/src/lib/hooks/useBlockNumber.tsx @@ -93,8 +93,9 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) { }, [activeChainId, provider, windowVisible, onChainBlock]) useEffect(() => { - if (mainnetBlock === undefined && provider === undefined) { - RPC_PROVIDERS[ChainId.MAINNET] + if (mainnetBlock === undefined) { + const mainnetProvider = chainId === ChainId.MAINNET && provider ? provider : RPC_PROVIDERS[ChainId.MAINNET] + mainnetProvider .getBlockNumber() .then((block) => { onChainBlock(ChainId.MAINNET, block) @@ -102,7 +103,7 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) { // swallow errors - it's ok if this fails, as we'll try again if we activate mainnet .catch(() => undefined) } - }, [mainnetBlock, onChainBlock, provider]) + }, [mainnetBlock, onChainBlock, provider, chainId]) const value = useMemo( () => ({