import useMangoStore, { mangoClient } from '../stores/useMangoStore' import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table' import { getMarketByPublicKey, nativeI80F48ToUi, PerpAccount, PerpMarket, ZERO_I80F48, } from '@blockworks-foundation/mango-client' import { useMemo } from 'react' import Button from './Button' import { notify } from '../utils/notifications' import { QUOTE_INDEX } from '@blockworks-foundation/mango-client/lib/src/MangoGroup' import BN from 'bn.js' import SideBadge from './SideBadge' import { useState } from 'react' import Loading from './Loading' const PositionsTable = () => { const actions = useMangoStore((s) => s.actions) const groupConfig = useMangoStore((s) => s.selectedMangoGroup.config) const mangoGroup = useMangoStore((s) => s.selectedMangoGroup.current) const mangoAccount = useMangoStore((s) => s.selectedMangoAccount.current) const mangoCache = useMangoStore((s) => s.selectedMangoGroup.cache) const allMarkets = useMangoStore((s) => s.selectedMangoGroup.markets) const [settlingPerpAcc, setSettlingPerpAcc] = useState(null) const perpMarkets = useMemo( () => mangoGroup ? groupConfig.perpMarkets.map( (m) => mangoGroup.perpMarkets[m.marketIndex] ) : [], [mangoGroup] ) const perpAccounts = mangoAccount ? groupConfig.perpMarkets.map((m) => { return { perpAccount: mangoAccount.perpAccounts[m.marketIndex], marketIndex: m.marketIndex, } }) : [] const filteredPerpAccounts = perpAccounts.filter( ({ perpAccount }) => !( perpAccount.quotePosition.eq(ZERO_I80F48) && perpAccount.basePosition.eq(new BN(0)) ) ) console.log('perp acc length', filteredPerpAccounts.length) const handleSettlePnl = async ( perpMarket: PerpMarket, perpAccount: PerpAccount ) => { const mangoAccount = useMangoStore.getState().selectedMangoAccount.current const mangoGroup = useMangoStore.getState().selectedMangoGroup.current const wallet = useMangoStore.getState().wallet.current const marketIndex = mangoGroup.getPerpMarketIndex(perpMarket.publicKey) setSettlingPerpAcc(perpAccount) try { const txid = await mangoClient.settlePnl( mangoGroup, mangoAccount, perpMarket, mangoGroup.rootBankAccounts[QUOTE_INDEX], mangoCache.priceCache[marketIndex].price, wallet ) actions.fetchMangoAccounts() notify({ title: 'Successfully settled PNL', description: '', txid, }) } catch (e) { console.log('Error settling PNL: ', `${e}`, `${perpAccount}`) notify({ title: 'Error settling PNL', description: e.message, txid: e.txid, type: 'error', }) } finally { setSettlingPerpAcc(null) } } return (
Market | Side | Base Position | Quote Position | Unrealized PnL | Health | Edit |
---|---|---|---|---|---|---|
{marketConfig.name} |
|
{perpMarket.baseLotsToNumber( perpAccount.basePosition )} | {nativeI80F48ToUi( perpAccount.quotePosition, marketConfig.quoteDecimals ).toFixed()} | $ {nativeI80F48ToUi( perpAccount.getPnl(perpMarketInfo, price), marketConfig.quoteDecimals ).toFixed()} | {perpAccount .getHealth( perpMarketInfo, price, perpMarketInfo.maintAssetWeight, perpMarketInfo.maintLiabWeight, marketCache.longFunding, marketCache.shortFunding ) .toFixed(3)} |
|