Skip to content

[SDK] Fix: wagmi-adapter getProvider ignores the persisted active chain - #8907

Open
pucedoteth wants to merge 1 commit into
thirdweb-dev:mainfrom
pucedoteth:fix-wagmi-adapter-last-chain
Open

[SDK] Fix: wagmi-adapter getProvider ignores the persisted active chain#8907
pucedoteth wants to merge 1 commit into
thirdweb-dev:mainfrom
pucedoteth:fix-wagmi-adapter-last-chain

Conversation

@pucedoteth

@pucedoteth pucedoteth commented Aug 19, 2026

Copy link
Copy Markdown

Notes for the reviewer

packages/wagmi-adapter/src/connector.ts stores the active chain under one key, thirdweb:active-chain, but reads it back two different ways.

WriterswitchChain (line 274) stores a serialized Chain object:

rawStorage?.setItem(activeChainIdKey, JSON.stringify(defineChain(chain.id)));

Reader 1connect (line 124) parses it correctly:

const lastChain = lastChainIdStr ? (JSON.parse(lastChainIdStr) as Chain) : undefined;
const lastChainId = lastChain ? lastChain.id : undefined;

Reader 2getProvider (line 220) parsed it as a number:

const lastChainId = lastChainIdStr ? Number(lastChainIdStr) : undefined;

Number('{"id":137,"rpc":"…"}') is NaN, and NaN is falsy, so the || 1 in the next line always won:

const chain = defineChain(
  params?.chainId || args.smartAccount?.chain?.id || lastChainId || 1,
);

Running the three code paths against the same stored value:

stored value  : {"id":137,"rpc":"https://137.rpc.thirdweb.com"}
connect()     : 137
getProvider() : NaN -> falsy? true => defineChain( 1 )
fixed         : 137 => defineChain( 137 )

Impact: when getProvider is the first thing to run after a page load (wagmi calls it before connect on a cold getProvider/getAccounts path), the persisted chain is discarded and autoConnect is handed chain 1 instead of the user's last active chain — even though connect on the same storage value resolves it correctly. The two readers disagree about the same key.

packages/thirdweb/src/wallets/manager/index.ts uses the identical key and the same JSON-object format (stringify(_chain) on write, JSON.parse(value) as Chain in getLastConnectedChain), so the object form is the intended encoding and getProvider is the odd one out.

The fix makes getProvider read the value exactly the way connect does, 90 lines above it in the same file.

How to test

  • pnpm lint in packages/wagmi-adapter is clean apart from the 3 pre-existing noExplicitAny warnings that are also present on main.
  • tsc --project ./tsconfig.build.json --noEmit produces the same output as on a clean checkout (only TS2307 for the unbuilt workspace thirdweb package). The new expression is type-identical to the one already in connect, and Chain was already imported.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed automatic connection behavior so the app consistently restores the previously active blockchain network.
    • Prevented unintended fallback to the default network when restoring the active chain.

`switchChain` persists the active chain under `thirdweb:active-chain` as
`JSON.stringify(defineChain(chain.id))`, and `connect` reads it back with
`JSON.parse(...) as Chain`. `getProvider` instead read the same value with
`Number(...)`, which evaluates to `NaN` for the serialized object. `NaN` is
falsy, so the `|| 1` fallback always won and `autoConnect` was handed chain 1
instead of the user's last active chain.

Read the value the same way `connect` does.
@changeset-bot

changeset-bot Bot commented Aug 19, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0af2b50

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@thirdweb-dev/wagmi-adapter Patch
wagmi-inapp Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

@pucedoteth is attempting to deploy a commit to the thirdweb Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 19, 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: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2e4745a5-5022-45a4-807b-0c792a487940

📥 Commits

Reviewing files that changed from the base of the PR and between 07fcec0 and 0af2b50.

📒 Files selected for processing (2)
  • .changeset/olive-moons-repeat.md
  • packages/wagmi-adapter/src/connector.ts

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


Walkthrough

The adapter now parses the serialized active chain in getProvider and uses its chain ID. A patch changeset documents the fix.

Changes

Wagmi provider chain selection

Layer / File(s) Summary
Parse serialized active-chain data
packages/wagmi-adapter/src/connector.ts, .changeset/olive-moons-repeat.md
getProvider parses the stored active chain as Chain data and uses its id. The changeset documents the patch and the corrected fallback behavior.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Merge Risk: ⚪ Minimal · up to 0af2b

This change makes provider initialization honor the user’s persisted active chain instead of falling back to chain 1. The fix is localized and merge-ready after normal checks, with no actionable merge-blocking risk remaining.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly and concisely identifies the SDK fix for getProvider ignoring the persisted active chain.
Description check ✅ Passed The description explains the root cause, impact, implementation, and testing results, and includes the required reviewer notes and testing sections.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant