Skip to content

Commit

Permalink
show wrong chain message instead of throwing on incorrect chain conne…
Browse files Browse the repository at this point in the history
…ction (#3153)
  • Loading branch information
JFrankfurt authored Jan 19, 2022
1 parent 1efda07 commit 5dec0cf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
11 changes: 1 addition & 10 deletions src/lib/components/Error/ErrorGenerator.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ALL_SUPPORTED_CHAIN_IDS } from 'constants/chains'
import { ChainIdError, IntegrationError } from 'lib/errors'
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
import { IntegrationError } from 'lib/errors'
import { SwapWidgetProps } from 'lib/index'
import { useEffect } from 'react'

Expand All @@ -14,13 +12,6 @@ export default function ErrorGenerator(swapWidgetProps: SwapWidgetProps) {
}
}, [provider, jsonRpcEndpoint])

const { chainId } = useActiveWeb3React()
useEffect(() => {
if (chainId && !ALL_SUPPORTED_CHAIN_IDS.includes(chainId)) {
throw new ChainIdError('Switch to a network supported by the Uniswap Protocol.')
}
}, [chainId])

// size constraints
const { width } = swapWidgetProps
useEffect(() => {
Expand Down
13 changes: 12 additions & 1 deletion src/lib/components/Swap/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Trans } from '@lingui/macro'
import { ALL_SUPPORTED_CHAIN_IDS } from 'constants/chains'
import { useAtomValue } from 'jotai/utils'
import useActiveWeb3React from 'lib/hooks/useActiveWeb3React'
import { AlertTriangle, Info, largeIconCss, Spinner } from 'lib/icons'
import { Field, Input, inputAtom, outputAtom, stateAtom, swapAtom } from 'lib/state/swap'
import styled, { ThemedText, ThemeProvider } from 'lib/theme'
Expand Down Expand Up @@ -65,6 +67,7 @@ export default function Toolbar({ disabled }: { disabled?: boolean }) {
const input = useAtomValue(inputAtom)
const output = useAtomValue(outputAtom)
const balance = mockBalance
const { chainId } = useActiveWeb3React()

const caption = useMemo(() => {
const filledInput = asFilledInput(input)
Expand All @@ -77,6 +80,14 @@ export default function Toolbar({ disabled }: { disabled?: boolean }) {
</>
)
}
if (chainId && !ALL_SUPPORTED_CHAIN_IDS.includes(chainId)) {
return (
<>
<AlertTriangle color="secondary" />
<Trans>Unsupported network&#8211;switch to another to trade.</Trans>
</>
)
}
if (activeInput === Field.INPUT ? filledInput && output.token : filledOutput && input.token) {
if (!swap) {
return (
Expand Down Expand Up @@ -109,7 +120,7 @@ export default function Toolbar({ disabled }: { disabled?: boolean }) {
<Trans>Enter an amount</Trans>
</>
)
}, [activeInput, balance, disabled, input, output, swap])
}, [activeInput, balance, chainId, disabled, input, output, swap])

return (
<>
Expand Down
7 changes: 0 additions & 7 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,3 @@ export class IntegrationError extends Error {
this.name = 'Integration Error'
}
}

export class ChainIdError extends Error {
constructor(message: string) {
super(message)
this.name = 'Unsupported network'
}
}

0 comments on commit 5dec0cf

Please sign in to comment.