Skip to content

Commit

Permalink
fix: prevent non-current network tokens from being hidden incorrectly (
Browse files Browse the repository at this point in the history
…#28674)

<!--
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**

fix to prevent non current network tokens from being hidden incorrecly

core PR: MetaMask/core#4967

<!--
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/28674?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. add different network accros different chains
2. choose ethereum as current chain
3. try to hide token of polygon for instance

## **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/2637ed94-6ad1-4025-8000-963906aca187



### **After**

<!-- [screenshots/recordings] -->



https://github.com/user-attachments/assets/4c5d6cbd-4af2-43c5-bcbd-879a71b9997e



## **Pre-merge author checklist**

- [x] 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).
- [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-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
salimtb authored Nov 25, 2024
1 parent eeb085f commit 9c4d1b4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ index a13403446a2376d4d905a9ef733941798da89c88..3c8229f9ea40f4c1ee760a22884e1066
/**
* The list of currencies that can be supplied as the `vsCurrency` parameter to
* the `/spot-prices` endpoint, in lowercase form.
diff --git a/dist/TokensController.cjs b/dist/TokensController.cjs
index 343b343b8300136756d96acac77aab8140efc95a..69d8e2ea84d6303a3af02bd95458ef3060c76f2b 100644
--- a/dist/TokensController.cjs
+++ b/dist/TokensController.cjs
@@ -270,13 +270,16 @@ class TokensController extends base_controller_1.BaseController {
* @param networkClientId - Optional network client ID used to determine interacting chain ID.
*/
ignoreTokens(tokenAddressesToIgnore, networkClientId) {
- const { ignoredTokens, detectedTokens, tokens } = this.state;
- const ignoredTokensMap = {};
- let newIgnoredTokens = [...ignoredTokens];
let interactingChainId;
if (networkClientId) {
interactingChainId = this.messagingSystem.call('NetworkController:getNetworkClientById', networkClientId).configuration.chainId;
}
+ const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
+ const ignoredTokensMap = {};
+ const ignoredTokens = allIgnoredTokens[interactingChainId ?? __classPrivateFieldGet(this, _TokensController_chainId, "f")]?.[__classPrivateFieldGet(this, _TokensController_instances, "m", _TokensController_getSelectedAddress).call(this)] || [];
+ let newIgnoredTokens = [...ignoredTokens];
+ const tokens = allTokens[interactingChainId ?? __classPrivateFieldGet(this, _TokensController_chainId, "f")]?.[__classPrivateFieldGet(this, _TokensController_instances, "m", _TokensController_getSelectedAddress).call(this)] || [];
+ const detectedTokens = allDetectedTokens[interactingChainId ?? __classPrivateFieldGet(this, _TokensController_chainId, "f")]?.[__classPrivateFieldGet(this, _TokensController_instances, "m", _TokensController_getSelectedAddress).call(this)] || [];
const checksummedTokenAddresses = tokenAddressesToIgnore.map((address) => {
const checksumAddress = (0, controller_utils_1.toChecksumHexAddress)(address);
ignoredTokensMap[address.toLowerCase()] = true;
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import thunk from 'redux-thunk';
import * as actions from '../../../../store/actions';
import { renderWithProvider } from '../../../../../test/lib/render-helpers';
import mockState from '../../../../../test/data/mock-state.json';
import { mockNetworkState } from '../../../../../test/stub/networks';
import HideTokenConfirmationModal from '.';

const mockHistoryPush = jest.fn();
Expand All @@ -25,6 +26,13 @@ describe('Hide Token Confirmation Modal', () => {
image: '',
};

const tokenState2 = {
address: '0xTokenAddress2',
symbol: 'TKN2',
image: '',
chainId: '0x89',
};

const tokenModalState = {
...mockState,
appState: {
Expand Down Expand Up @@ -82,4 +90,47 @@ describe('Hide Token Confirmation Modal', () => {
networkClientId: 'goerli',
});
});

it('should hide token from another chain', () => {
const tokenModalStateWithDifferentChain = {
...mockState,
metamask: {
...mockState.metamask,
selectedNetworkClientId: 'bsc',
...mockNetworkState({ chainId: '0x89', id: 'bsc' }),
},
appState: {
...mockState.appState,
modal: {
modalState: {
props: {
history: {
push: mockHistoryPush,
},
token: tokenState2,
},
},
},
},
};

const mockStoreDifferentChain = configureMockStore([thunk])(
tokenModalStateWithDifferentChain,
);

const { queryByTestId } = renderWithProvider(
<HideTokenConfirmationModal />,
mockStoreDifferentChain,
);

const hideButton = queryByTestId('hide-token-confirmation__hide');

fireEvent.click(hideButton);

expect(mockHideModal).toHaveBeenCalled();
expect(actions.ignoreTokens).toHaveBeenCalledWith({
tokensToIgnore: tokenState2.address,
networkClientId: 'bsc',
});
});
});
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4975,7 +4975,7 @@ __metadata:

"@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A45.1.0#~/.yarn/patches/@metamask-assets-controllers-npm-45.1.0-d914c453f0.patch":
version: 45.1.0
resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A45.1.0#~/.yarn/patches/@metamask-assets-controllers-npm-45.1.0-d914c453f0.patch::version=45.1.0&hash=86167d"
resolution: "@metamask/assets-controllers@patch:@metamask/assets-controllers@npm%3A45.1.0#~/.yarn/patches/@metamask-assets-controllers-npm-45.1.0-d914c453f0.patch::version=45.1.0&hash=cfcadc"
dependencies:
"@ethereumjs/util": "npm:^8.1.0"
"@ethersproject/abi": "npm:^5.7.0"
Expand Down Expand Up @@ -5008,7 +5008,7 @@ __metadata:
"@metamask/keyring-controller": ^19.0.0
"@metamask/network-controller": ^22.0.0
"@metamask/preferences-controller": ^15.0.0
checksum: 10/985ec7dffb75aaff8eea00f556157e42cd5db063cbfa94dfd4f070c5b9d98b1315f3680fa7370f4c734a1688598bbda9c44a7c33c342e1d123d6ee2edd6120fc
checksum: 10/d2f7d5bb07feceb5b972beda019f411cd073ece3ed682b21373fc6d4c06812ec10245b40c78ce6316c5fb1718278fd269b73e13d37c2ff07b5bb3ecdfd8278f7
languageName: node
linkType: hard

Expand Down

0 comments on commit 9c4d1b4

Please sign in to comment.