import { useCallback, useState } from 'react' import useMangoStore, { mangoClient } from '../stores/useMangoStore' import { Table, Thead, Tbody, Tr, Th, Td } from 'react-super-responsive-table' import { getMarketByPublicKey, nativeI80F48ToUi, PerpAccount, PerpMarket, QUOTE_INDEX, ZERO_BN, } from '@blockworks-foundation/mango-client' import Button from './Button' import { notify } from '../utils/notifications' import SideBadge from './SideBadge' import Loading from './Loading' import { formatUsdValue, usdFormatter } from '../utils' import useTradeHistory from '../hooks/useTradeHistory' import Tooltip from './Tooltip' import { SettlePnlTooltip } from './MarketPosition' import usePerpPositions from '../hooks/usePerpPositions' import MarketCloseModal from './MarketCloseModal' export function getAvgEntryPrice( mangoAccount, perpAccount, perpMarket, perpTradeHistory ) { let avgEntryPrice = '--' if (perpTradeHistory.length) { try { avgEntryPrice = formatUsdValue( perpAccount.getAverageOpenPrice( mangoAccount, perpMarket, perpTradeHistory ) ) } catch { avgEntryPrice = '--' } } return avgEntryPrice } export function getBreakEvenPrice( mangoAccount, perpAccount, perpMarket, perpTradeHistory ) { let breakEvenPrice = '--' if (perpTradeHistory.length) { try { breakEvenPrice = formatUsdValue( perpAccount.getBreakEvenPrice( mangoAccount, perpMarket, perpTradeHistory ) ) } catch { breakEvenPrice = '--' } } return breakEvenPrice } 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 [showMarketCloseModal, setShowMarketCloseModal] = useState(false) const tradeHistory = useTradeHistory() const setMangoStore = useMangoStore((s) => s.set) const perpPositions = usePerpPositions() const handleCloseWarning = useCallback(() => { setShowMarketCloseModal(false) }, []) 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, mangoCache, 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) } } const handleSizeClick = (size) => { setMangoStore((state) => { state.tradeForm.baseSize = size }) } return (
Market | Side | Position size | Notional size | Avg entry price | Break-even price |
|
Edit |
---|---|---|---|---|---|---|---|
{marketConfig.name}
|
{!perpAccount.basePosition.eq(ZERO_BN) ? (
|
{perpAccount && Math.abs( perpMarket.baseLotsToNumber( perpAccount.basePosition ) ) > 0 ? ( handleSizeClick( Math.abs( perpMarket.baseLotsToNumber( perpAccount.basePosition ) ) ) } > {`${Math.abs( perpMarket.baseLotsToNumber( perpAccount.basePosition ) )} ${marketConfig.baseSymbol}`} ) : ( `0 ${marketConfig.baseSymbol}` )} | {usdFormatter( Math.abs( perpMarket.baseLotsToNumber( perpAccount.basePosition ) * mangoGroup .getPrice(marketIndex, mangoCache) .toNumber() ) )} | {getAvgEntryPrice( mangoAccount, perpAccount, perpMarket, perpTradeHistory )} | {getBreakEvenPrice( mangoAccount, perpAccount, perpMarket, perpTradeHistory )} | {usdFormatter( +nativeI80F48ToUi( perpAccount.getPnl( perpMarketInfo, perpMarketCache, price ), marketConfig.quoteDecimals ), marketConfig.quoteDecimals )} |
{Math.abs(
perpMarket.baseLotsToNumber(
perpAccount.basePosition
)
) > 0 ? (
) : null}
|
{showMarketCloseModal ? (