Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use wallet provider for ENS queries if its on main net #73

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/hooks/useContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ export function useContract<T extends Contract = Contract>(
}

function useMainnetContract<T extends Contract = Contract>(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() {
Expand Down
11 changes: 5 additions & 6 deletions src/hooks/useFetchListCallback.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -11,15 +12,13 @@ import { fetchTokenList } from '../state/lists/actions'

export function useFetchListCallback(): (listUrl: string, skipValidation?: boolean) => Promise<TokenList> {
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
Expand All @@ -30,6 +29,6 @@ export function useFetchListCallback(): (listUrl: string, skipValidation?: boole
throw error
})
},
[dispatch]
[dispatch, mainnetProvider]
)
}
7 changes: 4 additions & 3 deletions src/lib/hooks/useBlockNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,17 @@ 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)
})
// 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(
() => ({
Expand Down