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

feat: Multichain: Migrate getCurrentChainId to use page context #27710

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 26 additions & 12 deletions shared/modules/selectors/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
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';

Check failure on line 8 in shared/modules/selectors/networks.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

`../../constants/app` import should occur before import of `./util`

export type NetworkState = {
metamask: InternalNetworkState;
Expand All @@ -30,7 +30,14 @@
};

export type ProviderConfigState = NetworkConfigurationsByChainIdState &
SelectedNetworkClientIdState;
SelectedNetworkClientIdState & {
activeTab?: {
origin: string;
};
metamask: {
domains: Record<string, string>;
}

Check failure on line 39 in shared/modules/selectors/networks.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Insert `;`
};

export const getNetworkConfigurationsByChainId = createDeepEqualSelector(
(state: NetworkConfigurationsByChainIdState) =>
Expand All @@ -50,19 +57,26 @@
* @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 ?? 0];

Check failure on line 79 in shared/modules/selectors/networks.ts

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

Replace `network.defaultBlockExplorerUrlIndex·??·0` with `⏎··············network.defaultBlockExplorerUrlIndex·??·0⏎············`

return {
chainId: network.chainId,
Expand Down
2 changes: 2 additions & 0 deletions ui/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
import * as actions from './store/actions';
import configureStore from './store/store';
import {
getOriginOfCurrentTab,

Check failure on line 26 in ui/index.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'getOriginOfCurrentTab' is defined but never used
getPermittedAccountsForCurrentTab,
getSelectedInternalAccount,
getUnapprovedTransactions,
getNetworkToAutomaticallySwitchTo,

Check failure on line 30 in ui/index.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'getNetworkToAutomaticallySwitchTo' is defined but never used
getSwitchedNetworkDetails,

Check failure on line 31 in ui/index.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'getSwitchedNetworkDetails' is defined but never used
} from './selectors';
import { ALERT_STATE } from './ducks/alerts';
import {
Expand Down Expand Up @@ -217,8 +217,9 @@
}

async function runInitialActions(store) {
const state = store.getState();

Check failure on line 220 in ui/index.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'state' is assigned a value but never used

/*
// 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
Expand All @@ -239,6 +240,7 @@
// 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
Expand Down
2 changes: 2 additions & 0 deletions ui/pages/routes/routes.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
hideDeprecatedNetworkModal: PropTypes.func.isRequired,
clearSwitchedNetworkDetails: PropTypes.func.isRequired,
networkToAutomaticallySwitchTo: PropTypes.object,
automaticallySwitchNetwork: PropTypes.func.isRequired,

Check failure on line 193 in ui/pages/routes/routes.component.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'automaticallySwitchNetwork' PropType is defined but prop is never used
totalUnapprovedConfirmationCount: PropTypes.number.isRequired,
currentExtensionPopupId: PropTypes.number,
clearEditedNetwork: PropTypes.func.isRequired,
Expand All @@ -212,8 +212,8 @@
componentDidUpdate(prevProps) {
const {
theme,
networkToAutomaticallySwitchTo,

Check failure on line 215 in ui/pages/routes/routes.component.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'networkToAutomaticallySwitchTo' is assigned a value but never used
activeTabOrigin,

Check failure on line 216 in ui/pages/routes/routes.component.js

View workflow job for this annotation

GitHub Actions / Test lint / Test lint

'activeTabOrigin' is assigned a value but never used
totalUnapprovedConfirmationCount,
isUnlocked,
currentExtensionPopupId,
Expand All @@ -222,6 +222,7 @@
setTheme(theme);
}

/*
// Automatically switch the network if the user
// no longer has unapproved transactions and they
// should be on a different network for the
Expand All @@ -237,6 +238,7 @@
activeTabOrigin,
);
}
*/

// Terminate the popup when another popup is opened
// if the user is using RPC queueing
Expand Down
Loading