handle when perpmarket isnt loaded yet

This commit is contained in:
Tyler Shipe 2022-01-12 16:46:41 -06:00
parent a58b49c507
commit e1114d7477
3 changed files with 18 additions and 6 deletions

View File

@ -44,7 +44,7 @@ function calculateFundingRate(perpStats, perpMarket) {
}
function parseOpenInterest(perpMarket: PerpMarket) {
if (!(perpMarket instanceof PerpMarket)) return 0
if (!perpMarket || !(perpMarket instanceof PerpMarket)) return 0
return perpMarket.baseLotsToNumber(perpMarket.openInterest) / 2
}

View File

@ -231,7 +231,16 @@ export default function AdvancedTradeForm({
mangoGroup.getPrice(marketIndex, mangoCache).toNumber()
return { max: scaledMax, deposits, borrows, spotMax, reduceMax }
}, [mangoAccount, mangoGroup, mangoCache, marketIndex, market, side, price, reduceOnly])
}, [
mangoAccount,
mangoGroup,
mangoCache,
marketIndex,
market,
side,
price,
reduceOnly,
])
const onChangeSide = (side) => {
setPositionSizePercent('')
@ -424,8 +433,11 @@ export default function AdvancedTradeForm({
const handleSetPositionSize = (percent, spotMargin, reduceOnly) => {
setPositionSizePercent(percent)
const baseSizeMax =
reduceOnly ? reduceMax : spotMargin || marketConfig.kind === 'perp' ? max : spotMax
const baseSizeMax = reduceOnly
? reduceMax
: spotMargin || marketConfig.kind === 'perp'
? max
: spotMax
const baseSize = baseSizeMax * (parseInt(percent) / 100)
const step = parseFloat(minOrderSize)
const roundedSize = (Math.floor(baseSize / step) * step).toFixed(
@ -504,7 +516,7 @@ export default function AdvancedTradeForm({
}
const estimatedSize =
perpAccount && reduceOnly
perpAccount && reduceOnly && market instanceof PerpMarket
? Math.abs(
(market as PerpMarket).baseLotsToNumber(perpAccount.basePosition)
)

View File

@ -55,7 +55,7 @@ export const collectPerpPosition = (
console.error(e)
}
const basePosition = perpMarket.baseLotsToNumber(perpAccount.basePosition)
const basePosition = perpMarket?.baseLotsToNumber(perpAccount.basePosition)
const indexPrice = mangoGroup.getPrice(marketIndex, mangoCache).toNumber()
const notionalSize = Math.abs(basePosition * indexPrice)
const unrealizedPnl = basePosition * (indexPrice - breakEvenPrice)