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