Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .changeset/khaki-chairs-start.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
'@reown/appkit-controllers': patch
'@reown/appkit': patch
'pay-test-exchange': patch
'@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-utils': patch
'@reown/appkit-cdn': patch
'@reown/appkit-cli': patch
'@reown/appkit-codemod': patch
'@reown/appkit-common': patch
'@reown/appkit-core': patch
'@reown/appkit-experimental': patch
'@reown/appkit-pay': patch
'@reown/appkit-polyfills': patch
'@reown/appkit-scaffold-ui': patch
'@reown/appkit-siwe': patch
'@reown/appkit-siwx': patch
'@reown/appkit-testing': patch
'@reown/appkit-ui': patch
'@reown/appkit-universal-connector': patch
'@reown/appkit-wallet': patch
'@reown/appkit-wallet-button': patch
---

Fixed an issue where using the `open` function with send arguments and attempting to switch networks did not throw, causing the network state to become inconsistent
13 changes: 10 additions & 3 deletions packages/appkit/src/client/appkit-base-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ export interface OpenOptions<V extends Views | undefined = Views> {
: never
}

interface AppKitSwitchNetworkOptions {
throwOnFailure?: boolean
}

export abstract class AppKitBaseClient {
protected universalProvider?: UniversalProvider
protected connectionControllerClient?: ConnectionControllerClient
Expand Down Expand Up @@ -231,7 +235,7 @@ export abstract class AppKitBaseClient {
throw new Error(`openSend: caipNetwork with chainId ${args.chainId} not found`)
}

await this.switchNetwork(caipNetwork)
await this.switchNetwork(caipNetwork, { throwOnFailure: true })
}

try {
Expand Down Expand Up @@ -2159,14 +2163,17 @@ export abstract class AppKitBaseClient {
return ChainController.state.activeCaipNetwork?.id
}

public async switchNetwork(appKitNetwork: AppKitNetwork) {
public async switchNetwork(
appKitNetwork: AppKitNetwork,
{ throwOnFailure = false }: AppKitSwitchNetworkOptions = {}
) {
const network = this.getCaipNetworks().find(n => n.id === appKitNetwork.id)
if (!network) {
AlertController.open(ErrorUtil.ALERT_ERRORS.SWITCH_NETWORK_NOT_FOUND, 'error')

return
}
await ChainController.switchActiveNetwork(network)
await ChainController.switchActiveNetwork(network, { throwOnFailure })
}

public getWalletProvider() {
Expand Down
41 changes: 40 additions & 1 deletion packages/appkit/tests/client/appkit-base-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
import type { MockInstance } from 'vitest'

import { ConstantsUtil } from '@reown/appkit-common'
import type { ChainNamespace } from '@reown/appkit-common'
import type { CaipNetwork, ChainNamespace } from '@reown/appkit-common'
import {
AlertController,
ApiController,
Expand Down Expand Up @@ -444,4 +444,43 @@ describe('AppKitBaseClient.open', () => {

expect(result).toEqual({ hash: '0xabc123def456' })
})

it('it should throw an error if failed to switch network', async () => {
const mockArgs = {
assetAddress: '0x833589fcd6edb6e08f4c7c32d4f71b54bda02913',
amount: '1.0',
namespace: 'eip155' as ChainNamespace,
chainId: '1',
to: '0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6'
}
vi.spyOn(ChainController, 'state', 'get').mockReturnValue({
...ChainController.state,
activeChain: 'eip155',
activeCaipAddress: 'eip155:137:0x1234567890123456789012345678901234567890',
chains: new Map([
[
'eip155',
{
accountState: {
caipAddress: 'eip155:137:0x1234567890123456789012345678901234567890'
}
}
]
]) as unknown as Map<ChainNamespace, ChainAdapter>
})

vi.spyOn(baseClient as any, 'getCaipNetwork').mockReturnValue({ id: 137 } as CaipNetwork)
vi.spyOn(ChainController, 'getCaipNetworkById').mockReturnValue({
id: 1,
chainNamespace: 'eip155'
} as CaipNetwork)

vi.spyOn(ChainController, 'switchActiveNetwork').mockRejectedValue(
new Error('Failed to switch')
)

await expect(baseClient.open({ view: 'WalletSend', arguments: mockArgs })).rejects.toThrow(
'Failed to switch'
)
})
})
4 changes: 2 additions & 2 deletions packages/appkit/tests/client/public-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,9 +733,9 @@ describe('Base Public methods', () => {
await appKit.switchNetwork(mainnet)

expect(switchActiveNetwork).toHaveBeenCalledWith(
mainnet,
expect.objectContaining({
id: mainnet.id,
name: mainnet.name
throwOnFailure: false
})
)

Expand Down
13 changes: 12 additions & 1 deletion packages/controllers/src/controllers/ChainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export interface ChainControllerState {

type ChainControllerStateKey = keyof ChainControllerState

export interface SwitchActiveNetworkOptions {
throwOnFailure?: boolean
}

// -- State --------------------------------------------- //
const state = proxy<ChainControllerState>({
chains: proxyMap<ChainNamespace, ChainAdapter>(),
Expand Down Expand Up @@ -410,7 +414,10 @@ const controller = {
}
},

async switchActiveNetwork(network: CaipNetwork) {
async switchActiveNetwork(
network: CaipNetwork,
{ throwOnFailure = false }: SwitchActiveNetworkOptions = {}
) {
const namespace = ChainController.state.activeChain

if (!namespace) {
Expand All @@ -434,6 +441,10 @@ const controller = {
ModalController.close()
}
} catch (error) {
if (throwOnFailure) {
throw error
}

RouterController.goBack()
}

Expand Down