Skip to content

Commit

Permalink
Update chain stack type check (#4901)
Browse files Browse the repository at this point in the history
## Problem solved

Short description of the bug fixed or feature added

<!-- start pr-codex -->

---

## 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}`

<!-- end pr-codex -->
  • Loading branch information
kumaryash90 committed Oct 3, 2024
1 parent 985c4b0 commit e91581a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,6 +72,7 @@ export const ConfigureNetworkForm: React.FC<NetworkConfigFormProps> = ({
type: editingChain?.testnet ? "testnet" : "mainnet",
icon: editingChain?.icon?.url || "",
slug: prefillSlug || editingChain?.slug || "",
stackType: "",
},
mode: "onChange",
});
Expand Down Expand Up @@ -146,6 +148,7 @@ export const ConfigureNetworkForm: React.FC<NetworkConfigFormProps> = ({
format: "",
},
testnet: data.type === "testnet",
stackType: data.stackType || "",
};
} else {
configuredNetwork = {
Expand All @@ -170,6 +173,7 @@ export const ConfigureNetworkForm: React.FC<NetworkConfigFormProps> = ({
format: "",
}
: undefined,
stackType: data.stackType || "",
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ export const EmbedSetup: React.FC<EmbedSetupProps> = ({
shortName: "unknown",
slug: "unknown",
testnet: false,
stackType: "",
};

const { register, watch } = useForm<{
Expand Down
1 change: 1 addition & 0 deletions packages/thirdweb/src/chains/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export type ChainMetadata = {
type: string;
bridges?: Readonly<Array<{ url: string }>>;
};
stackType: string;
};

/**
Expand Down
1 change: 1 addition & 0 deletions packages/thirdweb/src/chains/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,6 @@ function createChainMetadata(
url: e.url,
standard: "EIP3091",
})) || data?.explorers,
stackType: data?.stackType || "",
};
}
30 changes: 7 additions & 23 deletions packages/thirdweb/src/utils/any-evm/zksync/isZkSyncChain.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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<string> {
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 },
);
}

0 comments on commit e91581a

Please sign in to comment.