Skip to content

Commit

Permalink
feat: enable smart account chain switching between zk/non-zk (#5250)
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquim-verges authored Oct 31, 2024
1 parent ae217a3 commit f40d247
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
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

0 comments on commit f40d247

Please sign in to comment.