Skip to content

Commit

Permalink
Enhance event validation and serialization in order execution tests
Browse files Browse the repository at this point in the history
- Improved the TestMsgServerExecuteOrder in msg_server_execute_order_test.go by adding event validation to ensure the correct execution of limit open perpetual orders.
- Updated the NewExecuteLimitOpenPerpetualOrderEvt function in events.go to serialize the trigger price as a JSON string for better clarity in emitted events.

These changes strengthen the testing framework and improve the accuracy of event emissions in the Tradeshield module.
  • Loading branch information
cosmic-vagabond committed Dec 10, 2024
1 parent 1005eea commit c5daaff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
30 changes: 29 additions & 1 deletion x/tradeshield/keeper/msg_server_execute_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,35 @@ func (suite *TradeshieldKeeperTestSuite) TestMsgServerExecuteOrder() {
PerpetualOrderIds: []uint64{1},
}
},
func() {},
func() {
// Get events from context
events := suite.ctx.EventManager().Events()

// Find the specific event we're looking for
var foundEvent sdk.Event
for _, event := range events {
if event.Type == types.TypeEvtExecuteLimitOpenPerpetualOrder {
foundEvent = event
break
}
}

// Assert event was emitted
suite.Require().NotNil(foundEvent)

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

// Check specific attributes
for _, attr := range foundEvent.Attributes {
switch string(attr.Key) {
case "order_id":
suite.Require().Equal("1", string(attr.Value))
case "trigger_price":
suite.Require().Equal(string(attr.Value), "{\"trading_asset_denom\":\"uatom\",\"rate\":\"10.000000000000000000\"}")
}
}
},
},
}

Expand Down
8 changes: 7 additions & 1 deletion x/tradeshield/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ func NewExecuteSpotOrderEvt(order SpotOrder, res *ammtypes.MsgSwapByDenomRespons
}

func NewExecuteLimitOpenPerpetualOrderEvt(order PerpetualOrder, positionId uint64) sdk.Event {
// convert trigger price to json string
triggerPrice, err := json.Marshal(order.TriggerPrice)
if err != nil {
panic(err)
}

return sdk.NewEvent(TypeEvtExecuteLimitOpenPerpetualOrder,
sdk.NewAttribute("order_id", strconv.FormatInt(int64(order.OrderId), 10)),
sdk.NewAttribute("owner_address", order.OwnerAddress),
sdk.NewAttribute("order_type", order.PerpetualOrderType.String()),
sdk.NewAttribute("position", order.Position.String()),
sdk.NewAttribute("position_id", strconv.FormatInt(int64(positionId), 10)),
sdk.NewAttribute("trigger_price", order.TriggerPrice.String()),
sdk.NewAttribute("trigger_price", string(triggerPrice)),
)
}

0 comments on commit c5daaff

Please sign in to comment.