2021-04-07 14:49:37 -07:00
|
|
|
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'
|
2021-04-07 14:49:37 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-07 14:49:37 -07:00
|
|
|
|
|
|
|
return (
|
2021-04-12 09:49:02 -07:00
|
|
|
<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 ? (
|
2021-04-12 09:49:02 -07:00
|
|
|
<div className={`text-right`}>
|
2021-04-10 14:12:15 -07:00
|
|
|
<Button onClick={handleSettleAll}>Settle All</Button>
|
|
|
|
</div>
|
|
|
|
) : null}
|
2021-04-07 14:49:37 -07:00
|
|
|
{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-07 14:49:37 -07:00
|
|
|
>
|
2021-04-12 20:39:08 -07:00
|
|
|
<table className={`min-w-full divide-y divide-th-bkg-2`}>
|
2021-04-07 14:49:37 -07:00
|
|
|
<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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
Net
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{balances.map((balance, index) => (
|
|
|
|
<tr
|
|
|
|
key={`${index}`}
|
2021-04-12 09:49:02 -07:00
|
|
|
className={`
|
2021-04-12 20:39:08 -07:00
|
|
|
${index % 2 === 0 ? `bg-th-bkg-1` : `bg-th-bkg-3`}
|
2021-04-12 09:49:02 -07:00
|
|
|
`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
<td
|
2021-04-13 16:41:04 -07:00
|
|
|
className={`px-6 py-4 whitespace-nowrap text-sm text-th-fgd-2`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
{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`}
|
2021-04-07 14:49:37 -07:00
|
|
|
>
|
|
|
|
No balances
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BalancesTable
|