1
1
import React , { createContext , useContext , useEffect , useMemo , useState } from "react" ;
2
2
import { Provider } from "@ethersproject/abstract-provider" ;
3
3
import { getNetwork } from "@ethersproject/networks" ;
4
- import { BaseProvider , Web3Provider } from "@ethersproject/providers" ;
4
+ import { Web3Provider } from "@ethersproject/providers" ;
5
5
import { useWeb3React } from "@web3-react/core" ;
6
6
7
7
import { isBatchedProvider , isWebSocketAugmentedProvider } from "@liquity/providers" ;
@@ -36,16 +36,6 @@ const wsParams = (network: string, infuraApiKey: string): [string, string] => [
36
36
37
37
const webSocketSupportedNetworks = [ "homestead" , "kovan" , "rinkeby" , "ropsten" , "goerli" ] ;
38
38
39
- const getClientVersion = async ( provider : BaseProvider ) : Promise < string | undefined > => {
40
- try {
41
- const clientVersion = await provider . perform ( "web3_clientVersion" , [ ] ) ;
42
-
43
- if ( typeof clientVersion === "string" ) {
44
- return clientVersion ;
45
- }
46
- } catch { }
47
- } ;
48
-
49
39
export const LiquityProvider : React . FC < LiquityProviderProps > = ( {
50
40
children,
51
41
loader,
@@ -72,14 +62,7 @@ export const LiquityProvider: React.FC<LiquityProviderProps> = ({
72
62
} , [ ] ) ;
73
63
74
64
useEffect ( ( ) => {
75
- if ( ! config || ! connection ) {
76
- return ;
77
- }
78
-
79
- let mounted = true ;
80
- let wsOpen = false ;
81
-
82
- const setupProvider = async ( ) => {
65
+ if ( config && connection ) {
83
66
const { provider, chainId } = connection ;
84
67
85
68
if ( isBatchedProvider ( provider ) && provider . chainId !== chainId ) {
@@ -88,41 +71,22 @@ export const LiquityProvider: React.FC<LiquityProviderProps> = ({
88
71
89
72
if ( isWebSocketAugmentedProvider ( provider ) ) {
90
73
const network = getNetwork ( chainId ) ;
91
- const clientVersion = await getClientVersion ( provider ) ;
92
74
93
- if ( ! mounted ) {
94
- return ;
75
+ if (
76
+ network . name &&
77
+ webSocketSupportedNetworks . includes ( network . name ) &&
78
+ config . infuraApiKey
79
+ ) {
80
+ provider . openWebSocket ( ...wsParams ( network . name , config . infuraApiKey ) ) ;
81
+ } else if ( connection . _isDev ) {
82
+ provider . openWebSocket ( `ws://${ window . location . hostname } :8546` , chainId ) ;
95
83
}
96
84
97
- // If client is HardHat, assume that it's running in fork mode, therefore don't use an
98
- // Infura WebSocket connection, as that would see a different (unforked) blockchain state.
99
- if ( ! clientVersion || ! clientVersion . match ( / h a r d h a t / i) ) {
100
- if (
101
- network . name &&
102
- webSocketSupportedNetworks . includes ( network . name ) &&
103
- config . infuraApiKey
104
- ) {
105
- provider . openWebSocket ( ...wsParams ( network . name , config . infuraApiKey ) ) ;
106
- wsOpen = true ;
107
- } else if ( connection . _isDev ) {
108
- provider . openWebSocket ( `ws://${ window . location . hostname } :8546` , chainId ) ;
109
- wsOpen = true ;
110
- }
111
- }
85
+ return ( ) => {
86
+ provider . closeWebSocket ( ) ;
87
+ } ;
112
88
}
113
- } ;
114
-
115
- setupProvider ( ) ;
116
-
117
- return ( ) => {
118
- const { provider } = connection ;
119
-
120
- if ( isWebSocketAugmentedProvider ( provider ) && wsOpen ) {
121
- provider . closeWebSocket ( ) ;
122
- }
123
-
124
- mounted = false ;
125
- } ;
89
+ }
126
90
} , [ config , connection ] ) ;
127
91
128
92
if ( ! config || ! provider || ! account || ! chainId ) {
0 commit comments