mango-ui-v3/components/BalancesTable.tsx

475 lines
19 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'
2021-09-03 05:11:21 -07:00
import { 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'
const BalancesTable = ({ showZeroBalances = false }) => {
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)
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
2021-04-10 14:12:15 -07:00
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() {
2021-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
const markets = useMangoStore.getState().selectedMangoGroup.markets
2021-04-10 14:12:15 -07:00
const wallet = useMangoStore.getState().wallet.current
try {
2021-08-31 10:47:03 -07:00
setSubmitting(true)
const spotMarkets = Object.values(markets).filter(
(mkt) => mkt instanceof Market
) as Market[]
await mangoClient.settleAll(mangoGroup, mangoAccount, spotMarkets, wallet)
2021-08-31 10:47:03 -07:00
2021-07-21 14:11:17 -07:00
notify({ title: 'Successfully settled funds' })
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: 'There are no unsettled funds',
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: 'Error settling funds',
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 {
await actions.reloadMangoAccount()
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 (
<div className={`flex flex-col pb-2 sm:pb-4 sm:pt-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">
<div className="flex items-center sm:text-lg">
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" />
Unsettled Balances
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}
>
2021-09-03 05:11:21 -07:00
{submitting ? <Loading /> : 'Settle All'}
</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-09-06 04:51:18 -07:00
<div className={`-my-2 overflow-x-auto`}>
<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')}
>
Asset
<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')}
>
Deposits
<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')}
>
Borrows
<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')}
>
In Orders
<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')}
>
Unsettled
<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')}
>
Net Balance
<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')}
>
2021-09-03 05:11:21 -07:00
Value
<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')}
>
Deposit Rate
<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')}
>
Borrow Rate
<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) => (
<TrBody index={index} key={`${balance.symbol}${index}`}>
<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>{balance.net.toFixed()}</Td>
<Td>{formatUsdValue(balance.value)}</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>
<Td>
<div className="flex justify-end">
<Button
className="text-xs pt-0 pb-0 h-8 pl-3 pr-3"
onClick={() =>
handleOpenDepositModal(balance.symbol)
}
>
Deposit
</Button>
<Button
className="text-xs pt-0 pb-0 h-8 ml-4 pl-3 pr-3"
onClick={() =>
handleOpenWithdrawModal(balance.symbol)
}
>
Withdraw
</Button>
</div>
</Td>
2021-09-03 05:11:21 -07:00
</TrBody>
))}
</tbody>
{showDepositModal && (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
tokenSymbol={actionSymbol}
/>
)}
{showWithdrawModal && (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
tokenSymbol={actionSymbol}
/>
)}
2021-09-03 05:11:21 -07:00
</Table>
) : (
<>
<MobileTableHeader
headerTemplate={
<>
<div className="col-span-7">Asset</div>
<div className="col-span-4 text-right">Net Balance</div>
</>
}
/>
2021-09-03 05:11:21 -07:00
{items.map((balance, index) => (
<ExpandableRow
buttonTemplate={
2021-09-03 05:11:21 -07:00
<>
<div className="col-span-7 flex items-center text-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>
<div className="col-span-4 text-fgd-1 text-right">
{balance.net.toFixed()}
</div>
</>
}
key={`${balance.symbol}${index}`}
index={index}
panelTemplate={
<>
<div className="col-span-1 text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
Deposits
2021-09-03 05:11:21 -07:00
</div>
{balance.deposits.toFixed()}
</div>
<div className="col-span-1 text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
Borrows
2021-09-03 05:11:21 -07:00
</div>
{balance.borrows.toFixed()}
</div>
<div className="col-span-1 text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
In Orders
</div>
{balance.orders.toFixed()}
</div>
<div className="col-span-1 text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
Unsettled
</div>
{balance.unsettled.toFixed()}
</div>
<div className="col-span-1 text-left">
<div className="pb-0.5 text-th-fgd-3 text-xs">
Value
</div>
{formatUsdValue(balance.value)}
</div>
<div className="col-span-1 text-left text-th-fgd-4">
<div className="pb-0.5 text-th-fgd-3 text-xs">
Deposit/Borrow 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>
<Button
className="col-span-1 text-xs pt-0 pb-0 h-8 pl-3 pr-3"
onClick={() => handleOpenDepositModal(balance.symbol)}
>
Deposit
</Button>
<Button
className="col-span-1 text-xs pt-0 pb-0 h-8 pl-3 pr-3"
onClick={() =>
handleOpenWithdrawModal(balance.symbol)
}
>
Withdraw
</Button>
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`}
>
No balances
</div>
)}
</div>
</div>
</div>
)
}
export default BalancesTable