mango-ui-v3/components/BalancesTable.tsx

157 lines
5.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'
import { settleAll } from '../utils/mango'
import useConnection from '../hooks/useConnection'
import Button from '../components/Button'
2021-04-11 21:17:23 -07:00
import { notify } from '../utils/notifications'
const BalancesTable = () => {
const balances = useBalances()
2021-04-10 14:12:15 -07:00
const { programId, connection } = useConnection()
async function handleSettleAll() {
const markets = Object.values(
useMangoStore.getState().selectedMangoGroup.markets
)
const marginAccount = useMangoStore.getState().selectedMarginAccount.current
const mangoGroup = useMangoStore.getState().selectedMangoGroup.current
const wallet = useMangoStore.getState().wallet.current
try {
await settleAll(
connection,
programId,
mangoGroup,
marginAccount,
markets,
wallet
)
} 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,
type: 'error',
})
2021-04-10 14:12:15 -07:00
}
}
}
return (
<div className={`flex flex-col py-6`}>
<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`}>
2021-04-10 14:12:15 -07:00
{balances.length ? (
<div className={`text-right`}>
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`}
>
2021-04-12 20:39:08 -07:00
<table className={`min-w-full divide-y divide-th-bkg-2`}>
<thead>
<tr>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
Coin
</th>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
Deposits
</th>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
Borrows
</th>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
In Orders
</th>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
Unsettled
</th>
<th
scope="col"
2021-04-12 21:40:26 -07:00
className={`px-6 py-3 text-left text-base font-medium text-th-fgd-2 tracking-wider`}
>
Net
</th>
</tr>
</thead>
<tbody>
{balances.map((balance, index) => (
<tr
key={`${index}`}
className={`
2021-04-12 20:39:08 -07:00
${index % 2 === 0 ? `bg-th-bkg-1` : `bg-th-bkg-3`}
`}
>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.coin}
</td>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.marginDeposits}
</td>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.borrows}
</td>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.orders}
</td>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.unsettled}
</td>
<td
2021-04-13 16:41:04 -07:00
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
>
{balance.net}
</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<div
2021-04-16 04:50:56 -07:00
className={`w-full text-center py-6 text-base bg-th-bkg-1 text-th-fgd-3 rounded-md`}
>
No balances
</div>
)}
</div>
</div>
</div>
)
}
export default BalancesTable