2021-04-07 14:49:37 -07:00
import { useBalances } from '../hooks/useBalances'
2021-06-24 15:15:23 -07:00
import useMangoStore , { mangoClient } from '../stores/useMangoStore'
2021-04-10 14:12:15 -07:00
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-06-09 08:52:32 -07:00
import { InformationCircleIcon } from '@heroicons/react/outline'
import Tooltip from './Tooltip'
2021-08-29 12:15:37 -07:00
import { sleep } from '../utils'
2021-06-24 15:15:23 -07:00
import { Market } from '@project-serum/serum'
2021-08-29 12:15:37 -07:00
import { ZERO_I80F48 } from '@blockworks-foundation/mango-client'
2021-04-07 14:49:37 -07:00
const BalancesTable = ( ) = > {
const balances = useBalances ( )
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() {
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
2021-06-24 15:15:23 -07:00
const markets = useMangoStore . getState ( ) . selectedMangoGroup . markets
2021-04-10 14:12:15 -07:00
const wallet = useMangoStore . getState ( ) . wallet . current
try {
2021-06-24 15:15:23 -07:00
const spotMarkets = Object . values ( markets ) . filter (
( mkt ) = > mkt instanceof Market
) as Market [ ]
await mangoClient . settleAll ( mangoGroup , mangoAccount , spotMarkets , wallet )
2021-07-21 14:11:17 -07:00
notify ( { title : 'Successfully settled funds' } )
2021-04-22 13:20:57 -07:00
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 ( {
2021-07-06 15:04:20 -07:00
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 ( {
2021-07-06 15:04:20 -07:00
title : 'Error settling funds' ,
2021-04-11 21:17:23 -07:00
description : e.message ,
2021-05-21 15:00:39 -07:00
txid : e.txid ,
2021-04-11 21:17:23 -07:00
type : 'error' ,
} )
2021-04-10 14:12:15 -07:00
}
}
}
2021-04-07 14:49:37 -07:00
2021-08-15 06:31:59 -07:00
const filteredBalances = balances . filter (
( bal ) = >
2021-08-18 13:43:51 -07:00
+ bal . deposits > 0 ||
2021-08-15 06:31:59 -07:00
+ bal . borrows > 0 ||
bal . orders > 0 ||
bal . unsettled > 0
)
2021-04-07 14:49:37 -07:00
return (
User account page (#22)
* layout, overview, start on assets, borrows and open orders
* trade history, sortable data hook for tables, borrow page
* handle deposit and withdraw buttons
* borrow modal ui and integration + settle borrow for individual assets
* in orders balance to asset table and totals, responsive css, new connected wallet button + small tweaks
* account switch/creation flow
* accounts modal, update to usebalances hook
* handle settle, deposit before settle, save last account
* disable borrow/withdraw button when no account
2021-06-05 07:11:44 -07:00
< div className = { ` flex flex-col py-4 ` } >
2021-04-12 09:49:02 -07:00
< 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-06-09 08:52:32 -07:00
{ balances . length > 0 &&
( balances . find ( ( { unsettled } ) = > unsettled > 0 ) ||
balances . find (
2021-08-18 13:43:51 -07:00
( { borrows , deposits } ) = >
borrows . gt ( ZERO_I80F48 ) && deposits . gt ( ZERO_I80F48 )
2021-06-09 08:52:32 -07:00
) ) ? (
2021-04-20 07:19:08 -07:00
< div
2021-08-15 06:31:59 -07:00
className = { ` flex items-center justify-between px-6 py-3 mb-2 rounded-md bg-th-bkg-1 ` }
2021-04-20 07:19:08 -07:00
>
2021-06-09 08:52:32 -07:00
< div className = "flex items-center text-fgd-1 font-semibold pr-4" >
2021-04-21 05:45:03 -07:00
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 >
2021-06-09 08:52:32 -07:00
< / div >
2021-04-10 14:12:15 -07:00
< Button onClick = { handleSettleAll } > Settle All < / Button >
< / div >
) : null }
2021-08-15 06:31:59 -07:00
{ filteredBalances . length > 0 ? (
2021-04-07 14:49:37 -07:00
< 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 >
2021-07-29 06:19:32 -07:00
< Tr className = "text-th-fgd-3 text-xs" >
2021-04-20 07:19:08 -07:00
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-2 text-left font-normal ` }
2021-04-07 14:49:37 -07:00
>
2021-07-05 08:03:57 -07:00
Asset
2021-04-20 07:19:08 -07:00
< / Th >
< Th
2021-04-07 14:49:37 -07:00
scope = "col"
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-2 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-08-29 23:33:41 -07:00
className = { ` px-6 py-2 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-08-29 23:33:41 -07:00
className = { ` px-6 py-2 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-08-29 23:33:41 -07:00
className = { ` px-6 py-2 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-08-29 23:33:41 -07:00
className = { ` px-6 py-2 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-08-15 06:31:59 -07:00
{ filteredBalances . map ( ( balance , index ) = > {
2021-07-24 13:15:27 -07:00
return (
< Tr
key = { ` ${ index } ` }
className = { ` border-b border-th-bkg-3
2021-04-20 07:19:08 -07:00
$ { 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-07-24 13:15:27 -07:00
< Td
2021-08-29 23:33:41 -07:00
className = { ` flex items-center px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
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 }
< / Td >
< Td
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-07-24 13:15:27 -07:00
>
2021-08-29 12:15:37 -07:00
{ balance . deposits . toFixed ( ) }
2021-07-24 13:15:27 -07:00
< / Td >
< Td
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-07-24 13:15:27 -07:00
>
2021-08-29 12:15:37 -07:00
{ balance . borrows . toFixed ( ) }
2021-07-24 13:15:27 -07:00
< / Td >
< Td
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-07-24 13:15:27 -07:00
>
{ balance . orders }
< / Td >
< Td
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-07-24 13:15:27 -07:00
>
{ balance . unsettled }
< / Td >
< Td
2021-08-29 23:33:41 -07:00
className = { ` px-6 py-3.5 whitespace-nowrap text-sm text-th-fgd-1 ` }
2021-07-24 13:15:27 -07:00
>
2021-08-29 12:15:37 -07:00
{ balance . net . toFixed ( ) }
2021-07-24 13:15:27 -07:00
< / Td >
< / Tr >
)
} ) }
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