Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Increase token name validation length and perf improvement when processing #11171

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
39 changes: 25 additions & 14 deletions packages/token-lists/react/getTokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,44 @@ export default async function getTokenList(listUrl: string): Promise<TokenList>
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<number>()
const preFilterValidationErrors: string =
tokenListValidator.errors?.reduce<string>((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<TokenInfo>(json.tokens, (token) => {
return !tokenListValidator({ ...json, tokens: [token] })
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

no need to do process all token one by one since in error message indices can be obtained

})
let isValid = false
if (Array.isArray(json.tokens)) {
const jsonWithIndicesRemoved = json.tokens.filter((_, index) => !invalidIndices.has(index))
if (!tokenListValidator({ ...json, tokens: jsonWithIndicesRemoved })) {
remove<TokenInfo>(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<string>((memo, error) => {
const add = `${(error as any).dataPath} ${error.message ?? ''}`
Expand Down
2 changes: 1 addition & 1 deletion packages/token-lists/schema/pancakeswap.json
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"type": "string",
"description": "The name of the token",
"minLength": 1,
"maxLength": 40,
"maxLength": 50,
"pattern": "^[ \\w.'+\\-%/À-ÖØ-öø-ÿ:&\\[\\]\\(\\)]+$",
"examples": ["USD Coin"]
},
Expand Down
Loading