mango-v4-ui/components/trade/MaxSizeButton.tsx

160 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-01-15 19:58:01 -08:00
import { PerpMarket, Serum3Market } from '@blockworks-foundation/mango-v4'
import MaxAmountButton from '@components/shared/MaxAmountButton'
import { FadeInFadeOut } from '@components/shared/Transitions'
import mangoStore from '@store/mangoStore'
import useMangoAccount from 'hooks/useMangoAccount'
import useSelectedMarket from 'hooks/useSelectedMarket'
import { useTranslation } from 'next-i18next'
import { useCallback, useMemo } from 'react'
2023-01-05 20:10:25 -08:00
import { floorToDecimal } from 'utils/numbers'
2023-01-15 19:58:01 -08:00
import { useSpotMarketMax } from './SpotSlider'
const MaxSizeButton = ({
minOrderDecimals,
tickDecimals,
2023-01-15 19:58:01 -08:00
useMargin,
}: {
minOrderDecimals: number
tickDecimals: number
2023-01-15 19:58:01 -08:00
useMargin: boolean
}) => {
const { t } = useTranslation(['common', 'trade'])
const { mangoAccount } = useMangoAccount()
const { selectedMarket, price: oraclePrice } = useSelectedMarket()
2023-01-04 17:09:43 -08:00
const { price, side, tradeType } = mangoStore((s) => s.tradeForm)
2023-01-15 19:58:01 -08:00
const spotMax = useSpotMarketMax(
mangoAccount,
selectedMarket,
side,
useMargin
)
2023-01-15 19:58:01 -08:00
const perpMax = useMemo(() => {
const group = mangoStore.getState().group
if (!mangoAccount || !group || !selectedMarket) return 0
2023-01-15 19:58:01 -08:00
if (selectedMarket instanceof PerpMarket) {
try {
2023-01-04 17:09:43 -08:00
if (side === 'buy') {
return mangoAccount.getMaxQuoteForPerpBidUi(
group,
selectedMarket.perpMarketIndex
)
} else {
return mangoAccount.getMaxBaseForPerpAskUi(
group,
selectedMarket.perpMarketIndex
)
}
2023-01-15 19:58:01 -08:00
} catch (e) {
console.error('Error calculating max leverage: spot btn group: ', e)
return 0
}
}
2023-01-04 17:09:43 -08:00
}, [mangoAccount, side, selectedMarket])
2023-01-15 19:58:01 -08:00
// const leverageMax = useMemo(() => {
// const group = mangoStore.getState().group
// if (!mangoAccount || !group || !selectedMarket) return 0
// try {
// if (selectedMarket instanceof Serum3Market) {
// if (side === 'buy') {
// return mangoAccount.getMaxQuoteForSerum3BidUi(
// group,
// selectedMarket.serumMarketExternal
// )
// } else {
// return mangoAccount.getMaxBaseForSerum3AskUi(
// group,
// selectedMarket.serumMarketExternal
// )
// }
// } else {
// if (side === 'buy') {
// return mangoAccount.getMaxQuoteForPerpBidUi(
// group,
// selectedMarket.perpMarketIndex
// )
// } else {
// return mangoAccount.getMaxBaseForPerpAskUi(
// group,
// selectedMarket.perpMarketIndex
// )
// }
// }
// } catch (e) {
// console.error('Error calculating max leverage: spot btn group: ', e)
// return 0
// }
// }, [mangoAccount, side, selectedMarket])
const handleMax = useCallback(() => {
2023-01-15 19:58:01 -08:00
const max = selectedMarket instanceof Serum3Market ? spotMax : perpMax || 0
const set = mangoStore.getState().set
set((state) => {
2023-01-04 17:09:43 -08:00
if (side === 'buy') {
2023-01-15 19:58:01 -08:00
state.tradeForm.quoteSize = floorToDecimal(max, tickDecimals).toFixed()
2023-01-04 17:09:43 -08:00
if (tradeType === 'Market' || !price) {
2023-01-05 20:10:25 -08:00
state.tradeForm.baseSize = floorToDecimal(
2023-01-15 19:58:01 -08:00
max / oraclePrice,
minOrderDecimals
2023-01-05 20:10:25 -08:00
).toFixed()
} else {
2023-01-05 20:10:25 -08:00
state.tradeForm.baseSize = floorToDecimal(
2023-01-15 19:58:01 -08:00
max / parseFloat(price),
minOrderDecimals
2023-01-05 20:10:25 -08:00
).toFixed()
}
} else {
2023-01-15 19:58:01 -08:00
state.tradeForm.baseSize = floorToDecimal(max, tickDecimals).toFixed()
2023-01-04 17:09:43 -08:00
if (tradeType === 'Market' || !price) {
2023-01-05 20:10:25 -08:00
state.tradeForm.quoteSize = floorToDecimal(
2023-01-15 19:58:01 -08:00
max * oraclePrice,
minOrderDecimals
2023-01-05 20:10:25 -08:00
).toFixed()
} else {
2023-01-05 20:10:25 -08:00
state.tradeForm.quoteSize = floorToDecimal(
2023-01-15 19:58:01 -08:00
max * parseFloat(price),
minOrderDecimals
2023-01-05 20:10:25 -08:00
).toFixed()
}
}
})
2023-01-15 19:58:01 -08:00
}, [perpMax, spotMax, price, side, tradeType, selectedMarket])
const maxAmount = useMemo(() => {
2023-01-15 19:58:01 -08:00
const max = selectedMarket instanceof Serum3Market ? spotMax : perpMax || 0
2023-01-04 17:09:43 -08:00
const tradePrice = tradeType === 'Market' ? oraclePrice : Number(price)
if (side === 'buy') {
2023-01-15 19:58:01 -08:00
return floorToDecimal(max / tradePrice, tickDecimals).toFixed()
} else {
2023-01-15 19:58:01 -08:00
return floorToDecimal(max, minOrderDecimals).toFixed()
}
2023-01-15 19:58:01 -08:00
}, [
perpMax,
spotMax,
selectedMarket,
minOrderDecimals,
tickDecimals,
price,
side,
tradeType,
])
return (
<div className="mb-2 mt-3 flex items-center justify-between">
<p className="text-xs text-th-fgd-3">{t('trade:size')}</p>
2023-01-04 17:09:43 -08:00
<FadeInFadeOut show={!!price}>
<MaxAmountButton
className="text-xs"
label={t('max')}
onClick={handleMax}
value={maxAmount}
/>
</FadeInFadeOut>
</div>
)
}
export default MaxSizeButton