Skip to content

Commit fc1939a

Browse files
authored
Merge pull request #321 from blocknative/enhancement/wallet-connect-balance
Enhancement: WalletConnect Balance. Closes #316
2 parents 40c2abd + bd69209 commit fc1939a

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

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

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,44 @@ 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) {
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+
}
1431

1532
return {
1633
name: label || 'WalletConnect',
1734
svg: svg || walletConnectIcon,
1835
iconSrc,
1936
wallet: async (helpers: Helpers) => {
20-
const { resetWalletState } = helpers
21-
37+
const createProvider = (await import('./providerEngine')).default
2238
const { default: WalletConnectProvider } = await import(
2339
'@walletconnect/web3-provider'
2440
)
2541

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+
2651
const provider = new WalletConnectProvider({
2752
infuraId: infuraKey,
2853
rpc,
@@ -68,18 +93,13 @@ function walletConnect(
6893
}
6994
},
7095
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+
}
77100

78-
provider.send('eth_getBalance', [
79-
provider.wc._accounts[0],
80-
'latest'
81-
])
82-
})
101+
return getBalance(balanceProvider, provider.wc._accounts[0])
102+
}
83103
},
84104
disconnect: () => {
85105
provider.wc.killSession()

0 commit comments

Comments
 (0)