Skip to content

fix(wallet): guard undefined connector on reconnect to stop refresh crash - #7772

Merged
Danziger merged 3 commits into
cowprotocol:developfrom
tenderdeve:fix/7769-wallet-refresh-id-crash
Jul 10, 2026
Merged

Danziger merged 3 commits into
cowprotocol:developfrom
tenderdeve:fix/7769-wallet-refresh-id-crash

Conversation

@tenderdeve

@tenderdeve tenderdeve commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #7769

On refresh, a connected wallet (repro: Rabby on Polygon) disconnects and the console throws Cannot read properties of undefined (reading 'id').

The throw comes from @reown/appkit-adapter-wagmi's watchAccount onChange handler, which reads accountData.connector.id unguarded — even though the very next operand already uses prevAccountData.connector?.id:

const hasConnectorChanged = accountData.connector.id !== prevAccountData.connector?.id;

During reconnection rehydration, wagmi can briefly report status: 'connected' before the connector object is attached (the window is wider with a custom injected connector + EIP-6963 isolation and a non-mainnet chain). accountData.connector is then undefined, so .id throws. Because the throw happens inside the connected handler, reconnection never finalizes and the wallet ends up disconnected — both reported symptoms from one line.

Extended the existing patches/@reown__appkit-adapter-wagmi@1.8.19.patch to read accountData.connector?.id, matching the optional chaining already used on the previous value.

To Test

  1. Connect a wallet (e.g. Rabby) on a non-mainnet chain like Polygon.
  • Refresh the page — no reading 'id' error in the console.
  • The wallet stays connected across the refresh instead of dropping.

Background

The unguarded .id is upstream library code, so the fix is a dependency patch rather than an app change. I don't have an on-device Rabby/Polygon setup to reproduce the exact rehydration timing, so this targets the single unguarded dereference the stack points to; the asymmetric optional chaining on the same line is a clear upstream oversight.

Summary by CodeRabbit

  • Bug Fixes
    • Improved account change detection so wallet connection updates are handled more reliably.
    • Fixed native balance display/return values by ensuring a readable balance is always derived when needed.
    • Updated balance caching to use the same consistent formatted value shown to users.

@vercel

vercel Bot commented Jun 30, 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 30, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 561070f0-4444-4085-9948-a46c0d6572a9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Walkthrough

Patches the @reown/appkit-adapter-wagmi@1.8.19 package to guard connector-change detection with optional chaining and a presence check on accountData.connector, and adds a fallback for computing native balance formatted values via formatUnits when balance.formatted is unavailable.

Changes

Wagmi adapter patch update

Layer / File(s) Summary
Connector-change detection guard
patches/@reown__appkit-adapter-wagmi@1.8.19.patch
Compares connector IDs with optional chaining and only evaluates the connector-change condition when accountData.connector exists, preventing undefined property reads during account updates.
Native balance formatted fallback
patches/@reown__appkit-adapter-wagmi@1.8.19.patch
Derives formatted from balance.formatted or via formatUnits(balance.value, balance.decimals), used for both the cache update and resolved return payload.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

  • cowprotocol/cowswap#7509: Fixes the same transient accountData.connector undefined issue during connected-state hydration in the wagmi adapter.

Suggested reviewers: elena-zh, limitofzero, kernelwhisperer

Poem

A hop, a patch, a guarded chain,
No more errors to explain,
Balances formatted, safe and sound,
Connectors checked before they're found,
This bunny thumps in pure delight— 🐇
Refresh the page, connection's tight!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The native balance formatting change is unrelated to the refresh crash fix and appears out of scope. Remove the balance formatting tweak or split it into a separate PR unless it is required for #7769.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the refresh-time connector guard fix.
Description check ✅ Passed The description follows the template with Summary, To Test, and Background sections and links the issue.
Linked Issues check ✅ Passed The connector guard and gating address the refresh crash and disconnect reported in #7769.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ 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.

@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 30, 2026
@github-actions

github-actions Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Cloudflare Pages preview mirror

Preview branch URL: https://github.com/cowprotocol/cowswap/tree/cf-preview/pr-7772
Mirror PR: #7773
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/7769-wallet-refresh-id-crash
Approval target SHA: 3864536caaf0
Last mirrored SHA: 3f548fcbd2de
Last comment update: @Danziger at 2026-07-10T10:23:19.243Z

  • 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 30, 2026
@vercel
vercel Bot temporarily deployed to Preview – storybook June 30, 2026 13:46 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator June 30, 2026 13:47 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi June 30, 2026 13:48 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.

Hey @tenderdeve , thanks, but not fixed.

Image

@tenderdeve

Copy link
Copy Markdown
Contributor Author

@elena-zh updated in 8bdd9b9 — the previous connector?.id guard only fixed the comparison; the crash then moved into handleAccountChanged, which calls connector.getProvider() (the new Cannot read properties of undefined (reading 'getProvider') in your screenshot). On refresh wagmi briefly reports status connected before the connector attaches. Now the connected account is only finalized once accountData.connector is present; wagmi re-fires onChange when the connector attaches, so nothing is lost and the wallet stays connected.

@vercel
vercel Bot temporarily deployed to Preview – storybook July 1, 2026 11:49 Inactive
@vercel
vercel Bot temporarily deployed to Preview – widget-configurator July 1, 2026 11:49 Inactive
@vercel
vercel Bot temporarily deployed to Preview – cowfi July 1, 2026 11:50 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.

Looks good now, thanks

@elena-zh

elena-zh commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

@tenderdeve could you please rebase the PR to point to develop instead of main?

@elena-zh
elena-zh requested review from elena-zh and limitofzero July 1, 2026 13:05

@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.

See my comment above

@tenderdeve
tenderdeve changed the base branch from main to develop July 1, 2026 13:20
@tenderdeve

tenderdeve commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

See my comment above

@elena-zh fixed, now PR pointed to develop

@vercel

vercel Bot commented Jul 1, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
swap-dev Ready Ready Preview Jul 1, 2026 1:31pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – swap-dev July 1, 2026 13:31 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.

Thanks!

…rash

@reown/appkit-adapter-wagmi reads `accountData.connector.id` unguarded in the
`watchAccount` onChange handler, while the very next operand already uses
`prevAccountData.connector?.id`. During reconnection rehydration on page refresh
(e.g. Rabby on Polygon) wagmi can briefly report status 'connected' before the
connector object is attached, so `.id` throws 'Cannot read properties of
undefined (reading id)'. The throw aborts the connected handler, so reconnection
never finalizes and the wallet ends up disconnected.

Patch the dependency to read `accountData.connector?.id`, matching the existing
optional chaining on the prev value.
The connector?.id guard alone only fixed the comparison in the
watchAccount onChange handler; the crash then moved into
handleAccountChanged, which calls connector.getProvider() and reads
connector.id unguarded. On page refresh (e.g. Rabby on Polygon) wagmi
can report status 'connected' before the connector object is attached,
so getProvider() throws "Cannot read properties of undefined (reading
'getProvider')" and reconnection never finalizes, leaving the wallet
disconnected.

Only finalize the connected account when accountData.connector is
present. The connector-less 'connected' state is a transient rehydration
step; wagmi fires onChange again once the connector attaches (which
registers as a connector change), so no event is lost.
@tenderdeve
tenderdeve force-pushed the fix/7769-wallet-refresh-id-crash branch from 6f76a2a to 02249cf Compare July 2, 2026 06:12
@tenderdeve

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto develop — the branch had merged main, which pulled in the unrelated zeroApproval/USDT change (#7775) that isn't in develop yet, so it was showing up in this PR's diff. Dropped the merge commits; the diff is now just the reown adapter patch + its pnpm-lock hash. @Themoor1 that stray shouldZeroApprove.ts file is gone now. @elena-zh no change to the actual fix you approved.

@Danziger
Danziger enabled auto-merge (squash) July 10, 2026 10:25
@Danziger
Danziger merged commit 042991a into cowprotocol:develop Jul 10, 2026
11 of 19 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 10, 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.

Wallet is disconnected on refresh, Cannot read properties of undefined (reading 'id') error on console

4 participants