improve perp quering
This commit is contained in:
parent
7610af5287
commit
074bf76f6d
|
@ -162,20 +162,6 @@ const HydrateStore = () => {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
newMangoAccount.perps
|
||||
.map((x) => x.basePositionLots.toNumber())
|
||||
.toString() !==
|
||||
mangoAccount.perps
|
||||
.map((x) => x.basePositionLots.toNumber())
|
||||
.toString()
|
||||
) {
|
||||
set((s) => {
|
||||
s.mangoAccount.current = newMangoAccount
|
||||
s.mangoAccount.lastSlot = context.slot
|
||||
})
|
||||
}
|
||||
actions.fetchOpenOrders()
|
||||
}
|
||||
},
|
||||
|
|
|
@ -20,7 +20,7 @@ const AccountTabs = () => {
|
|||
const { isMobile, isTablet } = useViewport()
|
||||
const unsettledSpotBalances = useUnsettledSpotBalances()
|
||||
const unsettledPerpPositions = useUnsettledPerpPositions()
|
||||
const openPerpPositions = useOpenPerpPositions()
|
||||
const { openPerpPositions } = useOpenPerpPositions()
|
||||
const openOrders = mangoStore((s) => s.mangoAccount.openOrders)
|
||||
|
||||
const tabsWithCount: [string, number][] = useMemo(() => {
|
||||
|
|
|
@ -80,6 +80,7 @@ import DepositWithdrawModal from '@components/modals/DepositWithdrawModal'
|
|||
import CreateAccountModal from '@components/modals/CreateAccountModal'
|
||||
import TradeformSubmitButton from './TradeformSubmitButton'
|
||||
import useIpAddress from 'hooks/useIpAddress'
|
||||
import useOpenPerpPositions from 'hooks/useOpenPerpPositions'
|
||||
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
|
@ -113,6 +114,7 @@ type FormErrors = Partial<Record<keyof TradeForm, string>>
|
|||
|
||||
const AdvancedTradeForm = () => {
|
||||
const { t } = useTranslation(['common', 'settings', 'swap', 'trade'])
|
||||
const { poolIsPerpReadyForRefresh } = useOpenPerpPositions()
|
||||
const { mangoAccount, mangoAccountAddress } = useMangoAccount()
|
||||
const { usedSerum3, totalSerum3, usedPerps, totalPerps } =
|
||||
useMangoAccountAccounts()
|
||||
|
@ -736,7 +738,7 @@ const AdvancedTradeForm = () => {
|
|||
|
||||
const orderPrice = calcOrderPrice(price, orderbook)
|
||||
|
||||
const { signature: tx } = await client.perpPlaceOrder(
|
||||
const { signature: tx, slot } = await client.perpPlaceOrder(
|
||||
group,
|
||||
mangoAccount,
|
||||
selectedMarket.perpMarketIndex,
|
||||
|
@ -750,7 +752,19 @@ const AdvancedTradeForm = () => {
|
|||
undefined,
|
||||
undefined,
|
||||
)
|
||||
actions.fetchOpenOrders(true)
|
||||
await poolIsPerpReadyForRefresh(
|
||||
() => {
|
||||
actions.reloadMangoAccount(slot)
|
||||
},
|
||||
() => {
|
||||
notify({
|
||||
type: 'error',
|
||||
title:
|
||||
'Timeout during perp refresh, please refresh data manually',
|
||||
})
|
||||
},
|
||||
)
|
||||
actions.reloadMangoAccount(slot)
|
||||
set((s) => {
|
||||
s.successAnimation.trade = true
|
||||
})
|
||||
|
|
|
@ -72,7 +72,7 @@ const CloseAllPositionsModal: FunctionComponent<ModalProps> = ({
|
|||
}) => {
|
||||
const { t } = useTranslation(['common', 'trade'])
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const openPerpPositions = useOpenPerpPositions()
|
||||
const { openPerpPositions } = useOpenPerpPositions()
|
||||
const { group } = useMangoGroup()
|
||||
|
||||
if (!group) return null
|
||||
|
|
|
@ -24,6 +24,7 @@ import { isMangoError } from 'types'
|
|||
import { decodeBook, decodeBookL2 } from 'utils/orderbook'
|
||||
import InlineNotification from '@components/shared/InlineNotification'
|
||||
import { getDecimalCount } from 'utils/numbers'
|
||||
import useOpenPerpPositions from 'hooks/useOpenPerpPositions'
|
||||
|
||||
interface MarketCloseModalProps {
|
||||
onClose: () => void
|
||||
|
@ -47,6 +48,7 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
|||
}) => {
|
||||
const { t } = useTranslation(['common', 'trade'])
|
||||
const [submitting, setSubmitting] = useState(false)
|
||||
const { poolIsPerpReadyForRefresh } = useOpenPerpPositions()
|
||||
const connection = mangoStore((s) => s.connection)
|
||||
const group = mangoStore((s) => s.group)
|
||||
const [soundSettings] = useLocalStorageState(
|
||||
|
@ -176,7 +178,7 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
|||
}),
|
||||
})
|
||||
|
||||
const { signature: tx } = await client.perpPlaceOrder(
|
||||
const { signature: tx, slot } = await client.perpPlaceOrder(
|
||||
group,
|
||||
mangoAccount,
|
||||
perpMarket.perpMarketIndex,
|
||||
|
@ -190,10 +192,17 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
|||
undefined,
|
||||
undefined,
|
||||
)
|
||||
actions.fetchOpenOrders(true)
|
||||
set((s) => {
|
||||
s.successAnimation.trade = true
|
||||
})
|
||||
await poolIsPerpReadyForRefresh(
|
||||
() => {
|
||||
actions.reloadMangoAccount(slot)
|
||||
},
|
||||
() => {
|
||||
notify({
|
||||
type: 'error',
|
||||
title: 'Timeout during perp refresh, please refresh data manually',
|
||||
})
|
||||
},
|
||||
)
|
||||
if (soundSettings['swap-success']) {
|
||||
successSound.play()
|
||||
}
|
||||
|
@ -202,6 +211,9 @@ const MarketCloseModal: FunctionComponent<MarketCloseModalProps> = ({
|
|||
title: 'Transaction successful',
|
||||
txid: tx,
|
||||
})
|
||||
set((s) => {
|
||||
s.successAnimation.trade = true
|
||||
})
|
||||
} catch (e) {
|
||||
if (isMangoError(e)) {
|
||||
notify({
|
||||
|
|
|
@ -41,7 +41,7 @@ const PerpPositions = () => {
|
|||
const [positionToShare, setPositionToShare] = useState<PerpPosition | null>(
|
||||
null,
|
||||
)
|
||||
const openPerpPositions = useOpenPerpPositions()
|
||||
const { openPerpPositions } = useOpenPerpPositions()
|
||||
const { selectedMarket } = useSelectedMarket()
|
||||
const { connected } = useWallet()
|
||||
const { mangoAccount } = useMangoAccount()
|
||||
|
|
|
@ -19,7 +19,7 @@ const TradeInfoTabs = () => {
|
|||
const selectedMarketName = mangoStore((s) => s.selectedMarket.current?.name)
|
||||
const unsettledSpotBalances = useUnsettledSpotBalances()
|
||||
const unsettledPerpPositions = useUnsettledPerpPositions()
|
||||
const openPerpPositions = useOpenPerpPositions()
|
||||
const { openPerpPositions } = useOpenPerpPositions()
|
||||
const { isMobile, isTablet, width } = useViewport()
|
||||
const fillTabWidth = width ? width < breakpoints['2xl'] : false
|
||||
|
||||
|
|
|
@ -39,7 +39,7 @@ const TradeSummary = ({
|
|||
const tradeForm = mangoStore((s) => s.tradeForm)
|
||||
const orderbook = mangoStore((s) => s.selectedMarket.orderbook)
|
||||
const { selectedMarket, quoteBank } = useSelectedMarket()
|
||||
const openPerpPositions = useOpenPerpPositions()
|
||||
const { openPerpPositions } = useOpenPerpPositions()
|
||||
|
||||
// calc new avg price if an open position exists
|
||||
const avgEntryPrice = useMemo(() => {
|
||||
|
|
|
@ -3,10 +3,58 @@ import { useMemo } from 'react'
|
|||
import useMangoAccount from './useMangoAccount'
|
||||
|
||||
const useOpenPerpPositions = () => {
|
||||
const { mangoAccountAddress } = useMangoAccount()
|
||||
const { mangoAccountAddress, mangoAccountPk } = useMangoAccount()
|
||||
const client = mangoStore((s) => s.client)
|
||||
const perpPositions = mangoStore((s) => s.mangoAccount.perpPositions)
|
||||
|
||||
const openPositions = useMemo(() => {
|
||||
const poolIsPerpReadyForRefresh = async (
|
||||
successCallback: () => void,
|
||||
timeoutCallback?: () => void,
|
||||
): Promise<'ready' | 'timeout'> => {
|
||||
const timeout = 15000
|
||||
let interval: NodeJS.Timeout
|
||||
let isTimeout = false
|
||||
|
||||
const checkPerps = async (): Promise<boolean> => {
|
||||
const newMangoAccount = await client.getMangoAccount(mangoAccountPk!)
|
||||
return newMangoAccount.perps.every((x) => x.takerBaseLots.isZero())
|
||||
}
|
||||
|
||||
const poll = async (
|
||||
resolve: (value: 'ready') => void,
|
||||
reject: (reason: 'timeout') => void,
|
||||
): Promise<void> => {
|
||||
if (await checkPerps()) {
|
||||
clearInterval(interval)
|
||||
resolve('ready')
|
||||
} else if (isTimeout) {
|
||||
clearInterval(interval)
|
||||
reject('timeout')
|
||||
}
|
||||
}
|
||||
|
||||
const pollPromise = new Promise<'ready' | 'timeout'>((resolve, reject) => {
|
||||
interval = setInterval(() => poll(resolve, reject), 700)
|
||||
setTimeout(() => {
|
||||
isTimeout = true
|
||||
}, timeout)
|
||||
})
|
||||
|
||||
try {
|
||||
const resp = await pollPromise
|
||||
successCallback()
|
||||
return resp
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
if (timeoutCallback) {
|
||||
timeoutCallback()
|
||||
}
|
||||
|
||||
return 'timeout'
|
||||
}
|
||||
}
|
||||
|
||||
const openPerpPositions = useMemo(() => {
|
||||
const group = mangoStore.getState().group
|
||||
if (!mangoAccountAddress || !group) return []
|
||||
return Object.values(perpPositions)
|
||||
|
@ -22,7 +70,7 @@ const useOpenPerpPositions = () => {
|
|||
})
|
||||
}, [mangoAccountAddress, perpPositions])
|
||||
|
||||
return openPositions
|
||||
return { openPerpPositions, poolIsPerpReadyForRefresh }
|
||||
}
|
||||
|
||||
export default useOpenPerpPositions
|
||||
|
|
Loading…
Reference in New Issue