Skip to content
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

refactor: Remove useArbTokenBridge from store #1479

Closed
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1a053aa
Remove useArbTokenBridge from store
chrstph-dvx Jan 26, 2024
0e08dfd
Update syncers
chrstph-dvx Jan 26, 2024
14f7d20
Add useCallback to useArbTokenBridge, move addBridgeTokenListToBridge
chrstph-dvx Jan 26, 2024
955547e
Memoize useTransactions
chrstph-dvx Jan 26, 2024
2d39062
Fix typing
chrstph-dvx Jan 26, 2024
4ad3bb5
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Jan 26, 2024
7dc7d8c
Reset useArbTokenBridge
chrstph-dvx Jan 29, 2024
4cf85e2
Add shared zustand store for bridgeTokens
chrstph-dvx Jan 29, 2024
c02f0e5
Replace params with useNetworks in useArbTokenBridge
chrstph-dvx Jan 29, 2024
a96c5a6
Add addBridgeTokenListToBridge to useArbTokenBridge
chrstph-dvx Jan 29, 2024
d2e4846
Move updateTokenData
chrstph-dvx Jan 29, 2024
1ffeabb
Move addToken
chrstph-dvx Jan 29, 2024
65583ee
Move addToExecutedMessagesCache
chrstph-dvx Jan 29, 2024
b87dc6f
Add useCallback for updateEthBalances
chrstph-dvx Jan 29, 2024
b4da51b
Add useCallback for useArbTokenBridge functions
chrstph-dvx Jan 29, 2024
207c6f9
Memoize returned object in useArbTokenBridge
chrstph-dvx Jan 29, 2024
8cef344
Remove unused import
chrstph-dvx Jan 29, 2024
aa1b575
Rework call to useArbTokenBridge
chrstph-dvx Jan 29, 2024
f2c7c82
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Jan 30, 2024
59f56e4
Keep track of source/destination chain in state
chrstph-dvx Jan 30, 2024
5354099
Fix warning with missing autoresizer style
chrstph-dvx Jan 30, 2024
b6503cd
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Jan 31, 2024
130e603
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Feb 8, 2024
5576101
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Feb 8, 2024
dcb5145
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Feb 20, 2024
f96c6a7
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Feb 27, 2024
22b6448
Remove ledger from yarn.lock
chrstph-dvx Feb 27, 2024
96e9124
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Mar 20, 2024
0af6601
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Jul 30, 2024
8be9478
fix import dialog
fionnachan Jul 31, 2024
cfcadf2
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
fionnachan Jul 31, 2024
5df85c3
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Aug 1, 2024
801d145
Fix import token
chrstph-dvx Aug 1, 2024
38cf72a
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Aug 2, 2024
d8e12da
Add missing hooks in App
chrstph-dvx Aug 2, 2024
3bd8a45
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
fionnachan Aug 2, 2024
303d33d
Remove connectionState
chrstph-dvx Aug 2, 2024
ce1ade6
Remove unused import
chrstph-dvx Aug 2, 2024
b868e0e
Add lost code after merge
chrstph-dvx Aug 2, 2024
a99a910
Remove commented code
chrstph-dvx Aug 5, 2024
fe8b01a
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
fionnachan Aug 6, 2024
7dd835d
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Aug 14, 2024
24440ca
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Aug 14, 2024
d915ff1
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Aug 19, 2024
e52eebe
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
fionnachan Aug 30, 2024
396576e
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Sep 9, 2024
04c22f8
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Sep 9, 2024
d7a8593
Merge branch 'master' into 1432-use-usenetworksrelationship-inside-us…
chrstph-dvx Sep 9, 2024
30462e6
Remove extra token button
chrstph-dvx Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 17 additions & 36 deletions packages/arb-token-bridge-ui/src/components/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import merge from 'lodash-es/merge'
import axios from 'axios'
import { createOvermind, Overmind } from 'overmind'
import { Provider } from 'overmind-react'
import { useLocalStorage } from 'react-use'
import { useInterval, useLocalStorage } from 'react-use'
import { ConnectionState } from '../../util'
import { TokenBridgeParams } from '../../hooks/useArbTokenBridge'
import { WelcomeDialog } from './WelcomeDialog'
import { BlockedDialog } from './BlockedDialog'
import { AppContextProvider } from './AppContext'
import { config, useActions, useAppState } from '../../state'
import { Alert } from '../common/Alert'
import { MainContent } from '../MainContent/MainContent'
import { ArbTokenBridgeStoreSync } from '../syncers/ArbTokenBridgeStoreSync'
import { BalanceUpdater } from '../syncers/BalanceUpdater'
import { TokenListSyncer } from '../syncers/TokenListSyncer'
import { useDialog } from '../common/Dialog'
import {
Expand All @@ -47,6 +44,7 @@ import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { HeaderConnectWalletButton } from '../common/HeaderConnectWalletButton'
import { AppConnectionFallbackContainer } from './AppConnectionFallbackContainer'
import { ProviderName, trackEvent } from '../../util/AnalyticsUtils'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

declare global {
interface Window {
Expand All @@ -66,8 +64,17 @@ const rainbowkitTheme = merge(darkTheme(), {
const AppContent = (): JSX.Element => {
const [{ sourceChain }] = useNetworks()
const {
app: { connectionState }
app: { connectionState, selectedToken }
} = useAppState()
const {
token: { updateTokenData }
} = useArbTokenBridge()

useInterval(() => {
if (selectedToken) {
updateTokenData(selectedToken.address)
}
}, 10_000)

const headerOverridesProps: HeaderOverridesProps = useMemo(() => {
const { isTestnet, isGoerli } = isNetwork(sourceChain.id ?? 0)
Expand Down Expand Up @@ -110,7 +117,6 @@ const AppContent = (): JSX.Element => {
</HeaderContent>

<TokenListSyncer />
<BalanceUpdater />
<Notifications />
<MainContent />
</>
Expand All @@ -131,9 +137,6 @@ 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()

const [tokenBridgeParams, setTokenBridgeParams] =
useState<TokenBridgeParams | null>(null)

useEffect(() => {
if (!nativeCurrency.isCustom) {
return
Expand All @@ -152,12 +155,11 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
) {
actions.app.setSelectedToken(null)
}
}, [selectedToken, nativeCurrency])
}, [selectedToken, nativeCurrency, actions.app])

// Listen for account and network changes
useEffect(() => {
// Any time one of those changes
setTokenBridgeParams(null)
actions.app.setConnectionState(ConnectionState.LOADING)

if (!isConnected) {
Expand All @@ -173,10 +175,6 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
).isEthereumMainnetOrTestnet

actions.app.reset(networks.sourceChain.id)
actions.app.setChainIds({
l1NetworkChainId: parentChain.id,
l2NetworkChainId: childChain.id
fionnachan marked this conversation as resolved.
Show resolved Hide resolved
})

if (
(isParentChainEthereum && isConnectedToArbitrum) ||
Expand All @@ -188,17 +186,6 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
console.info('Deposit mode detected:')
actions.app.setConnectionState(ConnectionState.L1_CONNECTED)
}

setTokenBridgeParams({
l1: {
network: parentChain,
provider: parentChainProvider
},
l2: {
network: childChain,
provider: childChainProvider
}
})
}, [
isConnected,
address,
Expand All @@ -208,7 +195,8 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
parentChain,
childChain,
parentChainProvider,
childChainProvider
childChainProvider,
actions.app
])

useEffect(() => {
Expand All @@ -222,7 +210,7 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
.catch(err => {
console.warn('Failed to fetch warning tokens:', err)
})
}, [])
}, [actions.app])

if (address && isBlocked) {
return (
Expand All @@ -238,14 +226,7 @@ const Injector = ({ children }: { children: React.ReactNode }): JSX.Element => {
)
}

return (
<>
{tokenBridgeParams && (
<ArbTokenBridgeStoreSync tokenBridgeParams={tokenBridgeParams} />
)}
{children}
</>
)
return <>{children}</>
}

// connector names: https://github.com/wagmi-dev/wagmi/blob/b17c07443e407a695dfe9beced2148923b159315/docs/pages/core/connectors/_meta.en-US.json#L4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ import { sanitizeTokenSymbol } from '../../util/TokenUtils'
import { useNativeCurrency } from '../../hooks/useNativeCurrency'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

export function TokenButton(): JSX.Element {
const {
app: {
selectedToken,
arbTokenBridge: { bridgeTokens },
arbTokenBridgeLoaded
}
app: { selectedToken }
} = useAppState()
const [networks] = useNetworks()
const { bridgeTokens } = useArbTokenBridge()
const { childChain, childChainProvider, parentChain, isDepositMode } =
useNetworksRelationship(networks)

Expand All @@ -29,9 +27,7 @@ export function TokenButton(): JSX.Element {
if (!selectedAddress) {
return nativeCurrency.logoUrl
}
if (!arbTokenBridgeLoaded) {
return undefined
}

if (typeof bridgeTokens === 'undefined') {
return undefined
}
Expand All @@ -40,12 +36,7 @@ export function TokenButton(): JSX.Element {
return sanitizeImageSrc(logo)
}
return undefined
}, [
nativeCurrency,
bridgeTokens,
selectedToken?.address,
arbTokenBridgeLoaded
])
}, [nativeCurrency, bridgeTokens, selectedToken?.address])
const chainId = isDepositMode ? parentChain.id : childChain.id

const tokenSymbol = useMemo(() => {
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

useAppState is now an unused import

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { isWithdrawOnlyToken } from '../../util/WithdrawOnlyUtils'
import { isTransferDisabledToken } from '../../util/TokenTransferDisabledUtils'
import { useTransferDisabledDialogStore } from './TransferDisabledDialog'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

enum ImportStatus {
LOADING,
Expand Down Expand Up @@ -72,11 +73,9 @@ export function TokenImportDialog({
}: TokenImportDialogProps): JSX.Element {
const { address: walletAddress } = useAccount()
const {
app: {
arbTokenBridge: { bridgeTokens, token },
selectedToken
}
app: { selectedToken }
} = useAppState()
const { bridgeTokens, token } = useArbTokenBridge()
const [networks] = useNetworks()
const {
childChain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { constants } from 'ethers'
import { Chain, useAccount } from 'wagmi'

import { Loader } from '../common/atoms/Loader'
import { useAppState } from '../../state'
import {
listIdsToNames,
SPECIAL_ARBITRUM_TOKEN_TOKEN_LIST_ID
Expand All @@ -33,6 +32,7 @@ import { useAccountType } from '../../hooks/useAccountType'
import { useNativeCurrency } from '../../hooks/useNativeCurrency'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

function tokenListIdsToNames(ids: number[]): string {
return ids
Expand Down Expand Up @@ -82,11 +82,7 @@ export function TokenRow({
token
}: TokenRowProps): JSX.Element {
const { address: walletAddress } = useAccount()
const {
app: {
arbTokenBridge: { bridgeTokens }
}
} = useAppState()
const { bridgeTokens } = useArbTokenBridge()
const { isLoading: isLoadingAccountType } = useAccountType()
const [networks] = useNetworks()
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import Image from 'next/image'
import { useAccount } from 'wagmi'

import { Loader } from '../common/atoms/Loader'
import { useActions, useAppState } from '../../state'
import { useActions } from '../../state'
import {
BRIDGE_TOKEN_LISTS,
BridgeTokenList,
addBridgeTokenListToBridge,
SPECIAL_ARBITRUM_TOKEN_TOKEN_LIST_ID
} from '../../util/TokenListUtils'
import {
Expand Down Expand Up @@ -44,6 +43,10 @@ import { useTransferDisabledDialogStore } from './TransferDisabledDialog'
import { isWithdrawOnlyToken } from '../../util/WithdrawOnlyUtils'
import { isTransferDisabledToken } from '../../util/TokenTransferDisabledUtils'
import { useTokenFromSearchParams } from './TransferPanelUtils'
import {
useArbTokenBridge,
useBridgeTokensStore
} from '../../hooks/useArbTokenBridge'

enum Panel {
TOKENS,
Expand All @@ -70,11 +73,11 @@ const ARB_SEPOLIA_NATIVE_USDC_TOKEN = {

function TokenListsPanel() {
const {
app: { arbTokenBridge }
} = useAppState()
token: { removeTokensFromList, addBridgeTokenListToBridge }
} = useArbTokenBridge()
const { bridgeTokens } = useBridgeTokensStore()
const [networks] = useNetworks()
const { childChain } = useNetworksRelationship(networks)
const { bridgeTokens, token } = arbTokenBridge

const listsToShow: BridgeTokenList[] = useMemo(() => {
return BRIDGE_TOKEN_LISTS.filter(tokenList => {
Expand All @@ -96,9 +99,9 @@ function TokenListsPanel() {
isActive: boolean
) => {
if (isActive) {
token.removeTokensFromList(bridgeTokenList.id)
removeTokensFromList(bridgeTokenList.id)
} else {
addBridgeTokenListToBridge(bridgeTokenList, arbTokenBridge)
addBridgeTokenListToBridge(bridgeTokenList)
}
}

Expand Down Expand Up @@ -153,11 +156,7 @@ function TokensPanel({
onTokenSelected: (token: ERC20BridgeToken | null) => void
}): JSX.Element {
const { address: walletAddress } = useAccount()
const {
app: {
arbTokenBridge: { token, bridgeTokens }
}
} = useAppState()
const { token, bridgeTokens } = useArbTokenBridge()
const [networks] = useNetworks()
const { childChain, childChainProvider, parentChainProvider, isDepositMode } =
useNetworksRelationship(networks)
Expand Down Expand Up @@ -472,11 +471,7 @@ function TokensPanel({

export function TokenSearch({ close }: { close: () => void }) {
const { address: walletAddress } = useAccount()
const {
app: {
arbTokenBridge: { token, bridgeTokens }
}
} = useAppState()
const { token, bridgeTokens } = useArbTokenBridge()
const {
app: { setSelectedToken }
} = useActions()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMemo } from 'react'
import { useAppState } from '../../state'
import {
ContractStorage,
ERC20BridgeToken,
Expand All @@ -9,6 +8,7 @@ import { useTokenLists } from '../../hooks/useTokenLists'
import { TokenListWithId } from '../../util/TokenListUtils'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { useNetworks } from '../../hooks/useNetworks'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

export function useTokensFromLists(): ContractStorage<ERC20BridgeToken> {
const [networks] = useNetworks()
Expand All @@ -25,11 +25,7 @@ export function useTokensFromLists(): ContractStorage<ERC20BridgeToken> {
}

export function useTokensFromUser(): ContractStorage<ERC20BridgeToken> {
const {
app: {
arbTokenBridge: { bridgeTokens }
}
} = useAppState()
const { bridgeTokens } = useArbTokenBridge()

return useMemo(() => {
const storage: ContractStorage<ERC20BridgeToken> = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { getBridgeUiConfigForChain } from '../../util/bridgeUiConfig'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { CctpTransferStarter } from '@/token-bridge-sdk/CctpTransferStarter'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

const isAllowedL2 = async ({
l1TokenAddress,
Expand Down Expand Up @@ -118,14 +119,9 @@ export function TransferPanel() {
const [showSCWalletTooltip, setShowSCWalletTooltip] = useState(false)

const {
app: {
connectionState,
Copy link
Member

Choose a reason for hiding this comment

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

i see that connectionState is not used in the whole app, let's nuke it from the code base

selectedToken,
arbTokenBridgeLoaded,
arbTokenBridge: { eth, token },
warningTokens
}
app: { connectionState, selectedToken, warningTokens }
} = useAppState()
const { eth, token } = useArbTokenBridge()
const { layout } = useAppContextState()
const { isTransferring } = layout
const { address: walletAddress, isConnected } = useAccount()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import {
} from './TransferDisabledDialog'
import { getBridgeUiConfigForChain } from '../../util/bridgeUiConfig'
import { useIsTestnetMode } from '../../hooks/useIsTestnetMode'
import { useArbTokenBridge } from '../../hooks/useArbTokenBridge'

enum NetworkType {
l1 = 'l1',
Expand Down Expand Up @@ -361,8 +362,8 @@ export function TransferPanelMain({

const { app } = useAppState()
const { address: walletAddress } = useAccount()
const { arbTokenBridge, selectedToken } = app
const { token } = arbTokenBridge
const { selectedToken } = app
const { token } = useArbTokenBridge()

const { destinationAddress, setDestinationAddress } =
useDestinationAddressStore()
Expand Down Expand Up @@ -853,6 +854,7 @@ export function TransferPanelMain({
networks.destinationChain,
isTestnetMode,
setNetworks,
actions.app,
Copy link
Member

Choose a reason for hiding this comment

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

would be better if we just kept this out so we don't introduce unnecessary rerenders

Copy link
Contributor Author

@chrstph-dvx chrstph-dvx Jan 29, 2024

Choose a reason for hiding this comment

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

actions.app is the same reference every time, it doesn't cause rerender

switchNetworksOnTransferPanel,
openOneNovaTransferDialog
])
Expand Down
Loading
Loading