Merge pull request #132 from blockworks-foundation/reduce-only-patch

Fix reduceOnly checkbox functionality
This commit is contained in:
tjshipe 2022-01-11 20:32:24 -05:00 committed by GitHub
commit c7e4577d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 7 deletions

View File

@ -170,7 +170,7 @@ export default function AdvancedTradeForm({
}
}, [set, tradeType, side])
const { max, deposits, borrows, spotMax } = useMemo(() => {
const { max, deposits, borrows, spotMax, reduceMax } = useMemo(() => {
if (!mangoAccount) return { max: 0 }
const priceOrDefault = price
? I80F48.fromNumber(price)
@ -211,6 +211,16 @@ export default function AdvancedTradeForm({
priceOrDefault
)
let reduceMax
if (marketConfig.kind === 'perp') {
reduceMax =
Math.abs(
(market as PerpMarket)?.baseLotsToNumber(perpAccount?.basePosition)
) || 0
} else {
reduceMax = 0
}
if (maxQuote.toNumber() <= 0) return { max: 0 }
// multiply the maxQuote by a scaler value to account for
// srm fees or rounding issues in getMaxLeverageForMarket
@ -220,8 +230,8 @@ export default function AdvancedTradeForm({
: (maxQuote.toNumber() * maxScaler) /
mangoGroup.getPrice(marketIndex, mangoCache).toNumber()
return { max: scaledMax, deposits, borrows, spotMax }
}, [mangoAccount, mangoGroup, mangoCache, marketIndex, market, side, price])
return { max: scaledMax, deposits, borrows, spotMax, reduceMax }
}, [mangoAccount, mangoGroup, mangoCache, marketIndex, market, side, price, reduceOnly])
const onChangeSide = (side) => {
setPositionSizePercent('')
@ -402,19 +412,20 @@ export default function AdvancedTradeForm({
setIoc(checked)
}
const reduceOnChange = (checked) => {
handleSetPositionSize(positionSizePercent, spotMargin, checked)
setReduceOnly(checked)
}
const marginOnChange = (checked) => {
setSpotMargin(checked)
if (positionSizePercent) {
handleSetPositionSize(positionSizePercent, checked)
handleSetPositionSize(positionSizePercent, checked, reduceOnly)
}
}
const handleSetPositionSize = (percent, spotMargin) => {
const handleSetPositionSize = (percent, spotMargin, reduceOnly) => {
setPositionSizePercent(percent)
const baseSizeMax =
spotMargin || marketConfig.kind === 'perp' ? max : spotMax
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(
@ -840,7 +851,7 @@ export default function AdvancedTradeForm({
<div className="col-span-12 -mt-1">
<ButtonGroup
activeValue={positionSizePercent}
onChange={(p) => handleSetPositionSize(p, spotMargin)}
onChange={(p) => handleSetPositionSize(p, spotMargin, reduceOnly)}
unit="%"
values={
isMobile