Skip to content

Commit 2fbbb1f

Browse files
committed
updating logic for add collateral and close position
1 parent b955384 commit 2fbbb1f

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

x/perpetual/keeper/close_position.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

x/perpetual/keeper/msg_server_add_collateral.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package keeper
22

33
import (
44
"context"
5+
"cosmossdk.io/math"
56
"errors"
67
sdk "github.com/cosmos/cosmos-sdk/types"
78
"github.com/elys-network/elys/v7/x/perpetual/types"
@@ -52,6 +53,9 @@ func (k msgServer) AddCollateral(goCtx context.Context, msg *types.MsgAddCollate
5253
}
5354
k.EmitForceClose(ctx, "add_collateral", mtp, repayAmt, returnAmt, fundingFeeAmt, fundingAmtDistributed, interestAmt, insuranceAmt, msg.Creator, allInterestsPaid, tradingAssetPrice, totalPerpetualFeesCoins, closingPrice, initialCollateralCoin, initialCustody, initialLiabilities, usdcPrice, closingRatio)
5455
// hooks are being called inside MTPTriggerChecksAndUpdates
56+
}
57+
58+
if forceClosed && closingRatio.Equal(math.LegacyOneDec()) {
5559
return &types.MsgAddCollateralResponse{}, nil
5660
}
5761

0 commit comments

Comments
 (0)