Skip to content

Feature Request: Add optional timeout parameter to switchChain #5045

@Zelys-DFKH

Description

@Zelys-DFKH

Problem

switchChain can hang indefinitely on mobile wallets over WalletConnect when:

  • The wallet app is backgrounded
  • The user ignores the chain switch prompt
  • The request is dropped mid-flight

The promise never rejects, leaving users stuck with no error feedback.

In production, we work around this with Promise.race() + manual 30-second timeouts, but this forces every consumer to re-implement the same fragile workaround.

Proposed Solution

Add an optional timeout parameter (milliseconds) to switchChain. If the request doesn't resolve within that window, the promise rejects with a clear SwitchChainTimeoutError.

const { switchChainAsync } = useSwitchChain();

try {
  await switchChainAsync({ 
    chainId: 137, 
    timeout: 30_000  // 30 second timeout
  });
} catch (error) {
  if (error.name === 'SwitchChainTimeoutError') {
    // Handle timeout — wallet backgrounded or request dropped
  }
}

Implementation Notes

Following waitForTransactionReceipt precedent:

  • Optional per-call parameter in SwitchChainParameters
  • setTimeout() + Promise.race() for timeout implementation
  • New SwitchChainTimeoutError extending BaseError
  • No default timeout (let consumers decide)

Use Case

DeFi apps need reliable chain switching before transaction submission. A timeout parameter eliminates the need for workarounds and gives developers control over their UX when wallets become unresponsive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions