Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adarsh/perp events #996

Merged
merged 10 commits into from
Nov 27, 2024
4 changes: 2 additions & 2 deletions x/leveragelp/keeper/begin_blocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (k Keeper) CheckAndLiquidateUnhealthyPosition(ctx sdk.Context, position *ty
ctx.Logger().Debug(errorsmod.Wrap(err, "error executing liquidation").Error())
return isHealthy, true, h, err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClose,
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventCloseUnhealthyPosition,
sdk.NewAttribute("id", strconv.FormatInt(int64(position.Id), 10)),
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("collateral", position.Collateral.String()),
Expand Down Expand Up @@ -133,7 +133,7 @@ func (k Keeper) CheckAndCloseAtStopLoss(ctx sdk.Context, position *types.Positio
ctx.Logger().Error(errorsmod.Wrap(err, "error executing close for stopLossPrice").Error())
return underStopLossPrice, true, err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClose,
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClosePositionStopLoss,
sdk.NewAttribute("id", strconv.FormatInt(int64(position.Id), 10)),
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("collateral", position.Collateral.String()),
Expand Down
4 changes: 2 additions & 2 deletions x/leveragelp/keeper/msg_server_close_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package keeper

import (
"context"
errorsmod "cosmossdk.io/errors"
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/leveragelp/types"
)
Expand Down Expand Up @@ -48,7 +49,6 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
return nil, err
}
}

}

// Handle stop loss
Expand Down
10 changes: 6 additions & 4 deletions x/leveragelp/types/events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package types

const (
EventOpen = "leveragelp/position_open"
EventClose = "leveragelp/position_close"
EventAddCollateral = "leveragelp/add_collateral"
EventClosePositions = "leveragelp/close_positions"
EventOpen = "leveragelp/position_open"
EventClose = "leveragelp/position_close"
EventAddCollateral = "leveragelp/add_collateral"
EventClosePositions = "leveragelp/close_positions"
EventCloseUnhealthyPosition = "leveragelp/close_unhealthy_position"
EventClosePositionStopLoss = "leveragelp/close_position_stop_loss"
)
4 changes: 2 additions & 2 deletions x/perpetual/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func (k Keeper) EmitFundPayment(ctx sdk.Context, mtp *types.MTP, takeAmount math
))
}

func (k Keeper) EmitForceClose(ctx sdk.Context, mtp *types.MTP, repayAmount math.Int, closer string) {
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventForceClose,
func (k Keeper) EmitForceClose(ctx sdk.Context, eventType string, mtp *types.MTP, repayAmount math.Int, closer string) {
ctx.EventManager().EmitEvent(sdk.NewEvent(eventType,
sdk.NewAttribute("id", strconv.FormatInt(int64(mtp.Id), 10)),
sdk.NewAttribute("position", mtp.Position.String()),
sdk.NewAttribute("address", mtp.Address),
Expand Down
16 changes: 0 additions & 16 deletions x/perpetual/keeper/msg_server_close_positions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package keeper
import (
"context"
"fmt"
"strconv"
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -41,11 +40,6 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
if err != nil {
// Add log about error or not liquidated
liqLog = append(liqLog, fmt.Sprintf("Position: Address:%s Id:%d cannot be liquidated due to err: %s", position.Address, position.Id, err.Error()))
} else {
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClosePositions,
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("id", strconv.FormatUint(position.Id, 10)),
))
}
}

Expand All @@ -67,11 +61,6 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
if err != nil {
// Add log about error or not closed
closeLog = append(closeLog, fmt.Sprintf("Position: Address:%s Id:%d cannot be liquidated due to err: %s", position.Address, position.Id, err.Error()))
} else {
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClosePositions,
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("id", strconv.FormatUint(position.Id, 10)),
))
}
}

Expand All @@ -93,11 +82,6 @@ func (k msgServer) ClosePositions(goCtx context.Context, msg *types.MsgClosePosi
if err != nil {
// Add log about error or not closed
takeProfitLog = append(takeProfitLog, fmt.Sprintf("Position: Address:%s Id:%d cannot be liquidated due to err: %s", position.Address, position.Id, err.Error()))
} else {
ctx.EventManager().EmitEvent(sdk.NewEvent(types.EventClosePositions,
sdk.NewAttribute("address", position.Address),
sdk.NewAttribute("id", strconv.FormatUint(position.Id, 10)),
))
}
}

Expand Down
6 changes: 3 additions & 3 deletions x/perpetual/keeper/process_mtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (k Keeper) CheckAndLiquidateUnhealthyPosition(ctx sdk.Context, mtp *types.M

if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
k.EmitForceClose(ctx, types.EventForceCloseUnhealthy, mtp, repayAmount, "")
} else {
return errors.Wrap(err, "error executing force close")
}
Expand Down Expand Up @@ -116,7 +116,7 @@ func (k Keeper) CheckAndCloseAtStopLoss(ctx sdk.Context, mtp *types.MTP, pool ty

if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
k.EmitForceClose(ctx, types.EventForceCloseStopLoss, mtp, repayAmount, "")
} else {
return errors.Wrap(err, "error executing force close")
}
Expand Down Expand Up @@ -160,7 +160,7 @@ func (k Keeper) CheckAndCloseAtTakeProfit(ctx sdk.Context, mtp *types.MTP, pool

if err == nil {
// Emit event if position was closed
k.EmitForceClose(ctx, mtp, repayAmount, "")
k.EmitForceClose(ctx, types.EventForceCloseTakeprofit, mtp, repayAmount, "")
} else {
return errors.Wrap(err, "error executing force close")
}
Expand Down
14 changes: 8 additions & 6 deletions x/perpetual/types/events.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

const (
EventOpen = "perpetual/mtp_open"
EventClose = "perpetual/mtp_close"
EventForceClose = "perpetual/mtp_force_close"
EventIncrementalPayFund = "perpetual/incremental_pay_fund"
EventRepayFund = "perpetual/repay_fund"
EventClosePositions = "perpetual/close_positions"
EventOpen = "perpetual/mtp_open"
EventClose = "perpetual/mtp_close"
EventForceCloseUnhealthy = "perpetual/mtp_force_close_unhealthy"
EventForceCloseStopLoss = "perpetual/mtp_force_close_stopLoss"
EventForceCloseTakeprofit = "perpetual/mtp_force_close_takeprofit"
EventIncrementalPayFund = "perpetual/incremental_pay_fund"
EventRepayFund = "perpetual/repay_fund"
EventClosePositions = "perpetual/close_positions"
)
12 changes: 8 additions & 4 deletions x/tradeshield/keeper/msg_server_execute_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
ammtypes "github.com/elys-network/elys/x/amm/types"
"github.com/elys-network/elys/x/tradeshield/types"
)

Expand All @@ -22,27 +23,30 @@
}

var err error
var res *ammtypes.MsgSwapByDenomResponse

// dispatch based on the order type
switch spotOrder.OrderType {
case types.SpotOrderType_STOPLOSS:
// execute the stop loss order
err = k.ExecuteStopLossOrder(ctx, spotOrder)
res, err = k.ExecuteStopLossOrder(ctx, spotOrder)

Check warning on line 32 in x/tradeshield/keeper/msg_server_execute_orders.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/keeper/msg_server_execute_orders.go#L32

Added line #L32 was not covered by tests
case types.SpotOrderType_LIMITSELL:
// execute the limit sell order
err = k.ExecuteLimitSellOrder(ctx, spotOrder)
res, err = k.ExecuteLimitSellOrder(ctx, spotOrder)

Check warning on line 35 in x/tradeshield/keeper/msg_server_execute_orders.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/keeper/msg_server_execute_orders.go#L35

Added line #L35 was not covered by tests
case types.SpotOrderType_LIMITBUY:
// execute the limit buy order
err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
res, err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
case types.SpotOrderType_MARKETBUY:
// execute the market buy order
err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
res, err = k.ExecuteMarketBuyOrder(ctx, spotOrder)

Check warning on line 41 in x/tradeshield/keeper/msg_server_execute_orders.go

View check run for this annotation

Codecov / codecov/patch

x/tradeshield/keeper/msg_server_execute_orders.go#L41

Added line #L41 was not covered by tests
}

// log the error if any
if err != nil {
// Add log about error or not executed
spotLog = append(spotLog, fmt.Sprintf("Spot order Id:%d cannot be executed due to err: %s", spotOrderId, err.Error()))
} else {
ctx.EventManager().EmitEvent(types.NewExecuteSpotOrderEvt(spotOrder, res))
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/tradeshield/keeper/msg_server_spot_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k msgServer) CreateSpotOrder(goCtx context.Context, msg *types.MsgCreateSp

// if the order is market buy, execute it immediately
if msg.OrderType == types.SpotOrderType_MARKETBUY {
err := k.ExecuteMarketBuyOrder(ctx, pendingSpotOrder)
_, err := k.ExecuteMarketBuyOrder(ctx, pendingSpotOrder)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion x/tradeshield/keeper/msg_server_spot_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerUpdateSpotOrder() {
},
},
{
"Incorrect Order Owner updating the order",
"Incorrect Order-Owner updating the order",
"incorrect owner",
func() *types.MsgUpdateSpotOrder {
_, _, _ = suite.SetPerpetualPool(1)
Expand Down
4 changes: 3 additions & 1 deletion x/tradeshield/keeper/pending_perpetual_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func (k Keeper) ExecuteLimitOpenOrder(ctx sdk.Context, order types.PerpetualOrde
return err
}

_, err = k.perpetual.Open(ctx, &perpetualtypes.MsgOpen{
res, err := k.perpetual.Open(ctx, &perpetualtypes.MsgOpen{
Creator: order.OwnerAddress,
Position: perpetualtypes.Position(order.Position),
Leverage: order.Leverage,
Expand All @@ -238,6 +238,8 @@ func (k Keeper) ExecuteLimitOpenOrder(ctx sdk.Context, order types.PerpetualOrde
// Remove the order from the pending order list
k.RemovePendingPerpetualOrder(ctx, order.OrderId)

ctx.EventManager().EmitEvent(types.NewExecuteLimitOpenPerpetualOrderEvt(order, res.Id))

return nil
}

Expand Down
Loading
Loading