Skip to content

Commit edbdd5d

Browse files
chore(runway): cherry-pick fix: MUSD-771 updated selectAccountTokensAcrossChainsUnified to select tokens across chains for evm-scope specifically (#29762)
- fix: MUSD-771 updated selectAccountTokensAcrossChainsUnified to select tokens across chains for evm-scope specifically cp-7.76.0 (#29675) <!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until this PR meets the canonical Definition of Ready For Review in `docs/readme/ready-for-review.md`. In short: the template must be materially complete (not just section titles present), all status checks must be currently passing, and the only expected follow-up commits must be reviewer-driven. --> ## **Description** This PR fixes an app crash that occurs when the user clicks the "unstake" button (ETH pooled-staking) when non-evm network is selected. <!-- 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? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: fixed app crash occurring when user clicked pooled-staking unstake button when non-evm network is selected. ## **Related issues** Fixes: [MUSD-771: App Crash when user clicks pooled-staking "unstake" button](https://consensyssoftware.atlassian.net/browse/MUSD-771) ## **Manual testing steps** ```gherkin Feature: Fix app crash when clicking "unstake" button (pooled-staking) Scenario: App crash fixed Given user has Staked ETH to unstake When user clicks "Tokens" section header on home screen And user selects non-EVM network (e.g. Tron) in network selector And user returns to home screen And user clicks Ethereum asset row And user clicks "unstake" button on Ethereum asset details screen Then user is redirected to unstake screen with keypad And the app does not crash ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> https://github.com/user-attachments/assets/a5ec5ab5-f4e2-47a3-b780-f6b289364ab7 ### **After** <!-- [screenshots/recordings] --> https://github.com/user-attachments/assets/3661139d-ab90-4437-9127-ac94830eda88 ## **Pre-merge author checklist** <!-- Every checklist item must be consciously assessed before marking this PR as "Ready for review". A checked box means you deliberately considered that responsibility, not that you literally performed every action listed. Unchecked boxes are ambiguous: they are not an implicit "N/A" and they are not a silent "skip". See `docs/readme/ready-for-review.md` for the full checklist semantics. --> - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. #### Performance checks (if applicable) - [ ] I've tested on Android - Ideally on a mid-range device; emulator is acceptable - [ ] I've tested with a power user scenario - Use these [power-user SRPs](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/edit-v2/401401446401?draftShareId=9d77e1e1-4bdc-4be1-9ebb-ccd916988d93) to import wallets with many accounts and tokens - [ ] I've instrumented key operations with Sentry traces for production performance metrics - See [`trace()`](/app/util/trace.ts) for usage and [`addToken`](/app/components/Views/AddAsset/components/AddCustomToken/AddCustomToken.tsx#L274) for an example For performance guidelines and tooling, see the [Performance Guide](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/400085549067/Performance+Guide+for+Engineers). ## **Pre-merge reviewer checklist** <!-- Reviewer checklist items follow the same semantics as the author checklist: an unchecked box is ambiguous, a checked box means the reviewer consciously assessed that responsibility. See `docs/readme/ready-for-review.md`. --> - [ ] 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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Medium risk because it changes how the unified multichain token selector chooses the EVM address, which can affect token lists and Earn flows across networks; however the change is localized and covered by updated unit tests/mocks. > > **Overview** > Fixes a crash/empty-balance issue when a non-EVM network is active by making `selectAccountTokensAcrossChainsUnified` always derive EVM tokens from the *EVM-scoped* account in the selected account group, rather than whatever account is currently selected. > > Adds `selectAccountTokensAcrossChainsForEvmScope` (using `selectSelectedInternalAccountByScope(EVM_SCOPE)` + `selectAccountTokensAcrossChainsForAddress`) and updates multichain/Earn-related tests to mock `selectSelectedInternalAccountByScope` and the new EVM-token selector signature. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d5602d9. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> [0a39d7f](0a39d7f) Co-authored-by: Matthew Grainger <46547583+Matt561@users.noreply.github.com>
1 parent aeb0da4 commit edbdd5d

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

app/components/UI/Earn/hooks/useEarnTokens.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ jest.mock('../selectors/featureFlags', () => ({
3737
mockSelectStablecoinLendingEnabledFlag(),
3838
}));
3939

40+
jest.mock('../../../../selectors/multichainAccounts/accounts', () => ({
41+
selectSelectedInternalAccountByScope: () => () =>
42+
jest.requireActual('../../../../util/test/accountsControllerTestUtils')
43+
.internalAccount2,
44+
}));
45+
4046
const MOCK_ROOT_STATE_WITH_EARN_CONTROLLER = mockEarnControllerRootState();
4147
const MOCK_RATE = {
4248
price: 0.99,

app/selectors/earnController/earn/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ jest.mock('../../../components/UI/Earn/selectors/featureFlags', () => ({
5959
prioritizeFlagsByEnv: jest.fn().mockReturnValue(true),
6060
}));
6161

62+
jest.mock('../../../selectors/multichainAccounts/accounts', () => ({
63+
selectSelectedInternalAccountByScope: () => () =>
64+
jest.requireActual('../../../util/test/accountsControllerTestUtils')
65+
.internalAccount2,
66+
}));
67+
6268
const MOCK_ROOT_STATE_WITH_EARN_CONTROLLER = mockEarnControllerRootState();
6369
const MOCK_RATE = {
6470
price: 0.99,

app/selectors/multichain/multichain.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,16 @@ describe('MultichainNonEvm Selectors', () => {
690690
selectAccountTokensAcrossChains: () => mockEvmTokensByChain,
691691
}));
692692

693+
jest.doMock('./evm', () => ({
694+
selectAccountTokensAcrossChainsForAddress: () => mockEvmTokensByChain,
695+
}));
696+
697+
jest.doMock('../multichainAccounts/accounts', () => ({
698+
selectSelectedInternalAccountByScope: () => () => ({
699+
address: '0xMockEvmAddress',
700+
}),
701+
}));
702+
693703
jest.doMock('../multichainAccounts/accountTreeController', () => ({
694704
selectSelectedAccountGroupInternalAccounts: () =>
695705
mockSelectedGroupAccounts,
@@ -727,6 +737,16 @@ describe('MultichainNonEvm Selectors', () => {
727737
selectAccountTokensAcrossChains: () => mockEvmTokensByChain,
728738
}));
729739

740+
jest.doMock('./evm', () => ({
741+
selectAccountTokensAcrossChainsForAddress: () => mockEvmTokensByChain,
742+
}));
743+
744+
jest.doMock('../multichainAccounts/accounts', () => ({
745+
selectSelectedInternalAccountByScope: () => () => ({
746+
address: '0xMockEvmAddress',
747+
}),
748+
}));
749+
730750
jest.doMock('../multichainAccounts/accountTreeController', () => ({
731751
selectSelectedAccountGroupInternalAccounts: () =>
732752
mockSelectedGroupAccounts,

app/selectors/multichain/multichain.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ import {
4545
import { TokenI } from '../../components/UI/Tokens/types';
4646
import { createSelector } from 'reselect';
4747
import { selectSelectedAccountGroupInternalAccounts } from '../multichainAccounts/accountTreeController';
48-
import { selectAccountTokensAcrossChains } from '../multichain';
48+
import { selectAccountTokensAcrossChainsForAddress } from './evm';
49+
import { selectSelectedInternalAccountByScope } from '../multichainAccounts/accounts';
50+
import { EVM_SCOPE } from '../../components/UI/Earn/constants/networks';
4951
import { MULTICHAIN_ACCOUNT_TYPE_TO_MAINNET } from '../../core/Multichain/constants';
5052
import { isTronSpecialAsset } from '../../core/Multichain/utils';
5153

@@ -340,13 +342,28 @@ export const selectMultichainTokenListForAccountsAnyChain =
340342
},
341343
);
342344

345+
/**
346+
* EVM tokens resolved from the EVM-scoped account within the selected account
347+
* group, regardless of the currently active network. This prevents non-EVM
348+
* active networks (e.g. TRON) from causing `selectSelectedInternalAccount` to
349+
* resolve to a non-EVM address that has no EVM balance data.
350+
*/
351+
const selectAccountTokensAcrossChainsForEvmScope = createSelector(
352+
(state: RootState) => state,
353+
selectSelectedInternalAccountByScope,
354+
(state, accountByScope) => {
355+
const evmAddress = accountByScope(EVM_SCOPE)?.address;
356+
return selectAccountTokensAcrossChainsForAddress(state, evmAddress);
357+
},
358+
);
359+
343360
/**
344361
* Unified selector: EVM tokens (native + ERC20) for the selected EVM address
345362
* plus non-EVM tokens (e.g., TRX) across all accounts in the selected account group.
346363
* Returns a map keyed by chainId (hex for EVM, CAIP-2 for non-EVM) to TokenI[].
347364
*/
348365
export const selectAccountTokensAcrossChainsUnified = createDeepEqualSelector(
349-
selectAccountTokensAcrossChains,
366+
selectAccountTokensAcrossChainsForEvmScope,
350367
selectSelectedAccountGroupInternalAccounts,
351368
(state: RootState) => state,
352369
(evmTokensByChain, selectedGroupAccounts, state) => {

0 commit comments

Comments
 (0)