From 68cb6698a2159095f0ca2aea573b5f31b840ffbd Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Mon, 12 Dec 2022 13:29:58 -0500 Subject: [PATCH 1/6] updating detectedtoken list length to zero to a undefined check in _getNewAllTokensState while updating newAllDetectedTokens --- packages/assets-controllers/src/TokensController.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index 5c49ef469f..969d5d8215 100644 --- a/packages/assets-controllers/src/TokensController.ts +++ b/packages/assets-controllers/src/TokensController.ts @@ -767,7 +767,7 @@ export class TokensController extends BaseController< } let newAllDetectedTokens = allDetectedTokens; - if (newDetectedTokens?.length) { + if (newDetectedTokens !== undefined) { const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens]; const newDetectedNetworkTokens = { ...networkDetectedTokens, From 3e22bbb7b0085f9ede6e5d7f293ce0f5bae7419c Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Tue, 13 Dec 2022 17:03:39 -0500 Subject: [PATCH 2/6] updating test --- .../src/TokensController.test.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 5f885d8490..8522dfc18a 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -756,6 +756,7 @@ describe('TokensController', () => { [DETECTED_CHAINID]: { [DETECTED_ADDRESS]: [detectedToken], }, + '4': { '0xabc': [] }, }); expect(tokensController.state.allTokens).toStrictEqual({ @@ -763,7 +764,6 @@ describe('TokensController', () => { [CONFIGURED_ADDRESS]: [directlyAddedToken], }, }); - stub.restore(); }); }); @@ -852,6 +852,21 @@ describe('TokensController', () => { ).toStrictEqual(dummyTokens); }); + it('should nest detectedTokens under chain ID and selected address when detectedTokens provided is an empty list', () => { + tokensController.configure({ + selectedAddress: dummySelectedAddress, + chainId: NetworksChainId.mainnet, + }); + const processedTokens = tokensController._getNewAllTokensState({ + newDetectedTokens: [], + }); + expect( + processedTokens.newAllDetectedTokens[NetworksChainId.mainnet][ + dummySelectedAddress + ], + ).toStrictEqual([]); + }); + it('should nest ignoredTokens under chain ID and selected address when provided with ignoredTokens as input', () => { tokensController.configure({ selectedAddress: dummySelectedAddress, From f24865ef9a8ed46acb907b2e828267cdbbf67b1d Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Thu, 15 Dec 2022 15:28:07 -0500 Subject: [PATCH 3/6] updating the changes to alltokens and allignoredtokens list and making sure empty lists are not added just because used switched to an account or network --- .../src/TokensController.ts | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index 969d5d8215..19272398b6 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 !== undefined && + 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 !== undefined && + 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 !== undefined) { + if ( + newDetectedTokens?.length || + (newDetectedTokens !== undefined && + allDetectedTokens && + allDetectedTokens[chainIdToAddTokens] && + allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens]) + ) { const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens]; const newDetectedNetworkTokens = { ...networkDetectedTokens, From 7acc9b00a7a42d0d53c0f786a9c49ff536659e8c Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:37:54 -0500 Subject: [PATCH 4/6] updating test --- .../src/TokensController.test.ts | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 8522dfc18a..9e95bb7a6a 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -756,7 +756,6 @@ describe('TokensController', () => { [DETECTED_CHAINID]: { [DETECTED_ADDRESS]: [detectedToken], }, - '4': { '0xabc': [] }, }); expect(tokensController.state.allTokens).toStrictEqual({ @@ -852,11 +851,44 @@ describe('TokensController', () => { ).toStrictEqual(dummyTokens); }); - it('should nest detectedTokens under chain ID and selected address when detectedTokens provided is an empty list', () => { + it('should nest allTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { tokensController.configure({ selectedAddress: dummySelectedAddress, chainId: NetworksChainId.mainnet, }); + await tokensController.addTokens(dummyTokens); + const processedTokens = tokensController._getNewAllTokensState({ + newTokens: [], + }); + expect( + processedTokens.newAllTokens[NetworksChainId.mainnet][ + dummySelectedAddress + ], + ).toStrictEqual([]); + }); + + it('should nest allIgnoredTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { + tokensController.configure({ + selectedAddress: dummySelectedAddress, + chainId: NetworksChainId.mainnet, + }); + await tokensController.ignoreTokens(['0xabc']); + const processedTokens = tokensController._getNewAllTokensState({ + newIgnoredTokens: [], + }); + expect( + processedTokens.newAllIgnoredTokens[NetworksChainId.mainnet][ + dummySelectedAddress + ], + ).toStrictEqual([]); + }); + + it('should nest allDetectedTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { + tokensController.configure({ + selectedAddress: dummySelectedAddress, + chainId: NetworksChainId.mainnet, + }); + await tokensController.addDetectedTokens(dummyTokens); const processedTokens = tokensController._getNewAllTokensState({ newDetectedTokens: [], }); From 24d5625542d938775fa7f272a7588b0932139e24 Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Thu, 15 Dec 2022 18:15:53 -0500 Subject: [PATCH 5/6] updating test --- .../src/TokensController.test.ts | 107 ++++++++++-------- 1 file changed, 59 insertions(+), 48 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.test.ts b/packages/assets-controllers/src/TokensController.test.ts index 9e95bb7a6a..fcb8f87de8 100644 --- a/packages/assets-controllers/src/TokensController.test.ts +++ b/packages/assets-controllers/src/TokensController.test.ts @@ -851,54 +851,6 @@ describe('TokensController', () => { ).toStrictEqual(dummyTokens); }); - it('should nest allTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { - tokensController.configure({ - selectedAddress: dummySelectedAddress, - chainId: NetworksChainId.mainnet, - }); - await tokensController.addTokens(dummyTokens); - const processedTokens = tokensController._getNewAllTokensState({ - newTokens: [], - }); - expect( - processedTokens.newAllTokens[NetworksChainId.mainnet][ - dummySelectedAddress - ], - ).toStrictEqual([]); - }); - - it('should nest allIgnoredTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { - tokensController.configure({ - selectedAddress: dummySelectedAddress, - chainId: NetworksChainId.mainnet, - }); - await tokensController.ignoreTokens(['0xabc']); - const processedTokens = tokensController._getNewAllTokensState({ - newIgnoredTokens: [], - }); - expect( - processedTokens.newAllIgnoredTokens[NetworksChainId.mainnet][ - dummySelectedAddress - ], - ).toStrictEqual([]); - }); - - it('should nest allDetectedTokens under chain ID and selected address when detectedTokens provided is an empty list', async () => { - tokensController.configure({ - selectedAddress: dummySelectedAddress, - chainId: NetworksChainId.mainnet, - }); - await tokensController.addDetectedTokens(dummyTokens); - const processedTokens = tokensController._getNewAllTokensState({ - newDetectedTokens: [], - }); - expect( - processedTokens.newAllDetectedTokens[NetworksChainId.mainnet][ - dummySelectedAddress - ], - ).toStrictEqual([]); - }); - it('should nest ignoredTokens under chain ID and selected address when provided with ignoredTokens as input', () => { tokensController.configure({ selectedAddress: dummySelectedAddress, @@ -1242,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([]); + }); + }); }); From b602a133c2af49985742347608d73b508af97a3b Mon Sep 17 00:00:00 2001 From: Niranjana Binoy <43930900+NiranjanaBinoy@users.noreply.github.com> Date: Thu, 15 Dec 2022 19:17:05 -0500 Subject: [PATCH 6/6] updating the undefined check --- packages/assets-controllers/src/TokensController.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/assets-controllers/src/TokensController.ts b/packages/assets-controllers/src/TokensController.ts index 19272398b6..62d5b28f5a 100644 --- a/packages/assets-controllers/src/TokensController.ts +++ b/packages/assets-controllers/src/TokensController.ts @@ -743,7 +743,7 @@ export class TokensController extends BaseController< let newAllTokens = allTokens; if ( newTokens?.length || - (newTokens !== undefined && + (newTokens && allTokens && allTokens[chainIdToAddTokens] && allTokens[chainIdToAddTokens][userAddressToAddTokens]) @@ -762,7 +762,7 @@ export class TokensController extends BaseController< let newAllIgnoredTokens = allIgnoredTokens; if ( newIgnoredTokens?.length || - (newIgnoredTokens !== undefined && + (newIgnoredTokens && allIgnoredTokens && allIgnoredTokens[chainIdToAddTokens] && allIgnoredTokens[chainIdToAddTokens][userAddressToAddTokens]) @@ -781,7 +781,7 @@ export class TokensController extends BaseController< let newAllDetectedTokens = allDetectedTokens; if ( newDetectedTokens?.length || - (newDetectedTokens !== undefined && + (newDetectedTokens && allDetectedTokens && allDetectedTokens[chainIdToAddTokens] && allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens])