Skip to content

fix: handle ethers.js wrapped 4902 error for Base Sepolia chain switch#25

Merged
dzianisv merged 1 commit intomainfrom
fix/sepolia-chain-switch
Feb 16, 2026
Merged

fix: handle ethers.js wrapped 4902 error for Base Sepolia chain switch#25
dzianisv merged 1 commit intomainfrom
fix/sepolia-chain-switch

Conversation

@dzianisv
Copy link
Copy Markdown
Contributor

Summary

  • Fix Base Sepolia chain switch failing because ethers.js v6 wraps the raw provider 4902 error code inside UNKNOWN_ERROR with data.originalError.code
  • Add wallet_addEthereumChain fallback so the wallet automatically adds Base Sepolia when missing
  • Hide testnet chains by default in the payment UI; show only when ?test=true is set

Changes

web/src/lib/wallets/evm.ts

  • New isChainNotAddedError() helper that checks three patterns: raw code === 4902, ethers-wrapped data.originalError.code === 4902, and message string fallback
  • On 4902, call wallet_addEthereumChain with chain params instead of throwing

web/src/lib/config.ts

  • Added EVM_CHAIN_PARAMS map with full Base Sepolia config (RPC URL, block explorer, native currency)

web/src/app/pay/page.tsx

  • Parse ?test=true from URL; filter chains to hide testnet: true entries by default

Tests

  • New: web/src/__tests__/lib/evm.test.ts — 9 tests covering isEvmAvailable, raw 4902, ethers-wrapped 4902, message-based fallback, missing chain params, non-4902 re-throw, no-provider error
  • Updated: page.test.tsx — testnet visibility assertions (hidden by default, shown with ?test=true)

Test Results

  • 146 tests passing (81 web + 65 API)
  • Build passes cleanly

- Add isChainNotAddedError() helper to detect 4902 in raw, wrapped, and message forms
- On 4902, call wallet_addEthereumChain with full chain params instead of throwing
- Add EVM_CHAIN_PARAMS config for Base Sepolia (RPC, explorer, native currency)
- Hide testnet chains by default; show only when ?test=true is set
- Add 9 new evm.test.ts tests covering all 4902 detection patterns
- Update page.test.tsx to verify testnet visibility logic
@dzianisv dzianisv merged commit 8f5d902 into main Feb 16, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant