Commit 94adc449 authored by cartcrom's avatar cartcrom Committed by GitHub

refactor: convert getConnection to non-hook (#6714)

* refactor: convert getConnection to non-hook

* fix: remove getConnection from hook dependecies

* fix: unecessary optional
parent 4b24e5f7
......@@ -10,7 +10,7 @@ import { AutoRow } from 'components/Row'
import { LoadingBubble } from 'components/Tokens/loading'
import { formatDelta } from 'components/Tokens/TokenDetails/PriceChart'
import Tooltip from 'components/Tooltip'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { usePortfolioBalancesQuery } from 'graphql/data/__generated__/types-and-hooks'
import { useAtomValue } from 'jotai/utils'
import { useProfilePageState, useSellAsset, useWalletCollections } from 'nft/hooks'
......@@ -178,7 +178,6 @@ export default function AuthenticatedHeader({ account, openSettings }: { account
const unclaimedAmount: CurrencyAmount<Token> | undefined = useUserUnclaimedAmount(account)
const isUnclaimed = useUserHasAvailableClaim(account)
const getConnection = useGetConnection()
const connection = getConnection(connector)
const openClaimModal = useToggleModal(ApplicationModal.ADDRESS_CLAIM)
const openNftModal = useToggleModal(ApplicationModal.UNISWAP_NFT_AIRDROP_CLAIM)
......
......@@ -2,7 +2,7 @@ import { t } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core'
import { WalletConnect } from '@web3-react/walletconnect-v2'
import { MouseoverTooltip } from 'components/Tooltip'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { ConnectionType } from 'connection/types'
import { getChainInfo } from 'constants/chainInfo'
import { SupportedChainId, UniWalletSupportedChains } from 'constants/chains'
......@@ -53,8 +53,6 @@ function getChainsFromEIP155Accounts(accounts?: string[]): SupportedChainId[] {
function useWalletSupportedChains() {
const { connector } = useWeb3React()
const getConnection = useGetConnection()
const connectionType = getConnection(connector).type
switch (connectionType) {
......
......@@ -4,7 +4,7 @@ import { InterfaceEventName, WalletConnectionResult } from '@uniswap/analytics-e
import { initializeConnector, MockEIP1193Provider } from '@web3-react/core'
import { EIP1193 } from '@web3-react/eip1193'
import { Provider as EIP1193Provider } from '@web3-react/types'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { Connection, ConnectionType } from 'connection/types'
import useEagerlyConnect from 'hooks/useEagerlyConnect'
import useOrderedConnections from 'hooks/useOrderedConnections'
......@@ -21,7 +21,7 @@ jest.mock('@uniswap/analytics', () => ({
}))
jest.mock('connection', () => {
const { ConnectionType } = jest.requireActual('connection')
return { ConnectionType, useGetConnection: jest.fn() }
return { ConnectionType, getConnection: jest.fn() }
})
jest.mock('hooks/useEagerlyConnect', () => jest.fn())
jest.mock('hooks/useOrderedConnections', () => jest.fn())
......@@ -72,7 +72,7 @@ describe('Web3Provider', () => {
describe('analytics', () => {
beforeEach(() => {
mocked(useGetConnection).mockReturnValue(jest.fn().mockReturnValue(connection))
mocked(getConnection).mockReturnValue(connection)
})
it('sends event when the active account changes', async () => {
......
......@@ -3,7 +3,7 @@ import { CustomUserProperties, InterfaceEventName, WalletConnectionResult } from
import { getWalletMeta } from '@uniswap/conedison/provider/meta'
import { useWeb3React, Web3ReactHooks, Web3ReactProvider } from '@web3-react/core'
import { Connector } from '@web3-react/types'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { isSupportedChain } from 'constants/chains'
import { RPC_PROVIDERS } from 'constants/providers'
import { TraceJsonRpcVariant, useTraceJsonRpcFlag } from 'featureFlags/flags/traceJsonRpc'
......@@ -54,7 +54,6 @@ function Updater() {
// Send analytics events when the active account changes.
const previousAccount = usePrevious(account)
const getConnection = useGetConnection()
const [connectedWallets, addConnectedWallet] = useConnectedWallets()
useEffect(() => {
if (account && account !== previousAccount) {
......@@ -85,17 +84,7 @@ function Updater() {
addConnectedWallet({ account, walletType })
}
}, [
account,
addConnectedWallet,
currentPage,
chainId,
connectedWallets,
connector,
getConnection,
previousAccount,
provider,
])
}, [account, addConnectedWallet, currentPage, chainId, connectedWallets, connector, previousAccount, provider])
return null
}
......
......@@ -6,7 +6,7 @@ import PortfolioDrawer, { useAccountDrawer } from 'components/AccountDrawer'
import PrefetchBalancesWrapper from 'components/AccountDrawer/PrefetchBalancesWrapper'
import Loader from 'components/Icons/LoadingSpinner'
import { IconWrapper } from 'components/Identicon/StatusIcon'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { navSearchInputVisibleSize } from 'hooks/useScreenSize'
import { Portal } from 'nft/components/common/Portal'
import { useIsNftClaimAvailable } from 'nft/hooks/useIsNftClaimAvailable'
......@@ -150,7 +150,6 @@ const StyledConnectButton = styled.button`
function Web3StatusInner() {
const { account, connector, chainId, ENSName } = useWeb3React()
const getConnection = useGetConnection()
const connection = getConnection(connector)
const [, toggleAccountDrawer] = useAccountDrawer()
const handleWalletDropdownClick = useCallback(() => {
......
import INJECTED_DARK_ICON from 'assets/svg/browser-wallet-dark.svg'
import INJECTED_LIGHT_ICON from 'assets/svg/browser-wallet-light.svg'
import { getConnections, useGetConnection } from 'connection'
import { renderHook } from 'test-utils/render'
import { getConnection, getConnections } from 'connection'
import { ConnectionType } from './types'
......@@ -16,7 +15,6 @@ describe('connection utility/metadata tests', () => {
global.window.ethereum = ethereum
const displayed = getConnections().filter((c) => c.shouldDisplay())
const getConnection = renderHook(() => useGetConnection()).result.current
const injected = getConnection(ConnectionType.INJECTED)
const coinbase = getConnection(ConnectionType.COINBASE_WALLET)
const uniswap = getConnection(ConnectionType.UNISWAP_WALLET)
......
......@@ -13,7 +13,6 @@ import INJECTED_DARK_ICON from 'assets/svg/browser-wallet-dark.svg'
import INJECTED_LIGHT_ICON from 'assets/svg/browser-wallet-light.svg'
import UNISWAP_LOGO from 'assets/svg/logo.svg'
import { SupportedChainId } from 'constants/chains'
import { useCallback } from 'react'
import { isMobile, isNonIOSPhone } from 'utils/userAgent'
import { RPC_URLS } from '../constants/networks'
......@@ -157,31 +156,29 @@ export function getConnections() {
]
}
export function useGetConnection() {
return useCallback((c: Connector | ConnectionType) => {
if (c instanceof Connector) {
const connection = getConnections().find((connection) => connection.connector === c)
if (!connection) {
throw Error('unsupported connector')
}
return connection
} else {
switch (c) {
case ConnectionType.INJECTED:
return injectedConnection
case ConnectionType.COINBASE_WALLET:
return coinbaseWalletConnection
case ConnectionType.WALLET_CONNECT:
return walletConnectConnection
case ConnectionType.WALLET_CONNECT_V2:
return walletConnectV2Connection
case ConnectionType.UNISWAP_WALLET:
return uniwalletConnectConnection
case ConnectionType.NETWORK:
return networkConnection
case ConnectionType.GNOSIS_SAFE:
return gnosisSafeConnection
}
export function getConnection(c: Connector | ConnectionType) {
if (c instanceof Connector) {
const connection = getConnections().find((connection) => connection.connector === c)
if (!connection) {
throw Error('unsupported connector')
}
}, [])
return connection
} else {
switch (c) {
case ConnectionType.INJECTED:
return injectedConnection
case ConnectionType.COINBASE_WALLET:
return coinbaseWalletConnection
case ConnectionType.WALLET_CONNECT:
return walletConnectConnection
case ConnectionType.WALLET_CONNECT_V2:
return walletConnectV2Connection
case ConnectionType.UNISWAP_WALLET:
return uniwalletConnectConnection
case ConnectionType.NETWORK:
return networkConnection
case ConnectionType.GNOSIS_SAFE:
return gnosisSafeConnection
}
}
}
import { Connector } from '@web3-react/types'
import { gnosisSafeConnection, networkConnection } from 'connection'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { Connection } from 'connection/types'
import { useEffect } from 'react'
import { useAppDispatch, useAppSelector } from 'state/hooks'
......@@ -22,7 +22,6 @@ export default function useEagerlyConnect() {
const dispatch = useAppDispatch()
const selectedWallet = useAppSelector((state) => state.user.selectedWallet)
const getConnection = useGetConnection()
let selectedConnection: Connection | undefined
if (selectedWallet) {
......
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { ConnectionType } from 'connection/types'
import { useMemo } from 'react'
import { useAppSelector } from 'state/hooks'
......@@ -12,7 +12,7 @@ const SELECTABLE_WALLETS = [
export default function useOrderedConnections() {
const selectedWallet = useAppSelector((state) => state.user.selectedWallet)
const getConnection = useGetConnection()
return useMemo(() => {
const orderedConnectionTypes: ConnectionType[] = []
......@@ -29,5 +29,5 @@ export default function useOrderedConnections() {
orderedConnectionTypes.push(ConnectionType.NETWORK)
return orderedConnectionTypes.map((connectionType) => getConnection(connectionType))
}, [getConnection, selectedWallet])
}, [selectedWallet])
}
import { useWeb3React } from '@web3-react/core'
import { useGetConnection } from 'connection'
import { getConnection } from 'connection'
import { SupportedChainId } from 'constants/chains'
import { useCallback } from 'react'
import { addPopup } from 'state/application/reducer'
......@@ -10,7 +10,6 @@ import { switchChain } from 'utils/switchChain'
export default function useSelectChain() {
const dispatch = useAppDispatch()
const { connector } = useWeb3React()
const getConnection = useGetConnection()
return useCallback(
async (targetChain: SupportedChainId) => {
......@@ -28,6 +27,6 @@ export default function useSelectChain() {
dispatch(addPopup({ content: { failedSwitchNetwork: targetChain }, key: 'failed-network-switch' }))
}
},
[connector, dispatch, getConnection]
[connector, dispatch]
)
}
......@@ -6157,28 +6157,6 @@
"@walletconnect/types" "^1.8.0"
"@walletconnect/utils" "^1.8.0"
"@walletconnect/core@2.7.7":
version "2.7.7"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.7.7.tgz#49ddaa9d8aff365cd347b951d9b4c1c39a949e83"
integrity sha512-/Tmrjx9XDG8qylsUFU2fWvMoxlDwW+zzUcCgTaebMAmssCZ8NSknbBdjAdAKiey1TaLEgFkaCxXgXfioinWNYg==
dependencies:
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "1.0.3"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/jsonrpc-ws-connection" "^1.0.11"
"@walletconnect/keyvaluestorage" "^1.0.2"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/relay-auth" "^1.0.4"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.7.7"
"@walletconnect/utils" "2.7.7"
events "^3.3.0"
lodash.isequal "4.5.0"
uint8arrays "^3.1.0"
"@walletconnect/core@2.7.8":
version "2.7.8"
resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.7.8.tgz#e75329379cc61dc124c85619998a65eecabe4f53"
......@@ -6412,21 +6390,6 @@
dependencies:
tslib "1.14.1"
"@walletconnect/sign-client@2.7.7":
version "2.7.7"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.7.7.tgz#a2be064eaff37ab036919bd33f1cf9ddf4681fdd"
integrity sha512-lTyF8ZEp+HwPNBW/Fw5iWnMm9O5tC1qwf5YfhNczZ7+q6+UUopOoRrsAvwqftJIkgKmfC8lHT52G/XM2JGVjbQ==
dependencies:
"@walletconnect/core" "2.7.7"
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-utils" "1.0.8"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.7.7"
"@walletconnect/utils" "2.7.7"
events "^3.3.0"
"@walletconnect/sign-client@2.7.8":
version "2.7.8"
resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.7.8.tgz#02a4030080d585bbc7772d77b102e3b6fa78e19b"
......@@ -6470,18 +6433,6 @@
dependencies:
tslib "1.14.1"
"@walletconnect/types@2.7.7":
version "2.7.7"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.7.7.tgz#c02831a17b6162d8594c45e3cc4668015e022f51"
integrity sha512-Z4Y+BKPX7X1UBCf7QV35mVy2QU9CS+5G+EthCaJwpieirZNHamHEwNXUjuUUb3PrYOLwlfRYUT5edeFW9wvoeQ==
dependencies:
"@walletconnect/events" "^1.0.1"
"@walletconnect/heartbeat" "1.2.1"
"@walletconnect/jsonrpc-types" "1.0.3"
"@walletconnect/keyvaluestorage" "^1.0.2"
"@walletconnect/logger" "^2.0.1"
events "^3.3.0"
"@walletconnect/types@2.7.8":
version "2.7.8"
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.7.8.tgz#681bd2a3c0e80fcda877a6b6aba09567b938c7a6"
......@@ -6499,22 +6450,6 @@
resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-1.8.0.tgz#3f5e85b2d6b149337f727ab8a71b8471d8d9a195"
integrity sha512-Cn+3I0V0vT9ghMuzh1KzZvCkiAxTq+1TR2eSqw5E5AVWfmCtECFkVZBP6uUJZ8YjwLqXheI+rnjqPy7sVM4Fyg==
"@walletconnect/universal-provider@2.7.7":
version "2.7.7"
resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.7.7.tgz#5c9017672b5d1255442533395eda67b19ffb2f2f"
integrity sha512-MY+R1sLmIKjFYjanWUM6bOM077+SnShSUfSjCTrsoZE2RDddcSz9EtcATovBSPfzPwUTS20mgcgrkRT4zrFRyQ==
dependencies:
"@walletconnect/jsonrpc-http-connection" "^1.0.7"
"@walletconnect/jsonrpc-provider" "1.0.13"
"@walletconnect/jsonrpc-types" "^1.0.2"
"@walletconnect/jsonrpc-utils" "^1.0.7"
"@walletconnect/logger" "^2.0.1"
"@walletconnect/sign-client" "2.7.7"
"@walletconnect/types" "2.7.7"
"@walletconnect/utils" "2.7.7"
eip1193-provider "1.0.1"
events "^3.3.0"
"@walletconnect/universal-provider@2.7.8":
version "2.7.8"
resolved "https://registry.yarnpkg.com/@walletconnect/universal-provider/-/universal-provider-2.7.8.tgz#f7ba6830aeb19fc094f63f6c2fe7f3b04499c88d"
......@@ -6531,26 +6466,6 @@
eip1193-provider "1.0.1"
events "^3.3.0"
"@walletconnect/utils@2.7.7":
version "2.7.7"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.7.7.tgz#e2d8732f8ac3ffbc1de13e923891b256eb3bbefb"
integrity sha512-ozh9gvRAdXkiu+6nOAkoDCokDVPXK/tNATrrYuOhhR+EmGDjlZU2d27HT+HiGREdza0b1HdZN4XneGm0gERV5w==
dependencies:
"@stablelib/chacha20poly1305" "1.0.1"
"@stablelib/hkdf" "1.0.1"
"@stablelib/random" "^1.0.2"
"@stablelib/sha256" "1.0.1"
"@stablelib/x25519" "^1.0.3"
"@walletconnect/relay-api" "^1.0.9"
"@walletconnect/safe-json" "^1.0.2"
"@walletconnect/time" "^1.0.2"
"@walletconnect/types" "2.7.7"
"@walletconnect/window-getters" "^1.0.1"
"@walletconnect/window-metadata" "^1.0.1"
detect-browser "5.3.0"
query-string "7.1.3"
uint8arrays "^3.1.0"
"@walletconnect/utils@2.7.8":
version "2.7.8"
resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.7.8.tgz#494647eb5ed1fa30363c6a127e1a76356e2780a5"
......
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