Skip to content

Commit

Permalink
feat: sort token list
Browse files Browse the repository at this point in the history
  • Loading branch information
quangdz1704 committed Sep 25, 2024
1 parent 54c26d3 commit f9f6442
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/hooks/useCoingecko.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { CoinGeckoId } from '@oraichain/oraidex-common';
*/
export const buildCoinGeckoPricesURL = (tokens: readonly string[]): string =>
// `https://api.coingecko.com/api/v3/simple/price?ids=${tokens.join('%2C')}&vs_currencies=usd`;
`https://price.market.orai.io/simple/price?ids=${tokens.join('%2C')}&vs_currencies=usd`;
`https://price.market.orai.io/simple/price?ids=${tokens.join('%2C')}&vs_currencies=usd&include_24hr_vol=true`;

/**
* Prices of each token.
Expand All @@ -32,15 +32,17 @@ export const useCoinGeckoPrices = <T extends CoinGeckoId>(

// use cached first then update by query, if is limited then return cached version
const [cachePrices, setCachePrices] = useConfigReducer('coingecko');
const [tokenRank, setTokenRank] = useConfigReducer('tokenRank');

return useQuery({
initialData: cachePrices,
...options,
// make unique
queryKey: ['coinGeckoPrices', ...tokens],
queryFn: async ({ signal }) => {
const prices = await getCoingeckoPrices(tokens, cachePrices, signal);
const { prices, ranks } = await getCoingeckoPrices(tokens, cachePrices, tokenRank, signal);
setCachePrices(prices);
setTokenRank(ranks);

return Object.fromEntries(tokens.map((token) => [token, prices[token]])) as CoinGeckoPrices<T>;
}
Expand All @@ -50,27 +52,31 @@ export const useCoinGeckoPrices = <T extends CoinGeckoId>(
export const getCoingeckoPrices = async <T extends CoinGeckoId>(
tokens: string[],
cachePrices?: CoinGeckoPrices<string>,
cacheRank?: CoinGeckoPrices<string>,
signal?: AbortSignal
): Promise<CoinGeckoPrices<string>> => {
): Promise<{ prices: CoinGeckoPrices<string>; ranks: CoinGeckoPrices<string> }> => {
const coingeckoPricesURL = buildCoinGeckoPricesURL(tokens);

const prices = { ...cachePrices };
const ranks = { ...cacheRank };

// by default not return data then use cached version
try {
const resp = await fetch(coingeckoPricesURL, { signal });
const rawData = (await resp.json()) as {
[C in T]?: {
usd: number;
usd_24h_vol: number;
};
};
// update cached
for (const key in rawData) {
prices[key] = rawData[key].usd;
ranks[key] = rawData[key].usd_24h_vol;
}
} catch {
// remain old cache
console.log('error getting coingecko prices: ', prices);
}
return prices;
return { prices, ranks };
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { toSumDisplay } from 'libs/utils';
import { formatDisplayUsdt } from 'pages/Pools/helpers';
import React, { useEffect, useState } from 'react';
import { getSubAmountDetails } from 'rest/api';
import useConfigReducer from 'hooks/useConfigReducer';

const cx = cn.bind(styles);
interface InputSwapProps {
Expand Down Expand Up @@ -65,6 +66,8 @@ export default function SelectToken({
const [textChain, setTextChain] = useState('');
const [textSearch, setTextSearch] = useState('');
const isLightTheme = theme === 'light';
const [tokenRank = {}] = useConfigReducer('tokenRank');

useEffect(() => {
if (selectChain && selectChain !== textChain) setTextChain(selectChain);
}, [selectChain]);
Expand Down Expand Up @@ -146,7 +149,12 @@ export default function SelectToken({
};
})
.sort((a, b) => {
return Number(b.usd) - Number(a.usd);
const balanceDelta = Number(b.usd) - Number(a.usd);

if (!balanceDelta) {
return tokenRank[b.coinGeckoId] - tokenRank[a.coinGeckoId];
}
return balanceDelta;
})
.map(({ key, tokenIcon, networkIcon, balance, usd, ...token }) => {
return (
Expand Down
2 changes: 2 additions & 0 deletions src/reducer/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ConfigState {
hideOraichainSmallAmount: boolean;
theme: Themes;
coingecko: CoinGeckoPrices<string>;
tokenRank: Record<string, number>;
apr: {
[key: string]: number;
};
Expand Down Expand Up @@ -83,6 +84,7 @@ const initialState: ConfigState = {
hideOraichainSmallAmount: false,
theme: 'dark',
coingecko: {},
tokenRank: {},
apr: {},
rewardPools: [],
liquidityPools: {},
Expand Down
2 changes: 1 addition & 1 deletion src/store/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// change version when update, add state redux-persist storage
export const PERSIST_VER = 2.4;
export const PERSIST_VER = 2.5;

export const PERSIST_CONFIG_KEY = 'root';
export const ADDRESS_BOOK_KEY_BACKUP = 'addressBookBackup';

0 comments on commit f9f6442

Please sign in to comment.