2020-05-12 01:23:01 +03:00
|
|
|
import { Web3Provider } from '@ethersproject/providers'
|
2021-03-24 04:45:58 +03:00
|
|
|
import { ChainId } from '@uniswap/sdk-core'
|
2019-11-26 01:56:31 +03:00
|
|
|
import { useWeb3React as useWeb3ReactCore } from '@web3-react/core'
|
2020-06-03 21:07:01 +03:00
|
|
|
import { Web3ReactContextInterface } from '@web3-react/core/dist/types'
|
2020-05-29 04:17:45 +03:00
|
|
|
import { useEffect, useState } from 'react'
|
2019-12-05 01:31:56 +03:00
|
|
|
import { isMobile } from 'react-device-detect'
|
2019-11-26 01:56:31 +03:00
|
|
|
import { injected } from '../connectors'
|
2020-05-29 04:17:45 +03:00
|
|
|
import { NetworkContextName } from '../constants'
|
2019-11-26 01:56:31 +03:00
|
|
|
|
2020-06-03 21:07:01 +03:00
|
|
|
export function useActiveWeb3React(): Web3ReactContextInterface<Web3Provider> & { chainId?: ChainId } {
|
2020-05-12 01:23:01 +03:00
|
|
|
const context = useWeb3ReactCore<Web3Provider>()
|
|
|
|
const contextNetwork = useWeb3ReactCore<Web3Provider>(NetworkContextName)
|
2019-11-26 01:56:31 +03:00
|
|
|
return context.active ? context : contextNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useEagerConnect() {
|
|
|
|
const { activate, active } = useWeb3ReactCore() // specifically using useWeb3ReactCore because of what this hook does
|
|
|
|
const [tried, setTried] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-02-16 11:46:17 +03:00
|
|
|
injected.isAuthorized().then((isAuthorized) => {
|
2019-11-26 01:56:31 +03:00
|
|
|
if (isAuthorized) {
|
|
|
|
activate(injected, undefined, true).catch(() => {
|
|
|
|
setTried(true)
|
|
|
|
})
|
|
|
|
} else {
|
2020-05-09 01:32:36 +03:00
|
|
|
if (isMobile && window.ethereum) {
|
2019-12-05 01:31:56 +03:00
|
|
|
activate(injected, undefined, true).catch(() => {
|
|
|
|
setTried(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setTried(true)
|
|
|
|
}
|
2019-11-26 01:56:31 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [activate]) // intentionally only running on mount (make sure it's only mounted once :))
|
|
|
|
|
|
|
|
// if the connection worked, wait until we get confirmation of that to flip the flag
|
|
|
|
useEffect(() => {
|
|
|
|
if (active) {
|
|
|
|
setTried(true)
|
|
|
|
}
|
|
|
|
}, [active])
|
|
|
|
|
|
|
|
return tried
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use for network and injected - logs user in
|
|
|
|
* and out after checking what network theyre on
|
|
|
|
*/
|
|
|
|
export function useInactiveListener(suppress = false) {
|
|
|
|
const { active, error, activate } = useWeb3ReactCore() // specifically using useWeb3React because of what this hook does
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-05-09 01:32:36 +03:00
|
|
|
const { ethereum } = window
|
2019-11-26 01:56:31 +03:00
|
|
|
|
|
|
|
if (ethereum && ethereum.on && !active && !error && !suppress) {
|
2019-12-05 01:31:56 +03:00
|
|
|
const handleChainChanged = () => {
|
2019-11-26 01:56:31 +03:00
|
|
|
// eat errors
|
2021-02-16 11:46:17 +03:00
|
|
|
activate(injected, undefined, true).catch((error) => {
|
2020-05-29 04:17:45 +03:00
|
|
|
console.error('Failed to activate after chain changed', error)
|
2020-05-09 01:32:36 +03:00
|
|
|
})
|
2019-11-26 01:56:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-20 22:39:28 +03:00
|
|
|
const handleAccountsChanged = (accounts: string[]) => {
|
2019-11-26 01:56:31 +03:00
|
|
|
if (accounts.length > 0) {
|
|
|
|
// eat errors
|
2021-02-16 11:46:17 +03:00
|
|
|
activate(injected, undefined, true).catch((error) => {
|
2020-05-29 04:17:45 +03:00
|
|
|
console.error('Failed to activate after accounts changed', error)
|
2020-05-09 01:32:36 +03:00
|
|
|
})
|
2019-11-26 01:56:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 01:31:56 +03:00
|
|
|
ethereum.on('chainChanged', handleChainChanged)
|
2019-11-26 01:56:31 +03:00
|
|
|
ethereum.on('accountsChanged', handleAccountsChanged)
|
|
|
|
|
|
|
|
return () => {
|
2019-12-05 01:31:56 +03:00
|
|
|
if (ethereum.removeListener) {
|
|
|
|
ethereum.removeListener('chainChanged', handleChainChanged)
|
|
|
|
ethereum.removeListener('accountsChanged', handleAccountsChanged)
|
|
|
|
}
|
2019-11-26 01:56:31 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 20:05:09 +03:00
|
|
|
return undefined
|
2019-11-26 01:56:31 +03:00
|
|
|
}, [active, error, suppress, activate])
|
|
|
|
}
|