Skip to content

Commit 842baa0

Browse files
committed
[SDK] Fix: ensure optional chains are passed to wc (#4838)
<!-- start pr-codex --> ## PR-Codex overview This PR focuses on enhancing the `wallet-connect` functionality by ensuring that optional chains are correctly handled in the wallet connection request process. ### Detailed summary - Updated the destructuring of the return value from `getChainsToRequest` to include `requiredChain`. - Changed how `optionalChains` and `chains` are assigned in `connectWC` and `initProvider` functions. - Modified the return structure of `getChainsToRequest` to include `requiredChain` and `optionalChains`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 601958d commit 842baa0

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

.changeset/red-donkeys-teach.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+
Ensure optional chains are passed to wallet connect request

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ export async function connectWC(
119119
}
120120
}
121121

122-
const { rpcMap, chainsToRequest } = getChainsToRequest({
122+
const {
123+
rpcMap,
124+
requiredChain,
125+
optionalChains: chainsToRequest,
126+
} = getChainsToRequest({
123127
client: options.client,
124128
chain: chainToRequest,
125129
optionalChains: optionalChains,
@@ -131,7 +135,7 @@ export async function connectWC(
131135
? { pairingTopic: wcOptions?.pairingTopic }
132136
: {}),
133137
optionalChains: chainsToRequest,
134-
chains: chainToRequest ? [chainToRequest.id] : undefined,
138+
chains: requiredChain ? [requiredChain.id] : undefined,
135139
rpcMap: rpcMap,
136140
});
137141
}
@@ -245,7 +249,11 @@ async function initProvider(
245249
}
246250
}
247251

248-
const { rpcMap, chainsToRequest } = getChainsToRequest({
252+
const {
253+
rpcMap,
254+
requiredChain,
255+
optionalChains: chainsToRequest,
256+
} = getChainsToRequest({
249257
client: options.client,
250258
chain: chainToRequest,
251259
optionalChains: optionalChains,
@@ -261,8 +269,8 @@ async function initProvider(
261269
projectId: wcOptions?.projectId || DEFAULT_PROJECT_ID,
262270
optionalMethods: OPTIONAL_METHODS,
263271
optionalEvents: OPTIONAL_EVENTS,
264-
optionalChains: chainsToRequest || [1],
265-
chains: chainToRequest ? [chainToRequest.id] : undefined,
272+
optionalChains: chainsToRequest,
273+
chains: requiredChain ? [requiredChain.id] : undefined,
266274
metadata: {
267275
name: wcOptions?.appMetadata?.name || getDefaultAppMetadata().name,
268276
description:
@@ -525,7 +533,11 @@ function getChainsToRequest(options: {
525533
chain?: Chain;
526534
optionalChains?: Chain[];
527535
client: ThirdwebClient;
528-
}) {
536+
}): {
537+
rpcMap: Record<number, string>;
538+
requiredChain: Chain | undefined;
539+
optionalChains: ArrayOneOrMore<number>;
540+
} {
529541
const rpcMap: Record<number, string> = {};
530542

531543
if (options.chain) {
@@ -545,17 +557,17 @@ function getChainsToRequest(options: {
545557
});
546558
}
547559

548-
const chainsToRequest: ArrayOneOrMore<number> | undefined = options.chain
549-
? [options.chain.id]
550-
: undefined;
551-
552560
if (!options.chain && optionalChains.length === 0) {
553561
rpcMap[1] = getCachedChain(1).rpc;
554562
}
555563

556564
return {
557565
rpcMap,
558-
chainsToRequest,
566+
requiredChain: options.chain ? options.chain : undefined,
567+
optionalChains:
568+
optionalChains.length > 0
569+
? (optionalChains.map((x) => x.id) as ArrayOneOrMore<number>)
570+
: [1],
559571
};
560572
}
561573

0 commit comments

Comments
 (0)