-
Notifications
You must be signed in to change notification settings - Fork 203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: don't require wallet connection #1452
base: master
Are you sure you want to change the base?
Changes from 8 commits
f9b3eb5
5647bc7
ac2904c
761f76b
8983c6c
7fc643a
83ff1e0
fd27e67
6c5f22f
9024c9f
ffdaa64
7afe65c
b7e88e2
a4b42b9
4d41c70
31ca518
b09541e
2bc8d60
70009f6
0cfd82b
8308443
3559b13
8da44c8
372d448
eacaf21
cac297d
4d1d3a0
ccaa45b
ad56e66
bd63a10
d487132
17fcc0e
5963c24
185f985
cff40a4
1fb780b
9c6386f
fa6956b
a3e5a46
b081628
b155e33
9e2cbf5
c4c2ac4
34e738f
e3899dc
4a26749
6c8a094
628b2df
1e0bca7
01440ca
b919b1a
4410acf
58aa6ec
706aea6
a46e804
1cb8915
3ae13c1
f1f8cd4
bb03868
7b8b0da
9434492
82be657
24d4540
cf2fe84
ffaaed9
ec99753
38ab23e
692932b
5711ed3
abdb15e
aad2762
c4f6b11
23d11b0
9e6d9cd
b3e758b
e5f7919
89f1dae
fd7e13b
3b1e1ce
4bbabfb
d16ba20
660868c
43f81d3
5c15c73
cd4de57
b3de47e
be282e0
efe9ccf
6367f2b
8364d25
a79bf54
9417a00
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,7 @@ | ||
import React, { useEffect, useMemo, useState } from 'react' | ||
import * as Sentry from '@sentry/react' | ||
|
||
import { useAccount, useNetwork, WagmiConfig } from 'wagmi' | ||
import { | ||
darkTheme, | ||
RainbowKitProvider, | ||
Theme, | ||
useConnectModal | ||
} from '@rainbow-me/rainbowkit' | ||
import { useAccount, WagmiConfig } from 'wagmi' | ||
import { darkTheme, RainbowKitProvider, Theme } from '@rainbow-me/rainbowkit' | ||
import merge from 'lodash-es/merge' | ||
import axios from 'axios' | ||
import { createOvermind, Overmind } from 'overmind' | ||
|
@@ -32,21 +26,17 @@ import { | |
} from '../common/Header' | ||
import { HeaderAccountPopover } from '../common/HeaderAccountPopover' | ||
import { Notifications } from '../common/Notifications' | ||
import { isNetwork, rpcURLs } from '../../util/networks' | ||
import { | ||
ArbQueryParamProvider, | ||
useArbQueryParams | ||
} from '../../hooks/useArbQueryParams' | ||
import { isNetwork } from '../../util/networks' | ||
import { ArbQueryParamProvider } from '../../hooks/useArbQueryParams' | ||
import { GET_HELP_LINK, TOS_LOCALSTORAGE_KEY } from '../../constants' | ||
import { getProps } from '../../util/wagmi/setup' | ||
import { useAccountIsBlocked } from '../../hooks/useAccountIsBlocked' | ||
import { useCCTPIsBlocked } from '../../hooks/CCTP/useCCTPIsBlocked' | ||
import { useNativeCurrency } from '../../hooks/useNativeCurrency' | ||
import { sanitizeQueryParams, useNetworks } from '../../hooks/useNetworks' | ||
import { useNetworks } from '../../hooks/useNetworks' | ||
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship' | ||
import { HeaderConnectWalletButton } from '../common/HeaderConnectWalletButton' | ||
import { AppConnectionFallbackContainer } from './AppConnectionFallbackContainer' | ||
import { ProviderName, trackEvent } from '../../util/AnalyticsUtils' | ||
import { useSyncConnectedChainToAnalytics } from './useSyncConnectedChainToAnalytics' | ||
import { useSyncConnectedChainToQueryParams } from './useSyncConnectedChainToQueryParams' | ||
|
||
declare global { | ||
interface Window { | ||
|
@@ -64,6 +54,7 @@ const rainbowkitTheme = merge(darkTheme(), { | |
} as Theme) | ||
|
||
const AppContent = (): JSX.Element => { | ||
const { isConnected } = useAccount() | ||
const [{ sourceChain }] = useNetworks() | ||
const { | ||
app: { connectionState } | ||
|
@@ -105,9 +96,7 @@ const AppContent = (): JSX.Element => { | |
<> | ||
<HeaderOverrides {...headerOverridesProps} /> | ||
|
||
<HeaderContent> | ||
<HeaderAccountPopover /> | ||
</HeaderContent> | ||
<HeaderContent>{isConnected && <HeaderAccountPopover />}</HeaderContent> | ||
|
||
<TokenListSyncer /> | ||
<BalanceUpdater /> | ||
|
@@ -131,6 +120,9 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => { | |
// We want to be sure this fetch is completed by the time we open the USDC modals | ||
useCCTPIsBlocked() | ||
|
||
useSyncConnectedChainToAnalytics() | ||
useSyncConnectedChainToQueryParams() | ||
|
||
const [tokenBridgeParams, setTokenBridgeParams] = | ||
useState<TokenBridgeParams | null>(null) | ||
|
||
|
@@ -248,92 +240,6 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => { | |
) | ||
} | ||
|
||
// connector names: https://github.com/wagmi-dev/wagmi/blob/b17c07443e407a695dfe9beced2148923b159315/docs/pages/core/connectors/_meta.en-US.json#L4 | ||
function getWalletName(connectorName: string): ProviderName { | ||
switch (connectorName) { | ||
case 'MetaMask': | ||
case 'Coinbase Wallet': | ||
case 'Trust Wallet': | ||
case 'Safe': | ||
case 'Injected': | ||
case 'Ledger': | ||
return connectorName | ||
|
||
case 'WalletConnectLegacy': | ||
case 'WalletConnect': | ||
return 'WalletConnect' | ||
|
||
default: | ||
return 'Other' | ||
} | ||
} | ||
|
||
/** given our RPC url, sanitize it before logging to Sentry, to only pass the url and not the keys */ | ||
function getBaseUrl(url: string) { | ||
try { | ||
const urlObject = new URL(url) | ||
return `${urlObject.protocol}//${urlObject.hostname}` | ||
} catch { | ||
// if invalid url passed | ||
return '' | ||
} | ||
} | ||
|
||
function NetworkReady({ children }: { children: React.ReactNode }) { | ||
const [networks] = useNetworks() | ||
const { parentChain, childChain } = useNetworksRelationship(networks) | ||
const { isConnected, connector } = useAccount() | ||
const [tosAccepted] = useLocalStorage<string>(TOS_LOCALSTORAGE_KEY) | ||
const { openConnectModal } = useConnectModal() | ||
|
||
useEffect(() => { | ||
if (tosAccepted !== undefined && !isConnected) { | ||
openConnectModal?.() | ||
} | ||
}, [isConnected, tosAccepted, openConnectModal]) | ||
|
||
useEffect(() => { | ||
if (isConnected && connector) { | ||
const walletName = getWalletName(connector.name) | ||
trackEvent('Connect Wallet Click', { walletName }) | ||
} | ||
|
||
// set a custom tag in sentry to filter issues by connected wallet.name | ||
Sentry.setTag('wallet.name', connector?.name ?? '') | ||
}, [isConnected, connector]) | ||
|
||
useEffect(() => { | ||
Sentry.setTag('network.parent_chain_id', parentChain.id) | ||
Sentry.setTag( | ||
'network.parent_chain_rpc_url', | ||
getBaseUrl(rpcURLs[parentChain.id] ?? '') | ||
) | ||
Sentry.setTag('network.child_chain_id', childChain.id) | ||
Sentry.setTag( | ||
'network.child_chain_rpc_url', | ||
getBaseUrl(rpcURLs[childChain.id] ?? '') | ||
) | ||
}, [childChain.id, parentChain.id]) | ||
|
||
if (!isConnected) { | ||
return ( | ||
<> | ||
<HeaderContent> | ||
<HeaderConnectWalletButton /> | ||
</HeaderContent> | ||
|
||
<AppConnectionFallbackContainer> | ||
<span className="text-white"> | ||
Please connect your wallet to use the bridge. | ||
</span> | ||
</AppConnectionFallbackContainer> | ||
</> | ||
) | ||
} | ||
|
||
return <>{children}</> | ||
} | ||
|
||
// We're doing this as a workaround so users can select their preferred chain on WalletConnect. | ||
// | ||
// https://github.com/orgs/WalletConnect/discussions/2733 | ||
|
@@ -352,47 +258,6 @@ Object.keys(localStorage).forEach(key => { | |
} | ||
}) | ||
|
||
function ConnectedChainSyncer() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extracted into |
||
const [shouldSync, setShouldSync] = useState(false) | ||
const [didSync, setDidSync] = useState(false) | ||
|
||
const [{ sourceChain, destinationChain }, setQueryParams] = | ||
useArbQueryParams() | ||
const { chain } = useNetwork() | ||
|
||
useEffect(() => { | ||
if (shouldSync) { | ||
return | ||
} | ||
|
||
// Only sync connected chain to query params if the query params were not initially provided | ||
if ( | ||
typeof sourceChain === 'undefined' && | ||
typeof destinationChain === 'undefined' | ||
) { | ||
setShouldSync(true) | ||
} | ||
}, [shouldSync, sourceChain, destinationChain]) | ||
|
||
useEffect(() => { | ||
// When the chain is connected and we should sync, and we haven't synced yet, sync the connected chain to the query params | ||
if (chain && shouldSync && !didSync) { | ||
const { | ||
sourceChainId: sourceChain, | ||
destinationChainId: destinationChain | ||
} = sanitizeQueryParams({ | ||
sourceChainId: chain.id, | ||
destinationChainId: undefined | ||
}) | ||
|
||
setQueryParams({ sourceChain, destinationChain }) | ||
setDidSync(true) | ||
} | ||
}, [chain, shouldSync, didSync, setQueryParams]) | ||
|
||
return null | ||
} | ||
|
||
export default function App() { | ||
const [overmind] = useState<Overmind<typeof config>>(createOvermind(config)) | ||
const [tosAccepted, setTosAccepted] = | ||
|
@@ -423,13 +288,10 @@ export default function App() { | |
theme={rainbowkitTheme} | ||
{...rainbowKitProviderProps} | ||
> | ||
<ConnectedChainSyncer /> | ||
<WelcomeDialog {...welcomeDialogProps} onClose={onClose} /> | ||
<NetworkReady> | ||
<AppContextProvider> | ||
<Injector>{isTosAccepted && <AppContent />}</Injector> | ||
</AppContextProvider> | ||
</NetworkReady> | ||
<AppContextProvider> | ||
<Injector>{isTosAccepted && <AppContent />}</Injector> | ||
</AppContextProvider> | ||
</RainbowKitProvider> | ||
</WagmiConfig> | ||
</ArbQueryParamProvider> | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useEffect } from 'react' | ||
import { useAccount } from 'wagmi' | ||
import * as Sentry from '@sentry/react' | ||
|
||
import { useNetworks } from '../../hooks/useNetworks' | ||
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship' | ||
import { ProviderName, trackEvent } from '../../util/AnalyticsUtils' | ||
import { rpcURLs } from '../../util/networks' | ||
|
||
// connector names: https://github.com/wagmi-dev/wagmi/blob/b17c07443e407a695dfe9beced2148923b159315/docs/pages/core/connectors/_meta.en-US.json#L4 | ||
function getWalletName(connectorName: string): ProviderName { | ||
switch (connectorName) { | ||
case 'MetaMask': | ||
case 'Coinbase Wallet': | ||
case 'Trust Wallet': | ||
case 'Safe': | ||
case 'Injected': | ||
case 'Ledger': | ||
return connectorName | ||
|
||
case 'WalletConnectLegacy': | ||
case 'WalletConnect': | ||
return 'WalletConnect' | ||
|
||
default: | ||
return 'Other' | ||
} | ||
} | ||
|
||
/** given our RPC url, sanitize it before logging to Sentry, to only pass the url and not the keys */ | ||
function getBaseUrl(url: string | undefined): string | null { | ||
if (typeof url === 'undefined') { | ||
return null | ||
} | ||
|
||
try { | ||
const urlObject = new URL(url) | ||
return `${urlObject.protocol}//${urlObject.hostname}` | ||
} catch { | ||
// if invalid url passed | ||
return null | ||
} | ||
} | ||
|
||
export function useSyncConnectedChainToAnalytics() { | ||
const { isConnected, connector } = useAccount() | ||
|
||
const [networks] = useNetworks() | ||
const { parentChain, childChain } = useNetworksRelationship(networks) | ||
|
||
useEffect(() => { | ||
if (isConnected && connector) { | ||
const walletName = getWalletName(connector.name) | ||
trackEvent('Connect Wallet Click', { walletName }) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know it's coming exactly from the previous file, but wouldn't this fire on every-refresh? Ideally we want it to fire only when we connect to UI using the connect wallet button. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// set a custom tag in sentry to filter issues by connected wallet.name | ||
Sentry.setTag('wallet.name', walletName) | ||
} | ||
}, [isConnected, connector]) | ||
|
||
useEffect(() => { | ||
Sentry.setTag('network.parent_chain_id', parentChain.id) | ||
Sentry.setTag('network.child_chain_id', childChain.id) | ||
|
||
const parentChainRpcUrl = getBaseUrl(rpcURLs[parentChain.id]) | ||
const childChainRpcUrl = getBaseUrl(rpcURLs[childChain.id]) | ||
|
||
if (parentChainRpcUrl) { | ||
Sentry.setTag('network.parent_chain_rpc_url', parentChainRpcUrl) | ||
} | ||
|
||
if (childChainRpcUrl) { | ||
Sentry.setTag('network.child_chain_rpc_url', childChainRpcUrl) | ||
} | ||
}, [childChain.id, parentChain.id]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useState, useEffect } from 'react' | ||
import { useNetwork } from 'wagmi' | ||
|
||
import { useArbQueryParams } from '../../hooks/useArbQueryParams' | ||
import { sanitizeQueryParams } from '../../hooks/useNetworks' | ||
|
||
export function useSyncConnectedChainToQueryParams() { | ||
const [shouldSync, setShouldSync] = useState(false) | ||
const [didSync, setDidSync] = useState(false) | ||
|
||
const { chain } = useNetwork() | ||
const [{ sourceChain, destinationChain }, setQueryParams] = | ||
useArbQueryParams() | ||
|
||
useEffect(() => { | ||
if (shouldSync) { | ||
return | ||
} | ||
|
||
// Only sync connected chain to query params if the query params were not initially provided | ||
if ( | ||
typeof sourceChain === 'undefined' && | ||
typeof destinationChain === 'undefined' | ||
) { | ||
setShouldSync(true) | ||
} | ||
}, [shouldSync, sourceChain, destinationChain]) | ||
|
||
useEffect(() => { | ||
// When the chain is connected and we should sync, and we haven't synced yet, sync the connected chain to the query params | ||
if (chain && shouldSync && !didSync) { | ||
const { | ||
sourceChainId: sourceChain, | ||
destinationChainId: destinationChain | ||
} = sanitizeQueryParams({ | ||
sourceChainId: chain.id, | ||
destinationChainId: undefined | ||
}) | ||
|
||
setQueryParams({ sourceChain, destinationChain }) | ||
setDidSync(true) | ||
} | ||
}, [chain, shouldSync, didSync, setQueryParams]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extracted these into
useSyncConnectedChainToAnalytics