Skip to content

Commit

Permalink
fix: Filter out tokens in list that are not matching the criteria (#1…
Browse files Browse the repository at this point in the history
…0684)

<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->

<!-- start pr-codex -->

---

## PR-Codex overview
This PR focuses on fixing the token filtering process in the
`getTokenList.ts` file to ensure only tokens meeting criteria are
included.

### Detailed summary
- Added filtering out tokens that do not match criteria
- Improved error handling for token list validation
- Added warning message for failed validation before filtering

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Sep 18, 2024
1 parent 01cb6f1 commit f3e8a77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-mirrors-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pancakeswap/token-lists': patch
---

fix: Filter out tokens in list that are not matching the criteria
23 changes: 16 additions & 7 deletions packages/token-lists/react/getTokenList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,27 @@ export default async function getTokenList(listUrl: string): Promise<TokenList>
}

const json = await response.json()
if (json.tokens) {
remove<TokenInfo>(json.tokens, (token) => {
return token.symbol ? token.symbol.length === 0 : true
})
}
if (!tokenListValidator(json)) {
const validationErrors: string =
const preFilterValidationErrors: string =
tokenListValidator.errors?.reduce<string>((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<TokenInfo>(json.tokens, (token) => {
return !tokenListValidator({ ...json, tokens: [token] })
})
}
if (!tokenListValidator(json)) {
const validationErrors: string =
tokenListValidator.errors?.reduce<string>((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
}
Expand Down

0 comments on commit f3e8a77

Please sign in to comment.