Skip to content

Commit 6beec7a

Browse files
committed
Check zksync stack (#4869)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on updating the `isZkSyncChain` function to be asynchronous and modifying various parts of the codebase to await its result. This change enhances the handling of chain identification, particularly for zkSync-related operations. ### Detailed summary - Changed `isZkSyncChain` to an async function. - Updated calls to `isZkSyncChain` to use `await` in several files. - Modified the logic in `isZkSyncChain` to include additional checks for specific chain IDs. - Introduced a new async function `getChainStack` for fetching chain stack information. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 31f371d commit 6beec7a

File tree

7 files changed

+52
-16
lines changed

7 files changed

+52
-16
lines changed

apps/dashboard/src/components/contract-components/contract-deploy-form/custom-contract.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ export const CustomContractForm: React.FC<CustomContractFormProps> = ({
386386
throw new Error("no chain");
387387
}
388388

389-
const compilerType = isZkSyncChain(walletChain) ? "zksolc" : "solc";
389+
const compilerType = (await isZkSyncChain(walletChain))
390+
? "zksolc"
391+
: "solc";
390392

391393
let _contractURI = "";
392394

packages/thirdweb/src/contract/deployment/deploy-via-autofactory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function deployViaAutoFactory(
7373
salt,
7474
} = options;
7575

76-
if (isZkSyncChain(chain)) {
76+
if (await isZkSyncChain(chain)) {
7777
return zkDeployProxy({
7878
chain,
7979
client,

packages/thirdweb/src/contract/deployment/utils/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export async function getOrDeployInfraForPublishedContract(
4444
compilerType,
4545
} = args;
4646

47-
if (isZkSyncChain(chain)) {
47+
if (await isZkSyncChain(chain)) {
4848
const cloneFactoryContract = await zkDeployCreate2Factory({
4949
chain,
5050
client,

packages/thirdweb/src/extensions/prebuilts/deploy-published.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ async function directDeploy(options: {
267267
const { account, client, chain, compilerMetadata, contractParams, salt } =
268268
options;
269269

270-
if (isZkSyncChain(chain)) {
270+
if (await isZkSyncChain(chain)) {
271271
return zkDeployContract({
272272
account,
273273
client,

packages/thirdweb/src/extensions/prebuilts/get-required-transactions.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export async function getRequiredTransactions(
4343
modules = [],
4444
} = options;
4545

46+
const isZkSync = await isZkSyncChain(chain);
47+
4648
if (deployMetadata?.deployType === "autoFactory") {
4749
const results: (
4850
| DeployTransactionResult
@@ -53,7 +55,7 @@ export async function getRequiredTransactions(
5355
chain,
5456
client,
5557
}).then((c) =>
56-
c || isZkSyncChain(chain)
58+
c || isZkSync
5759
? null
5860
: ({ type: "infra", contractId: "Create2Factory" } as const),
5961
),
@@ -62,7 +64,7 @@ export async function getRequiredTransactions(
6264
client,
6365
contractId: "Forwarder",
6466
}).then((c) =>
65-
c || isZkSyncChain(chain)
67+
c || isZkSync
6668
? null
6769
: ({ type: "infra", contractId: "Forwarder" } as const),
6870
),
@@ -78,7 +80,7 @@ export async function getRequiredTransactions(
7880
}),
7981
},
8082
}).then((c) =>
81-
c || isZkSyncChain(chain)
83+
c || isZkSync
8284
? null
8385
: ({ type: "infra", contractId: "TWCloneFactory" } as const),
8486
),
Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
11
import type { Chain } from "../../../chains/types.js";
2+
import { withCache } from "../../promise/withCache.js";
23

3-
export function isZkSyncChain(chain: Chain) {
4-
return (
5-
chain.id === 324 ||
6-
chain.id === 300 ||
7-
chain.id === 302 ||
8-
chain.id === 11124 ||
9-
chain.id === 282 || // cronos zkevm testnet
10-
chain.id === 388 // cronos zkevm mainnet
4+
export async function isZkSyncChain(chain: Chain) {
5+
if (chain.id === 1337 || chain.id === 31337) {
6+
return false;
7+
}
8+
9+
const stack = await getChainStack(chain.id).catch(() => {
10+
// fall back to checking against these zksync chain-ids
11+
if (
12+
chain.id === 324 ||
13+
chain.id === 300 ||
14+
chain.id === 302 ||
15+
chain.id === 11124 ||
16+
chain.id === 282 || // cronos zkevm testnet
17+
chain.id === 388 // cronos zkevm mainnet
18+
) {
19+
return "zksync-stack";
20+
}
21+
22+
return "";
23+
});
24+
25+
return stack === "zksync-stack";
26+
}
27+
28+
async function getChainStack(chainId: number): Promise<string> {
29+
return withCache(
30+
async () => {
31+
const res = await fetch(`https://${chainId}.rpc.thirdweb.com/stack`);
32+
33+
if (!res.ok) {
34+
res.body?.cancel();
35+
throw new Error(`Error fetching stack for ${chainId}`);
36+
}
37+
38+
const data = await res.json();
39+
40+
return data.stack;
41+
},
42+
{ cacheKey: `stack:${chainId}`, cacheTime: 24 * 60 * 60 * 1000 },
1143
);
1244
}

packages/thirdweb/src/wallets/smart/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export async function connectSmartWallet(
104104
const sponsorGas =
105105
"gasless" in options ? options.gasless : options.sponsorGas;
106106

107-
if (isZkSyncChain(chain)) {
107+
if (await isZkSyncChain(chain)) {
108108
return [
109109
createZkSyncAccount({
110110
creationOptions,

0 commit comments

Comments
 (0)