import { Bank } from '@blockworks-foundation/mango-v4' import MedalIcon from '@components/icons/MedalIcon' import ProfileImage from '@components/profile/ProfileImage' import FormatNumericValue from '@components/shared/FormatNumericValue' import SheenLoader from '@components/shared/SheenLoader' import { ChevronRightIcon, NoSymbolIcon } from '@heroicons/react/20/solid' import { useQuery } from '@tanstack/react-query' import { useTranslation } from 'next-i18next' import { MANGO_DATA_API_URL } from 'utils/constants' type TopDepositorBorrower = { date_hour: string mango_account: string token_index: number value: number wallet_pk: string } type TopTokenAccount = { date_hour: string mango_account: string token_index: number value: number wallet_pk: string profile_image_url?: string profile_name?: string trader_category?: string } const fetchTopTokenAccounts = async (tokenIndex: number) => { try { const promises = [ fetch( `${MANGO_DATA_API_URL}/leaderboard-token-deposits?token-index=${tokenIndex}`, ), fetch( `${MANGO_DATA_API_URL}/leaderboard-token-borrows?token-index=${tokenIndex}`, ), ] const [depositsResponse, borrowsResponse] = await Promise.all(promises) const [depositsData, borrowsData]: [ TopDepositorBorrower[], TopDepositorBorrower[], ] = await Promise.all([depositsResponse.json(), borrowsResponse.json()]) const depositsDataToShow = depositsData && depositsData.length ? depositsData.slice(0, 10).filter((d) => d.value >= 1) : [] const borrowsDataToShow = borrowsData && borrowsData.length ? borrowsData.slice(0, 10).filter((d) => d.value <= -1) : [] const depositorProfilesResponse = await Promise.all( depositsDataToShow.map((r: TopDepositorBorrower) => fetch( `${MANGO_DATA_API_URL}/user-data/profile-details?wallet-pk=${r.wallet_pk}`, ), ), ) const depositorProfilesData = await Promise.all( depositorProfilesResponse.map((d) => d.json()), ) const borrowerProfilesResponse = await Promise.all( borrowsDataToShow.map((r: TopDepositorBorrower) => fetch( `${MANGO_DATA_API_URL}/user-data/profile-details?wallet-pk=${r.wallet_pk}`, ), ), ) const borrowerProfilesData = await Promise.all( borrowerProfilesResponse.map((d) => d.json()), ) return [ depositsDataToShow.map((data, i) => { if (depositorProfilesData[i]) { return { ...data, ...depositorProfilesData[i] } } else return data }), borrowsDataToShow.map((data, i) => { if (borrowerProfilesData[i]) { return { ...data, ...borrowerProfilesData[i] } } else return data }), ] } catch (e) { console.log('Failed to fetch top token accounts', e) } } const TopTokenAccounts = ({ bank }: { bank: Bank }) => { const { t } = useTranslation('token') const { data, isLoading, isFetching } = useQuery( ['topTokenAccounts', bank.tokenIndex], () => fetchTopTokenAccounts(bank!.tokenIndex), { cacheTime: 1000 * 60 * 10, staleTime: 1000 * 60, retry: 3, refetchOnWindowFocus: false, }, ) const topAccountsData = data ? data : [[], []] return (

{t('top-depositors', { symbol: bank.name })}

{topAccountsData[0] && topAccountsData[0].length ? ( topAccountsData[0].map((acc, i) => ( )) ) : isLoading || isFetching ? (
{[...Array(5)].map((x, i) => (
))}
) : (

{t('no-depositors')}

)}

{t('top-borrowers', { symbol: bank.name })}

{topAccountsData[1] && topAccountsData[1].length ? ( topAccountsData[1].map((acc, i) => ( )) ) : isLoading || isFetching ? (
{[...Array(5)].map((x, i) => (
))}
) : (

{t('no-borrowers')}

)}
) } export default TopTokenAccounts const LeaderboardRow = ({ item, rank, }: { item: TopTokenAccount rank: number }) => { const { profile_name, profile_image_url, mango_account, value, wallet_pk } = item return (

{rank}

{rank < 4 ? : null}

{profile_name || wallet_pk.slice(0, 4) + '...' + wallet_pk.slice(-4)}

Acc: {mango_account.slice(0, 4) + '...' + mango_account.slice(-4)}

{/* remove isUsd when api returns token amount rather than value */}

) }