Skip to content

Commit 12894f1

Browse files
authored
Merge pull request #137 from blocknative/fix/select-wallet-twice
Only allow wallet to be instantiated once. Closes #136
2 parents f8e5bba + 62b4519 commit 12894f1

File tree

6 files changed

+36
-51
lines changed

6 files changed

+36
-51
lines changed

src/modules/select/wallets/authereum.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,33 @@ function authereum(options: {
2424
iconSrc: authereumIcon,
2525
wallet: async () => {
2626
const { default: Authereum } = await import('authereum')
27-
const authereum = new Authereum({
27+
const instance = new Authereum({
2828
networkName: networkName(networkId),
2929
disableNotifications: true
3030
})
3131

32-
const provider = authereum.getProvider()
32+
const provider = instance.getProvider()
3333

3434
return {
3535
provider,
36+
instance,
3637
interface: {
3738
name: 'Authereum',
3839
connect: () => provider.enable(),
39-
disconnect: () => authereum.logout(),
40+
disconnect: () => instance.logout(),
4041
loading: new Promise((resolve: () => void) => {
41-
authereum.on('openPopup', resolve)
42+
instance.on('openPopup', resolve)
4243
}),
4344
address: {
44-
get: () => authereum.getAccountAddress()
45+
get: () => instance.getAccountAddress()
4546
},
4647
network: {
4748
get: () => Promise.resolve(networkId)
4849
},
4950
balance: {
5051
get: async () => {
51-
const loggedIn = await authereum.isAuthenticated()
52-
return loggedIn && authereum.getBalance()
52+
const loggedIn = await instance.isAuthenticated()
53+
return loggedIn && instance.getBalance()
5354
}
5455
}
5556
}

src/modules/select/wallets/fortmatic.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,17 @@ function fortmatic(options: SdkWalletOptions): WalletModule {
3030
optional: true
3131
})
3232

33-
let instance: any
34-
let provider: any
35-
3633
return {
3734
name: 'Fortmatic',
3835
svg: fortmaticIcon,
3936
wallet: async (helpers: Helpers) => {
40-
if (!instance) {
41-
const { default: Fortmatic } = await import('fortmatic')
37+
const { default: Fortmatic } = await import('fortmatic')
4238

43-
instance = new Fortmatic(
44-
apiKey,
45-
networkId === 1 ? undefined : networkName(networkId)
46-
)
47-
provider = instance.getProvider()
48-
}
39+
const instance = new Fortmatic(
40+
apiKey,
41+
networkId === 1 ? undefined : networkName(networkId)
42+
)
43+
const provider = instance.getProvider()
4944

5045
const { BigNumber } = helpers
5146

src/modules/select/wallets/portis.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,13 @@ function portis(options: SdkWalletOptions): WalletModule {
2020
optional: true
2121
})
2222

23-
let instance: any
24-
let provider: any
25-
2623
return {
2724
name: 'Portis',
2825
svg: portisIcon,
2926
wallet: async (helpers: Helpers) => {
30-
if (!instance) {
31-
const { default: Portis } = await import('@portis/web3')
32-
instance = new Portis(apiKey, networkName(networkId))
33-
provider = instance.provider
34-
}
27+
const { default: Portis } = await import('@portis/web3')
28+
const instance = new Portis(apiKey, networkName(networkId))
29+
const provider = instance.provider
3530

3631
const { BigNumber } = helpers
3732

src/modules/select/wallets/squarelink.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,17 @@ function squarelink(options: SdkWalletOptions): WalletModule {
2929
optional: true
3030
})
3131

32-
let instance: any
33-
let provider: any
34-
3532
return {
3633
name: 'Squarelink',
3734
svg: sqlkIcon,
3835
wallet: async (helpers: Helpers) => {
39-
if (!instance) {
40-
const { default: Squarelink } = await import('squarelink')
36+
const { default: Squarelink } = await import('squarelink')
4137

42-
instance = new Squarelink(apiKey, networkName(networkId), {
43-
useSync: true
44-
})
38+
const instance = new Squarelink(apiKey, networkName(networkId), {
39+
useSync: true
40+
})
4541

46-
provider = instance.getProviderSync()
47-
}
42+
const provider = instance.getProviderSync()
4843

4944
const { BigNumber } = helpers
5045

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,19 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
3030
optional: true
3131
})
3232

33-
let provider: any
34-
3533
return {
3634
name: 'WalletConnect',
3735
svg: walletConnectIcon,
3836
wallet: async () => {
39-
if (!provider) {
40-
const { default: WalletConnectProvider } = await import(
41-
'@walletconnect/web3-provider'
42-
)
37+
const { default: WalletConnectProvider } = await import(
38+
'@walletconnect/web3-provider'
39+
)
4340

44-
provider = new WalletConnectProvider({
45-
infuraId: infuraKey
46-
})
41+
const provider = new WalletConnectProvider({
42+
infuraId: infuraKey
43+
})
4744

48-
provider.autoRefreshOnNetworkChange = false
49-
}
45+
provider.autoRefreshOnNetworkChange = false
5046

5147
return {
5248
provider,
@@ -64,10 +60,6 @@ function walletConnect(options: WalletConnectOptions): WalletModule {
6460
})
6561
)
6662
}),
67-
disconnect: () => {
68-
provider.close()
69-
provider = undefined
70-
},
7163
address: {
7264
onChange: func => {
7365
provider

src/views/WalletSelect.svelte

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@
105105
}
106106
107107
async function handleWalletSelect(module: WalletModule) {
108+
const currentWalletInterface = get(walletInterface)
109+
110+
if (currentWalletInterface && currentWalletInterface.name === module.name) {
111+
finish({ completed: true })
112+
return
113+
}
114+
108115
loadingWallet = module.name
109116
110117
const {

0 commit comments

Comments
 (0)