2022-11-15 17:32:55 -08:00
|
|
|
import ButtonGroup from '@components/forms/ButtonGroup'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
2022-12-26 03:58:17 -08:00
|
|
|
import useMangoAccount from 'hooks/useMangoAccount'
|
|
|
|
import useSelectedMarket from 'hooks/useSelectedMarket'
|
2023-12-01 20:11:00 -08:00
|
|
|
import { useCallback, useState } from 'react'
|
2023-01-24 19:11:42 -08:00
|
|
|
import { floorToDecimal } from 'utils/numbers'
|
2023-12-01 20:11:00 -08:00
|
|
|
import { usePerpMarketMax } from './PerpSlider'
|
2022-11-15 17:32:55 -08:00
|
|
|
|
2022-12-21 20:19:00 -08:00
|
|
|
const PerpButtonGroup = ({
|
|
|
|
minOrderDecimals,
|
|
|
|
tickDecimals,
|
|
|
|
}: {
|
|
|
|
minOrderDecimals: number
|
|
|
|
tickDecimals: number
|
|
|
|
}) => {
|
2022-12-26 03:58:17 -08:00
|
|
|
const side = mangoStore((s) => s.tradeForm.side)
|
|
|
|
const { selectedMarket } = useSelectedMarket()
|
|
|
|
const { mangoAccount } = useMangoAccount()
|
2022-11-15 17:32:55 -08:00
|
|
|
const [sizePercentage, setSizePercentage] = useState('')
|
2023-12-01 20:11:00 -08:00
|
|
|
const perpMax = usePerpMarketMax(mangoAccount, selectedMarket, side)
|
2022-11-15 17:32:55 -08:00
|
|
|
|
|
|
|
const handleSizePercentage = useCallback(
|
|
|
|
(percentage: string) => {
|
|
|
|
const set = mangoStore.getState().set
|
|
|
|
setSizePercentage(percentage)
|
2023-12-01 20:11:00 -08:00
|
|
|
const size = perpMax * (Number(percentage) / 100)
|
2022-11-15 17:32:55 -08:00
|
|
|
|
|
|
|
set((s) => {
|
|
|
|
if (s.tradeForm.side === 'buy') {
|
2023-01-24 19:11:42 -08:00
|
|
|
s.tradeForm.quoteSize = floorToDecimal(size, tickDecimals).toString()
|
2022-11-15 17:32:55 -08:00
|
|
|
|
|
|
|
if (Number(s.tradeForm.price)) {
|
2023-01-24 19:11:42 -08:00
|
|
|
s.tradeForm.baseSize = floorToDecimal(
|
2022-12-21 20:19:00 -08:00
|
|
|
size / Number(s.tradeForm.price),
|
2023-07-21 11:47:53 -07:00
|
|
|
minOrderDecimals,
|
2023-01-24 19:11:42 -08:00
|
|
|
).toString()
|
2022-11-15 17:32:55 -08:00
|
|
|
} else {
|
|
|
|
s.tradeForm.baseSize = ''
|
|
|
|
}
|
|
|
|
} else if (s.tradeForm.side === 'sell') {
|
2023-01-24 19:11:42 -08:00
|
|
|
s.tradeForm.baseSize = floorToDecimal(
|
|
|
|
size,
|
2023-07-21 11:47:53 -07:00
|
|
|
minOrderDecimals,
|
2023-01-24 19:11:42 -08:00
|
|
|
).toString()
|
2022-11-15 17:32:55 -08:00
|
|
|
|
|
|
|
if (Number(s.tradeForm.price)) {
|
2023-01-24 19:11:42 -08:00
|
|
|
s.tradeForm.quoteSize = floorToDecimal(
|
2022-12-21 20:19:00 -08:00
|
|
|
size * Number(s.tradeForm.price),
|
2023-07-21 11:47:53 -07:00
|
|
|
tickDecimals,
|
2023-01-24 19:11:42 -08:00
|
|
|
).toString()
|
2022-11-15 17:32:55 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2023-12-01 20:11:00 -08:00
|
|
|
[perpMax, minOrderDecimals, tickDecimals],
|
2022-11-15 17:32:55 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
2023-01-02 17:35:54 -08:00
|
|
|
<div className="w-full px-3 md:px-4">
|
2022-11-15 17:32:55 -08:00
|
|
|
<ButtonGroup
|
|
|
|
activeValue={sizePercentage}
|
|
|
|
onChange={(p) => handleSizePercentage(p)}
|
|
|
|
values={['25', '50', '75', '100']}
|
|
|
|
unit="%"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PerpButtonGroup
|