Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
65 changes: 65 additions & 0 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -844,6 +844,71 @@ describe('TokensController', () => {
},
);
});

it('should not retain ignored tokens from a different network', async () => {
const selectedAddress = '0x0001';
const selectedAccount = createMockInternalAccount({
address: selectedAddress,
});

await withController(
{
mocks: {
getSelectedAccount: selectedAccount,
getAccount: selectedAccount,
},
},
async ({ controller, triggerSelectedAccountChange, changeNetwork }) => {
// Select the first account
triggerSelectedAccountChange(selectedAccount);

// Add and ignore a token on Sepolia
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia });
await controller.addToken({
address: '0x01',
symbol: 'Token1',
decimals: 18,
});
expect(controller.state.tokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toHaveLength(0);

// Switch to Goerli network
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.goerli });
expect(controller.state.ignoredTokens).toHaveLength(0);

// Ignore the token on Sepolia
controller.ignoreTokens(['0x01'], InfuraNetworkType.sepolia);
expect(controller.state.tokens).toHaveLength(0);
expect(controller.state.ignoredTokens).toStrictEqual(['0x01']);

// Attempt to ignore a token that was added on Goerli
await controller.addToken({
address: '0x02',
symbol: 'Token2',
decimals: 8,
});
controller.ignoreTokens(['0x02'], InfuraNetworkType.goerli);
expect(controller.state.tokens).toHaveLength(0);
expect(controller.state.ignoredTokens).toStrictEqual(['0x02']);

// Verify that the ignored tokens from Sepolia are not retained
expect(controller.state.ignoredTokens).toHaveLength(1);
expect(controller.state.ignoredTokens).toStrictEqual(['0x02']);
expect(controller.state.allIgnoredTokens).toStrictEqual({
[ChainId.sepolia]: {
[selectedAddress]: ['0x01'],
},
[ChainId.goerli]: {
[selectedAddress]: ['0x02'],
},
});

// Switch back to Sepolia and check ignored tokens
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.sepolia });
expect(controller.state.ignoredTokens).toStrictEqual(['0x01']);
},
);
});
});

it('should ignore multiple tokens with single ignoreTokens call', async () => {
Expand Down
22 changes: 18 additions & 4 deletions packages/assets-controllers/src/TokensController.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -576,10 +576,6 @@ export class TokensController extends BaseController<
tokenAddressesToIgnore: string[],
networkClientId?: NetworkClientId,
) {
const { ignoredTokens, detectedTokens, tokens } = this.state;
const ignoredTokensMap: { [key: string]: true } = {};
let newIgnoredTokens: string[] = [...ignoredTokens];

let interactingChainId;
if (networkClientId) {
interactingChainId = this.messagingSystem.call(
Expand All@@ -588,6 +584,24 @@ export class TokensController extends BaseController<
).configuration.chainId;
}

const { allTokens, allDetectedTokens, allIgnoredTokens } = this.state;
const ignoredTokensMap: { [key: string]: true } = {};
const ignoredTokens =
allIgnoredTokens[interactingChainId ?? this.#chainId]?.[
this.#getSelectedAddress()
] || [];
let newIgnoredTokens: string[] = [...ignoredTokens];

const tokens =
allTokens[interactingChainId ?? this.#chainId]?.[
this.#getSelectedAddress()
] || [];

const detectedTokens =
allDetectedTokens[interactingChainId ?? this.#chainId]?.[
this.#getSelectedAddress()
] || [];

const checksummedTokenAddresses = tokenAddressesToIgnore.map((address) => {
const checksumAddress = toChecksumHexAddress(address);
ignoredTokensMap[address.toLowerCase()] = true;
Expand Down