Skip to content

Commit 4446dd5

Browse files
committed
Simplify getting chain name in the "Switch Chain" modal
Consistent naming.
1 parent 702c4ae commit 4446dd5

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

src/modals/SwitchNetworkModal.tsx

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,26 @@ 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 { getChainDisplayName, getChainKey } from '~/utils/chains'
5+
import { getChainDisplayName, getChainKey, isKnownChainId } from '~/utils/chains'
66
import { RejectionReason } from '~/utils/exceptions'
77
import { Footer } from './BaseModal'
88
import Modal, { ModalProps } from './Modal'
99

1010
interface Props extends Pick<ModalProps, 'onReject' | 'darkBackdrop'> {
11-
expectedNetwork: number | string
12-
actualNetwork: number | string
11+
expectedChainId: number
12+
actualChainId: number
1313
onResolve?: () => void
1414
}
1515

16-
function getChainName(chainId: number | string) {
17-
try {
18-
return getChainDisplayName(getChainKey(chainId, { failOnNotFound: true }))
19-
} catch (_) {
20-
return `#${chainId}`
21-
}
16+
function getChainName(chainId: number) {
17+
return isKnownChainId(chainId)
18+
? getChainDisplayName(getChainKey(chainId))
19+
: `#${chainId}`
2220
}
2321

2422
export default function SwitchNetworkModal({
25-
expectedNetwork,
26-
actualNetwork,
23+
expectedChainId,
24+
actualChainId,
2725
onReject,
2826
onResolve,
2927
...props
@@ -47,9 +45,9 @@ export default function SwitchNetworkModal({
4745
<PngIcon name="wallet" alt="Switch network" />
4846
</IconWrap>
4947
<P>
50-
Please switch to the <em>{getChainName(expectedNetwork)}</em> network
48+
Please switch to the <em>{getChainName(expectedChainId)}</em> network
5149
in your Ethereum wallet. It&apos;s currently in{' '}
52-
<em>{getChainName(actualNetwork)}</em>
50+
<em>{getChainName(actualChainId)}</em>
5351
&nbsp;network.
5452
</P>
5553
</Content>

src/utils/chains.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,14 @@ import formatConfigUrl from './formatConfigUrl'
1212

1313
const lowerCasedChainKeyToChainKeyMap: Record<string, ChainKey | null | undefined> = {}
1414

15-
interface GetChainKeyOptions {
16-
failOnNotFound?: boolean
17-
}
18-
1915
/**
2016
* @param candidate Chain key or chain slug (from config extension) or chain number. Defaults
2117
* to the default chain key (currently 'polygon').
2218
*/
23-
export function getChainKey(
24-
candidate: string | number,
25-
{ failOnNotFound = false }: GetChainKeyOptions = {},
26-
): ChainKey {
19+
export function getChainKey(candidate: string | number): ChainKey {
2720
const key = typeof candidate === 'number' ? candidate : candidate.toLowerCase()
2821

2922
if (lowerCasedChainKeyToChainKeyMap[key] === null) {
30-
if (failOnNotFound) {
31-
throw new Error('ChainKey not found')
32-
}
33-
3423
return defaultChainKey
3524
}
3625

@@ -74,10 +63,6 @@ export function getChainKey(
7463

7564
lowerCasedChainKeyToChainKeyMap[key] = null
7665

77-
if (failOnNotFound) {
78-
throw new Error('ChainKey not found')
79-
}
80-
8166
return defaultChainKey
8267
}
8368

@@ -186,6 +171,10 @@ export function isChainKey(candidate: string): candidate is ChainKey {
186171
return Object.prototype.hasOwnProperty.call(configs, candidate)
187172
}
188173

174+
export function isKnownChainId(candidate: number): boolean {
175+
return Object.entries(configs).some(([, { id }]) => id === candidate)
176+
}
177+
189178
export function getChainDisplayName(chainIdOrChainKey: ChainKey | number): string {
190179
const { config, configExtension } = getChainEntry(getChainKey(chainIdOrChainKey))
191180

src/utils/networkPreflight.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ export default async function networkPreflight(expectedChainId: number) {
1414
const provider = await getWalletProvider()
1515

1616
try {
17-
const currentChainId = await getChainId()
17+
const actualChainId = await getChainId()
1818

19-
if (currentChainId === expectedChainId) {
19+
if (actualChainId === expectedChainId) {
2020
return false
2121
}
2222

2323
await toaster(SwitchNetworkModal, Layer.Modal).pop({
24-
expectedNetwork: expectedChainId,
25-
actualNetwork: currentChainId,
24+
expectedChainId,
25+
actualChainId,
2626
})
2727

2828
await provider.request({

0 commit comments

Comments
 (0)