Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ETH-794: chain name refactor #1965

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/shared/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const paymentCurrencies = {
NATIVE: 'NATIVE',
}

export const ethereumNetworks = {
export const ethereumNetworkDisplayName = {
'1': 'Ethereum Mainnet',
'3': 'Ropsten',
'4': 'Rinkeby',
Expand All @@ -33,4 +33,6 @@ export const ethereumNetworks = {
'80002': 'Amoy',
}

export const defaultEthereumNetwork = 'polygon'

export const maxFileSizeForImageUpload = 5242880
133 changes: 59 additions & 74 deletions src/utils/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,43 @@ 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 {
try {
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,
)
}

Expand All @@ -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])
}
Expand All @@ -62,95 +77,65 @@ export function useCurrentChainFullName() {
}

interface ChainEntry {
name: string
config: Chain
configExtension: ChainConfigExtension
symbolicName: string
}

const chainEntriesByIdOrName: Partial<Record<string | number, ChainEntry | null>> = {}

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<Chain>(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<string, ChainEntry | null> = {}

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 {
return getChainEntry(chainIdOrSymbolicName).config
}

export function getSymbolicChainName(chainId: number) {
return getChainEntry(chainId).symbolicName
return getChainEntry(chainId).name
}

export function getChainConfigExtension(chainId: number) {
Expand Down
Loading