Skip to content

Commit 4435f43

Browse files
authored
Merge pull request #292 from blocknative/develop
Release 1.7.2
2 parents 6630eb5 + ab2a574 commit 4435f43

File tree

10 files changed

+176
-55
lines changed

10 files changed

+176
-55
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.7.1",
3+
"version": "1.7.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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,22 @@ export interface StateAndHelpers extends UserState {
6868
walletSelect: WalletSelectFunction
6969
wallet: Wallet
7070
exit: (completed?: boolean) => void
71+
stateSyncStatus: {
72+
[key: string]:
73+
| null
74+
| CancelablePromise
75+
| Promise<Array<string>>
76+
| Promise<string>
77+
| Promise<void>
78+
balance: null | CancelablePromise
79+
address: null | Promise<Array<string>>
80+
network: null | Promise<string>
81+
}
82+
stateStore: {
83+
address: WalletStateSliceStore
84+
network: WalletStateSliceStore
85+
balance: BalanceStore
86+
}
7187
}
7288

7389
export interface WalletModule {
@@ -253,12 +269,14 @@ export interface WalletStateSliceStore {
253269
setStateSyncer: (
254270
stateSyncer: StateSyncer
255271
) => { clear: () => void } | undefined
272+
get: () => any
256273
}
257274

258275
export interface BalanceStore {
259276
subscribe: (subscriber: (store: any) => void) => () => void
260277
setStateSyncer: (stateSyncer: StateSyncer) => undefined
261278
reset: () => void
279+
get: () => any
262280
}
263281

264282
export interface AppState {
@@ -274,6 +292,8 @@ export interface AppState {
274292
walletCheckInProgress: boolean
275293
walletCheckCompleted: boolean
276294
accountSelectInProgress: boolean
295+
walletSelectDisplayedUI: boolean
296+
walletCheckDisplayedUI: boolean
277297
}
278298

279299
export interface CancelablePromise extends Promise<any> {

src/modules/check/balance.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function balance(
99
description?: string
1010
icon?: string
1111
} = { minimumBalance: '0' }
12-
): (currentState: StateAndHelpers) => WalletCheckModal | undefined {
12+
): (currentState: StateAndHelpers) => Promise<WalletCheckModal | undefined> {
1313
validateType({ name: 'balance options', value: options, type: 'object' })
1414

1515
const { minimumBalance, heading, description, icon } = options
@@ -20,10 +20,20 @@ function balance(
2020
type: 'string'
2121
})
2222

23-
return (StateAndHelpers: StateAndHelpers) => {
24-
const { balance, BigNumber } = StateAndHelpers
23+
return async (StateAndHelpers: StateAndHelpers) => {
24+
const { balance, BigNumber, stateSyncStatus, stateStore } = StateAndHelpers
25+
26+
if (balance === null) {
27+
// wait for balance sync if is still on initial value
28+
if (stateSyncStatus.balance) {
29+
try {
30+
await stateSyncStatus.balance
31+
} catch (error) {}
32+
}
33+
}
34+
2535
// if balance is less than minimum
26-
if (BigNumber(balance).lt(BigNumber(minimumBalance))) {
36+
if (BigNumber(stateStore.balance.get()).lt(BigNumber(minimumBalance))) {
2737
return {
2838
heading: heading || 'Get Some ETH',
2939
description:

src/modules/check/connect.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ function connect(
1414
): WalletCheckModule {
1515
const { heading, description, icon } = options
1616

17-
return (stateAndHelpers: StateAndHelpers): WalletCheckModal | undefined => {
18-
const { wallet, address } = stateAndHelpers
19-
if (!address && wallet && wallet.name) {
17+
return async (
18+
stateAndHelpers: StateAndHelpers
19+
): Promise<WalletCheckModal | undefined> => {
20+
const { wallet, address, stateSyncStatus, stateStore } = stateAndHelpers
21+
if (address === null) {
22+
// wait for address sync if is still on initial value
23+
if (stateSyncStatus.address) {
24+
try {
25+
await stateSyncStatus.address
26+
} catch (error) {}
27+
}
28+
}
29+
30+
if (!stateStore.address.get() && wallet && wallet.name) {
2031
return {
2132
heading: heading || 'Login and Authorize Your Wallet',
2233
description:

src/modules/check/derivation-path.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,13 @@ function derivationPath(
9797
.map((derivation: { path?: string; label: string }) => {
9898
const { path, label } = derivation
9999
return `
100-
<button style="${baseStyles +
100+
<button style="${
101+
baseStyles +
101102
buttonStyles +
102103
(state.dPath === path && !state.showCustomInput
103104
? selectedStyles
104-
: '')}" onclick="window.handleDerivationClick(this)" data-path="${path}">
105+
: '')
106+
}" onclick="window.handleDerivationClick(this)" data-path="${path}">
105107
${label} - ${path}
106108
</button>
107109
`
@@ -110,8 +112,9 @@ function derivationPath(
110112
${
111113
state.showCustomInput
112114
? customInputHtmlString(state.error)
113-
: `<button style="${baseStyles +
114-
buttonStyles}" onclick="window.handleDerivationClick(this)" data-path="custom">Custom Path</button>`
115+
: `<button style="${
116+
baseStyles + buttonStyles
117+
}" onclick="window.handleDerivationClick(this)" data-path="custom">Custom Path</button>`
115118
}
116119
${
117120
state.loading
@@ -178,7 +181,6 @@ function derivationPath(
178181
}
179182
;(window as any).handleCustomInput = handleCustomInput
180183
;(window as any).handleDerivationClick = handleDerivationClick
181-
182184
return {
183185
heading: heading || 'Hardware Wallet Connect',
184186
description:

src/modules/check/network.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { networkName } from '../../utilities'
2-
import { WalletCheckModule, StateAndHelpers } from '../../interfaces'
2+
import { WalletCheckModal, StateAndHelpers } from '../../interfaces'
33
import { networkIcon } from './icons'
44

55
function network(
@@ -8,13 +8,29 @@ function network(
88
description?: string
99
icon?: string
1010
} = {}
11-
): WalletCheckModule | never {
11+
): (currentState: StateAndHelpers) => Promise<WalletCheckModal | undefined> {
1212
const { heading, description, icon } = options
1313

14-
return (stateAndHelpers: StateAndHelpers) => {
15-
const { network, appNetworkId, walletSelect, exit } = stateAndHelpers
14+
return async (stateAndHelpers: StateAndHelpers) => {
15+
const {
16+
network,
17+
appNetworkId,
18+
walletSelect,
19+
exit,
20+
stateSyncStatus,
21+
stateStore
22+
} = stateAndHelpers
1623

17-
if (network != appNetworkId) {
24+
if (network === null) {
25+
// wait for network sync if is still on initial value
26+
if (stateSyncStatus.network) {
27+
try {
28+
await stateSyncStatus.network
29+
} catch (error) {}
30+
}
31+
}
32+
33+
if (stateStore.network.get() != appNetworkId) {
1834
return {
1935
heading: heading || 'You Must Change Networks',
2036
description:

src/onboard.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,22 @@ function init(initialization: Initialization): API {
116116
}))
117117

118118
const appUnsubscribe = app.subscribe((store: AppState) => {
119-
const { walletSelectInProgress, walletSelectCompleted } = store
119+
const {
120+
walletSelectInProgress,
121+
walletSelectCompleted,
122+
walletSelectDisplayedUI
123+
} = store
120124

121125
if (walletSelectInProgress === false) {
122126
appUnsubscribe()
123-
setTimeout(() => resolve(walletSelectCompleted), 500)
127+
128+
// timeout for UI transitions if it was displayed
129+
walletSelectDisplayedUI
130+
? setTimeout(() => {
131+
resolve(walletSelectCompleted)
132+
app.update(store => ({ ...store, displayedUI: false }))
133+
}, 500)
134+
: resolve(walletSelectCompleted)
124135
}
125136
})
126137
})
@@ -138,10 +149,19 @@ function init(initialization: Initialization): API {
138149
}))
139150

140151
const appUnsubscribe = app.subscribe((store: AppState) => {
141-
const { walletCheckInProgress, walletCheckCompleted } = store
152+
const {
153+
walletCheckInProgress,
154+
walletCheckCompleted,
155+
walletCheckDisplayedUI
156+
} = store
142157
if (walletCheckInProgress === false) {
143158
appUnsubscribe()
144-
setTimeout(() => resolve(walletCheckCompleted), 500)
159+
walletCheckDisplayedUI
160+
? setTimeout(() => {
161+
resolve(walletCheckCompleted)
162+
app.update(store => ({ ...store, displayedUI: false }))
163+
}, 500)
164+
: resolve(walletCheckCompleted)
145165
}
146166
})
147167
})
@@ -164,10 +184,15 @@ function init(initialization: Initialization): API {
164184
}))
165185

166186
const appUnsubscribe = app.subscribe((store: AppState) => {
167-
const { accountSelectInProgress } = store
187+
const { accountSelectInProgress, walletSelectDisplayedUI } = store
168188
if (accountSelectInProgress === false) {
169189
appUnsubscribe()
170-
setTimeout(() => resolve(true), 500)
190+
walletSelectDisplayedUI
191+
? setTimeout(() => {
192+
resolve(true)
193+
app.update(store => ({ ...store, displayedUI: false }))
194+
}, 500)
195+
: resolve(true)
171196
}
172197
})
173198
})

src/stores.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@ export const app: WritableStore = writable({
2626
walletCheckCompleted: false,
2727
accountSelectInProgress: false,
2828
autoSelectWallet: '',
29-
checkModules: []
29+
checkModules: [],
30+
walletSelectDisplayedUI: false,
31+
walletCheckDisplayedUI: false
3032
})
3133

32-
export const balanceSyncStatus: {
33-
syncing: null | CancelablePromise
34-
error: string
34+
export const stateSyncStatus: {
35+
[key: string]:
36+
| null
37+
| CancelablePromise
38+
| Promise<Array<string>>
39+
| Promise<string>
40+
| Promise<void>
41+
balance: null | CancelablePromise
42+
address: null | Promise<Array<string>>
43+
network: null | Promise<string>
3544
} = {
36-
syncing: null,
37-
error: ''
45+
balance: null,
46+
address: null,
47+
network: null
3848
}
3949

4050
export const address: WalletStateSliceStore = createWalletStateSliceStore({
@@ -155,8 +165,7 @@ export function resetWalletState(options?: {
155165
return {
156166
...store,
157167
walletSelectInProgress: false,
158-
walletSelectCompleted: false,
159-
autoSelect: false
168+
walletSelectCompleted: false
160169
}
161170
})
162171
}
@@ -202,6 +211,7 @@ function createWalletStateSliceStore(options: {
202211
reset: () => {
203212
set(undefined)
204213
},
214+
get: () => currentState,
205215
setStateSyncer: (stateSyncer: StateSyncer) => {
206216
validateType({ name: 'stateSyncer', value: stateSyncer, type: 'object' })
207217

@@ -222,18 +232,22 @@ function createWalletStateSliceStore(options: {
222232
})
223233

224234
if (onChange) {
225-
onChange(newVal => {
226-
if (newVal || currentState !== initialState) {
227-
set(newVal)
228-
}
235+
stateSyncStatus[parameter] = new Promise(resolve => {
236+
onChange(newVal => {
237+
resolve()
238+
if (newVal || currentState !== initialState) {
239+
set(newVal)
240+
}
241+
})
229242
})
230243
return
231244
}
232245

233246
if (get) {
234247
const interval: any = createInterval(() => {
235-
get()
248+
stateSyncStatus[parameter] = get()
236249
.then(newVal => {
250+
stateSyncStatus[parameter] = null
237251
if (newVal || currentState !== initialState) {
238252
interval.status.active && set(newVal)
239253
}
@@ -242,6 +256,7 @@ function createWalletStateSliceStore(options: {
242256
console.warn(
243257
`Error getting ${parameter} from state syncer: ${err}`
244258
)
259+
stateSyncStatus[parameter] = null
245260
})
246261
}, 200)
247262

@@ -311,8 +326,14 @@ function createBalanceStore(initialState: string | null): BalanceStore {
311326
}
312327
)
313328

329+
let currentState: string | null
330+
subscribe((store: any) => {
331+
currentState = store
332+
})
333+
314334
return {
315335
subscribe,
336+
get: () => currentState,
316337
setStateSyncer: (syncer: StateSyncer) => {
317338
validateType({ name: 'syncer', value: syncer, type: 'object' })
318339

@@ -355,18 +376,21 @@ function syncStateWithTimeout(options: {
355376

356377
const prom = makeCancelable(getState())
357378

358-
balanceSyncStatus.syncing = prom
379+
stateSyncStatus.balance = prom
359380

360381
prom
361382
.then(async (result: string) => {
362383
if (result === currentBalance && pollStart) {
363384
await wait(350)
364385
syncStateWithTimeout(options)
365386
} else {
387+
stateSyncStatus.balance = null
366388
setState(result)
367389
}
368390
})
369-
.catch(() => {})
391+
.catch(() => {
392+
stateSyncStatus.balance = null
393+
})
370394

371395
const timedOut = wait(timeout)
372396

0 commit comments

Comments
 (0)