Skip to content

Commit

Permalink
move getCurrentChainId from selectors/selectors.js to `selectors/…
Browse files Browse the repository at this point in the history
…network.ts`
  • Loading branch information
davidmurdoch committed Nov 26, 2024
1 parent d24389c commit 8a9ba91
Show file tree
Hide file tree
Showing 78 changed files with 201 additions and 155 deletions.
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');
},
);

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,
): 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,
getChainIdsToPoll,
getShouldHideZeroBalanceTokens,
getSelectedAccount,
} 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
2 changes: 1 addition & 1 deletion ui/components/app/assets/nfts/nft-details/nft-details.tsx
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 @@ -13,8 +13,8 @@ import {
nonceSortedCompletedTransactionsSelector,
nonceSortedPendingTransactionsSelector,
} from '../../../selectors/transactions';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';
import {
getCurrentChainId,
getSelectedAccount,
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
getShouldHideZeroBalanceTokens,
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import EditableLabel from '../../ui/editable-label/editable-label';

import { setAccountLabel } from '../../../store/actions';
import {
getCurrentChainId,
getHardwareWalletType,
getInternalAccountByAddress,
} from '../../../selectors';
Expand All @@ -30,6 +29,7 @@ import {
MetaMetricsEventName,
} from '../../../../shared/constants/metametrics';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { getCurrentChainId } from '../../../../shared/modules/selectors/networks';

export const AccountDetailsDisplay = ({
accounts,
Expand Down
Loading

0 comments on commit 8a9ba91

Please sign in to comment.