Skip to content

Commit 702c4ae

Browse files
committed
Refactor chain display names; + other tweaks
I also move some coingeco stuff into chain config extensions so that we don't handle any edge cases and all local chain config customisations are in one place.
1 parent 780ebd3 commit 702c4ae

17 files changed

+159
-147
lines changed

src/components/ChainSelector.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ import UnstyledNetworkIcon from '~/shared/components/NetworkIcon'
99
import SvgIcon from '~/shared/components/SvgIcon'
1010
import { COLORS, LAPTOP } from '~/shared/utils/styled'
1111
import { StreamDraft } from '~/stores/streamDraft'
12-
import { getChainSlug, useCurrentChain } from '~/utils/chains'
12+
import { getChainDisplayName, getChainSlug, useCurrentChain } from '~/utils/chains'
1313

1414
type MenuItemProps = {
15-
chain: Chain
15+
chainId: number
1616
isSelected: boolean
1717
onClick: () => void
1818
}
1919

20-
const MenuItem = ({ chain, isSelected, onClick }: MenuItemProps) => (
20+
const MenuItem = ({ chainId, isSelected, onClick }: MenuItemProps) => (
2121
<MenuItemContainer onClick={onClick}>
22-
<NetworkIcon chainId={chain.id} />
23-
<div>{chain.name}</div>
22+
<NetworkIcon chainId={chainId} />
23+
<div>{getChainDisplayName(chainId)}</div>
2424
{isSelected ? <SvgIcon name="tick" /> : <div />}
2525
</MenuItemContainer>
2626
)
@@ -40,7 +40,7 @@ const Menu = ({ chains, selectedChain, toggle }: MenuProps) => {
4040
{chains.map((c) => (
4141
<MenuItem
4242
key={c.id}
43-
chain={c}
43+
chainId={c.id}
4444
isSelected={c.id === selectedChain.id}
4545
onClick={() => {
4646
toggle(false)
@@ -67,7 +67,7 @@ interface Props {
6767
}
6868

6969
export const ChainSelector = ({ menuAlignment = 'left', ...props }: Props) => {
70-
const availableChains = getEnvironmentConfig().availableChains
70+
const { availableChains } = getEnvironmentConfig()
7171

7272
const selectedChain = useCurrentChain()
7373

@@ -89,7 +89,7 @@ export const ChainSelector = ({ menuAlignment = 'left', ...props }: Props) => {
8989
{(toggle, isOpen) => (
9090
<Toggle $isOpen={isOpen} onClick={() => toggle((v) => !v)}>
9191
<NetworkIcon chainId={selectedChain.id} />
92-
<div>{selectedChain.name}</div>
92+
<div>{getChainDisplayName(selectedChain.id)}</div>
9393
<Caret name="caretUp" $isOpen={isOpen} />
9494
</Toggle>
9595
)}

src/components/SalePointSelector/SalePointOption.tsx

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
import { produce } from 'immer'
12
import React, { ComponentProps, ReactNode, useEffect, useRef, useState } from 'react'
23
import styled, { css } from 'styled-components'
34
import { z } from 'zod'
4-
import { produce } from 'immer'
5-
import SvgIcon from '~/shared/components/SvgIcon'
6-
import { SalePoint } from '~/shared/types'
7-
import { COLORS } from '~/shared/utils/styled'
5+
import { getDataUnion, getDataUnionsOwnedByInChain } from '~/getters/du'
6+
import { SelectField2 } from '~/marketplace/components/SelectField2'
87
import { Tick as PrestyledTick } from '~/shared/components/Checkbox'
98
import NetworkIcon from '~/shared/components/NetworkIcon'
10-
import { formatChainName } from '~/utils'
9+
import SvgIcon from '~/shared/components/SvgIcon'
1110
import { useWalletAccount } from '~/shared/stores/wallet'
12-
import { SelectField2 } from '~/marketplace/components/SelectField2'
13-
import { getDataUnion, getDataUnionsOwnedByInChain } from '~/getters/du'
14-
import { getChainConfig } from '~/utils/chains'
11+
import { SalePoint } from '~/shared/types'
12+
import { COLORS } from '~/shared/utils/styled'
13+
import { getChainDisplayName } from '~/utils/chains'
1514
import { Root as SalePointTokenSelectorRoot } from './SalePointTokenSelector'
1615

1716
export interface OptionProps {
@@ -32,10 +31,6 @@ export default function SalePointOption({
3231
}: SalePointOptionProps) {
3332
const { chainId, enabled, readOnly } = salePoint
3433

35-
const chain = getChainConfig(chainId)
36-
37-
const formattedChainName = formatChainName(chain.name)
38-
3934
return (
4035
<DropdownWrap $open={enabled}>
4136
<DropdownToggle
@@ -56,7 +51,7 @@ export default function SalePointOption({
5651
<RadioCircle $checked={enabled} $disabled={readOnly} />
5752
)}
5853
<ChainIcon chainId={chainId} />
59-
<ToggleText>{formattedChainName}</ToggleText>
54+
<ToggleText>{getChainDisplayName(chainId)}</ToggleText>
6055
{multiSelect && <PlusSymbol />}
6156
</DropdownToggle>
6257
<DropdownOuter>

src/config/chains.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
[ethereum]
2+
displayName = "Ethereum Mainnet"
3+
4+
[binance]
5+
coingeckoNetworkId = "binance-smart-chain"
6+
displayName = "BNB Smart Chain"
7+
8+
[gnosis]
9+
coingeckoNetworkId = "xdai"
10+
displayName = "Gnosis"
11+
112
[dev0]
13+
coingeckoNetworkId = "ethereum"
214
dockerHost = "localhost"
15+
displayName = "Dev0"
316

417
[dev1]
18+
coingeckoNetworkId = "ethereum"
519
dockerHost = "localhost"
20+
displayName = "Dev1"
621

722
[dev2]
23+
coingeckoNetworkId = "ethereum"
824
dataUnionJoinServerUrl = ":5555"
25+
displayName = "Dev2"
926
dockerHost = "localhost"
1027
marketplaceChains = ['dev2']
1128
networkSubgraphUrl = ":8800/subgraphs/name/streamr-dev/network-subgraphs"
@@ -21,6 +38,7 @@ name = "streamr-dev/dataunion"
2138

2239
[polygonAmoy]
2340
dataUnionJoinServerUrl = "https://join.dataunions.org/"
41+
displayName = "Amoy"
2442
marketplaceChains = ['polygonAmoy']
2543
slug = 'amoy'
2644

src/modals/AccessPeriodModal.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ import Text from '~/shared/components/Ui/Text'
1010
import useIsMounted from '~/shared/hooks/useIsMounted'
1111
import { COLORS, LIGHT, MEDIUM } from '~/shared/utils/styled'
1212
import { TimeUnit, timeUnits } from '~/shared/utils/timeUnit'
13-
import { formatChainName } from '~/utils'
1413
import { toFloat } from '~/utils/bn'
15-
import { getChainConfig } from '~/utils/chains'
14+
import { getChainDisplayName } from '~/utils/chains'
1615
import { RejectionReason } from '~/utils/exceptions'
1716
import { convertPrice } from '~/utils/price'
1817
import ProjectModal, { Actions } from './ProjectModal'
@@ -202,8 +201,6 @@ export default function AccessPeriodModal({
202201
setSelectedUnit(unit)
203202
}, [unit])
204203

205-
const chainName = formatChainName(getChainConfig(chainId).name)
206-
207204
const total = ((a: bigint) => (a > 0n ? a : 0n))(
208205
convertPrice(pricePerSecond, [length, selectedUnit]),
209206
)
@@ -289,7 +286,7 @@ export default function AccessPeriodModal({
289286
<DetailsContainer>
290287
<Chain>
291288
<ChainIcon chainId={chainId} />
292-
<ChainName>{chainName}</ChainName>
289+
<ChainName>{getChainDisplayName(chainId)}</ChainName>
293290
</Chain>
294291
<AmountBox>
295292
<AmountBar>

src/modals/ChainSelectorModal.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import NetworkIcon from '~/shared/components/NetworkIcon'
66
import useIsMounted from '~/shared/hooks/useIsMounted'
77
import { getUsdRate } from '~/shared/utils/coingecko'
88
import { MEDIUM } from '~/shared/utils/styled'
9-
import { formatChainName } from '~/utils'
109
import { getBalance } from '~/utils/balance'
11-
import { getChainConfig } from '~/utils/chains'
10+
import { getChainDisplayName } from '~/utils/chains'
1211
import { RejectionReason } from '~/utils/exceptions'
1312
import networkPreflight from '~/utils/networkPreflight'
1413
import { getTokenInfo } from '~/utils/tokens'
@@ -241,9 +240,7 @@ export default function ChainSelectorModal({
241240
}
242241
>
243242
<ChainIcon chainId={chainId} />
244-
<ChainName>
245-
{formatChainName(getChainConfig(chainId).name)}
246-
</ChainName>
243+
<ChainName>{getChainDisplayName(chainId)}</ChainName>
247244
<Radio $selected={selectedChainId === chainId} />
248245
</Item>
249246
</li>

src/modals/SwitchNetworkModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import styled from 'styled-components'
33
import { Buttons } from '~/components/Buttons'
44
import PngIcon from '~/shared/components/PngIcon'
5-
import { getChainConfig } from '~/utils/chains'
5+
import { getChainDisplayName, getChainKey } from '~/utils/chains'
66
import { RejectionReason } from '~/utils/exceptions'
77
import { Footer } from './BaseModal'
88
import Modal, { ModalProps } from './Modal'
@@ -15,7 +15,7 @@ interface Props extends Pick<ModalProps, 'onReject' | 'darkBackdrop'> {
1515

1616
function getChainName(chainId: number | string) {
1717
try {
18-
return getChainConfig(chainId).name
18+
return getChainDisplayName(getChainKey(chainId, { failOnNotFound: true }))
1919
} catch (_) {
2020
return `#${chainId}`
2121
}

src/pages/ProjectPage/AccessManifest.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import { ProjectType, SalePoint } from '~/shared/types'
1313
import { REGULAR, TABLET } from '~/shared/utils/styled'
1414
import { timeUnits } from '~/shared/utils/timeUnit'
1515
import { useIsAccessibleByCurrentWallet } from '~/stores/projectDraft'
16-
import { formatChainName } from '~/utils'
17-
import { getChainConfig, useCurrentChainId, useCurrentChainKey } from '~/utils/chains'
16+
import {
17+
getChainDisplayName,
18+
useCurrentChainId,
19+
useCurrentChainKey,
20+
} from '~/utils/chains'
1821
import { Route as R, routeOptions } from '~/utils/routes'
1922
import { errorToast } from '~/utils/toast'
2023

@@ -65,7 +68,7 @@ export function AccessManifest({
6568
timeUnit={timeUnits.hour}
6669
/>
6770
</strong>{' '}
68-
on <strong>{formatChainName(getChainConfig(chainId).name)}</strong>
71+
on <strong>{getChainDisplayName(chainId)}</strong>
6972
{count > 0 && (
7073
<>
7174
{' '}

src/pages/ProjectPage/GetAccess.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
import { ProjectType, SalePoint } from '~/shared/types'
1313
import { MEDIUM } from '~/shared/utils/styled'
1414
import { timeUnits } from '~/shared/utils/timeUnit'
15-
import { formatChainName } from '~/utils'
16-
import { getChainConfig, useCurrentChainId } from '~/utils/chains'
15+
import { getChainDisplayName, useCurrentChainId } from '~/utils/chains'
1716
import { errorToast } from '~/utils/toast'
1817

1918
const GetAccessContainer = styled.div`
@@ -90,7 +89,7 @@ export default function GetAccess({
9089
timeUnit={timeUnits.hour}
9190
/>
9291
</strong>{' '}
93-
on <strong>{formatChainName(getChainConfig(chainId).name)}</strong>
92+
on <strong>{getChainDisplayName(chainId)}</strong>
9493
{count > 0 && (
9594
<>
9695
and on {count} other chain{count > 1 && 's'}

src/pages/ProjectPage/ProjectEditorPage.tsx

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ import useIsMounted from '~/shared/hooks/useIsMounted'
2525
import { ProjectType, SalePoint } from '~/shared/types'
2626
import { ProjectDraft } from '~/stores/projectDraft'
2727
import { SalePointsPayload } from '~/types/projects'
28-
import { formatChainName } from '~/utils'
2928
import {
30-
getChainConfig,
31-
getChainConfigExtension,
29+
getChainDisplayName,
30+
getChainKey,
31+
getMarketplaceChainConfigs,
3232
useCurrentChainId,
3333
} from '~/utils/chains'
3434
import { Route as R, routeOptions } from '~/utils/routes'
@@ -59,15 +59,13 @@ export default function ProjectEditorPage() {
5959

6060
const chainId = useCurrentChainId()
6161

62-
const availableChains = useMemo<Chain[]>(
63-
() => getChainConfigExtension(chainId).marketplaceChains.map(getChainConfig),
62+
const availableChainConfigs = useMemo<Chain[]>(
63+
() => getMarketplaceChainConfigs(chainId),
6464
[chainId],
6565
)
6666

67-
const salePoints = availableChains
68-
.map<SalePoint | undefined>(
69-
({ name: chainName }) => existingSalePoints[chainName],
70-
)
67+
const salePoints = availableChainConfigs
68+
.map<SalePoint | undefined>(({ id }) => existingSalePoints[getChainKey(id)])
7169
.filter(Boolean) as SalePoint[]
7270

7371
function onSalePointChange(value: SalePoint) {
@@ -76,16 +74,16 @@ export default function ProjectEditorPage() {
7674
}
7775

7876
update((draft) => {
79-
const { name: chainName } = getChainConfig(value.chainId)
77+
const chainKey = getChainKey(value.chainId)
8078

81-
if (draft.salePoints[chainName]?.readOnly) {
79+
if (draft.salePoints[chainKey]?.readOnly) {
8280
/**
8381
* Read-only sale point must not be updated.
8482
*/
8583
return
8684
}
8785

88-
draft.salePoints[chainName] = value
86+
draft.salePoints[chainKey] = value
8987

9088
if (draft.type !== ProjectType.DataUnion) {
9189
return
@@ -151,14 +149,15 @@ export default function ProjectEditorPage() {
151149
</Content>
152150
<Content $desktopMaxWidth={728}>
153151
{salePoints.map((salePoint) => {
154-
const chainName = getChainConfig(
152+
const chainKey = getChainKey(
155153
salePoint.chainId,
156-
).name
154+
)
157155

158-
const formattedChainName =
159-
formatChainName(chainName)
156+
const chainName = getChainDisplayName(
157+
salePoint.chainId,
158+
)
160159

161-
const beneficiaryErrorKey = `salePoints.${chainName}.beneficiaryAddress`
160+
const beneficiaryErrorKey = `salePoints.${chainKey}.beneficiaryAddress`
162161

163162
const beneficiaryInvalid =
164163
!!errors[beneficiaryErrorKey]
@@ -172,7 +171,7 @@ export default function ProjectEditorPage() {
172171
>
173172
<h4>
174173
Set the payment token and price on
175-
the {formattedChainName} chain
174+
the {chainName} chain
176175
</h4>
177176
<p>
178177
You can set a price for others to
@@ -191,7 +190,7 @@ export default function ProjectEditorPage() {
191190
<p>
192191
This wallet address receives the
193192
payments for this product on{' '}
194-
{formattedChainName} chain.
193+
{chainName} chain.
195194
</p>
196195
<BeneficiaryAddressEditor
197196
invalid={beneficiaryInvalid}
@@ -325,10 +324,8 @@ export default function ProjectEditorPage() {
325324
<>
326325
Set the payment token and
327326
price on&nbsp;the&nbsp;
328-
{formatChainName(
329-
getChainConfig(
330-
salePoint.chainId,
331-
).name,
327+
{getChainDisplayName(
328+
salePoint.chainId,
332329
)}{' '}
333330
chain
334331
</>

0 commit comments

Comments
 (0)