-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix: cp-13.0.0 ensure we enable any missing networks when performing swap/bridge. #34376
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
Changes from all commits
db12945
977041e
c5fb3d3
b7d9452
cf588e3
953b6dc
05141ef
5fdc6e4
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ import { | |
| BRIDGE_DEFAULT_SLIPPAGE, | ||
| GenericQuoteRequest, | ||
| } from '@metamask/bridge-controller'; | ||
| import { Hex, parseCaipChainId } from '@metamask/utils'; | ||
| import { | ||
| setFromToken, | ||
| setFromTokenInputValue, | ||
|
|
@@ -82,6 +83,7 @@ import { | |
| } from '../../../helpers/constants/design-system'; | ||
| import { useI18nContext } from '../../../hooks/useI18nContext'; | ||
| import { useTokensWithFiltering } from '../../../hooks/bridge/useTokensWithFiltering'; | ||
| import { setEnabledNetworks } from '../../../store/actions'; | ||
| import { calcTokenValue } from '../../../../shared/lib/swaps-utils'; | ||
| import { | ||
| formatTokenAmount, | ||
|
|
@@ -102,6 +104,7 @@ import useLatestBalance from '../../../hooks/bridge/useLatestBalance'; | |
| import { useCountdownTimer } from '../../../hooks/bridge/useCountdownTimer'; | ||
| import { | ||
| getCurrentKeyring, | ||
| getEnabledNetworksByNamespace, | ||
| getSelectedEvmInternalAccount, | ||
| getSelectedInternalAccount, | ||
| getTokenList, | ||
|
|
@@ -128,14 +131,58 @@ import type { BridgeToken } from '../../../ducks/bridge/types'; | |
| import { toAssetId } from '../../../../shared/lib/asset-utils'; | ||
| import { getIsSmartTransaction } from '../../../../shared/modules/selectors'; | ||
| import { endTrace, TraceName } from '../../../../shared/lib/trace'; | ||
| import { FEATURED_NETWORK_CHAIN_IDS } from '../../../../shared/constants/network'; | ||
| import { useBridgeQueryParams } from '../../../hooks/bridge/useBridgeQueryParams'; | ||
| import useBridgeDefaultToToken from '../../../hooks/bridge/useBridgeDefaultToToken'; | ||
| import { BridgeInputGroup } from './bridge-input-group'; | ||
| import { BridgeCTAButton } from './bridge-cta-button'; | ||
| import { DestinationAccountPicker } from './components/destination-account-picker'; | ||
|
|
||
| /** | ||
| * Ensures that any missing network gets added to the NetworkEnabledMap (which handles network polling) | ||
| * | ||
| * @returns callback to enable a network config. | ||
| */ | ||
| export const useEnableMissingNetwork = () => { | ||
| const enabledNetworksByNamespace = useSelector(getEnabledNetworksByNamespace); | ||
| const dispatch = useDispatch(); | ||
|
|
||
| const enableMissingNetwork = useCallback( | ||
| (chainId: Hex) => { | ||
| const enabledNetworkKeys = Object.keys(enabledNetworksByNamespace ?? {}); | ||
|
|
||
| const caipChainId = formatChainIdToCaip(chainId); | ||
| const { namespace } = parseCaipChainId(caipChainId); | ||
|
|
||
| if (namespace) { | ||
| const isPopularNetwork = FEATURED_NETWORK_CHAIN_IDS.includes(chainId); | ||
|
Member
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. Should this enable all featured networks or just the user's imported chains that are also allowlisted by the bridge service (
Member
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. What are the other implications of not adding a network to the enabled list? I wonder if this should happen earlier on in the application loading process (maybe in the
Contributor
Author
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. Great Q, will ask the team and test it out.
Contributor
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. Another important implication is that if a network is selected as the source or destination in the swap flow, it must be enabled; otherwise, users won’t see their assets in the token list, leading to a poor user experience. |
||
|
|
||
| if (isPopularNetwork) { | ||
| const isNetworkEnabled = enabledNetworkKeys.includes(chainId); | ||
| if (!isNetworkEnabled) { | ||
| const enabledEvmNetworks = enabledNetworkKeys.filter((key) => | ||
| FEATURED_NETWORK_CHAIN_IDS.includes(key as Hex), | ||
| ); | ||
| const newNetworkEnabledEvmNetworks = [ | ||
| chainId, | ||
| ...enabledEvmNetworks, | ||
| ]; | ||
| dispatch( | ||
| setEnabledNetworks(newNetworkEnabledEvmNetworks, namespace), | ||
| ); | ||
|
Comment on lines
+170
to
+172
Contributor
Author
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. This is the special magic sauce... For send flows we set the enable network before switching to it.
Contributor
Author
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. It would be ideal that the NetworkOrderController only be responsible for the UI, but we are very closely coupled with the the NetworkController & the selected network. For example, all of our polling mechanisms rely on the NetworkOrderController, since we are polling on networks a user has toggled.
Contributor
Author
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. Cleaned this up - it now only enabled missing networks, it does not move to active network.
Member
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. Re: enabling a network before we switch to it There are a number of instances where the network is switched before routing to the bridge page (see
Contributor
Author
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. CC @salimtb for visibility on this. Longer term I would really like to decouple the need for bridging/swaps/send to rely on this NetworkOrderController.
Contributor
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. @micaelae yes, I believe the network needs to be enabled — otherwise, certain data like balances and conversion rates won’t be updated. As @Prithpal-Sooriya mentioned, we only keep balances, token metadata, and prices up to date for enabled networks.
Contributor
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. the ideal solution will be to create a separated polling for the enabled swaps network when the users start the flow if you see what i mean , but we can do this separately |
||
| } | ||
| } | ||
| } | ||
| }, | ||
| [dispatch, enabledNetworksByNamespace], | ||
| ); | ||
|
|
||
| return enableMissingNetwork; | ||
| }; | ||
|
|
||
| const PrepareBridgePage = () => { | ||
| const dispatch = useDispatch(); | ||
| const enableMissingNetwork = useEnableMissingNetwork(); | ||
|
|
||
| const t = useI18nContext(); | ||
|
|
||
|
|
@@ -622,6 +669,9 @@ const PrepareBridgePage = () => { | |
| ) { | ||
| dispatch(setToChainId(null)); | ||
| } | ||
| if (isNetworkAdded(networkConfig)) { | ||
| enableMissingNetwork(networkConfig.chainId); | ||
| } | ||
| dispatch( | ||
| setFromChain({ | ||
| networkConfig, | ||
|
|
@@ -803,6 +853,9 @@ const PrepareBridgePage = () => { | |
| network: toChain, | ||
| networks: toChains, | ||
| onNetworkChange: (networkConfig) => { | ||
| if (isNetworkAdded(networkConfig)) { | ||
| enableMissingNetwork(networkConfig.chainId); | ||
| } | ||
| networkConfig.chainId !== toChain?.chainId && | ||
| trackInputEvent({ | ||
| input: 'chain_destination', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.