Skip to content

Commit

Permalink
DetectedTokens getting reloaded from allDetectedTokens even after the…
Browse files Browse the repository at this point in the history
… tokens are imported for a chain and selectedAddress (#1015)
  • Loading branch information
NiranjanaBinoy authored and MajorLift committed Oct 11, 2023
1 parent 15b0a77 commit 1df2ecc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
60 changes: 59 additions & 1 deletion packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,6 @@ describe('TokensController', () => {
[CONFIGURED_ADDRESS]: [directlyAddedToken],
},
});

stub.restore();
});
});
Expand Down Expand Up @@ -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([]);
});
});
});
24 changes: 21 additions & 3 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 1df2ecc

Please sign in to comment.