Skip to content

Commit 386fd79

Browse files
authored
Fix wallet connect rpc methods (#860)
1 parent 7a505db commit 386fd79

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

packages/walletconnect/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/walletconnect",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"description": "WalletConnect module for web3-onboard",
55
"module": "dist/index.js",
66
"browser": "dist/index.js",
@@ -20,6 +20,7 @@
2020
"typescript": "^4.5.5"
2121
},
2222
"dependencies": {
23+
"@ethersproject/providers": "^5.5.0",
2324
"@web3-onboard/common": "^2.0.0",
2425
"@walletconnect/client": "^1.7.1",
2526
"@walletconnect/qrcode-modal": "^1.7.1",

packages/walletconnect/src/index.ts

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { StaticJsonRpcProvider } from '@ethersproject/providers'
2+
13
import {
24
Chain,
3-
EIP1193Provider,
45
ProviderAccounts,
5-
WalletInit
6+
WalletInit,
7+
EIP1193Provider,
8+
ProviderRpcError,
9+
ProviderRpcErrorCode
610
} from '@web3-onboard/common'
711

8-
import { ProviderRpcError } from '@web3-onboard/common'
9-
1012
interface WalletConnectOptions {
1113
bridge?: string
1214
qrcodeModalOptions?: {
@@ -48,6 +50,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
4850
public removeListener: typeof EventEmitter['removeListener']
4951

5052
private disconnected$: InstanceType<typeof Subject>
53+
private providers: Record<string, StaticJsonRpcProvider>
5154

5255
constructor({
5356
connector,
@@ -63,6 +66,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
6366
this.connector = connector
6467
this.chains = chains
6568
this.disconnected$ = new Subject()
69+
this.providers = {}
6670

6771
// listen for session updates
6872
fromEvent(this.connector, 'session_update', (error, payload) => {
@@ -103,11 +107,9 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
103107

104108
this.disconnect = () => this.connector.killSession()
105109

106-
this.request = ({ method, params }) => {
110+
this.request = async ({ method, params }) => {
107111
if (method === 'eth_chainId') {
108-
return Promise.resolve(
109-
`0x${this.connector.chainId.toString(16)}`
110-
)
112+
return `0x${this.connector.chainId.toString(16)}`
111113
}
112114

113115
if (method === 'eth_requestAccounts') {
@@ -161,7 +163,7 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
161163
method === 'eth_selectAccounts'
162164
) {
163165
throw new ProviderRpcError({
164-
code: 4200,
166+
code: ProviderRpcErrorCode.UNSUPPORTED_METHOD,
165167
message: `The Provider does not support the requested method: ${method}`
166168
})
167169
}
@@ -196,12 +198,33 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
196198
return this.connector.signTypedData(params)
197199
}
198200

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)
205228
}
206229
}
207230
}

0 commit comments

Comments
 (0)