@@ -10,19 +10,42 @@ import walletConnectIcon from '../wallet-icons/icon-wallet-connect'
10
10
function walletConnect (
11
11
options : WalletConnectOptions & CommonWalletOptions
12
12
) : WalletModule {
13
- const { infuraKey, rpc, bridge, preferred, label, iconSrc, svg } = options
13
+ const {
14
+ infuraKey,
15
+ rpc,
16
+ bridge,
17
+ preferred,
18
+ label,
19
+ iconSrc,
20
+ svg,
21
+ networkId
22
+ } = options
23
+
24
+ if ( ! infuraKey || ( rpc && ! rpc [ networkId ] ) ) {
25
+ throw new Error (
26
+ `A "infuraKey" or a "rpc" object with a parameter of ${ networkId } must be included in the WalletConnect initialization object`
27
+ )
28
+ }
14
29
15
30
return {
16
31
name : label || 'WalletConnect' ,
17
32
svg : svg || walletConnectIcon ,
18
33
iconSrc,
19
34
wallet : async ( helpers : Helpers ) => {
20
- const { resetWalletState } = helpers
21
-
35
+ const createProvider = ( await import ( './providerEngine' ) ) . default
22
36
const { default : WalletConnectProvider } = await import (
23
37
'@walletconnect/web3-provider'
24
38
)
25
39
40
+ const { resetWalletState, networkName, getBalance } = helpers
41
+
42
+ const rpcUrl =
43
+ rpc && rpc [ networkId ]
44
+ ? rpc [ networkId ]
45
+ : `https://${ networkName ( networkId ) } .infura.io/v3/${ infuraKey } `
46
+
47
+ const balanceProvider = createProvider ( { rpcUrl } )
48
+
26
49
const provider = new WalletConnectProvider ( {
27
50
infuraId : infuraKey ,
28
51
rpc,
@@ -68,18 +91,13 @@ function walletConnect(
68
91
}
69
92
} ,
70
93
balance : {
71
- get : ( ) =>
72
- new Promise ( resolve => {
73
- if ( ! provider . wc . _accounts [ 0 ] ) {
74
- resolve ( null )
75
- return
76
- }
94
+ get : async ( ) => {
95
+ if ( ! provider . wc . _accounts [ 0 ] ) {
96
+ return null
97
+ }
77
98
78
- provider . send ( 'eth_getBalance' , [
79
- provider . wc . _accounts [ 0 ] ,
80
- 'latest'
81
- ] )
82
- } )
99
+ return getBalance ( balanceProvider , provider . wc . _accounts [ 0 ] )
100
+ }
83
101
} ,
84
102
disconnect : ( ) => {
85
103
provider . wc . killSession ( )
0 commit comments