-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #266 from berachain/fix/update-loading-states-of-t…
…oken-avatars feat(hub): update loading times of tokens for swaps and pools
- Loading branch information
Showing
12 changed files
with
329 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,14 @@ | ||
import { chainId } from "@bera/config"; | ||
import { tokenListUrl } from "@bera/config"; | ||
|
||
import { BeraConfig } from "../../types"; | ||
import { Token } from "../../types/dex"; | ||
|
||
export interface GetTokensRequest { | ||
externalList?: Token[]; | ||
config: BeraConfig; | ||
} | ||
|
||
export interface GetTokens { | ||
tokenList?: Token[] | undefined; | ||
customTokenList?: Token[] | undefined; | ||
tokenDictionary?: { [key: string]: Token } | undefined; | ||
featuredTokenList?: Token[] | undefined; | ||
} | ||
|
||
/** | ||
* fetch and format the token list | ||
*/ | ||
function tokenListToDict(list: Token[]): { [key: string]: Token } { | ||
return list.reduce((acc, item) => { | ||
// @ts-ignore | ||
acc[item.address] = item; | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
export const getTokens = async ({ | ||
externalList, | ||
config, | ||
}: GetTokensRequest): Promise<GetTokens> => { | ||
if (!config.endpoints?.tokenList) { | ||
return { | ||
tokenList: [], | ||
customTokenList: [...(externalList ?? [])], | ||
tokenDictionary: tokenListToDict(externalList ?? []), | ||
featuredTokenList: [], | ||
}; | ||
} | ||
export const getTokens = async (): Promise<Token[]> => { | ||
try { | ||
const tokenList = await fetch(config.endpoints?.tokenList); | ||
const temp = await tokenList.json(); | ||
if (!temp.tokens) | ||
return { | ||
tokenList: externalList ?? [], | ||
customTokenList: externalList ?? [], | ||
featuredTokenList: [], | ||
tokenDictionary: {}, | ||
}; | ||
const defaultList = temp.tokens | ||
.filter( | ||
(token: any) => | ||
!token.chainId || Number(token.chainId) === Number(chainId), | ||
) | ||
.map((token: any) => { | ||
return { ...token, default: true }; | ||
}); | ||
|
||
const isFeatured = (tag: string) => tag === "featured"; | ||
|
||
const defaultFeaturedList = defaultList | ||
.filter((token: any) => { | ||
return token.tags.some(isFeatured); | ||
}) | ||
.map((token: any) => { | ||
return { ...token, default: true }; | ||
}); | ||
|
||
const list = [...defaultList, ...(externalList ?? [])]; | ||
|
||
const uniqueList = list.filter( | ||
(item, index) => | ||
list.findIndex((i) => i.address === item.address) === index, | ||
); | ||
|
||
console.log({ uniqueList, temp, defaultList, defaultFeaturedList }); | ||
|
||
return { | ||
tokenList: uniqueList, | ||
customTokenList: [...(externalList ?? [])], | ||
tokenDictionary: tokenListToDict(list), | ||
featuredTokenList: defaultFeaturedList ?? [], | ||
}; | ||
const tokenList = await fetch(tokenListUrl); | ||
const tokenData = await tokenList.json(); | ||
return tokenData.tokens ?? []; | ||
} catch (error) { | ||
console.error("Error fetching token list", error); | ||
return { | ||
tokenList: [...(externalList ?? [])], | ||
customTokenList: [...(externalList ?? [])], | ||
featuredTokenList: [], | ||
tokenDictionary: tokenListToDict(externalList ?? []), | ||
}; | ||
return []; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Token } from "../types"; | ||
|
||
/** | ||
* fetch and format the token list | ||
*/ | ||
function tokenListToDict(list: Token[]): { [key: string]: Token } { | ||
return list.reduce((acc, item) => { | ||
// @ts-ignore | ||
acc[item.address] = item; | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
const formatTokenList = ( | ||
tokenList: Token[], | ||
externalList: Token[], | ||
chainId: number, | ||
) => { | ||
if (!tokenList || tokenList.length === 0) | ||
return { | ||
tokenList: externalList ?? [], | ||
customTokenList: externalList ?? [], | ||
featuredTokenList: [], | ||
tokenDictionary: {}, | ||
}; | ||
const defaultList = tokenList | ||
.filter( | ||
(token: any) => | ||
!token.chainId || Number(token.chainId) === Number(chainId), | ||
) | ||
.map((token: any) => { | ||
return { ...token, default: true }; | ||
}); | ||
|
||
const isFeatured = (tag: string) => tag === "featured"; | ||
|
||
const defaultFeaturedList = defaultList | ||
.filter((token: any) => { | ||
return token.tags.some(isFeatured); | ||
}) | ||
.map((token: any) => { | ||
return { ...token, default: true }; | ||
}); | ||
|
||
const list = [...defaultList, ...(externalList ?? [])]; | ||
|
||
const uniqueList = list.filter( | ||
(item, index) => | ||
list.findIndex((i) => i.address === item.address) === index, | ||
); | ||
|
||
return { | ||
tokenList: uniqueList, | ||
customTokenList: [...(externalList ?? [])], | ||
tokenDictionary: tokenListToDict(list), | ||
featuredTokenList: defaultFeaturedList ?? [], | ||
}; | ||
}; | ||
|
||
export { formatTokenList, tokenListToDict }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.