Skip to content

Commit

Permalink
fix: switching networks should not disconnect wallets (#198)
Browse files Browse the repository at this point in the history
* fix: switching networks should not disconnect wallets

* test(core): update WalletManager tests

* test(vue): fix failing test
  • Loading branch information
drichar authored Jul 2, 2024
1 parent 554f952 commit 47e5704
Show file tree
Hide file tree
Showing 6 changed files with 3 additions and 35 deletions.
2 changes: 0 additions & 2 deletions packages/use-wallet-react/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export const useWallet = () => {
if (networkId === activeNetwork) {
return
}
// Disconnect any connected wallets
await manager.disconnect()

console.info(`[React] Creating Algodv2 client for ${networkId}...`)

Expand Down
2 changes: 0 additions & 2 deletions packages/use-wallet-solid/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ export function useWallet() {
if (activeNetwork() === networkId) {
return
}
// Disconnect any connected wallets
await manager().disconnect()

console.info(`[Solid] Creating Algodv2 client for ${networkId}...`)

Expand Down
4 changes: 3 additions & 1 deletion packages/use-wallet-vue/src/__tests__/useWallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ describe('useWallet', () => {
const { setActiveNetwork, algodClient } = useWallet()

const newNetwork = NetworkId.MAINNET
const newAlgodClient = new algosdk.Algodv2('mock-token', 'https://mock-server', '')

// Default mainnet algod config
const newAlgodClient = new algosdk.Algodv2('', 'https://mainnet-api.algonode.cloud/', '')

mockWalletManager.setActiveNetwork = async (networkId: NetworkId) => {
mockSetAlgodClient(newAlgodClient)
Expand Down
2 changes: 0 additions & 2 deletions packages/use-wallet-vue/src/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ export function useWallet() {
if (networkId === activeNetwork.value) {
return
}
// Disconnect any connected wallets
await manager.disconnect()

console.info(`[Vue] Creating Algodv2 client for ${networkId}...`)

Expand Down
26 changes: 0 additions & 26 deletions packages/use-wallet/src/__tests__/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,36 +207,10 @@ describe('WalletManager', () => {
[WalletId.KIBISIS, mockKibisisWallet]
])

// Mock isConnected to return true
vi.spyOn(mockDeflyWallet, 'isConnected', 'get').mockReturnValue(true)
vi.spyOn(mockKibisisWallet, 'isConnected', 'get').mockReturnValue(true)

const disconnectMock = vi.spyOn(manager, 'disconnect')

await manager.setActiveNetwork(NetworkId.MAINNET)

expect(disconnectMock).toHaveBeenCalled()
expect(manager.activeNetwork).toBe(NetworkId.MAINNET)
})

it('does not call disconnect if the network is already active', async () => {
const manager = new WalletManager({
wallets: [WalletId.DEFLY, WalletId.KIBISIS]
})
manager._clients = new Map<WalletId, BaseWallet>([
[WalletId.DEFLY, mockDeflyWallet],
[WalletId.KIBISIS, mockKibisisWallet]
])

const disconnectMock = vi.spyOn(manager, 'disconnect')

// Set initial active network
manager.store.setState((state) => ({ ...state, activeNetwork: NetworkId.MAINNET }))

await manager.setActiveNetwork(NetworkId.MAINNET)

expect(disconnectMock).not.toHaveBeenCalled()
})
})

describe('subscribe', () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/use-wallet/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ export class WalletManager {
if (this.activeNetwork === networkId) {
return
}
// Disconnect any connected wallets
await this.disconnect()

setActiveNetwork(this.store, { networkId })
this.algodClient = this.createAlgodClient(this.networkConfig[networkId])
Expand Down

0 comments on commit 47e5704

Please sign in to comment.