From e91581aec63b77ba6e0242c308db5b5063cd0592 Mon Sep 17 00:00:00 2001 From: kumaryash90 Date: Thu, 3 Oct 2024 00:17:49 +0000 Subject: [PATCH] Update chain stack type check (#4901) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem solved Short description of the bug fixed or feature added --- ## PR-Codex overview This PR introduces a new property `stackType` across various components and utilities, enhancing the handling of blockchain network configurations and metadata. ### Detailed summary - Added `stackType` property in `types.ts` for chains. - Updated `utils.ts` to include `stackType` from data. - Included `stackType` in `embed-setup.tsx` and `ConfigureNetworkForm.tsx`. - Modified the `isZkSyncChain` function to use `getChainMetadata` for `stackType` validation. - Removed the obsolete `getChainStack` function. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` --- .../ConfigureNetworkForm.tsx | 4 +++ .../tabs/embed/components/embed-setup.tsx | 1 + packages/thirdweb/src/chains/types.ts | 1 + packages/thirdweb/src/chains/utils.ts | 1 + .../src/utils/any-evm/zksync/isZkSyncChain.ts | 30 +++++-------------- 5 files changed, 14 insertions(+), 23 deletions(-) diff --git a/apps/dashboard/src/components/configure-networks/ConfigureNetworkForm.tsx b/apps/dashboard/src/components/configure-networks/ConfigureNetworkForm.tsx index a5cbe33f45e..c1a5640f4c4 100644 --- a/apps/dashboard/src/components/configure-networks/ConfigureNetworkForm.tsx +++ b/apps/dashboard/src/components/configure-networks/ConfigureNetworkForm.tsx @@ -28,6 +28,7 @@ export type NetworkConfigFormData = { type: "testnet" | "mainnet"; icon: string; slug: string; + stackType?: string; }; // lowercase it, replace all spaces with hyphens, and then strip all non-alphanumeric characters @@ -71,6 +72,7 @@ export const ConfigureNetworkForm: React.FC = ({ type: editingChain?.testnet ? "testnet" : "mainnet", icon: editingChain?.icon?.url || "", slug: prefillSlug || editingChain?.slug || "", + stackType: "", }, mode: "onChange", }); @@ -146,6 +148,7 @@ export const ConfigureNetworkForm: React.FC = ({ format: "", }, testnet: data.type === "testnet", + stackType: data.stackType || "", }; } else { configuredNetwork = { @@ -170,6 +173,7 @@ export const ConfigureNetworkForm: React.FC = ({ format: "", } : undefined, + stackType: data.stackType || "", }; } diff --git a/apps/dashboard/src/contract-ui/tabs/embed/components/embed-setup.tsx b/apps/dashboard/src/contract-ui/tabs/embed/components/embed-setup.tsx index 04b613d7c3e..9f90151f381 100644 --- a/apps/dashboard/src/contract-ui/tabs/embed/components/embed-setup.tsx +++ b/apps/dashboard/src/contract-ui/tabs/embed/components/embed-setup.tsx @@ -243,6 +243,7 @@ export const EmbedSetup: React.FC = ({ shortName: "unknown", slug: "unknown", testnet: false, + stackType: "", }; const { register, watch } = useForm<{ diff --git a/packages/thirdweb/src/chains/types.ts b/packages/thirdweb/src/chains/types.ts index cb3cd41e24c..7a9298c92eb 100644 --- a/packages/thirdweb/src/chains/types.ts +++ b/packages/thirdweb/src/chains/types.ts @@ -82,6 +82,7 @@ export type ChainMetadata = { type: string; bridges?: Readonly>; }; + stackType: string; }; /** diff --git a/packages/thirdweb/src/chains/utils.ts b/packages/thirdweb/src/chains/utils.ts index 769e88eb96f..5ce4eefa70d 100644 --- a/packages/thirdweb/src/chains/utils.ts +++ b/packages/thirdweb/src/chains/utils.ts @@ -365,5 +365,6 @@ function createChainMetadata( url: e.url, standard: "EIP3091", })) || data?.explorers, + stackType: data?.stackType || "", }; } diff --git a/packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts b/packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts index 91a13b1f0c3..4b6901f7f39 100644 --- a/packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts +++ b/packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts @@ -1,5 +1,5 @@ import type { Chain } from "../../../chains/types.js"; -import { withCache } from "../../promise/withCache.js"; +import { getChainMetadata } from "../../../chains/utils.js"; export async function isZkSyncChain(chain: Chain) { if (chain.id === 1337 || chain.id === 31337) { @@ -12,36 +12,20 @@ export async function isZkSyncChain(chain: Chain) { chain.id === 300 || chain.id === 302 || chain.id === 11124 || - chain.id === 282 || // cronos zkevm testnet - chain.id === 388 // cronos zkevm mainnet + chain.id === 282 || + chain.id === 388 || + chain.id === 4654 || + chain.id === 333271 ) { return true; } // fallback to checking the stack on rpc try { - const stack = await getChainStack(chain.id); - return stack === "zksync-stack"; + 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; } } - -async function getChainStack(chainId: number): Promise { - return withCache( - async () => { - const res = await fetch(`https://${chainId}.rpc.thirdweb.com/stack`); - - if (!res.ok) { - res.body?.cancel(); - throw new Error(`Error fetching stack for ${chainId}`); - } - - const data = await res.json(); - - return data.stack; - }, - { cacheKey: `stack:${chainId}`, cacheTime: 24 * 60 * 60 * 1000 }, - ); -}