Skip to content
Open
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
30 changes: 28 additions & 2 deletions libs/wallet/src/updaters/WidgetStandaloneMode.updater.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useConnection } from 'wagmi'
import { ConnectorAlreadyConnectedError, useConnection } from 'wagmi'

import { isInjectedWidget } from '@cowprotocol/common-utils'
import { isInjectedWidget, logWallet } from '@cowprotocol/common-utils'
Comment thread
limitofzero marked this conversation as resolved.

import { ConnectorController, OptionsController } from '@reown/appkit-controllers'
import { render, RenderResult, waitFor } from '@testing-library/react'
Expand All @@ -15,10 +15,13 @@ import { useDisconnectWallet } from '../wagmi/hooks/useDisconnectWallet'

jest.mock('@cowprotocol/common-utils', () => ({
isInjectedWidget: jest.fn(),
logWallet: { error: jest.fn() },
normalizeError: (err: unknown) => (err instanceof Error ? err : new Error(String(err))),
}))

jest.mock('wagmi', () => ({
useConnection: jest.fn(),
ConnectorAlreadyConnectedError: class MockConnectorAlreadyConnectedError extends Error {},
}))

jest.mock('../utils/connectWalletById', () => ({
Expand Down Expand Up @@ -63,6 +66,7 @@ const wagmiAdapterSyncConnectionsMock = wagmiAdapter.syncConnections as jest.Moc
const wagmiAdapterSyncConnectorsMock = wagmiAdapter.syncConnectors as jest.Mock
const optionsControllerSetEIP6963EnabledMock = OptionsController.setEIP6963Enabled as jest.Mock
const connectorControllerSubscribeMock = ConnectorController.subscribe as jest.Mock
const logWalletErrorMock = logWallet.error as jest.Mock

const disconnectMock = jest.fn()

Expand Down Expand Up @@ -150,6 +154,28 @@ describe('WidgetStandaloneModeUpdater', () => {

expect(connectWalletByIdMock).toHaveBeenCalledTimes(1)
})

it('swallows ConnectorAlreadyConnectedError without logging (auto-reconnect or the bridge beat us to it)', async () => {
connectWalletByIdMock.mockRejectedValue(new ConnectorAlreadyConnectedError())

renderUpdater(DAPP_MODE)

await waitFor(() => {
expect(connectWalletByIdMock).toHaveBeenCalledTimes(1)
})

expect(logWalletErrorMock).not.toHaveBeenCalled()
})

it('logs unexpected connect errors', async () => {
connectWalletByIdMock.mockRejectedValue(new Error('provider unavailable'))

renderUpdater(DAPP_MODE)

await waitFor(() => {
expect(logWalletErrorMock).toHaveBeenCalledTimes(1)
})
})
})

describe('standalone mode: injected wallet discovery', () => {
Expand Down
17 changes: 14 additions & 3 deletions libs/wallet/src/updaters/WidgetStandaloneMode.updater.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useSetAtom } from 'jotai'
import { useEffect, useRef } from 'react'

import { useConnection } from 'wagmi'
import { ConnectorAlreadyConnectedError, useConnection } from 'wagmi'

import { isInjectedWidget } from '@cowprotocol/common-utils'
import { isInjectedWidget, logWallet, normalizeError } from '@cowprotocol/common-utils'

import { ConnectorController, OptionsController } from '@reown/appkit-controllers'

Expand Down Expand Up @@ -71,7 +71,18 @@ export function WidgetStandaloneModeUpdater({ standaloneMode }: WidgetStandalone
console.debug('[WidgetStandaloneModeUpdater] connect widget connector')

await reownAppKit.disconnect()
connectWalletById(COW_WIDGET_CONNECTOR_ID, 'injected')

try {
await connectWalletById(COW_WIDGET_CONNECTOR_ID, 'injected')
} catch (err: unknown) {
const error = normalizeError(err)

// Auto-reconnect or the bridged provider's own connect event can beat us to it -
// wagmi is already connected to this connector, nothing left to do.
if (error instanceof ConnectorAlreadyConnectedError) return

logWallet.error(new Error('Failed to connect widget connector', { cause: error }))
}
})()
}
}, [isDappMode, isSafeApp])
Expand Down
Loading