Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/new-rules-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Allow smart accounts to switch chains between zk and non zk chains
10 changes: 1 addition & 9 deletions packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Chain } from "../../../chains/types.js";
import { getChainMetadata } from "../../../chains/utils.js";

export async function isZkSyncChain(chain: Chain) {
if (chain.id === 1337 || chain.id === 31337) {
Expand All @@ -22,12 +21,5 @@ export async function isZkSyncChain(chain: Chain) {
return true;
}

// fallback to checking the stack on rpc
try {
const chainMetadata = await getChainMetadata(chain);
return chainMetadata.stackType === "zksync_stack";
} catch {
// If the network check fails, assume it's not a ZkSync chain
return false;
}
return false;
}
32 changes: 19 additions & 13 deletions packages/thirdweb/src/wallets/smart/smart-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { trackConnect } from "../../analytics/track/connect.js";
import type { Chain } from "../../chains/types.js";
import { getCachedChainIfExists } from "../../chains/utils.js";
import { getContract } from "../../contract/contract.js";
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
import type { Account, Wallet } from "../interfaces/wallet.js";
import { createWalletEmitter } from "../wallet-emitter.js";
Expand Down Expand Up @@ -202,19 +203,24 @@ export function smartWallet(
if (!lastConnectOptions) {
throw new Error("Cannot switch chain without a previous connection");
}
// check if factory is deployed
const factory = getContract({
address:
createOptions.factoryAddress ||
getDefaultAccountFactory(createOptions.overrides?.entrypointAddress),
chain: newChain,
client: lastConnectOptions.client,
});
const isDeployed = await isContractDeployed(factory);
if (!isDeployed) {
throw new Error(
`Factory contract not deployed on chain: ${newChain.id}`,
);
const isZksyncChain = await isZkSyncChain(newChain);
if (!isZksyncChain) {
// check if factory is deployed
const factory = getContract({
address:
createOptions.factoryAddress ||
getDefaultAccountFactory(
createOptions.overrides?.entrypointAddress,
),
chain: newChain,
client: lastConnectOptions.client,
});
const isDeployed = await isContractDeployed(factory);
if (!isDeployed) {
throw new Error(
`Factory contract not deployed on chain: ${newChain.id}`,
);
}
}
const { connectSmartWallet } = await import("./index.js");
const [connectedAccount, connectedChain] = await connectSmartWallet(
Expand Down