Skip to content

Commit fc5ff4a

Browse files
committed
Dynamic import sdk libraries
1 parent 378cb92 commit fc5ff4a

20 files changed

+316
-127
lines changed

src/components/Wallets.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { WalletSelectModalData, WalletModule } from '../interfaces'
66
export let modalData: WalletSelectModalData
77
export let handleWalletSelect: (wallet: WalletModule) => void
8+
export let loadingWallet: string | undefined
89
910
let showingAllWalletModules: boolean = false
1011
</script>
@@ -62,7 +63,8 @@
6263
onclick={() => handleWalletSelect(wallet)}
6364
iconSrc={wallet.iconSrc}
6465
iconSrcSet={wallet.iconSrcSet}
65-
text={wallet.name} />
66+
text={wallet.name}
67+
{loadingWallet} />
6668
</li>
6769
{/each}
6870

@@ -82,7 +84,8 @@
8284
iconSrc={wallet.iconSrc}
8385
iconSrcSet={wallet.iconSrcSet}
8486
svg={wallet.svg}
85-
text={wallet.name} />
87+
text={wallet.name}
88+
{loadingWallet} />
8689
</li>
8790
{/each}
8891
{/if}

src/elements/IconButton.svelte

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<script lang="ts">
2-
import { app } from "../stores";
3-
export let iconSrc: string;
4-
export let iconSrcSet: string;
5-
export let svg: string;
6-
export let onclick: () => void = () => {};
7-
export let text: string;
2+
import Spinner from './Spinner.svelte'
3+
import { app } from '../stores'
4+
export let iconSrc: string
5+
export let iconSrcSet: string
6+
export let svg: string
7+
export let onclick: () => void = () => {}
8+
export let text: string
9+
export let loadingWallet: string | undefined
810
</script>
911

1012
<style>
@@ -23,7 +25,7 @@
2325
cursor: pointer;
2426
color: inherit;
2527
line-height: 1.15;
26-
font-family: "Helvetica Neue";
28+
font-family: 'Helvetica Neue';
2729
}
2830
2931
button:hover {
@@ -35,6 +37,7 @@
3537
}
3638
3739
div {
40+
display: flex;
3841
justify-content: center;
3942
align-items: center;
4043
text-align: center;
@@ -70,7 +73,9 @@
7073
class="bn-onboard-custom bn-onboard-icon-button"
7174
class:bn-onboard-dark-mode-background-hover={$app.darkMode}>
7275
<div>
73-
{#if svg}
76+
{#if loadingWallet === text}
77+
<Spinner />
78+
{:else if svg}
7479
{@html svg}
7580
{:else}
7681
<img src={iconSrc} srcset={iconSrcSet} alt={text} />

src/elements/Spinner.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
position: absolute;
1313
width: 2em;
1414
height: 2em;
15-
border: 3px solid #4a90e2;
15+
border: 3px solid;
1616
border-radius: 50%;
1717
animation: bn-onboard-loading 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
18-
border-color: #4a90e2 transparent transparent transparent;
18+
border-color: currentColor transparent transparent transparent;
1919
}
2020
.bn-onboard-loading div:nth-child(1) {
2121
animation-delay: -0.45s;

src/interfaces.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export interface Initialization {
33
networkId: number
44
subscriptions: Subscriptions
55
modules: Modules
6+
darkMode?: boolean
67
}
78

89
export interface Subscriptions {
@@ -15,13 +16,13 @@ export interface Subscriptions {
1516

1617
interface Modules {
1718
walletSelect: WalletSelectModule
18-
walletCheck: WalletCheckModule[]
19+
walletCheck: WalletCheckModule[] | Promise<WalletCheckModule[]>
1920
}
2021

2122
export interface WalletSelectModule {
2223
heading: string
2324
description: string
24-
wallets: WalletModule[]
25+
wallets: WalletModule[] | Promise<WalletModule[]>
2526
}
2627

2728
export interface WalletCheckModule {
@@ -74,11 +75,11 @@ export interface WalletModule {
7475
svg?: string
7576
wallet: (
7677
helpers: Helpers
77-
) => {
78+
) => Promise<{
7879
provider: any | undefined
7980
interface: WalletInterface | null
8081
instance?: any
81-
}
82+
}>
8283
link?: string
8384
installMessage?: (wallets: {
8485
currentWallet: string
@@ -125,10 +126,12 @@ export interface Wallet {
125126
export interface SdkWalletOptions {
126127
apiKey: string
127128
networkId: number
129+
preferred?: boolean
128130
}
129131

130132
export interface WalletConnectOptions {
131133
infuraKey: string
134+
preferred?: boolean
132135
}
133136

134137
export interface WalletInit {

src/modules/select/wallets/authereum.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
1-
import Authereum from 'authereum'
21
import authereumIcon from '../wallet-icons/authereum.png'
3-
42
import { networkName } from '../../../utilities'
53
import { WalletModule } from '../../../interfaces'
64
import { validateType } from '../../../validation'
75

8-
function authereum(options: { networkId: number }): WalletModule {
6+
function authereum(options: {
7+
networkId: number
8+
preferred?: boolean
9+
}): WalletModule {
910
validateType({ name: 'Authereum Options', value: options, type: 'object' })
1011

11-
const { networkId } = options
12+
const { networkId, preferred } = options
1213

1314
validateType({ name: 'networkId', value: networkId, type: 'number' })
15+
validateType({
16+
name: 'preferred',
17+
value: preferred,
18+
type: 'boolean',
19+
optional: true
20+
})
1421

1522
return {
1623
name: 'Authereum',
1724
iconSrc: authereumIcon,
18-
wallet: () => {
25+
wallet: async () => {
26+
const { default: Authereum } = await import('authereum')
1927
const authereum = new Authereum({
2028
networkName: networkName(networkId),
2129
disableNotifications: true
@@ -49,7 +57,8 @@ function authereum(options: { networkId: number }): WalletModule {
4957
}
5058
},
5159
desktop: true,
52-
mobile: true
60+
mobile: true,
61+
preferred
5362
}
5463
}
5564

src/modules/select/wallets/coinbase.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import { mobileWalletInstallMessage } from '../content'
22
import { WalletModule, Helpers } from '../../../interfaces'
3+
import { validateType } from '../../../validation'
4+
5+
function coinbase(options: { preferred?: boolean } = {}): WalletModule {
6+
const { preferred } = options
7+
validateType({
8+
name: 'preferred',
9+
value: preferred,
10+
type: 'boolean',
11+
optional: true
12+
})
313

4-
function coinbase(): WalletModule {
514
return {
615
name: 'Coinbase',
716
iconSrc:
817
'https://cdn-images-1.medium.com/max/1200/1*7ywNS48PnonfsvvMu1tTsA.png',
9-
wallet: (helpers: Helpers) => {
18+
wallet: async (helpers: Helpers) => {
1019
const { getProviderName, createLegacyProviderInterface } = helpers
1120
const provider =
1221
(window as any).web3 && (window as any).web3.currentProvider
@@ -21,7 +30,8 @@ function coinbase(): WalletModule {
2130
},
2231
link: 'https://go.cb-w.com/',
2332
installMessage: mobileWalletInstallMessage,
24-
mobile: true
33+
mobile: true,
34+
preferred
2535
}
2636
}
2737

src/modules/select/wallets/dapper.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { extensionInstallMessage } from '../content'
22
import { WalletModule, Helpers } from '../../../interfaces'
3+
import { validateType } from '../../../validation'
34

45
import dapperIcon from '../wallet-icons/icon-dapper.png'
56
import dapperIcon2x from '../wallet-icons/icon-dapper@2x.png'
67

7-
function dapper(): WalletModule {
8+
function dapper(options: { preferred?: boolean } = {}): WalletModule {
9+
const { preferred } = options
10+
11+
validateType({
12+
name: 'preferred',
13+
value: preferred,
14+
type: 'boolean',
15+
optional: true
16+
})
17+
818
return {
919
name: 'Dapper',
1020
iconSrc: dapperIcon,
1121
iconSrcSet: dapperIcon2x,
12-
wallet: (helpers: Helpers) => {
22+
wallet: async (helpers: Helpers) => {
1323
const { createModernProviderInterface, getProviderName } = helpers
1424
const provider = (window as any).ethereum
1525

@@ -23,7 +33,8 @@ function dapper(): WalletModule {
2333
},
2434
link: 'https://www.meetdapper.com/',
2535
installMessage: extensionInstallMessage,
26-
desktop: true
36+
desktop: true,
37+
preferred
2738
}
2839
}
2940

src/modules/select/wallets/fortmatic.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Fortmatic from 'fortmatic'
21
import fortmaticIcon from '../wallet-icons/icon-fortmatic.svg'
32
import { networkName } from '../../../utilities'
43
import { validateType } from '../../../validation'
@@ -7,22 +6,35 @@ import { SdkWalletOptions, WalletModule, Helpers } from '../../../interfaces'
76
function fortmatic(options: SdkWalletOptions): WalletModule {
87
validateType({ name: 'Fortmatic options', value: options, type: 'object' })
98

10-
const { apiKey, networkId } = options
9+
const { apiKey, networkId, preferred } = options
1110

1211
validateType({ name: 'apiKey', value: apiKey, type: 'string' })
1312
validateType({ name: 'networkId', value: networkId, type: 'number' })
13+
validateType({
14+
name: 'preferred',
15+
value: preferred,
16+
type: 'boolean',
17+
optional: true
18+
})
19+
20+
let instance: any
21+
let provider: any
1422

1523
return {
1624
name: 'Fortmatic',
1725
iconSrc: fortmaticIcon,
18-
wallet: (helpers: Helpers) => {
19-
const { BigNumber } = helpers
26+
wallet: async (helpers: Helpers) => {
27+
if (!instance) {
28+
const { default: Fortmatic } = await import('fortmatic')
2029

21-
const instance = new Fortmatic(
22-
apiKey,
23-
networkId === 1 ? undefined : networkName(networkId)
24-
)
25-
const provider = instance.getProvider()
30+
instance = new Fortmatic(
31+
apiKey,
32+
networkId === 1 ? undefined : networkName(networkId)
33+
)
34+
provider = instance.getProvider()
35+
}
36+
37+
const { BigNumber } = helpers
2638

2739
return {
2840
provider,
@@ -51,7 +63,8 @@ function fortmatic(options: SdkWalletOptions): WalletModule {
5163
}
5264
},
5365
desktop: true,
54-
mobile: true
66+
mobile: true,
67+
preferred
5568
}
5669
}
5770

src/modules/select/wallets/metamask.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { extensionInstallMessage } from '../content'
2+
import { WalletModule, Helpers } from '../../../interfaces'
3+
import { validateType } from '../../../validation'
24

35
import metamaskIcon from '../wallet-icons/icon-metamask.png'
46
import metamaskIcon2x from '../wallet-icons/icon-metamask@2x.png'
57

6-
import { WalletModule, Helpers } from '../../../interfaces'
8+
function metamask(options: { preferred?: boolean } = {}): WalletModule {
9+
const { preferred } = options
10+
11+
validateType({
12+
name: 'preferred',
13+
value: preferred,
14+
type: 'boolean',
15+
optional: true
16+
})
717

8-
function metamask(): WalletModule {
918
return {
1019
name: 'MetaMask',
1120
iconSrc: metamaskIcon,
1221
iconSrcSet: metamaskIcon2x,
13-
wallet: (helpers: Helpers) => {
22+
wallet: async (helpers: Helpers) => {
1423
const {
1524
getProviderName,
1625
createModernProviderInterface,
@@ -33,7 +42,8 @@ function metamask(): WalletModule {
3342
},
3443
link: 'https://metamask.io/',
3544
installMessage: extensionInstallMessage,
36-
desktop: true
45+
desktop: true,
46+
preferred
3747
}
3848
}
3949

src/modules/select/wallets/opera-touch.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { extensionInstallMessage } from '../content'
2+
import { WalletModule, Helpers } from '../../../interfaces'
3+
import { validateType } from '../../../validation'
24

35
import operaTouchIcon from '../wallet-icons/icon-opera-touch.png'
46
import operaTouchIcon2x from '../wallet-icons/icon-opera-touch@2x.png'
57

6-
import { WalletModule, Helpers } from '../../../interfaces'
8+
function operaTouch(options: { preferred?: boolean } = {}): WalletModule {
9+
const { preferred } = options
10+
11+
validateType({
12+
name: 'preferred',
13+
value: preferred,
14+
type: 'boolean',
15+
optional: true
16+
})
717

8-
function operaTouch(): WalletModule {
918
return {
1019
name: 'Opera Touch',
1120
iconSrc: operaTouchIcon,
1221
iconSrcSet: operaTouchIcon2x,
13-
wallet: (helpers: Helpers) => {
22+
wallet: async (helpers: Helpers) => {
1423
const { getProviderName, createModernProviderInterface } = helpers
1524

1625
const provider =
@@ -27,7 +36,8 @@ function operaTouch(): WalletModule {
2736
},
2837
link: 'https://www.opera.com/mobile/touch',
2938
installMessage: extensionInstallMessage,
30-
mobile: true
39+
mobile: true,
40+
preferred
3141
}
3242
}
3343

0 commit comments

Comments
 (0)