Skip to content

Commit bbe75b0

Browse files
committed
[SDK] Fix: Only request specified wallet connect chains (#4832)
Falls in line with the WC spec and appeases some picky wallets <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on fixing wallet connection issues in the `thirdweb` package, particularly for wallets with limited chain support. It improves handling of optional chains and modifies how chains are requested. ### Detailed summary - Updated handling of `optionalChains` to default to `[1]` if empty. - Changed `chains` to be `undefined` if `chainToRequest` is not provided. - Modified `setRequestedChainsIds` function to accept `chains` as `undefined`. - Simplified logic for `chainsToRequest` to use `undefined` when no chains are available. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent cefa4eb commit bbe75b0

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

.changeset/chilly-colts-beam.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+
Fixes WC connections with wallets that have limited chain support

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

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ export async function connectWC(
131131
? { pairingTopic: wcOptions?.pairingTopic }
132132
: {}),
133133
optionalChains: chainsToRequest,
134-
chains: chainToRequest
135-
? [chainToRequest.id]
136-
: chainsToRequest.length > 0
137-
? [chainsToRequest[0]]
138-
: [1],
134+
chains: chainToRequest ? [chainToRequest.id] : undefined,
139135
rpcMap: rpcMap,
140136
});
141137
}
@@ -265,12 +261,8 @@ async function initProvider(
265261
projectId: wcOptions?.projectId || DEFAULT_PROJECT_ID,
266262
optionalMethods: OPTIONAL_METHODS,
267263
optionalEvents: OPTIONAL_EVENTS,
268-
optionalChains: chainsToRequest,
269-
chains: chainToRequest
270-
? [chainToRequest.id]
271-
: chainsToRequest.length > 0
272-
? [chainsToRequest[0]]
273-
: [1],
264+
optionalChains: chainsToRequest || [1],
265+
chains: chainToRequest ? [chainToRequest.id] : undefined,
274266
metadata: {
275267
name: wcOptions?.appMetadata?.name || getDefaultAppMetadata().name,
276268
description:
@@ -509,7 +501,10 @@ async function switchChainWC(
509501
* Set the requested chains to the storage.
510502
* @internal
511503
*/
512-
function setRequestedChainsIds(chains: number[], storage: AsyncStorage) {
504+
function setRequestedChainsIds(
505+
chains: number[] | undefined,
506+
storage: AsyncStorage,
507+
) {
513508
storage?.setItem(storageKeys.requestedChains, JSON.stringify(chains));
514509
}
515510

@@ -550,13 +545,9 @@ function getChainsToRequest(options: {
550545
});
551546
}
552547

553-
const optionalChainIds = optionalChains.map((c) => c.id) || [];
554-
555-
const chainsToRequest: ArrayOneOrMore<number> = options.chain
556-
? [options.chain.id, ...optionalChainIds]
557-
: optionalChainIds.length > 0
558-
? (optionalChainIds as ArrayOneOrMore<number>)
559-
: [1];
548+
const chainsToRequest: ArrayOneOrMore<number> | undefined = options.chain
549+
? [options.chain.id]
550+
: undefined;
560551

561552
if (!options.chain && optionalChains.length === 0) {
562553
rpcMap[1] = getCachedChain(1).rpc;

0 commit comments

Comments
 (0)