Skip to content

Commit 25fb542

Browse files
committed
Code refactor, add feature to auto select a wallet for user
1 parent 2be1824 commit 25fb542

File tree

12 files changed

+375
-385
lines changed

12 files changed

+375
-385
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/components/SelectedWallet.svelte

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import Button from "../elements/Button.svelte";
33
import IconDisplay from "../elements/IconDisplay.svelte";
44
import { fade } from "svelte/transition";
5-
export let selectedWallet;
5+
export let selectedWalletModule;
66
export let onBack;
77
export let installMessage;
88
</script>
@@ -21,17 +21,20 @@
2121

2222
<section class="bn-onboard-custom bn-onboard-modal-selected-wallet" in:fade>
2323
<IconDisplay
24-
iconSrc={selectedWallet.iconSrc}
25-
iconSrcSet={selectedWallet.iconSrcSet}
26-
text={selectedWallet.name} />
24+
iconSrc={selectedWalletModule.iconSrc}
25+
iconSrcSet={selectedWalletModule.iconSrcSet}
26+
text={selectedWalletModule.name} />
2727

2828
{#if installMessage}
2929
{@html installMessage}
3030
{/if}
3131

3232
<footer class="bn-onboard-custom bn-onboard-modal-selected-wallet-footer">
33-
<a href={selectedWallet.link} rel="noreferrer noopener" target="_blank">
34-
<Button>Install {selectedWallet.name}</Button>
33+
<a
34+
href={selectedWalletModule.link}
35+
rel="noreferrer noopener"
36+
target="_blank">
37+
<Button>Install {selectedWalletModule.name}</Button>
3538
</a>
3639
<Button onclick={onBack}>Back</Button>
3740
</footer>

src/components/Wallets.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</style>
3636

3737
<ul class="bn-onboard-custom bn-onboard-modal-select-wallets">
38-
{#each modalData.walletModules as wallet}
38+
{#each modalData.primaryWallets as wallet}
3939
<li in:fade>
4040
<IconButton
4141
onclick={() => handleWalletSelect(wallet)}
@@ -45,7 +45,7 @@
4545
</li>
4646
{/each}
4747

48-
{#if modalData.extraWalletModules && !showingAllWalletModules}
48+
{#if modalData.secondaryWallets && !showingAllWalletModules}
4949
<div>
5050
<Button highlight={true} onclick={() => (showingAllWalletModules = true)}>
5151
Show More
@@ -54,7 +54,7 @@
5454
{/if}
5555

5656
{#if showingAllWalletModules}
57-
{#each modalData.extraWalletModules as wallet}
57+
{#each modalData.secondaryWallets as wallet}
5858
<li in:fade>
5959
<IconButton
6060
onclick={() => handleWalletSelect(wallet)}

src/index.js

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
import "regenerator-runtime/runtime"
22

3+
import { get } from "svelte/store"
4+
35
import Onboard from "./views/Onboard.svelte"
4-
import { app, address, network, balance, provider } from "./stores"
5-
import { selectWallet, prepareWallet, config, getState } from "./api"
6-
import { validateInit } from "./validation"
7-
import { getUserAgent } from "./utilities"
6+
7+
import {
8+
app,
9+
address,
10+
network,
11+
balance,
12+
wallet,
13+
state,
14+
walletInterface
15+
} from "./stores"
16+
17+
import { validateInit, validateConfig } from "./validation"
18+
import { isMobileDevice } from "./utilities"
819
import { initializeBlocknative } from "./services"
920

10-
function init(initialization) {
11-
getUserAgent()
21+
import { version } from "../package.json"
1222

23+
function init(initialization) {
1324
validateInit(initialization)
1425

15-
const { subscriptions, ...rest } = initialization
26+
const { subscriptions, dappId, networkId, modules } = initialization
1627

17-
initializeBlocknative(initialization.dappId, initialization.networkId)
28+
initializeBlocknative(dappId, networkId)
1829

1930
app.update(store => ({
2031
...store,
21-
...rest
32+
dappId,
33+
networkId,
34+
version,
35+
mobileDevice: isMobileDevice()
2236
}))
2337

2438
new Onboard({
2539
target: document.body,
2640
props: {
27-
onboardingModules: initialization.modules.prepareWallet
41+
walletSelectModule: modules.walletSelect,
42+
walletReadyModules: modules.walletReady,
43+
walletSelect
2844
}
2945
})
3046

@@ -42,12 +58,63 @@ function init(initialization) {
4258
balance.subscribe(subscriptions.balance)
4359
}
4460

45-
if (subscriptions.provider) {
46-
provider.subscribe(subscriptions.provider)
61+
if (subscriptions.wallet) {
62+
wallet.subscribe(subscriptions.wallet)
4763
}
4864
}
4965

50-
return { selectWallet, prepareWallet, config, getState }
66+
function walletSelect(autoSelectWallet) {
67+
return new Promise(resolve => {
68+
app.update(store => ({
69+
...store,
70+
walletSelectInProgress: true,
71+
autoSelectWallet:
72+
typeof autoSelectWallet === "string" && autoSelectWallet
73+
}))
74+
75+
const appUnsubscribe = app.subscribe(
76+
({ walletSelectInProgress, walletSelectCompleted }) => {
77+
if (walletSelectInProgress === false) {
78+
appUnsubscribe()
79+
setTimeout(() => resolve(walletSelectCompleted), 500)
80+
}
81+
}
82+
)
83+
})
84+
}
85+
86+
function walletReady() {
87+
return new Promise(resolve => {
88+
if (!get(walletInterface)) {
89+
throw new Error("walletSelect must be called before walletReady")
90+
}
91+
92+
app.update(store => ({
93+
...store,
94+
walletReadyInProgress: true
95+
}))
96+
97+
const appUnsubscribe = app.subscribe(
98+
({ walletReadyInProgress, walletReadyCompleted }) => {
99+
if (walletReadyInProgress === false) {
100+
appUnsubscribe()
101+
setTimeout(() => resolve(walletReadyCompleted), 500)
102+
}
103+
}
104+
)
105+
})
106+
}
107+
108+
function config(options) {
109+
validateConfig(options)
110+
app.update(store => ({ ...store, ...options }))
111+
}
112+
113+
function getState() {
114+
return get(state)
115+
}
116+
117+
return { walletSelect, walletReady, config, getState }
51118
}
52119

53120
export default init

src/provider.js

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)