Skip to content

Commit

Permalink
fix: bitcoin adapter switch network (#3639)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka authored Jan 14, 2025
1 parent 40ef5c7 commit 489de7c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
23 changes: 23 additions & 0 deletions .changeset/few-birds-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@reown/appkit-adapter-bitcoin': patch
'@reown/appkit-adapter-ethers': patch
'@reown/appkit-adapter-ethers5': patch
'@reown/appkit-adapter-solana': patch
'@reown/appkit-adapter-wagmi': patch
'@reown/appkit': patch
'@reown/appkit-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-ui': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Fix BitcoinAdapter `switchNetwork` function execution
5 changes: 0 additions & 5 deletions packages/adapters/bitcoin/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,6 @@ export class BitcoinAdapter extends AdapterBlueprint<BitcoinConnector> {
return walletConnectProvider as unknown as Provider
}

override switchNetwork(_params: AdapterBlueprint.SwitchNetworkParams): Promise<void> {
// Switch network
return Promise.resolve()
}

override async disconnect(params: AdapterBlueprint.DisconnectParams): Promise<void> {
if (params?.provider) {
await params.provider.disconnect()
Expand Down
39 changes: 36 additions & 3 deletions packages/adapters/bitcoin/tests/BitcoinAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { beforeEach, describe, it, vi, type Mock, expect } from 'vitest'
import { beforeEach, describe, it, vi, type Mock, expect, type MockedFunction } from 'vitest'
import { BitcoinAdapter, type BitcoinConnector } from '../src'
import type { BitcoinApi } from '../src/utils/BitcoinApi'
import { bitcoin, mainnet } from '@reown/appkit/networks'
import { bitcoin, bitcoinTestnet, mainnet } from '@reown/appkit/networks'
import { mockUTXO } from './mocks/mockUTXO'
import { SatsConnectConnector } from '../src/connectors/SatsConnectConnector'
import { mockSatsConnectProvider } from './mocks/mockSatsConnect'
Expand All @@ -10,6 +10,7 @@ import { OKXConnector } from '../src/connectors/OKXConnector'
import { LeatherConnector } from '../src/connectors/LeatherConnector'
import { WalletConnectProvider } from '../src/utils/WalletConnectProvider'
import { ConstantsUtil } from '@reown/appkit-common'
import { mockUniversalProvider } from './mocks/mockUniversalProvider'

function mockBitcoinApi(): { [K in keyof BitcoinApi.Interface]: Mock<BitcoinApi.Interface[K]> } {
return {
Expand Down Expand Up @@ -481,6 +482,39 @@ describe('BitcoinAdapter', () => {
})
})

describe('switchNetwork', () => {
it('should execute switch network', async () => {
const provider = new SatsConnectConnector({
provider: mockSatsConnectProvider().provider,
requestedChains: [bitcoin],
getActiveNetwork: () => bitcoin
})

await expect(
adapter.switchNetwork({
caipNetwork: bitcoinTestnet,
provider,
providerType: provider.type
})
).resolves.toBeUndefined()
})

it('should execute switch network for WalletConnectConnector', async () => {
const provider = mockUniversalProvider()
const setDefaultChainSpy = provider.setDefaultChain as MockedFunction<
typeof provider.setDefaultChain
>

await adapter.switchNetwork({
caipNetwork: bitcoinTestnet,
provider,
providerType: 'WALLET_CONNECT'
})

expect(setDefaultChainSpy).toHaveBeenCalledWith(bitcoinTestnet.caipNetworkId)
})
})

it('should not throw for not used methods', async () => {
expect(await adapter.getProfile({} as any)).toEqual({})
expect(await adapter.estimateGas({} as any)).toEqual({})
Expand All @@ -492,6 +526,5 @@ describe('BitcoinAdapter', () => {
expect(await adapter.grantPermissions({})).toEqual({})
expect(await adapter.getCapabilities({} as any)).toEqual({})
expect(await adapter.revokePermissions({} as any)).toEqual('0x')
await expect(adapter.switchNetwork({} as any)).resolves.toBeUndefined()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function mockUniversalProvider(
return {
disconnect: vi.fn(),
request: vi.fn(),
setDefaultChain: vi.fn(),
...replaces
} as UniversalProvider
}
Expand Down

0 comments on commit 489de7c

Please sign in to comment.