Skip to content

Commit

Permalink
getChainId
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Dec 1, 2023
1 parent 40fc24f commit 5f81344
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/getChainId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import networks from "./networks";

export const getChainId = (chainNameOrAlias: string): number | null => {
if (networks.hasOwnProperty(chainNameOrAlias)) {
return networks[chainNameOrAlias as keyof typeof networks].chain_id;
}

// Iterate through networks to check aliases
for (const key in networks) {
if (networks.hasOwnProperty(key)) {
const chain: any = networks[key as keyof typeof networks];
if (
chain.chain_aliases &&
chain.chain_aliases.includes(chainNameOrAlias)
) {
return chain.chain_id;
}
}
}

return null;
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "./getEndpoints";
export * from "./getExplorers";
export * from "./getSupportedNetworks";
export * from "./getNetworkName";
export { default as networks } from "./networks";
export * from "./getChainId";

export { default as networks } from "./networks";
export { default as getHardhatConfigNetworks } from "./getHardhatConfigNetworks";

0 comments on commit 5f81344

Please sign in to comment.