2020-05-12 01:23:01 +03:00
|
|
|
import { Web3Provider } from '@ethersproject/providers'
|
2021-09-14 01:17:54 +03:00
|
|
|
import { useWeb3React } 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'
|
2021-07-23 20:04:18 +03:00
|
|
|
import { gnosisSafe, injected } from '../connectors'
|
2021-09-14 01:17:54 +03:00
|
|
|
import { IS_IN_IFRAME, NetworkContextName } from '../constants/misc'
|
2019-11-26 01:56:31 +03:00
|
|
|
|
2021-05-20 19:21:40 +03:00
|
|
|
export function useActiveWeb3React(): Web3ReactContextInterface<Web3Provider> {
|
2021-09-14 01:17:54 +03:00
|
|
|
const context = useWeb3React<Web3Provider>()
|
|
|
|
const contextNetwork = useWeb3React<Web3Provider>(NetworkContextName)
|
2019-11-26 01:56:31 +03:00
|
|
|
return context.active ? context : contextNetwork
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useEagerConnect() {
|
2021-09-14 01:17:54 +03:00
|
|
|
const { activate, active } = useWeb3React()
|
2019-11-26 01:56:31 +03:00
|
|
|
const [tried, setTried] = useState(false)
|
|
|
|
|
2021-09-14 01:17:54 +03:00
|
|
|
// gnosisSafe.isSafeApp() races a timeout against postMessage, so it delays pageload if we are not in a safe app;
|
|
|
|
// if we are not embedded in an iframe, it is not worth checking
|
|
|
|
const [triedSafe, setTriedSafe] = useState(!IS_IN_IFRAME)
|
2021-07-23 20:04:18 +03:00
|
|
|
|
2021-09-14 01:17:54 +03:00
|
|
|
// first, try connecting to a gnosis safe
|
2019-11-26 01:56:31 +03:00
|
|
|
useEffect(() => {
|
2021-09-14 01:17:54 +03:00
|
|
|
if (!triedSafe) {
|
|
|
|
gnosisSafe.isSafeApp().then((loadedInSafe) => {
|
|
|
|
if (loadedInSafe) {
|
|
|
|
activate(gnosisSafe, undefined, true).catch(() => {
|
|
|
|
setTriedSafe(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setTriedSafe(true)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-09-14 21:20:52 +03:00
|
|
|
}, [activate, setTriedSafe, triedSafe])
|
2021-09-14 01:17:54 +03:00
|
|
|
|
|
|
|
// then, if that fails, try connecting to an injected connector
|
|
|
|
useEffect(() => {
|
|
|
|
if (!active && triedSafe) {
|
2021-07-23 20:04:18 +03:00
|
|
|
injected.isAuthorized().then((isAuthorized) => {
|
|
|
|
if (isAuthorized) {
|
2019-12-05 01:31:56 +03:00
|
|
|
activate(injected, undefined, true).catch(() => {
|
|
|
|
setTried(true)
|
|
|
|
})
|
|
|
|
} else {
|
2021-07-23 20:04:18 +03:00
|
|
|
if (isMobile && window.ethereum) {
|
|
|
|
activate(injected, undefined, true).catch(() => {
|
|
|
|
setTried(true)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setTried(true)
|
|
|
|
}
|
2019-12-05 01:31:56 +03:00
|
|
|
}
|
2021-07-23 20:04:18 +03:00
|
|
|
})
|
|
|
|
}
|
2021-09-14 01:17:54 +03:00
|
|
|
}, [activate, active, triedSafe])
|
2019-11-26 01:56:31 +03:00
|
|
|
|
2021-09-14 01:17:54 +03:00
|
|
|
// wait until we get confirmation of a connection to flip the flag
|
2019-11-26 01:56:31 +03:00
|
|
|
useEffect(() => {
|
2021-09-14 01:17:54 +03:00
|
|
|
if (active) {
|
2019-11-26 01:56:31 +03:00
|
|
|
setTried(true)
|
|
|
|
}
|
2021-09-14 01:17:54 +03:00
|
|
|
}, [active])
|
2019-11-26 01:56:31 +03:00
|
|
|
|
|
|
|
return tried
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use for network and injected - logs user in
|
|
|
|
* and out after checking what network theyre on
|
|
|
|
*/
|
|
|
|
export function useInactiveListener(suppress = false) {
|
2021-09-14 01:17:54 +03:00
|
|
|
const { active, error, activate } = useWeb3React()
|
2019-11-26 01:56:31 +03:00
|
|
|
|
|
|
|
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])
|
|
|
|
}
|