Skip to content

Commit

Permalink
Refactor order execution events and update event types in Tradeshield…
Browse files Browse the repository at this point in the history
… module

- Updated event types in events.go to include specific events for limit buy, limit sell, stop loss, and market buy spot orders.
- Modified the TestMsgServerExecuteOrder in msg_server_execute_order_test.go to validate the correct event type for limit buy orders.
- Adjusted the ExecuteOrders function in msg_server_execute_orders.go to remove unused response variables and ensure proper event emissions.
- Enhanced the CreateSpotOrder function in msg_server_spot_order.go to emit the correct event for market buy orders.
- Added event emissions in the Execute*Order functions in pending_spot_order.go to ensure accurate event logging.

These changes improve event handling and enhance the clarity of order execution in the Tradeshield module.
  • Loading branch information
cosmic-vagabond committed Dec 11, 2024
1 parent 51e532f commit 0399c52
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 14 deletions.
4 changes: 2 additions & 2 deletions x/tradeshield/keeper/msg_server_execute_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
// Find the specific event we're looking for
var foundEvent sdk.Event
for _, event := range events {
if event.Type == types.TypeEvtExecuteSpotOrder {
if event.Type == types.TypeEvtExecuteLimitBuySpotOrder {
foundEvent = event
break
}
Expand All @@ -111,7 +111,7 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
suite.Require().NotNil(foundEvent)

// Check event attributes
suite.Require().Equal(types.TypeEvtExecuteSpotOrder, foundEvent.Type)
suite.Require().Equal(types.TypeEvtExecuteLimitBuySpotOrder, foundEvent.Type)

// Check specific attributes
for _, attr := range foundEvent.Attributes {
Expand Down
12 changes: 4 additions & 8 deletions x/tradeshield/keeper/msg_server_execute_orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"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 @@ -23,30 +22,27 @@ func (k msgServer) ExecuteOrders(goCtx context.Context, msg *types.MsgExecuteOrd
}

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
res, err = k.ExecuteStopLossOrder(ctx, spotOrder)
_, err = k.ExecuteStopLossOrder(ctx, spotOrder)
case types.SpotOrderType_LIMITSELL:
// execute the limit sell order
res, err = k.ExecuteLimitSellOrder(ctx, spotOrder)
_, err = k.ExecuteLimitSellOrder(ctx, spotOrder)
case types.SpotOrderType_LIMITBUY:
// execute the limit buy order
res, err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
_, err = k.ExecuteLimitBuyOrder(ctx, spotOrder)
case types.SpotOrderType_MARKETBUY:
// execute the market buy order
res, err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
_, err = k.ExecuteMarketBuyOrder(ctx, spotOrder)
}

// 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 @@ -30,7 +30,7 @@ func (k msgServer) CreateSpotOrder(goCtx context.Context, msg *types.MsgCreateSp
return nil, err
}

ctx.EventManager().EmitEvent(types.NewExecuteSpotOrderEvt(pendingSpotOrder, res))
ctx.EventManager().EmitEvent(types.NewExecuteMarketBuySpotOrderEvt(pendingSpotOrder, res))

return &types.MsgCreateSpotOrderResponse{
OrderId: pendingSpotOrder.OrderId,
Expand Down
12 changes: 12 additions & 0 deletions x/tradeshield/keeper/pending_spot_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ func (k Keeper) ExecuteStopLossOrder(ctx sdk.Context, order types.SpotOrder) (*a
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteStopLossSpotOrderEvt(order, res))

return res, nil
}

Expand Down Expand Up @@ -263,6 +266,9 @@ func (k Keeper) ExecuteLimitSellOrder(ctx sdk.Context, order types.SpotOrder) (*
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteLimitSellSpotOrderEvt(order, res))

return res, nil
}

Expand Down Expand Up @@ -305,6 +311,9 @@ func (k Keeper) ExecuteLimitBuyOrder(ctx sdk.Context, order types.SpotOrder) (*a
// Remove the order from the pending order list
k.RemovePendingSpotOrder(ctx, order.OrderId)

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteLimitBuySpotOrderEvt(order, res))

return res, nil
}

Expand All @@ -324,5 +333,8 @@ func (k Keeper) ExecuteMarketBuyOrder(ctx sdk.Context, order types.SpotOrder) (*
return res, err
}

// emit the event
ctx.EventManager().EmitEvent(types.NewExecuteMarketBuySpotOrderEvt(order, res))

return res, nil
}
78 changes: 75 additions & 3 deletions x/tradeshield/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ const (
TypeEvtCancelPerpetualOrder = "tradeshield/cancel_perpetual_order"
TypeEvtExecuteOrders = "tradeshield/execute_orders"
TypeEvtExecuteLimitOpenPerpetualOrder = "tradeshield/execute_perpetual_limit_open_order"
TypeEvtExecuteSpotOrder = "tradeshield/execute_spot_order"
TypeEvtExecuteLimitBuySpotOrder = "tradeshield/execute_limit_buy_spot_order"
TypeEvtExecuteLimitSellSpotOrder = "tradeshield/execute_limit_sell_spot_order"
TypeEvtExecuteStopLossSpotOrder = "tradeshield/execute_stop_loss_spot_order"
TypeEvtExecuteMarketBuySpotOrder = "tradeshield/execute_market_buy_spot_order"
)

func EmitCloseSpotOrderEvent(ctx sdk.Context, order SpotOrder) {
Expand Down Expand Up @@ -58,14 +61,83 @@ func NewCancelPerpetualOrderEvt(order PerpetualOrder) sdk.Event {
)
}

func NewExecuteSpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomResponse) sdk.Event {
func NewExecuteLimitBuySpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomResponse) sdk.Event {
// convert order price to json string
orderPrice, err := json.Marshal(order.OrderPrice)
if err != nil {
panic(err)
}

return sdk.NewEvent(TypeEvtExecuteSpotOrder,
return sdk.NewEvent(TypeEvtExecuteLimitBuySpotOrder,
sdk.NewAttribute("order_type", order.OrderType.String()),
sdk.NewAttribute("order_id", strconv.FormatInt(int64(order.OrderId), 10)),
sdk.NewAttribute("order_price", string(orderPrice)),
sdk.NewAttribute("order_amount", order.OrderAmount.String()),
sdk.NewAttribute("owner_address", order.OwnerAddress),
sdk.NewAttribute("order_target_denom", order.OrderTargetDenom),
sdk.NewAttribute("date", order.Date.String()),
sdk.NewAttribute("amount", res.Amount.String()),
sdk.NewAttribute("spot_price", res.SpotPrice.String()),
sdk.NewAttribute("swap_fee", res.SwapFee.String()),
sdk.NewAttribute("discount", res.Discount.String()),
sdk.NewAttribute("recipient", res.Recipient),
)
}

func NewExecuteLimitSellSpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomResponse) sdk.Event {
// convert order price to json string
orderPrice, err := json.Marshal(order.OrderPrice)
if err != nil {
panic(err)
}

return sdk.NewEvent(TypeEvtExecuteLimitSellSpotOrder,
sdk.NewAttribute("order_type", order.OrderType.String()),
sdk.NewAttribute("order_id", strconv.FormatInt(int64(order.OrderId), 10)),
sdk.NewAttribute("order_price", string(orderPrice)),
sdk.NewAttribute("order_amount", order.OrderAmount.String()),
sdk.NewAttribute("owner_address", order.OwnerAddress),
sdk.NewAttribute("order_target_denom", order.OrderTargetDenom),
sdk.NewAttribute("date", order.Date.String()),
sdk.NewAttribute("amount", res.Amount.String()),
sdk.NewAttribute("spot_price", res.SpotPrice.String()),
sdk.NewAttribute("swap_fee", res.SwapFee.String()),
sdk.NewAttribute("discount", res.Discount.String()),
sdk.NewAttribute("recipient", res.Recipient),
)
}

func NewExecuteStopLossSpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomResponse) sdk.Event {
// convert order price to json string
orderPrice, err := json.Marshal(order.OrderPrice)
if err != nil {
panic(err)
}

return sdk.NewEvent(TypeEvtExecuteStopLossSpotOrder,
sdk.NewAttribute("order_type", order.OrderType.String()),
sdk.NewAttribute("order_id", strconv.FormatInt(int64(order.OrderId), 10)),
sdk.NewAttribute("order_price", string(orderPrice)),
sdk.NewAttribute("order_amount", order.OrderAmount.String()),
sdk.NewAttribute("owner_address", order.OwnerAddress),
sdk.NewAttribute("order_target_denom", order.OrderTargetDenom),
sdk.NewAttribute("date", order.Date.String()),
sdk.NewAttribute("amount", res.Amount.String()),
sdk.NewAttribute("spot_price", res.SpotPrice.String()),
sdk.NewAttribute("swap_fee", res.SwapFee.String()),
sdk.NewAttribute("discount", res.Discount.String()),
sdk.NewAttribute("recipient", res.Recipient),
)
}

func NewExecuteMarketBuySpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomResponse) sdk.Event {
// convert order price to json string
orderPrice, err := json.Marshal(order.OrderPrice)
if err != nil {
panic(err)
}

return sdk.NewEvent(TypeEvtExecuteMarketBuySpotOrder,
sdk.NewAttribute("order_type", order.OrderType.String()),
sdk.NewAttribute("order_id", strconv.FormatInt(int64(order.OrderId), 10)),
sdk.NewAttribute("order_price", string(orderPrice)),
Expand Down

0 comments on commit 0399c52

Please sign in to comment.