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-20 07:19:08 -07:00
import { Table , Thead , Tbody , Tr , Th , Td } from 'react-super-responsive-table'
2021-04-20 15:10:10 -07:00
import useMarket from '../hooks/useMarket'
2021-04-21 05:45:03 -07:00
import { ElementTitle } from './styles'
import { InformationCircleIcon } from '@heroicons/react/outline'
import Tooltip from './Tooltip'
2021-04-22 13:20:57 -07:00
import { sleep } from '../utils'
2021-04-25 10:23:06 -07:00
import { PublicKey } from '@solana/web3.js'
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 ( )
2021-04-20 07:09:25 -07:00
const actions = useMangoStore ( ( s ) = > s . actions )
2021-04-20 15:10:10 -07:00
const { marketName } = useMarket ( )
2021-04-10 14:12:15 -07:00
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 ,
2021-04-25 10:23:06 -07:00
new PublicKey ( programId ) ,
2021-04-10 14:12:15 -07:00
mangoGroup ,
marginAccount ,
markets ,
wallet
)
2021-04-22 13:20:57 -07:00
await sleep ( 250 )
2021-04-20 07:09:25 -07:00
actions . fetchMarginAccounts ( )
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 ,
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-21 05:45:03 -07:00
< ElementTitle >
< div className = "pr-1" > { marketName . split ( '/' ) [ 0 ] } < / div >
< span className = "text-th-fgd-4" > / < / span >
< div className = "pl-1" > { marketName . split ( '/' ) [ 1 ] } < / div >
< / ElementTitle >
{ balances . length &&
( balances . find ( ( { unsettled } ) = > unsettled > 0 ) ||
balances . find (
( { borrows , marginDeposits } ) = > borrows > 0 && marginDeposits > 0
) ) ? (
2021-04-20 07:19:08 -07:00
< div
2021-04-20 15:10:10 -07:00
className = { ` flex items-center justify-between p-4 mb-2 rounded-md bg-th-bkg-1 ` }
2021-04-20 07:19:08 -07:00
>
2021-04-21 05:45:03 -07:00
< 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." >
2021-04-21 05:45:03 -07:00
< 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 }
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-20 07:19:08 -07:00
< Table className = { ` min-w-full divide-y divide-th-bkg-2 ` } >
< Thead >
< Tr className = "text-th-fgd-3" >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
Coin
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
Deposits
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
Borrows
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
In Orders
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
Unsettled
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-04-20 07:19:08 -07:00
className = { ` px-6 py-3 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
Net
2021-04-20 07:19:08 -07:00
< / Th >
< / Tr >
< / Thead >
< Tbody >
2021-04-07 14:49:37 -07:00
{ balances . map ( ( balance , index ) = > (
2021-04-20 07:19:08 -07:00
< Tr
2021-04-07 14:49:37 -07:00
key = { ` ${ index } ` }
2021-04-20 07:19:08 -07:00
className = { ` border-b border-th-bkg-3
$ { index % 2 === 0 ? ` bg-th-bkg-3 ` : ` bg-th-bkg-2 ` }
2021-04-12 09:49:02 -07:00
` }
2021-04-07 14:49:37 -07:00
>
2021-04-20 07:19:08 -07:00
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . coin }
2021-04-20 07:19:08 -07:00
< / Td >
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . marginDeposits }
2021-04-20 07:19:08 -07:00
< / Td >
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . borrows }
2021-04-20 07:19:08 -07:00
< / Td >
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . orders }
2021-04-20 07:19:08 -07:00
< / Td >
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . unsettled }
2021-04-20 07:19:08 -07:00
< / Td >
< Td
className = { ` px-6 py-4 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-04-07 14:49:37 -07:00
>
{ balance . net }
2021-04-20 07:19:08 -07:00
< / Td >
< / Tr >
2021-04-07 14:49:37 -07:00
) ) }
2021-04-20 07:19:08 -07:00
< / Tbody >
< / Table >
2021-04-07 14:49:37 -07:00
< / 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 ` }
2021-04-07 14:49:37 -07:00
>
No balances
< / div >
) }
< / div >
< / div >
< / div >
)
}
export default BalancesTable