Skip to content

Commit 12f6430

Browse files
authored
Merge pull request #176 from blocknative/feature/wallet-reset
Feature: Wallet Reset Function. Closes #175
2 parents f0bbae2 + 6e69499 commit 12f6430

File tree

6 files changed

+83
-19
lines changed

6 files changed

+83
-19
lines changed

src/interfaces.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ 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: (disconnected?: boolean) => void
9798
}
9899

99100
export interface WalletInterface {
@@ -108,7 +109,7 @@ export interface WalletInterface {
108109

109110
export interface StateSyncer {
110111
get?: () => Promise<string | number | null>
111-
onChange?: (updater: (val: number | string) => void) => void
112+
onChange?: (updater: (val: number | string | undefined) => void) => void
112113
}
113114

114115
export interface Wallet {
@@ -176,6 +177,7 @@ export interface ConfigOptions {
176177
export interface API {
177178
walletSelect: WalletSelectFunction
178179
walletCheck: WalletCheck
180+
walletReset: () => void
179181
config: Config
180182
getState: GetState
181183
}
@@ -189,7 +191,7 @@ export interface WritableStore {
189191
export interface WalletInterfaceStore {
190192
subscribe: (subscriber: (store: any) => void) => () => void
191193
update: (
192-
updater: (walletInterface: WalletInterface | null) => WalletInterface
194+
updater: (walletInterface: WalletInterface | null) => WalletInterface | null
193195
) => void
194196
set: (walletInterface: WalletInterface) => void | never
195197
}

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { WalletConnectOptions, WalletModule } from '../../../interfaces'
1+
import {
2+
WalletConnectOptions,
3+
WalletModule,
4+
Helpers
5+
} from '../../../interfaces'
26

37
import walletConnectIcon from '../wallet-icons/icon-wallet-connect'
48

@@ -9,7 +13,9 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
913
name: label || 'WalletConnect',
1014
svg: svg || walletConnectIcon,
1115
iconSrc,
12-
wallet: async () => {
16+
wallet: async (helpers: Helpers) => {
17+
const { resetWalletState } = helpers
18+
1319
const { default: WalletConnectProvider } = await import(
1420
'@walletconnect/web3-provider'
1521
)
@@ -20,6 +26,10 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
2026

2127
provider.autoRefreshOnNetworkChange = false
2228

29+
provider.wc.on('disconnect', () => {
30+
resetWalletState(true)
31+
})
32+
2333
return {
2434
provider,
2535
interface: {
@@ -40,7 +50,7 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
4050
onChange: func => {
4151
provider
4252
.send('eth_accounts')
43-
.then((accounts: string[]) => func(accounts[0]))
53+
.then((accounts: string[]) => accounts[0] && func(accounts[0]))
4454
provider.on('accountsChanged', (accounts: string[]) =>
4555
func(accounts[0])
4656
)
@@ -65,7 +75,8 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
6575
'latest'
6676
])
6777
})
68-
}
78+
},
79+
disconnect: () => provider.close()
6980
}
7081
}
7182
},

src/onboard.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import {
1111
balance,
1212
wallet,
1313
state,
14-
walletInterface
14+
walletInterface,
15+
resetWalletState
1516
} from './stores'
1617

1718
import { getDeviceInfo } from './utilities'
@@ -84,7 +85,9 @@ function init(initialization: Initialization): API {
8485

8586
if (subscriptions.network) {
8687
network.subscribe((networkId: number | null) => {
87-
networkId && subscriptions.network(networkId)
88+
if (networkId !== null) {
89+
subscriptions.network(networkId)
90+
}
8891
})
8992
}
9093

@@ -98,7 +101,7 @@ function init(initialization: Initialization): API {
98101

99102
if (subscriptions.wallet) {
100103
wallet.subscribe((wallet: Wallet) => {
101-
wallet.provider && subscriptions.wallet(wallet)
104+
wallet.provider !== null && subscriptions.wallet(wallet)
102105
})
103106
}
104107
}
@@ -144,6 +147,10 @@ function init(initialization: Initialization): API {
144147
})
145148
}
146149

150+
function walletReset(): void {
151+
resetWalletState()
152+
}
153+
147154
function config(options: ConfigOptions): void {
148155
validateConfig(options)
149156
app.update((store: AppState) => ({ ...store, ...options }))
@@ -153,7 +160,7 @@ function init(initialization: Initialization): API {
153160
return get(state)
154161
}
155162

156-
return { walletSelect, walletCheck, config, getState }
163+
return { walletSelect, walletCheck, walletReset, config, getState }
157164
}
158165

159166
export default init

src/stores.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ export const walletInterface: WalletInterfaceStore = createWalletInterfaceStore(
7676

7777
walletInterface.subscribe((walletInterface: WalletInterface | null) => {
7878
if (walletInterface) {
79+
const currentState = get(state)
7980
// reset state
80-
balance.reset()
81-
address.reset()
82-
network.reset()
81+
currentState.balance && balance.reset()
82+
currentState.address && address.reset()
83+
currentState.network && network.reset()
8384

8485
// clear all current intervals if they exist
8586
currentSyncerIntervals.forEach(
@@ -95,6 +96,36 @@ walletInterface.subscribe((walletInterface: WalletInterface | null) => {
9596
}
9697
})
9798

99+
export function resetWalletState(disconnected?: boolean) {
100+
walletInterface.update((currentInterface: WalletInterface | null) => {
101+
!disconnected &&
102+
currentInterface &&
103+
currentInterface.disconnect &&
104+
currentInterface.disconnect()
105+
return null
106+
})
107+
108+
wallet.update(() => ({
109+
name: undefined,
110+
provider: undefined,
111+
connect: undefined,
112+
instance: undefined,
113+
url: undefined,
114+
loading: undefined
115+
}))
116+
117+
balance.reset()
118+
address.reset()
119+
network.reset()
120+
121+
app.update(store => ({
122+
...store,
123+
walletSelectInProgress: false,
124+
walletSelectCompleted: false,
125+
autoSelect: false
126+
}))
127+
}
128+
98129
function createWalletInterfaceStore(
99130
initialState: null | WalletInterface
100131
): WalletInterfaceStore {
@@ -164,7 +195,7 @@ function createWalletStateSliceStore(options: {
164195
function createBalanceStore(initialState: string | null): BalanceStore {
165196
let stateSyncer: StateSyncer
166197
let emitter: any
167-
let emitterAddress: String
198+
let emitterAddress: String | undefined
168199
let cancel: () => void = () => {}
169200

170201
const { subscribe } = derived(
@@ -209,8 +240,11 @@ function createBalanceStore(initialState: string | null): BalanceStore {
209240
} else if (emitterAddress && !$address) {
210241
// no address, so set balance to undefined
211242
set && set(undefined)
243+
emitterAddress = undefined
212244
}
213245
}
246+
247+
set(initialState)
214248
}
215249
)
216250

src/views/WalletCheck.svelte

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import BigNumber from 'bignumber.js'
66
77
import { getBlocknative } from '../services'
8-
import { app, state, balanceSyncStatus } from '../stores'
8+
import { app, state, balanceSyncStatus, walletInterface } from '../stores'
99
import { validateModal, validateWalletCheckModule } from '../validation'
1010
import { isPromise } from '../utilities'
1111
@@ -35,9 +35,18 @@
3535
let actionResolved: boolean | undefined = undefined
3636
let loading: boolean = false
3737
let loadingModal: boolean = false
38+
let stopChecking: boolean = false
39+
40+
const unsubscribe = walletInterface.subscribe(currentInterface => {
41+
if (currentInterface === null) {
42+
stopChecking = true
43+
handleExit()
44+
unsubscribe()
45+
}
46+
})
3847
3948
// recheck modules if below conditions
40-
$: if (!activeModal && !checkingModule) {
49+
$: if (!activeModal && !checkingModule && !stopChecking) {
4150
renderModule()
4251
}
4352
@@ -93,7 +102,7 @@
93102
94103
// poll to automatically to check if condition has been met
95104
pollingInterval = setInterval(async () => {
96-
if (currentModule) {
105+
if (currentModule && !stopChecking) {
97106
const invalid = await invalidState(currentModule, get(state))
98107
if (!invalid && actionResolved !== false) {
99108
resetState()

src/views/WalletSelect.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { get } from 'svelte/store'
44
import { fade } from 'svelte/transition'
55
6-
import { app, walletInterface, wallet } from '../stores'
6+
import { app, walletInterface, wallet, resetWalletState } from '../stores'
77
88
import Modal from '../components/Modal.svelte'
99
import ModalHeader from '../components/ModalHeader.svelte'
@@ -116,7 +116,8 @@
116116
BigNumber,
117117
getNetwork,
118118
getAddress,
119-
getBalance
119+
getBalance,
120+
resetWalletState
120121
})
121122
122123
loadingWallet = undefined

0 commit comments

Comments
 (0)