Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 number Diff line number Diff line change
Expand Up @@ -1878,6 +1878,71 @@ describe('TokensController', () => {
},
);
});

it('should not add duplicate tokens to state', async () => {
Copy link
Contributor Author

@sahar-fehri sahar-fehri Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails on main and state.allTokens ends up with duplicate entries of the same token

await withController(
{
mockNetworkClientConfigurationsByNetworkClientId: {
networkClientId1: buildCustomNetworkClientConfiguration({
chainId: '0x5',
}),
},
},
async ({ controller, changeNetwork }) => {
changeNetwork({ selectedNetworkClientId: InfuraNetworkType.goerli });

const tokensToImport = [
{
address: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', // lowercase USDC
symbol: 'USDC',
decimals: 6,
name: 'USDC',
image: 'test',
aggregators: [
'Metamask',
'1inch',
'LiFi',
'XSwap',
'Socket',
'Rubic',
'Squid',
'Rango',
'Sonarwatch',
'SushiSwap',
'PMM',
'Bancor',
],
},
];
// Add tokens for first time in state
await controller.addTokens(tokensToImport, 'mainnet');
expect(
controller.state.allTokens[ChainId.mainnet][
defaultMockInternalAccount.address
],
).toStrictEqual([
{
...tokensToImport[0],
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // checksummed USDC
},
]);

// Add same tokens again and check the state does not have duplicate entries
await controller.addTokens(tokensToImport, 'mainnet');

expect(
controller.state.allTokens[ChainId.mainnet][
defaultMockInternalAccount.address
],
).toStrictEqual([
{
...tokensToImport[0],
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // checksummed USDC
},
]);
},
);
});
});

describe('watchAsset', () => {
Expand Down
11 changes: 9 additions & 2 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,11 +510,18 @@ export class TokensController extends BaseController<
networkClientId,
).configuration.chainId;

const tokensToImportChecksummed = tokensToImport.map((token) => {
return {
...token,
address: toChecksumHexAddress(token.address),
};
});

// Used later to dedupe imported tokens
const newTokensMap = [
...(allTokens[interactingChainId]?.[this.#getSelectedAccount().address] ||
[]),
...tokensToImport,
...tokensToImportChecksummed,
].reduce(
(output, token) => {
output[token.address] = token;
Expand All @@ -523,7 +530,7 @@ export class TokensController extends BaseController<
{} as { [address: string]: Token },
);
try {
tokensToImport.forEach((tokenToAdd) => {
tokensToImportChecksummed.forEach((tokenToAdd) => {
const { address, symbol, decimals, image, aggregators, name } =
tokenToAdd;
const checksumAddress = toChecksumHexAddress(address);
Expand Down
Loading