mango-ui-v3/components/BalancesTable.tsx

182 lines
6.3 KiB
TypeScript
Raw Normal View History

import { useBalances } from '../hooks/useBalances'
2021-04-10 14:12:15 -07:00
import useMangoStore from '../stores/useMangoStore'
2021-06-20 18:50:26 -07:00
// import { settleAll } from '../utils/mango'
2021-04-10 14:12:15 -07:00
import useConnection from '../hooks/useConnection'
import Button from '../components/Button'
2021-04-11 21:17:23 -07:00
import { notify } from '../utils/notifications'
import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table'
import { InformationCircleIcon } from '@heroicons/react/outline'
import Tooltip from './Tooltip'
import { sleep } from '../utils'
import { PublicKey } from '@solana/web3.js'
const BalancesTable = () => {
const balances = useBalances()
2021-04-10 14:12:15 -07:00
const { programId, connection } = useConnection()
2021-04-20 07:09:25 -07:00
const actions = useMangoStore((s) => s.actions)
2021-04-10 14:12:15 -07:00
async function handleSettleAll() {
const markets = Object.values(
useMangoStore.getState().selectedMangoGroup.markets
)
2021-06-23 08:32:33 -07:00
const mangoAccount = useMangoStore.getState().selectedMangoAccount.current
2021-04-10 14:12:15 -07:00
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
try {
2021-06-22 20:32:10 -07:00
// await settleAll(
// connection,
// new PublicKey(programId),
// mangoGroup,
2021-06-23 08:32:33 -07:00
// mangoAccount,
2021-06-22 20:32:10 -07:00
// markets,
// wallet
// )
await sleep(250)
2021-06-23 08:32:33 -07:00
actions.fetchMangoAccounts()
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({
message: 'There are no unsettled funds',
type: 'error',
})
2021-04-10 14:12:15 -07:00
} else {
2021-04-11 21:17:23 -07:00
notify({
message: 'Error settling funds',
description: e.message,
txid: e.txid,
2021-04-11 21:17:23 -07:00
type: 'error',
})
2021-04-10 14:12:15 -07:00
}
}
}
return (
<div className={`flex flex-col py-4`}>
<div className={`-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8`}>
<div className={`align-middle inline-block min-w-full sm:px-6 lg:px-8`}>
{balances.length > 0 &&
(balances.find(({ unsettled }) => unsettled > 0) ||
balances.find(
({ borrows, marginDeposits }) => borrows > 0 && marginDeposits > 0
)) ? (
<div
2021-06-12 10:46:06 -07:00
className={`flex items-center justify-between px-4 py-2 mb-2 rounded-md bg-th-bkg-1`}
>
<div className="flex items-center text-fgd-1 font-semibold pr-4">
You have unsettled funds
2021-04-22 08:43:20 -07:00
<Tooltip content="Use the Settle All button to move unsettled funds to your deposits. If you have borrows, settling will use deposits for that asset to reduce your borrows.">
<div>
<InformationCircleIcon
className={`h-5 w-5 ml-2 text-th-primary cursor-help`}
/>
</div>
</Tooltip>
</div>
2021-04-10 14:12:15 -07:00
<Button onClick={handleSettleAll}>Settle All</Button>
</div>
) : null}
{balances.length ? (
<div
2021-04-12 20:39:08 -07:00
className={`overflow-hidden border-b border-th-bkg-2 sm:rounded-md`}
>
<Table className={`min-w-full divide-y divide-th-bkg-2`}>
<Thead>
<Tr className="text-th-fgd-3">
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
Coin
</Th>
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
Deposits
</Th>
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
Borrows
</Th>
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
In Orders
</Th>
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
Unsettled
</Th>
<Th
scope="col"
2021-06-12 10:46:06 -07:00
className={`px-6 py-2 text-left font-normal`}
>
Net
</Th>
</Tr>
</Thead>
<Tbody>
{balances.map((balance, index) => (
<Tr
key={`${index}`}
className={`border-b border-th-bkg-3
${index % 2 === 0 ? `bg-th-bkg-3` : `bg-th-bkg-2`}
`}
>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.coin}
</Td>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.marginDeposits}
</Td>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.borrows}
</Td>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.orders}
</Td>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.unsettled}
</Td>
<Td
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1`}
>
{balance.net}
</Td>
</Tr>
))}
</Tbody>
</Table>
</div>
) : (
<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