import { ChainId, NetworkId, networkIdToName } from '@jarvisnetwork/core-sdk'; import { NetworksIconKeys } from '@jarvisnetwork/ui'; import { RPC_URLS } from './rpc'; export interface ChainInformation { readonly url: string[]; readonly name: string; readonly iconName: NetworksIconKeys; readonly testnetIdentifier?: string; readonly blockExplorerUrls: string[]; readonly nativeCurrency: { name: string; // e.g. 'Goerli ETH', symbol: string; // e.g. 'gorETH', decimals: number; // e.g. 18, }; } export const NETWORKS: { [chainId in NetworkId]?: ChainInformation; } = { [ChainId.mainnet]: { url: RPC_URLS[ChainId.mainnet], name: networkIdToName[ChainId.mainnet], iconName: 'Eth', nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, blockExplorerUrls: ['https://etherscan.io'], }, [ChainId.bsc]: { url: RPC_URLS[ChainId.bsc], name: networkIdToName[ChainId.bsc], iconName: 'BSC', nativeCurrency: { name: 'Binance Coin', symbol: 'BNB', decimals: 18, }, blockExplorerUrls: ['https://bscscan.com'], }, [ChainId.bscTestnet]: { url: RPC_URLS[ChainId.bscTestnet], name: networkIdToName[ChainId.bscTestnet], iconName: 'BSC', nativeCurrency: { name: 'Binance Coin', symbol: 'BNB', decimals: 18, }, testnetIdentifier: 'T', blockExplorerUrls: ['https://testnet.bscscan.com/'], }, [ChainId.polygon]: { url: RPC_URLS[ChainId.polygon], name: networkIdToName[ChainId.polygon], iconName: 'Matic', nativeCurrency: { name: 'Polygon Matic', symbol: 'MATIC', decimals: 18 }, blockExplorerUrls: ['https://polygonscan.com'], }, [ChainId.mumbai]: { url: RPC_URLS[ChainId.mumbai], name: networkIdToName[ChainId.mumbai], iconName: 'Mumbai', nativeCurrency: { name: 'Polygon Mumbai Matic', symbol: 'mMATIC', decimals: 18, }, testnetIdentifier: 'M', blockExplorerUrls: ['https://mumbai.polygonscan.com'], }, [ChainId.avalanche]: { url: RPC_URLS[ChainId.avalanche], name: networkIdToName[ChainId.avalanche], iconName: 'AVAX', nativeCurrency: { name: 'Avalanche', symbol: 'AVAX', decimals: 18, }, blockExplorerUrls: ['https://subnets.avax.network'], }, [ChainId.fuji]: { url: RPC_URLS[ChainId.fuji], name: networkIdToName[ChainId.fuji], iconName: 'AVAX', nativeCurrency: { name: 'Avalanche', symbol: 'AVAX', decimals: 18, }, testnetIdentifier: 'F', blockExplorerUrls: [' https://testnet.snowtrace.io/'], }, [ChainId.gnosis]: { url: RPC_URLS[ChainId.gnosis], name: networkIdToName[ChainId.gnosis], iconName: 'GNO', nativeCurrency: { name: 'Gnosis', symbol: 'GNO', decimals: 18, }, blockExplorerUrls: ['https://blockscout.com/xdai/mainnet'], }, [ChainId.optimism]: { url: RPC_URLS[ChainId.optimism], name: networkIdToName[ChainId.optimism], iconName: 'OP', nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18, }, blockExplorerUrls: ['https://optimistic.etherscan.io/'], }, [ChainId.arbitrum]: { url: RPC_URLS[ChainId.arbitrum], name: networkIdToName[ChainId.arbitrum], iconName: 'Arbitrum', nativeCurrency: { name: 'Ethereum', symbol: 'ETH', decimals: 18, }, blockExplorerUrls: ['https://arbiscan.io/'], }, }; const parseSupportedNetworks = (varName: string, variable?: string) => { if (typeof variable !== 'string') { throw new Error(`${varName} is not a string`); } return variable.split(',').map((id) => { const result = parseInt(id, 10); if (!result) throw new Error(`Network id '${id}' is not a valid integer`); return result; }); }; export const supportedNetworkIds = parseSupportedNetworks( 'NEXT_PUBLIC_SUPPORTED_NETWORKS', process.env.NEXT_PUBLIC_SUPPORTED_NETWORKS ) as NetworkId[]; if (supportedNetworkIds.length === 0) { throw new Error('NEXT_PUBLIC_SUPPORTED_NETWORKS has 0 networks'); } export const isSupportedNetworkId = (chainId: number | undefined): boolean => !!chainId && supportedNetworkIds.includes(chainId as NetworkId); const defaultNetworkId = parseSupportedNetworks( 'NEXT_PUBLIC_DEFAULT_SUPPORTED_NETWORKS', process.env.NEXT_PUBLIC_DEFAULT_SUPPORTED_NETWORKS ); if (defaultNetworkId.length > 1) { throw new Error( 'NEXT_PUBLIC_DEFAULT_SUPPORTED_NETWORKS has more than one network defined' ); } export const defaultSupportedNetworkId = supportedNetworkIds.find( (networkId) => networkId === defaultNetworkId[0] ); if (!defaultSupportedNetworkId) { throw new Error( 'NEXT_PUBLIC_DEFAULT_SUPPORTED_NETWORKS has 0 default network' ); }