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: use new multichain selectors in accounts related components #25290

Merged
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c782bcd
test(multichain): deep clone evm/non-evm state for each tests
ccharly Jun 13, 2024
00dbae1
feat(multichain): better typing + onboarding checks in selectors
ccharly Jun 13, 2024
491bcf2
fix(multichain): fix getMultichain{CurrentCurrency,DefaultToken}
ccharly Jun 13, 2024
94621da
feat(multichain): add getMultichain{IsMainnet,CurrentChainId} selectors
ccharly Jun 13, 2024
68c1239
refactor(ui): use multichain selectors in AssetList
ccharly Jun 13, 2024
6670309
refactor(ui): use multichain selectors in UserPreferencedCurrencyDisplay
ccharly Jun 13, 2024
0ff7cbb
fix(ui): fix AvatarNetwork alt
ccharly Jun 13, 2024
7eb1846
refactor(ui): use multichain selectors in RampsCard
ccharly Jun 13, 2024
fa3c43a
refactor(ui): use multichain selectors in TokenListItem
ccharly Jun 13, 2024
a328892
refactor(hooks): use multichain selectors in useCurrencyDisplay
ccharly Jun 13, 2024
82bf937
refactor(hooks): use multichain selectors in useUserPreferencedCurrency
ccharly Jun 13, 2024
cefc849
test(hooks): fix missing multichain stubs/mocks
ccharly Jun 13, 2024
9315087
test: re-generate AssetPage snapshot
ccharly Jun 13, 2024
d54864a
refactor(ui): remove some TODO/FIXME
ccharly Jun 13, 2024
76598c6
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
b9e6353
refactor(ui): remove changes related to bitcoin account overview support
ccharly Jun 13, 2024
9023b7d
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
11cd32c
refactor(multichain): use ternary in getMaybeSelectedInternalAccount
ccharly Jun 13, 2024
6be2973
refactor(multichain): better error message
ccharly Jun 13, 2024
cf24e63
docs(multichain): add jsdoc for getMultichainProviderConfig
ccharly Jun 13, 2024
907559f
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
c2f7a53
chore: lint
ccharly Jun 13, 2024
be53a20
docs(multichain): fix doc for getMultichainProviderConfig
ccharly Jun 13, 2024
79b91bd
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
74aef5a
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
de1e55d
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 13, 2024
99d9128
Merge branch 'develop' into refactor/use-multichain-selectors-in-acco…
ccharly Jun 14, 2024
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
31 changes: 17 additions & 14 deletions ui/components/app/asset-list/asset-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
import { useUserPreferencedCurrency } from '../../../hooks/useUserPreferencedCurrency';
import {
getSelectedAccountCachedBalance,
getShouldShowFiat,
getNativeCurrencyImage,
getDetectedTokensInCurrentNetwork,
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
getShouldHideZeroBalanceTokens,
///: BEGIN:ONLY_INCLUDE_IF(build-main,build-beta,build-flask)
getIsBuyableChain,
///: END:ONLY_INCLUDE_IF
getCurrentNetwork,
getSelectedAccount,
getPreferences,
getIsMainnet,
} from '../../../selectors';
import {
getNativeCurrency,
getProviderConfig,
} from '../../../ducks/metamask/metamask';
getMultichainCurrentNetwork,
getMultichainNativeCurrency,
getMultichainIsEvm,
getMultichainShouldShowFiat,
getMultichainCurrencyImage,
getMultichainIsMainnet,
} from '../../../selectors/multichain';
import { useCurrencyDisplay } from '../../../hooks/useCurrencyDisplay';
import { MetaMetricsContext } from '../../../contexts/metametrics';
import {
Expand Down Expand Up @@ -53,12 +53,13 @@ import {
const AssetList = ({ onClickAsset }) => {
const [showDetectedTokens, setShowDetectedTokens] = useState(false);
const selectedAccountBalance = useSelector(getSelectedAccountCachedBalance);
const nativeCurrency = useSelector(getNativeCurrency);
const showFiat = useSelector(getShouldShowFiat);
const { chainId } = useSelector(getCurrentNetwork);
const isMainnet = useSelector(getIsMainnet);
const nativeCurrency = useSelector(getMultichainNativeCurrency);
const showFiat = useSelector(getMultichainShouldShowFiat);
const isMainnet = useSelector(getMultichainIsMainnet);
const { useNativeCurrencyAsPrimaryCurrency } = useSelector(getPreferences);
const { ticker, type, rpcUrl } = useSelector(getProviderConfig);
const { chainId, ticker, type, rpcUrl } = useSelector(
getMultichainCurrentNetwork,
);
const isOriginalNativeSymbol = useIsOriginalNativeTokenSymbol(
chainId,
ticker,
Expand Down Expand Up @@ -94,7 +95,7 @@ const AssetList = ({ onClickAsset }) => {
currency: secondaryCurrency,
});

const primaryTokenImage = useSelector(getNativeCurrencyImage);
const primaryTokenImage = useSelector(getMultichainCurrencyImage);
const detectedTokens = useSelector(getDetectedTokensInCurrentNetwork) || [];
const isTokenDetectionInactiveOnNonMainnetSupportedNetwork = useSelector(
getIstokenDetectionInactiveOnNonMainnetSupportedNetwork,
Expand All @@ -112,7 +113,9 @@ const AssetList = ({ onClickAsset }) => {
const shouldShowBuy = isBuyableChain && balanceIsZero;
///: END:ONLY_INCLUDE_IF

let isStakeable = isMainnet;
const isEvm = useSelector(getMultichainIsEvm);

let isStakeable = isMainnet && isEvm;
///: BEGIN:ONLY_INCLUDE_IF(build-mmi)
isStakeable = false;
///: END:ONLY_INCLUDE_IF
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import React, { useMemo } from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import PropTypes from 'prop-types';
import { EtherDenomination } from '../../../../shared/constants/common';
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
import CurrencyDisplay from '../../ui/currency-display';
import { useUserPreferencedCurrency } from '../../../hooks/useUserPreferencedCurrency';
import { AvatarNetwork, AvatarNetworkSize } from '../../component-library';
import { getCurrentNetwork } from '../../../selectors';
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
import {
getMultichainNativeCurrency,
getMultichainCurrentNetwork,
} from '../../../selectors/multichain';

/* eslint-disable jsdoc/require-param-name */
// eslint-disable-next-line jsdoc/require-param
Expand All @@ -24,9 +26,10 @@ export default function UserPreferencedCurrencyDisplay({
showCurrencySuffix,
...restProps
}) {
const currentNetwork = useSelector(getCurrentNetwork);
const nativeCurrency = useSelector(getNativeCurrency);
const currentNetwork = useSelector(getMultichainCurrentNetwork);
const nativeCurrency = useSelector(getMultichainNativeCurrency);
const { currency, numberOfDecimals } = useUserPreferencedCurrency(type, {
currency: nativeCurrency, // Required for non-EVM support for now
montelaidev marked this conversation as resolved.
Show resolved Hide resolved
ethNumberOfDecimals,
fiatNumberOfDecimals,
numberOfDecimals: propsNumberOfDecimals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const AvatarNetwork: AvatarNetworkComponent = React.forwardRef(
}
onError={handleOnError}
src={src}
alt={`${name} logo` || 'network logo'}
alt={(name && `${name} logo`) || 'network logo'}
/>
</>
)}
Expand Down
13 changes: 9 additions & 4 deletions ui/components/multichain/ramps-card/ramps-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
TextVariant,
} from '../../../helpers/constants/design-system';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { getCurrentNetwork, getSwapsDefaultToken } from '../../../selectors';
import {
getMultichainDefaultToken,
getMultichainCurrentNetwork,
} from '../../../selectors/multichain';
import {
MetaMetricsEventCategory,
MetaMetricsEventName,
Expand Down Expand Up @@ -68,14 +71,15 @@ export const RampsCard = ({ variant }) => {
const { openBuyCryptoInPdapp } = useRamps(metamaskEntryMap[variant]);
const trackEvent = useContext(MetaMetricsContext);
const currentLocale = useSelector(getCurrentLocale);
const { chainId, nickname } = useSelector(getCurrentNetwork);
const { symbol = 'ETH' } = useSelector(getSwapsDefaultToken);
const { chainId, nickname } = useSelector(getMultichainCurrentNetwork);
const { symbol } = useSelector(getMultichainDefaultToken);

useEffect(() => {
trackEvent({
event: MetaMetricsEventName.EmptyBuyBannerDisplayed,
category: MetaMetricsEventCategory.Navigation,
properties: {
// FIXME: This might not be a number for non-EVM networks
chain_id: chainId,
locale: currentLocale,
network: nickname,
Expand All @@ -85,13 +89,14 @@ export const RampsCard = ({ variant }) => {
}, [currentLocale, chainId, nickname, trackEvent]);

const onClick = useCallback(() => {
openBuyCryptoInPdapp();
openBuyCryptoInPdapp(chainId);
montelaidev marked this conversation as resolved.
Show resolved Hide resolved
trackEvent({
event: MetaMetricsEventName.NavBuyButtonClicked,
category: MetaMetricsEventCategory.Navigation,
properties: {
location: `${variant} tab`,
text: `Buy ${symbol}`,
// FIXME: This might not be a number for non-EVM networks
chain_id: chainId,
token_symbol: symbol,
},
Expand Down
4 changes: 2 additions & 2 deletions ui/components/multichain/token-list-item/token-list-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import { ModalContent } from '../../component-library/modal-content/deprecated';
import { ModalHeader } from '../../component-library/modal-header/deprecated';
import {
getCurrentChainId,
getCurrentNetwork,
getMetaMetricsId,
getNativeCurrencyImage,
getPreferences,
getTestNetworkBackgroundColor,
} from '../../../selectors';
import { getMultichainCurrentNetwork } from '../../../selectors/multichain';
import Tooltip from '../../ui/tooltip';
import { useI18nContext } from '../../../hooks/useI18nContext';
import { MetaMetricsContext } from '../../../contexts/metametrics';
Expand Down Expand Up @@ -132,7 +132,7 @@ export const TokenListItem = ({
</Box>
);
// Used for badge icon
const currentNetwork = useSelector(getCurrentNetwork);
const currentNetwork = useSelector(getMultichainCurrentNetwork);
const testNetworkBackgroundColor = useSelector(getTestNetworkBackgroundColor);

return (
Expand Down
72 changes: 42 additions & 30 deletions ui/hooks/useCurrencyDisplay.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { useMemo } from 'react';
import { useSelector } from 'react-redux';
import BigNumber from 'bignumber.js';
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
import { getCurrentCurrency } from '../selectors';
import {
getConversionRate,
getNativeCurrency,
} from '../ducks/metamask/metamask';
getMultichainCurrentCurrency,
getMultichainIsEvm,
getMultichainNativeCurrency,
} from '../selectors/multichain';
import { getConversionRate } from '../ducks/metamask/metamask';

import { getValueFromWeiHex } from '../../shared/modules/conversion.utils';
import { TEST_NETWORK_TICKER_MAP } from '../../shared/constants/network';
Expand Down Expand Up @@ -60,40 +61,51 @@ export function useCurrencyDisplay(
inputValue,
{ displayValue, prefix, numberOfDecimals, denomination, currency, ...opts },
) {
const currentCurrency = useSelector(getCurrentCurrency);
const nativeCurrency = useSelector(getNativeCurrency);
const isEvm = useSelector(getMultichainIsEvm);
const currentCurrency = useSelector(getMultichainCurrentCurrency);
const nativeCurrency = useSelector(getMultichainNativeCurrency);
const conversionRate = useSelector(getConversionRate);
const isUserPreferredCurrency = currency === currentCurrency;

const value = useMemo(() => {
if (displayValue) {
return displayValue;
}
if (
currency === nativeCurrency ||
(!isUserPreferredCurrency && !nativeCurrency)
) {
const ethDisplayValue = new Numeric(inputValue, 16, EtherDenomination.WEI)
.toDenomination(denomination || EtherDenomination.ETH)
.round(numberOfDecimals || DEFAULT_PRECISION)
.toBase(10)
.toString();

return ethDisplayValue === '0' && inputValue && Number(inputValue) !== 0
? MIN_AMOUNT_DISPLAY
: ethDisplayValue;
} else if (isUserPreferredCurrency && conversionRate) {
return formatCurrency(
getValueFromWeiHex({
value: inputValue,
fromCurrency: nativeCurrency,
toCurrency: currency,
conversionRate,
numberOfDecimals: numberOfDecimals || 2,
toDenomination: denomination,
}),
currency,
);
if (isEvm) {
if (
currency === nativeCurrency ||
(!isUserPreferredCurrency && !nativeCurrency)
) {
const ethDisplayValue = new Numeric(
inputValue,
16,
EtherDenomination.WEI,
)
.toDenomination(denomination || EtherDenomination.ETH)
.round(numberOfDecimals || DEFAULT_PRECISION)
.toBase(10)
.toString();

return ethDisplayValue === '0' && inputValue && Number(inputValue) !== 0
? MIN_AMOUNT_DISPLAY
: ethDisplayValue;
} else if (isUserPreferredCurrency && conversionRate) {
return formatCurrency(
getValueFromWeiHex({
value: inputValue,
fromCurrency: nativeCurrency,
toCurrency: currency,
conversionRate,
numberOfDecimals: numberOfDecimals || 2,
toDenomination: denomination,
}),
currency,
);
}
} else {
// For non-EVM we assume the input value can be formatted "as-is"
return formatCurrency(inputValue, currency);
}
return null;
}, [
Expand Down
17 changes: 15 additions & 2 deletions ui/hooks/useCurrencyDisplay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { renderHook } from '@testing-library/react-hooks';
import * as reactRedux from 'react-redux';
import sinon from 'sinon';
import { getCurrentCurrency } from '../selectors';
import {
getMultichainCurrentCurrency,
getMultichainIsEvm,
getMultichainNativeCurrency,
} from '../selectors/multichain';
import {
getConversionRate,
getNativeCurrency,
Expand Down Expand Up @@ -128,9 +133,17 @@ describe('useCurrencyDisplay', () => {
describe(`when input is { value: ${value}, decimals: ${restProps.numberOfDecimals}, denomation: ${restProps.denomination} }`, () => {
const stub = sinon.stub(reactRedux, 'useSelector');
stub.callsFake((selector) => {
if (selector === getCurrentCurrency) {
if (selector === getMultichainIsEvm) {
return true;
} else if (
selector === getCurrentCurrency ||
selector === getMultichainCurrentCurrency
) {
return 'usd';
} else if (selector === getNativeCurrency) {
} else if (
selector === getNativeCurrency ||
selector === getMultichainNativeCurrency
) {
return 'ETH';
} else if (selector === getConversionRate) {
return 280.45;
Expand Down
25 changes: 21 additions & 4 deletions ui/hooks/useTransactionDisplayData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { CHAIN_IDS } from '../../shared/constants/network';
import { TransactionGroupCategory } from '../../shared/constants/transaction';
import { formatDateWithYearContext } from '../helpers/utils/util';
import { getMessage } from '../helpers/utils/i18n-helper';
import {
getMultichainCurrentCurrency,
getMultichainIsEvm,
getMultichainNativeCurrency,
getMultichainShouldShowFiat,
} from '../selectors/multichain';
import * as i18nhooks from './useI18nContext';
import * as useTokenFiatAmountHooks from './useTokenFiatAmount';
import { useTransactionDisplayData } from './useTransactionDisplayData';
Expand Down Expand Up @@ -201,7 +207,9 @@ describe('useTransactionDisplayData', () => {
getMessage('en', messages, key, variables),
);
useSelector.callsFake((selector) => {
if (selector === getTokens) {
if (selector === getMultichainIsEvm) {
return true;
} else if (selector === getTokens) {
return [
{
address: '0xabca64466f257793eaa52fcfff5066894b76a149',
Expand All @@ -213,11 +221,20 @@ describe('useTransactionDisplayData', () => {
return {
useNativeCurrencyAsPrimaryCurrency: true,
};
} else if (selector === getShouldShowFiat) {
} else if (
selector === getShouldShowFiat ||
selector === getMultichainShouldShowFiat
) {
return false;
} else if (selector === getNativeCurrency) {
} else if (
selector === getNativeCurrency ||
selector === getMultichainNativeCurrency
) {
return 'ETH';
} else if (selector === getCurrentCurrency) {
} else if (
selector === getCurrentCurrency ||
selector === getMultichainCurrentCurrency
) {
return 'ETH';
} else if (selector === getCurrentChainId) {
return CHAIN_IDS.MAINNET;
Expand Down
17 changes: 9 additions & 8 deletions ui/hooks/useUserPreferencedCurrency.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { shallowEqual, useSelector } from 'react-redux';
import { getPreferences } from '../selectors';
import {
getPreferences,
getShouldShowFiat,
getCurrentCurrency,
} from '../selectors';
import { getNativeCurrency } from '../ducks/metamask/metamask';
getMultichainNativeCurrency,
getMultichainCurrentCurrency,
getMultichainShouldShowFiat,
} from '../selectors/multichain';

import { PRIMARY, SECONDARY } from '../helpers/constants/common';
import { EtherDenomination } from '../../shared/constants/common';
Expand Down Expand Up @@ -41,13 +41,14 @@ import { ETH_DEFAULT_DECIMALS } from '../constants';
* @returns {UserPreferredCurrency}
*/
export function useUserPreferencedCurrency(type, opts = {}) {
const nativeCurrency = useSelector(getNativeCurrency);
const nativeCurrency = useSelector(getMultichainNativeCurrency);

const { useNativeCurrencyAsPrimaryCurrency } = useSelector(
getPreferences,
shallowEqual,
);
const showFiat = useSelector(getShouldShowFiat);
const currentCurrency = useSelector(getCurrentCurrency);
const showFiat = useSelector(getMultichainShouldShowFiat);
const currentCurrency = useSelector(getMultichainCurrentCurrency);

const fiatReturn = {
currency: currentCurrency,
Expand Down
Loading