1
+ import { StaticJsonRpcProvider } from '@ethersproject/providers'
2
+
1
3
import {
2
4
Chain ,
3
- EIP1193Provider ,
4
5
ProviderAccounts ,
5
- WalletInit
6
+ WalletInit ,
7
+ EIP1193Provider ,
8
+ ProviderRpcError ,
9
+ ProviderRpcErrorCode
6
10
} from '@web3-onboard/common'
7
11
8
- import { ProviderRpcError } from '@web3-onboard/common'
9
-
10
12
interface WalletConnectOptions {
11
13
bridge ?: string
12
14
qrcodeModalOptions ?: {
@@ -48,6 +50,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
48
50
public removeListener : typeof EventEmitter [ 'removeListener' ]
49
51
50
52
private disconnected$ : InstanceType < typeof Subject >
53
+ private providers : Record < string , StaticJsonRpcProvider >
51
54
52
55
constructor ( {
53
56
connector,
@@ -63,6 +66,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
63
66
this . connector = connector
64
67
this . chains = chains
65
68
this . disconnected$ = new Subject ( )
69
+ this . providers = { }
66
70
67
71
// listen for session updates
68
72
fromEvent ( this . connector , 'session_update' , ( error , payload ) => {
@@ -103,11 +107,9 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
103
107
104
108
this . disconnect = ( ) => this . connector . killSession ( )
105
109
106
- this . request = ( { method, params } ) => {
110
+ this . request = async ( { method, params } ) => {
107
111
if ( method === 'eth_chainId' ) {
108
- return Promise . resolve (
109
- `0x${ this . connector . chainId . toString ( 16 ) } `
110
- )
112
+ return `0x${ this . connector . chainId . toString ( 16 ) } `
111
113
}
112
114
113
115
if ( method === 'eth_requestAccounts' ) {
@@ -161,7 +163,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
161
163
method === 'eth_selectAccounts'
162
164
) {
163
165
throw new ProviderRpcError ( {
164
- code : 4200 ,
166
+ code : ProviderRpcErrorCode . UNSUPPORTED_METHOD ,
165
167
message : `The Provider does not support the requested method: ${ method } `
166
168
} )
167
169
}
@@ -196,12 +198,33 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
196
198
return this . connector . signTypedData ( params )
197
199
}
198
200
199
- return this . connector . sendCustomRequest ( {
200
- id : 1337 ,
201
- jsonrpc : '2.0' ,
202
- method,
203
- params
204
- } )
201
+ if ( method === 'eth_accounts' ) {
202
+ return this . connector . sendCustomRequest ( {
203
+ id : 1337 ,
204
+ jsonrpc : '2.0' ,
205
+ method,
206
+ params
207
+ } )
208
+ }
209
+
210
+ const chainId = await this . request ( { method : 'eth_chainId' } )
211
+
212
+ if ( ! this . providers [ chainId ] ) {
213
+ const currentChain = chains . find ( ( { id } ) => id === chainId )
214
+
215
+ if ( ! currentChain ) {
216
+ throw new ProviderRpcError ( {
217
+ code : ProviderRpcErrorCode . CHAIN_NOT_ADDED ,
218
+ message : `The Provider does not have a rpcUrl to make a request for the requested method: ${ method } `
219
+ } )
220
+ }
221
+
222
+ this . providers [ chainId ] = new StaticJsonRpcProvider (
223
+ currentChain . rpcUrl
224
+ )
225
+ }
226
+
227
+ return this . providers [ chainId ] . send ( method , params )
205
228
}
206
229
}
207
230
}
0 commit comments