From 209dd018996962255bd58a3eb8e5df836d2fc8ce Mon Sep 17 00:00:00 2001 From: David Murdoch <187813+davidmurdoch@users.noreply.github.com> Date: Tue, 24 Sep 2024 13:37:31 -0400 Subject: [PATCH] move `getCurrentChainId` from `selectors/selectors.js` to `selectors/network.ts` --- app/scripts/metamask-controller.js | 12 +++--- shared/modules/selectors/feature-flags.ts | 13 +++++-- .../modules/selectors/smart-transactions.ts | 10 +++-- .../quote-card/hooks/useEthFeeData.tsx | 2 +- ui/ducks/ramps/ramps.test.ts | 4 +- .../dropdown-search-list.js | 2 +- ui/pages/swaps/index.js | 2 +- .../list-with-search/list-with-search.js | 2 +- .../list-item-search.component.js | 2 +- ui/selectors/confirm-transaction.js | 3 +- ui/selectors/institutional/selectors.ts | 6 +-- ui/selectors/multichain.ts | 2 +- ui/selectors/networks.ts | 37 +++++++++++-------- ui/selectors/nft.ts | 5 ++- ui/selectors/selectors.js | 8 +--- ui/selectors/transactions.js | 3 +- 16 files changed, 61 insertions(+), 52 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3a8229720675..9cb02d42dd86 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -225,12 +225,12 @@ 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'; -// TODO: Remove restricted import -// eslint-disable-next-line import/no-restricted-paths -import { getProviderConfig } from '../../ui/selectors/networks'; +import { + getCurrentChainId, + getProviderConfig, + // TODO: Remove restricted import + // eslint-disable-next-line import/no-restricted-paths +} from '../../ui/selectors/networks'; import { endTrace, trace } from '../../shared/lib/trace'; import { BalancesController as MultichainBalancesController } from './lib/accounts/BalancesController'; import { diff --git a/shared/modules/selectors/feature-flags.ts b/shared/modules/selectors/feature-flags.ts index 429df2c06da6..639b56557947 100644 --- a/shared/modules/selectors/feature-flags.ts +++ b/shared/modules/selectors/feature-flags.ts @@ -1,6 +1,9 @@ -// 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 { + NetworkState, + getCurrentChainId, + // TODO: Remove restricted import + // eslint-disable-next-line import/no-restricted-paths +} from '../../../ui/selectors/networks'; // TODO: Migrate shared selectors to this file. import { getNetworkNameByChainId } from '../feature-flags'; type FeatureFlagsMetaMaskState = { @@ -21,7 +24,9 @@ type FeatureFlagsMetaMaskState = { }; }; -export function getFeatureFlagsByChainId(state: FeatureFlagsMetaMaskState) { +export function getFeatureFlagsByChainId( + state: NetworkState & FeatureFlagsMetaMaskState, +) { const chainId = getCurrentChainId(state); const networkName = getNetworkNameByChainId(chainId); const featureFlags = state.metamask.swapsState?.swapsFeatureFlags; diff --git a/shared/modules/selectors/smart-transactions.ts b/shared/modules/selectors/smart-transactions.ts index 134394d4d0ba..a1d37f82bb3f 100644 --- a/shared/modules/selectors/smart-transactions.ts +++ b/shared/modules/selectors/smart-transactions.ts @@ -3,7 +3,6 @@ import { SKIP_STX_RPC_URL_CHECK_CHAIN_IDS, } from '../../constants/smartTransactions'; import { - getCurrentChainId, getCurrentNetwork, accountSupportsSmartTx, getSelectedAccount, @@ -15,8 +14,11 @@ import { isProduction } from '../environment'; // TODO: Remove restricted imports // eslint-disable-next-line import/no-restricted-paths import { MultichainState } from '../../../ui/selectors/multichain'; -// eslint-disable-next-line import/no-restricted-paths -import { NetworkState } from '../../../ui/selectors/networks'; +import { + getCurrentChainId, + NetworkState, + // eslint-disable-next-line import/no-restricted-paths +} from '../../../ui/selectors/networks'; type SmartTransactionsMetaMaskState = { metamask: { @@ -65,7 +67,7 @@ export const getSmartTransactionsOptInStatus = ( }; export const getCurrentChainSupportsSmartTransactions = ( - state: SmartTransactionsMetaMaskState, + state: NetworkState, ): boolean => { const chainId = getCurrentChainId(state); return getAllowedSmartTransactionsChainIds().includes(chainId); diff --git a/ui/components/multichain/pages/send/components/quote-card/hooks/useEthFeeData.tsx b/ui/components/multichain/pages/send/components/quote-card/hooks/useEthFeeData.tsx index 25a77edfced4..7c38602eed76 100644 --- a/ui/components/multichain/pages/send/components/quote-card/hooks/useEthFeeData.tsx +++ b/ui/components/multichain/pages/send/components/quote-card/hooks/useEthFeeData.tsx @@ -11,9 +11,9 @@ import { EtherDenomination } from '../../../../../../../../shared/constants/comm import { getCurrentCurrency, checkNetworkAndAccountSupports1559, - getCurrentChainId, getIsSwapsChain, } from '../../../../../../../selectors/selectors'; +import { getCurrentChainId } from '../../../../../../../selectors/networks'; import { fetchAndSetSwapsGasPriceInfo, getUsedSwapsGasPrice, diff --git a/ui/ducks/ramps/ramps.test.ts b/ui/ducks/ramps/ramps.test.ts index 3cd543a65219..8bd6865295d8 100644 --- a/ui/ducks/ramps/ramps.test.ts +++ b/ui/ducks/ramps/ramps.test.ts @@ -205,7 +205,7 @@ describe('rampsSlice', () => { }); it('should return true when Bitcoin is buyable and current chain is Bitcoin', () => { - getCurrentChainIdMock.mockReturnValue(MultichainNetworks.BITCOIN); + getCurrentChainIdMock.mockReturnValue(CHAIN_IDS.MAINNET); getMultichainIsBitcoinMock.mockReturnValue(true); const mockBuyableChains = [ { chainId: MultichainNetworks.BITCOIN, active: true }, @@ -219,7 +219,7 @@ describe('rampsSlice', () => { }); it('should return false when Bitcoin is not buyable and current chain is Bitcoin', () => { - getCurrentChainIdMock.mockReturnValue(MultichainNetworks.BITCOIN); + getCurrentChainIdMock.mockReturnValue(CHAIN_IDS.MAINNET); getMultichainIsBitcoinMock.mockReturnValue(true); const mockBuyableChains = [ { chainId: MultichainNetworks.BITCOIN, active: false }, diff --git a/ui/pages/swaps/dropdown-search-list/dropdown-search-list.js b/ui/pages/swaps/dropdown-search-list/dropdown-search-list.js index 1182ad12d72a..970c7fdfffd9 100644 --- a/ui/pages/swaps/dropdown-search-list/dropdown-search-list.js +++ b/ui/pages/swaps/dropdown-search-list/dropdown-search-list.js @@ -23,9 +23,9 @@ import ImportToken from '../import-token'; import { isHardwareWallet, getHardwareWalletType, - getCurrentChainId, getRpcPrefsForCurrentProvider, } from '../../../selectors/selectors'; +import { getCurrentChainId } from '../../../selectors/networks'; import { getSmartTransactionsOptInStatus, getSmartTransactionsEnabled, diff --git a/ui/pages/swaps/index.js b/ui/pages/swaps/index.js index 877e38aa7c84..c4a71d1de980 100644 --- a/ui/pages/swaps/index.js +++ b/ui/pages/swaps/index.js @@ -20,12 +20,12 @@ import { I18nContext } from '../../contexts/i18n'; import { getSelectedAccount, - getCurrentChainId, getIsSwapsChain, isHardwareWallet, getHardwareWalletType, getTokenList, } from '../../selectors/selectors'; +import { getCurrentChainId } from '../../selectors/networks'; import { getQuotes, clearSwapsState, diff --git a/ui/pages/swaps/list-with-search/list-with-search.js b/ui/pages/swaps/list-with-search/list-with-search.js index 6208e6d0f8bb..ed9339ec9ed1 100644 --- a/ui/pages/swaps/list-with-search/list-with-search.js +++ b/ui/pages/swaps/list-with-search/list-with-search.js @@ -19,7 +19,7 @@ import ItemList from '../searchable-item-list/item-list'; import { isValidHexAddress } from '../../../../shared/modules/hexstring-utils'; import { I18nContext } from '../../../contexts/i18n'; import { fetchToken } from '../swaps.util'; -import { getCurrentChainId } from '../../../selectors/selectors'; +import { getCurrentChainId } from '../../../selectors/networks'; let timeoutIdForSearch; diff --git a/ui/pages/swaps/searchable-item-list/list-item-search/list-item-search.component.js b/ui/pages/swaps/searchable-item-list/list-item-search/list-item-search.component.js index 079089ef36bd..8b32164545df 100644 --- a/ui/pages/swaps/searchable-item-list/list-item-search/list-item-search.component.js +++ b/ui/pages/swaps/searchable-item-list/list-item-search/list-item-search.component.js @@ -8,7 +8,7 @@ import TextField from '../../../../components/ui/text-field'; import { usePrevious } from '../../../../hooks/usePrevious'; import { isValidHexAddress } from '../../../../../shared/modules/hexstring-utils'; import { fetchToken } from '../../swaps.util'; -import { getCurrentChainId } from '../../../../selectors/selectors'; +import { getCurrentChainId } from '../../../../selectors/networks'; import SearchIcon from '../../../../components/ui/icon/search-icon'; const renderAdornment = () => ( diff --git a/ui/selectors/confirm-transaction.js b/ui/selectors/confirm-transaction.js index 80e6d42aecc7..e84a1c884d57 100644 --- a/ui/selectors/confirm-transaction.js +++ b/ui/selectors/confirm-transaction.js @@ -28,11 +28,10 @@ import { subtractHexes, sumHexes, } from '../../shared/modules/conversion.utils'; -import { getProviderConfig } from './networks'; +import { getProviderConfig, getCurrentChainId } from './networks'; import { getAveragePriceEstimateInHexWEI } from './custom-gas'; import { checkNetworkAndAccountSupports1559, - getCurrentChainId, getMetaMaskAccounts, getTokenExchangeRates, } from './selectors'; diff --git a/ui/selectors/institutional/selectors.ts b/ui/selectors/institutional/selectors.ts index 8066f528ee64..0a8ce0502b7b 100644 --- a/ui/selectors/institutional/selectors.ts +++ b/ui/selectors/institutional/selectors.ts @@ -173,12 +173,12 @@ export function getIsCustodianSupportedChain( const selectedAccount = getSelectedInternalAccount(state); const accountType = getAccountType(state); - const providerConfig = getProviderConfig(state); - - if (!selectedAccount || !accountType || !providerConfig) { + if (!selectedAccount || !accountType) { throw new Error('Invalid state'); } + const providerConfig = getProviderConfig(state); + if (typeof providerConfig.chainId !== 'string') { throw new Error('Chain ID must be a string'); } diff --git a/ui/selectors/multichain.ts b/ui/selectors/multichain.ts index 28cb840e4f51..c45223f1bc22 100644 --- a/ui/selectors/multichain.ts +++ b/ui/selectors/multichain.ts @@ -27,10 +27,10 @@ import { getProviderConfig, NetworkState, getNetworkConfigurationsByChainId, + getCurrentChainId, } from './networks'; import { AccountsState, getSelectedInternalAccount } from './accounts'; import { - getCurrentChainId, getCurrentCurrency, getIsMainnet, getMaybeSelectedInternalAccount, diff --git a/ui/selectors/networks.ts b/ui/selectors/networks.ts index 998d8469a060..25101aad6523 100644 --- a/ui/selectors/networks.ts +++ b/ui/selectors/networks.ts @@ -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 '../../shared/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; }; }; export type SelectedNetworkClientIdState = { - metamask: { - selectedNetworkClientId: string; - }; + metamask: Pick; }; -export type MetaMaskExtensionNetworkConfiguration = NetworkConfiguration; - export type NetworkConfigurationsByChainIdState = { - metamask: Pick<_NetworkState, 'networkConfigurationsByChainId'>; + metamask: Pick; +}; + +export type NetworksMetadataState = { + metamask: Pick; }; export type ProviderConfigState = NetworkConfigurationsByChainIdState & @@ -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), @@ -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 { +): Record { return state.metamask.networkConfigurations; } @@ -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; +} diff --git a/ui/selectors/nft.ts b/ui/selectors/nft.ts index ab3836714923..6d2409f010cd 100644 --- a/ui/selectors/nft.ts +++ b/ui/selectors/nft.ts @@ -1,6 +1,7 @@ import { Nft, NftContract } from '@metamask/assets-controllers'; import { createSelector } from 'reselect'; import { getMemoizedCurrentChainId } from './selectors'; +import { NetworkState } from './networks'; export type NftState = { metamask: { @@ -62,9 +63,9 @@ export const getNftContractsByAddressByChain = createSelector( ); export const getNftContractsByAddressOnCurrentChain = createSelector( + (state: NftState & NetworkState) => getMemoizedCurrentChainId(state), getNftContractsByAddressByChain, - getMemoizedCurrentChainId, - (nftContractsByAddressByChain, currentChainId) => { + (currentChainId, nftContractsByAddressByChain) => { return nftContractsByAddressByChain[currentChainId] ?? {}; }, ); diff --git a/ui/selectors/selectors.js b/ui/selectors/selectors.js index fb06ad8f0d99..96e53794b635 100644 --- a/ui/selectors/selectors.js +++ b/ui/selectors/selectors.js @@ -107,8 +107,9 @@ import { MultichainNativeAssets } from '../../shared/constants/multichain/assets import { BridgeFeatureFlagsKey } from '../../app/scripts/controllers/bridge/types'; import { hasTransactionData } from '../../shared/modules/transaction.utils'; import { - getProviderConfig, getNetworkConfigurationsByChainId, + getCurrentChainId, + getProviderConfig, } from './networks'; import { getAllUnapprovedTransactions, @@ -133,11 +134,6 @@ export function getNetworkIdentifier(state) { return nickname || rpcUrl || type; } -export function getCurrentChainId(state) { - const { chainId } = getProviderConfig(state); - return chainId; -} - export function getMetaMetricsId(state) { const { metaMetricsId } = state.metamask; return metaMetricsId; diff --git a/ui/selectors/transactions.js b/ui/selectors/transactions.js index 7fbfef9602a1..725636a33aca 100644 --- a/ui/selectors/transactions.js +++ b/ui/selectors/transactions.js @@ -12,8 +12,7 @@ import { import txHelper from '../helpers/utils/tx-helper'; import { SmartTransactionStatus } from '../../shared/constants/transaction'; import { hexToDecimal } from '../../shared/modules/conversion.utils'; -import { getProviderConfig } from './networks'; -import { getCurrentChainId } from './selectors'; +import { getProviderConfig, getCurrentChainId } from './networks'; import { getSelectedInternalAccount } from './accounts'; import { hasPendingApprovals, getApprovalRequestsByType } from './approvals'; import {