/**
* Network Configuration for Etherscan v2 API
*
* This module provides comprehensive network configuration supporting all chains
* available in the Etherscan v2 API. The v2 API uses a unified base URL with
* a `chainid` parameter instead of separate URLs per network.
*/
/**
* Configuration for a blockchain network
*/
export interface NetworkConfig {
/** Chain ID as defined in EIP-155 */
chainId: number;
/** Full name of the network */
name: string;
/** Short identifier used as slug (e.g., 'eth', 'arb', 'op') */
shortName: string;
/** Whether this is a testnet */
isTestnet: boolean;
/** Whether this is a Layer 2 network */
isL2: boolean;
/** Native currency information */
nativeCurrency: {
/** Name of the native currency */
name: string;
/** Symbol of the native currency */
symbol: string;
/** Number of decimals (typically 18) */
decimals: number;
};
}
/**
* Base URL for Etherscan v2 API
* All networks use this unified endpoint with a chainid parameter
*/
export const V2_API_BASE_URL = 'https://api.etherscan.io/v2/api';
/**
* Comprehensive network configuration mapping
* Key: network slug (used for backward compatibility and API calls)
* Value: NetworkConfig object
*/
export const NETWORKS: Record<string, NetworkConfig> = {
// Ethereum Networks
'mainnet': {
chainId: 1,
name: 'Ethereum Mainnet',
shortName: 'eth',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'ethereum': {
chainId: 1,
name: 'Ethereum Mainnet',
shortName: 'eth',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'sepolia': {
chainId: 11155111,
name: 'Sepolia Testnet',
shortName: 'sep',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
'holesky': {
chainId: 17000,
name: 'Holesky Testnet',
shortName: 'holesky',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Holesky Ether',
symbol: 'ETH',
decimals: 18
}
},
'hoodi': {
chainId: 560048,
name: 'Hoodi Testnet',
shortName: 'hoodi',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Hoodi Ether',
symbol: 'ETH',
decimals: 18
}
},
// Abstract
'abstract': {
chainId: 2741,
name: 'Abstract Mainnet',
shortName: 'abstract',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'abstract-sepolia': {
chainId: 11124,
name: 'Abstract Sepolia Testnet',
shortName: 'abstract-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// ApeChain
'apechain': {
chainId: 33139,
name: 'ApeChain Mainnet',
shortName: 'ape',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'ApeCoin',
symbol: 'APE',
decimals: 18
}
},
'apechain-curtis': {
chainId: 33111,
name: 'ApeChain Curtis Testnet',
shortName: 'ape-curtis',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'ApeCoin',
symbol: 'APE',
decimals: 18
}
},
// Arbitrum
'arbitrum': {
chainId: 42161,
name: 'Arbitrum One Mainnet',
shortName: 'arb',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'arbitrum-nova': {
chainId: 42170,
name: 'Arbitrum Nova Mainnet',
shortName: 'arb-nova',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'arbitrum-sepolia': {
chainId: 421614,
name: 'Arbitrum Sepolia Testnet',
shortName: 'arb-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// Avalanche
'avalanche': {
chainId: 43114,
name: 'Avalanche C-Chain',
shortName: 'avax',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18
}
},
'avalanche-fuji': {
chainId: 43113,
name: 'Avalanche Fuji Testnet',
shortName: 'avax-fuji',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18
}
},
// Base
'base': {
chainId: 8453,
name: 'Base Mainnet',
shortName: 'base',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'base-sepolia': {
chainId: 84532,
name: 'Base Sepolia Testnet',
shortName: 'base-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// Berachain
'berachain': {
chainId: 80094,
name: 'Berachain Mainnet',
shortName: 'bera',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Bera',
symbol: 'BERA',
decimals: 18
}
},
'berachain-bepolia': {
chainId: 80069,
name: 'Berachain Bepolia Testnet',
shortName: 'bera-bepolia',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Bera',
symbol: 'BERA',
decimals: 18
}
},
// BitTorrent Chain
'bittorrent': {
chainId: 199,
name: 'BitTorrent Chain Mainnet',
shortName: 'btt',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'BitTorrent',
symbol: 'BTT',
decimals: 18
}
},
'bittorrent-testnet': {
chainId: 1029,
name: 'BitTorrent Chain Testnet',
shortName: 'btt-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'BitTorrent',
symbol: 'BTT',
decimals: 18
}
},
// Blast
'blast': {
chainId: 81457,
name: 'Blast Mainnet',
shortName: 'blast',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'blast-sepolia': {
chainId: 168587773,
name: 'Blast Sepolia Testnet',
shortName: 'blast-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// BNB Smart Chain
'bnb': {
chainId: 56,
name: 'BNB Smart Chain Mainnet',
shortName: 'bnb',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18
}
},
'bnb-testnet': {
chainId: 97,
name: 'BNB Smart Chain Testnet',
shortName: 'bnb-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18
}
},
// Celo
'celo': {
chainId: 42220,
name: 'Celo Mainnet',
shortName: 'celo',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'CELO',
symbol: 'CELO',
decimals: 18
}
},
'celo-sepolia': {
chainId: 11142220,
name: 'Celo Sepolia Testnet',
shortName: 'celo-sep',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'CELO',
symbol: 'CELO',
decimals: 18
}
},
// Cronos
'cronos': {
chainId: 25,
name: 'Cronos Mainnet',
shortName: 'cro',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Cronos',
symbol: 'CRO',
decimals: 18
}
},
'cronos-testnet': {
chainId: 338,
name: 'Cronos Testnet',
shortName: 'cro-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Cronos',
symbol: 'CRO',
decimals: 18
}
},
// Fantom
'fantom': {
chainId: 250,
name: 'Fantom Opera',
shortName: 'ftm',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Fantom',
symbol: 'FTM',
decimals: 18
}
},
'fantom-testnet': {
chainId: 4002,
name: 'Fantom Testnet',
shortName: 'ftm-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Fantom',
symbol: 'FTM',
decimals: 18
}
},
// Fraxtal
'fraxtal': {
chainId: 252,
name: 'Fraxtal Mainnet',
shortName: 'frax',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Frax Ether',
symbol: 'frxETH',
decimals: 18
}
},
'fraxtal-hoodi': {
chainId: 2523,
name: 'Fraxtal Hoodi Testnet',
shortName: 'frax-hoodi',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Frax Ether',
symbol: 'frxETH',
decimals: 18
}
},
// Gnosis
'gnosis': {
chainId: 100,
name: 'Gnosis',
shortName: 'gno',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'xDAI',
symbol: 'xDAI',
decimals: 18
}
},
// HyperEVM
'hyperevm': {
chainId: 999,
name: 'HyperEVM Mainnet',
shortName: 'hyperevm',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
// Katana
'katana': {
chainId: 747474,
name: 'Katana Mainnet',
shortName: 'katana',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'RON',
symbol: 'RON',
decimals: 18
}
},
'katana-bokuto': {
chainId: 737373,
name: 'Katana Bokuto',
shortName: 'katana-bokuto',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'RON',
symbol: 'RON',
decimals: 18
}
},
// Linea
'linea': {
chainId: 59144,
name: 'Linea Mainnet',
shortName: 'linea',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'linea-sepolia': {
chainId: 59141,
name: 'Linea Sepolia Testnet',
shortName: 'linea-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// Mantle
'mantle': {
chainId: 5000,
name: 'Mantle Mainnet',
shortName: 'mnt',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Mantle',
symbol: 'MNT',
decimals: 18
}
},
'mantle-sepolia': {
chainId: 5003,
name: 'Mantle Sepolia Testnet',
shortName: 'mnt-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Mantle',
symbol: 'MNT',
decimals: 18
}
},
// Memecore
'memecore-testnet': {
chainId: 43521,
name: 'Memecore Testnet',
shortName: 'memecore-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Meme',
symbol: 'MEME',
decimals: 18
}
},
// Monad
'monad': {
chainId: 143,
name: 'Monad Mainnet',
shortName: 'monad',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Monad',
symbol: 'MON',
decimals: 18
}
},
'monad-testnet': {
chainId: 10143,
name: 'Monad Testnet',
shortName: 'monad-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Monad',
symbol: 'MON',
decimals: 18
}
},
// Moonbeam
'moonbeam': {
chainId: 1284,
name: 'Moonbeam Mainnet',
shortName: 'mbeam',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Glimmer',
symbol: 'GLMR',
decimals: 18
}
},
'moonriver': {
chainId: 1285,
name: 'Moonriver Mainnet',
shortName: 'mriver',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Moonriver',
symbol: 'MOVR',
decimals: 18
}
},
'moonbase': {
chainId: 1287,
name: 'Moonbase Alpha Testnet',
shortName: 'mbase',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Dev',
symbol: 'DEV',
decimals: 18
}
},
// opBNB
'opbnb': {
chainId: 204,
name: 'opBNB Mainnet',
shortName: 'opbnb',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18
}
},
'opbnb-testnet': {
chainId: 5611,
name: 'opBNB Testnet',
shortName: 'opbnb-test',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18
}
},
// Optimism
'optimism': {
chainId: 10,
name: 'Optimism Mainnet',
shortName: 'op',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'optimism-sepolia': {
chainId: 11155420,
name: 'Optimism Sepolia Testnet',
shortName: 'op-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// Polygon
'polygon': {
chainId: 137,
name: 'Polygon Mainnet',
shortName: 'matic',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
}
},
'polygon-amoy': {
chainId: 80002,
name: 'Polygon Amoy Testnet',
shortName: 'matic-amoy',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
}
},
'polygon-zkevm': {
chainId: 1101,
name: 'Polygon zkEVM Mainnet',
shortName: 'polygon-zkevm',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'polygon-zkevm-cardano': {
chainId: 2442,
name: 'Polygon zkEVM Cardano Testnet',
shortName: 'polygon-zkevm-cardano',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
// Scroll
'scroll': {
chainId: 534352,
name: 'Scroll Mainnet',
shortName: 'scroll',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'scroll-sepolia': {
chainId: 534351,
name: 'Scroll Sepolia Testnet',
shortName: 'scroll-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// Sei
'sei': {
chainId: 1329,
name: 'Sei Mainnet',
shortName: 'sei',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Sei',
symbol: 'SEI',
decimals: 18
}
},
'sei-testnet': {
chainId: 1328,
name: 'Sei Testnet',
shortName: 'sei-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Sei',
symbol: 'SEI',
decimals: 18
}
},
// Sonic
'sonic': {
chainId: 146,
name: 'Sonic Mainnet',
shortName: 'sonic',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'Sonic',
symbol: 'S',
decimals: 18
}
},
'sonic-testnet': {
chainId: 14601,
name: 'Sonic Testnet',
shortName: 'sonic-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Sonic',
symbol: 'S',
decimals: 18
}
},
// Stable
'stable-testnet': {
chainId: 2201,
name: 'Stable Testnet',
shortName: 'stable-test',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
// Swellchain
'swellchain': {
chainId: 1923,
name: 'Swellchain Mainnet',
shortName: 'swell',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'swellchain-testnet': {
chainId: 1924,
name: 'Swellchain Testnet',
shortName: 'swell-test',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
// Taiko
'taiko': {
chainId: 167000,
name: 'Taiko Mainnet',
shortName: 'taiko',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'taiko-hoodi': {
chainId: 167013,
name: 'Taiko Hoodi',
shortName: 'taiko-hoodi',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
// Unichain
'unichain': {
chainId: 130,
name: 'Unichain Mainnet',
shortName: 'unichain',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'unichain-sepolia': {
chainId: 1301,
name: 'Unichain Sepolia Testnet',
shortName: 'unichain-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// World
'world': {
chainId: 480,
name: 'World Mainnet',
shortName: 'world',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'world-sepolia': {
chainId: 4801,
name: 'World Sepolia Testnet',
shortName: 'world-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
},
// XDC
'xdc': {
chainId: 50,
name: 'XDC Mainnet',
shortName: 'xdc',
isTestnet: false,
isL2: false,
nativeCurrency: {
name: 'XDC',
symbol: 'XDC',
decimals: 18
}
},
'xdc-apothem': {
chainId: 51,
name: 'XDC Apothem Testnet',
shortName: 'xdc-apothem',
isTestnet: true,
isL2: false,
nativeCurrency: {
name: 'XDC',
symbol: 'XDC',
decimals: 18
}
},
// zkSync
'zksync': {
chainId: 324,
name: 'zkSync Mainnet',
shortName: 'zksync',
isTestnet: false,
isL2: true,
nativeCurrency: {
name: 'Ether',
symbol: 'ETH',
decimals: 18
}
},
'zksync-sepolia': {
chainId: 300,
name: 'zkSync Sepolia Testnet',
shortName: 'zksync-sep',
isTestnet: true,
isL2: true,
nativeCurrency: {
name: 'Sepolia Ether',
symbol: 'ETH',
decimals: 18
}
}
};
/**
* Mapping from chain ID to network slug
* Enables quick lookup of network configuration by chain ID
*/
export const CHAIN_ID_TO_NETWORK: Record<number, string> = {
// Ethereum
1: 'ethereum',
11155111: 'sepolia',
17000: 'holesky',
560048: 'hoodi',
// Abstract
2741: 'abstract',
11124: 'abstract-sepolia',
// ApeChain
33139: 'apechain',
33111: 'apechain-curtis',
// Arbitrum
42161: 'arbitrum',
42170: 'arbitrum-nova',
421614: 'arbitrum-sepolia',
// Avalanche
43114: 'avalanche',
43113: 'avalanche-fuji',
// Base
8453: 'base',
84532: 'base-sepolia',
// Berachain
80094: 'berachain',
80069: 'berachain-bepolia',
// BitTorrent
199: 'bittorrent',
1029: 'bittorrent-testnet',
// Blast
81457: 'blast',
168587773: 'blast-sepolia',
// BNB
56: 'bnb',
97: 'bnb-testnet',
// Celo
42220: 'celo',
11142220: 'celo-sepolia',
// Cronos
25: 'cronos',
338: 'cronos-testnet',
// Fantom
250: 'fantom',
4002: 'fantom-testnet',
// Fraxtal
252: 'fraxtal',
2523: 'fraxtal-hoodi',
// Gnosis
100: 'gnosis',
// HyperEVM
999: 'hyperevm',
// Katana
747474: 'katana',
737373: 'katana-bokuto',
// Linea
59144: 'linea',
59141: 'linea-sepolia',
// Mantle
5000: 'mantle',
5003: 'mantle-sepolia',
// Memecore
43521: 'memecore-testnet',
// Monad
143: 'monad',
10143: 'monad-testnet',
// Moonbeam
1284: 'moonbeam',
1285: 'moonriver',
1287: 'moonbase',
// opBNB
204: 'opbnb',
5611: 'opbnb-testnet',
// Optimism
10: 'optimism',
11155420: 'optimism-sepolia',
// Polygon
137: 'polygon',
80002: 'polygon-amoy',
1101: 'polygon-zkevm',
2442: 'polygon-zkevm-cardano',
// Scroll
534352: 'scroll',
534351: 'scroll-sepolia',
// Sei
1329: 'sei',
1328: 'sei-testnet',
// Sonic
146: 'sonic',
14601: 'sonic-testnet',
// Stable
2201: 'stable-testnet',
// Swellchain
1923: 'swellchain',
1924: 'swellchain-testnet',
// Taiko
167000: 'taiko',
167013: 'taiko-hoodi',
// Unichain
130: 'unichain',
1301: 'unichain-sepolia',
// World
480: 'world',
4801: 'world-sepolia',
// XDC
50: 'xdc',
51: 'xdc-apothem',
// zkSync
324: 'zksync',
300: 'zksync-sepolia'
};
/**
* Type representing all valid network slugs
* Automatically derived from NETWORKS keys
*/
export type NetworkSlug = keyof typeof NETWORKS;
/**
* Get network configuration by chain ID
*
* @param chainId - EIP-155 chain ID
* @returns NetworkConfig if found, undefined otherwise
*
* @example
* ```typescript
* const config = getNetworkByChainId(1);
* // Returns: { chainId: 1, name: 'Ethereum Mainnet', ... }
* ```
*/
export function getNetworkByChainId(chainId: number): NetworkConfig | undefined {
const slug = CHAIN_ID_TO_NETWORK[chainId];
if (!slug) {
return undefined;
}
return NETWORKS[slug];
}
/**
* Get network configuration by slug
*
* @param slug - Network slug (e.g., 'ethereum', 'arbitrum', 'base')
* @returns NetworkConfig if found, undefined otherwise
*
* @example
* ```typescript
* const config = getNetworkBySlug('ethereum');
* // Returns: { chainId: 1, name: 'Ethereum Mainnet', ... }
* ```
*/
export function getNetworkBySlug(slug: string): NetworkConfig | undefined {
return NETWORKS[slug];
}
/**
* Get chain ID from network slug or chain ID
* Handles both string slugs and numeric chain IDs
*
* @param networkOrChainId - Network slug string or chain ID number
* @returns Chain ID number
* @throws Error if network is not found or invalid
*
* @example
* ```typescript
* getChainId('ethereum') // Returns: 1
* getChainId(1) // Returns: 1
* getChainId('arbitrum') // Returns: 42161
* ```
*/
export function getChainId(networkOrChainId: string | number): number {
if (typeof networkOrChainId === 'number') {
// Validate that the chain ID exists
if (!CHAIN_ID_TO_NETWORK[networkOrChainId]) {
throw new Error(`Invalid chain ID: ${networkOrChainId}`);
}
return networkOrChainId;
}
// Handle string slug
const network = NETWORKS[networkOrChainId];
if (!network) {
throw new Error(`Unknown network: ${networkOrChainId}`);
}
return network.chainId;
}
/**
* Get all available network configurations
*
* @returns Array of all NetworkConfig objects
*
* @example
* ```typescript
* const allNetworks = getAllNetworks();
* console.log(`Total networks: ${allNetworks.length}`);
* ```
*/
export function getAllNetworks(): NetworkConfig[] {
// Use Set to deduplicate by chainId (since mainnet/ethereum both map to chain 1)
const seen = new Set<number>();
const unique: NetworkConfig[] = [];
for (const config of Object.values(NETWORKS)) {
if (!seen.has(config.chainId)) {
seen.add(config.chainId);
unique.push(config);
}
}
return unique;
}
/**
* Get all mainnet network configurations
*
* @returns Array of mainnet NetworkConfig objects
*
* @example
* ```typescript
* const mainnets = getMainnets();
* // Returns all networks where isTestnet === false
* ```
*/
export function getMainnets(): NetworkConfig[] {
return getAllNetworks().filter(network => !network.isTestnet);
}
/**
* Get all testnet network configurations
*
* @returns Array of testnet NetworkConfig objects
*
* @example
* ```typescript
* const testnets = getTestnets();
* // Returns all networks where isTestnet === true
* ```
*/
export function getTestnets(): NetworkConfig[] {
return getAllNetworks().filter(network => network.isTestnet);
}
/**
* Validate if a network slug or chain ID is supported
*
* @param networkOrChainId - Network slug string or chain ID number
* @returns true if valid, false otherwise
*
* @example
* ```typescript
* isValidNetwork('ethereum') // Returns: true
* isValidNetwork(1) // Returns: true
* isValidNetwork('invalid') // Returns: false
* isValidNetwork(999999) // Returns: false
* ```
*/
export function isValidNetwork(networkOrChainId: string | number): boolean {
if (typeof networkOrChainId === 'number') {
return networkOrChainId in CHAIN_ID_TO_NETWORK;
}
return networkOrChainId in NETWORKS;
}