From 3efb08269517792ae16de036750f4e2338011345 Mon Sep 17 00:00:00 2001 From: Juuso Takalainen Date: Wed, 11 Dec 2024 19:20:36 +0200 Subject: [PATCH] refactor: chain name selection it used to not really tolerate "IoTeX Testnet" for some reason. Now it seems to work (tested manually). --- src/shared/utils/constants.ts | 4 +- src/utils/chains.ts | 133 +++++++++++++++------------------- 2 files changed, 62 insertions(+), 75 deletions(-) diff --git a/src/shared/utils/constants.ts b/src/shared/utils/constants.ts index 45206bfdc3..3ffdb1fdcf 100644 --- a/src/shared/utils/constants.ts +++ b/src/shared/utils/constants.ts @@ -19,7 +19,7 @@ export const paymentCurrencies = { NATIVE: 'NATIVE', } -export const ethereumNetworks = { +export const ethereumNetworkDisplayName = { '1': 'Ethereum Mainnet', '3': 'Ropsten', '4': 'Rinkeby', @@ -33,4 +33,6 @@ export const ethereumNetworks = { '80002': 'Amoy', } +export const defaultEthereumNetwork = 'polygon' + export const maxFileSizeForImageUpload = 5242880 diff --git a/src/utils/chains.ts b/src/utils/chains.ts index 7e781fd7b9..4fef848275 100644 --- a/src/utils/chains.ts +++ b/src/utils/chains.ts @@ -8,15 +8,30 @@ import { parsedChainConfigExtension, } from '~/utils/chainConfigExtension' import { Chain } from '~/types' -import { ethereumNetworks } from '~/shared/utils/constants' +import { ethereumNetworkDisplayName, defaultEthereumNetwork } from '~/shared/utils/constants' import formatConfigUrl from './formatConfigUrl' -function getPreferredChainName(chainName: string) { - if (/amoy/i.test(chainName)) { - return 'amoy' +/** + * @param chainNameOrId Chain name from displayNames or config (if string), or chainId (if number) + * @returns Chain name as enumerated in @streamr/config, or default ("polygon" as of 2024) if not found + */ +function parseChainName(chainNameOrId: string | number): string { + if (typeof chainNameOrId === 'string') { + const nameFromConfig = Object.keys(configs).find(configChainName => + chainNameOrId.toLowerCase() === configChainName.toLowerCase() + ) + const [chainIdString, _] = Object.entries(ethereumNetworkDisplayName).find(([_, displayName]) => + chainNameOrId.toLowerCase() == displayName.toLowerCase() + ) ?? [] + const nameFromDisplayNames = Object.keys(configs).find(configChainName => + configs[configChainName].id.toString() === chainIdString + ) + return nameFromConfig ?? nameFromDisplayNames ?? defaultEthereumNetwork + } else { + return Object.keys(configs).find(configChainName => + configs[configChainName].id === chainNameOrId + ) ?? defaultEthereumNetwork } - - return chainName.toLowerCase() } function getChainConfigWithFallback(chainName: string): Chain { @@ -24,12 +39,12 @@ function getChainConfigWithFallback(chainName: string): Chain { return getChainConfig(chainName) } catch (_) {} - return getChainConfig('polygon') + return getChainConfig(defaultEthereumNetwork) } export function getCurrentChain() { return getChainConfigWithFallback( - new URLSearchParams(window.location.search).get('chain') || 'polygon', + new URLSearchParams(window.location.search).get('chain') || defaultEthereumNetwork, ) } @@ -38,7 +53,7 @@ export function getCurrentChainId() { } export function useCurrentChain() { - const chainName = useSearchParams()[0].get('chain') || 'polygon' + const chainName = useSearchParams()[0].get('chain') || defaultEthereumNetwork return useMemo(() => getChainConfigWithFallback(chainName), [chainName]) } @@ -62,87 +77,57 @@ export function useCurrentChainFullName() { } interface ChainEntry { + name: string config: Chain configExtension: ChainConfigExtension - symbolicName: string } -const chainEntriesByIdOrName: Partial> = {} - -function getChainEntry(chainIdOrName: string | number) { - const key = - typeof chainIdOrName === 'string' - ? getPreferredChainName(chainIdOrName) - : chainIdOrName - - let entry = chainEntriesByIdOrName[key] - - if (typeof entry === 'undefined') { - entry = (() => { - const source = Object.entries(configs).find(([symbolicName, config]) => - typeof chainIdOrName === 'string' - ? getPreferredChainName(chainIdOrName) === - getPreferredChainName(symbolicName) - : chainIdOrName === config.id, - ) - - if (!source) { - return null - } - - const [rawSymbolicName, config] = source +const chainEntries: Record = {} - const symbolicName = getPreferredChainName(rawSymbolicName) +function getChainEntry(chainIdOrName: string | number): ChainEntry { + const chainName = parseChainName(chainIdOrName) - const configExtension = - parsedChainConfigExtension[symbolicName] || fallbackChainConfigExtension + if (!(chainName in chainEntries)) { + const config = configs[chainName] - const { dockerHost } = configExtension + const configExtension = + parsedChainConfigExtension[chainName] || fallbackChainConfigExtension - const sanitizedConfig = produce(config, (draft) => { - draft.name = ethereumNetworks[config.id] || config.name + const { dockerHost } = configExtension - for (const rpc of draft.rpcEndpoints) { - rpc.url = formatConfigUrl(rpc.url, { - dockerHost, - }) - } + const sanitizedConfig = produce(config, (draft) => { + draft.name = ethereumNetworkDisplayName[config.id] || config.name - if (draft.entryPoints) { - for (const entrypoint of draft.entryPoints) { - entrypoint.websocket.host = formatConfigUrl( - entrypoint.websocket.host, - { - dockerHost, - }, - ) - } - } + for (const rpc of draft.rpcEndpoints) { + rpc.url = formatConfigUrl(rpc.url, { + dockerHost, + }) + } - if (draft.theGraphUrl) { - draft.theGraphUrl = formatConfigUrl(draft.theGraphUrl, { dockerHost }) + if (draft.entryPoints) { + for (const entrypoint of draft.entryPoints) { + entrypoint.websocket.host = formatConfigUrl( + entrypoint.websocket.host, + { + dockerHost, + }, + ) } - }) - - return { - symbolicName, - config: sanitizedConfig, - configExtension, } - })() - chainEntriesByIdOrName[key] = entry - } + if (draft.theGraphUrl) { + draft.theGraphUrl = formatConfigUrl(draft.theGraphUrl, { dockerHost }) + } + }) - if (!entry) { - throw new Error( - `Could not find config for "${chainIdOrName}" (${ - typeof chainIdOrName === 'string' ? 'chain name' : 'chain id' - })`, - ) + chainEntries[chainName] = { + name: chainName, + config: sanitizedConfig, + configExtension, + } } - return entry + return chainEntries[chainName]! } export function getChainConfig(chainIdOrSymbolicName: string | number): Chain { @@ -150,7 +135,7 @@ export function getChainConfig(chainIdOrSymbolicName: string | number): Chain { } export function getSymbolicChainName(chainId: number) { - return getChainEntry(chainId).symbolicName + return getChainEntry(chainId).name } export function getChainConfigExtension(chainId: number) {