diff --git a/.changeset/funny-mirrors-rescue.md b/.changeset/funny-mirrors-rescue.md new file mode 100644 index 0000000000000..c528019631576 --- /dev/null +++ b/.changeset/funny-mirrors-rescue.md @@ -0,0 +1,5 @@ +--- +'@pancakeswap/token-lists': patch +--- + +fix: Filter out tokens in list that are not matching the criteria diff --git a/packages/token-lists/react/getTokenList.ts b/packages/token-lists/react/getTokenList.ts index 692cc9c22915d..62dbc6adb6add 100644 --- a/packages/token-lists/react/getTokenList.ts +++ b/packages/token-lists/react/getTokenList.ts @@ -33,18 +33,27 @@ export default async function getTokenList(listUrl: string): Promise } const json = await response.json() - if (json.tokens) { - remove(json.tokens, (token) => { - return token.symbol ? token.symbol.length === 0 : true - }) - } if (!tokenListValidator(json)) { - const validationErrors: string = + const preFilterValidationErrors: string = tokenListValidator.errors?.reduce((memo, error) => { const add = `${(error as any).dataPath} ${error.message ?? ''}` return memo.length > 0 ? `${memo}; ${add}` : `${add}` }, '') ?? 'unknown error' - throw new Error(`Token list failed validation: ${validationErrors}`) + if (json.tokens) { + remove(json.tokens, (token) => { + return !tokenListValidator({ ...json, tokens: [token] }) + }) + } + if (!tokenListValidator(json)) { + const validationErrors: string = + tokenListValidator.errors?.reduce((memo, error) => { + const add = `${(error as any).dataPath} ${error.message ?? ''}` + return memo.length > 0 ? `${memo}; ${add}` : `${add}` + }, '') ?? 'unknown error' + throw new Error(`Token list ${url} failed validation: ${validationErrors}`) + } else { + console.warn(`Token list ${url} validation failed before token filtering: ${preFilterValidationErrors}`) + } } return json as TokenList }