diff --git a/components/MarketPosition.tsx b/components/MarketPosition.tsx index 7c028935..d33578ec 100644 --- a/components/MarketPosition.tsx +++ b/components/MarketPosition.tsx @@ -25,7 +25,6 @@ import { useRouter } from 'next/router' export const settlePosPnl = async ( perpMarkets: PerpMarket[], - perpAccount: PerpAccount, t, mangoAccounts: MangoAccount[] | undefined, wallet: Wallet @@ -71,7 +70,7 @@ export const settlePosPnl = async ( }) } } catch (e) { - console.log('Error settling PNL: ', `${e}`, `${perpAccount}`) + console.log('Error settling PNL: ', `${e}`) notify({ title: t('pnl-error'), description: e.message, @@ -81,6 +80,63 @@ export const settlePosPnl = async ( } } +export async function settleAllPnl( + perpMarkets: PerpMarket[], + t, + mangoAccounts: MangoAccount[] | undefined, + wallet: Wallet +) { + const mangoAccount = useMangoStore.getState().selectedMangoAccount.current + const mangoGroup = useMangoStore.getState().selectedMangoGroup.current + const mangoCache = useMangoStore.getState().selectedMangoGroup.cache + const actions = useMangoStore.getState().actions + const mangoClient = useMangoStore.getState().connection.client + + const rootBankAccount = mangoGroup?.rootBankAccounts[QUOTE_INDEX] + + if (!mangoGroup || !mangoCache || !mangoAccount || !rootBankAccount) return + + try { + const txids = await mangoClient.settleAllPerpPnl( + mangoGroup, + mangoCache, + mangoAccount, + perpMarkets, + rootBankAccount, + wallet?.adapter, + mangoAccounts + ) + actions.reloadMangoAccount() + const filteredTxids = txids?.filter( + (x) => x !== null + ) as string[] + if (filteredTxids) { + for (const txid of filteredTxids) { + notify({ + title: t('pnl-success'), + description: '', + txid, + }) + } + } else { + notify({ + title: t('pnl-error'), + description: t('transaction-failed'), + type: 'error', + }) + } + } catch (e) { + console.log('Error settling PNL: ', `${e}`) + notify({ + title: t('pnl-error'), + description: e.message, + txid: e.txid, + type: 'error', + }) + } + +} + export const settlePnl = async ( perpMarket: PerpMarket, perpAccount: PerpAccount, diff --git a/components/PerpPositions/RedeemDropdown.tsx b/components/PerpPositions/RedeemDropdown.tsx index 702d037f..4a6326f7 100644 --- a/components/PerpPositions/RedeemDropdown.tsx +++ b/components/PerpPositions/RedeemDropdown.tsx @@ -1,6 +1,6 @@ import { ChevronDownIcon } from '@heroicons/react/solid' import React, { Fragment, useState } from 'react' -import { settlePnl, settlePosPnl } from 'components/MarketPosition' +import {settleAllPnl, settlePosPnl} from 'components/MarketPosition' import Button from 'components/Button' import { Transition } from '@headlessui/react' import { useTranslation } from 'next-i18next' @@ -27,7 +27,6 @@ const MenuButton: React.FC<{ export const RedeemDropdown: React.FC = () => { const { t } = useTranslation('common') - const { reloadMangoAccount } = useMangoStore((s) => s.actions) const [settling, setSettling] = useState(false) const { wallet } = useWallet() const [settlingPosPnl, setSettlingPosPnl] = useState(false) @@ -46,22 +45,22 @@ export const RedeemDropdown: React.FC = () => { if (!wallet) return setOpen(false) setSettling(true) - for (const p of unsettledPositions) { - await settlePnl(p.perpMarket, p.perpAccount, t, undefined, wallet) + try { + await settleAllPnl(unsettledPositions.map((p) => p.perpMarket), t, undefined, wallet) + } finally { + setSettling(false) } - - reloadMangoAccount() - setSettling(false) } const handleSettlePosPnl = async () => { if (!wallet) return setOpen(false) setSettlingPosPnl(true) - for (const p of unsettledPositivePositions) { - await settlePosPnl([p.perpMarket], p.perpAccount, t, undefined, wallet) + try { + await settlePosPnl(unsettledPositivePositions.map((p) => p.perpMarket), t, undefined, wallet) + } finally { + setSettlingPosPnl(false) } - setSettlingPosPnl(false) } const buttons = [