From 94f4e2383263e24c3ba35360875a489829e4b874 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Fri, 10 Jan 2025 16:00:11 -0600 Subject: [PATCH 1/2] Switch to using dapp-selected-chain for global network picker when available --- shared/modules/selectors/networks.ts | 29 +++++++++++++++++----------- ui/index.js | 2 ++ ui/pages/routes/routes.component.js | 2 ++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/shared/modules/selectors/networks.ts b/shared/modules/selectors/networks.ts index bc8d05b5164d..122cd765f2a4 100644 --- a/shared/modules/selectors/networks.ts +++ b/shared/modules/selectors/networks.ts @@ -3,9 +3,9 @@ import { type NetworkConfiguration, type NetworkState as InternalNetworkState, } from '@metamask/network-controller'; -import { createSelector } from 'reselect'; import { NetworkStatus } from '../../constants/network'; import { createDeepEqualSelector } from './util'; +import { ORIGIN_METAMASK } from '../../constants/app'; export type NetworkState = { metamask: InternalNetworkState; @@ -50,19 +50,26 @@ export function getSelectedNetworkClientId( * @param state - Redux state object. * @throws `new Error('Provider configuration not found')` If the provider configuration is not found. */ -export const getProviderConfig = createSelector( - (state: ProviderConfigState) => getNetworkConfigurationsByChainId(state), - getSelectedNetworkClientId, - (networkConfigurationsByChainId, selectedNetworkClientId) => { +export const getProviderConfig = createDeepEqualSelector( + (state: ProviderConfigState) => ({ + networkConfigurationsByChainId: getNetworkConfigurationsByChainId(state), + selectedNetworkClientId: getSelectedNetworkClientId(state), + metamask: state.metamask, + activeTabOrigin: state.activeTab?.origin || ORIGIN_METAMASK, + }), + ({ + networkConfigurationsByChainId, + selectedNetworkClientId, + metamask, + activeTabOrigin, + }) => { + const networkClientId = metamask.domains[activeTabOrigin]; + const networkClientIdToUse = networkClientId || selectedNetworkClientId; for (const network of Object.values(networkConfigurationsByChainId)) { for (const rpcEndpoint of network.rpcEndpoints) { - if (rpcEndpoint.networkClientId === selectedNetworkClientId) { + if (rpcEndpoint.networkClientId === networkClientIdToUse) { const blockExplorerUrl = - network.defaultBlockExplorerUrlIndex === undefined - ? undefined - : network.blockExplorerUrls?.[ - network.defaultBlockExplorerUrlIndex - ]; + network.blockExplorerUrls?.[network.defaultBlockExplorerUrlIndex]; return { chainId: network.chainId, diff --git a/ui/index.js b/ui/index.js index 54d952eb85f4..56b202024367 100644 --- a/ui/index.js +++ b/ui/index.js @@ -219,6 +219,7 @@ async function startApp(metamaskState, backgroundConnection, opts) { async function runInitialActions(store) { const state = store.getState(); + /* // This block autoswitches chains based on the last chain used // for a given dapp, when there are no pending confimrations // This allows the user to be connected on one chain @@ -239,6 +240,7 @@ async function runInitialActions(store) { // if the user didn't just change the dapp network await store.dispatch(actions.clearSwitchedNetworkDetails()); } + */ // Register this window as the current popup // and set in background state diff --git a/ui/pages/routes/routes.component.js b/ui/pages/routes/routes.component.js index 74f15a5f99c6..9da9763a11a9 100644 --- a/ui/pages/routes/routes.component.js +++ b/ui/pages/routes/routes.component.js @@ -222,6 +222,7 @@ export default class Routes extends Component { setTheme(theme); } + /* // Automatically switch the network if the user // no longer has unapproved transactions and they // should be on a different network for the @@ -237,6 +238,7 @@ export default class Routes extends Component { activeTabOrigin, ); } + */ // Terminate the popup when another popup is opened // if the user is using RPC queueing From 89aa70795f4860264525192c5309f5f45ff207e6 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Fri, 10 Jan 2025 16:13:03 -0600 Subject: [PATCH 2/2] Fix typescript --- shared/modules/selectors/networks.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/shared/modules/selectors/networks.ts b/shared/modules/selectors/networks.ts index 122cd765f2a4..2409db5ad837 100644 --- a/shared/modules/selectors/networks.ts +++ b/shared/modules/selectors/networks.ts @@ -30,7 +30,14 @@ export type NetworksMetadataState = { }; export type ProviderConfigState = NetworkConfigurationsByChainIdState & - SelectedNetworkClientIdState; + SelectedNetworkClientIdState & { + activeTab?: { + origin: string; + }; + metamask: { + domains: Record; + } + }; export const getNetworkConfigurationsByChainId = createDeepEqualSelector( (state: NetworkConfigurationsByChainIdState) => @@ -69,7 +76,7 @@ export const getProviderConfig = createDeepEqualSelector( for (const rpcEndpoint of network.rpcEndpoints) { if (rpcEndpoint.networkClientId === networkClientIdToUse) { const blockExplorerUrl = - network.blockExplorerUrls?.[network.defaultBlockExplorerUrlIndex]; + network.blockExplorerUrls?.[network.defaultBlockExplorerUrlIndex ?? 0]; return { chainId: network.chainId,