Skip to content

Commit

Permalink
upgrade to walletconnect v1 (#903)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister authored Jun 23, 2020
1 parent a5ff3be commit 678cd1a
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 136 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@web3-react/fortmatic-connector": "^6.0.9",
"@web3-react/injected-connector": "^6.0.7",
"@web3-react/portis-connector": "^6.0.9",
"@web3-react/walletconnect-connector": "^6.0.9",
"@web3-react/walletconnect-connector": "^6.1.1",
"@web3-react/walletlink-connector": "^6.0.9",
"copy-to-clipboard": "^3.2.0",
"cross-env": "^7.0.2",
Expand Down
5 changes: 2 additions & 3 deletions src/components/WalletModal/Option.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const InfoCard = styled.button<{ active?: boolean }>`
border-color: ${({ theme, active }) => (active ? 'transparent' : theme.bg3)};
`

const OptionCard = styled(InfoCard)`
const OptionCard = styled(InfoCard as any)`
display: flex;
flex-direction: row;
align-items: center;
Expand All @@ -30,7 +30,7 @@ const OptionCardLeft = styled.div`
height: 100%;
`

const OptionCardClickable = styled(OptionCard)<{ clickable?: boolean }>`
const OptionCardClickable = styled(OptionCard as any)<{ clickable?: boolean }>`
margin-top: 0;
&:hover {
cursor: ${({ clickable }) => (clickable ? 'pointer' : '')};
Expand Down Expand Up @@ -114,7 +114,6 @@ export default function Option({
<OptionCardClickable id={id} onClick={onClick} clickable={clickable && !active} active={active}>
<OptionCardLeft>
<HeaderText color={color}>
{' '}
{active ? (
<CircleWrapper>
<GreenCircle>
Expand Down
18 changes: 6 additions & 12 deletions src/components/WalletModal/PendingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import React from 'react'
import styled from 'styled-components'
import Option from './Option'
import { SUPPORTED_WALLETS } from '../../constants'
import WalletConnectData from './WalletConnectData'
import { walletconnect, injected } from '../../connectors'
import { injected } from '../../connectors'
import { darken } from 'polished'
import Loader from '../Loader'

Expand Down Expand Up @@ -65,28 +64,22 @@ const LoadingWrapper = styled.div`
`

export default function PendingView({
uri = '',
size,
connector,
error = false,
setPendingError,
tryActivation
}: {
uri?: string
size?: number
connector?: AbstractConnector
error?: boolean
setPendingError: (error: boolean) => void
tryActivation: (connector: AbstractConnector) => void
}) {
const isMetamask = window.ethereum && window.ethereum.isMetaMask
const isMetamask = window?.ethereum?.isMetaMask

return (
<PendingSection>
{!error && connector === walletconnect && <WalletConnectData size={size} uri={uri} />}
<LoadingMessage error={error}>
<LoadingWrapper>
{!error && <StyledLoader />}
{error ? (
<ErrorGroup>
<div>Error connecting.</div>
Expand All @@ -99,10 +92,11 @@ export default function PendingView({
Try Again
</ErrorButton>
</ErrorGroup>
) : connector === walletconnect ? (
'Scan QR code with a compatible wallet...'
) : (
'Initializing...'
<>
<StyledLoader />
Initializing...
</>
)}
</LoadingWrapper>
</LoadingMessage>
Expand Down
24 changes: 0 additions & 24 deletions src/components/WalletModal/WalletConnectData.tsx

This file was deleted.

25 changes: 8 additions & 17 deletions src/components/WalletModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ReactGA from 'react-ga'
import styled from 'styled-components'
import { isMobile } from 'react-device-detect'
import { UnsupportedChainIdError, useWeb3React } from '@web3-react/core'
import { URI_AVAILABLE } from '@web3-react/walletconnect-connector'
import usePrevious from '../../hooks/usePrevious'
import { useWalletModalOpen, useWalletModalToggle } from '../../state/application/hooks'

Expand All @@ -15,8 +14,9 @@ import { SUPPORTED_WALLETS } from '../../constants'
import { ExternalLink } from '../../theme'
import MetamaskIcon from '../../assets/images/metamask.png'
import { ReactComponent as Close } from '../../assets/images/x.svg'
import { injected, walletconnect, fortmatic, portis } from '../../connectors'
import { injected, fortmatic, portis } from '../../connectors'
import { OVERLAY_READY } from '../../connectors/Fortmatic'
import { WalletConnectConnector } from '@web3-react/walletconnect-connector'

const CloseIcon = styled.div`
position: absolute;
Expand Down Expand Up @@ -152,19 +152,6 @@ export default function WalletModal({
}
}, [walletModalOpen])

// set up uri listener for walletconnect
const [uri, setUri] = useState()
useEffect(() => {
const activateWC = uri => {
setUri(uri)
// setWalletView(WALLET_VIEWS.PENDING)
}
walletconnect.on(URI_AVAILABLE, activateWC)
return () => {
walletconnect.off(URI_AVAILABLE, activateWC)
}
}, [])

// close modal when a connection is successful
const activePrevious = usePrevious(active)
const connectorPrevious = usePrevious(connector)
Expand All @@ -190,6 +177,12 @@ export default function WalletModal({
})
setPendingWallet(connector) // set wallet for pending view
setWalletView(WALLET_VIEWS.PENDING)

// if the connector is walletconnect and the user has already tried to connect, manually reset the connector
if (connector instanceof WalletConnectConnector && connector.walletConnectProvider?.wc?.uri) {
connector.walletConnectProvider = undefined
}

activate(connector, undefined, true).catch(error => {
if (error instanceof UnsupportedChainIdError) {
activate(connector) // a little janky...can't use setError because the connector isn't set
Expand Down Expand Up @@ -345,8 +338,6 @@ export default function WalletModal({
<ContentWrapper>
{walletView === WALLET_VIEWS.PENDING ? (
<PendingView
uri={uri}
size={220}
connector={pendingWallet}
error={pendingError}
setPendingError={setPendingError}
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const injected = new InjectedConnector({
export const walletconnect = new WalletConnectConnector({
rpc: { 1: NETWORK_URL },
bridge: 'https://bridge.walletconnect.org',
qrcode: false,
qrcode: true,
pollingInterval: POLLING_INTERVAL
})

Expand Down
9 changes: 5 additions & 4 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const DUMMY_PAIRS_TO_PIN: { readonly [chainId in ChainId]?: Pair[] } = {
]
}

const MAINNET_WALLETS = {
const TESTNET_CAPABLE_WALLETS = {
INJECTED: {
connector: injected,
name: 'Injected',
Expand All @@ -80,17 +80,18 @@ const MAINNET_WALLETS = {

export const SUPPORTED_WALLETS =
process.env.REACT_APP_CHAIN_ID !== '1'
? MAINNET_WALLETS
? TESTNET_CAPABLE_WALLETS
: {
...MAINNET_WALLETS,
...TESTNET_CAPABLE_WALLETS,
...{
WALLET_CONNECT: {
connector: walletconnect,
name: 'WalletConnect',
iconName: 'walletConnectIcon.svg',
description: 'Connect to Trust Wallet, Rainbow Wallet and more...',
href: null,
color: '#4196FC'
color: '#4196FC',
mobile: true
},
WALLET_LINK: {
connector: walletlink,
Expand Down
Loading

1 comment on commit 678cd1a

@vercel
Copy link

@vercel vercel bot commented on 678cd1a Jun 23, 2020

Choose a reason for hiding this comment

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

Please sign in to comment.