Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DetectedTokens getting reloaded from allDetectedTokens even after the tokens are imported for a chain and selectedAddress #1015

Merged
merged 6 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 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 @@ -852,6 +851,54 @@ 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({
NiranjanaBinoy marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down
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 !== undefined &&
legobeat marked this conversation as resolved.
Show resolved Hide resolved
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 !== undefined &&
legobeat marked this conversation as resolved.
Show resolved Hide resolved
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 !== undefined &&
legobeat marked this conversation as resolved.
Show resolved Hide resolved
allDetectedTokens &&
allDetectedTokens[chainIdToAddTokens] &&
allDetectedTokens[chainIdToAddTokens][userAddressToAddTokens])
) {
const networkDetectedTokens = allDetectedTokens[chainIdToAddTokens];
const newDetectedNetworkTokens = {
...networkDetectedTokens,
Expand Down