From 3efb08269517792ae16de036750f4e2338011345 Mon Sep 17 00:00:00 2001 From: Juuso Takalainen Date: Wed, 11 Dec 2024 19:20:36 +0200 Subject: [PATCH 1/2] 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) { From 062773482c42abf73d450dcf91ea64c3ca21e470 Mon Sep 17 00:00:00 2001 From: Juuso Takalainen Date: Sat, 14 Dec 2024 01:59:42 +0200 Subject: [PATCH 2/2] build: add IoTeX network to configs manually tested, creating a stream works fine! --- package-lock.json | 13 +++++++++---- package.json | 2 +- src/config/chains.toml | 6 ++++++ src/config/environments.toml | 2 +- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index d810e3e46e..3b6f5d86d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,7 +18,7 @@ "@metamask/providers": "^17.1.2", "@sambego/storybook-styles": "^1.0.0", "@sentry/react": "^8.34.0", - "@streamr/config": "^5.4.0", + "@streamr/config": "5.4.1-testing.2", "@streamr/hub-contracts": "^1.1.2", "@streamr/sdk": "^101.1.2", "@streamr/streamr-icons": "^0.1.9", @@ -10842,9 +10842,9 @@ } }, "node_modules/@streamr/config": { - "version": "5.4.0", - "resolved": "https://registry.npmjs.org/@streamr/config/-/config-5.4.0.tgz", - "integrity": "sha512-MdCBFffBRVPgGlaUN6kXfuNWXU20OuK+oQK4YKrdm7bkR/hyx3O78NbH8Mm/LhwQJhKVvl28s+WCr/TgH+uvrw==" + "version": "5.4.1-testing.2", + "resolved": "https://registry.npmjs.org/@streamr/config/-/config-5.4.1-testing.2.tgz", + "integrity": "sha512-yr9Rua03Y3v8p8kw3pQxUdIlitax7wSI02P3l6GcwG9LAcdo/LiaRgirhN/E9m6vQM31KMdgxf5SZNYvd4IJ8w==" }, "node_modules/@streamr/dht": { "version": "101.1.2", @@ -11280,6 +11280,11 @@ "utf-8-validate": "^6.0.3" } }, + "node_modules/@streamr/sdk/node_modules/@streamr/config": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/@streamr/config/-/config-5.4.1.tgz", + "integrity": "sha512-m9K44wrQ1fMLLKilh4PZt19tpDsm0e9eSgXom2TFSKvRX/BtY9DpkxlGZNUfaabjlImEouijibIGkGzCcd1kKA==" + }, "node_modules/@streamr/sdk/node_modules/node-fetch": { "version": "2.7.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", diff --git a/package.json b/package.json index 145c5d032d..6ce80b1e51 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "@metamask/providers": "^17.1.2", "@sambego/storybook-styles": "^1.0.0", "@sentry/react": "^8.34.0", - "@streamr/config": "^5.4.0", + "@streamr/config": "5.4.1-testing.2", "@streamr/hub-contracts": "^1.1.2", "@streamr/sdk": "^101.1.2", "@streamr/streamr-icons": "^0.1.9", diff --git a/src/config/chains.toml b/src/config/chains.toml index 395c25aea9..c58e0a58b0 100644 --- a/src/config/chains.toml +++ b/src/config/chains.toml @@ -19,6 +19,12 @@ address = "0xde1112f631486CfC759A50196853011528bC5FA0" chainId = 31337 name = "streamr-dev/dataunion" +[iotex] +networkSubgraphUrl = "https://api.studio.thegraph.com/query/58754/streamr-iotex/version/latest" + +[peaq] +networkSubgraphUrl = "https://api.studio.thegraph.com/query/58729/streamr-amoy-testnet/version/latest" + [amoy] dataUnionJoinServerUrl = "https://join.dataunions.org/" marketplaceChains = ['amoy'] diff --git a/src/config/environments.toml b/src/config/environments.toml index 7d3940ce4b..b6ce8c7e95 100644 --- a/src/config/environments.toml +++ b/src/config/environments.toml @@ -1,5 +1,5 @@ [development] -availableChains = ['dev2', 'polygon', 'amoy'] +availableChains = ['dev2', 'polygon', 'amoy', 'peaq', 'iotex'] platformOriginUrl = "" streamrUrl = ""