import config from '../config/config'; export const normalizeChainId = (value: unknown): string | null => { if (!value) { return null; } let newValue: string = typeof value === 'number' ? value.toString(16) : String(value); if (!newValue.startsWith('0x')) { newValue = '0x' + newValue; } return newValue; }; export const checkIsChainSupported = (chainId: any) => { const parsedChainId = normalizeChainId(chainId); if (!parsedChainId) { return false; } return !!config.availableWallets.find( (v) => normalizeChainId(v.chainId) === parsedChainId ); }; export const translateChainIdToName = (chainId: number | string) => config.availableWallets.find((v) => v.chainId === normalizeChainId(chainId)) ?.name || 'n/a'; export const getWalletConfigByChainId = (chainId?: unknown) => { const chainConfig = config.availableWallets.find( (network) => normalizeChainId(chainId) === normalizeChainId(network.chainId) ); if (!chainConfig) { throw new Error(`Cannot fetch config for chainId ${chainId}`); } const systemData = config.getSystemData(chainConfig.chainId); return { ...chainConfig, // layerZeroChainId: systemData.layerZeroChainId, contractAddress: { ...chainConfig.contractAddress, ...systemData }, }; }; /** * Translate chain id to layer zero network id * * @param {string} chainId */ export const translateChainIdToLayer0ChainId = (chainId: string) => getWalletConfigByChainId(chainId)?.layerZeroChainId; export const translateChainIdToAxelarChainId = (chainId: string) => getWalletConfigByChainId(chainId)?.axelarChainId; export const translateLayer0ChainIdToChainId = (layerZeroChainId: number) => { const wallet = config.availableWallets.find( (network) => network.layerZeroChainId === layerZeroChainId ); if (wallet) { return normalizeChainId(wallet.chainId); } }; export const generateTxUrl = (chainId: string, tx: string) => { const wallet = getWalletConfigByChainId(chainId); const url = new URL('/tx/' + tx, wallet?.scanUrl); return url.toString(); };