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