Skip to content

Commit

Permalink
refactor: move getCurrentCurrency from ui/selectors/selectors.js
Browse files Browse the repository at this point in the history
…to `ui/ducks/metamask/metamask.js` (#27648)

This change is related to circular dependency work; no runtime code has
changed. QA is not required.

This PR moves `getCurrentCurrency` into `ui/ducks/` instead of keeping
it in `ui/selectors/` because `ui/ducks/metamask/metamask.js` already
exports a `getNativeCurrency`, so it seems to fit in well there.


<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.


## **Description**



[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27648?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to this page...
2.
3.

## **Screenshots/Recordings**


### **Before**

### **After**


## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
-->
  • Loading branch information
davidmurdoch authored Dec 17, 2024
1 parent 6fe17a0 commit da16971
Show file tree
Hide file tree
Showing 53 changed files with 139 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
getMultichainShouldShowFiat,
} from '../../../../../selectors/multichain';
import {
getCurrentCurrency,
getPreferences,
getSelectedInternalAccount,
} from '../../../../../selectors';
import { getCurrentCurrency } from '../../../../../ducks/metamask/metamask';
import { useIsOriginalNativeTokenSymbol } from '../../../../../hooks/useIsOriginalNativeTokenSymbol';
import { PRIMARY, SECONDARY } from '../../../../../helpers/constants/common';
import { useUserPreferencedCurrency } from '../../../../../hooks/useUserPreferencedCurrency';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { useSelector } from 'react-redux';
import { setTokenSortConfig } from '../../../../../store/actions';
import { renderWithProvider } from '../../../../../../test/lib/render-helpers';
import { MetaMetricsContext } from '../../../../../contexts/metametrics';
import { getCurrentCurrency, getPreferences } from '../../../../../selectors';
import { getPreferences } from '../../../../../selectors';
import { getCurrentCurrency } from '../../../../../ducks/metamask/metamask';
import SortControl from './sort-control';

// Mock the sortAssets utility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
MetaMetricsEventName,
MetaMetricsUserTrait,
} from '../../../../../../shared/constants/metametrics';
import { getCurrentCurrency, getPreferences } from '../../../../../selectors';
import { getPreferences } from '../../../../../selectors';
import { getCurrentCurrency } from '../../../../../ducks/metamask/metamask';
import { useI18nContext } from '../../../../../hooks/useI18nContext';
import { getCurrencySymbol } from '../../../../../helpers/utils/common.util';

Expand Down
11 changes: 5 additions & 6 deletions ui/components/app/assets/nfts/nft-details/nft-details.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ 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 {
getCurrentCurrency,
getCurrentNetwork,
getIpfsGateway,
} from '../../../../../selectors';
import { getCurrentNetwork, getIpfsGateway } from '../../../../../selectors';
import {
ASSET_ROUTE,
DEFAULT_ROUTE,
Expand Down Expand Up @@ -67,7 +63,10 @@ import { Content, Footer, Page } from '../../../../multichain/pages/page';
import { formatCurrency } from '../../../../../helpers/utils/confirm-tx.util';
import { getShortDateFormatterV2 } from '../../../../../pages/asset/util';
import { CHAINID_DEFAULT_BLOCK_EXPLORER_URL_MAP } from '../../../../../../shared/constants/common';
import { getConversionRate } from '../../../../../ducks/metamask/metamask';
import {
getConversionRate,
getCurrentCurrency,
} from '../../../../../ducks/metamask/metamask';
import { Numeric } from '../../../../../../shared/modules/Numeric';
// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/assets/token-cell/token-cell.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { fireEvent } from '@testing-library/react';
import { useSelector } from 'react-redux';
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
import { useTokenFiatAmount } from '../../../../hooks/useTokenFiatAmount';
import { getCurrentCurrency } from '../../../../ducks/metamask/metamask';
import {
getTokenList,
getPreferences,
getCurrentCurrency,
getCurrencyRates,
} from '../../../../selectors';
import {
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/assets/token-cell/token-cell.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { BigNumber } from 'bignumber.js';
import { getCurrentCurrency } from '../../../../ducks/metamask/metamask';
import {
getCurrentCurrency,
getTokenList,
selectERC20TokensByChain,
getNativeCurrencyForChain,
Expand Down
7 changes: 5 additions & 2 deletions ui/components/app/currency-input/currency-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { Box } from '../../component-library';
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 {
getNativeCurrency,
getCurrentCurrency,
} from '../../../ducks/metamask/metamask';
import {
getProviderConfig,
getCurrentChainId,
} from '../../../../shared/modules/selectors/networks';
import { getCurrentCurrency, getShouldShowFiat } from '../../../selectors';
import { 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
Expand Up @@ -3,14 +3,14 @@ import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { getIntlLocale } from '../../../ducks/locale/locale';
import {
getCurrentCurrency,
getSelectedAccount,
getShouldHideZeroBalanceTokens,
getPreferences,
getMarketData,
getAllTokens,
getChainIdsToPoll,
} from '../../../selectors';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';
import { useAccountTotalCrossChainFiatBalance } from '../../../hooks/useAccountTotalCrossChainFiatBalance';
import { getNetworkConfigurationsByChainId } from '../../../../shared/modules/selectors/networks';
import { AggregatedPercentageOverviewCrossChains } from './aggregated-percentage-overview-cross-chains';
Expand All @@ -31,7 +31,6 @@ jest.mock('../../../ducks/locale/locale', () => ({
}));

jest.mock('../../../selectors', () => ({
getCurrentCurrency: jest.fn(),
getSelectedAccount: jest.fn(),
getPreferences: jest.fn(),
getShouldHideZeroBalanceTokens: jest.fn(),
Expand All @@ -40,6 +39,10 @@ jest.mock('../../../selectors', () => ({
getChainIdsToPoll: jest.fn(),
}));

jest.mock('../../../ducks/metamask/metamask', () => ({
getCurrentCurrency: jest.fn(),
}));

jest.mock('../../../../shared/modules/selectors/networks', () => ({
getNetworkConfigurationsByChainId: jest.fn(),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { toChecksumAddress } from 'ethereumjs-util';
import { getNativeTokenAddress } from '@metamask/assets-controllers';
import { Hex } from '@metamask/utils';
import {
getCurrentCurrency,
getSelectedAccount,
getShouldHideZeroBalanceTokens,
getPreferences,
getMarketData,
getChainIdsToPoll,
} from '../../../selectors';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';

// TODO: Remove restricted import
// eslint-disable-next-line import/no-restricted-paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { getIntlLocale } from '../../../ducks/locale/locale';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';
import {
getCurrentCurrency,
getSelectedAccount,
getShouldHideZeroBalanceTokens,
getTokensMarketData,
Expand All @@ -21,8 +21,11 @@ jest.mock('../../../ducks/locale/locale', () => ({
getIntlLocale: jest.fn(),
}));

jest.mock('../../../selectors', () => ({
jest.mock('../../../ducks/metamask/metamask', () => ({
getCurrentCurrency: jest.fn(),
}));

jest.mock('../../../selectors', () => ({
getSelectedAccount: jest.fn(),
getPreferences: jest.fn(),
getShouldHideZeroBalanceTokens: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { useSelector } from 'react-redux';

import { toChecksumAddress } from 'ethereumjs-util';
import { getNativeTokenAddress } from '@metamask/assets-controllers';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';
import {
getCurrentCurrency,
getSelectedAccount,
getShouldHideZeroBalanceTokens,
getTokensMarketData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import { Text } from '../../../component-library';
import UserPreferencedCurrencyDisplay from '../../../app/user-preferenced-currency-display';
import { PRIMARY } from '../../../../helpers/constants/common';
import { Asset } from '../../../../ducks/send';
import {
getCurrentCurrency,
getSelectedAccountCachedBalance,
} from '../../../../selectors';
import { getSelectedAccountCachedBalance } from '../../../../selectors';
import { getCurrentCurrency } from '../../../../ducks/metamask/metamask';
import { AssetType } from '../../../../../shared/constants/transaction';
import {
TextColor,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { BigNumber } from 'bignumber.js';
import { getCurrentCurrency } from '../../../../ducks/metamask/metamask';
import {
getCurrentCurrency,
getNetworkConfigurationIdByChainId,
getTokenList,
} from '../../../../selectors';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import {
} from '@metamask/network-controller';
import { getCurrentChainId } from '../../../../../shared/modules/selectors/networks';
import {
getCurrentCurrency,
getCurrentNetwork,
getSelectedAccountCachedBalance,
} from '../../../../selectors';
import { getNativeCurrency } from '../../../../ducks/metamask/metamask';
import {
getCurrentCurrency,
getNativeCurrency,
} from '../../../../ducks/metamask/metamask';
import { useCurrencyDisplay } from '../../../../hooks/useCurrencyDisplay';
import { AssetType } from '../../../../../shared/constants/transaction';
import { Box } from '../../../component-library';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { useI18nContext } from '../../../../hooks/useI18nContext';
///: END:ONLY_INCLUDE_IF
import { NetworkListItem } from '../../network-list-item';
import { getNetworkConfigurationsByChainId } from '../../../../../shared/modules/selectors/networks';
import { getCurrentCurrency } from '../../../../selectors';
import { getCurrentCurrency } from '../../../../ducks/metamask/metamask';
import { formatCurrency } from '../../../../helpers/utils/confirm-tx.util';
import { useMultichainBalances } from '../../../../hooks/useMultichainBalances';
import { NETWORK_TO_SHORT_NETWORK_NAME_MAP } from '../../../../../shared/constants/bridge';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { renderWithProvider } from '../../../../../test/lib/render-helpers';
import mockState from '../../../../../test/data/mock-send-state.json';
import { AssetType } from '../../../../../shared/constants/transaction';
import {
getCurrentCurrency,
getNativeCurrencyImage,
getSelectedAccountCachedBalance,
getSelectedInternalAccount,
Expand All @@ -24,6 +23,7 @@ import {
getConversionRate,
getNativeCurrency,
getTokens,
getCurrentCurrency,
} from '../../../../ducks/metamask/metamask';
import { getTopAssets } from '../../../../ducks/swaps/swaps';
import { getRenderableTokenData } from '../../../../hooks/useTokensToSearch';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
} from '../../../../../shared/modules/selectors/networks';
import {
getAllTokens,
getCurrentCurrency,
getNativeCurrencyImage,
getSelectedAccountCachedBalance,
getSelectedInternalAccount,
Expand All @@ -46,6 +45,7 @@ import {
} from '../../../../selectors';
import {
getConversionRate,
getCurrentCurrency,
getNativeCurrency,
} from '../../../../ducks/metamask/metamask';
import { useTokenTracker } from '../../../../hooks/useTokenTracker';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
getNativeCurrency,
getConversionRate,
getGasFeeEstimates,
getCurrentCurrency,
} from '../../../../../../../ducks/metamask/metamask';
import { getUsedSwapsGasPrice } from '../../../../../../../ducks/swaps/swaps';
import {
getCurrentCurrency,
checkNetworkAndAccountSupports1559,
getIsSwapsChain,
} from '../../../../../../../selectors';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
getConversionRate,
getGasFeeEstimates,
getNativeCurrency,
getCurrentCurrency,
} from '../../../../../../../ducks/metamask/metamask';
import { EtherDenomination } from '../../../../../../../../shared/constants/common';
import {
getCurrentCurrency,
checkNetworkAndAccountSupports1559,
getIsSwapsChain,
} from '../../../../../../../selectors/selectors';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { zeroAddress } from 'ethereumjs-util';
import { MarketDataDetails } from '@metamask/assets-controllers';
import { getIntlLocale } from '../../../../../ducks/locale/locale';
import {
getCurrentCurrency,
getSelectedAccountCachedBalance,
getTokensMarketData,
} from '../../../../../selectors';
import { getCurrentChainId } from '../../../../../../shared/modules/selectors/networks';
import {
getConversionRate,
getCurrentCurrency,
getNativeCurrency,
} from '../../../../../ducks/metamask/metamask';
import { PercentageAndAmountChange } from './percentage-and-amount-change';
Expand All @@ -25,7 +25,6 @@ jest.mock('../../../../../ducks/locale/locale', () => ({
}));

jest.mock('../../../../../selectors', () => ({
getCurrentCurrency: jest.fn(),
getSelectedAccountCachedBalance: jest.fn(),
getTokensMarketData: jest.fn(),
}));
Expand All @@ -35,6 +34,7 @@ jest.mock('../../../../../../shared/modules/selectors/networks', () => ({
}));

jest.mock('../../../../../ducks/metamask/metamask', () => ({
getCurrentCurrency: jest.fn(),
getConversionRate: jest.fn(),
getNativeCurrency: jest.fn(),
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
} from '../../../../../helpers/constants/design-system';
import { getCurrentChainId } from '../../../../../../shared/modules/selectors/networks';
import {
getCurrentCurrency,
getSelectedAccountCachedBalance,
getTokensMarketData,
} from '../../../../../selectors';
Expand All @@ -20,6 +19,7 @@ import { EtherDenomination } from '../../../../../../shared/constants/common';
import { Numeric } from '../../../../../../shared/modules/Numeric';
import {
getConversionRate,
getCurrentCurrency,
getNativeCurrency,
} from '../../../../../ducks/metamask/metamask';
import {
Expand Down
4 changes: 4 additions & 0 deletions ui/ducks/metamask/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,7 @@ export function doesUserHaveALedgerAccount(state) {
return kr.type === KeyringType.ledger;
});
}

export function getCurrentCurrency(state) {
return state.metamask.currentCurrency;
}
9 changes: 9 additions & 0 deletions ui/ducks/metamask/metamask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import reduceMetamask, {
getSendHexDataFeatureFlagState,
getSendToAccounts,
isNotEIP1559Network,
getCurrentCurrency,
} from './metamask';

jest.mock('@metamask/transaction-controller', () => ({
Expand Down Expand Up @@ -126,6 +127,7 @@ describe('MetaMask Reducers', () => {
conversionRate: 1200.88200327,
},
},
currentCurrency: 'usd',
...mockNetworkState({ chainId: CHAIN_IDS.GOERLI }),
accounts: {
'0xfdea65c8e26263f6d9a1b5de9555d2931a33b825': {
Expand Down Expand Up @@ -384,6 +386,13 @@ describe('MetaMask Reducers', () => {
});
});

describe('getCurrentCurrency', () => {
it('should return the `currentCurrency`', () => {
const currentCurrency = getCurrentCurrency(mockState);
expect(currentCurrency).toStrictEqual('usd');
});
});

describe('getSendHexDataFeatureFlagState()', () => {
it('should return the sendHexData feature flag state', () => {
expect(getSendHexDataFeatureFlagState(mockState)).toStrictEqual(true);
Expand Down
3 changes: 2 additions & 1 deletion ui/hooks/bridge/events/useConvertedUsdAmounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
getToTokenConversionRate,
getFromAmount,
} from '../../../ducks/bridge/selectors';
import { getCurrentCurrency, getUSDConversionRate } from '../../../selectors';
import { getCurrentCurrency } from '../../../ducks/metamask/metamask';
import { getUSDConversionRate } from '../../../selectors';
import { tokenAmountToCurrency } from '../../../ducks/bridge/utils';

const USD_CURRENCY_CODE = 'usd';
Expand Down
7 changes: 2 additions & 5 deletions ui/hooks/bridge/useBridgeExchangeRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@ import {
getQuoteRequest,
getToChain,
} from '../../ducks/bridge/selectors';
import {
getCurrentCurrency,
getMarketData,
getParticipateInMetaMetrics,
} from '../../selectors';
import { getMarketData, getParticipateInMetaMetrics } from '../../selectors';
import { getCurrentCurrency } from '../../ducks/metamask/metamask';
import { decimalToPrefixedHex } from '../../../shared/modules/conversion.utils';
import { getCurrentChainId } from '../../../shared/modules/selectors/networks';
import {
Expand Down
Loading

0 comments on commit da16971

Please sign in to comment.