From d790f2e7ed7a8a0ad4719638a6cde1486ef0ece8 Mon Sep 17 00:00:00 2001 From: memoyil <2213635+memoyil@users.noreply.github.com> Date: Tue, 28 Jan 2025 10:52:02 +0100 Subject: [PATCH] fix: Increase token name validation length and perf improvement when processing --- packages/token-lists/react/getTokenList.ts | 39 +++++++++++++------- packages/token-lists/schema/pancakeswap.json | 2 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/token-lists/react/getTokenList.ts b/packages/token-lists/react/getTokenList.ts index 62dbc6adb6add..991bc3050a260 100644 --- a/packages/token-lists/react/getTokenList.ts +++ b/packages/token-lists/react/getTokenList.ts @@ -18,33 +18,44 @@ export default async function getTokenList(listUrl: string): Promise for (let i = 0; i < urls.length; i++) { const url = urls[i] const isLast = i === urls.length - 1 - let response + let json: any try { - response = await fetch(url) + const response = await fetch(url) + if (!response.ok) { + throw new Error(`Response not ok ${listUrl}`) + } + json = await response.json() } catch (error) { console.error('Failed to fetch list', listUrl, error) if (isLast) throw new Error(`Failed to download list ${listUrl}`) continue } - - if (!response.ok) { - if (isLast) throw new Error(`Failed to download list ${listUrl}`) - continue - } - - const json = await response.json() if (!tokenListValidator(json)) { + const invalidIndices = new Set() const preFilterValidationErrors: string = tokenListValidator.errors?.reduce((memo, error) => { + const dataPath = (error as any)?.dataPath as string + const match = dataPath?.match(/\.tokens\[(\d+)\]/) + if (match) { + const index = parseInt(match[1], 10) + invalidIndices.add(index) + } const add = `${(error as any).dataPath} ${error.message ?? ''}` return memo.length > 0 ? `${memo}; ${add}` : `${add}` }, '') ?? 'unknown error' - if (json.tokens) { - remove(json.tokens, (token) => { - return !tokenListValidator({ ...json, tokens: [token] }) - }) + let isValid = false + if (Array.isArray(json.tokens)) { + const jsonWithIndicesRemoved = json.tokens.filter((_, index) => !invalidIndices.has(index)) + if (!tokenListValidator({ ...json, tokens: jsonWithIndicesRemoved })) { + remove(json.tokens, (token) => { + return !tokenListValidator({ ...json, tokens: [token] }) + }) + } else { + json.tokens = jsonWithIndicesRemoved + isValid = true + } } - if (!tokenListValidator(json)) { + if (!isValid || !tokenListValidator(json)) { const validationErrors: string = tokenListValidator.errors?.reduce((memo, error) => { const add = `${(error as any).dataPath} ${error.message ?? ''}` diff --git a/packages/token-lists/schema/pancakeswap.json b/packages/token-lists/schema/pancakeswap.json index 25df79153222a..2aace104bdbfb 100644 --- a/packages/token-lists/schema/pancakeswap.json +++ b/packages/token-lists/schema/pancakeswap.json @@ -196,7 +196,7 @@ "type": "string", "description": "The name of the token", "minLength": 1, - "maxLength": 40, + "maxLength": 50, "pattern": "^[ \\w.'+\\-%/À-ÖØ-öø-ÿ:&\\[\\]\\(\\)]+$", "examples": ["USD Coin"] },