Skip to content

Commit

Permalink
fix: only switch chain once (#219)
Browse files Browse the repository at this point in the history
* fix: do not flood chain switching

* fix: message

* test: fix rerender
  • Loading branch information
zzmp authored Sep 30, 2022
1 parent 1a352c6 commit 5699cd5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
11 changes: 6 additions & 5 deletions e2e/connect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,23 @@ describe('connect', () => {
})

describe('with provider', () => {
const HARDHAT_ACCOUNT_DISPLAY_STRING = `${hardhat.account.address?.substring(
0,
6
)}...${hardhat.account.address?.substring(hardhat.account.address.length - 4)}`

// The real hardhat.provider relies on real timeouts when providing data.
jest.useRealTimers()

const renderWidget = () => render(<SwapWidget provider={hardhat.provider} />)

it('displays connected account chip', async () => {
const HARDHAT_ACCOUNT_DISPLAY_STRING = `${hardhat.account.address?.substring(
0,
6
)}...${hardhat.account.address?.substring(hardhat.account.address.length - 4)}`

const widget = renderWidget()
const toolbar = await widget.findByTestId('toolbar')
// The toolbar will reflect a pending connection until it connects.
await waitFor(() => expect(toolbar.textContent).not.toBe('Connecting…'), { timeout: 10000 })

widget.rerender(<SwapWidget provider={hardhat.provider} />)
const account = await widget.findByTestId('account')
await waitFor(() => expect(account.textContent?.toLowerCase()).toBe(HARDHAT_ACCOUNT_DISPLAY_STRING))
})
Expand Down
17 changes: 10 additions & 7 deletions src/components/Swap/SwapActionButton/SwitchChainButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { Trans } from '@lingui/macro'
import { useWeb3React } from '@web3-react/core'
import ActionButton from 'components/ActionButton'
import useSwitchChain from 'hooks/useSwitchChain'
import useConnectors from 'hooks/web3/useConnectors'
import { Spinner } from 'icons'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Colors } from 'theme'

/** A chain-switching ActionButton. */
export default function ChainSwitchButton({ color, chainId }: { color: keyof Colors; chainId: number }) {
const { account } = useWeb3React()
const [isPending, setIsPending] = useState(!account)
const { connector } = useWeb3React()
const isNetwork = connector === useConnectors().network
const [isPending, setIsPending] = useState(false)

const switchChain = useSwitchChain()
const [error, setError] = useState()
Expand All @@ -25,24 +27,25 @@ export default function ChainSwitchButton({ color, chainId }: { color: keyof Col
}, [chainId, switchChain])
if (error) throw error

// If there is no account (ie no wallet to take agency), switch chains automatically
useEffect(() => {
if (!account) onSwitchChain()
}, [account, onSwitchChain])
if (isNetwork && !isPending) {
onSwitchChain()
}
}, [isNetwork, isPending, onSwitchChain])

const actionProps = useMemo(
() =>
isPending
? {
message: account ? <Trans>Switch network in your wallet</Trans> : <Trans>Switching network</Trans>,
message: isNetwork ? <Trans>Switching network</Trans> : <Trans>Switch network in your wallet</Trans>,
icon: Spinner,
}
: {
message: <Trans>Switch network</Trans>,
onClick: onSwitchChain,
children: <Trans>Switch</Trans>,
},
[account, isPending, onSwitchChain]
[isNetwork, isPending, onSwitchChain]
)

return <ActionButton color={color} action={actionProps} />
Expand Down

1 comment on commit 5699cd5

@vercel
Copy link

@vercel vercel bot commented on 5699cd5 Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

widgets – ./

widgets-git-main-uniswap.vercel.app
widgets-uniswap.vercel.app
widgets-seven-tau.vercel.app

Please sign in to comment.