From 7434315ce1a08794c45535ada6cb667e8ab8a549 Mon Sep 17 00:00:00 2001 From: Abhinav Kumar <57705190+avkr003@users.noreply.github.com> Date: Tue, 5 Nov 2024 01:36:04 +0530 Subject: [PATCH] Avkr003/short fixes (#909) * fixing short stop loss price and long take profit price * remove redundant check --- x/perpetual/keeper/open.go | 12 +++++++++--- x/perpetual/types/keys.go | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/x/perpetual/keeper/open.go b/x/perpetual/keeper/open.go index dfc9af3ab..e13a75201 100644 --- a/x/perpetual/keeper/open.go +++ b/x/perpetual/keeper/open.go @@ -45,11 +45,15 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen, isBroker bool) (*types if !msg.StopLossPrice.IsZero() && msg.StopLossPrice.GTE(tradingAssetPrice) { return nil, fmt.Errorf("stop loss price cannot be greater than equal to tradingAssetPrice for long (Stop loss: %s, asset price: %s)", msg.StopLossPrice.String(), tradingAssetPrice.String()) } + // no need to override msg.TakeProfitPrice as the above ratio check it } if msg.Position == types.Position_SHORT { if ratio.GT(params.MaximumShortTakeProfitPriceRatio) { return nil, fmt.Errorf("take profit price should be less than %s times of current market price for short (current ratio: %s)", params.MaximumShortTakeProfitPriceRatio.String(), ratio.String()) } + if msg.StopLossPrice.IsZero() { + msg.StopLossPrice = types.MaxShortStopLossPrice + } if !msg.StopLossPrice.IsZero() && msg.StopLossPrice.LTE(tradingAssetPrice) { return nil, fmt.Errorf("stop loss price cannot be less than equal to tradingAssetPrice for short (Stop loss: %s, asset price: %s)", msg.StopLossPrice.String(), tradingAssetPrice.String()) } @@ -63,14 +67,16 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen, isBroker bool) (*types existingMtp := k.CheckSameAssetPosition(ctx, msg) if existingMtp == nil { - if msg.Leverage.Equal(math.LegacyOneDec()) { - return nil, fmt.Errorf("cannot open new position with leverage 1") + // opening new position + if msg.Leverage.LTE(math.LegacyOneDec()) { + return nil, fmt.Errorf("cannot open new position with leverage <= 1") } // Check if max positions are exceeded as we are opening new position, not updating old position if err = k.CheckMaxOpenPositions(ctx); err != nil { return nil, err } - } else if msg.Leverage.Equal(math.LegacyOneDec()) { + } else if msg.Leverage.Equal(math.LegacyZeroDec()) { + // adding collateral to existing position (when leverage > 1, we leave the case for modifying old position) // Enforce collateral addition (for leverage 1) without modifying take profit price msg.TakeProfitPrice = existingMtp.TakeProfitPrice } diff --git a/x/perpetual/types/keys.go b/x/perpetual/types/keys.go index 47114bd3f..a3252a658 100644 --- a/x/perpetual/types/keys.go +++ b/x/perpetual/types/keys.go @@ -36,6 +36,7 @@ const ( var ( TakeProfitPriceDefault = sdk.MustNewDecFromStr("10000000000000000000000000000000000000000") // 10^40 StopLossPriceDefault = sdk.ZeroDec() + MaxShortStopLossPrice = sdk.MustNewDecFromStr("10000000000000000000000000000000000000000") // 10^40 ) var (