import { Bank } from '@blockworks-foundation/mango-v4' import Change from '@components/shared/Change' import ChartRangeButtons from '@components/shared/ChartRangeButtons' import FormatNumericValue from '@components/shared/FormatNumericValue' import SheenLoader from '@components/shared/SheenLoader' import { ArrowSmallUpIcon, NoSymbolIcon } from '@heroicons/react/20/solid' import { useQuery } from '@tanstack/react-query' import { makeApiRequest } from 'apis/birdeye/helpers' import dayjs from 'dayjs' import relativeTime from 'dayjs/plugin/relativeTime' import { BirdeyePriceResponse } from 'hooks/useBirdeyeMarketPrices' import parse from 'html-react-parser' import { useTranslation } from 'next-i18next' import { useMemo, useState } from 'react' import PriceChart from '@components/token/PriceChart' import { DAILY_SECONDS } from 'utils/constants' dayjs.extend(relativeTime) const DEFAULT_COINGECKO_VALUES = { ath: 0, atl: 0, ath_change_percentage: 0, atl_change_percentage: 0, ath_date: 0, atl_date: 0, high_24h: 0, circulating_supply: 0, fully_diluted_valuation: 0, low_24h: 0, market_cap: 0, max_supply: 0, price_change_percentage_24h: 0, total_supply: 0, total_volume: 0, } interface BirdeyeResponse { data: { items: BirdeyePriceResponse[] } success: boolean } const fetchBirdeyePrices = async ( daysToShow: string, mint: string, ): Promise => { const interval = daysToShow === '1' ? '30m' : daysToShow === '7' ? '1H' : '4H' const queryEnd = Math.floor(Date.now() / 1000) const queryStart = queryEnd - parseInt(daysToShow) * DAILY_SECONDS const query = `defi/history_price?address=${mint}&address_type=token&type=${interval}&time_from=${queryStart}&time_to=${queryEnd}` const response: BirdeyeResponse = await makeApiRequest(query) if (response.success && response?.data?.items) { return response.data.items } return [] } const CoingeckoStats = ({ bank, coingeckoData, }: { bank: Bank // TODO: Add Coingecko api types // eslint-disable-next-line @typescript-eslint/no-explicit-any coingeckoData: any }) => { const { t } = useTranslation(['common', 'token']) const [showFullDesc, setShowFullDesc] = useState(false) const [daysToShow, setDaysToShow] = useState('1') const { data: birdeyePrices, isLoading: loadingBirdeyePrices, isFetching: fetchingBirdeyePrices, } = useQuery( ['birdeye-token-prices', daysToShow, bank.mint], () => fetchBirdeyePrices(daysToShow, bank.mint.toString()), { cacheTime: 1000 * 60 * 15, staleTime: 1000 * 60 * 10, retry: 3, enabled: !!bank, refetchOnWindowFocus: false, }, ) const { ath, atl, ath_change_percentage, atl_change_percentage, ath_date, atl_date, circulating_supply, fully_diluted_valuation, market_cap, max_supply, total_supply, total_volume, } = coingeckoData ? coingeckoData : DEFAULT_COINGECKO_VALUES const truncateDescription = (desc: string) => desc.substring(0, (desc + ' ').lastIndexOf(' ', 144)) const description = useMemo(() => { const desc = coingeckoData?.description?.en if (!desc) return '' return showFullDesc ? coingeckoData.description.en : truncateDescription(coingeckoData.description.en) }, [coingeckoData, showFullDesc]) return ( <> {description ? (

About {bank.name}

{parse(description)}

{coingeckoData.description.en.length > description.length || showFullDesc ? ( setShowFullDesc(!showFullDesc)} > {showFullDesc ? 'Less' : 'More'} ) : null}
) : null}

{bank.name} Price Chart

setDaysToShow(v)} />
{birdeyePrices?.length ? ( ) : loadingBirdeyePrices || fetchingBirdeyePrices ? (
) : (

{t('chart-unavailable')}

)}

{bank.name} Stats

{t('token:market-cap')}

{market_cap?.usd ? ( ) : ( {t('unavailable')} )}{' '} {coingeckoData.market_cap_rank ? `#${coingeckoData.market_cap_rank}` : ''}

{t('token:volume')}

{total_volume?.usd ? ( ) : ( {t('unavailable')} )}

{t('token:all-time-high')}

{ath?.usd ? ( ) : ( {t('unavailable')} )} {ath_change_percentage.usd ? ( ) : null}

{dayjs(ath_date.usd).format('MMM, D, YYYY')} ( {dayjs(ath_date.usd).fromNow()})

{t('token:all-time-low')}

{atl?.usd ? ( ) : ( {t('unavailable')} )}

{dayjs(atl_date.usd).format('MMM, D, YYYY')} ( {dayjs(atl_date.usd).fromNow()})

{fully_diluted_valuation.usd ? (

{t('token:fdv')}

{fully_diluted_valuation?.usd ? ( ) : ( {t('unavailable')} )}

) : null}

{t('token:circulating-supply')}

{circulating_supply ? ( ) : ( {t('unavailable')} )}

{t('token:total-supply')}

{total_supply ? ( ) : ( {t('unavailable')} )}

{max_supply ? (

{t('token:max-supply')}

{max_supply ? ( ) : ( {t('unavailable')} )}

) : null}
) } export default CoingeckoStats