Skip to content

Commit 4d2ef42

Browse files
committed
Add provider for balance check
1 parent bbbe247 commit 4d2ef42

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

src/modules/select/wallets/wallet-connect.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,42 @@ import walletConnectIcon from '../wallet-icons/icon-wallet-connect'
1010
function walletConnect(
1111
options: WalletConnectOptions & CommonWalletOptions
1212
): 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+
}
1429

1530
return {
1631
name: label || 'WalletConnect',
1732
svg: svg || walletConnectIcon,
1833
iconSrc,
1934
wallet: async (helpers: Helpers) => {
20-
const { resetWalletState } = helpers
21-
35+
const createProvider = (await import('./providerEngine')).default
2236
const { default: WalletConnectProvider } = await import(
2337
'@walletconnect/web3-provider'
2438
)
2539

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+
2649
const provider = new WalletConnectProvider({
2750
infuraId: infuraKey,
2851
rpc,
@@ -68,18 +91,13 @@ function walletConnect(
6891
}
6992
},
7093
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+
}
7798

78-
provider.send('eth_getBalance', [
79-
provider.wc._accounts[0],
80-
'latest'
81-
])
82-
})
99+
return getBalance(balanceProvider, provider.wc._accounts[0])
100+
}
83101
},
84102
disconnect: () => {
85103
provider.wc.killSession()

0 commit comments

Comments
 (0)