From ffb330b900a6d40e4ee677e1e9f79a941865c726 Mon Sep 17 00:00:00 2001 From: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> Date: Wed, 7 Feb 2024 22:02:08 +0100 Subject: [PATCH] fix: open consolidate perpetual --- x/perpetual/keeper/open.go | 10 +++++---- x/perpetual/keeper/open_consolidate.go | 22 ++++++++++++-------- x/perpetual/keeper/open_consolidate_long.go | 21 ++++++++++++------- x/perpetual/keeper/open_consolidate_short.go | 21 ++++++++++++------- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/x/perpetual/keeper/open.go b/x/perpetual/keeper/open.go index ae2139cf5..4643b1be3 100644 --- a/x/perpetual/keeper/open.go +++ b/x/perpetual/keeper/open.go @@ -33,10 +33,8 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons return nil, err } - // Check if it is the same direction position for the same trader. - if mtp := k.OpenChecker.CheckSameAssetPosition(ctx, msg); mtp != nil { - return k.OpenConsolidate(ctx, mtp, msg, baseCurrency) - } + // check if existing mtp to consolidate + existingMtp := k.OpenChecker.CheckSameAssetPosition(ctx, msg) if err := k.OpenChecker.CheckMaxOpenPositions(ctx); err != nil { return nil, err @@ -68,6 +66,10 @@ func (k Keeper) Open(ctx sdk.Context, msg *types.MsgOpen) (*types.MsgOpenRespons return nil, errorsmod.Wrap(types.ErrInvalidPosition, msg.Position.String()) } + if existingMtp != nil { + return k.OpenConsolidate(ctx, existingMtp, mtp, msg, baseCurrency) + } + k.OpenChecker.EmitOpenEvent(ctx, mtp) if k.hooks != nil { diff --git a/x/perpetual/keeper/open_consolidate.go b/x/perpetual/keeper/open_consolidate.go index f47b88f35..d62e0ea22 100644 --- a/x/perpetual/keeper/open_consolidate.go +++ b/x/perpetual/keeper/open_consolidate.go @@ -6,30 +6,34 @@ import ( "github.com/elys-network/elys/x/perpetual/types" ) -func (k Keeper) OpenConsolidate(ctx sdk.Context, mtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MsgOpenResponse, error) { - poolId := mtp.AmmPoolId +func (k Keeper) OpenConsolidate(ctx sdk.Context, existingMtp *types.MTP, newMtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MsgOpenResponse, error) { + if !existingMtp.Leverage.Equal(msg.Leverage) { + return nil, types.ErrInvalidLeverage + } + + poolId := existingMtp.AmmPoolId pool, found := k.OpenLongChecker.GetPool(ctx, poolId) if !found { - return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, mtp.CustodyAsset) + return nil, errorsmod.Wrap(types.ErrPoolDoesNotExist, newMtp.CustodyAsset) } if !k.OpenLongChecker.IsPoolEnabled(ctx, poolId) { - return nil, errorsmod.Wrap(types.ErrMTPDisabled, mtp.CustodyAsset) + return nil, errorsmod.Wrap(types.ErrMTPDisabled, existingMtp.CustodyAsset) } - ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, mtp.CustodyAsset) + ammPool, err := k.OpenLongChecker.GetAmmPool(ctx, poolId, existingMtp.CustodyAsset) if err != nil { return nil, err } switch msg.Position { case types.Position_LONG: - mtp, err = k.OpenConsolidateLong(ctx, poolId, mtp, msg, baseCurrency) + existingMtp, err = k.OpenConsolidateLong(ctx, poolId, existingMtp, newMtp, msg, baseCurrency) if err != nil { return nil, err } case types.Position_SHORT: - mtp, err = k.OpenConsolidateShort(ctx, poolId, mtp, msg, baseCurrency) + existingMtp, err = k.OpenConsolidateShort(ctx, poolId, existingMtp, newMtp, msg, baseCurrency) if err != nil { return nil, err } @@ -37,13 +41,13 @@ func (k Keeper) OpenConsolidate(ctx sdk.Context, mtp *types.MTP, msg *types.MsgO return nil, errorsmod.Wrap(types.ErrInvalidPosition, msg.Position.String()) } - ctx.EventManager().EmitEvent(types.GenerateOpenEvent(mtp)) + ctx.EventManager().EmitEvent(types.GenerateOpenEvent(existingMtp)) if k.hooks != nil { k.hooks.AfterPerpetualPositionModified(ctx, ammPool, pool) } return &types.MsgOpenResponse{ - Id: mtp.Id, + Id: existingMtp.Id, }, nil } diff --git a/x/perpetual/keeper/open_consolidate_long.go b/x/perpetual/keeper/open_consolidate_long.go index aada10ee1..7c778aed0 100644 --- a/x/perpetual/keeper/open_consolidate_long.go +++ b/x/perpetual/keeper/open_consolidate_long.go @@ -5,15 +5,20 @@ import ( "github.com/elys-network/elys/x/perpetual/types" ) -func (k Keeper) OpenConsolidateLong(ctx sdk.Context, poolId uint64, mtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MTP, error) { - if !mtp.Leverage.Equal(msg.Leverage) { - return nil, types.ErrInvalidLeverage +func (k Keeper) OpenConsolidateLong(ctx sdk.Context, poolId uint64, existingMtp *types.MTP, newMtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MTP, error) { + existingMtp.Collateral = existingMtp.Collateral.Add(newMtp.Collateral) + existingMtp.Custody = existingMtp.Custody.Add(newMtp.Custody) + existingMtp.Liabilities = existingMtp.Liabilities.Add(newMtp.Liabilities) + + // Set existing MTP + if err := k.SetMTP(ctx, existingMtp); err != nil { + return nil, err } - maxLeverage := k.OpenLongChecker.GetMaxLeverageParam(ctx) - leverage := sdk.MinDec(mtp.Leverage, maxLeverage) - eta := leverage.Sub(sdk.OneDec()) - collateralAmountDec := sdk.NewDecFromBigInt(msg.Collateral.Amount.BigInt()) + // Destroy new MTP + if err := k.DestroyMTP(ctx, newMtp.Address, newMtp.Id); err != nil { + return nil, err + } - return k.ProcessOpenLong(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg, baseCurrency) + return existingMtp, nil } diff --git a/x/perpetual/keeper/open_consolidate_short.go b/x/perpetual/keeper/open_consolidate_short.go index 70d313f72..ce175e4e9 100644 --- a/x/perpetual/keeper/open_consolidate_short.go +++ b/x/perpetual/keeper/open_consolidate_short.go @@ -5,15 +5,20 @@ import ( "github.com/elys-network/elys/x/perpetual/types" ) -func (k Keeper) OpenConsolidateShort(ctx sdk.Context, poolId uint64, mtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MTP, error) { - if mtp.Leverage != msg.Leverage { - return nil, types.ErrInvalidLeverage +func (k Keeper) OpenConsolidateShort(ctx sdk.Context, poolId uint64, existingMtp *types.MTP, newMtp *types.MTP, msg *types.MsgOpen, baseCurrency string) (*types.MTP, error) { + existingMtp.Collateral = existingMtp.Collateral.Add(newMtp.Collateral) + existingMtp.Custody = existingMtp.Custody.Add(newMtp.Custody) + existingMtp.Liabilities = existingMtp.Liabilities.Add(newMtp.Liabilities) + + // Set existing MTP + if err := k.SetMTP(ctx, existingMtp); err != nil { + return nil, err } - maxLeverage := k.OpenLongChecker.GetMaxLeverageParam(ctx) - leverage := sdk.MinDec(mtp.Leverage, maxLeverage) - eta := leverage.Sub(sdk.OneDec()) - collateralAmountDec := sdk.NewDecFromBigInt(msg.Collateral.Amount.BigInt()) + // Destroy new MTP + if err := k.DestroyMTP(ctx, newMtp.Address, newMtp.Id); err != nil { + return nil, err + } - return k.ProcessOpenShort(ctx, mtp, leverage, eta, collateralAmountDec, poolId, msg, baseCurrency) + return existingMtp, nil }