Merge pull request #125 from blockworks-foundation/add-unsettled-spotmax

add unsettled balance to spot trade max
This commit is contained in:
tlrsssss 2023-04-18 18:10:51 -04:00 committed by GitHub
commit ad6218ba87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 5 deletions

View File

@ -13,6 +13,7 @@ export const useSpotMarketMax = (
side: string,
useMargin: boolean
) => {
const spotBalances = mangoStore((s) => s.mangoAccount.spotBalances)
const max = useMemo(() => {
const group = mangoStore.getState().group
if (!mangoAccount || !group || !selectedMarket) return 100
@ -26,21 +27,27 @@ export const useSpotMarketMax = (
group,
selectedMarket.serumMarketExternal
)
spotMax = mangoAccount.getTokenBalanceUi(
group.getFirstBankByTokenIndex(selectedMarket.quoteTokenIndex)
const bank = group.getFirstBankByTokenIndex(
selectedMarket.quoteTokenIndex
)
const balance = mangoAccount.getTokenBalanceUi(bank)
const unsettled = spotBalances[bank.mint.toString()]?.unsettled || 0
spotMax = balance + unsettled
} else {
leverageMax = mangoAccount.getMaxBaseForSerum3AskUi(
group,
selectedMarket.serumMarketExternal
)
spotMax = mangoAccount.getTokenBalanceUi(
group.getFirstBankByTokenIndex(selectedMarket.baseTokenIndex)
const bank = group.getFirstBankByTokenIndex(
selectedMarket.baseTokenIndex
)
const balance = mangoAccount.getTokenBalanceUi(bank)
const unsettled = spotBalances[bank.mint.toString()]?.unsettled || 0
spotMax = balance + unsettled
}
return useMargin ? leverageMax : Math.max(spotMax, 0)
} catch (e) {
console.error('Error calculating max leverage: spot btn group: ', e)
console.error('Error calculating max size: ', e)
return 0
}
}, [side, selectedMarket, mangoAccount, useMargin])