Skip to content

Commit ae8f9a1

Browse files
working state
1 parent ecd14b0 commit ae8f9a1

File tree

4 files changed

+108
-68
lines changed

4 files changed

+108
-68
lines changed

apps/playground-web/src/components/styled-connect-embed.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ export function StyledConnectEmbed(
3636
) : (
3737
<ConnectEmbed
3838
chains={[
39+
base,
40+
ethereum,
41+
polygon,
42+
optimism,
43+
arbitrum,
3944
sepolia,
4045
baseSepolia,
4146
optimismSepolia,
4247
polygonAmoy,
4348
arbitrumSepolia,
4449
abstract,
45-
base,
46-
ethereum,
47-
polygon,
48-
optimism,
49-
arbitrum,
5050
]}
5151
client={THIRDWEB_CLIENT}
5252
theme={theme === "light" ? "light" : "dark"}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@ export function createWallet<const ID extends WalletId>(
399399
getConfig: () => args[1],
400400
id,
401401
subscribe: emitter.subscribe,
402-
switchChain: (c) => handleSwitchChain(c),
402+
switchChain: async (c) => {
403+
try {
404+
await handleSwitchChain(c);
405+
chain = c;
406+
} catch (e) {
407+
console.error("Error switching chain", e);
408+
}
409+
},
403410
};
404411
return wallet;
405412
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,14 @@ export function createWallet<const ID extends WalletId>(
230230
getConfig: () => args[1],
231231
id,
232232
subscribe: emitter.subscribe,
233-
switchChain: (c) => handleSwitchChain(c),
233+
switchChain: async (c) => {
234+
try {
235+
await handleSwitchChain(c);
236+
chain = c;
237+
} catch (e) {
238+
console.error("Error switching chain", e);
239+
}
240+
},
234241
};
235242
return wallet;
236243
}

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

Lines changed: 87 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -129,41 +129,47 @@ export async function connectWC(
129129
});
130130

131131
// For UniversalProvider, we need to connect with namespaces
132-
if (provider.session) {
133-
await provider.connect({
134-
...(wcOptions?.pairingTopic
135-
? { pairingTopic: wcOptions?.pairingTopic }
136-
: {}),
137-
namespaces: {
138-
[NAMESPACE]: {
139-
chains: chainsToRequest,
140-
events: ["chainChanged", "accountsChanged"],
141-
methods: [
142-
"eth_sendTransaction",
143-
"eth_signTransaction",
144-
"eth_sign",
145-
"personal_sign",
146-
"eth_signTypedData",
147-
],
148-
rpcMap,
149-
},
132+
await provider.connect({
133+
...(wcOptions?.pairingTopic
134+
? { pairingTopic: wcOptions?.pairingTopic }
135+
: {}),
136+
namespaces: {
137+
[NAMESPACE]: {
138+
chains: chainsToRequest,
139+
events: ["chainChanged", "accountsChanged"],
140+
methods: [
141+
"eth_sendTransaction",
142+
"eth_signTransaction",
143+
"eth_sign",
144+
"personal_sign",
145+
"eth_signTypedData",
146+
"wallet_switchEthereumChain",
147+
"wallet_addEthereumChain",
148+
],
149+
rpcMap,
150150
},
151-
});
152-
}
151+
},
152+
});
153153

154154
setRequestedChainsIds(
155155
chainsToRequest.map((x) => Number(x.split(":")[1])),
156156
storage,
157157
);
158158
// If session exists and chains are authorized, enable provider for required chain
159-
const addresses = await provider.enable();
160-
const address = addresses[0];
159+
const accounts: string[] = await provider.request(
160+
{
161+
method: "eth_requestAccounts",
162+
params: [],
163+
},
164+
`eip155:${chainToRequest?.id}`,
165+
);
166+
const address = accounts[0];
161167
if (!address) {
162168
throw new Error("No accounts found on provider.");
163169
}
164170

165171
// For UniversalProvider, get chainId from the session namespaces
166-
const currentChainId = chainsToRequest[0] || 1;
172+
const currentChainId = chainsToRequest[0]?.split(":")[1] || 1;
167173
const providerChainId = normalizeChainId(currentChainId);
168174

169175
const chain =
@@ -328,26 +334,31 @@ function createAccount({
328334
provider,
329335
address,
330336
client,
337+
chain,
331338
}: {
332339
provider: WCProvider;
333340
address: string;
334341
client: ThirdwebClient;
342+
chain: Chain;
335343
}) {
336344
const account: Account = {
337345
address: getAddress(address),
338346
async sendTransaction(tx: SendTransactionOption) {
339-
const transactionHash = (await provider.request({
340-
method: "eth_sendTransaction",
341-
params: [
342-
{
343-
data: tx.data,
344-
from: getAddress(address),
345-
gas: tx.gas ? numberToHex(tx.gas) : undefined,
346-
to: tx.to as Address,
347-
value: tx.value ? numberToHex(tx.value) : undefined,
348-
},
349-
],
350-
})) as Hex;
347+
const transactionHash = (await provider.request(
348+
{
349+
method: "eth_sendTransaction",
350+
params: [
351+
{
352+
data: tx.data,
353+
from: getAddress(address),
354+
gas: tx.gas ? numberToHex(tx.gas) : undefined,
355+
to: tx.to as Address,
356+
value: tx.value ? numberToHex(tx.value) : undefined,
357+
},
358+
],
359+
},
360+
`eip155:${tx.chainId}`,
361+
)) as Hex;
351362

352363
trackTransaction({
353364
chainId: tx.chainId,
@@ -373,10 +384,13 @@ function createAccount({
373384
}
374385
return message.raw;
375386
})();
376-
return provider.request({
377-
method: "personal_sign",
378-
params: [messageToSign, this.address],
379-
});
387+
return provider.request(
388+
{
389+
method: "personal_sign",
390+
params: [messageToSign, this.address],
391+
},
392+
`eip155:${chain.id}`,
393+
);
380394
},
381395
async signTypedData(_data) {
382396
const data = parseTypedData(_data);
@@ -399,10 +413,13 @@ function createAccount({
399413
types,
400414
});
401415

402-
return await provider.request({
403-
method: "eth_signTypedData_v4",
404-
params: [this.address, typedData],
405-
});
416+
return await provider.request(
417+
{
418+
method: "eth_signTypedData_v4",
419+
params: [this.address, typedData],
420+
},
421+
`eip155:${chain.id}`,
422+
);
406423
},
407424
};
408425

@@ -417,7 +434,7 @@ function onConnect(
417434
storage: AsyncStorage,
418435
client: ThirdwebClient,
419436
): [Account, Chain, DisconnectFn, SwitchChainFn] {
420-
const account = createAccount({ address, client, provider });
437+
const account = createAccount({ address, chain, client, provider });
421438

422439
async function disconnect() {
423440
provider.removeListener("accountsChanged", onAccountsChanged);
@@ -437,6 +454,7 @@ function onConnect(
437454
if (accounts[0]) {
438455
const newAccount = createAccount({
439456
address: getAddress(accounts[0]),
457+
chain,
440458
client,
441459
provider,
442460
});
@@ -491,6 +509,8 @@ async function switchChainWC(
491509
const namespaceMethods = getNamespaceMethods(provider);
492510
const isChainApproved = namespaceChains.includes(chainId);
493511

512+
provider.setDefaultChain(`eip155:${chainId}`);
513+
494514
if (!isChainApproved && namespaceMethods.includes(ADD_ETH_CHAIN_METHOD)) {
495515
const apiChain = await getChainMetadata(chain);
496516

@@ -501,27 +521,33 @@ async function switchChainWC(
501521
]),
502522
];
503523

504-
await provider.request({
505-
method: ADD_ETH_CHAIN_METHOD,
506-
params: [
507-
{
508-
blockExplorerUrls:
509-
blockExplorerUrls.length > 0 ? blockExplorerUrls : undefined,
510-
chainId: numberToHex(apiChain.chainId),
511-
chainName: apiChain.name,
512-
nativeCurrency: apiChain.nativeCurrency, // no clientId on purpose
513-
rpcUrls: getValidPublicRPCUrl(apiChain),
514-
},
515-
],
516-
});
524+
await provider.request(
525+
{
526+
method: ADD_ETH_CHAIN_METHOD,
527+
params: [
528+
{
529+
blockExplorerUrls:
530+
blockExplorerUrls.length > 0 ? blockExplorerUrls : undefined,
531+
chainId: numberToHex(apiChain.chainId),
532+
chainName: apiChain.name,
533+
nativeCurrency: apiChain.nativeCurrency, // no clientId on purpose
534+
rpcUrls: getValidPublicRPCUrl(apiChain),
535+
},
536+
],
537+
},
538+
`eip155:${chainId}`,
539+
);
517540
const requestedChains = await getRequestedChainsIds(storage);
518541
requestedChains.push(chainId);
519542
setRequestedChainsIds(requestedChains, storage);
520543
}
521-
await provider.request({
522-
method: "wallet_switchEthereumChain",
523-
params: [{ chainId: numberToHex(chainId) }],
524-
});
544+
await provider.request(
545+
{
546+
method: "wallet_switchEthereumChain",
547+
params: [{ chainId: numberToHex(chainId) }],
548+
},
549+
`eip155:${8453}`,
550+
);
525551
} catch (error) {
526552
const message =
527553
typeof error === "string" ? error : (error as ProviderRpcError)?.message;

0 commit comments

Comments
 (0)