Skip to content

Commit e071438

Browse files
authored
fix(generation): fix filtering being dismissed (#189)
* fix(generation): fix filtering being dismissed * Remove early return * Restore early return, remove logs on invalid list
1 parent ce9fc12 commit e071438

4 files changed

Lines changed: 76 additions & 28 deletions

File tree

__test__/unit/removeInvalidTokens.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@ import { removeInvalidTokensFromList } from '../../src/lib/utils';
22
import { ArbTokenList } from '../../src/lib/types';
33

44
describe('removeInvalidTokensFromList - performance bug', () => {
5+
it('should filter out tokens with non-EVM addresses such as Solana tokens', () => {
6+
const listWithSolanaToken: ArbTokenList = {
7+
name: 'Test List',
8+
timestamp: '2025-01-01T00:00:00.000Z',
9+
version: { major: 1, minor: 0, patch: 0 },
10+
tokens: [
11+
{
12+
chainId: 42161,
13+
address: '0x1111111111111111111111111111111111111111',
14+
name: 'Valid Token',
15+
symbol: 'VALID',
16+
decimals: 18,
17+
},
18+
{
19+
chainId: 501000101,
20+
address: '5mbK36SZ7J19An8jFochhQS4of8g6BwUjbeCSxBSoWdp',
21+
name: 'michi',
22+
symbol: '$MICHI',
23+
decimals: 6,
24+
},
25+
],
26+
};
27+
28+
const result = removeInvalidTokensFromList(listWithSolanaToken);
29+
30+
expect(result.tokens).toHaveLength(1);
31+
expect(result.tokens[0].symbol).toBe('VALID');
32+
expect(result.tokens).not.toContainEqual({
33+
chainId: 501000101,
34+
address: '5mbK36SZ7J19An8jFochhQS4of8g6BwUjbeCSxBSoWdp',
35+
name: 'michi',
36+
symbol: '$MICHI',
37+
decimals: 6,
38+
});
39+
});
40+
541
it('should remove token with name > 40 characters without hanging', () => {
642
const listWithLongName: ArbTokenList = {
743
name: 'Test List',

src/lib/token_list_gen.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,9 @@ export const arbifyL1List = async (
376376
newList: ArbTokenList;
377377
l1ListName: string;
378378
}> => {
379-
const l1TokenList = await getTokenListObj(pathOrUrl);
380-
381-
removeInvalidTokensFromList(l1TokenList);
379+
const l1TokenList = removeInvalidTokensFromList(
380+
await getTokenListObj(pathOrUrl),
381+
) as TokenList;
382382
const prevArbTokenList = ignorePreviousList
383383
? null
384384
: await getPrevList(prevArbifiedList);
@@ -429,8 +429,9 @@ export const updateArbifiedList = async (
429429
prevArbifiedList: string | undefined;
430430
},
431431
) => {
432-
const arbTokenList = await getTokenListObj(pathOrUrl);
433-
removeInvalidTokensFromList(arbTokenList);
432+
const arbTokenList = removeInvalidTokensFromList(
433+
await getTokenListObj(pathOrUrl),
434+
) as ArbTokenList;
434435
const prevArbTokenList = ignorePreviousList
435436
? null
436437
: await getPrevList(prevArbifiedList);

src/lib/utils.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { L2GatewayRouter__factory } from '@arbitrum/sdk/dist/lib/abi/factories/L
88

99
import { ArbTokenList, GraphTokenResult } from './types';
1010
import path from 'path';
11-
import { tokenListIsValid } from './validateTokenList';
11+
import { tokenInfoIsValid, tokenListIsValid } from './validateTokenList';
1212
import {
1313
l2ToL1GatewayAddresses,
1414
l2ToL1GatewayAddressesNova,
@@ -246,31 +246,31 @@ export const getTokenListObjFromLocalPath = async (path: string) => {
246246
export const removeInvalidTokensFromList = (
247247
tokenList: ArbTokenList | TokenList,
248248
): ArbTokenList | TokenList => {
249-
// Quick check: if valid, return immediately
250-
if (tokenListIsValid(tokenList)) {
249+
if (tokenListIsValid(tokenList, { logErrors: false })) {
251250
return tokenList;
252251
}
253252

254-
console.log('Invalid token list detected, filtering invalid tokens...');
253+
let removedCount = 0;
254+
let loggedInvalidList = false;
255255

256-
// Validate each token individually
257256
const validTokens = tokenList.tokens.filter((token) => {
258-
const singleTokenList = {
259-
...tokenList,
260-
tokens: [token],
261-
};
262-
263-
const isValid = tokenListIsValid(singleTokenList);
257+
const isValid = tokenInfoIsValid(token);
264258

265259
if (!isValid) {
260+
if (!loggedInvalidList) {
261+
console.log('Invalid token list detected, filtering invalid tokens...');
262+
loggedInvalidList = true;
263+
}
264+
removedCount++;
266265
console.log('Removing invalid token:', token.name || token.address);
267266
}
268267

269268
return isValid;
270269
});
271270

272-
const removedCount = tokenList.tokens.length - validTokens.length;
273-
console.log(`Removed ${removedCount} invalid token(s)`);
271+
if (removedCount > 0) {
272+
console.log(`Removed ${removedCount} invalid token(s)`);
273+
}
274274

275275
// Create cleaned list
276276
const cleanedList = {

src/lib/validateTokenList.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,36 @@
11
import Ajv from 'ajv';
22
import betterAjvErrors from 'better-ajv-errors';
33
import addFormats from 'ajv-formats';
4-
import { schema, TokenList } from '@uniswap/token-lists';
4+
import { schema, TokenInfo, TokenList } from '@uniswap/token-lists';
55
import { ArbTokenList } from './types';
66

7-
export const tokenListIsValid = (tokenList: ArbTokenList | TokenList) => {
8-
const ajv = new Ajv();
9-
addFormats(ajv);
10-
schema.properties.tokens.minItems = 0;
11-
schema.properties.tokens.maxItems = 15_000;
12-
const validate = ajv.compile(schema);
7+
const ajv = new Ajv({ allErrors: true });
8+
addFormats(ajv);
9+
schema.properties.tokens.minItems = 0;
10+
schema.properties.tokens.maxItems = 15_000;
11+
const validate = ajv.compile(schema);
12+
const validateTokenInfo = ajv.compile({
13+
...schema.definitions.TokenInfo,
14+
definitions: schema.definitions,
15+
});
1316

14-
const res = validate(tokenList);
15-
if (validate.errors) {
17+
export const tokenInfoIsValid = (tokenInfo: TokenInfo) =>
18+
validateTokenInfo(tokenInfo) as boolean;
19+
20+
export const tokenListIsValid = (
21+
tokenList: ArbTokenList | TokenList,
22+
{ logErrors = true }: { logErrors?: boolean } = {},
23+
) => {
24+
const isValid = validate(tokenList) as boolean;
25+
26+
if (logErrors && validate.errors) {
1627
const output = betterAjvErrors(schema, tokenList, validate.errors, {
1728
indent: 2,
1829
});
1930
console.log(output);
2031
}
2132

22-
return res;
33+
return isValid;
2334
};
2435

2536
export const validateTokenListWithErrorThrowing = (

0 commit comments

Comments
 (0)