diff --git a/src/components/explorer/ExplorerHeader.tsx b/src/components/explorer/ExplorerHeader.tsx index 478d3726e..1fb1ae720 100644 --- a/src/components/explorer/ExplorerHeader.tsx +++ b/src/components/explorer/ExplorerHeader.tsx @@ -1,13 +1,17 @@ +import { buttonStyles } from 'components/common/button/buttonStyles'; import { TokensOverlap } from 'components/common/tokensOverlap'; import { getStrategyType } from 'components/strategies/common/utils'; import { useContract } from 'hooks/useContract'; import { useTokens } from 'hooks/useTokens'; import { Strategy, useGetStrategList } from 'libs/queries'; -import { Trending, useTrending } from 'libs/queries/extApi/tradeCount'; -import { StrategyType } from 'libs/routing'; +import { + TradeCount, + Trending, + useTrending, +} from 'libs/queries/extApi/tradeCount'; +import { Link, StrategyType } from 'libs/routing'; import { Token } from 'libs/tokens'; -import { fetchTokenData } from 'libs/tokens/tokenHelperFn'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; import { prettifyNumber } from 'utils/helpers'; interface TrendingPair { @@ -17,30 +21,44 @@ interface TrendingPair { trades: number; } +const typeLabel = { + disposable: 'Limit / Range', + recurring: 'Recurring', + overlapping: 'Concentrated', +}; + const useTrendingPairs = (trending?: Trending) => { const [loading, setLoading] = useState(true); const { tokensMap, importToken } = useTokens(); const { Token } = useContract(); - useEffect(() => { - const missingTokens = new Set(); - for (const { token0, token1 } of trending?.tradeCount ?? []) { - if (!tokensMap.has(token0.toLowerCase())) missingTokens.add(token0); - if (!tokensMap.has(token1.toLowerCase())) missingTokens.add(token1); - } - if (!missingTokens.size) return; - const getTokens = Array.from(missingTokens).map((address) => { - return fetchTokenData(Token, address); - }); - Promise.all(getTokens).then((tokens) => { - tokens.forEach((data) => importToken(data)); - setLoading(false); - }); - }, [Token, importToken, tokensMap, trending?.tradeCount]); - if (!trending || loading) return []; + // useEffect(() => { + // const missingTokens = new Set(); + // for (const { token0, token1 } of trending?.tradeCount ?? []) { + // if (!tokensMap.has(token0.toLowerCase())) missingTokens.add(token0); + // if (!tokensMap.has(token1.toLowerCase())) missingTokens.add(token1); + // } + // if (!missingTokens.size) return; + // const getTokens = Array.from(missingTokens).map((address) => { + // return fetchTokenData(Token, address); + // }); + // Promise.all(getTokens).then((tokens) => { + // console.log({ tokens }); + // tokens.forEach((data) => importToken(data)); + // setLoading(false); + // }); + // }, [Token, importToken, tokensMap, trending?.tradeCount]); + if (!trending) return { isLoading: true, data: [] }; + + const isMissingToken = (trade: TradeCount) => { + if (!tokensMap.has(trade.token0.toLowerCase())) return true; + if (!tokensMap.has(trade.token1.toLowerCase())) return true; + return false; + }; const pairs: Record = {}; for (const trade of trending.tradeCount) { + if (isMissingToken(trade)) continue; pairs[trade.pairAddresses] ||= { trades: 0, trades_24h: 0 }; pairs[trade.pairAddresses].trades += trade.strategyTrades; pairs[trade.pairAddresses].trades_24h += trade.strategyTrades_24h; @@ -49,8 +67,8 @@ const useTrendingPairs = (trending?: Trending) => { .filter(([, { trades_24h }]) => !!trades_24h) .map(([pairAddress, { trades }]) => ({ pairAddress: pairAddress, - base: tokensMap.get(pairAddress.split('/').shift()!)!, - quote: tokensMap.get(pairAddress.split('/').pop()!)!, + base: tokensMap.get(pairAddress.split('/').shift()!.toLowerCase())!, + quote: tokensMap.get(pairAddress.split('/').pop()!.toLowerCase())!, trades, })) .sort((a, b) => b.trades - a.trades) @@ -61,14 +79,19 @@ const useTrendingPairs = (trending?: Trending) => { const index = Math.floor(Math.random() * list.length); result[i] = list.splice(index, 1)[0]; } - return result.sort((a, b) => b.trades - a.trades); + return { + isLoading: false, + data: result.sort((a, b) => b.trades - a.trades), + }; }; interface StrategyWithTradeCount extends Strategy { trades: number; type: StrategyType; } -const useTrendStrategies = (trending?: Trending): StrategyWithTradeCount[] => { +const useTrendStrategies = ( + trending?: Trending +): { isLoading: boolean; data: StrategyWithTradeCount[] } => { const list = (trending?.tradeCount ?? []) .filter((t) => !!t.strategyTrades_24h) .sort((a, b) => b.strategyTrades - a.strategyTrades) @@ -81,67 +104,114 @@ const useTrendStrategies = (trending?: Trending): StrategyWithTradeCount[] => { record[item.id] = item.strategyTrades; } const query = useGetStrategList(Object.keys(record)); - return (query.data ?? []).map((strategy) => ({ + if (query.isLoading) { + return { isLoading: true, data: [] }; + } + const data = (query.data ?? []).map((strategy) => ({ ...strategy, type: getStrategyType(strategy), trades: record[strategy.id], })); + return { isLoading: false, data }; }; export const ExplorerHeader = () => { const { data: trending } = useTrending(); - const strategies = useTrendStrategies(trending); - const pairs = useTrendingPairs(trending); - console.log({ strategies, pairs }); + const trendingStrategies = useTrendStrategies(trending); + const trendingPairs = useTrendingPairs(trending); + const strategies = trendingStrategies.data; + const pairs = trendingPairs.data; return ( -
-
+
+

Total Trades

- {trending?.totalTradeCount} + {prettifyNumber(trending?.totalTradeCount ?? '0')}

+
+ + Trade + + + Create Strategy + +
-
+

Populare Pairs

- +
- + + {trendingPairs.isLoading && + [1, 2, 3].map((id) => ( + + + + + ))} {pairs.map(({ pairAddress, base, quote, trades }) => ( - + ))}
Token PairTradesTrades
+ + + +
- - {base?.symbol}/{quote?.symbol} +
+ + {base?.symbol} + / + {quote?.symbol} +
{prettifyNumber(trades)}{prettifyNumber(trades)}
-
+

Trending Strategies

- +
- + + {trendingStrategies.isLoading && + [1, 2, 3].map((id) => ( + + + + + + ))} {strategies.map(({ id, idDisplay, base, quote, type, trades }) => ( - - + + ))} @@ -150,3 +220,9 @@ export const ExplorerHeader = () => { ); }; + +const Loading = () => ( +
+
+
+); diff --git a/src/libs/queries/extApi/tradeCount.ts b/src/libs/queries/extApi/tradeCount.ts index 2aa1ec037..cb7bfbf1c 100644 --- a/src/libs/queries/extApi/tradeCount.ts +++ b/src/libs/queries/extApi/tradeCount.ts @@ -3,7 +3,7 @@ import { QueryKey } from 'libs/queries/queryKey'; import { ONE_HOUR_IN_MS } from 'utils/time'; import { carbonApi } from 'utils/carbonApi'; -interface TradeCount { +export interface TradeCount { id: string; strategyTrades: number; strategyTrades_24h: number;
ID TypesTradesTrades
+ + + + + +
- - {idDisplay} +
+ + {idDisplay} +
{type}{prettifyNumber(trades)}{typeLabel[type]}{prettifyNumber(trades)}