From 1eec10090700b94aee21e9a36794e01db09b750d Mon Sep 17 00:00:00 2001 From: Linkie Link Date: Fri, 28 Jul 2023 21:37:54 +0200 Subject: [PATCH] fix: updated the account balances and added debts (#325) --- .../Account/AccountBalancesTable.tsx | 108 ++++++++++-------- src/components/Account/AccountComposition.tsx | 15 ++- src/components/Account/AccountOverview.tsx | 20 +++- src/components/Account/AccountSummary.tsx | 18 ++- src/types/interfaces/account.d.ts | 2 +- 5 files changed, 105 insertions(+), 58 deletions(-) diff --git a/src/components/Account/AccountBalancesTable.tsx b/src/components/Account/AccountBalancesTable.tsx index 5be4f619..8f36e039 100644 --- a/src/components/Account/AccountBalancesTable.tsx +++ b/src/components/Account/AccountBalancesTable.tsx @@ -19,15 +19,43 @@ import { DISPLAY_CURRENCY_KEY } from 'constants/localStore' import useLocalStorage from 'hooks/useLocalStorage' import usePrices from 'hooks/usePrices' import { BNCoin } from 'types/classes/BNCoin' +import { byDenom } from 'utils/array' import { getAssetByDenom } from 'utils/assets' -import { convertToDisplayAmount, demagnify } from 'utils/formatters' +import { convertLiquidityRateToAPR, convertToDisplayAmount, demagnify } from 'utils/formatters' import { BN } from 'utils/helpers' +import { convertAprToApy } from 'utils/parsers' interface Props { - data: Account + account: Account + lendingData: LendingMarketTableData[] + borrowingData: BorrowMarketTableData[] } -export const AccountBalancesTable = (props: Props) => { +function calculatePositionValues( + type: 'deposits' | 'borrowing' | 'lending', + asset: Asset, + prices: BNCoin[], + displayCurrencyDenom: string, + position: BNCoin, + apy: number, +) { + const { amount, denom } = position + return { + type, + symbol: asset.symbol, + size: demagnify(amount, asset), + value: convertToDisplayAmount( + BNCoin.fromDenomAndBigNumber(denom, amount), + displayCurrencyDenom, + prices, + ).toString(), + denom, + amount: type === 'borrowing' ? amount.negated() : amount, + apy, + } +} + +export default function AccountBalancesTable(props: Props) { const [displayCurrency] = useLocalStorage( DISPLAY_CURRENCY_KEY, DEFAULT_SETTINGS.displayCurrency, @@ -35,48 +63,35 @@ export const AccountBalancesTable = (props: Props) => { const { data: prices } = usePrices() const [sorting, setSorting] = React.useState([]) - const balanceData = React.useMemo(() => { - const accountDeposits = props.data?.deposits ?? [] - const accountLends = props.data?.lends ?? [] + const accountDeposits = props.account?.deposits ?? [] + const accountLends = props.account?.lends ?? [] + const accountDebts = props.account?.debts ?? [] const deposits = accountDeposits.map((deposit) => { const asset = ASSETS.find((asset) => asset.denom === deposit.denom) ?? ASSETS[0] const apy = 0 - return { - type: 'deposit', - symbol: asset.symbol, - denom: deposit.denom, - amount: deposit.amount, - size: demagnify(deposit.amount, asset), - value: convertToDisplayAmount( - new BNCoin({ amount: deposit.amount, denom: deposit.denom }), - displayCurrency, - prices, - ).toString(), - apy, - } - }) - const lends = accountLends.map((lending) => { - const asset = ASSETS.find((asset) => asset.denom === lending.denom) ?? ASSETS[0] - const apy = 0 - return { - type: 'lending', - symbol: asset.symbol, - denom: lending.denom, - amount: lending.amount, - size: demagnify(lending.amount, asset), - value: convertToDisplayAmount( - new BNCoin({ amount: lending.amount, denom: lending.denom }), - displayCurrency, - prices, - ).toString(), - apy, - } + return calculatePositionValues('deposits', asset, prices, displayCurrency, deposit, apy) }) - return [...deposits, ...lends] - }, [displayCurrency, prices, props.data?.deposits, props.data?.lends]) + const lends = accountLends.map((lending) => { + const asset = ASSETS.find((asset) => asset.denom === lending.denom) ?? ASSETS[0] + const apr = convertLiquidityRateToAPR( + props.lendingData.find((market) => market.asset.denom === lending.denom) + ?.marketLiquidityRate ?? 0, + ) + const apy = convertAprToApy(apr, 365) + return calculatePositionValues('lending', asset, prices, displayCurrency, lending, apy) + }) + const debts = accountDebts.map((debt) => { + const asset = ASSETS.find(byDenom(debt.denom)) ?? ASSETS[0] + const apy = + props.borrowingData.find((market) => market.asset.denom === debt.denom)?.borrowRate ?? 0 + return calculatePositionValues('borrowing', asset, prices, displayCurrency, debt, apy * -100) + }) + + return [...deposits, ...lends, ...debts] + }, [displayCurrency, prices, props.account, props.borrowingData, props.lendingData]) const columns = React.useMemo[]>( () => [ @@ -88,7 +103,7 @@ export const AccountBalancesTable = (props: Props) => { return ( {row.original.symbol} - {row.original.type === 'lending' && (Lent)} + {row.original.type === 'lending' && (lent)} ) }, @@ -98,8 +113,11 @@ export const AccountBalancesTable = (props: Props) => { accessorKey: 'value', id: 'value', cell: ({ row }) => { - const coin = new BNCoin({ denom: row.original.denom, amount: row.original.amount }) - return + const coin = new BNCoin({ + denom: row.original.denom, + amount: row.original.amount.toString(), + }) + return }, }, { @@ -113,8 +131,8 @@ export const AccountBalancesTable = (props: Props) => { ) return ( @@ -174,7 +192,7 @@ export const AccountBalancesTable = (props: Props) => { 'align-center', )} > - + {header.column.getCanSort() ? { asc: , @@ -203,7 +221,7 @@ export const AccountBalancesTable = (props: Props) => { {row.getVisibleCells().map((cell) => { const borderClass = - cell.row.original.type === 'deposit' ? 'border-profit' : 'border-loss' + cell.row.original.type === 'borrowing' ? 'border-loss' : 'border-profit' return ( s.address) const { data: account } = useAccounts(address) + const { availableAssets: borrowAvailableAssets, accountBorrowedAssets } = + useBorrowMarketAssetsTableData() + const { availableAssets: lendingAvailableAssets, accountLentAssets } = + useLendingMarketAssetsTableData() + const borrowAssetsData = [...borrowAvailableAssets, ...accountBorrowedAssets] + const lendingAssetsData = [...lendingAvailableAssets, ...accountLentAssets] if (!address) { return ( @@ -35,7 +43,11 @@ function Content() { Balances - + ))} diff --git a/src/components/Account/AccountSummary.tsx b/src/components/Account/AccountSummary.tsx index e54d5348..9839a138 100644 --- a/src/components/Account/AccountSummary.tsx +++ b/src/components/Account/AccountSummary.tsx @@ -1,5 +1,5 @@ import Accordion from 'components/Accordion' -import { AccountBalancesTable } from 'components/Account/AccountBalancesTable' +import AccountBalancesTable from 'components/Account/AccountBalancesTable' import AccountComposition from 'components/Account/AccountComposition' import AccountHealth from 'components/Account/AccountHealth' import Card from 'components/Card' @@ -8,7 +8,9 @@ import { ArrowChartLineUp } from 'components/Icons' import Text from 'components/Text' import { BN_ZERO } from 'constants/math' import { ORACLE_DENOM } from 'constants/oracle' +import useBorrowMarketAssetsTableData from 'hooks/useBorrowMarketAssetsTableData' import useIsOpenArray from 'hooks/useIsOpenArray' +import useLendingMarketAssetsTableData from 'hooks/useLendingMarketAssetsTableData' import usePrices from 'hooks/usePrices' import { BNCoin } from 'types/classes/BNCoin' import { calculateAccountDepositsValue } from 'utils/accounts' @@ -24,6 +26,12 @@ export default function AccountSummary(props: Props) { const accountBalance = props.account ? calculateAccountDepositsValue(props.account, prices) : BN_ZERO + const { availableAssets: borrowAvailableAssets, accountBorrowedAssets } = + useBorrowMarketAssetsTableData() + const { availableAssets: lendingAvailableAssets, accountLentAssets } = + useLendingMarketAssetsTableData() + const borrowAssetsData = [...borrowAvailableAssets, ...accountBorrowedAssets] + const lendingAssetsData = [...lendingAvailableAssets, ...accountLentAssets] if (!props.account) return null return ( @@ -60,7 +68,13 @@ export default function AccountSummary(props: Props) { { title: 'Balances', renderContent: () => - props.account ? : null, + props.account ? ( + + ) : null, isOpen: isOpen[1], toggleOpen: (index: number) => toggleOpen(index), renderSubTitle: () => <>, diff --git a/src/types/interfaces/account.d.ts b/src/types/interfaces/account.d.ts index f4ca6903..1f66ed60 100644 --- a/src/types/interfaces/account.d.ts +++ b/src/types/interfaces/account.d.ts @@ -17,7 +17,7 @@ interface AccountBalanceRow { type: string symbol: string denom: string - amount: string + amount: BigNumber value: string | number size: number apy: number