Skip to content

Commit 889bfa4

Browse files
authored
fix: Aggregated balance for enabled networks (#33967)
<!-- 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** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/33967?quickstart=1) ## **Related issues** Fixes: #33936 ## **Manual testing steps** 1. Go to this page... 2. 3. ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **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/main/.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/main/.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.
1 parent 7be1951 commit 889bfa4

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

ui/components/app/wallet-overview/aggregated-percentage-overview-cross-chains.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ export const AggregatedPercentageOverviewCrossChains = () => {
113113
const amountChangeCrossChains =
114114
totalCrossChainBalance - crossChainTotalBalance1dAgo;
115115
const percentageChangeCrossChains =
116-
(amountChangeCrossChains / crossChainTotalBalance1dAgo) * 100 || 0;
116+
crossChainTotalBalance1dAgo === 0
117+
? 0
118+
: (amountChangeCrossChains / crossChainTotalBalance1dAgo) * 100;
117119

118120
const formattedPercentChangeCrossChains = formatValue(
119121
amountChangeCrossChains === 0 ? 0 : percentageChangeCrossChains,

ui/hooks/useAccountTotalCrossChainFiatBalance.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
import {
77
getCrossChainTokenExchangeRates,
88
getCrossChainMetaMaskCachedBalances,
9+
getEnabledNetworksByNamespace,
910
} from '../selectors';
1011
import {
1112
getCurrentCurrency,
@@ -25,6 +26,7 @@ jest.mock('react-redux', () => ({
2526
jest.mock('../selectors', () => ({
2627
getCrossChainTokenExchangeRates: jest.fn(),
2728
getCrossChainMetaMaskCachedBalances: jest.fn(),
29+
getEnabledNetworksByNamespace: jest.fn(),
2830
}));
2931
jest.mock('../ducks/metamask/metamask', () => ({
3032
getCurrentCurrency: jest.fn(),
@@ -49,6 +51,9 @@ const mockGetCrossChainTokenExchangeRates = jest.mocked(
4951
const mockGetCrossChainMetaMaskCachedBalances = jest.mocked(
5052
getCrossChainMetaMaskCachedBalances,
5153
);
54+
const mockGetEnabledNetworksByNamespace = jest.mocked(
55+
getEnabledNetworksByNamespace,
56+
);
5257

5358
const mockUseTokenBalances = jest.fn().mockReturnValue({
5459
tokenBalances: {
@@ -193,6 +198,11 @@ describe('useAccountTotalCrossChainFiatBalance', () => {
193198
mockCachedBalances(),
194199
);
195200

201+
mockGetEnabledNetworksByNamespace.mockReturnValue({
202+
'0x1': {},
203+
'0xe708': {},
204+
});
205+
196206
jest.clearAllMocks();
197207
});
198208

ui/hooks/useAccountTotalCrossChainFiatBalance.ts

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import {
99
getCrossChainTokenExchangeRates,
1010
getCrossChainMetaMaskCachedBalances,
11+
getEnabledNetworksByNamespace,
1112
} from '../selectors';
1213
import {
1314
getValueFromWeiHex,
@@ -36,6 +37,9 @@ export const useAccountTotalCrossChainFiatBalance = (
3637
formattedTokensWithBalancesPerChain: FormattedTokensWithBalances[],
3738
) => {
3839
const allNetworks = useSelector(getNetworkConfigurationsByChainId);
40+
41+
const enabledNetworksByNamespace = useSelector(getEnabledNetworksByNamespace);
42+
3943
const currencyRates = useSelector(getCurrencyRates);
4044
const currentCurrency = useSelector(getCurrentCurrency);
4145

@@ -53,9 +57,26 @@ export const useAccountTotalCrossChainFiatBalance = (
5357
[crossChainContractRates],
5458
);
5559

60+
const filteredBalances = useMemo(() => {
61+
return formattedTokensWithBalancesPerChain
62+
.map((balances) => {
63+
if (
64+
Object.keys(enabledNetworksByNamespace).includes(
65+
balances.chainId.toString(),
66+
)
67+
) {
68+
return balances;
69+
}
70+
return null;
71+
})
72+
.filter(
73+
(balance): balance is FormattedTokensWithBalances => balance !== null,
74+
);
75+
}, [formattedTokensWithBalancesPerChain, enabledNetworksByNamespace]);
76+
5677
const tokenFiatBalancesCrossChains = useMemo(
5778
() =>
58-
formattedTokensWithBalancesPerChain.map((singleChainTokenBalances) => {
79+
filteredBalances.map((singleChainTokenBalances) => {
5980
const { tokensWithBalances } = singleChainTokenBalances;
6081
// Attempt to use known currency symbols in map
6182
// Otherwise fallback to user defined currency
@@ -67,23 +88,25 @@ export const useAccountTotalCrossChainFiatBalance = (
6788
.nativeCurrency;
6889
const conversionRate =
6990
currencyRates?.[matchedChainSymbol]?.conversionRate;
70-
const tokenFiatBalances = tokensWithBalances.map((token) => {
71-
const tokenExchangeRate =
72-
mergedCrossChainRates?.[singleChainTokenBalances.chainId]?.[
73-
toChecksumAddress(token.address)
74-
];
75-
const totalFiatValue = getTokenFiatAmount(
76-
tokenExchangeRate,
77-
conversionRate,
78-
currentCurrency,
79-
token.string,
80-
token.symbol,
81-
false,
82-
false,
83-
);
91+
const tokenFiatBalances = tokensWithBalances.map(
92+
(token: TokenWithBalance) => {
93+
const tokenExchangeRate =
94+
mergedCrossChainRates?.[singleChainTokenBalances.chainId]?.[
95+
toChecksumAddress(token.address)
96+
];
97+
const totalFiatValue = getTokenFiatAmount(
98+
tokenExchangeRate,
99+
conversionRate,
100+
currentCurrency,
101+
token.string,
102+
token.symbol,
103+
false,
104+
false,
105+
);
84106

85-
return totalFiatValue;
86-
});
107+
return totalFiatValue;
108+
},
109+
);
87110

88111
const balanceCached =
89112
crossChainCachedBalances?.[singleChainTokenBalances.chainId]?.[
@@ -102,7 +125,7 @@ export const useAccountTotalCrossChainFiatBalance = (
102125
};
103126
}),
104127
[
105-
formattedTokensWithBalancesPerChain,
128+
filteredBalances,
106129
allNetworks,
107130
currencyRates,
108131
mergedCrossChainRates,
@@ -117,7 +140,7 @@ export const useAccountTotalCrossChainFiatBalance = (
117140
tokenFiatBalancesCrossChains.reduce((accumulator, currentValue) => {
118141
const tmpCurrentValueFiatBalances: string[] =
119142
currentValue.tokenFiatBalances.filter(
120-
(value): value is string => value !== undefined,
143+
(value: string | undefined): value is string => value !== undefined,
121144
);
122145
const totalFiatBalance = sumDecimals(
123146
currentValue.nativeFiatValue,

0 commit comments

Comments
 (0)