diff --git a/src/getChainId.ts b/src/getChainId.ts new file mode 100644 index 0000000..62e2169 --- /dev/null +++ b/src/getChainId.ts @@ -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; +}; diff --git a/src/index.ts b/src/index.ts index fd78853..e9b3d10 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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";