From 0af2b50d3d9ac4f169418b9b8ffdf4391741d8fa Mon Sep 17 00:00:00 2001 From: pucedoteth Date: Thu, 20 Aug 2026 01:15:34 +0200 Subject: [PATCH] fix(wagmi-adapter): parse stored active chain as JSON in getProvider `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/olive-moons-repeat.md | 7 +++++++ packages/wagmi-adapter/src/connector.ts | 5 ++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/olive-moons-repeat.md diff --git a/.changeset/olive-moons-repeat.md b/.changeset/olive-moons-repeat.md new file mode 100644 index 00000000000..847e92e7c7e --- /dev/null +++ b/.changeset/olive-moons-repeat.md @@ -0,0 +1,7 @@ +--- +"@thirdweb-dev/wagmi-adapter": patch +--- + +Fix `getProvider` reading the last active chain from storage + +`switchChain` persists the active chain under `thirdweb:active-chain` as a JSON-serialized `Chain` object, and `connect` reads it back with `JSON.parse(...)`. `getProvider` instead parsed the same value with `Number(...)`, which produces `NaN` for the stored object. Because `NaN` is falsy, `getProvider` silently fell back to chain `1` instead of the user's last active chain when auto-connecting. It now parses the value the same way `connect` does. diff --git a/packages/wagmi-adapter/src/connector.ts b/packages/wagmi-adapter/src/connector.ts index 7deccad84ff..1bfa16e9a07 100644 --- a/packages/wagmi-adapter/src/connector.ts +++ b/packages/wagmi-adapter/src/connector.ts @@ -218,7 +218,10 @@ export function inAppWalletConnector( }, getProvider: async (params) => { const lastChainIdStr = await rawStorage?.getItem(activeChainIdKey); - const lastChainId = lastChainIdStr ? Number(lastChainIdStr) : undefined; + const lastChain = lastChainIdStr + ? (JSON.parse(lastChainIdStr) as Chain) + : undefined; + const lastChainId = lastChain ? lastChain.id : undefined; const chain = defineChain( params?.chainId || args.smartAccount?.chain?.id || lastChainId || 1, );