Skip to content

Commit 5194d9e

Browse files
authored
Merge pull request #194 from blocknative/develop
Release 1.2.2
2 parents 6586f48 + 5adcb28 commit 5194d9e

File tree

7 files changed

+83
-331
lines changed

7 files changed

+83
-331
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-onboard",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"description": "Onboard users to web3 by allowing them to select a wallet, get that wallet ready to transact and have access to synced wallet state.",
55
"keywords": [
66
"ethereum",

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/torus.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ function torus(options: TorusOptions & CommonWalletOptions): WalletModule {
8080
},
8181
desktop: true,
8282
mobile: true,
83+
url: 'https://app.tor.us/',
8384
preferred
8485
}
8586
}

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: 57 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,19 @@ 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+
)
82+
83+
const currentState = get(state)
8484

85-
// clear all current intervals if they exist
86-
currentSyncerIntervals.forEach(
87-
(interval: number | undefined) => interval && clearInterval(interval)
88-
)
85+
// reset state
86+
currentState.balance && balance.reset()
87+
currentState.address && address.reset()
88+
currentState.network && network.reset()
8989

90+
if (walletInterface) {
9091
// start syncing state and save intervals
9192
currentSyncerIntervals = [
9293
address.setStateSyncer(walletInterface.address),
@@ -96,33 +97,54 @@ walletInterface.subscribe((walletInterface: WalletInterface | null) => {
9697
}
9798
})
9899

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-
100+
export function resetWalletState(options?: {
101+
disconnected: boolean
102+
walletName: string
103+
}) {
105104
walletInterface.update((currentInterface: WalletInterface | null) => {
106-
const { disconnected } = options
107-
!disconnected &&
108-
currentInterface &&
109-
currentInterface.disconnect &&
110-
currentInterface.disconnect()
111-
return null
112-
})
105+
// no interface then don't do anything
106+
if (!currentInterface) {
107+
return currentInterface
108+
}
113109

114-
wallet.update(() => ({
115-
name: undefined,
116-
provider: undefined,
117-
connect: undefined,
118-
instance: undefined,
119-
url: undefined,
120-
loading: undefined
121-
}))
110+
// no options object, so do a full reset by disconnecting and setting interface to null
111+
if (!options) {
112+
wallet.update(() => ({
113+
name: undefined,
114+
provider: undefined,
115+
connect: undefined,
116+
instance: undefined,
117+
url: undefined,
118+
loading: undefined
119+
}))
120+
121+
currentInterface.disconnect && currentInterface.disconnect()
122+
123+
return null
124+
}
122125

123-
balance.reset()
124-
address.reset()
125-
network.reset()
126+
const { walletName, disconnected } = options
127+
128+
// if walletName is the same as the current interface name then do a full reset (checking if to do a disconnect)
129+
if (currentInterface.name === walletName) {
130+
wallet.update(() => ({
131+
name: undefined,
132+
provider: undefined,
133+
connect: undefined,
134+
instance: undefined,
135+
url: undefined,
136+
loading: undefined
137+
}))
138+
139+
disconnected &&
140+
currentInterface.disconnect &&
141+
currentInterface.disconnect()
142+
143+
return null
144+
}
145+
146+
return currentInterface
147+
})
126148

127149
app.update(store => ({
128150
...store,
@@ -155,14 +177,13 @@ function createWalletStateSliceStore(options: {
155177
const { subscribe, set } = writable(initialState)
156178

157179
let currentState: string | number | null | undefined
158-
const unsubscribe = subscribe(store => {
180+
subscribe(store => {
159181
currentState = store
160182
})
161183

162184
return {
163185
subscribe,
164186
reset: () => {
165-
unsubscribe()
166187
set(undefined)
167188
},
168189
setStateSyncer: (stateSyncer: StateSyncer) => {

src/utilities.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export function getNetwork(provider: any): Promise<number | any> {
1313
},
1414
(e: any, res: any) => {
1515
e && reject(e)
16-
resolve(Number(res && res.result))
16+
const result = res && res.result
17+
resolve(result && Number(result))
1718
}
1819
)
1920
})
@@ -29,7 +30,8 @@ export function getAddress(provider: any): Promise<string | any> {
2930
},
3031
(e: any, res: any) => {
3132
e && reject(e)
32-
resolve(res && res.result && res.result[0])
33+
const result = res && res.result && res.result[0]
34+
resolve(result)
3335
}
3436
)
3537
})
@@ -52,7 +54,8 @@ export function getBalance(provider: any): Promise<string | any> {
5254
},
5355
(e: any, res: any) => {
5456
e && reject(e)
55-
resolve(res && res.result && new BigNumber(res.result).toString(10))
57+
const result = res && res.result
58+
resolve(result && new BigNumber(result).toString(10))
5659
}
5760
)
5861
})
@@ -70,7 +73,7 @@ export function createModernProviderInterface(provider: any): WalletInterface {
7073
// get the initial value
7174
getAddress(provider).then(func)
7275
provider.on('accountsChanged', (accounts: string[]) =>
73-
func(accounts[0])
76+
func(accounts && accounts[0])
7477
)
7578
}
7679
}
@@ -82,7 +85,9 @@ export function createModernProviderInterface(provider: any): WalletInterface {
8285
onChange: (func: (val: string | number) => void) => {
8386
// get initial value
8487
getNetwork(provider).then(func)
85-
provider.on('networkChanged', func)
88+
provider.on('networkChanged', (netId: string | number) =>
89+
func(netId && Number(netId))
90+
)
8691
}
8792
}
8893
: { get: () => getNetwork(provider) },

0 commit comments

Comments
 (0)