Make Redeem All and Redeem Positive have correct behavior where you don't have to sit and wait for each transaction to go through and confirm; still has bug with Redeem All where the spinner doesn't stop
This commit is contained in:
parent
b168abc26a
commit
e0098f3733
|
@ -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,
|
||||
|
|
|
@ -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 = [
|
||||
|
|
Loading…
Reference in New Issue