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 Oct 8, 2024
1 parent ac769fa commit 4f64a71
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 43 deletions.
13 changes: 9 additions & 4 deletions shared/modules/selectors/feature-flags.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions shared/modules/selectors/smart-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
SKIP_STX_RPC_URL_CHECK_CHAIN_IDS,
} from '../../constants/smartTransactions';
import {
getCurrentChainId,
getCurrentNetwork,
accountSupportsSmartTx,
getSelectedAccount,
Expand All @@ -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: {
Expand Down Expand Up @@ -65,7 +67,7 @@ export const getSmartTransactionsOptInStatus = (
};

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 @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions ui/ducks/ramps/ramps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/swaps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/swaps/list-with-search/list-with-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => (
Expand Down
3 changes: 1 addition & 2 deletions ui/selectors/confirm-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion ui/selectors/multichain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import {
getProviderConfig,
NetworkState,
getNetworkConfigurationsByChainId,
getCurrentChainId,
} from './networks';
import { AccountsState, getSelectedInternalAccount } from './accounts';
import {
getCurrentChainId,
getCurrentCurrency,
getIsMainnet,
getMaybeSelectedInternalAccount,
Expand Down
36 changes: 21 additions & 15 deletions ui/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 '../../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<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 Down Expand Up @@ -81,13 +80,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 +105,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: 3 additions & 2 deletions ui/selectors/nft.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NftContract } from '@metamask/assets-controllers';
import { createSelector } from 'reselect';
import { getMemoizedCurrentChainId } from './selectors';
import { NetworkState } from './networks';

type NftState = {
metamask: {
Expand Down Expand Up @@ -47,9 +48,9 @@ export const getNftContractsByAddressByChain = createSelector(
);

export const getNftContractsByAddressOnCurrentChain = createSelector(
(state: NftState & NetworkState) => getMemoizedCurrentChainId(state),
getNftContractsByAddressByChain,
getMemoizedCurrentChainId,
(nftContractsByAddressByChain, currentChainId) => {
(currentChainId, nftContractsByAddressByChain) => {
return nftContractsByAddressByChain[currentChainId] ?? {};
},
);
7 changes: 1 addition & 6 deletions ui/selectors/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ import { MultichainNativeAssets } from '../../shared/constants/multichain/assets
// eslint-disable-next-line import/no-restricted-paths
import { BridgeFeatureFlagsKey } from '../../app/scripts/controllers/bridge/types';
import { hasTransactionData } from '../../shared/modules/transaction.utils';
import { getProviderConfig } from './networks';
import { getCurrentChainId, getProviderConfig } from './networks';
import {
getAllUnapprovedTransactions,
getCurrentNetworkTransactions,
Expand All @@ -132,11 +132,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;
Expand Down
3 changes: 1 addition & 2 deletions ui/selectors/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4f64a71

Please sign in to comment.