-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcec9a7
commit f8dbf5b
Showing
7 changed files
with
13,474 additions
and
1,942 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { InjectedConnector } from '@web3-react/injected-connector' | ||
|
||
export const injected = new InjectedConnector({ | ||
supportedChainIds: [1, 5], | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useWeb3React } from '@web3-react/core' | ||
|
||
import { injected } from '../connectors' | ||
|
||
export function useEagerConnect() { | ||
const { activate, active } = useWeb3React() | ||
|
||
const [tried, setTried] = useState(false) | ||
|
||
useEffect(() => { | ||
injected.isAuthorized().then((isAuthorized: boolean) => { | ||
if (isAuthorized) { | ||
activate(injected, undefined, true).catch(() => { | ||
setTried(true) | ||
}) | ||
} else { | ||
setTried(true) | ||
} | ||
}) | ||
}, []) // 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 (!tried && active) { | ||
setTried(true) | ||
} | ||
}, [tried, active]) | ||
|
||
return tried | ||
} | ||
|
||
export function useInactiveListener(suppress: boolean = false) { | ||
const { active, error, activate } = useWeb3React() | ||
|
||
useEffect((): any => { | ||
const { ethereum } = window as any | ||
if (ethereum && ethereum.on && !active && !error && !suppress) { | ||
const handleConnect = () => { | ||
console.log("Handling 'connect' event") | ||
activate(injected) | ||
} | ||
const handleChainChanged = (chainId: string | number) => { | ||
console.log("Handling 'chainChanged' event with payload", chainId) | ||
activate(injected) | ||
} | ||
const handleAccountsChanged = (accounts: string[]) => { | ||
console.log("Handling 'accountsChanged' event with payload", accounts) | ||
if (accounts.length > 0) { | ||
activate(injected) | ||
} | ||
} | ||
const handleNetworkChanged = (networkId: string | number) => { | ||
console.log("Handling 'networkChanged' event with payload", networkId) | ||
activate(injected) | ||
} | ||
|
||
ethereum.on('connect', handleConnect) | ||
ethereum.on('chainChanged', handleChainChanged) | ||
ethereum.on('accountsChanged', handleAccountsChanged) | ||
ethereum.on('networkChanged', handleNetworkChanged) | ||
|
||
return () => { | ||
if (ethereum.removeListener) { | ||
ethereum.removeListener('connect', handleConnect) | ||
ethereum.removeListener('chainChanged', handleChainChanged) | ||
ethereum.removeListener('accountsChanged', handleAccountsChanged) | ||
ethereum.removeListener('networkChanged', handleNetworkChanged) | ||
} | ||
} | ||
} | ||
}, [active, error, suppress, activate]) | ||
} |
Oops, something went wrong.