import { getTokenBySymbol } from '@blockworks-foundation/mango-client' import { useEffect, useMemo, useState } from 'react' import useMangoStore from '../../stores/useMangoStore' import Loading from '../Loading' import Select from '../Select' import { Table, Td, Th, TrBody, TrHead } from '../TableElements' import { useTranslation } from 'next-i18next' import { isEmpty } from 'lodash' import usePagination from '../../hooks/usePagination' import { roundToDecimal } from '../../utils/' import Pagination from '../Pagination' interface InterestStats { [key: string]: { total_borrow_interest: number total_deposit_interest: number } } const AccountInterest = () => { const { t } = useTranslation('common') const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const [interestStats, setInterestStats] = useState([]) const [hourlyInterestStats, setHourlyInterestStats] = useState([]) const [loading, setLoading] = useState(false) const [selectedAsset, setSelectedAsset] = useState('USDC') const { paginated, setData, totalPages, nextPage, previousPage, page, firstPage, lastPage, } = usePagination(hourlyInterestStats[selectedAsset]) const mangoAccountPk = useMemo(() => { return mangoAccount.publicKey.toString() }, [mangoAccount]) const token = useMemo(() => { return getTokenBySymbol(groupConfig, selectedAsset) }, [selectedAsset]) useEffect(() => { if (!isEmpty(hourlyInterestStats)) { setData(hourlyInterestStats[selectedAsset]) } }, [selectedAsset, hourlyInterestStats]) useEffect(() => { const fetchInterestStats = async () => { const response = await fetch( `https://mango-transaction-log.herokuapp.com/v3/stats/total-interest-earned?mango-account=${mangoAccountPk}` ) const parsedResponse: InterestStats = await response.json() setInterestStats(Object.entries(parsedResponse)) } const fetchHourlyInterestStats = async () => { setLoading(true) const response = await fetch( `https://mango-transaction-log.herokuapp.com/v3/stats/hourly-interest?mango-account=${mangoAccountPk}` ) const parsedResponse = await response.json() const assets = Object.keys(parsedResponse) const stats = {} for (const asset of assets) { const x: any = Object.entries(parsedResponse[asset]) const token = getTokenBySymbol(groupConfig, asset) stats[asset] = x .map(([key, value]) => { const borrows = roundToDecimal( value.borrow_interest, token.decimals + 1 ) const deposits = roundToDecimal( value.deposit_interest, token.decimals + 1 ) if (borrows > 0 || deposits > 0) { return { ...value, time: key } } else { return null } }) .filter((x) => x) .reverse() } setLoading(false) setHourlyInterestStats(stats) } fetchHourlyInterestStats() fetchInterestStats() }, [mangoAccountPk]) return ( <>
{t('interest-earned')}
{mangoAccount ? (
{interestStats.length === 0 ? ( ) : ( interestStats.map(([symbol, stats], index) => { const decimals = getTokenBySymbol( groupConfig, symbol ).decimals return ( ) }) )}
{t('token')} {t('total-deposit-interest')} {t('total-borrow-interest')} {t('net')}
{t('no-interest')}
{symbol}
{stats.total_deposit_interest.toFixed(decimals)}{' '} {symbol} {stats.total_borrow_interest.toFixed(decimals)} {symbol} {( stats.total_deposit_interest - stats.total_borrow_interest ).toFixed(decimals)}{' '} {symbol}
<> {!isEmpty(hourlyInterestStats) && !loading ? ( <>
{Object.keys(hourlyInterestStats).map((token: string) => (
setSelectedAsset(token)} key={token} > {token}
))}
{paginated.length ? ( {paginated.map((stat, index) => { const date = new Date(stat.time) return ( ) })}
{t('time')} {t('interest')}
{date.toLocaleDateString()}{' '} {date.toLocaleTimeString()} {stat.borrow_interest > 0 ? `-${stat.borrow_interest.toFixed( token.decimals + 1 )}` : stat.deposit_interest.toFixed( token.decimals + 1 )}{' '} {selectedAsset}
) : (
No interest earned/paid
)}
) : loading ? (
) : null}
) : (
{t('connect-wallet')}
)} ) } export default AccountInterest