Skip to content

Commit

Permalink
fix: open consolidate perpetual
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Feb 7, 2024
1 parent bee1f36 commit ffb330b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
10 changes: 6 additions & 4 deletions x/perpetual/keeper/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 13 additions & 9 deletions x/perpetual/keeper/open_consolidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,48 @@ 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
}
default:
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
}
21 changes: 13 additions & 8 deletions x/perpetual/keeper/open_consolidate_long.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 13 additions & 8 deletions x/perpetual/keeper/open_consolidate_short.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ffb330b

Please sign in to comment.