@@ -7,6 +7,7 @@ import WalletConnectProvider from "@walletconnect/web3-provider";
7
7
type Web3ContextType = {
8
8
account : null | string ;
9
9
provider : null | ethers . providers . Web3Provider ;
10
+ errors : any
10
11
} ;
11
12
12
13
const providerOptions : IProviderOptions = {
@@ -15,7 +16,7 @@ const providerOptions: IProviderOptions = {
15
16
package : WalletConnectProvider ,
16
17
options : {
17
18
rpc : {
18
- 1 : ' https://rinkeby.infura.io/v3/a2eae6b8a91442c8a6e3fe5e8c4ef4bd' ,
19
+ 1 : " https://rinkeby.infura.io/v3/a2eae6b8a91442c8a6e3fe5e8c4ef4bd" ,
19
20
} ,
20
21
} ,
21
22
} ,
@@ -29,6 +30,7 @@ const web3Modal =
29
30
} ) ;
30
31
31
32
const Web3Context = createContext < Web3ContextType & { loading : boolean ; connectWeb3 : ( ) => void } > ( {
33
+ errors : null ,
32
34
account : null ,
33
35
provider : null ,
34
36
loading : false ,
@@ -37,37 +39,36 @@ const Web3Context = createContext<Web3ContextType & { loading: boolean; connectW
37
39
38
40
const Web3ContextProvider : React . FC = ( { children } ) => {
39
41
const [ loading , setLoading ] = useState ( false ) ;
40
- const [ { account, provider } , setWeb3State ] = useState < Web3ContextType > ( {
42
+ const [ { account, provider, errors } , setWeb3State ] = useState < Web3ContextType > ( {
41
43
account : null ,
42
44
provider : null ,
45
+ errors : null ,
43
46
} ) ;
44
47
45
48
const connectWeb3 = useCallback ( async ( ) => {
46
- const modalProvider = web3Modal && ( await web3Modal . connect ( ) ) ;
47
- const currentprovider = new ethers . providers . Web3Provider (
48
- new Web3 ( modalProvider ) . currentProvider as ethers . providers . ExternalProvider ,
49
- ) ;
50
- const currentAccount = await currentprovider . getSigner ( ) . getAddress ( ) ;
49
+ try {
50
+ const modalProvider = web3Modal && ( await web3Modal . connect ( ) ) ;
51
+ const currentprovider = new ethers . providers . Web3Provider (
52
+ new Web3 ( modalProvider ) . currentProvider as ethers . providers . ExternalProvider ,
53
+ ) ;
54
+ const currentAccount = await currentprovider . getSigner ( ) . getAddress ( ) ;
51
55
52
- modalProvider . on ( "accountsChanged" , async ( newAcc : string [ ] ) =>
53
- setWeb3State ( ( prev ) => ( { ...prev , account : newAcc [ 0 ] } ) ) ,
54
- ) ;
56
+ modalProvider . on ( "accountsChanged" , async ( newAcc : string [ ] ) =>
57
+ setWeb3State ( ( prev ) => ( { ...prev , account : newAcc [ 0 ] } ) ) ,
58
+ ) ;
55
59
56
- setWeb3State ( { provider : currentprovider , account : currentAccount } ) ;
57
- setLoading ( false ) ;
60
+ setWeb3State ( { provider : currentprovider , account : currentAccount , errors : null } ) ;
61
+ } catch ( e ) {
62
+ setWeb3State ( { provider : null , account : null , errors : e } ) ;
63
+ } finally {
64
+ setLoading ( false ) ;
65
+ }
58
66
} , [ ] ) ;
59
67
60
- useEffect ( ( ) => {
61
- setLoading ( true ) ;
62
- if ( window . ethereum ) window . ethereum . autoRefreshOnNetworkChange = false ;
63
-
64
- // eslint-disable-next-line no-unused-expressions
65
- web3Modal && web3Modal . cachedProvider ? connectWeb3 ( ) : setLoading ( false ) ;
66
- } , [ connectWeb3 ] ) ;
67
-
68
68
return (
69
69
< Web3Context . Provider
70
70
value = { {
71
+ errors,
71
72
account,
72
73
provider,
73
74
loading,
0 commit comments