Skip to content

Commit b14c460

Browse files
feat: enable smart account chain switching between zk/non-zk
1 parent 7791a99 commit b14c460

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

.changeset/new-rules-buy.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+
Allow smart accounts to switch chains between zk and non zk chains

packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Chain } from "../../../chains/types.js";
2-
import { getChainMetadata } from "../../../chains/utils.js";
32

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

25-
// fallback to checking the stack on rpc
26-
try {
27-
const chainMetadata = await getChainMetadata(chain);
28-
return chainMetadata.stackType === "zksync_stack";
29-
} catch {
30-
// If the network check fails, assume it's not a ZkSync chain
31-
return false;
32-
}
24+
return false;
3325
}

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { trackConnect } from "../../analytics/track/connect.js";
22
import type { Chain } from "../../chains/types.js";
33
import { getCachedChainIfExists } from "../../chains/utils.js";
44
import { getContract } from "../../contract/contract.js";
5+
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
56
import { isContractDeployed } from "../../utils/bytecode/is-contract-deployed.js";
67
import type { Account, Wallet } from "../interfaces/wallet.js";
78
import { createWalletEmitter } from "../wallet-emitter.js";
@@ -202,19 +203,24 @@ export function smartWallet(
202203
if (!lastConnectOptions) {
203204
throw new Error("Cannot switch chain without a previous connection");
204205
}
205-
// check if factory is deployed
206-
const factory = getContract({
207-
address:
208-
createOptions.factoryAddress ||
209-
getDefaultAccountFactory(createOptions.overrides?.entrypointAddress),
210-
chain: newChain,
211-
client: lastConnectOptions.client,
212-
});
213-
const isDeployed = await isContractDeployed(factory);
214-
if (!isDeployed) {
215-
throw new Error(
216-
`Factory contract not deployed on chain: ${newChain.id}`,
217-
);
206+
const isZksyncChain = await isZkSyncChain(newChain);
207+
if (!isZksyncChain) {
208+
// check if factory is deployed
209+
const factory = getContract({
210+
address:
211+
createOptions.factoryAddress ||
212+
getDefaultAccountFactory(
213+
createOptions.overrides?.entrypointAddress,
214+
),
215+
chain: newChain,
216+
client: lastConnectOptions.client,
217+
});
218+
const isDeployed = await isContractDeployed(factory);
219+
if (!isDeployed) {
220+
throw new Error(
221+
`Factory contract not deployed on chain: ${newChain.id}`,
222+
);
223+
}
218224
}
219225
const { connectSmartWallet } = await import("./index.js");
220226
const [connectedAccount, connectedChain] = await connectSmartWallet(

0 commit comments

Comments
 (0)