Skip to content

Commit 355efbd

Browse files
authored
Merge pull request #189 from blocknative/fix/wallet-connect-disconnect
Improve disconnect for wallet connect. Closes #188
2 parents b2220fa + da91f4d commit 355efbd

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

src/interfaces.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,10 @@ export interface Helpers {
9494
getAddress: (provider: any) => Promise<string | any>
9595
getNetwork: (provider: any) => Promise<number | any>
9696
getBalance: (provider: any) => Promise<string | any>
97-
resetWalletState: (options: { disconnected?: boolean }) => void
97+
resetWalletState: (options: {
98+
disconnected?: boolean
99+
walletName?: string
100+
}) => void
98101
}
99102

100103
export interface WalletInterface {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function walletConnect(
3030
provider.autoRefreshOnNetworkChange = false
3131

3232
provider.wc.on('disconnect', () => {
33-
resetWalletState({ disconnected: true })
33+
resetWalletState({ disconnected: true, walletName: 'WalletConnect' })
3434
})
3535

3636
return {

src/stores.ts

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,54 +75,55 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
7575
)
7676

7777
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
78-
if (walletInterface) {
79-
const currentState = get(state)
80-
// reset state
81-
currentState.balance && balance.reset()
82-
currentState.address && address.reset()
83-
currentState.network && network.reset()
78+
// clear all current intervals if they exist
79+
currentSyncerIntervals.forEach(
80+
(interval: number | undefined) => interval && clearInterval(interval)
81+
)
8482

85-
// clear all current intervals if they exist
86-
currentSyncerIntervals.forEach(
87-
(interval: number | undefined) => interval && clearInterval(interval)
88-
)
83+
const currentState = get(state)
8984

85+
// reset state
86+
currentState.balance && balance.reset()
87+
currentState.address && address.reset()
88+
currentState.network && network.reset()
89+
90+
if (walletInterface) {
9091
// start syncing state and save intervals
9192
currentSyncerIntervals = [
9293
address.setStateSyncer(walletInterface.address),
9394
network.setStateSyncer(walletInterface.network),
9495
balance.setStateSyncer(walletInterface.balance)
9596
]
97+
} else {
98+
wallet.update(() => ({
99+
name: undefined,
100+
provider: undefined,
101+
connect: undefined,
102+
instance: undefined,
103+
url: undefined,
104+
loading: undefined
105+
}))
96106
}
97107
})
98108

99-
export function resetWalletState(options: { disconnected?: boolean } = {}) {
100-
// clear all current intervals if they exist
101-
currentSyncerIntervals.forEach(
102-
(interval: number | undefined) => interval && clearInterval(interval)
103-
)
104-
109+
export function resetWalletState(
110+
options: { disconnected?: boolean; walletName?: string } = {}
111+
) {
105112
walletInterface.update((currentInterface: WalletInterface | null) => {
106-
const { disconnected } = options
107-
!disconnected &&
108-
currentInterface &&
109-
currentInterface.disconnect &&
110-
currentInterface.disconnect()
111-
return null
112-
})
113-
114-
wallet.update(() => ({
115-
name: undefined,
116-
provider: undefined,
117-
connect: undefined,
118-
instance: undefined,
119-
url: undefined,
120-
loading: undefined
121-
}))
113+
if (
114+
!options.walletName ||
115+
(currentInterface && currentInterface.name === options.walletName)
116+
) {
117+
const { disconnected } = options
118+
!disconnected &&
119+
currentInterface &&
120+
currentInterface.disconnect &&
121+
currentInterface.disconnect()
122+
return null
123+
}
122124

123-
balance.reset()
124-
address.reset()
125-
network.reset()
125+
return currentInterface
126+
})
126127

127128
app.update(store => ({
128129
...store,

0 commit comments

Comments
 (0)