mango-ui-v3/components/BalancesTable.tsx

579 lines
23 KiB
TypeScript
Raw Normal View History

import { useCallback } from 'react'
import { useBalances } from '../hooks/useBalances'
2021-09-13 14:14:59 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-09-03 05:11:21 -07:00
import Button, { LinkButton } from '../components/Button'
2021-04-11 21:17:23 -07:00
import { notify } from '../utils/notifications'
2021-09-03 05:11:21 -07:00
import { ArrowSmDownIcon, ExclamationIcon } from '@heroicons/react/outline'
import { Market } from '@project-serum/serum'
import {
getMarketIndexBySymbol,
getTokenBySymbol,
} from '@blockworks-foundation/mango-client'
2021-08-31 10:47:03 -07:00
import { useState } from 'react'
import Loading from './Loading'
2021-09-03 05:11:21 -07:00
import { useViewport } from '../hooks/useViewport'
import { breakpoints } from './TradePageGrid'
import { floorToDecimal, formatUsdValue } from '../utils'
import { Table, Td, Th, TrBody, TrHead } from './TableElements'
import { useSortableData } from '../hooks/useSortableData'
import DepositModal from './DepositModal'
import WithdrawModal from './WithdrawModal'
import { ExpandableRow } from './TableElements'
import MobileTableHeader from './mobile/MobileTableHeader'
import { useTranslation } from 'next-i18next'
2022-01-15 13:18:17 -08:00
import { TransactionSignature } from '@solana/web3.js'
const BalancesTable = ({
showZeroBalances = false,
showDepositWithdraw = false,
}) => {
const { t } = useTranslation('common')
const [showDepositModal, setShowDepositModal] = useState(false)
const [showWithdrawModal, setShowWithdrawModal] = useState(false)
const [actionSymbol, setActionSymbol] = useState('')
const balances = useBalances()
2021-09-03 05:11:21 -07:00
const { items, requestSort, sortConfig } = useSortableData(
balances
.filter(
(bal) =>
showZeroBalances ||
2021-09-03 05:11:21 -07:00
+bal.deposits > 0 ||
+bal.borrows > 0 ||
bal.orders > 0 ||
bal.unsettled > 0
)
.sort((a, b) => Math.abs(+b.value) - Math.abs(+a.value))
)
2021-04-20 07:09:25 -07:00
const actions = useMangoStore((s) => s.actions)
2021-09-03 05:11:21 -07:00
const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current)
const mangoGroupConfig = useMangoStore((s) => s.selectedMangoGroup.config)
2021-09-13 14:14:59 -07:00
const mangoClient = useMangoStore((s) => s.connection.client)
const selectedMarket = useMangoStore((s) => s.selectedMarket.current)
const marketConfig = useMangoStore((s) => s.selectedMarket.config)
const setMangoStore = useMangoStore((s) => s.set)
const price = useMangoStore((s) => s.tradeForm.price)
const mangoGroupCache = useMangoStore((s) => s.selectedMangoGroup.cache)
2021-09-03 05:11:21 -07:00
const { width } = useViewport()
2021-08-31 10:47:03 -07:00
const [submitting, setSubmitting] = useState(false)
const isMobile = width ? width < breakpoints.md : false
2022-02-09 07:11:51 -08:00
const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current)
const wallet = useMangoStore((s) => s.wallet.current)
2022-02-09 07:48:05 -08:00
const canWithdraw = mangoAccount?.owner.equals(wallet.publicKey)
2021-04-10 14:12:15 -07:00
const handleSizeClick = (size, symbol) => {
const step = selectedMarket.minOrderSize
const marketIndex = getMarketIndexBySymbol(
mangoGroupConfig,
marketConfig.baseSymbol
)
const priceOrDefault = price
? price
: mangoGroup.getPrice(marketIndex, mangoGroupCache).toNumber()
if (symbol === 'USDC') {
const baseSize = Math.floor(size / priceOrDefault / step) * step
setMangoStore((state) => {
state.tradeForm.baseSize = baseSize
2022-02-10 09:49:59 -08:00
state.tradeForm.quoteSize = baseSize * priceOrDefault
state.tradeForm.side = 'buy'
})
} else {
const roundedSize = Math.round(size / step) * step
const quoteSize = roundedSize * priceOrDefault
setMangoStore((state) => {
state.tradeForm.baseSize = roundedSize
2022-02-10 09:49:59 -08:00
state.tradeForm.quoteSize = quoteSize
state.tradeForm.side = 'sell'
})
}
}
const handleOpenDepositModal = useCallback((symbol) => {
setActionSymbol(symbol)
setShowDepositModal(true)
}, [])
const handleOpenWithdrawModal = useCallback((symbol) => {
setActionSymbol(symbol)
setShowWithdrawModal(true)
}, [])
2021-04-10 14:12:15 -07:00
async function handleSettleAll() {
const markets = useMangoStore.getState().selectedMangoGroup.markets
2021-04-10 14:12:15 -07:00
try {
2021-08-31 10:47:03 -07:00
setSubmitting(true)
const spotMarkets = Object.values(markets).filter(
(mkt) => mkt instanceof Market
) as Market[]
2022-01-15 13:25:14 -08:00
2022-01-15 13:18:17 -08:00
const txids: TransactionSignature[] = await mangoClient.settleAll(
mangoGroup,
mangoAccount,
spotMarkets,
wallet
)
2021-08-31 10:47:03 -07:00
for (const txid of txids) {
notify({ title: t('settle-success'), txid })
}
2021-04-10 14:12:15 -07:00
} catch (e) {
console.warn('Error settling all:', e)
if (e.message === 'No unsettled funds') {
2021-04-11 21:17:23 -07:00
notify({
title: t('no-unsettled'),
2021-04-11 21:17:23 -07:00
type: 'error',
})
2021-04-10 14:12:15 -07:00
} else {
2021-04-11 21:17:23 -07:00
notify({
title: t('settle-error'),
2021-04-11 21:17:23 -07:00
description: e.message,
txid: e.txid,
2021-04-11 21:17:23 -07:00
type: 'error',
})
2021-04-10 14:12:15 -07:00
}
2021-08-31 10:47:03 -07:00
} finally {
actions.reloadOrders()
2022-01-27 19:57:18 -08:00
// actions.reloadMangoAccount()
2021-08-31 10:47:03 -07:00
setSubmitting(false)
2021-04-10 14:12:15 -07:00
}
}
2021-09-03 05:11:21 -07:00
const unsettledBalances = balances.filter((bal) => bal.unsettled > 0)
2021-08-15 06:31:59 -07:00
return (
2021-12-07 20:00:02 -08:00
<div className={`flex flex-col pb-2 sm:pb-4`}>
2021-09-03 05:11:21 -07:00
{unsettledBalances.length > 0 ? (
<div className="border border-th-bkg-4 rounded-lg mb-6 p-4 sm:p-6">
2021-09-03 05:11:21 -07:00
<div className="flex items-center justify-between pb-4">
2022-02-20 19:55:13 -08:00
<div className="flex items-center">
2021-09-03 05:11:21 -07:00
<ExclamationIcon className="flex-shrink-0 h-5 mr-1.5 mt-0.5 text-th-primary w-5" />
2022-02-20 19:55:13 -08:00
<h3>{t('unsettled-balances')}</h3>
2021-04-10 14:12:15 -07:00
</div>
2021-09-03 05:11:21 -07:00
<Button
className="text-xs pt-0 pb-0 h-8 pl-3 pr-3 whitespace-nowrap"
onClick={handleSettleAll}
>
{submitting ? <Loading /> : t('settle-all')}
2021-09-03 05:11:21 -07:00
</Button>
</div>
{unsettledBalances.map((bal) => {
const tokenConfig = getTokenBySymbol(mangoGroupConfig, bal.symbol)
return (
<div
className="border-b border-th-bkg-4 flex items-center justify-between py-4 last:border-b-0 last:pb-0"
key={bal.symbol}
>
<div className="flex items-center">
<img
alt=""
width="20"
height="20"
src={`/assets/icons/${bal.symbol.toLowerCase()}.svg`}
className={`mr-2.5`}
/>
<div>{bal.symbol}</div>
</div>
{`${floorToDecimal(bal.unsettled, tokenConfig.decimals)} ${
bal.symbol
}`}
</div>
)
})}
</div>
) : null}
2021-12-07 20:00:02 -08:00
<div className={`md:overflow-x-auto`}>
2021-09-06 04:51:18 -07:00
<div className={`align-middle inline-block min-w-full`}>
2021-09-03 05:11:21 -07:00
{items.length > 0 ? (
!isMobile ? (
<Table>
<thead>
<TrHead>
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('symbol')}
>
{t('asset')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'symbol'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('deposits')}
>
{t('deposits')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'deposits'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('borrows')}
>
{t('borrows')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'borrows'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('orders')}
>
{t('in-orders')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'orders'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('unsettled')}
>
{t('unsettled')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'unsettled'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('net')}
>
{t('net-balance')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'net'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('value')}
>
{t('value')}
2021-09-03 05:11:21 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'value'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-06 04:51:18 -07:00
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('depositRate')}
>
{t('deposit-rate')}
2021-09-06 04:51:18 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'depositRate'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
<Th>
<LinkButton
className="flex items-center no-underline font-normal text-left"
onClick={() => requestSort('borrowRate')}
>
{t('borrow-rate')}
2021-09-06 04:51:18 -07:00
<ArrowSmDownIcon
className={`default-transition flex-shrink-0 h-4 w-4 ml-1 ${
sortConfig?.key === 'borrowRate'
? sortConfig.direction === 'ascending'
? 'transform rotate-180'
: 'transform rotate-360'
: null
}`}
/>
</LinkButton>
</Th>
2021-09-03 05:11:21 -07:00
</TrHead>
</thead>
<tbody>
{items.map((balance, index) => (
2022-02-10 15:48:14 -08:00
<TrBody key={`${balance.symbol}${index}`}>
2021-09-03 05:11:21 -07:00
<Td>
<div className="flex items-center">
2021-07-24 13:15:27 -07:00
<img
alt=""
width="20"
height="20"
src={`/assets/icons/${balance.symbol.toLowerCase()}.svg`}
className={`mr-2.5`}
/>
2021-07-19 10:13:03 -07:00
2021-07-24 13:15:27 -07:00
{balance.symbol}
2021-09-03 05:11:21 -07:00
</div>
</Td>
<Td>{balance.deposits.toFixed()}</Td>
<Td>{balance.borrows.toFixed()}</Td>
<Td>{balance.orders}</Td>
<Td>{balance.unsettled}</Td>
<Td>
{marketConfig.kind === 'spot' &&
marketConfig.name.includes(balance.symbol) &&
selectedMarket ? (
<span
className={
balance.net.toNumber() > 0
? 'cursor-pointer underline hover:no-underline'
: ''
}
onClick={() =>
handleSizeClick(balance.net, balance.symbol)
}
>
{balance.net.toFixed()}
</span>
) : (
balance.net.toFixed()
)}
</Td>
<Td>{formatUsdValue(balance.value.toNumber())}</Td>
2021-09-06 04:51:18 -07:00
<Td>
<span className="text-th-green">
{balance.depositRate.toFixed(2)}%
</span>
</Td>
<Td>
<span className="text-th-red">
{balance.borrowRate.toFixed(2)}%
</span>
</Td>
{showDepositWithdraw ? (
<Td>
<div className="flex justify-end">
<Button
className="text-xs pt-0 pb-0 h-7 pl-3 pr-3"
onClick={() =>
handleOpenDepositModal(balance.symbol)
}
>
{balance.borrows.toNumber() > 0
? t('repay')
: t('deposit')}
</Button>
<Button
className="text-xs pt-0 pb-0 h-7 ml-4 pl-3 pr-3"
onClick={() =>
handleOpenWithdrawModal(balance.symbol)
}
2022-02-09 07:11:51 -08:00
disabled={!canWithdraw}
>
{t('withdraw')}
</Button>
</div>
</Td>
) : null}
2021-09-03 05:11:21 -07:00
</TrBody>
))}
</tbody>
{showDepositModal && (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
tokenSymbol={actionSymbol}
// repayAmount={
// balance.borrows.toNumber() > 0
// ? balance.borrows.toFixed()
// : ''
// }
/>
)}
{showWithdrawModal && (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
tokenSymbol={actionSymbol}
/>
)}
2021-09-03 05:11:21 -07:00
</Table>
) : (
<>
<MobileTableHeader
colOneHeader={t('asset')}
colTwoHeader={t('net-balance')}
/>
2021-09-03 05:11:21 -07:00
{items.map((balance, index) => (
<ExpandableRow
buttonTemplate={
2022-02-10 17:50:40 -08:00
<div className="flex items-center justify-between text-th-fgd-1 w-full">
<div className="flex items-center text-th-fgd-1">
<img
alt=""
width="20"
height="20"
src={`/assets/icons/${balance.symbol.toLowerCase()}.svg`}
className={`mr-2.5`}
/>
2021-09-03 05:11:21 -07:00
{balance.symbol}
</div>
2022-02-10 17:50:40 -08:00
<div className="text-th-fgd-1 text-right">
{balance.net.toFixed()}
</div>
2021-09-28 06:15:36 -07:00
</div>
}
key={`${balance.symbol}${index}`}
index={index}
panelTemplate={
<>
<div className="grid grid-cols-2 grid-flow-row gap-4 pb-4">
<div className="text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('deposits')}
</div>
{balance.deposits.toFixed()}
2021-09-03 05:11:21 -07:00
</div>
<div className="text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('borrows')}
</div>
{balance.borrows.toFixed()}
2021-09-03 05:11:21 -07:00
</div>
<div className="text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('in-orders')}
</div>
{balance.orders.toFixed()}
</div>
<div className="text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('unsettled')}
</div>
{balance.unsettled.toFixed()}
</div>
<div className="text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('value')}
</div>
{formatUsdValue(balance.value.toNumber())}
</div>
<div className="text-left text-th-fgd-4">
<div className="pb-0.5 text-th-fgd-3 text-xs">
{t('rates')}
</div>
<span className="mr-1 text-th-green">
{balance.depositRate.toFixed(2)}%
</span>
/
<span className="ml-1 text-th-red">
{balance.borrowRate.toFixed(2)}%
</span>
</div>
</div>
<div className="flex space-x-4">
<Button
className="text-xs pt-0 pb-0 h-7 pl-3 pr-3 w-1/2"
onClick={() =>
handleOpenDepositModal(balance.symbol)
}
>
2021-11-09 16:34:03 -08:00
{balance.borrows.toNumber() > 0
? t('repay')
: t('deposit')}
</Button>
<Button
className="text-xs pt-0 pb-0 h-7 pl-3 pr-3 w-1/2"
onClick={() =>
handleOpenWithdrawModal(balance.symbol)
}
>
{t('withdraw')}
</Button>
</div>
2021-11-09 16:34:03 -08:00
{showDepositModal && (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
tokenSymbol={actionSymbol}
repayAmount={
balance.borrows.toNumber() > 0
? balance.borrows.toFixed()
: ''
}
/>
)}
{showWithdrawModal && (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
tokenSymbol={actionSymbol}
/>
)}
2021-09-03 05:11:21 -07:00
</>
}
/>
2021-09-03 05:11:21 -07:00
))}
</>
)
) : (
<div
2021-04-19 06:45:59 -07:00
className={`w-full text-center py-6 bg-th-bkg-1 text-th-fgd-3 rounded-md`}
>
{t('no-balances')}
</div>
)}
</div>
</div>
</div>
)
}
export default BalancesTable