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: move getCurrentChainId from selectors/selectors.js to shared/modules/selectors/networks.ts #27647

Merged
merged 2 commits into from
Nov 27, 2024
Merged
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
4 changes: 1 addition & 3 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ import {
FakeLedgerBridge,
FakeTrezorBridge,
} from '../../test/stub/keyring-bridge';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../ui/selectors';
import { getCurrentChainId } from '../../shared/modules/selectors/networks';
import { addNonceToCsp } from '../../shared/modules/add-nonce-to-csp';
import { checkURLForProviderInjection } from '../../shared/modules/provider-injection';
import migrations from './migrations';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import {
TRUSTED_SIGNERS,
} from '../../../../shared/constants/verification';
import { MESSAGE_TYPE } from '../../../../shared/constants/app';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../../../ui/selectors';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';

export type TxParams = {
chainId?: `0x${string}`;
Expand Down
8 changes: 4 additions & 4 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ import {
TOKEN_TRANSFER_LOG_TOPIC_HASH,
TRANSFER_SINFLE_LOG_TOPIC_HASH,
} from '../../shared/lib/transactions-controller-utils';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../ui/selectors/selectors';
import { getProviderConfig } from '../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getProviderConfig,
} from '../../shared/modules/selectors/networks';
import { endTrace, trace } from '../../shared/lib/trace';
// eslint-disable-next-line import/no-restricted-paths
import { isSnapId } from '../../ui/helpers/utils/snaps';
Expand Down
8 changes: 4 additions & 4 deletions shared/modules/selectors/feature-flags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getCurrentChainId } from '../../../ui/selectors/selectors'; // TODO: Migrate shared selectors to this file.
import { getNetworkNameByChainId } from '../feature-flags';
import { ProviderConfigState, getCurrentChainId } from './networks';

type FeatureFlagsMetaMaskState = {
metamask: {
Expand All @@ -21,7 +19,9 @@ type FeatureFlagsMetaMaskState = {
};
};

export function getFeatureFlagsByChainId(state: FeatureFlagsMetaMaskState) {
export function getFeatureFlagsByChainId(
state: ProviderConfigState & FeatureFlagsMetaMaskState,
) {
const chainId = getCurrentChainId(state);
const networkName = getNetworkNameByChainId(chainId);
const featureFlags = state.metamask.swapsState?.swapsFeatureFlags;
Expand Down
37 changes: 22 additions & 15 deletions shared/modules/selectors/networks.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
import {
RpcEndpointType,
type NetworkConfiguration,
type NetworkState as _NetworkState,
type NetworkState as InternalNetworkState,
} from '@metamask/network-controller';
import { createSelector } from 'reselect';
import { NetworkStatus } from '../../constants/network';
import { createDeepEqualSelector } from './util';

export type NetworkState = { metamask: _NetworkState };
export type NetworkState = {
metamask: InternalNetworkState;
};

export type NetworkConfigurationsState = {
metamask: {
networkConfigurations: Record<
string,
MetaMaskExtensionNetworkConfiguration
>;
networkConfigurations: Record<string, NetworkConfiguration>;
};
};

export type SelectedNetworkClientIdState = {
metamask: {
selectedNetworkClientId: string;
};
metamask: Pick<InternalNetworkState, 'selectedNetworkClientId'>;
};

export type MetaMaskExtensionNetworkConfiguration = NetworkConfiguration;

export type NetworkConfigurationsByChainIdState = {
metamask: Pick<_NetworkState, 'networkConfigurationsByChainId'>;
metamask: Pick<InternalNetworkState, 'networkConfigurationsByChainId'>;
};

export type NetworksMetadataState = {
metamask: Pick<InternalNetworkState, 'networksMetadata'>;
};

export type ProviderConfigState = NetworkConfigurationsByChainIdState &
Expand All @@ -49,6 +48,7 @@ export function getSelectedNetworkClientId(
* Get the provider configuration for the current selected network.
*
* @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),
Expand Down Expand Up @@ -81,13 +81,13 @@ export const getProviderConfig = createSelector(
}
}
}
return undefined; // should not be reachable
throw new Error('Provider configuration not found');
Copy link
Contributor Author

@davidmurdoch davidmurdoch Nov 22, 2024

Choose a reason for hiding this comment

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

One of two logic changes. Reason is that if this code is, in fact, reachable, we should yell about it so we know there is a bug (not ignore it like before). This is changed now because the previous return type (with undefined) isn't compatible with the existing implementation of getCurrentChainId.

It was only noticed now because getCurrentChainId is now typed.

},
);

export function getNetworkConfigurations(
state: NetworkConfigurationsState,
): Record<string, MetaMaskExtensionNetworkConfiguration> {
): Record<string, NetworkConfiguration> {
return state.metamask.networkConfigurations;
}

Expand All @@ -106,9 +106,16 @@ export function isNetworkLoading(state: NetworkState) {
);
}

export function getInfuraBlocked(state: NetworkState) {
export function getInfuraBlocked(
state: SelectedNetworkClientIdState & NetworksMetadataState,
) {
return (
state.metamask.networksMetadata[getSelectedNetworkClientId(state)]
.status === NetworkStatus.Blocked
);
}

export function getCurrentChainId(state: ProviderConfigState) {
const { chainId } = getProviderConfig(state);
return chainId;
}
5 changes: 2 additions & 3 deletions shared/modules/selectors/smart-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
SKIP_STX_RPC_URL_CHECK_CHAIN_IDS,
} from '../../constants/smartTransactions';
import {
getCurrentChainId,
getCurrentNetwork,
accountSupportsSmartTx,
getPreferences,
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
} from '../../../ui/selectors/selectors'; // TODO: Migrate shared selectors to this file.
import { isProduction } from '../environment';
import { NetworkState } from './networks';
import { getCurrentChainId, NetworkState } from './networks';

type SmartTransactionsMetaMaskState = {
metamask: {
Expand Down Expand Up @@ -108,7 +107,7 @@ export const getSmartTransactionsPreferenceEnabled = createSelector(
);

export const getCurrentChainSupportsSmartTransactions = (
state: SmartTransactionsMetaMaskState,
state: NetworkState,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previous type wasn't enough now that getCurrentChainId is typed.

): boolean => {
const chainId = getCurrentChainId(state);
return getAllowedSmartTransactionsChainIds().includes(chainId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { setTokenNetworkFilter } from '../../../../../store/actions';
import {
getCurrentChainId,
getCurrentNetwork,
getPreferences,
getShouldHideZeroBalanceTokens,
getSelectedAccount,
getAllChainsToPoll,
} from '../../../../../selectors';
import { getNetworkConfigurationsByChainId } from '../../../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getNetworkConfigurationsByChainId,
} from '../../../../../../shared/modules/selectors/networks';
import { useI18nContext } from '../../../../../hooks/useI18nContext';
import { SelectableListItem } from '../sort-control/sort-control';
import { Text } from '../../../../component-library/text/text';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
import { useI18nContext } from '../../../../../hooks/useI18nContext';
import { shortenAddress } from '../../../../../helpers/utils/util';
import { getNftImageAlt } from '../../../../../helpers/utils/nfts';
import { getCurrentChainId } from '../../../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getCurrentCurrency,
getCurrentNetwork,
getIpfsGateway,
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/assets/nfts/nfts-items/nfts-items.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import { ENVIRONMENT_TYPE_POPUP } from '../../../../../../shared/constants/app';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
import { getEnvironmentType } from '../../../../../../app/scripts/lib/util';
import { getCurrentChainId } from '../../../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getIpfsGateway,
getSelectedInternalAccount,
getCurrentNetwork,
Expand Down
7 changes: 3 additions & 4 deletions ui/components/app/currency-input/currency-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { BlockSize } from '../../../helpers/constants/design-system';
import UnitInput from '../../ui/unit-input';
import CurrencyDisplay from '../../ui/currency-display';
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
import { getProviderConfig } from '../../../../shared/modules/selectors/networks';
import {
getProviderConfig,
getCurrentChainId,
getCurrentCurrency,
getShouldShowFiat,
} from '../../../selectors';
} from '../../../../shared/modules/selectors/networks';
import { getCurrentCurrency, getShouldShowFiat } from '../../../selectors';
import { EtherDenomination } from '../../../../shared/constants/common';
import { Numeric } from '../../../../shared/modules/Numeric';
import { useIsOriginalNativeTokenSymbol } from '../../../hooks/useIsOriginalNativeTokenSymbol';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useMemo, useState } from 'react';
import { toChecksumAddress } from 'ethereumjs-util';
import { shallowEqual, useSelector } from 'react-redux';
import {
getCurrentChainId,
getTokenExchangeRates,
} from '../../../../selectors';
import { getCurrentChainId } from '../../../../../shared/modules/selectors/networks';
import { getTokenExchangeRates } from '../../../../selectors';
import { Numeric } from '../../../../../shared/modules/Numeric';
import {
getConversionRate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import {
MetaMetricsTokenEventSource,
} from '../../../../../shared/constants/metametrics';
import {
getAllDetectedTokensForSelectedAddress,
getCurrentChainId,
getNetworkConfigurationsByChainId,
} from '../../../../../shared/modules/selectors/networks';
import {
getAllDetectedTokensForSelectedAddress,
getCurrentNetwork,
getDetectedTokensInCurrentNetwork,
getPreferences,
} from '../../../../selectors';
import { getNetworkConfigurationsByChainId } from '../../../../../shared/modules/selectors/networks';

import Popover from '../../../ui/popover';
import Box from '../../../ui/box';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
TextVariant,
} from '../../../../helpers/constants/design-system';
import { useTokenFiatAmount } from '../../../../hooks/useTokenFiatAmount';
import { getCurrentChainId } from '../../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getSelectedAddress,
getUseCurrencyRateCheck,
} from '../../../../selectors';
Expand Down
10 changes: 5 additions & 5 deletions ui/components/app/detected-token/detected-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
setNewTokensImported,
} from '../../../store/actions';
import {
getAllDetectedTokensForSelectedAddress,
getCurrentChainId,
getDetectedTokensInCurrentNetwork,
getPreferences,
} from '../../../selectors';
import {
getSelectedNetworkClientId,
getNetworkConfigurationsByChainId,
} from '../../../../shared/modules/selectors/networks';
import {
getAllDetectedTokensForSelectedAddress,
getDetectedTokensInCurrentNetwork,
getPreferences,
} from '../../../selectors';
import { MetaMetricsContext } from '../../../contexts/metametrics';

import {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
MetaMetricsEventCategory,
MetaMetricsEventName,
} from '../../../../../shared/constants/metametrics';
import { getCurrentChainId } from '../../../../selectors';
import { getNetworkConfigurationsByChainId } from '../../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getNetworkConfigurationsByChainId,
} from '../../../../../shared/modules/selectors/networks';

function mapStateToProps(state) {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import InfoTooltip from '../../../ui/info-tooltip';
import { getCurrentChainId } from '../../../../selectors';
import { getCurrentChainId } from '../../../../../shared/modules/selectors/networks';
import { KeyringAccountListItem } from './keyring-account-list-item';

export default function KeyringRemovalSnapWarning({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
} from '../../../selectors/transactions';
import {
getCurrentChainId,
getNetworkConfigurationsByChainId,
} from '../../../../shared/modules/selectors/networks';
import {
getSelectedAccount,
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
getShouldHideZeroBalanceTokens,
Expand Down Expand Up @@ -55,7 +58,6 @@ import { MetaMetricsContext } from '../../../contexts/metametrics';
import { useMultichainSelector } from '../../../hooks/useMultichainSelector';
import { getMultichainNetwork } from '../../../selectors/multichain';
import { endTrace, TraceName } from '../../../../shared/lib/trace';
import { getNetworkConfigurationsByChainId } from '../../../../shared/modules/selectors/networks';

const PAGE_INCREMENT = 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
getShouldHideZeroBalanceTokens,
getTokensMarketData,
getPreferences,
getCurrentChainId,
} from '../../../selectors';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';
import { useAccountTotalFiatBalance } from '../../../hooks/useAccountTotalFiatBalance';
import { AggregatedPercentageOverview } from './aggregated-percentage-overview';

Expand All @@ -27,6 +27,9 @@ jest.mock('../../../selectors', () => ({
getPreferences: jest.fn(),
getShouldHideZeroBalanceTokens: jest.fn(),
getTokensMarketData: jest.fn(),
}));

jest.mock('../../../../shared/modules/selectors/networks', () => ({
getCurrentChainId: jest.fn(),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
getShouldHideZeroBalanceTokens,
getTokensMarketData,
getPreferences,
getCurrentChainId,
} from '../../../selectors';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';

import { useAccountTotalFiatBalance } from '../../../hooks/useAccountTotalFiatBalance';
// TODO: Remove restricted import
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/wallet-overview/coin-buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ import {
getMemoizedUnapprovedTemplatedConfirmations,
///: END:ONLY_INCLUDE_IF
getNetworkConfigurationIdByChainId,
getCurrentChainId,
} from '../../../selectors';
import Tooltip from '../../ui/tooltip';
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
Expand Down Expand Up @@ -100,6 +99,7 @@ import {
getMultichainNativeCurrency,
} from '../../../selectors/multichain';
import { useMultichainSelector } from '../../../hooks/useMultichainSelector';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';

type CoinButtonsProps = {
account: InternalAccount;
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/wallet-overview/eth-overview.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { EthMethod } from '@metamask/keyring-api';
import { isEqual } from 'lodash';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';
import {
isBalanceCached,
getIsSwapsChain,
getCurrentChainId,
getSelectedInternalAccount,
getSelectedAccountCachedBalance,
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
Expand Down
Loading
Loading