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:
dafyddd 2022-06-02 16:27:46 -04:00
parent b168abc26a
commit e0098f3733
2 changed files with 67 additions and 12 deletions

View File

@ -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,

View File

@ -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,23 +45,23 @@ 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)
}
reloadMangoAccount()
try {
await settleAllPnl(unsettledPositions.map((p) => p.perpMarket), t, undefined, wallet)
} finally {
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)
}
}
const buttons = [
{ onClick: handleSettleAll, disabled: false, text: t('redeem-all') },