From 1df2ecc5a52c10addb1dba90119664fd31726aeb Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Fri, 16 Dec 2022 09:37:31 -0500 Subject: [PATCH] DetectedTokens getting reloaded from allDetectedTokens even after the tokens are imported for a chain and selectedAddress (#1015) --- .../src/TokensController.test.ts | 60 ++++++++++++++++++- .../src/TokensController.ts | 24 +++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 5f885d84901..fcb8f87de89 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -763,7 +763,6 @@ describe('TokensController', () => { [CONFIGURED_ADDRESS]: [directlyAddedToken], }, }); - stub.restore(); }); }); @@ -1195,4 +1194,63 @@ describe('TokensController', () => { stub.restore(); }); }); + + describe('Clearing nested lists', function () { + const dummyTokens: Token[] = [ + { + address: '0x01', + symbol: 'barA', + decimals: 2, + aggregators: [], + image: undefined, + }, + ]; + const selectedAddress = '0x1'; + const tokenAddress = '0x01'; + + it('should clear nest allTokens under chain ID and selected address when an added token is ignored', async () => { + tokensController.configure({ + selectedAddress, + chainId: NetworksChainId.mainnet, + }); + await tokensController.addTokens(dummyTokens); + await tokensController.ignoreTokens(['0x01']); + expect( + tokensController.state.allTokens[NetworksChainId.mainnet][ + selectedAddress + ], + ).toStrictEqual([]); + }); + + it('should clear nest allIgnoredTokens under chain ID and selected address when an ignored token is re-added', async () => { + tokensController.configure({ + selectedAddress, + chainId: NetworksChainId.mainnet, + }); + await tokensController.addTokens(dummyTokens); + await tokensController.ignoreTokens([tokenAddress]); + await tokensController.addTokens(dummyTokens); + + expect( + tokensController.state.allIgnoredTokens[NetworksChainId.mainnet][ + selectedAddress + ], + ).toStrictEqual([]); + }); + + it('should clear nest allDetectedTokens under chain ID and selected address when an detected token is added to tokens list', async () => { + tokensController.configure({ + selectedAddress, + chainId: NetworksChainId.mainnet, + }); + await tokensController.addDetectedTokens(dummyTokens); + await tokensController.addTokens(dummyTokens); + + expect( + tokensController.state.allDetectedTokens[NetworksChainId.mainnet][ + selectedAddress + ], + ).toStrictEqual([]); + }); + }); }); diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index 5c49ef469fb..62d5b28f5a3 100644 --- a/packages/assets-controllers/src/TokensController.ts +++ b/packages/assets-controllers/src/TokensController.ts @@ -741,7 +741,13 @@ export class TokensController extends BaseController< const chainIdToAddTokens = detectionChainId ?? chainId; let newAllTokens = allTokens; - if (newTokens?.length) { + if ( + newTokens?.length || + (newTokens && + allTokens && + allTokens[chainIdToAddTokens] && + allTokens[chainIdToAddTokens][userAddressToAddTokens]) + ) { const networkTokens = allTokens[chainIdToAddTokens]; const newNetworkTokens = { ...networkTokens, @@ -754,7 +760,13 @@ export class TokensController extends BaseController< } let newAllIgnoredTokens = allIgnoredTokens; - if (newIgnoredTokens?.length) { + if ( + newIgnoredTokens?.length || + (newIgnoredTokens && + allIgnoredTokens && + allIgnoredTokens[chainIdToAddTokens] && + allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens]) + ) { const networkIgnoredTokens = allIgnoredTokens[chainIdToAddTokens]; const newIgnoredNetworkTokens = { ...networkIgnoredTokens, @@ -767,7 +779,13 @@ export class TokensController extends BaseController< } let newAllDetectedTokens = allDetectedTokens; - if (newDetectedTokens?.length) { + if ( + newDetectedTokens?.length || + (newDetectedTokens && + allDetectedTokens && + allDetectedTokens[chainIdToAddTokens] && + allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens]) + ) { const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens]; const newDetectedNetworkTokens = { ...networkDetectedTokens,