Skip to content

Commit c4683c5

Browse files
juanmigdrsalimtb
andauthored
fix: Autodetect NFTs does not detect NFTs on Linea and Ethereum Mainnet (#20504)
<!-- 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** Te logic of the useNftDetectionChainIds function was failing to get the chainIds and always defaulted to using Ethereum only. I have simplified the logic to make this work <!-- 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: ## **Related issues** Fixes: #19220 | https://consensyssoftware.atlassian.net/browse/ASSETS-1179 ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <img width="375" height="770" alt="image" src="https://github.com/user-attachments/assets/277dcf9a-4647-4e05-9ee5-153c6717c110" /> ### **After** <img width="362" height="751" alt="image" src="https://github.com/user-attachments/assets/d0faa9a9-8c04-415f-8285-849b27fdde39" /> ## **Pre-merge author checklist** - [ ] 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). - [ ] 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-mobile/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. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Simplifies NFT detection chain ID logic to use enabledNetworks from useCurrentNetworkInfo with fallback to current chainId, and updates tests accordingly. > > - **Hooks** > - **useNftDetectionChainIds**: Replace all-networks/popular-network selectors with `useCurrentNetworkInfo().enabledNetworks`; if any enabled, return their `chainId`s, otherwise fallback to current `selectChainId`. > - **Tests** > - Update `useNftDetectionChainIds.test.ts` to mock `useCurrentNetworkInfo` and validate both empty and populated `enabledNetworks`; remove reliance on popular-network selectors; add `beforeEach` to clear mocks. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit c42beb2. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Salim TOUBAL <[email protected]>
1 parent e5d7c95 commit c4683c5

File tree

2 files changed

+37
-46
lines changed

2 files changed

+37
-46
lines changed

app/components/hooks/useNftDetectionChainIds.test.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ import { backgroundState } from '../../util/test/initial-root-state';
66
import { RootState } from '../../reducers';
77
import { useNftDetectionChainIds } from './useNftDetectionChainIds';
88
import { useSelector } from 'react-redux';
9-
import {
10-
selectChainId,
11-
selectIsAllNetworks,
12-
selectIsPopularNetwork,
13-
selectAllPopularNetworkConfigurations,
14-
} from '../../selectors/networkController';
9+
import { selectChainId } from '../../selectors/networkController';
10+
import { useCurrentNetworkInfo } from './useCurrentNetworkInfo';
1511

1612
const mockInitialState: DeepPartial<RootState> = {
1713
settings: {},
@@ -27,45 +23,52 @@ jest.mock('react-redux', () => ({
2723
useSelector: jest.fn(),
2824
}));
2925

26+
jest.mock('./useCurrentNetworkInfo', () => ({
27+
useCurrentNetworkInfo: jest.fn(),
28+
}));
29+
3030
describe('useNftDetectionChainIds', () => {
31-
it('returns current chain id when filtered on current network', async () => {
31+
beforeEach(() => {
32+
jest.clearAllMocks();
33+
});
34+
35+
it('returns current chain id when enabledNetworks is empty', async () => {
3236
(useSelector as jest.Mock).mockImplementation((selector) => {
33-
if (selector === selectIsAllNetworks) {
34-
return false; // to show all networks
35-
}
3637
if (selector === selectChainId) {
3738
return '0x1';
3839
}
3940
return null;
4041
});
42+
43+
(useCurrentNetworkInfo as jest.Mock).mockReturnValue({
44+
enabledNetworks: [], // Empty array should fallback to current chainId
45+
});
46+
4147
const { result } = renderHookWithProvider(() => useNftDetectionChainIds(), {
4248
state: mockInitialState,
4349
});
4450
expect(result?.current).toEqual(['0x1']);
4551
});
46-
it('returns array of all popular networks when there is no filter on networks', async () => {
52+
53+
it('returns array of enabled network chain ids when enabledNetworks has networks', async () => {
4754
(useSelector as jest.Mock).mockImplementation((selector) => {
48-
if (selector === selectIsAllNetworks) {
49-
return true;
50-
}
51-
if (selector === selectIsPopularNetwork) {
52-
return true;
53-
}
54-
if (selector === selectAllPopularNetworkConfigurations) {
55-
return {
56-
'0x1': {
57-
chainId: '0x1',
58-
},
59-
'0x2': {
60-
chainId: '0x2',
61-
},
62-
};
55+
if (selector === selectChainId) {
56+
return '0x1';
6357
}
6458
return null;
6559
});
60+
61+
(useCurrentNetworkInfo as jest.Mock).mockReturnValue({
62+
enabledNetworks: [
63+
{ chainId: '0x1', enabled: true },
64+
{ chainId: '0x89', enabled: true },
65+
{ chainId: '0xa', enabled: true },
66+
],
67+
});
68+
6669
const { result } = renderHookWithProvider(() => useNftDetectionChainIds(), {
6770
state: mockInitialState,
6871
});
69-
expect(result?.current).toEqual(['0x1', '0x2']);
72+
expect(result?.current).toEqual(['0x1', '0x89', '0xa']);
7073
});
7174
});
Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,26 @@
11
import { useMemo } from 'react';
22
import { useSelector } from 'react-redux';
3-
import {
4-
selectAllPopularNetworkConfigurations,
5-
selectChainId,
6-
selectIsAllNetworks,
7-
selectIsPopularNetwork,
8-
} from '../../selectors/networkController';
3+
import { selectChainId } from '../../selectors/networkController';
94
import { Hex } from '@metamask/utils';
5+
import { useCurrentNetworkInfo } from './useCurrentNetworkInfo';
106

117
/**
128
* Hook to determine the chains that should detect NFTs
139
*
1410
* @returns an array of the chain ids allowed for NFTs search
1511
*/
1612
export const useNftDetectionChainIds = (): Hex[] => {
17-
const isAllNetworks = useSelector(selectIsAllNetworks);
18-
const isPopularNetworks = useSelector(selectIsPopularNetwork);
19-
const networkConfigurationsPopularNetworks = useSelector(
20-
selectAllPopularNetworkConfigurations,
21-
);
13+
const { enabledNetworks } = useCurrentNetworkInfo();
14+
2215
const chainId = useSelector(selectChainId);
2316

2417
return useMemo(
2518
() =>
26-
isAllNetworks && isPopularNetworks
27-
? (Object.values(networkConfigurationsPopularNetworks).map(
19+
enabledNetworks.length >= 1
20+
? (Object.values(enabledNetworks).map(
2821
(network) => network.chainId,
2922
) as Hex[])
3023
: ([chainId] as Hex[]),
31-
[
32-
isAllNetworks,
33-
isPopularNetworks,
34-
networkConfigurationsPopularNetworks,
35-
chainId,
36-
],
24+
[enabledNetworks, chainId],
3725
);
3826
};

0 commit comments

Comments
 (0)