@@ -18,6 +18,14 @@ func (k Keeper) ClosePosition(ctx sdk.Context, msg *types.MsgClose) (types.MTP,
1818 return types.MTP {}, math .ZeroInt (), math .LegacyZeroDec (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), false , false , zeroPerpFees , math .LegacyZeroDec (), sdk.Coin {}, math .ZeroInt (), math .ZeroInt (), err
1919 }
2020
21+ userClosingRatio := msg .Amount .ToLegacyDec ().Quo (mtp .Custody .ToLegacyDec ())
22+ if mtp .Position == types .Position_SHORT {
23+ userClosingRatio = msg .Amount .ToLegacyDec ().Quo (mtp .Liabilities .ToLegacyDec ())
24+ }
25+ if userClosingRatio .GT (math .LegacyOneDec ()) {
26+ userClosingRatio = math .LegacyOneDec ()
27+ }
28+
2129 initialCollateral := sdk .NewCoin (mtp .CollateralAsset , mtp .Collateral )
2230 initialCustody := mtp .Custody
2331 initialLiabilities := mtp .Liabilities
@@ -39,18 +47,13 @@ func (k Keeper) ClosePosition(ctx sdk.Context, msg *types.MsgClose) (types.MTP,
3947 return types.MTP {}, math .ZeroInt (), math .LegacyZeroDec (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), math .ZeroInt (), false , false , zeroPerpFees , math .LegacyZeroDec (), initialCollateral , initialCustody , initialLiabilities , err
4048 }
4149
42- if forceClosed {
50+ // user didn't want close position fully & it got only partially closed, we return as the value user wanted to close is different now
51+ if ! userClosingRatio .Equal (math .LegacyOneDec ()) && forceClosed && ! closingRatio .Equal (math .LegacyOneDec ()) {
4352 return mtp , repayAmt , closingRatio , returnAmt , fundingFeeAmt , fundingAmtDistributed , interestAmt , insuranceAmt , allInterestsPaid , forceClosed , perpetualFeesCoins , closingPrice , initialCollateral , initialCustody , initialLiabilities , nil
4453 }
4554
4655 // Should be reset after MTPTriggerChecksAndUpdates
47- closingRatio = msg .Amount .ToLegacyDec ().Quo (mtp .Custody .ToLegacyDec ())
48- if mtp .Position == types .Position_SHORT {
49- closingRatio = msg .Amount .ToLegacyDec ().Quo (mtp .Liabilities .ToLegacyDec ())
50- }
51- if closingRatio .GT (math .LegacyOneDec ()) {
52- closingRatio = math .LegacyOneDec ()
53- }
56+ closingRatio = userClosingRatio
5457
5558 // Estimate swap and repay
5659 repayAmt , returnAmt , perpFees , closingPrice , _ , err := k .EstimateAndRepay (ctx , & mtp , & pool , & ammPool , closingRatio , false )
0 commit comments