Skip to content

fix(trade-state): preserve URL chainId on first load when wallet is connected - #7692

Merged
elena-zh merged 4 commits into
cowprotocol:developfrom
tenderdeve:fix/7647-eoa-wc-network-reset
Jun 23, 2026
Merged

elena-zh merged 4 commits into
cowprotocol:developfrom
tenderdeve:fix/7647-eoa-wc-network-reset

Conversation

@tenderdeve

@tenderdeve tenderdeve commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #7647.

When the app boots with a connected wallet and an explicit chainId in the URL, onProviderNetworkChanges() was:

  1. requesting the wallet to switch to the URL chain (switchNetworkInWallet(urlChainId, providerChainId)), then
  2. unconditionally calling navigateAndSwitchNetwork(providerChainId, getDefaultTradeRawState(providerChainId), null) right after.

Step 2 immediately rewrote the URL back to the provider's chain (and reset the tokens to the default state for that chain), so the URL chain set by the user was lost.

This is most visible with EOA + WalletConnect: the persisted WC session reports the wallet's own chain on reconnect. Steps from the bug report:

  1. Connect an EOA wallet via WalletConnect (wallet on chain A).
  2. Switch the network in the app to chain B (URL becomes #/<B>/swap).
  3. Refresh the page.

Before the fix: the URL flips back to chain A and the default token pair for chain A.
After the fix: the URL stays on chain B, the app re-issues the network switch to the wallet, and the trade state is preserved.

Changes

apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useSetupTradeState.ts: in onProviderNetworkChanges(), when the first-load branch fires switchNetworkInWallet(urlChainId, providerChainId), return early so we don't overwrite the URL with the provider's chain. Once the wallet completes (or rejects) the switch, the existing handlers take over:

  • on accept, the provider's chainId becomes the URL chainId and subsequent runs of this effect fall through the noNetworkChanges short-circuit;
  • on reject, switchNetworkInWallet already navigates back to the provider's chain (existing behavior at lines 68-73).

Testing steps

  1. Open the app.
  2. Connect an EOA wallet via WalletConnect with the wallet on, e.g., Ethereum mainnet.
  3. Change the network in the app (e.g. to Gnosis Chain or Arbitrum). URL becomes #/<chosen>/swap/....
  4. Refresh the page.
  5. Verify the chain in the app stays on the chosen network and the tokens in the URL are preserved.

Also verify nothing regressed:

  • Opening a deep link #/100/swap/WETH/COW with the wallet on a different chain still prompts the wallet to switch.
  • Rejecting the switch reverts the URL to the wallet's current chain (unchanged behavior).
  • Switching network from the wallet UI still updates the URL.
  • Opening the app at / (no chain in URL) with a connected wallet still falls through to the existing navigateAndSwitchNetwork(providerChainId, ...) path.

Checks

  • pnpm exec nx run cowswap-frontend:lint — passes
  • pnpm run typecheck — passes
  • pnpm exec nx run cowswap-frontend:test — passes apart from one pre-existing date-dependent flake in AffiliateTraderExpiryBanner.test.tsx that fails on develop too (unrelated).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed an issue where the wallet would not maintain the correct blockchain network when refreshing the page with a specific chain specified in the URL.

@vercel

vercel Bot commented Jun 22, 2026

Copy link
Copy Markdown

@tenderdeve is attempting to deploy a commit to the cow-dev Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 81a62430-b08a-4cf5-bd38-64e8a4fa2e0c

📥 Commits

Reviewing files that changed from the base of the PR and between d965091 and de0891d.

📒 Files selected for processing (1)
  • apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useSetupTradeState.ts

Walkthrough

In useSetupTradeState, the onProviderNetworkChanges first-load path now adds an early return after calling the wallet network switch when urlChainId is present and INITIAL_CHAIN_ID_FROM_URL is non-null, preventing the subsequent default navigation logic from executing and overwriting the chain state.

Changes

Fix: Early return on explicit URL chain switch

Layer / File(s) Summary
Early return after explicit URL chain switch
apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useSetupTradeState.ts
The explicit-urlChainId branch inside the isFirstLoad + connected-wallet block now returns immediately after invoking the network switch, preventing the fallback navigateAndSwitchNetwork call from resetting the chain on page refresh.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested reviewers

  • elena-zh
  • limitofzero

Poem

🐇 A refresh used to wipe my chain away,
But now a return saves the day!
One line stood firm and said "no more"—
The network stays just as before.
Hop hop, the bug is gone to stay! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main fix: preserving URL chainId on first load with a connected wallet, which directly matches the core change in the PR.
Description check ✅ Passed The PR description is comprehensive, covering summary, detailed explanation of the bug, the specific fix, testing steps, and verification checks. It aligns well with the template structure and provides all necessary context.
Linked Issues check ✅ Passed The PR successfully addresses issue #7647 by fixing the network reset behavior on page refresh. The changes ensure the URL chain ID is preserved when a wallet is connected, matching the expected behavior described in the issue.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the specific issue of URL chainId preservation on first load. The modifications are limited to the onProviderNetworkChanges() function and are directly related to the stated objectives.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

…onnected

When the app boots with a connected wallet and an explicit chainId
in the URL, the first-load handler called navigateAndSwitchNetwork
with the provider chainId right after requesting the wallet to
switch, which immediately rewrote the URL to the wallet's chain and
dropped the trade state.

This is most visible with EOA + WalletConnect: the persisted WC
session reports the wallet's own chain on reconnect, so refreshing
the page after switching network in the app resets the chain.
Returning early after switchNetworkInWallet keeps the URL chain and
lets the provider catch up.

Fixes cowprotocol#7647
@tenderdeve
tenderdeve force-pushed the fix/7647-eoa-wc-network-reset branch from 9d69ab5 to de0891d Compare June 22, 2026 11:43
@elena-zh elena-zh added the trigger-preview Add to a fork PR to trigger CF-pages preview. See https://github.com/cowprotocol/cowswap/pull/7615 label Jun 22, 2026
@github-actions

github-actions Bot commented Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Cloudflare Pages preview mirror

Preview branch URL: https://github.com/cowprotocol/cowswap/tree/cf-preview/pr-7692
Mirror PR: #7694
Cloudflare Pages preview links will be posted on the mirror PR by the Cloudflare Pages GitHub integration once builds complete.

Source fork branch: tenderdeve/cowswap:fix/7647-eoa-wc-network-reset
Approval target SHA: d492e8d6961d
Last mirrored SHA: 6899e7019d60
Last comment update: @elena-zh at 2026-06-23T15:22:31.726Z

  • Sync Cloudflare preview to approval target commit

@github-actions github-actions Bot removed the trigger-preview Add to a fork PR to trigger CF-pages preview. See https://github.com/cowprotocol/cowswap/pull/7615 label Jun 22, 2026
@elena-zh elena-zh added the trigger-preview Add to a fork PR to trigger CF-pages preview. See https://github.com/cowprotocol/cowswap/pull/7615 label Jun 22, 2026
@github-actions github-actions Bot removed the trigger-preview Add to a fork PR to trigger CF-pages preview. See https://github.com/cowprotocol/cowswap/pull/7615 label Jun 22, 2026
@elena-zh
elena-zh self-requested a review June 22, 2026 12:51
@vercel
vercel Bot temporarily deployed to Preview – storybook June 22, 2026 12:51 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator June 22, 2026 12:52 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi June 22, 2026 12:53 Inactive

@elena-zh elena-zh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you

@kernelwhisperer kernelwhisperer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@fairlighteth fairlighteth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ AI Review (Codex GPT-5, worked 5m): no new findings

Review completed. I found no new non-duplicate comments worth posting.

Review scope and related context

Checked:

  • apps/cowswap-frontend/src/modules/trade/hooks/setupTradeState/useSetupTradeState.ts
  • First-load connected-wallet path with explicit URL chainId
  • Existing accept/reject convergence through switchNetworkInWallet
  • Existing PR comments/reviews: no active review threads; CodeRabbit reported no actionable comments

No manual WalletConnect browser run was performed in this review.

Generated using the pr-review skill from the CoW Protocol skills repo.

@elena-zh elena-zh left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleted

@elena-zh
elena-zh enabled auto-merge (squash) June 23, 2026 15:25
@elena-zh
elena-zh merged commit ef110aa into cowprotocol:develop Jun 23, 2026
12 of 18 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EOA+WC: Network is reset on the page refresh

4 participants