Skip to content

Commit cfff7e8

Browse files
MananTankjnsdls
andauthored
Fix custom chain not used in wallet connection (#2768)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent 948f155 commit cfff7e8

File tree

8 files changed

+77
-7
lines changed

8 files changed

+77
-7
lines changed

.changeset/tiny-cougars-crash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Fix custom chain not being used

packages/thirdweb/src/react/core/hooks/connection/useAutoConnect.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
// WalletWithPersonalAccount,
1111
} from "../../../../wallets/interfaces/wallet.js";
1212
import {
13+
getLastConnectedChain,
1314
getStoredActiveWalletId,
1415
getStoredConnectedWalletIds,
1516
} from "../../../../wallets/manager/index.js";
@@ -178,10 +179,13 @@ export function AutoConnect(props: AutoConnectProps) {
178179
return;
179180
}
180181

182+
const lastConnectedChain = await getLastConnectedChain(asyncLocalStorage);
183+
181184
async function handleWalletConnection(wallet: Wallet) {
182185
setConnectionStatus("connecting");
183186
return wallet.autoConnect({
184187
client: props.client,
188+
chain: lastConnectedChain ?? undefined,
185189
});
186190
}
187191

packages/thirdweb/src/wallets/coinbase/coinbaseSDKWallet.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,10 @@ export async function connectCoinbaseWalletSDK(
280280
})) as string | number;
281281

282282
const chainId = normalizeChainId(connectedChainId);
283-
let chain = defineChain(chainId);
283+
let chain =
284+
options.chain && options.chain.id === chainId
285+
? options.chain
286+
: defineChain(chainId);
284287
// Switch to chain if provided
285288
if (
286289
connectedChainId &&
@@ -318,7 +321,10 @@ export async function autoConnectCoinbaseWalletSDK(
318321
method: "eth_chainId",
319322
})) as string | number;
320323
const chainId = normalizeChainId(connectedChainId);
321-
const chain = defineChain(chainId);
324+
const chain =
325+
options.chain && options.chain.id === chainId
326+
? options.chain
327+
: defineChain(chainId);
322328

323329
return onConnect(address, chain, provider, emitter);
324330
}

packages/thirdweb/src/wallets/create-wallet.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { Account, Wallet } from "./interfaces/wallet.js";
88
import type {
99
CreateWalletArgs,
1010
InjectedConnectOptions,
11+
WalletAutoConnectionOption,
1112
WalletId,
1213
} from "./wallet-types.js";
1314

@@ -104,7 +105,11 @@ export function createWallet<const ID extends WalletId>(
104105
getConfig: () => args[1],
105106
getChain: () => chain,
106107
getAccount: () => account,
107-
autoConnect: async (options) => {
108+
autoConnect: async (
109+
options: WalletAutoConnectionOption<
110+
WCSupportedWalletIds | InjectedSupportedWalletIds
111+
>,
112+
) => {
108113
// injected wallet priority for autoConnect
109114
if (id !== "walletConnect" && injectedProvider(id)) {
110115
const { autoConnectInjectedWallet } = await import(
@@ -119,6 +124,7 @@ export function createWallet<const ID extends WalletId>(
119124
] = await autoConnectInjectedWallet(
120125
id as InjectedSupportedWalletIds,
121126
emitter,
127+
options.chain,
122128
);
123129
// set the states
124130
account = connectedAccount;

packages/thirdweb/src/wallets/injected/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export async function connectInjectedWallet(
6363
.request({ method: "eth_chainId" })
6464
.then(normalizeChainId);
6565

66-
let connectedChain = defineChain(chainId);
66+
let connectedChain =
67+
options.chain && options.chain.id === chainId
68+
? options.chain
69+
: defineChain(chainId);
6770

6871
// if we want a specific chainId and it is not the same as the provider chainId, trigger switchChain
6972
if (options.chain && options.chain.id !== chainId) {
@@ -80,6 +83,7 @@ export async function connectInjectedWallet(
8083
export async function autoConnectInjectedWallet(
8184
id: InjectedSupportedWalletIds,
8285
emitter: WalletEmitter<InjectedSupportedWalletIds>,
86+
chain?: Chain,
8387
): Promise<ReturnType<typeof onConnect>> {
8488
const provider = getInjectedProvider(id);
8589

@@ -101,7 +105,8 @@ export async function autoConnectInjectedWallet(
101105
.request({ method: "eth_chainId" })
102106
.then(normalizeChainId);
103107

104-
const connectedChain = defineChain(chainId);
108+
const connectedChain =
109+
chain && chain.id === chainId ? chain : defineChain(chainId);
105110

106111
return onConnect(provider, address, connectedChain, emitter);
107112
}

packages/thirdweb/src/wallets/manager/index.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type ConnectionStatus = "connected" | "disconnected" | "connecting";
1111

1212
const CONNECTED_WALLET_IDS = "thirdweb:connected-wallet-ids";
1313
const ACTIVE_WALLET_ID = "thirdweb:active-wallet-id";
14+
const LAST_ACTIVE_CHAIN = "thirdweb:active-chain";
1415

1516
export type ConnectionManager = ReturnType<typeof createConnectionManager>;
1617

@@ -126,6 +127,19 @@ export function createConnectionManager(storage: AsyncStorage) {
126127

127128
// side effects
128129

130+
effect(
131+
() => {
132+
const _chain = activeWalletChainStore.getValue();
133+
if (_chain) {
134+
storage.setItem(LAST_ACTIVE_CHAIN, JSON.stringify(_chain));
135+
} else {
136+
storage.removeItem(LAST_ACTIVE_CHAIN);
137+
}
138+
},
139+
[activeWalletChainStore],
140+
false,
141+
);
142+
129143
// save last connected wallet ids to storage
130144
effect(
131145
() => {
@@ -218,3 +232,21 @@ export async function getStoredActiveWalletId(
218232
return null;
219233
}
220234
}
235+
236+
/**
237+
* @internal
238+
*/
239+
export async function getLastConnectedChain(
240+
storage: AsyncStorage,
241+
): Promise<Chain | null> {
242+
try {
243+
const value = await storage.getItem(LAST_ACTIVE_CHAIN);
244+
if (value) {
245+
return JSON.parse(value) as Chain;
246+
}
247+
248+
return null;
249+
} catch {
250+
return null;
251+
}
252+
}

packages/thirdweb/src/wallets/wallet-connect/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ export async function connectWC(
102102
throw new Error("No accounts found on provider.");
103103
}
104104

105-
const chain = defineChain(normalizeChainId(provider.chainId));
105+
const providerChainId = normalizeChainId(provider.chainId);
106+
107+
const chain =
108+
options.chain && options.chain.id === providerChainId
109+
? options.chain
110+
: defineChain(providerChainId);
106111

107112
if (options) {
108113
const savedParams: SavedConnectParams = {
@@ -160,7 +165,12 @@ export async function autoConnectWC(
160165
throw new Error("No accounts found on provider.");
161166
}
162167

163-
const chain = defineChain(normalizeChainId(provider.chainId));
168+
const providerChainId = normalizeChainId(provider.chainId);
169+
170+
const chain =
171+
options.chain && options.chain.id === providerChainId
172+
? options.chain
173+
: defineChain(providerChainId);
164174

165175
return onConnect(address, chain, provider, emitter);
166176
}

packages/thirdweb/src/wallets/wallet-connect/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ export type WCAutoConnectOptions = {
179179
client: ThirdwebClient;
180180

181181
savedConnectParams?: SavedConnectParams;
182+
183+
chain?: Chain;
182184
};
183185

184186
type SavedConnectParams = {

0 commit comments

Comments
 (0)