[SDK] Fix: wagmi-adapter getProvider ignores the persisted active chain - #8907
[SDK] Fix: wagmi-adapter getProvider ignores the persisted active chain#8907pucedoteth wants to merge 1 commit into
Conversation
`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 detectedLatest commit: 0af2b50 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
|
@pucedoteth is attempting to deploy a commit to the thirdweb Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review. WalkthroughThe adapter now parses the serialized active chain in ChangesWagmi provider chain selection
Estimated code review effort: 1 (Trivial) | ~5 minutes Merge Risk: ⚪ Minimal · up to 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)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
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. Comment |
Notes for the reviewer
packages/wagmi-adapter/src/connector.tsstores the active chain under one key,thirdweb:active-chain, but reads it back two different ways.Writer —
switchChain(line 274) stores a serializedChainobject:Reader 1 —
connect(line 124) parses it correctly:Reader 2 —
getProvider(line 220) parsed it as a number:Number('{"id":137,"rpc":"…"}')isNaN, andNaNis falsy, so the|| 1in the next line always won:Running the three code paths against the same stored value:
Impact: when
getProvideris the first thing to run after a page load (wagmi calls it beforeconnecton a coldgetProvider/getAccountspath), the persisted chain is discarded andautoConnectis handed chain1instead of the user's last active chain — even thoughconnecton the same storage value resolves it correctly. The two readers disagree about the same key.packages/thirdweb/src/wallets/manager/index.tsuses the identical key and the same JSON-object format (stringify(_chain)on write,JSON.parse(value) as ChainingetLastConnectedChain), so the object form is the intended encoding andgetProvideris the odd one out.The fix makes
getProviderread the value exactly the wayconnectdoes, 90 lines above it in the same file.How to test
pnpm lintinpackages/wagmi-adapteris clean apart from the 3 pre-existingnoExplicitAnywarnings that are also present onmain.tsc --project ./tsconfig.build.json --noEmitproduces the same output as on a clean checkout (onlyTS2307for the unbuilt workspacethirdwebpackage). The new expression is type-identical to the one already inconnect, andChainwas already imported.Summary by CodeRabbit