Skip to content

Commit 739a2ae

Browse files
[wagmi-adapter] fix: Improve autoconnect handling and storage (#6273)
1 parent 4158a79 commit 739a2ae

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

.changeset/red-eagles-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/wagmi-adapter": patch
3+
---
4+
5+
Better autoconnection handling

packages/thirdweb/src/wallets/connection/autoConnect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ import type { AutoConnectProps } from "./types.js";
2929
* @returns {boolean} a promise resolving to true or false depending on whether the auto connect function connected to a wallet or not
3030
* @walletConnection
3131
*/
32-
export const autoConnect = async (
32+
export async function autoConnect(
3333
props: AutoConnectProps & {
3434
wallets?: Wallet[];
3535
},
36-
) => {
36+
): Promise<boolean> {
3737
const wallets = props.wallets || getDefaultWallets(props);
3838
const manager = createConnectionManager(webLocalStorage);
3939
const result = await autoConnectCore({
@@ -56,4 +56,4 @@ export const autoConnect = async (
5656
manager,
5757
});
5858
return result;
59-
};
59+
}

packages/wagmi-adapter/src/connector.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ type Properties = {
3232
chainId: number;
3333
}>;
3434
};
35-
type StorageItem = { "tw.lastChainId": number };
35+
type StorageItem = {
36+
"thirdweb:lastChainId": number;
37+
};
38+
39+
const activeWalletIdKey = "thirdweb:active-wallet-id";
40+
const connectedWalletIdsKey = "thirdweb:connected-wallet-ids";
3641

3742
/**
3843
* Connect to an in-app wallet using the auth strategy of your choice.
@@ -92,7 +97,11 @@ export function inAppWalletConnector(
9297
name: "In-App wallet",
9398
type: "in-app",
9499
connect: async (params) => {
95-
const lastChainId = await config.storage?.getItem("tw.lastChainId");
100+
const rawStorage =
101+
typeof window !== "undefined" && window.localStorage
102+
? window.localStorage
103+
: undefined;
104+
const lastChainId = await config.storage?.getItem("thirdweb:lastChainId");
96105
if (params?.isReconnecting) {
97106
const { autoConnect } = await import("thirdweb/wallets");
98107
const chainId = lastChainId || args.smartAccount?.chain?.id || 1;
@@ -130,7 +139,10 @@ export function inAppWalletConnector(
130139
chain,
131140
} as InAppWalletConnectionOptions;
132141
const account = await wallet.connect(decoratedOptions);
133-
await config.storage?.setItem("tw.lastChainId", chain.id);
142+
// setting up raw local storage value for autoConnect
143+
rawStorage?.setItem(connectedWalletIdsKey, JSON.stringify([wallet.id]));
144+
rawStorage?.setItem(activeWalletIdKey, wallet.id);
145+
await config.storage?.setItem("thirdweb:lastChainId", chain.id);
134146
return { accounts: [getAddress(account.address)], chainId: chain.id };
135147
},
136148
disconnect: async () => {
@@ -147,7 +159,7 @@ export function inAppWalletConnector(
147159
return wallet.getChain()?.id || 1;
148160
},
149161
getProvider: async (params) => {
150-
const lastChainId = await config.storage?.getItem("tw.lastChainId");
162+
const lastChainId = await config.storage?.getItem("thirdweb:lastChainId");
151163
const chain = defineChain(
152164
params?.chainId || args.smartAccount?.chain?.id || lastChainId || 1,
153165
);
@@ -169,9 +181,13 @@ export function inAppWalletConnector(
169181
switchChain: async (params) => {
170182
const chain = config.chains.find((x) => x.id === params.chainId);
171183
if (!chain) {
172-
throw new Error(`Chain ${params.chainId} not supported`);
184+
throw new Error(`Chain ${params.chainId} not configured`);
173185
}
174186
await wallet.switchChain(defineChain(chain.id));
187+
config.emitter.emit("change", {
188+
chainId: chain.id,
189+
});
190+
await config.storage?.setItem("thirdweb:lastChainId", chain.id);
175191
return chain;
176192
},
177193
onAccountsChanged: () => {

0 commit comments

Comments
 (0)