Commit 64cb9f3f authored by Vignesh Mohankumar's avatar Vignesh Mohankumar Committed by GitHub

chore: updates web3-react, adds key for changing connector order (#4085)

* fix connectors changing

* update package

* add connection name

* rename file

* de-dupe

* cb wallet fix

* fix

* yarn change

* log the key

* re-order connections

* memoize the key

* some updates

* rm console

* prevent memory leak
Co-authored-by: default avatarNoah Zinsmeister <noahwz@gmail.com>
parent cb094a1f
import { Trans } from '@lingui/macro' import { Trans } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core' import { useWeb3React } from '@web3-react/core'
import CopyHelper from 'components/AccountDetails/Copy' import CopyHelper from 'components/AccountDetails/Copy'
import { coinbaseWalletConnection } from 'connection'
import { getConnection, getConnectionName, getIsCoinbaseWallet, getIsMetaMask } from 'connection/utils' import { getConnection, getConnectionName, getIsCoinbaseWallet, getIsMetaMask } from 'connection/utils'
import { useCallback, useContext } from 'react' import { useCallback, useContext } from 'react'
import { ExternalLink as LinkIcon } from 'react-feather' import { ExternalLink as LinkIcon } from 'react-feather'
...@@ -247,13 +246,6 @@ export default function AccountDetails({ ...@@ -247,13 +246,6 @@ export default function AccountDetails({
onClick={() => { onClick={() => {
if (connector.deactivate) { if (connector.deactivate) {
connector.deactivate() connector.deactivate()
// Coinbase Wallet SDK does not emit a disconnect event to the provider,
// which is what web3-react uses to reset state. As a workaround we manually
// reset state.
if (connector === coinbaseWalletConnection.connector) {
connector.resetState()
}
} else { } else {
connector.resetState() connector.resetState()
} }
......
import { Web3ReactProvider } from '@web3-react/core' import { Web3ReactHooks, Web3ReactProvider } from '@web3-react/core'
import useConnectors from 'hooks/useConnectors' import { Connector } from '@web3-react/types'
import { Connection } from 'connection'
import { getConnectionName } from 'connection/utils'
import useEagerlyConnect from 'hooks/useEagerlyConnect' import useEagerlyConnect from 'hooks/useEagerlyConnect'
import { ReactNode } from 'react' import useOrderedConnections from 'hooks/useOrderedConnections'
import { ReactNode, useMemo } from 'react'
export default function Web3Provider({ children }: { children: ReactNode }) { export default function Web3Provider({ children }: { children: ReactNode }) {
useEagerlyConnect() useEagerlyConnect()
const connectors = useConnectors() const connections = useOrderedConnections()
const connectors: [Connector, Web3ReactHooks][] = connections.map(({ hooks, connector }) => [connector, hooks])
return <Web3ReactProvider connectors={connectors}>{children}</Web3ReactProvider> const key = useMemo(() => connections.map(({ type }: Connection) => getConnectionName(type)).join('-'), [connections])
return (
<Web3ReactProvider connectors={connectors} key={key}>
{children}
</Web3ReactProvider>
)
} }
...@@ -9,15 +9,6 @@ import { ...@@ -9,15 +9,6 @@ import {
walletConnectConnection, walletConnectConnection,
} from 'connection' } from 'connection'
const CONNECTIONS = [
coinbaseWalletConnection,
fortmaticConnection,
injectedConnection,
networkConnection,
walletConnectConnection,
gnosisSafeConnection,
]
export function getIsInjected(): boolean { export function getIsInjected(): boolean {
return Boolean(window.ethereum) return Boolean(window.ethereum)
} }
...@@ -30,6 +21,14 @@ export function getIsCoinbaseWallet(): boolean { ...@@ -30,6 +21,14 @@ export function getIsCoinbaseWallet(): boolean {
return window.ethereum?.isCoinbaseWallet ?? false return window.ethereum?.isCoinbaseWallet ?? false
} }
const CONNECTIONS = [
gnosisSafeConnection,
injectedConnection,
coinbaseWalletConnection,
walletConnectConnection,
fortmaticConnection,
networkConnection,
]
export function getConnection(c: Connector | ConnectionType) { export function getConnection(c: Connector | ConnectionType) {
if (c instanceof Connector) { if (c instanceof Connector) {
const connection = CONNECTIONS.find((connection) => connection.connector === c) const connection = CONNECTIONS.find((connection) => connection.connector === c)
......
import { Web3ReactHooks } from '@web3-react/core'
import { Connector } from '@web3-react/types'
import { ConnectionType } from 'connection' import { ConnectionType } from 'connection'
import { getConnection } from 'connection/utils' import { getConnection } from 'connection/utils'
import { useMemo } from 'react' import { useMemo } from 'react'
...@@ -8,7 +6,7 @@ import { useAppSelector } from 'state/hooks' ...@@ -8,7 +6,7 @@ import { useAppSelector } from 'state/hooks'
const SELECTABLE_WALLETS = [...BACKFILLABLE_WALLETS, ConnectionType.FORTMATIC] const SELECTABLE_WALLETS = [...BACKFILLABLE_WALLETS, ConnectionType.FORTMATIC]
export default function useConnectors() { export default function useOrderedConnections() {
const selectedWallet = useAppSelector((state) => state.user.selectedWallet) const selectedWallet = useAppSelector((state) => state.user.selectedWallet)
return useMemo(() => { return useMemo(() => {
const orderedConnectionTypes: ConnectionType[] = [] const orderedConnectionTypes: ConnectionType[] = []
...@@ -25,10 +23,6 @@ export default function useConnectors() { ...@@ -25,10 +23,6 @@ export default function useConnectors() {
// Add network connection last as it should be the fallback. // Add network connection last as it should be the fallback.
orderedConnectionTypes.push(ConnectionType.NETWORK) orderedConnectionTypes.push(ConnectionType.NETWORK)
// Convert to web3-react's representation of connectors. return orderedConnectionTypes.map(getConnection)
const web3Connectors: [Connector, Web3ReactHooks][] = orderedConnectionTypes
.map(getConnection)
.map(({ connector, hooks }) => [connector, hooks])
return web3Connectors
}, [selectedWallet]) }, [selectedWallet])
} }
...@@ -48,23 +48,30 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) { ...@@ -48,23 +48,30 @@ export function BlockNumberProvider({ children }: { children: ReactNode }) {
const windowVisible = useIsWindowVisible() const windowVisible = useIsWindowVisible()
useEffect(() => { useEffect(() => {
let stale = false
if (provider && activeChainId && windowVisible) { if (provider && activeChainId && windowVisible) {
// If chainId hasn't changed, don't clear the block. This prevents re-fetching still valid data. // If chainId hasn't changed, don't clear the block. This prevents re-fetching still valid data.
setChainBlock((chainBlock) => (chainBlock.chainId === activeChainId ? chainBlock : { chainId: activeChainId })) setChainBlock((chainBlock) => (chainBlock.chainId === activeChainId ? chainBlock : { chainId: activeChainId }))
provider provider
.getBlockNumber() .getBlockNumber()
.then(onBlock) .then((block) => {
if (!stale) onBlock(block)
})
.catch((error) => { .catch((error) => {
console.error(`Failed to get block number for chainId ${activeChainId}`, error) console.error(`Failed to get block number for chainId ${activeChainId}`, error)
}) })
provider.on('block', onBlock) provider.on('block', onBlock)
return () => { return () => {
stale = true
provider.removeListener('block', onBlock) provider.removeListener('block', onBlock)
} }
} }
return undefined
return void 0
}, [activeChainId, provider, onBlock, setChainBlock, windowVisible]) }, [activeChainId, provider, onBlock, setChainBlock, windowVisible])
const value = useMemo( const value = useMemo(
......
import { ConnectionType } from 'connection' import { ConnectionType } from 'connection'
export const BACKFILLABLE_WALLETS = [ export const BACKFILLABLE_WALLETS = [
ConnectionType.INJECTED,
ConnectionType.COINBASE_WALLET, ConnectionType.COINBASE_WALLET,
ConnectionType.WALLET_CONNECT, ConnectionType.WALLET_CONNECT,
ConnectionType.INJECTED,
] ]
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment